OSDN Git Service

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