OSDN Git Service

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