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