OSDN Git Service

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