OSDN Git Service

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