OSDN Git Service

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