OSDN Git Service

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