OSDN Git Service

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