OSDN Git Service

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