OSDN Git Service

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