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