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