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