OSDN Git Service

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