OSDN Git Service

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