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