OSDN Git Service

2010-09-30 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / sdbout.c
1 /* Output sdb-format symbol table information from GNU compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /*  mike@tredysvr.Tredydev.Unisys.COM says:
23 I modified the struct.c example and have a nm of a .o resulting from the
24 AT&T C compiler.  From the example below I would conclude the following:
25
26 1. All .defs from structures are emitted as scanned.  The example below
27    clearly shows the symbol table entries for BoxRec2 are after the first
28    function.
29
30 2. All functions and their locals (including statics) are emitted as scanned.
31
32 3. All nested unnamed union and structure .defs must be emitted before
33    the structure in which they are nested.  The AT&T assembler is a
34    one pass beast as far as symbolics are concerned.
35
36 4. All structure .defs are emitted before the typedefs that refer to them.
37
38 5. All top level static and external variable definitions are moved to the
39    end of file with all top level statics occurring first before externs.
40
41 6. All undefined references are at the end of the file.
42 */
43
44 #include "config.h"
45 #include "system.h"
46 #include "coretypes.h"
47 #include "tm.h"
48 #include "debug.h"
49 #include "tree.h"
50 #include "ggc.h"
51 #include "vec.h"
52
53 static GTY(()) tree anonymous_types;
54
55 /* Counter to generate unique "names" for nameless struct members.  */
56
57 static GTY(()) int unnamed_struct_number;
58
59 /* Declarations whose debug info was deferred till end of compilation.  */
60
61 static GTY(()) VEC(tree,gc) *deferred_global_decls;
62
63 /* The C front end may call sdbout_symbol before sdbout_init runs.
64    We save all such decls in this list and output them when we get
65    to sdbout_init.  */
66
67 static GTY(()) tree preinit_symbols;
68 static GTY(()) bool sdbout_initialized;
69
70 #ifdef SDB_DEBUGGING_INFO
71
72 #include "rtl.h"
73 #include "regs.h"
74 #include "flags.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "diagnostic-core.h"
79 #include "toplev.h"
80 #include "tm_p.h"
81 #include "gsyms.h"
82 #include "langhooks.h"
83 #include "target.h"
84
85 /* 1 if PARM is passed to this function in memory.  */
86
87 #define PARM_PASSED_IN_MEMORY(PARM) \
88  (MEM_P (DECL_INCOMING_RTL (PARM)))
89
90 /* A C expression for the integer offset value of an automatic variable
91    (C_AUTO) having address X (an RTX).  */
92 #ifndef DEBUGGER_AUTO_OFFSET
93 #define DEBUGGER_AUTO_OFFSET(X) \
94   (GET_CODE (X) == PLUS ? INTVAL (XEXP (X, 1)) : 0)
95 #endif
96
97 /* A C expression for the integer offset value of an argument (C_ARG)
98    having address X (an RTX).  The nominal offset is OFFSET.  */
99 #ifndef DEBUGGER_ARG_OFFSET
100 #define DEBUGGER_ARG_OFFSET(OFFSET, X) (OFFSET)
101 #endif
102
103 /* Line number of beginning of current function, minus one.
104    Negative means not in a function or not using sdb.  */
105
106 int sdb_begin_function_line = -1;
107
108
109 extern FILE *asm_out_file;
110
111 extern tree current_function_decl;
112
113 #include "sdbout.h"
114
115 static void sdbout_init                 (const char *);
116 static void sdbout_finish               (const char *);
117 static void sdbout_start_source_file    (unsigned int, const char *);
118 static void sdbout_end_source_file      (unsigned int);
119 static void sdbout_begin_block          (unsigned int, unsigned int);
120 static void sdbout_end_block            (unsigned int, unsigned int);
121 static void sdbout_source_line          (unsigned int, const char *, int, bool);
122 static void sdbout_end_epilogue         (unsigned int, const char *);
123 static void sdbout_global_decl          (tree);
124 #ifndef MIPS_DEBUGGING_INFO
125 static void sdbout_begin_prologue       (unsigned int, const char *);
126 #endif
127 static void sdbout_end_prologue         (unsigned int, const char *);
128 static void sdbout_begin_function       (tree);
129 static void sdbout_end_function         (unsigned int);
130 static void sdbout_toplevel_data        (tree);
131 static void sdbout_label                (rtx);
132 static char *gen_fake_label             (void);
133 static int plain_type                   (tree);
134 static int template_name_p              (tree);
135 static void sdbout_record_type_name     (tree);
136 static int plain_type_1                 (tree, int);
137 static void sdbout_block                (tree);
138 static void sdbout_syms                 (tree);
139 #ifdef SDB_ALLOW_FORWARD_REFERENCES
140 static void sdbout_queue_anonymous_type (tree);
141 static void sdbout_dequeue_anonymous_types (void);
142 #endif
143 static void sdbout_type                 (tree);
144 static void sdbout_field_types          (tree);
145 static void sdbout_one_type             (tree);
146 static void sdbout_parms                (tree);
147 static void sdbout_reg_parms            (tree);
148 static void sdbout_global_decl          (tree);
149
150 /* Random macros describing parts of SDB data.  */
151
152 /* Default value of delimiter is ";".  */
153 #ifndef SDB_DELIM
154 #define SDB_DELIM       ";"
155 #endif
156
157 /* Maximum number of dimensions the assembler will allow.  */
158 #ifndef SDB_MAX_DIM
159 #define SDB_MAX_DIM 4
160 #endif
161
162 #ifndef PUT_SDB_SCL
163 #define PUT_SDB_SCL(a) fprintf(asm_out_file, "\t.scl\t%d%s", (a), SDB_DELIM)
164 #endif
165
166 #ifndef PUT_SDB_INT_VAL
167 #define PUT_SDB_INT_VAL(a) \
168  do {                                                                   \
169    fprintf (asm_out_file, "\t.val\t" HOST_WIDE_INT_PRINT_DEC "%s",      \
170             (HOST_WIDE_INT) (a), SDB_DELIM);                            \
171  } while (0)
172
173 #endif
174
175 #ifndef PUT_SDB_VAL
176 #define PUT_SDB_VAL(a)                          \
177 ( fputs ("\t.val\t", asm_out_file),             \
178   output_addr_const (asm_out_file, (a)),        \
179   fprintf (asm_out_file, SDB_DELIM))
180 #endif
181
182 #ifndef PUT_SDB_DEF
183 #define PUT_SDB_DEF(a)                          \
184 do { fprintf (asm_out_file, "\t.def\t");        \
185      assemble_name (asm_out_file, a);   \
186      fprintf (asm_out_file, SDB_DELIM); } while (0)
187 #endif
188
189 #ifndef PUT_SDB_PLAIN_DEF
190 #define PUT_SDB_PLAIN_DEF(a) fprintf(asm_out_file,"\t.def\t.%s%s",a, SDB_DELIM)
191 #endif
192
193 #ifndef PUT_SDB_ENDEF
194 #define PUT_SDB_ENDEF fputs("\t.endef\n", asm_out_file)
195 #endif
196
197 #ifndef PUT_SDB_TYPE
198 #define PUT_SDB_TYPE(a) fprintf(asm_out_file, "\t.type\t0%o%s", a, SDB_DELIM)
199 #endif
200
201 #ifndef PUT_SDB_SIZE
202 #define PUT_SDB_SIZE(a) \
203  do {                                                                   \
204    fprintf (asm_out_file, "\t.size\t" HOST_WIDE_INT_PRINT_DEC "%s",     \
205             (HOST_WIDE_INT) (a), SDB_DELIM);                            \
206  } while(0)
207 #endif
208
209 #ifndef PUT_SDB_START_DIM
210 #define PUT_SDB_START_DIM fprintf(asm_out_file, "\t.dim\t")
211 #endif
212
213 #ifndef PUT_SDB_NEXT_DIM
214 #define PUT_SDB_NEXT_DIM(a) fprintf(asm_out_file, "%d,", a)
215 #endif
216
217 #ifndef PUT_SDB_LAST_DIM
218 #define PUT_SDB_LAST_DIM(a) fprintf(asm_out_file, "%d%s", a, SDB_DELIM)
219 #endif
220
221 #ifndef PUT_SDB_TAG
222 #define PUT_SDB_TAG(a)                          \
223 do { fprintf (asm_out_file, "\t.tag\t");        \
224      assemble_name (asm_out_file, a);   \
225      fprintf (asm_out_file, SDB_DELIM); } while (0)
226 #endif
227
228 #ifndef PUT_SDB_BLOCK_START
229 #define PUT_SDB_BLOCK_START(LINE)               \
230   fprintf (asm_out_file,                        \
231            "\t.def\t.bb%s\t.val\t.%s\t.scl\t100%s\t.line\t%d%s\t.endef\n", \
232            SDB_DELIM, SDB_DELIM, SDB_DELIM, (LINE), SDB_DELIM)
233 #endif
234
235 #ifndef PUT_SDB_BLOCK_END
236 #define PUT_SDB_BLOCK_END(LINE)                 \
237   fprintf (asm_out_file,                        \
238            "\t.def\t.eb%s\t.val\t.%s\t.scl\t100%s\t.line\t%d%s\t.endef\n",  \
239            SDB_DELIM, SDB_DELIM, SDB_DELIM, (LINE), SDB_DELIM)
240 #endif
241
242 #ifndef PUT_SDB_FUNCTION_START
243 #define PUT_SDB_FUNCTION_START(LINE)            \
244   fprintf (asm_out_file,                        \
245            "\t.def\t.bf%s\t.val\t.%s\t.scl\t101%s\t.line\t%d%s\t.endef\n", \
246            SDB_DELIM, SDB_DELIM, SDB_DELIM, (LINE), SDB_DELIM)
247 #endif
248
249 #ifndef PUT_SDB_FUNCTION_END
250 #define PUT_SDB_FUNCTION_END(LINE)              \
251   fprintf (asm_out_file,                        \
252            "\t.def\t.ef%s\t.val\t.%s\t.scl\t101%s\t.line\t%d%s\t.endef\n", \
253            SDB_DELIM, SDB_DELIM, SDB_DELIM, (LINE), SDB_DELIM)
254 #endif
255
256 /* Return the sdb tag identifier string for TYPE
257    if TYPE has already been defined; otherwise return a null pointer.  */
258
259 #define KNOWN_TYPE_TAG(type)  TYPE_SYMTAB_POINTER (type)
260
261 /* Set the sdb tag identifier string for TYPE to NAME.  */
262
263 #define SET_KNOWN_TYPE_TAG(TYPE, NAME) \
264   TYPE_SYMTAB_POINTER (TYPE) = (const char *)(NAME)
265
266 /* Return the name (a string) of the struct, union or enum tag
267    described by the TREE_LIST node LINK.  This is 0 for an anonymous one.  */
268
269 #define TAG_NAME(link) \
270   (((link) && TREE_PURPOSE ((link)) \
271     && IDENTIFIER_POINTER (TREE_PURPOSE ((link)))) \
272    ? IDENTIFIER_POINTER (TREE_PURPOSE ((link))) : (char *) 0)
273
274 /* Ensure we don't output a negative line number.  */
275 #define MAKE_LINE_SAFE(line)  \
276   if ((int) line <= sdb_begin_function_line) \
277     line = sdb_begin_function_line + 1
278
279 /* Perform linker optimization of merging header file definitions together
280    for targets with MIPS_DEBUGGING_INFO defined.  This won't work without a
281    post 960826 version of GAS.  Nothing breaks with earlier versions of GAS,
282    the optimization just won't be done.  The native assembler already has the
283    necessary support.  */
284
285 #ifdef MIPS_DEBUGGING_INFO
286
287 /* ECOFF linkers have an optimization that does the same kind of thing as
288    N_BINCL/E_INCL in stabs: eliminate duplicate debug information in the
289    executable.  To achieve this, GCC must output a .file for each file
290    name change.  */
291
292 /* This is a stack of input files.  */
293
294 struct sdb_file
295 {
296   struct sdb_file *next;
297   const char *name;
298 };
299
300 /* This is the top of the stack.  */
301
302 static struct sdb_file *current_file;
303
304 #endif /* MIPS_DEBUGGING_INFO */
305
306 /* The debug hooks structure.  */
307 const struct gcc_debug_hooks sdb_debug_hooks =
308 {
309   sdbout_init,                           /* init */
310   sdbout_finish,                         /* finish */
311   debug_nothing_void,                    /* assembly_start */
312   debug_nothing_int_charstar,            /* define */
313   debug_nothing_int_charstar,            /* undef */
314   sdbout_start_source_file,              /* start_source_file */
315   sdbout_end_source_file,                /* end_source_file */
316   sdbout_begin_block,                    /* begin_block */
317   sdbout_end_block,                      /* end_block */
318   debug_true_const_tree,                 /* ignore_block */
319   sdbout_source_line,                    /* source_line */
320 #ifdef MIPS_DEBUGGING_INFO
321   /* Defer on MIPS systems so that parameter descriptions follow
322      function entry.  */
323   debug_nothing_int_charstar,            /* begin_prologue */
324   sdbout_end_prologue,                   /* end_prologue */
325 #else
326   sdbout_begin_prologue,                 /* begin_prologue */
327   debug_nothing_int_charstar,            /* end_prologue */
328 #endif
329   debug_nothing_int_charstar,            /* begin_epilogue */
330   sdbout_end_epilogue,                   /* end_epilogue */
331   sdbout_begin_function,                 /* begin_function */
332   sdbout_end_function,                   /* end_function */
333   debug_nothing_tree,                    /* function_decl */
334   sdbout_global_decl,                    /* global_decl */
335   sdbout_symbol,                         /* type_decl */
336   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
337   debug_nothing_tree,                    /* deferred_inline_function */
338   debug_nothing_tree,                    /* outlining_inline_function */
339   sdbout_label,                          /* label */
340   debug_nothing_int,                     /* handle_pch */
341   debug_nothing_rtx,                     /* var_location */
342   debug_nothing_void,                    /* switch_text_section */
343   debug_nothing_tree,                    /* direct_call */
344   debug_nothing_tree_int,                /* virtual_call_token */
345   debug_nothing_rtx_rtx,                 /* copy_call_info */
346   debug_nothing_uid,                     /* virtual_call */
347   debug_nothing_tree_tree,               /* set_name */
348   0                                      /* start_end_main_source_file */
349 };
350
351 /* Return a unique string to name an anonymous type.  */
352
353 static char *
354 gen_fake_label (void)
355 {
356   char label[10];
357   char *labelstr;
358   sprintf (label, ".%dfake", unnamed_struct_number);
359   unnamed_struct_number++;
360   labelstr = xstrdup (label);
361   return labelstr;
362 }
363
364 /* Return the number which describes TYPE for SDB.
365    For pointers, etc., this function is recursive.
366    Each record, union or enumeral type must already have had a
367    tag number output.  */
368
369 /* The number is given by d6d5d4d3d2d1bbbb
370    where bbbb is 4 bit basic type, and di indicate  one of notype,ptr,fn,array.
371    Thus, char *foo () has bbbb=T_CHAR
372                           d1=D_FCN
373                           d2=D_PTR
374  N_BTMASK=     017       1111     basic type field.
375  N_TSHIFT=       2                derived type shift
376  N_BTSHFT=       4                Basic type shift */
377
378 /* Produce the number that describes a pointer, function or array type.
379    PREV is the number describing the target, value or element type.
380    DT_type describes how to transform that type.  */
381 #define PUSH_DERIVED_LEVEL(DT_type,PREV)                \
382   ((((PREV) & ~(int) N_BTMASK) << (int) N_TSHIFT)               \
383    | ((int) DT_type << (int) N_BTSHFT)                  \
384    | ((PREV) & (int) N_BTMASK))
385
386 /* Number of elements used in sdb_dims.  */
387 static int sdb_n_dims = 0;
388
389 /* Table of array dimensions of current type.  */
390 static int sdb_dims[SDB_MAX_DIM];
391
392 /* Size of outermost array currently being processed.  */
393 static int sdb_type_size = -1;
394
395 static int
396 plain_type (tree type)
397 {
398   int val = plain_type_1 (type, 0);
399
400   /* If we have already saved up some array dimensions, print them now.  */
401   if (sdb_n_dims > 0)
402     {
403       int i;
404       PUT_SDB_START_DIM;
405       for (i = sdb_n_dims - 1; i > 0; i--)
406         PUT_SDB_NEXT_DIM (sdb_dims[i]);
407       PUT_SDB_LAST_DIM (sdb_dims[0]);
408       sdb_n_dims = 0;
409
410       sdb_type_size = int_size_in_bytes (type);
411       /* Don't kill sdb if type is not laid out or has variable size.  */
412       if (sdb_type_size < 0)
413         sdb_type_size = 0;
414     }
415   /* If we have computed the size of an array containing this type,
416      print it now.  */
417   if (sdb_type_size >= 0)
418     {
419       PUT_SDB_SIZE (sdb_type_size);
420       sdb_type_size = -1;
421     }
422   return val;
423 }
424
425 static int
426 template_name_p (tree name)
427 {
428   const char *ptr = IDENTIFIER_POINTER (name);
429   while (*ptr && *ptr != '<')
430     ptr++;
431
432   return *ptr != '\0';
433 }
434
435 static void
436 sdbout_record_type_name (tree type)
437 {
438   const char *name = 0;
439   int no_name;
440
441   if (KNOWN_TYPE_TAG (type))
442     return;
443
444   if (TYPE_NAME (type) != 0)
445     {
446       tree t = 0;
447
448       /* Find the IDENTIFIER_NODE for the type name.  */
449       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
450         t = TYPE_NAME (type);
451       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
452         {
453           t = DECL_NAME (TYPE_NAME (type));
454           /* The DECL_NAME for templates includes "<>", which breaks
455              most assemblers.  Use its assembler name instead, which
456              has been mangled into being safe.  */
457           if (t && template_name_p (t))
458             t = DECL_ASSEMBLER_NAME (TYPE_NAME (type));
459         }
460
461       /* Now get the name as a string, or invent one.  */
462       if (t != NULL_TREE)
463         name = IDENTIFIER_POINTER (t);
464     }
465
466   no_name = (name == 0 || *name == 0);
467   if (no_name)
468     name = gen_fake_label ();
469
470   SET_KNOWN_TYPE_TAG (type, name);
471 #ifdef SDB_ALLOW_FORWARD_REFERENCES
472   if (no_name)
473     sdbout_queue_anonymous_type (type);
474 #endif
475 }
476
477 /* Return the .type value for type TYPE.
478
479    LEVEL indicates how many levels deep we have recursed into the type.
480    The SDB debug format can only represent 6 derived levels of types.
481    After that, we must output inaccurate debug info.  We deliberately
482    stop before the 7th level, so that ADA recursive types will not give an
483    infinite loop.  */
484
485 static int
486 plain_type_1 (tree type, int level)
487 {
488   if (type == 0)
489     type = void_type_node;
490   else if (type == error_mark_node)
491     type = integer_type_node;
492   else
493     type = TYPE_MAIN_VARIANT (type);
494
495   switch (TREE_CODE (type))
496     {
497     case VOID_TYPE:
498       return T_VOID;
499     case BOOLEAN_TYPE:
500     case INTEGER_TYPE:
501       {
502         int size = int_size_in_bytes (type) * BITS_PER_UNIT;
503
504         /* Carefully distinguish all the standard types of C,
505            without messing up if the language is not C.
506            Note that we check only for the names that contain spaces;
507            other names might occur by coincidence in other languages.  */
508         if (TYPE_NAME (type) != 0
509             && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
510             && DECL_NAME (TYPE_NAME (type)) != 0
511             && TREE_CODE (DECL_NAME (TYPE_NAME (type))) == IDENTIFIER_NODE)
512           {
513             const char *const name
514               = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
515
516             if (!strcmp (name, "char"))
517               return T_CHAR;
518             if (!strcmp (name, "unsigned char"))
519               return T_UCHAR;
520             if (!strcmp (name, "signed char"))
521               return T_CHAR;
522             if (!strcmp (name, "int"))
523               return T_INT;
524             if (!strcmp (name, "unsigned int"))
525               return T_UINT;
526             if (!strcmp (name, "short int"))
527               return T_SHORT;
528             if (!strcmp (name, "short unsigned int"))
529               return T_USHORT;
530             if (!strcmp (name, "long int"))
531               return T_LONG;
532             if (!strcmp (name, "long unsigned int"))
533               return T_ULONG;
534           }
535
536         if (size == INT_TYPE_SIZE)
537           return (TYPE_UNSIGNED (type) ? T_UINT : T_INT);
538         if (size == CHAR_TYPE_SIZE)
539           return (TYPE_UNSIGNED (type) ? T_UCHAR : T_CHAR);
540         if (size == SHORT_TYPE_SIZE)
541           return (TYPE_UNSIGNED (type) ? T_USHORT : T_SHORT);
542         if (size == LONG_TYPE_SIZE)
543           return (TYPE_UNSIGNED (type) ? T_ULONG : T_LONG);
544         if (size == LONG_LONG_TYPE_SIZE)        /* better than nothing */
545           return (TYPE_UNSIGNED (type) ? T_ULONG : T_LONG);
546         return 0;
547       }
548
549     case REAL_TYPE:
550       {
551         int precision = TYPE_PRECISION (type);
552         if (precision == FLOAT_TYPE_SIZE)
553           return T_FLOAT;
554         if (precision == DOUBLE_TYPE_SIZE)
555           return T_DOUBLE;
556 #ifdef EXTENDED_SDB_BASIC_TYPES
557         if (precision == LONG_DOUBLE_TYPE_SIZE)
558           return T_LNGDBL;
559 #else
560         if (precision == LONG_DOUBLE_TYPE_SIZE)
561           return T_DOUBLE;      /* better than nothing */
562 #endif
563         return 0;
564       }
565
566     case ARRAY_TYPE:
567       {
568         int m;
569         if (level >= 6)
570           return T_VOID;
571         else
572           m = plain_type_1 (TREE_TYPE (type), level+1);
573         if (sdb_n_dims < SDB_MAX_DIM)
574           sdb_dims[sdb_n_dims++]
575             = (TYPE_DOMAIN (type)
576                && TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != 0
577                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != 0
578                && host_integerp (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 0)
579                && host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0)
580                ? (tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 0)
581                   - tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0) + 1)
582                : 0);
583
584         return PUSH_DERIVED_LEVEL (DT_ARY, m);
585       }
586
587     case RECORD_TYPE:
588     case UNION_TYPE:
589     case QUAL_UNION_TYPE:
590     case ENUMERAL_TYPE:
591       {
592         const char *tag;
593 #ifdef SDB_ALLOW_FORWARD_REFERENCES
594         sdbout_record_type_name (type);
595 #endif
596 #ifndef SDB_ALLOW_UNKNOWN_REFERENCES
597         if ((TREE_ASM_WRITTEN (type) && KNOWN_TYPE_TAG (type) != 0)
598 #ifdef SDB_ALLOW_FORWARD_REFERENCES
599             || TYPE_MODE (type) != VOIDmode
600 #endif
601             )
602 #endif
603           {
604             /* Output the referenced structure tag name
605                only if the .def has already been finished.
606                At least on 386, the Unix assembler
607                cannot handle forward references to tags.  */
608             /* But the 88100, it requires them, sigh...  */
609             /* And the MIPS requires unknown refs as well...  */
610             tag = KNOWN_TYPE_TAG (type);
611             PUT_SDB_TAG (tag);
612             /* These 3 lines used to follow the close brace.
613                However, a size of 0 without a tag implies a tag of 0,
614                so if we don't know a tag, we can't mention the size.  */
615             sdb_type_size = int_size_in_bytes (type);
616             if (sdb_type_size < 0)
617               sdb_type_size = 0;
618           }
619         return ((TREE_CODE (type) == RECORD_TYPE) ? T_STRUCT
620                 : (TREE_CODE (type) == UNION_TYPE) ? T_UNION
621                 : (TREE_CODE (type) == QUAL_UNION_TYPE) ? T_UNION
622                 : T_ENUM);
623       }
624     case POINTER_TYPE:
625     case REFERENCE_TYPE:
626       {
627         int m;
628         if (level >= 6)
629           return T_VOID;
630         else
631           m = plain_type_1 (TREE_TYPE (type), level+1);
632         return PUSH_DERIVED_LEVEL (DT_PTR, m);
633       }
634     case FUNCTION_TYPE:
635     case METHOD_TYPE:
636       {
637         int m;
638         if (level >= 6)
639           return T_VOID;
640         else
641           m = plain_type_1 (TREE_TYPE (type), level+1);
642         return PUSH_DERIVED_LEVEL (DT_FCN, m);
643       }
644     default:
645       return 0;
646     }
647 }
648
649 /* Output the symbols defined in block number DO_BLOCK.
650
651    This function works by walking the tree structure of blocks,
652    counting blocks until it finds the desired block.  */
653
654 static int do_block = 0;
655
656 static void
657 sdbout_block (tree block)
658 {
659   while (block)
660     {
661       /* Ignore blocks never expanded or otherwise marked as real.  */
662       if (TREE_USED (block))
663         {
664           /* When we reach the specified block, output its symbols.  */
665           if (BLOCK_NUMBER (block) == do_block)
666             sdbout_syms (BLOCK_VARS (block));
667
668           /* If we are past the specified block, stop the scan.  */
669           if (BLOCK_NUMBER (block) > do_block)
670             return;
671
672           /* Scan the blocks within this block.  */
673           sdbout_block (BLOCK_SUBBLOCKS (block));
674         }
675
676       block = BLOCK_CHAIN (block);
677     }
678 }
679
680 /* Call sdbout_symbol on each decl in the chain SYMS.  */
681
682 static void
683 sdbout_syms (tree syms)
684 {
685   while (syms)
686     {
687       if (TREE_CODE (syms) != LABEL_DECL)
688         sdbout_symbol (syms, 1);
689       syms = TREE_CHAIN (syms);
690     }
691 }
692
693 /* Output SDB information for a symbol described by DECL.
694    LOCAL is nonzero if the symbol is not file-scope.  */
695
696 void
697 sdbout_symbol (tree decl, int local)
698 {
699   tree type = TREE_TYPE (decl);
700   tree context = NULL_TREE;
701   rtx value;
702   int regno = -1;
703   const char *name;
704
705   /* If we are called before sdbout_init is run, just save the symbol
706      for later.  */
707   if (!sdbout_initialized)
708     {
709       preinit_symbols = tree_cons (0, decl, preinit_symbols);
710       return;
711     }
712
713   sdbout_one_type (type);
714
715   switch (TREE_CODE (decl))
716     {
717     case CONST_DECL:
718       /* Enum values are defined by defining the enum type.  */
719       return;
720
721     case FUNCTION_DECL:
722       /* Don't mention a nested function under its parent.  */
723       context = decl_function_context (decl);
724       if (context == current_function_decl)
725         return;
726       /* Check DECL_INITIAL to distinguish declarations from definitions.
727          Don't output debug info here for declarations; they will have
728          a DECL_INITIAL value of 0.  */
729       if (! DECL_INITIAL (decl))
730         return;
731       if (!MEM_P (DECL_RTL (decl))
732           || GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
733         return;
734       PUT_SDB_DEF (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
735       PUT_SDB_VAL (XEXP (DECL_RTL (decl), 0));
736       PUT_SDB_SCL (TREE_PUBLIC (decl) ? C_EXT : C_STAT);
737       break;
738
739     case TYPE_DECL:
740       /* Done with tagged types.  */
741       if (DECL_NAME (decl) == 0)
742         return;
743       if (DECL_IGNORED_P (decl))
744         return;
745       /* Don't output intrinsic types.  GAS chokes on SDB .def
746          statements that contain identifiers with embedded spaces
747          (eg "unsigned long").  */
748       if (DECL_IS_BUILTIN (decl))
749         return;
750
751       /* Output typedef name.  */
752       if (template_name_p (DECL_NAME (decl)))
753         PUT_SDB_DEF (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
754       else
755         PUT_SDB_DEF (IDENTIFIER_POINTER (DECL_NAME (decl)));
756       PUT_SDB_SCL (C_TPDEF);
757       break;
758
759     case PARM_DECL:
760       /* Parm decls go in their own separate chains
761          and are output by sdbout_reg_parms and sdbout_parms.  */
762       gcc_unreachable ();
763
764     case VAR_DECL:
765       /* Don't mention a variable that is external.
766          Let the file that defines it describe it.  */
767       if (DECL_EXTERNAL (decl))
768         return;
769
770       /* Ignore __FUNCTION__, etc.  */
771       if (DECL_IGNORED_P (decl))
772         return;
773
774       /* If there was an error in the declaration, don't dump core
775          if there is no RTL associated with the variable doesn't
776          exist.  */
777       if (!DECL_RTL_SET_P (decl))
778         return;
779
780       SET_DECL_RTL (decl,
781                     eliminate_regs (DECL_RTL (decl), VOIDmode, NULL_RTX));
782 #ifdef LEAF_REG_REMAP
783       if (current_function_uses_only_leaf_regs)
784         leaf_renumber_regs_insn (DECL_RTL (decl));
785 #endif
786       value = DECL_RTL (decl);
787
788       /* Don't mention a variable at all
789          if it was completely optimized into nothingness.
790
791          If DECL was from an inline function, then its rtl
792          is not identically the rtl that was used in this
793          particular compilation.  */
794       if (REG_P (value))
795         {
796           regno = REGNO (value);
797           if (regno >= FIRST_PSEUDO_REGISTER)
798             return;
799         }
800       else if (GET_CODE (value) == SUBREG)
801         {
802           while (GET_CODE (value) == SUBREG)
803             value = SUBREG_REG (value);
804           if (REG_P (value))
805             {
806               if (REGNO (value) >= FIRST_PSEUDO_REGISTER)
807                 return;
808             }
809           regno = REGNO (alter_subreg (&value));
810           SET_DECL_RTL (decl, value);
811         }
812       /* Don't output anything if an auto variable
813          gets RTL that is static.
814          GAS version 2.2 can't handle such output.  */
815       else if (MEM_P (value) && CONSTANT_P (XEXP (value, 0))
816                && ! TREE_STATIC (decl))
817         return;
818
819       /* Emit any structure, union, or enum type that has not been output.
820          This occurs for tag-less structs (et al) used to declare variables
821          within functions.  */
822       if (TREE_CODE (type) == ENUMERAL_TYPE
823           || TREE_CODE (type) == RECORD_TYPE
824           || TREE_CODE (type) == UNION_TYPE
825           || TREE_CODE (type) == QUAL_UNION_TYPE)
826         {
827           if (COMPLETE_TYPE_P (type)            /* not a forward reference */
828               && KNOWN_TYPE_TAG (type) == 0)    /* not yet declared */
829             sdbout_one_type (type);
830         }
831
832       /* Defer SDB information for top-level initialized variables! */
833       if (! local
834           && MEM_P (value)
835           && DECL_INITIAL (decl))
836         return;
837
838       /* C++ in 2.3 makes nameless symbols.  That will be fixed later.
839          For now, avoid crashing.  */
840       if (DECL_NAME (decl) == NULL_TREE)
841         return;
842
843       /* Record the name for, starting a symtab entry.  */
844       if (local)
845         name = IDENTIFIER_POINTER (DECL_NAME (decl));
846       else
847         name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
848
849       if (MEM_P (value)
850           && GET_CODE (XEXP (value, 0)) == SYMBOL_REF)
851         {
852           PUT_SDB_DEF (name);
853           if (TREE_PUBLIC (decl))
854             {
855               PUT_SDB_VAL (XEXP (value, 0));
856               PUT_SDB_SCL (C_EXT);
857             }
858           else
859             {
860               PUT_SDB_VAL (XEXP (value, 0));
861               PUT_SDB_SCL (C_STAT);
862             }
863         }
864       else if (regno >= 0)
865         {
866           PUT_SDB_DEF (name);
867           PUT_SDB_INT_VAL (DBX_REGISTER_NUMBER (regno));
868           PUT_SDB_SCL (C_REG);
869         }
870       else if (MEM_P (value)
871                && (MEM_P (XEXP (value, 0))
872                    || (REG_P (XEXP (value, 0))
873                        && REGNO (XEXP (value, 0)) != HARD_FRAME_POINTER_REGNUM
874                        && REGNO (XEXP (value, 0)) != STACK_POINTER_REGNUM)))
875         /* If the value is indirect by memory or by a register
876            that isn't the frame pointer
877            then it means the object is variable-sized and address through
878            that register or stack slot.  COFF has no way to represent this
879            so all we can do is output the variable as a pointer.  */
880         {
881           PUT_SDB_DEF (name);
882           if (REG_P (XEXP (value, 0)))
883             {
884               PUT_SDB_INT_VAL (DBX_REGISTER_NUMBER (REGNO (XEXP (value, 0))));
885               PUT_SDB_SCL (C_REG);
886             }
887           else
888             {
889               /* DECL_RTL looks like (MEM (MEM (PLUS (REG...)
890                  (CONST_INT...)))).
891                  We want the value of that CONST_INT.  */
892               /* Encore compiler hates a newline in a macro arg, it seems.  */
893               PUT_SDB_INT_VAL (DEBUGGER_AUTO_OFFSET
894                                (XEXP (XEXP (value, 0), 0)));
895               PUT_SDB_SCL (C_AUTO);
896             }
897
898           /* Effectively do build_pointer_type, but don't cache this type,
899              since it might be temporary whereas the type it points to
900              might have been saved for inlining.  */
901           /* Don't use REFERENCE_TYPE because dbx can't handle that.  */
902           type = make_node (POINTER_TYPE);
903           TREE_TYPE (type) = TREE_TYPE (decl);
904         }
905       else if (MEM_P (value)
906                && ((GET_CODE (XEXP (value, 0)) == PLUS
907                     && REG_P (XEXP (XEXP (value, 0), 0))
908                     && CONST_INT_P (XEXP (XEXP (value, 0), 1)))
909                    /* This is for variables which are at offset zero from
910                       the frame pointer.  This happens on the Alpha.
911                       Non-frame pointer registers are excluded above.  */
912                    || (REG_P (XEXP (value, 0)))))
913         {
914           /* DECL_RTL looks like (MEM (PLUS (REG...) (CONST_INT...)))
915              or (MEM (REG...)).  We want the value of that CONST_INT
916              or zero.  */
917           PUT_SDB_DEF (name);
918           PUT_SDB_INT_VAL (DEBUGGER_AUTO_OFFSET (XEXP (value, 0)));
919           PUT_SDB_SCL (C_AUTO);
920         }
921       else
922         {
923           /* It is something we don't know how to represent for SDB.  */
924           return;
925         }
926       break;
927
928     default:
929       break;
930     }
931   PUT_SDB_TYPE (plain_type (type));
932   PUT_SDB_ENDEF;
933 }
934
935 /* Output SDB information for a top-level initialized variable
936    that has been delayed.  */
937
938 static void
939 sdbout_toplevel_data (tree decl)
940 {
941   tree type = TREE_TYPE (decl);
942
943   if (DECL_IGNORED_P (decl))
944     return;
945
946   gcc_assert (TREE_CODE (decl) == VAR_DECL);
947   gcc_assert (MEM_P (DECL_RTL (decl)));
948   gcc_assert (DECL_INITIAL (decl));
949
950   PUT_SDB_DEF (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
951   PUT_SDB_VAL (XEXP (DECL_RTL (decl), 0));
952   if (TREE_PUBLIC (decl))
953     {
954       PUT_SDB_SCL (C_EXT);
955     }
956   else
957     {
958       PUT_SDB_SCL (C_STAT);
959     }
960   PUT_SDB_TYPE (plain_type (type));
961   PUT_SDB_ENDEF;
962 }
963
964 #ifdef SDB_ALLOW_FORWARD_REFERENCES
965
966 /* Machinery to record and output anonymous types.  */
967
968 static void
969 sdbout_queue_anonymous_type (tree type)
970 {
971   anonymous_types = tree_cons (NULL_TREE, type, anonymous_types);
972 }
973
974 static void
975 sdbout_dequeue_anonymous_types (void)
976 {
977   tree types, link;
978
979   while (anonymous_types)
980     {
981       types = nreverse (anonymous_types);
982       anonymous_types = NULL_TREE;
983
984       for (link = types; link; link = TREE_CHAIN (link))
985         {
986           tree type = TREE_VALUE (link);
987
988           if (type && ! TREE_ASM_WRITTEN (type))
989             sdbout_one_type (type);
990         }
991     }
992 }
993
994 #endif
995
996 /* Given a chain of ..._TYPE nodes, all of which have names,
997    output definitions of those names, as typedefs.  */
998
999 void
1000 sdbout_types (tree types)
1001 {
1002   tree link;
1003
1004   for (link = types; link; link = TREE_CHAIN (link))
1005     sdbout_one_type (link);
1006
1007 #ifdef SDB_ALLOW_FORWARD_REFERENCES
1008   sdbout_dequeue_anonymous_types ();
1009 #endif
1010 }
1011
1012 static void
1013 sdbout_type (tree type)
1014 {
1015   if (type == error_mark_node)
1016     type = integer_type_node;
1017   PUT_SDB_TYPE (plain_type (type));
1018 }
1019
1020 /* Output types of the fields of type TYPE, if they are structs.
1021
1022    Formerly did not chase through pointer types, since that could be circular.
1023    They must come before TYPE, since forward refs are not allowed.
1024    Now james@bigtex.cactus.org says to try them.  */
1025
1026 static void
1027 sdbout_field_types (tree type)
1028 {
1029   tree tail;
1030
1031   for (tail = TYPE_FIELDS (type); tail; tail = TREE_CHAIN (tail))
1032     /* This condition should match the one for emitting the actual
1033        members below.  */
1034     if (TREE_CODE (tail) == FIELD_DECL
1035         && DECL_NAME (tail)
1036         && DECL_SIZE (tail)
1037         && host_integerp (DECL_SIZE (tail), 1)
1038         && host_integerp (bit_position (tail), 0))
1039       {
1040         if (POINTER_TYPE_P (TREE_TYPE (tail)))
1041           sdbout_one_type (TREE_TYPE (TREE_TYPE (tail)));
1042         else
1043           sdbout_one_type (TREE_TYPE (tail));
1044       }
1045 }
1046
1047 /* Use this to put out the top level defined record and union types
1048    for later reference.  If this is a struct with a name, then put that
1049    name out.  Other unnamed structs will have .xxfake labels generated so
1050    that they may be referred to later.
1051    The label will be stored in the KNOWN_TYPE_TAG slot of a type.
1052    It may NOT be called recursively.  */
1053
1054 static void
1055 sdbout_one_type (tree type)
1056 {
1057   if (current_function_decl != NULL_TREE
1058       && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
1059     ; /* Don't change section amid function.  */
1060   else
1061     switch_to_section (text_section);
1062
1063   switch (TREE_CODE (type))
1064     {
1065     case RECORD_TYPE:
1066     case UNION_TYPE:
1067     case QUAL_UNION_TYPE:
1068     case ENUMERAL_TYPE:
1069       type = TYPE_MAIN_VARIANT (type);
1070       /* Don't output a type twice.  */
1071       if (TREE_ASM_WRITTEN (type))
1072         /* James said test TREE_ASM_BEING_WRITTEN here.  */
1073         return;
1074
1075       /* Output nothing if type is not yet defined.  */
1076       if (!COMPLETE_TYPE_P (type))
1077         return;
1078
1079       TREE_ASM_WRITTEN (type) = 1;
1080
1081       /* This is reputed to cause trouble with the following case,
1082          but perhaps checking TYPE_SIZE above will fix it.  */
1083
1084       /* Here is a testcase:
1085
1086         struct foo {
1087           struct badstr *bbb;
1088         } forwardref;
1089
1090         typedef struct intermediate {
1091           int aaaa;
1092         } intermediate_ref;
1093
1094         typedef struct badstr {
1095           int ccccc;
1096         } badtype;   */
1097
1098       /* This change, which ought to make better output,
1099          used to make the COFF assembler unhappy.
1100          Changes involving KNOWN_TYPE_TAG may fix the problem.  */
1101       /* Before really doing anything, output types we want to refer to.  */
1102       /* Note that in version 1 the following two lines
1103          are not used if forward references are in use.  */
1104       if (TREE_CODE (type) != ENUMERAL_TYPE)
1105         sdbout_field_types (type);
1106
1107       /* Output a structure type.  */
1108       {
1109         int size = int_size_in_bytes (type);
1110         int member_scl = 0;
1111         tree tem;
1112
1113         /* Record the type tag, but not in its permanent place just yet.  */
1114         sdbout_record_type_name (type);
1115
1116         PUT_SDB_DEF (KNOWN_TYPE_TAG (type));
1117
1118         switch (TREE_CODE (type))
1119           {
1120           case UNION_TYPE:
1121           case QUAL_UNION_TYPE:
1122             PUT_SDB_SCL (C_UNTAG);
1123             PUT_SDB_TYPE (T_UNION);
1124             member_scl = C_MOU;
1125             break;
1126
1127           case RECORD_TYPE:
1128             PUT_SDB_SCL (C_STRTAG);
1129             PUT_SDB_TYPE (T_STRUCT);
1130             member_scl = C_MOS;
1131             break;
1132
1133           case ENUMERAL_TYPE:
1134             PUT_SDB_SCL (C_ENTAG);
1135             PUT_SDB_TYPE (T_ENUM);
1136             member_scl = C_MOE;
1137             break;
1138
1139           default:
1140             break;
1141           }
1142
1143         PUT_SDB_SIZE (size);
1144         PUT_SDB_ENDEF;
1145
1146         /* Print out the base class information with fields
1147            named after the types they hold.  */
1148         /* This is only relevant to aggregate types.  TYPE_BINFO is used
1149            for other purposes in an ENUMERAL_TYPE, so we must exclude that
1150            case.  */
1151         if (TREE_CODE (type) != ENUMERAL_TYPE && TYPE_BINFO (type))
1152           {
1153             int i;
1154             tree binfo, child;
1155
1156             for (binfo = TYPE_BINFO (type), i = 0;
1157                  BINFO_BASE_ITERATE (binfo, i, child); i++)
1158               {
1159                 tree child_type = BINFO_TYPE (child);
1160                 tree child_type_name;
1161
1162                 if (TYPE_NAME (child_type) == 0)
1163                   continue;
1164                 if (TREE_CODE (TYPE_NAME (child_type)) == IDENTIFIER_NODE)
1165                   child_type_name = TYPE_NAME (child_type);
1166                 else if (TREE_CODE (TYPE_NAME (child_type)) == TYPE_DECL)
1167                   {
1168                     child_type_name = DECL_NAME (TYPE_NAME (child_type));
1169                     if (child_type_name && template_name_p (child_type_name))
1170                       child_type_name
1171                         = DECL_ASSEMBLER_NAME (TYPE_NAME (child_type));
1172                   }
1173                 else
1174                   continue;
1175
1176                 PUT_SDB_DEF (IDENTIFIER_POINTER (child_type_name));
1177                 PUT_SDB_INT_VAL (tree_low_cst (BINFO_OFFSET (child), 0));
1178                 PUT_SDB_SCL (member_scl);
1179                 sdbout_type (BINFO_TYPE (child));
1180                 PUT_SDB_ENDEF;
1181               }
1182           }
1183
1184         /* Output the individual fields.  */
1185
1186         if (TREE_CODE (type) == ENUMERAL_TYPE)
1187           {
1188             for (tem = TYPE_VALUES (type); tem; tem = TREE_CHAIN (tem))
1189               {
1190                 tree value = TREE_VALUE (tem);
1191
1192                 if (TREE_CODE (value) == CONST_DECL)
1193                   value = DECL_INITIAL (value);
1194
1195                 if (host_integerp (value, 0))
1196                   {
1197                     PUT_SDB_DEF (IDENTIFIER_POINTER (TREE_PURPOSE (tem)));
1198                     PUT_SDB_INT_VAL (tree_low_cst (value, 0));
1199                     PUT_SDB_SCL (C_MOE);
1200                     PUT_SDB_TYPE (T_MOE);
1201                     PUT_SDB_ENDEF;
1202                   }
1203               }
1204           }
1205         else                    /* record or union type */
1206           for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
1207             /* Output the name, type, position (in bits), size (in bits)
1208                of each field.  */
1209
1210             /* Omit here the nameless fields that are used to skip bits.
1211                Also omit fields with variable size or position.
1212                Also omit non FIELD_DECL nodes that GNU C++ may put here.  */
1213             if (TREE_CODE (tem) == FIELD_DECL
1214                 && DECL_NAME (tem)
1215                 && DECL_SIZE (tem)
1216                 && host_integerp (DECL_SIZE (tem), 1)
1217                 && host_integerp (bit_position (tem), 0))
1218               {
1219                 const char *name;
1220
1221                 name = IDENTIFIER_POINTER (DECL_NAME (tem));
1222                 PUT_SDB_DEF (name);
1223                 if (DECL_BIT_FIELD_TYPE (tem))
1224                   {
1225                     PUT_SDB_INT_VAL (int_bit_position (tem));
1226                     PUT_SDB_SCL (C_FIELD);
1227                     sdbout_type (DECL_BIT_FIELD_TYPE (tem));
1228                     PUT_SDB_SIZE (tree_low_cst (DECL_SIZE (tem), 1));
1229                   }
1230                 else
1231                   {
1232                     PUT_SDB_INT_VAL (int_bit_position (tem) / BITS_PER_UNIT);
1233                     PUT_SDB_SCL (member_scl);
1234                     sdbout_type (TREE_TYPE (tem));
1235                   }
1236                 PUT_SDB_ENDEF;
1237               }
1238         /* Output end of a structure,union, or enumeral definition.  */
1239
1240         PUT_SDB_PLAIN_DEF ("eos");
1241         PUT_SDB_INT_VAL (size);
1242         PUT_SDB_SCL (C_EOS);
1243         PUT_SDB_TAG (KNOWN_TYPE_TAG (type));
1244         PUT_SDB_SIZE (size);
1245         PUT_SDB_ENDEF;
1246         break;
1247       }
1248
1249     default:
1250       break;
1251     }
1252 }
1253
1254 /* The following two functions output definitions of function parameters.
1255    Each parameter gets a definition locating it in the parameter list.
1256    Each parameter that is a register variable gets a second definition
1257    locating it in the register.
1258
1259    Printing or argument lists in gdb uses the definitions that
1260    locate in the parameter list.  But reference to the variable in
1261    expressions uses preferentially the definition as a register.  */
1262
1263 /* Output definitions, referring to storage in the parmlist,
1264    of all the parms in PARMS, which is a chain of PARM_DECL nodes.  */
1265
1266 static void
1267 sdbout_parms (tree parms)
1268 {
1269   for (; parms; parms = TREE_CHAIN (parms))
1270     if (DECL_NAME (parms))
1271       {
1272         int current_sym_value = 0;
1273         const char *name = IDENTIFIER_POINTER (DECL_NAME (parms));
1274
1275         if (name == 0 || *name == 0)
1276           name = gen_fake_label ();
1277
1278         /* Perform any necessary register eliminations on the parameter's rtl,
1279            so that the debugging output will be accurate.  */
1280         DECL_INCOMING_RTL (parms)
1281           = eliminate_regs (DECL_INCOMING_RTL (parms), VOIDmode, NULL_RTX);
1282         SET_DECL_RTL (parms,
1283                       eliminate_regs (DECL_RTL (parms), VOIDmode, NULL_RTX));
1284
1285         if (PARM_PASSED_IN_MEMORY (parms))
1286           {
1287             rtx addr = XEXP (DECL_INCOMING_RTL (parms), 0);
1288             tree type;
1289
1290             /* ??? Here we assume that the parm address is indexed
1291                off the frame pointer or arg pointer.
1292                If that is not true, we produce meaningless results,
1293                but do not crash.  */
1294             if (GET_CODE (addr) == PLUS
1295                 && CONST_INT_P (XEXP (addr, 1)))
1296               current_sym_value = INTVAL (XEXP (addr, 1));
1297             else
1298               current_sym_value = 0;
1299
1300             if (REG_P (DECL_RTL (parms))
1301                 && REGNO (DECL_RTL (parms)) < FIRST_PSEUDO_REGISTER)
1302               type = DECL_ARG_TYPE (parms);
1303             else
1304               {
1305                 int original_sym_value = current_sym_value;
1306
1307                 /* This is the case where the parm is passed as an int or
1308                    double and it is converted to a char, short or float
1309                    and stored back in the parmlist.  In this case, describe
1310                    the parm with the variable's declared type, and adjust
1311                    the address if the least significant bytes (which we are
1312                    using) are not the first ones.  */
1313                 if (BYTES_BIG_ENDIAN
1314                     && TREE_TYPE (parms) != DECL_ARG_TYPE (parms))
1315                   current_sym_value +=
1316                     (GET_MODE_SIZE (TYPE_MODE (DECL_ARG_TYPE (parms)))
1317                      - GET_MODE_SIZE (GET_MODE (DECL_RTL (parms))));
1318
1319                 if (MEM_P (DECL_RTL (parms))
1320                     && GET_CODE (XEXP (DECL_RTL (parms), 0)) == PLUS
1321                     && (GET_CODE (XEXP (XEXP (DECL_RTL (parms), 0), 1))
1322                         == CONST_INT)
1323                     && (INTVAL (XEXP (XEXP (DECL_RTL (parms), 0), 1))
1324                         == current_sym_value))
1325                   type = TREE_TYPE (parms);
1326                 else
1327                   {
1328                     current_sym_value = original_sym_value;
1329                     type = DECL_ARG_TYPE (parms);
1330                   }
1331               }
1332
1333             PUT_SDB_DEF (name);
1334             PUT_SDB_INT_VAL (DEBUGGER_ARG_OFFSET (current_sym_value, addr));
1335             PUT_SDB_SCL (C_ARG);
1336             PUT_SDB_TYPE (plain_type (type));
1337             PUT_SDB_ENDEF;
1338           }
1339         else if (REG_P (DECL_RTL (parms)))
1340           {
1341             rtx best_rtl;
1342             /* Parm passed in registers and lives in registers or nowhere.  */
1343
1344             /* If parm lives in a register, use that register;
1345                pretend the parm was passed there.  It would be more consistent
1346                to describe the register where the parm was passed,
1347                but in practice that register usually holds something else.  */
1348             if (REGNO (DECL_RTL (parms)) < FIRST_PSEUDO_REGISTER)
1349               best_rtl = DECL_RTL (parms);
1350             /* If the parm lives nowhere,
1351                use the register where it was passed.  */
1352             else
1353               best_rtl = DECL_INCOMING_RTL (parms);
1354
1355             PUT_SDB_DEF (name);
1356             PUT_SDB_INT_VAL (DBX_REGISTER_NUMBER (REGNO (best_rtl)));
1357             PUT_SDB_SCL (C_REGPARM);
1358             PUT_SDB_TYPE (plain_type (TREE_TYPE (parms)));
1359             PUT_SDB_ENDEF;
1360           }
1361         else if (MEM_P (DECL_RTL (parms))
1362                  && XEXP (DECL_RTL (parms), 0) != const0_rtx)
1363           {
1364             /* Parm was passed in registers but lives on the stack.  */
1365
1366             /* DECL_RTL looks like (MEM (PLUS (REG...) (CONST_INT...))),
1367                in which case we want the value of that CONST_INT,
1368                or (MEM (REG ...)) or (MEM (MEM ...)),
1369                in which case we use a value of zero.  */
1370             if (REG_P (XEXP (DECL_RTL (parms), 0))
1371                 || MEM_P (XEXP (DECL_RTL (parms), 0)))
1372               current_sym_value = 0;
1373             else
1374               current_sym_value = INTVAL (XEXP (XEXP (DECL_RTL (parms), 0), 1));
1375
1376             /* Again, this assumes the offset is based on the arg pointer.  */
1377             PUT_SDB_DEF (name);
1378             PUT_SDB_INT_VAL (DEBUGGER_ARG_OFFSET (current_sym_value,
1379                                                   XEXP (DECL_RTL (parms), 0)));
1380             PUT_SDB_SCL (C_ARG);
1381             PUT_SDB_TYPE (plain_type (TREE_TYPE (parms)));
1382             PUT_SDB_ENDEF;
1383           }
1384       }
1385 }
1386
1387 /* Output definitions for the places where parms live during the function,
1388    when different from where they were passed, when the parms were passed
1389    in memory.
1390
1391    It is not useful to do this for parms passed in registers
1392    that live during the function in different registers, because it is
1393    impossible to look in the passed register for the passed value,
1394    so we use the within-the-function register to begin with.
1395
1396    PARMS is a chain of PARM_DECL nodes.  */
1397
1398 static void
1399 sdbout_reg_parms (tree parms)
1400 {
1401   for (; parms; parms = TREE_CHAIN (parms))
1402     if (DECL_NAME (parms))
1403       {
1404         const char *name = IDENTIFIER_POINTER (DECL_NAME (parms));
1405
1406         /* Report parms that live in registers during the function
1407            but were passed in memory.  */
1408         if (REG_P (DECL_RTL (parms))
1409             && REGNO (DECL_RTL (parms)) < FIRST_PSEUDO_REGISTER
1410             && PARM_PASSED_IN_MEMORY (parms))
1411           {
1412             if (name == 0 || *name == 0)
1413               name = gen_fake_label ();
1414             PUT_SDB_DEF (name);
1415             PUT_SDB_INT_VAL (DBX_REGISTER_NUMBER (REGNO (DECL_RTL (parms))));
1416             PUT_SDB_SCL (C_REG);
1417             PUT_SDB_TYPE (plain_type (TREE_TYPE (parms)));
1418             PUT_SDB_ENDEF;
1419           }
1420         /* Report parms that live in memory but not where they were passed.  */
1421         else if (MEM_P (DECL_RTL (parms))
1422                  && GET_CODE (XEXP (DECL_RTL (parms), 0)) == PLUS
1423                  && CONST_INT_P (XEXP (XEXP (DECL_RTL (parms), 0), 1))
1424                  && PARM_PASSED_IN_MEMORY (parms)
1425                  && ! rtx_equal_p (DECL_RTL (parms), DECL_INCOMING_RTL (parms)))
1426           {
1427 #if 0 /* ??? It is not clear yet what should replace this.  */
1428             int offset = DECL_OFFSET (parms) / BITS_PER_UNIT;
1429             /* A parm declared char is really passed as an int,
1430                so it occupies the least significant bytes.
1431                On a big-endian machine those are not the low-numbered ones.  */
1432             if (BYTES_BIG_ENDIAN
1433                 && offset != -1
1434                 && TREE_TYPE (parms) != DECL_ARG_TYPE (parms))
1435               offset += (GET_MODE_SIZE (TYPE_MODE (DECL_ARG_TYPE (parms)))
1436                          - GET_MODE_SIZE (GET_MODE (DECL_RTL (parms))));
1437             if (INTVAL (XEXP (XEXP (DECL_RTL (parms), 0), 1)) != offset) {...}
1438 #endif
1439               {
1440                 if (name == 0 || *name == 0)
1441                   name = gen_fake_label ();
1442                 PUT_SDB_DEF (name);
1443                 PUT_SDB_INT_VAL (DEBUGGER_AUTO_OFFSET
1444                                  (XEXP (DECL_RTL (parms), 0)));
1445                 PUT_SDB_SCL (C_AUTO);
1446                 PUT_SDB_TYPE (plain_type (TREE_TYPE (parms)));
1447                 PUT_SDB_ENDEF;
1448               }
1449           }
1450       }
1451 }
1452
1453 /* Output debug information for a global DECL.  Called from toplev.c
1454    after compilation proper has finished.  */
1455
1456 static void
1457 sdbout_global_decl (tree decl)
1458 {
1459   if (TREE_CODE (decl) == VAR_DECL
1460       && !DECL_EXTERNAL (decl)
1461       && DECL_RTL_SET_P (decl))
1462     {
1463       /* The COFF linker can move initialized global vars to the end.
1464          And that can screw up the symbol ordering.  Defer those for
1465          sdbout_finish ().  */
1466       if (!DECL_INITIAL (decl) || !TREE_PUBLIC (decl))
1467         sdbout_symbol (decl, 0);
1468       else
1469         VEC_safe_push (tree, gc, deferred_global_decls, decl);
1470
1471       /* Output COFF information for non-global file-scope initialized
1472          variables.  */
1473       if (DECL_INITIAL (decl) && MEM_P (DECL_RTL (decl)))
1474         sdbout_toplevel_data (decl);
1475     }
1476 }
1477
1478 /* Output initialized global vars at the end, in the order of
1479    definition.  See comment in sdbout_global_decl.  */
1480
1481 static void
1482 sdbout_finish (const char *main_filename ATTRIBUTE_UNUSED)
1483 {
1484   size_t i;
1485   tree decl;
1486
1487   FOR_EACH_VEC_ELT (tree, deferred_global_decls, i, decl)
1488     sdbout_symbol (decl, 0);
1489 }
1490
1491 /* Describe the beginning of an internal block within a function.
1492    Also output descriptions of variables defined in this block.
1493
1494    N is the number of the block, by order of beginning, counting from 1,
1495    and not counting the outermost (function top-level) block.
1496    The blocks match the BLOCKs in DECL_INITIAL (current_function_decl),
1497    if the count starts at 0 for the outermost one.  */
1498
1499 static void
1500 sdbout_begin_block (unsigned int line, unsigned int n)
1501 {
1502   tree decl = current_function_decl;
1503   MAKE_LINE_SAFE (line);
1504
1505   /* The SCO compiler does not emit a separate block for the function level
1506      scope, so we avoid it here also.  However, mips ECOFF compilers do emit
1507      a separate block, so we retain it when MIPS_DEBUGGING_INFO is defined.  */
1508 #ifndef MIPS_DEBUGGING_INFO
1509   if (n != 1)
1510 #endif
1511     PUT_SDB_BLOCK_START (line - sdb_begin_function_line);
1512
1513   if (n == 1)
1514     {
1515       /* Include the outermost BLOCK's variables in block 1.  */
1516       do_block = BLOCK_NUMBER (DECL_INITIAL (decl));
1517       sdbout_block (DECL_INITIAL (decl));
1518     }
1519   /* If -g1, suppress all the internal symbols of functions
1520      except for arguments.  */
1521   if (debug_info_level != DINFO_LEVEL_TERSE)
1522     {
1523       do_block = n;
1524       sdbout_block (DECL_INITIAL (decl));
1525     }
1526
1527 #ifdef SDB_ALLOW_FORWARD_REFERENCES
1528   sdbout_dequeue_anonymous_types ();
1529 #endif
1530 }
1531
1532 /* Describe the end line-number of an internal block within a function.  */
1533
1534 static void
1535 sdbout_end_block (unsigned int line, unsigned int n ATTRIBUTE_UNUSED)
1536 {
1537   MAKE_LINE_SAFE (line);
1538
1539   /* The SCO compiler does not emit a separate block for the function level
1540      scope, so we avoid it here also.  However, mips ECOFF compilers do emit
1541      a separate block, so we retain it when MIPS_DEBUGGING_INFO is defined.  */
1542 #ifndef MIPS_DEBUGGING_INFO
1543   if (n != 1)
1544 #endif
1545   PUT_SDB_BLOCK_END (line - sdb_begin_function_line);
1546 }
1547
1548 /* Output a line number symbol entry for source file FILENAME and line
1549    number LINE.  */
1550
1551 static void
1552 sdbout_source_line (unsigned int line, const char *filename ATTRIBUTE_UNUSED,
1553                     int discriminator ATTRIBUTE_UNUSED,
1554                     bool is_stmt ATTRIBUTE_UNUSED)
1555 {
1556   /* COFF relative line numbers must be positive.  */
1557   if ((int) line > sdb_begin_function_line)
1558     {
1559 #ifdef SDB_OUTPUT_SOURCE_LINE
1560       SDB_OUTPUT_SOURCE_LINE (asm_out_file, line);
1561 #else
1562       fprintf (asm_out_file, "\t.ln\t%d\n",
1563                ((sdb_begin_function_line > -1)
1564                 ? line - sdb_begin_function_line : 1));
1565 #endif
1566     }
1567 }
1568
1569 /* Output sdb info for the current function name.
1570    Called from assemble_start_function.  */
1571
1572 static void
1573 sdbout_begin_function (tree decl ATTRIBUTE_UNUSED)
1574 {
1575   sdbout_symbol (current_function_decl, 0);
1576 }
1577
1578 /* Called at beginning of function body (before or after prologue,
1579    depending on MIPS_DEBUGGING_INFO).  Record the function's starting
1580    line number, so we can output relative line numbers for the other
1581    lines.  Describe beginning of outermost block.  Also describe the
1582    parameter list.  */
1583
1584 #ifndef MIPS_DEBUGGING_INFO
1585 static void
1586 sdbout_begin_prologue (unsigned int line, const char *file ATTRIBUTE_UNUSED)
1587 {
1588   sdbout_end_prologue (line, file);
1589 }
1590 #endif
1591
1592 static void
1593 sdbout_end_prologue (unsigned int line, const char *file ATTRIBUTE_UNUSED)
1594 {
1595   sdb_begin_function_line = line - 1;
1596   PUT_SDB_FUNCTION_START (line);
1597   sdbout_parms (DECL_ARGUMENTS (current_function_decl));
1598   sdbout_reg_parms (DECL_ARGUMENTS (current_function_decl));
1599 }
1600
1601 /* Called at end of function (before epilogue).
1602    Describe end of outermost block.  */
1603
1604 static void
1605 sdbout_end_function (unsigned int line)
1606 {
1607 #ifdef SDB_ALLOW_FORWARD_REFERENCES
1608   sdbout_dequeue_anonymous_types ();
1609 #endif
1610
1611   MAKE_LINE_SAFE (line);
1612   PUT_SDB_FUNCTION_END (line - sdb_begin_function_line);
1613
1614   /* Indicate we are between functions, for line-number output.  */
1615   sdb_begin_function_line = -1;
1616 }
1617
1618 /* Output sdb info for the absolute end of a function.
1619    Called after the epilogue is output.  */
1620
1621 static void
1622 sdbout_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1623                      const char *file ATTRIBUTE_UNUSED)
1624 {
1625   const char *const name ATTRIBUTE_UNUSED
1626     = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
1627
1628 #ifdef PUT_SDB_EPILOGUE_END
1629   PUT_SDB_EPILOGUE_END (name);
1630 #else
1631   fprintf (asm_out_file, "\t.def\t");
1632   assemble_name (asm_out_file, name);
1633   fprintf (asm_out_file, "%s\t.val\t.%s\t.scl\t-1%s\t.endef\n",
1634            SDB_DELIM, SDB_DELIM, SDB_DELIM);
1635 #endif
1636 }
1637
1638 /* Output sdb info for the given label.  Called only if LABEL_NAME (insn)
1639    is present.  */
1640
1641 static void
1642 sdbout_label (rtx insn)
1643 {
1644   PUT_SDB_DEF (LABEL_NAME (insn));
1645   PUT_SDB_VAL (insn);
1646   PUT_SDB_SCL (C_LABEL);
1647   PUT_SDB_TYPE (T_NULL);
1648   PUT_SDB_ENDEF;
1649 }
1650
1651 /* Change to reading from a new source file.  */
1652
1653 static void
1654 sdbout_start_source_file (unsigned int line ATTRIBUTE_UNUSED,
1655                           const char *filename ATTRIBUTE_UNUSED)
1656 {
1657 #ifdef MIPS_DEBUGGING_INFO
1658   struct sdb_file *n = XNEW (struct sdb_file);
1659
1660   n->next = current_file;
1661   n->name = filename;
1662   current_file = n;
1663   output_file_directive (asm_out_file, filename);
1664 #endif
1665 }
1666
1667 /* Revert to reading a previous source file.  */
1668
1669 static void
1670 sdbout_end_source_file (unsigned int line ATTRIBUTE_UNUSED)
1671 {
1672 #ifdef MIPS_DEBUGGING_INFO
1673   struct sdb_file *next;
1674
1675   next = current_file->next;
1676   free (current_file);
1677   current_file = next;
1678   output_file_directive (asm_out_file, current_file->name);
1679 #endif
1680 }
1681
1682 /* Set up for SDB output at the start of compilation.  */
1683
1684 static void
1685 sdbout_init (const char *input_file_name ATTRIBUTE_UNUSED)
1686 {
1687   tree t;
1688
1689 #ifdef MIPS_DEBUGGING_INFO
1690   current_file = XNEW (struct sdb_file);
1691   current_file->next = NULL;
1692   current_file->name = input_file_name;
1693 #endif
1694
1695   deferred_global_decls = VEC_alloc (tree, gc, 12);
1696
1697   /* Emit debug information which was queued by sdbout_symbol before
1698      we got here.  */
1699   sdbout_initialized = true;
1700
1701   for (t = nreverse (preinit_symbols); t; t = TREE_CHAIN (t))
1702     sdbout_symbol (TREE_VALUE (t), 0);
1703   preinit_symbols = 0;
1704 }
1705
1706 #else  /* SDB_DEBUGGING_INFO */
1707
1708 /* This should never be used, but its address is needed for comparisons.  */
1709 const struct gcc_debug_hooks sdb_debug_hooks =
1710 {
1711   0,            /* init */
1712   0,            /* finish */
1713   0,            /* assembly_start */
1714   0,            /* define */
1715   0,            /* undef */
1716   0,            /* start_source_file */
1717   0,            /* end_source_file */
1718   0,            /* begin_block */
1719   0,            /* end_block */
1720   0,            /* ignore_block */
1721   0,            /* source_line */
1722   0,            /* begin_prologue */
1723   0,            /* end_prologue */
1724   0,            /* begin_epilogue */
1725   0,            /* end_epilogue */
1726   0,            /* begin_function */
1727   0,            /* end_function */
1728   0,            /* function_decl */
1729   0,            /* global_decl */
1730   0,            /* type_decl */
1731   0,            /* imported_module_or_decl */
1732   0,            /* deferred_inline_function */
1733   0,            /* outlining_inline_function */
1734   0,            /* label */
1735   0,            /* handle_pch */
1736   0,            /* var_location */
1737   0,            /* switch_text_section */
1738   0,            /* direct_call */
1739   0,            /* virtual_call_token */
1740   0,            /* copy_call_info */
1741   0,            /* virtual_call */
1742   0,            /* set_name */
1743   0             /* start_end_main_source_file */
1744 };
1745
1746
1747 #endif /* SDB_DEBUGGING_INFO */
1748
1749 #include "gt-sdbout.h"