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