OSDN Git Service

Enable implicitly deleted functions (N2346)
[pf3gnuchains/gcc-fork.git] / gcc / dbxout.c
1 /* Output dbx-format symbol table information from GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 /* Output dbx-format symbol table data.
24    This consists of many symbol table entries, each of them
25    a .stabs assembler pseudo-op with four operands:
26    a "name" which is really a description of one symbol and its type,
27    a "code", which is a symbol defined in stab.h whose name starts with N_,
28    an unused operand always 0,
29    and a "value" which is an address or an offset.
30    The name is enclosed in doublequote characters.
31
32    Each function, variable, typedef, and structure tag
33    has a symbol table entry to define it.
34    The beginning and end of each level of name scoping within
35    a function are also marked by special symbol table entries.
36
37    The "name" consists of the symbol name, a colon, a kind-of-symbol letter,
38    and a data type number.  The data type number may be followed by
39    "=" and a type definition; normally this will happen the first time
40    the type number is mentioned.  The type definition may refer to
41    other types by number, and those type numbers may be followed
42    by "=" and nested definitions.
43
44    This can make the "name" quite long.
45    When a name is more than 80 characters, we split the .stabs pseudo-op
46    into two .stabs pseudo-ops, both sharing the same "code" and "value".
47    The first one is marked as continued with a double-backslash at the
48    end of its "name".
49
50    The kind-of-symbol letter distinguished function names from global
51    variables from file-scope variables from parameters from auto
52    variables in memory from typedef names from register variables.
53    See `dbxout_symbol'.
54
55    The "code" is mostly redundant with the kind-of-symbol letter
56    that goes in the "name", but not entirely: for symbols located
57    in static storage, the "code" says which segment the address is in,
58    which controls how it is relocated.
59
60    The "value" for a symbol in static storage
61    is the core address of the symbol (actually, the assembler
62    label for the symbol).  For a symbol located in a stack slot
63    it is the stack offset; for one in a register, the register number.
64    For a typedef symbol, it is zero.
65
66    If DEBUG_SYMS_TEXT is defined, all debugging symbols must be
67    output while in the text section.
68
69    For more on data type definitions, see `dbxout_type'.  */
70
71 #include "config.h"
72 #include "system.h"
73 #include "coretypes.h"
74 #include "tm.h"
75
76 #include "tree.h"
77 #include "rtl.h"
78 #include "flags.h"
79 #include "regs.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "dbxout.h"
84 #include "toplev.h"
85 #include "tm_p.h"
86 #include "ggc.h"
87 #include "debug.h"
88 #include "function.h"
89 #include "target.h"
90 #include "langhooks.h"
91 #include "obstack.h"
92 #include "expr.h"
93
94 #ifdef XCOFF_DEBUGGING_INFO
95 #include "xcoffout.h"
96 #endif
97
98 #define DBXOUT_DECR_NESTING \
99   if (--debug_nesting == 0 && symbol_queue_index > 0) \
100     { emit_pending_bincls_if_required (); debug_flush_symbol_queue (); }
101
102 #define DBXOUT_DECR_NESTING_AND_RETURN(x) \
103   do {--debug_nesting; return (x);} while (0)
104
105 #ifndef ASM_STABS_OP
106 # ifdef XCOFF_DEBUGGING_INFO
107 #  define ASM_STABS_OP "\t.stabx\t"
108 # else
109 #  define ASM_STABS_OP "\t.stabs\t"
110 # endif
111 #endif
112
113 #ifndef ASM_STABN_OP
114 #define ASM_STABN_OP "\t.stabn\t"
115 #endif
116
117 #ifndef ASM_STABD_OP
118 #define ASM_STABD_OP "\t.stabd\t"
119 #endif
120
121 #ifndef DBX_TYPE_DECL_STABS_CODE
122 #define DBX_TYPE_DECL_STABS_CODE N_LSYM
123 #endif
124
125 #ifndef DBX_STATIC_CONST_VAR_CODE
126 #define DBX_STATIC_CONST_VAR_CODE N_FUN
127 #endif
128
129 #ifndef DBX_REGPARM_STABS_CODE
130 #define DBX_REGPARM_STABS_CODE N_RSYM
131 #endif
132
133 #ifndef DBX_REGPARM_STABS_LETTER
134 #define DBX_REGPARM_STABS_LETTER 'P'
135 #endif
136
137 #ifndef NO_DBX_FUNCTION_END
138 #define NO_DBX_FUNCTION_END 0
139 #endif
140
141 #ifndef NO_DBX_BNSYM_ENSYM
142 #define NO_DBX_BNSYM_ENSYM 0
143 #endif
144
145 #ifndef NO_DBX_MAIN_SOURCE_DIRECTORY
146 #define NO_DBX_MAIN_SOURCE_DIRECTORY 0
147 #endif
148
149 #ifndef DBX_BLOCKS_FUNCTION_RELATIVE
150 #define DBX_BLOCKS_FUNCTION_RELATIVE 0
151 #endif
152
153 #ifndef DBX_LINES_FUNCTION_RELATIVE
154 #define DBX_LINES_FUNCTION_RELATIVE 0
155 #endif
156
157 #ifndef DBX_CONTIN_LENGTH
158 #define DBX_CONTIN_LENGTH 80
159 #endif
160
161 #ifndef DBX_CONTIN_CHAR
162 #define DBX_CONTIN_CHAR '\\'
163 #endif
164
165 enum typestatus {TYPE_UNSEEN, TYPE_XREF, TYPE_DEFINED};
166
167 /* Structure recording information about a C data type.
168    The status element says whether we have yet output
169    the definition of the type.  TYPE_XREF says we have
170    output it as a cross-reference only.
171    The file_number and type_number elements are used if DBX_USE_BINCL
172    is defined.  */
173
174 struct GTY(()) typeinfo {
175   enum typestatus status;
176   int file_number;
177   int type_number;
178 };
179
180 /* Vector recording information about C data types.
181    When we first notice a data type (a tree node),
182    we assign it a number using next_type_number.
183    That is its index in this vector.  */
184
185 static GTY ((length ("typevec_len"))) struct typeinfo *typevec;
186
187 /* Number of elements of space allocated in `typevec'.  */
188
189 static GTY(()) int typevec_len;
190
191 /* In dbx output, each type gets a unique number.
192    This is the number for the next type output.
193    The number, once assigned, is in the TYPE_SYMTAB_ADDRESS field.  */
194
195 static GTY(()) int next_type_number;
196
197 /* The C front end may call dbxout_symbol before dbxout_init runs.
198    We save all such decls in this list and output them when we get
199    to dbxout_init.  */
200
201 static GTY(()) tree preinit_symbols;
202
203 enum binclstatus {BINCL_NOT_REQUIRED, BINCL_PENDING, BINCL_PROCESSED};
204
205 /* When using N_BINCL in dbx output, each type number is actually a
206    pair of the file number and the type number within the file.
207    This is a stack of input files.  */
208
209 struct dbx_file
210 {
211   struct dbx_file *next;
212   int file_number;
213   int next_type_number;
214   enum binclstatus bincl_status;  /* Keep track of lazy bincl.  */
215   const char *pending_bincl_name; /* Name of bincl.  */
216   struct dbx_file *prev;          /* Chain to traverse all pending bincls.  */
217 };
218
219 /* This is the top of the stack.
220
221    This is not saved for PCH, because restoring a PCH should not change it.
222    next_file_number does have to be saved, because the PCH may use some
223    file numbers; however, just before restoring a PCH, next_file_number
224    should always be 0 because we should not have needed any file numbers
225    yet.  */
226
227 #if (defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)) \
228     && defined (DBX_USE_BINCL)
229 static struct dbx_file *current_file;
230 #endif
231
232 /* This is the next file number to use.  */
233
234 static GTY(()) int next_file_number;
235
236 /* A counter for dbxout_function_end.  */
237
238 static GTY(()) int scope_labelno;
239
240 /* A counter for dbxout_source_line.  */
241
242 static GTY(()) int dbxout_source_line_counter;
243
244 /* Number for the next N_SOL filename stabs label.  The number 0 is reserved
245    for the N_SO filename stabs label.  */
246
247 static GTY(()) int source_label_number = 1;
248
249 /* Last source file name mentioned in a NOTE insn.  */
250
251 static GTY(()) const char *lastfile;
252
253 /* Used by PCH machinery to detect if 'lastfile' should be reset to
254    base_input_file.  */
255 static GTY(()) int lastfile_is_base;
256
257 /* Typical USG systems don't have stab.h, and they also have
258    no use for DBX-format debugging info.  */
259
260 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
261
262 #ifdef DBX_USE_BINCL
263 /* If zero then there is no pending BINCL.  */
264 static int pending_bincls = 0;
265 #endif
266
267 /* The original input file name.  */
268 static const char *base_input_file;
269
270 #ifdef DEBUG_SYMS_TEXT
271 #define FORCE_TEXT switch_to_section (current_function_section ())
272 #else
273 #define FORCE_TEXT
274 #endif
275
276 #include "gstab.h"
277
278 /* 1 if PARM is passed to this function in memory.  */
279
280 #define PARM_PASSED_IN_MEMORY(PARM) \
281  (MEM_P (DECL_INCOMING_RTL (PARM)))
282
283 /* A C expression for the integer offset value of an automatic variable
284    (N_LSYM) having address X (an RTX).  */
285 #ifndef DEBUGGER_AUTO_OFFSET
286 #define DEBUGGER_AUTO_OFFSET(X) \
287   (GET_CODE (X) == PLUS ? INTVAL (XEXP (X, 1)) : 0)
288 #endif
289
290 /* A C expression for the integer offset value of an argument (N_PSYM)
291    having address X (an RTX).  The nominal offset is OFFSET.  */
292 #ifndef DEBUGGER_ARG_OFFSET
293 #define DEBUGGER_ARG_OFFSET(OFFSET, X) (OFFSET)
294 #endif
295
296 /* This obstack holds the stab string currently being constructed.  We
297    build it up here, then write it out, so we can split long lines up
298    properly (see dbxout_finish_complex_stabs).  */
299 static struct obstack stabstr_ob;
300 static size_t stabstr_last_contin_point;
301
302 #ifdef DBX_USE_BINCL
303 static void emit_bincl_stab             (const char *c);
304 static void emit_pending_bincls         (void);
305 #endif
306 static inline void emit_pending_bincls_if_required (void);
307
308 static void dbxout_init (const char *);
309
310 static void dbxout_finish (const char *);
311 static void dbxout_start_source_file (unsigned, const char *);
312 static void dbxout_end_source_file (unsigned);
313 static void dbxout_typedefs (tree);
314 static void dbxout_type_index (tree);
315 static void dbxout_args (tree);
316 static void dbxout_type_fields (tree);
317 static void dbxout_type_method_1 (tree);
318 static void dbxout_type_methods (tree);
319 static void dbxout_range_type (tree, tree, tree);
320 static void dbxout_type (tree, int);
321 static bool print_int_cst_bounds_in_octal_p (tree, tree, tree);
322 static bool is_fortran (void);
323 static void dbxout_type_name (tree);
324 static void dbxout_class_name_qualifiers (tree);
325 static int dbxout_symbol_location (tree, tree, const char *, rtx);
326 static void dbxout_symbol_name (tree, const char *, int);
327 static void dbxout_common_name (tree, const char *, stab_code_type);
328 static const char *dbxout_common_check (tree, int *);
329 static void dbxout_global_decl (tree);
330 static void dbxout_type_decl (tree, int);
331 static void dbxout_handle_pch (unsigned);
332 \f
333 /* The debug hooks structure.  */
334 #if defined (DBX_DEBUGGING_INFO)
335
336 static void dbxout_source_line (unsigned int, const char *, int, bool);
337 static void dbxout_begin_prologue (unsigned int, const char *);
338 static void dbxout_source_file (const char *);
339 static void dbxout_function_end (tree);
340 static void dbxout_begin_function (tree);
341 static void dbxout_begin_block (unsigned, unsigned);
342 static void dbxout_end_block (unsigned, unsigned);
343 static void dbxout_function_decl (tree);
344
345 const struct gcc_debug_hooks dbx_debug_hooks =
346 {
347   dbxout_init,
348   dbxout_finish,
349   debug_nothing_void,
350   debug_nothing_int_charstar,
351   debug_nothing_int_charstar,
352   dbxout_start_source_file,
353   dbxout_end_source_file,
354   dbxout_begin_block,
355   dbxout_end_block,
356   debug_true_const_tree,                 /* ignore_block */
357   dbxout_source_line,                    /* source_line */
358   dbxout_begin_prologue,                 /* begin_prologue */
359   debug_nothing_int_charstar,            /* end_prologue */
360   debug_nothing_int_charstar,            /* begin_epilogue */
361   debug_nothing_int_charstar,            /* end_epilogue */
362 #ifdef DBX_FUNCTION_FIRST
363   dbxout_begin_function,
364 #else
365   debug_nothing_tree,                    /* begin_function */
366 #endif
367   debug_nothing_int,                     /* end_function */
368   dbxout_function_decl,
369   dbxout_global_decl,                    /* global_decl */
370   dbxout_type_decl,                      /* type_decl */
371   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
372   debug_nothing_tree,                    /* deferred_inline_function */
373   debug_nothing_tree,                    /* outlining_inline_function */
374   debug_nothing_rtx,                     /* label */
375   dbxout_handle_pch,                     /* handle_pch */
376   debug_nothing_rtx,                     /* var_location */
377   debug_nothing_void,                    /* switch_text_section */
378   debug_nothing_tree,                    /* direct_call */
379   debug_nothing_tree_int,                /* virtual_call_token */
380   debug_nothing_rtx_rtx,                 /* copy_call_info */
381   debug_nothing_uid,                     /* virtual_call */
382   debug_nothing_tree_tree,               /* set_name */
383   0                                      /* start_end_main_source_file */
384 };
385 #endif /* DBX_DEBUGGING_INFO  */
386
387 #if defined (XCOFF_DEBUGGING_INFO)
388 const struct gcc_debug_hooks xcoff_debug_hooks =
389 {
390   dbxout_init,
391   dbxout_finish,
392   debug_nothing_void,
393   debug_nothing_int_charstar,
394   debug_nothing_int_charstar,
395   dbxout_start_source_file,
396   dbxout_end_source_file,
397   xcoffout_begin_block,
398   xcoffout_end_block,
399   debug_true_const_tree,                 /* ignore_block */
400   xcoffout_source_line,
401   xcoffout_begin_prologue,               /* begin_prologue */
402   debug_nothing_int_charstar,            /* end_prologue */
403   debug_nothing_int_charstar,            /* begin_epilogue */
404   xcoffout_end_epilogue,
405   debug_nothing_tree,                    /* begin_function */
406   xcoffout_end_function,
407   debug_nothing_tree,                    /* function_decl */
408   dbxout_global_decl,                    /* global_decl */
409   dbxout_type_decl,                      /* type_decl */
410   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
411   debug_nothing_tree,                    /* deferred_inline_function */
412   debug_nothing_tree,                    /* outlining_inline_function */
413   debug_nothing_rtx,                     /* label */
414   dbxout_handle_pch,                     /* handle_pch */
415   debug_nothing_rtx,                     /* var_location */
416   debug_nothing_void,                    /* switch_text_section */
417   debug_nothing_tree,                    /* direct_call */
418   debug_nothing_tree_int,                /* virtual_call_token */
419   debug_nothing_rtx_rtx,                 /* copy_call_info */
420   debug_nothing_uid,                     /* virtual_call */
421   debug_nothing_tree_tree,               /* set_name */
422   0                                      /* start_end_main_source_file */
423 };
424 #endif /* XCOFF_DEBUGGING_INFO  */
425 \f
426 /* Numeric formatting helper macro.  Note that this does not handle
427    hexadecimal.  */
428 #define NUMBER_FMT_LOOP(P, NUM, BASE)           \
429   do                                            \
430     {                                           \
431       int digit = NUM % BASE;                   \
432       NUM /= BASE;                              \
433       *--P = digit + '0';                       \
434     }                                           \
435   while (NUM > 0)
436
437 /* Utility: write a decimal integer NUM to asm_out_file.  */
438 void
439 dbxout_int (int num)
440 {
441   char buf[64];
442   char *p = buf + sizeof buf;
443   unsigned int unum;
444
445   if (num == 0)
446     {
447       putc ('0', asm_out_file);
448       return;
449     }
450   if (num < 0)
451     {
452       putc ('-', asm_out_file);
453       unum = -num;
454     }
455   else
456     unum = num;
457
458   NUMBER_FMT_LOOP (p, unum, 10);
459
460   while (p < buf + sizeof buf)
461     {
462       putc (*p, asm_out_file);
463       p++;
464     }
465 }
466
467 \f
468 /* Primitives for emitting simple stabs directives.  All other stabs
469    routines should use these functions instead of directly emitting
470    stabs.  They are exported because machine-dependent code may need
471    to invoke them, e.g. in a DBX_OUTPUT_* macro whose definition
472    forwards to code in CPU.c.  */
473
474 /* The following functions should all be called immediately after one
475    of the dbxout_begin_stab* functions (below).  They write out
476    various things as the value of a stab.  */
477
478 /* Write out a literal zero as the value of a stab.  */
479 void
480 dbxout_stab_value_zero (void)
481 {
482   fputs ("0\n", asm_out_file);
483 }
484
485 /* Write out the label LABEL as the value of a stab.  */
486 void
487 dbxout_stab_value_label (const char *label)
488 {
489   assemble_name (asm_out_file, label);
490   putc ('\n', asm_out_file);
491 }
492
493 /* Write out the difference of two labels, LABEL - BASE, as the value
494    of a stab.  */
495 void
496 dbxout_stab_value_label_diff (const char *label, const char *base)
497 {
498   assemble_name (asm_out_file, label);
499   putc ('-', asm_out_file);
500   assemble_name (asm_out_file, base);
501   putc ('\n', asm_out_file);
502 }
503
504 /* Write out an internal label as the value of a stab, and immediately
505    emit that internal label.  This should be used only when
506    dbxout_stabd will not work.  STEM is the name stem of the label,
507    COUNTERP is a pointer to a counter variable which will be used to
508    guarantee label uniqueness.  */
509 void
510 dbxout_stab_value_internal_label (const char *stem, int *counterp)
511 {
512   char label[100];
513   int counter = counterp ? (*counterp)++ : 0;
514
515   ASM_GENERATE_INTERNAL_LABEL (label, stem, counter);
516   dbxout_stab_value_label (label);
517   targetm.asm_out.internal_label (asm_out_file, stem, counter);
518 }
519
520 /* Write out the difference between BASE and an internal label as the
521    value of a stab, and immediately emit that internal label.  STEM and
522    COUNTERP are as for dbxout_stab_value_internal_label.  */
523 void
524 dbxout_stab_value_internal_label_diff (const char *stem, int *counterp,
525                                        const char *base)
526 {
527   char label[100];
528   int counter = counterp ? (*counterp)++ : 0;
529
530   ASM_GENERATE_INTERNAL_LABEL (label, stem, counter);
531   dbxout_stab_value_label_diff (label, base);
532   targetm.asm_out.internal_label (asm_out_file, stem, counter);
533 }
534
535 /* The following functions produce specific kinds of stab directives.  */
536
537 /* Write a .stabd directive with type STYPE and desc SDESC to asm_out_file.  */
538 void
539 dbxout_stabd (int stype, int sdesc)
540 {
541   fputs (ASM_STABD_OP, asm_out_file);
542   dbxout_int (stype);
543   fputs (",0,", asm_out_file);
544   dbxout_int (sdesc);
545   putc ('\n', asm_out_file);
546 }
547
548 /* Write a .stabn directive with type STYPE.  This function stops
549    short of emitting the value field, which is the responsibility of
550    the caller (normally it will be either a symbol or the difference
551    of two symbols).  */
552
553 void
554 dbxout_begin_stabn (int stype)
555 {
556   fputs (ASM_STABN_OP, asm_out_file);
557   dbxout_int (stype);
558   fputs (",0,0,", asm_out_file);
559 }
560
561 /* Write a .stabn directive with type N_SLINE and desc LINE.  As above,
562    the value field is the responsibility of the caller.  */
563 void
564 dbxout_begin_stabn_sline (int lineno)
565 {
566   fputs (ASM_STABN_OP, asm_out_file);
567   dbxout_int (N_SLINE);
568   fputs (",0,", asm_out_file);
569   dbxout_int (lineno);
570   putc (',', asm_out_file);
571 }
572
573 /* Begin a .stabs directive with string "", type STYPE, and desc and
574    other fields 0.  The value field is the responsibility of the
575    caller.  This function cannot be used for .stabx directives.  */
576 void
577 dbxout_begin_empty_stabs (int stype)
578 {
579   fputs (ASM_STABS_OP, asm_out_file);
580   fputs ("\"\",", asm_out_file);
581   dbxout_int (stype);
582   fputs (",0,0,", asm_out_file);
583 }
584
585 /* Begin a .stabs directive with string STR, type STYPE, and desc 0.
586    The value field is the responsibility of the caller.  */
587 void
588 dbxout_begin_simple_stabs (const char *str, int stype)
589 {
590   fputs (ASM_STABS_OP, asm_out_file);
591   output_quoted_string (asm_out_file, str);
592   putc (',', asm_out_file);
593   dbxout_int (stype);
594   fputs (",0,0,", asm_out_file);
595 }
596
597 /* As above but use SDESC for the desc field.  */
598 void
599 dbxout_begin_simple_stabs_desc (const char *str, int stype, int sdesc)
600 {
601   fputs (ASM_STABS_OP, asm_out_file);
602   output_quoted_string (asm_out_file, str);
603   putc (',', asm_out_file);
604   dbxout_int (stype);
605   fputs (",0,", asm_out_file);
606   dbxout_int (sdesc);
607   putc (',', asm_out_file);
608 }
609
610 /* The next set of functions are entirely concerned with production of
611    "complex" .stabs directives: that is, .stabs directives whose
612    strings have to be constructed piecemeal.  dbxout_type,
613    dbxout_symbol, etc. use these routines heavily.  The string is queued
614    up in an obstack, then written out by dbxout_finish_complex_stabs, which
615    is also responsible for splitting it up if it exceeds DBX_CONTIN_LENGTH.
616    (You might think it would be more efficient to go straight to stdio
617    when DBX_CONTIN_LENGTH is 0 (i.e. no length limit) but that turns
618    out not to be the case, and anyway this needs fewer #ifdefs.)  */
619
620 /* Begin a complex .stabs directive.  If we can, write the initial
621    ASM_STABS_OP to the asm_out_file.  */
622
623 static void
624 dbxout_begin_complex_stabs (void)
625 {
626   emit_pending_bincls_if_required ();
627   FORCE_TEXT;
628   fputs (ASM_STABS_OP, asm_out_file);
629   putc ('"', asm_out_file);
630   gcc_assert (stabstr_last_contin_point == 0);
631 }
632
633 /* As above, but do not force text or emit pending bincls.  This is
634    used by dbxout_symbol_location, which needs to do something else.  */
635 static void
636 dbxout_begin_complex_stabs_noforcetext (void)
637 {
638   fputs (ASM_STABS_OP, asm_out_file);
639   putc ('"', asm_out_file);
640   gcc_assert (stabstr_last_contin_point == 0);
641 }
642
643 /* Add CHR, a single character, to the string being built.  */
644 #define stabstr_C(chr) obstack_1grow (&stabstr_ob, chr)
645
646 /* Add STR, a normal C string, to the string being built.  */
647 #define stabstr_S(str) obstack_grow (&stabstr_ob, str, strlen(str))
648
649 /* Add the text of ID, an IDENTIFIER_NODE, to the string being built.  */
650 #define stabstr_I(id) obstack_grow (&stabstr_ob, \
651                                     IDENTIFIER_POINTER (id), \
652                                     IDENTIFIER_LENGTH (id))
653
654 /* Add NUM, a signed decimal number, to the string being built.  */
655 static void
656 stabstr_D (HOST_WIDE_INT num)
657 {
658   char buf[64];
659   char *p = buf + sizeof buf;
660   unsigned int unum;
661
662   if (num == 0)
663     {
664       stabstr_C ('0');
665       return;
666     }
667   if (num < 0)
668     {
669       stabstr_C ('-');
670       unum = -num;
671     }
672   else
673     unum = num;
674
675   NUMBER_FMT_LOOP (p, unum, 10);
676
677   obstack_grow (&stabstr_ob, p, (buf + sizeof buf) - p);
678 }
679
680 /* Add NUM, an unsigned decimal number, to the string being built.  */
681 static void
682 stabstr_U (unsigned HOST_WIDE_INT num)
683 {
684   char buf[64];
685   char *p = buf + sizeof buf;
686   if (num == 0)
687     {
688       stabstr_C ('0');
689       return;
690     }
691   NUMBER_FMT_LOOP (p, num, 10);
692   obstack_grow (&stabstr_ob, p, (buf + sizeof buf) - p);
693 }
694
695 /* Add CST, an INTEGER_CST tree, to the string being built as an
696    unsigned octal number.  This routine handles values which are
697    larger than a single HOST_WIDE_INT.  */
698 static void
699 stabstr_O (tree cst)
700 {
701   unsigned HOST_WIDE_INT high = TREE_INT_CST_HIGH (cst);
702   unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
703
704   char buf[128];
705   char *p = buf + sizeof buf;
706
707   /* GDB wants constants with no extra leading "1" bits, so
708      we need to remove any sign-extension that might be
709      present.  */
710   {
711     const unsigned int width = TYPE_PRECISION (TREE_TYPE (cst));
712     if (width == HOST_BITS_PER_WIDE_INT * 2)
713       ;
714     else if (width > HOST_BITS_PER_WIDE_INT)
715       high &= (((HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT)) - 1);
716     else if (width == HOST_BITS_PER_WIDE_INT)
717       high = 0;
718     else
719       high = 0, low &= (((HOST_WIDE_INT) 1 << width) - 1);
720   }
721
722   /* Leading zero for base indicator.  */
723   stabstr_C ('0');
724
725   /* If the value is zero, the base indicator will serve as the value
726      all by itself.  */
727   if (high == 0 && low == 0)
728     return;
729
730   /* If the high half is zero, we need only print the low half normally.  */
731   if (high == 0)
732     NUMBER_FMT_LOOP (p, low, 8);
733   else
734     {
735       /* When high != 0, we need to print enough zeroes from low to
736          give the digits from high their proper place-values.  Hence
737          NUMBER_FMT_LOOP cannot be used.  */
738       const int n_digits = HOST_BITS_PER_WIDE_INT / 3;
739       int i;
740
741       for (i = 1; i <= n_digits; i++)
742         {
743           unsigned int digit = low % 8;
744           low /= 8;
745           *--p = '0' + digit;
746         }
747
748       /* Octal digits carry exactly three bits of information.  The
749          width of a HOST_WIDE_INT is not normally a multiple of three.
750          Therefore, the next digit printed probably needs to carry
751          information from both low and high.  */
752       if (HOST_BITS_PER_WIDE_INT % 3 != 0)
753         {
754           const int n_leftover_bits = HOST_BITS_PER_WIDE_INT % 3;
755           const int n_bits_from_high = 3 - n_leftover_bits;
756
757           const unsigned HOST_WIDE_INT
758             low_mask = (((unsigned HOST_WIDE_INT)1) << n_leftover_bits) - 1;
759           const unsigned HOST_WIDE_INT
760             high_mask = (((unsigned HOST_WIDE_INT)1) << n_bits_from_high) - 1;
761
762           unsigned int digit;
763
764           /* At this point, only the bottom n_leftover_bits bits of low
765              should be set.  */
766           gcc_assert (!(low & ~low_mask));
767
768           digit = (low | ((high & high_mask) << n_leftover_bits));
769           high >>= n_bits_from_high;
770
771           *--p = '0' + digit;
772         }
773
774       /* Now we can format high in the normal manner.  However, if
775          the only bits of high that were set were handled by the
776          digit split between low and high, high will now be zero, and
777          we don't want to print extra digits in that case.  */
778       if (high)
779         NUMBER_FMT_LOOP (p, high, 8);
780     }
781
782   obstack_grow (&stabstr_ob, p, (buf + sizeof buf) - p);
783 }
784
785 /* Called whenever it is safe to break a stabs string into multiple
786    .stabs directives.  If the current string has exceeded the limit
787    set by DBX_CONTIN_LENGTH, mark the current position in the buffer
788    as a continuation point by inserting DBX_CONTIN_CHAR (doubled if
789    it is a backslash) and a null character.  */
790 static inline void
791 stabstr_continue (void)
792 {
793   if (DBX_CONTIN_LENGTH > 0
794       && obstack_object_size (&stabstr_ob) - stabstr_last_contin_point
795          > DBX_CONTIN_LENGTH)
796     {
797       if (DBX_CONTIN_CHAR == '\\')
798         obstack_1grow (&stabstr_ob, '\\');
799       obstack_1grow (&stabstr_ob, DBX_CONTIN_CHAR);
800       obstack_1grow (&stabstr_ob, '\0');
801       stabstr_last_contin_point = obstack_object_size (&stabstr_ob);
802     }
803 }
804 #define CONTIN stabstr_continue ()
805
806 /* Macro subroutine of dbxout_finish_complex_stabs, which emits
807    all of the arguments to the .stabs directive after the string.
808    Overridden by xcoffout.h.  CODE is the stabs code for this symbol;
809    LINE is the source line to write into the desc field (in extended
810    mode); SYM is the symbol itself.
811
812    ADDR, LABEL, and NUMBER are three different ways to represent the
813    stabs value field.  At most one of these should be nonzero.
814
815      ADDR is used most of the time; it represents the value as an
816      RTL address constant.
817
818      LABEL is used (currently) only for N_CATCH stabs; it represents
819      the value as a string suitable for assemble_name.
820
821      NUMBER is used when the value is an offset from an implicit base
822      pointer (e.g. for a stack variable), or an index (e.g. for a
823      register variable).  It represents the value as a decimal integer.  */
824
825 #ifndef DBX_FINISH_STABS
826 #define DBX_FINISH_STABS(SYM, CODE, LINE, ADDR, LABEL, NUMBER)  \
827 do {                                                            \
828   int line_ = use_gnu_debug_info_extensions ? LINE : 0;         \
829                                                                 \
830   dbxout_int (CODE);                                            \
831   fputs (",0,", asm_out_file);                                  \
832   dbxout_int (line_);                                           \
833   putc (',', asm_out_file);                                     \
834   if (ADDR)                                                     \
835     output_addr_const (asm_out_file, ADDR);                     \
836   else if (LABEL)                                               \
837     assemble_name (asm_out_file, LABEL);                        \
838   else                                                          \
839     dbxout_int (NUMBER);                                        \
840   putc ('\n', asm_out_file);                                    \
841 } while (0)
842 #endif
843
844 /* Finish the emission of a complex .stabs directive.  When DBX_CONTIN_LENGTH
845    is zero, this has only to emit the close quote and the remainder of
846    the arguments.  When it is nonzero, the string has been marshalled in
847    stabstr_ob, and this routine is responsible for breaking it up into
848    DBX_CONTIN_LENGTH-sized chunks.
849
850    SYM is the DECL of the symbol under consideration; it is used only
851    for its DECL_SOURCE_LINE.  The other arguments are all passed directly
852    to DBX_FINISH_STABS; see above for details.  */
853
854 static void
855 dbxout_finish_complex_stabs (tree sym, stab_code_type code,
856                              rtx addr, const char *label, int number)
857 {
858   int line ATTRIBUTE_UNUSED;
859   char *str;
860   size_t len;
861
862   line = sym ? DECL_SOURCE_LINE (sym) : 0;
863   if (DBX_CONTIN_LENGTH > 0)
864     {
865       char *chunk;
866       size_t chunklen;
867
868       /* Nul-terminate the growing string, then get its size and
869          address.  */
870       obstack_1grow (&stabstr_ob, '\0');
871
872       len = obstack_object_size (&stabstr_ob);
873       chunk = str = XOBFINISH (&stabstr_ob, char *);
874
875       /* Within the buffer are a sequence of NUL-separated strings,
876          each of which is to be written out as a separate stab
877          directive.  */
878       for (;;)
879         {
880           chunklen = strlen (chunk);
881           fwrite (chunk, 1, chunklen, asm_out_file);
882           fputs ("\",", asm_out_file);
883
884           /* Must add an extra byte to account for the NUL separator.  */
885           chunk += chunklen + 1;
886           len   -= chunklen + 1;
887
888           /* Only put a line number on the last stab in the sequence.  */
889           DBX_FINISH_STABS (sym, code, len == 0 ? line : 0,
890                             addr, label, number);
891           if (len == 0)
892             break;
893
894           fputs (ASM_STABS_OP, asm_out_file);
895           putc ('"', asm_out_file);
896         }
897       stabstr_last_contin_point = 0;
898     }
899   else
900     {
901       /* No continuations - we can put the whole string out at once.
902          It is faster to augment the string with the close quote and
903          comma than to do a two-character fputs.  */
904       obstack_grow (&stabstr_ob, "\",", 2);
905       len = obstack_object_size (&stabstr_ob);
906       str = XOBFINISH (&stabstr_ob, char *);
907
908       fwrite (str, 1, len, asm_out_file);
909       DBX_FINISH_STABS (sym, code, line, addr, label, number);
910     }
911   obstack_free (&stabstr_ob, str);
912 }
913
914 #if defined (DBX_DEBUGGING_INFO)
915
916 static void
917 dbxout_function_end (tree decl ATTRIBUTE_UNUSED)
918 {
919   char lscope_label_name[100];
920
921   /* The Lscope label must be emitted even if we aren't doing anything
922      else; dbxout_block needs it.  */
923   switch_to_section (function_section (current_function_decl));
924
925   /* Convert Lscope into the appropriate format for local labels in case
926      the system doesn't insert underscores in front of user generated
927      labels.  */
928   ASM_GENERATE_INTERNAL_LABEL (lscope_label_name, "Lscope", scope_labelno);
929   targetm.asm_out.internal_label (asm_out_file, "Lscope", scope_labelno);
930
931   /* The N_FUN tag at the end of the function is a GNU extension,
932      which may be undesirable, and is unnecessary if we do not have
933      named sections.  */
934   if (!use_gnu_debug_info_extensions
935       || NO_DBX_FUNCTION_END
936       || !targetm.have_named_sections)
937     return;
938
939   /* By convention, GCC will mark the end of a function with an N_FUN
940      symbol and an empty string.  */
941   if (flag_reorder_blocks_and_partition)
942     {
943       dbxout_begin_empty_stabs (N_FUN);
944       dbxout_stab_value_label_diff (crtl->subsections.hot_section_end_label,
945                                     crtl->subsections.hot_section_label);
946       dbxout_begin_empty_stabs (N_FUN);
947       dbxout_stab_value_label_diff (crtl->subsections.cold_section_end_label,
948                                     crtl->subsections.cold_section_label);
949     }
950   else
951     {
952       char begin_label[20];
953       /* Reference current function start using LFBB.  */
954       ASM_GENERATE_INTERNAL_LABEL (begin_label, "LFBB", scope_labelno);
955       dbxout_begin_empty_stabs (N_FUN);
956       dbxout_stab_value_label_diff (lscope_label_name, begin_label);
957     }
958
959   if (!NO_DBX_BNSYM_ENSYM && !flag_debug_only_used_symbols)
960     dbxout_stabd (N_ENSYM, 0);
961 }
962 #endif /* DBX_DEBUGGING_INFO */
963
964 /* Get lang description for N_SO stab.  */
965 static unsigned int ATTRIBUTE_UNUSED
966 get_lang_number (void)
967 {
968   const char *language_string = lang_hooks.name;
969
970   if (strcmp (language_string, "GNU C") == 0)
971     return N_SO_C;
972   else if (strcmp (language_string, "GNU C++") == 0)
973     return N_SO_CC;
974   else if (strcmp (language_string, "GNU F77") == 0)
975     return N_SO_FORTRAN;
976   else if (strcmp (language_string, "GNU Fortran") == 0)
977     return N_SO_FORTRAN90; /* CHECKME */
978   else if (strcmp (language_string, "GNU Pascal") == 0)
979     return N_SO_PASCAL;
980   else if (strcmp (language_string, "GNU Objective-C") == 0)
981     return N_SO_OBJC;
982   else if (strcmp (language_string, "GNU Objective-C++") == 0)
983     return N_SO_OBJCPLUS;
984   else
985     return 0;
986
987 }
988
989 static bool
990 is_fortran (void)
991 {
992    unsigned int lang = get_lang_number ();
993
994    return (lang == N_SO_FORTRAN) || (lang == N_SO_FORTRAN90);
995 }
996
997 /* At the beginning of compilation, start writing the symbol table.
998    Initialize `typevec' and output the standard data types of C.  */
999
1000 static void
1001 dbxout_init (const char *input_file_name)
1002 {
1003   char ltext_label_name[100];
1004   bool used_ltext_label_name = false;
1005   tree syms = lang_hooks.decls.getdecls ();
1006   const char *mapped_name;
1007
1008   typevec_len = 100;
1009   typevec = ggc_alloc_cleared_vec_typeinfo (typevec_len);
1010
1011   /* stabstr_ob contains one string, which will be just fine with
1012      1-byte alignment.  */
1013   obstack_specify_allocation (&stabstr_ob, 0, 1, xmalloc, free);
1014
1015   /* Convert Ltext into the appropriate format for local labels in case
1016      the system doesn't insert underscores in front of user generated
1017      labels.  */
1018   ASM_GENERATE_INTERNAL_LABEL (ltext_label_name, "Ltext", 0);
1019
1020   /* Put the current working directory in an N_SO symbol.  */
1021   if (use_gnu_debug_info_extensions && !NO_DBX_MAIN_SOURCE_DIRECTORY)
1022     {
1023       static const char *cwd;
1024
1025       if (!cwd)
1026         {
1027           cwd = get_src_pwd ();
1028           if (cwd[0] == '\0')
1029             cwd = "/";
1030           else if (!IS_DIR_SEPARATOR (cwd[strlen (cwd) - 1]))
1031             cwd = concat (cwd, "/", NULL);
1032           cwd = remap_debug_filename (cwd);
1033         }
1034 #ifdef DBX_OUTPUT_MAIN_SOURCE_DIRECTORY
1035       DBX_OUTPUT_MAIN_SOURCE_DIRECTORY (asm_out_file, cwd);
1036 #else /* no DBX_OUTPUT_MAIN_SOURCE_DIRECTORY */
1037       dbxout_begin_simple_stabs_desc (cwd, N_SO, get_lang_number ());
1038       dbxout_stab_value_label (ltext_label_name);
1039       used_ltext_label_name = true;
1040 #endif /* no DBX_OUTPUT_MAIN_SOURCE_DIRECTORY */
1041     }
1042
1043   mapped_name = remap_debug_filename (input_file_name);
1044 #ifdef DBX_OUTPUT_MAIN_SOURCE_FILENAME
1045   DBX_OUTPUT_MAIN_SOURCE_FILENAME (asm_out_file, mapped_name);
1046 #else
1047   dbxout_begin_simple_stabs_desc (mapped_name, N_SO, get_lang_number ());
1048   dbxout_stab_value_label (ltext_label_name);
1049   used_ltext_label_name = true;
1050 #endif
1051
1052   if (used_ltext_label_name)
1053     {
1054       switch_to_section (text_section);
1055       targetm.asm_out.internal_label (asm_out_file, "Ltext", 0);
1056     }
1057
1058   /* Emit an N_OPT stab to indicate that this file was compiled by GCC.
1059      The string used is historical.  */
1060 #ifndef NO_DBX_GCC_MARKER
1061   dbxout_begin_simple_stabs ("gcc2_compiled.", N_OPT);
1062   dbxout_stab_value_zero ();
1063 #endif
1064
1065   base_input_file = lastfile = input_file_name;
1066
1067   next_type_number = 1;
1068
1069 #ifdef DBX_USE_BINCL
1070   current_file = XNEW (struct dbx_file);
1071   current_file->next = NULL;
1072   current_file->file_number = 0;
1073   current_file->next_type_number = 1;
1074   next_file_number = 1;
1075   current_file->prev = NULL;
1076   current_file->bincl_status = BINCL_NOT_REQUIRED;
1077   current_file->pending_bincl_name = NULL;
1078 #endif
1079
1080   /* Get all permanent types that have typedef names, and output them
1081      all, except for those already output.  Some language front ends
1082      put these declarations in the top-level scope; some do not;
1083      the latter are responsible for calling debug_hooks->type_decl from
1084      their record_builtin_type function.  */
1085   dbxout_typedefs (syms);
1086
1087   if (preinit_symbols)
1088     {
1089       tree t;
1090       for (t = nreverse (preinit_symbols); t; t = TREE_CHAIN (t))
1091         dbxout_symbol (TREE_VALUE (t), 0);
1092       preinit_symbols = 0;
1093     }
1094 }
1095
1096 /* Output any typedef names for types described by TYPE_DECLs in SYMS.  */
1097
1098 static void
1099 dbxout_typedefs (tree syms)
1100 {
1101   for (; syms != NULL_TREE; syms = TREE_CHAIN (syms))
1102     {
1103       if (TREE_CODE (syms) == TYPE_DECL)
1104         {
1105           tree type = TREE_TYPE (syms);
1106           if (TYPE_NAME (type)
1107               && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1108               && COMPLETE_OR_VOID_TYPE_P (type)
1109               && ! TREE_ASM_WRITTEN (TYPE_NAME (type)))
1110             dbxout_symbol (TYPE_NAME (type), 0);
1111         }
1112     }
1113 }
1114
1115 #ifdef DBX_USE_BINCL
1116 /* Emit BINCL stab using given name.  */
1117 static void
1118 emit_bincl_stab (const char *name)
1119 {
1120   dbxout_begin_simple_stabs (name, N_BINCL);
1121   dbxout_stab_value_zero ();
1122 }
1123
1124 /* If there are pending bincls then it is time to emit all of them.  */
1125
1126 static inline void
1127 emit_pending_bincls_if_required (void)
1128 {
1129   if (pending_bincls)
1130     emit_pending_bincls ();
1131 }
1132
1133 /* Emit all pending bincls.  */
1134
1135 static void
1136 emit_pending_bincls (void)
1137 {
1138   struct dbx_file *f = current_file;
1139
1140   /* Find first pending bincl.  */
1141   while (f->bincl_status == BINCL_PENDING)
1142     f = f->next;
1143
1144   /* Now emit all bincls.  */
1145   f = f->prev;
1146
1147   while (f)
1148     {
1149       if (f->bincl_status == BINCL_PENDING)
1150         {
1151           emit_bincl_stab (f->pending_bincl_name);
1152
1153           /* Update file number and status.  */
1154           f->file_number = next_file_number++;
1155           f->bincl_status = BINCL_PROCESSED;
1156         }
1157       if (f == current_file)
1158         break;
1159       f = f->prev;
1160     }
1161
1162   /* All pending bincls have been emitted.  */
1163   pending_bincls = 0;
1164 }
1165
1166 #else
1167
1168 static inline void
1169 emit_pending_bincls_if_required (void) {}
1170 #endif
1171
1172 /* Change to reading from a new source file.  Generate a N_BINCL stab.  */
1173
1174 static void
1175 dbxout_start_source_file (unsigned int line ATTRIBUTE_UNUSED,
1176                           const char *filename ATTRIBUTE_UNUSED)
1177 {
1178 #ifdef DBX_USE_BINCL
1179   struct dbx_file *n = XNEW (struct dbx_file);
1180
1181   n->next = current_file;
1182   n->next_type_number = 1;
1183   /* Do not assign file number now.
1184      Delay it until we actually emit BINCL.  */
1185   n->file_number = 0;
1186   n->prev = NULL;
1187   current_file->prev = n;
1188   n->bincl_status = BINCL_PENDING;
1189   n->pending_bincl_name = remap_debug_filename (filename);
1190   pending_bincls = 1;
1191   current_file = n;
1192 #endif
1193 }
1194
1195 /* Revert to reading a previous source file.  Generate a N_EINCL stab.  */
1196
1197 static void
1198 dbxout_end_source_file (unsigned int line ATTRIBUTE_UNUSED)
1199 {
1200 #ifdef DBX_USE_BINCL
1201   /* Emit EINCL stab only if BINCL is not pending.  */
1202   if (current_file->bincl_status == BINCL_PROCESSED)
1203     {
1204       dbxout_begin_stabn (N_EINCL);
1205       dbxout_stab_value_zero ();
1206     }
1207   current_file->bincl_status = BINCL_NOT_REQUIRED;
1208   current_file = current_file->next;
1209 #endif
1210 }
1211
1212 /* Handle a few odd cases that occur when trying to make PCH files work.  */
1213
1214 static void
1215 dbxout_handle_pch (unsigned at_end)
1216 {
1217   if (! at_end)
1218     {
1219       /* When using the PCH, this file will be included, so we need to output
1220          a BINCL.  */
1221       dbxout_start_source_file (0, lastfile);
1222
1223       /* The base file when using the PCH won't be the same as
1224          the base file when it's being generated.  */
1225       lastfile = NULL;
1226     }
1227   else
1228     {
1229       /* ... and an EINCL.  */
1230       dbxout_end_source_file (0);
1231
1232       /* Deal with cases where 'lastfile' was never actually changed.  */
1233       lastfile_is_base = lastfile == NULL;
1234     }
1235 }
1236
1237 #if defined (DBX_DEBUGGING_INFO)
1238
1239 static void dbxout_block (tree, int, tree);
1240
1241 /* Output debugging info to FILE to switch to sourcefile FILENAME.  */
1242
1243 static void
1244 dbxout_source_file (const char *filename)
1245 {
1246   if (lastfile == 0 && lastfile_is_base)
1247     {
1248       lastfile = base_input_file;
1249       lastfile_is_base = 0;
1250     }
1251
1252   if (filename && (lastfile == 0 || strcmp (filename, lastfile)))
1253     {
1254       /* Don't change section amid function.  */
1255       if (current_function_decl == NULL_TREE)
1256         switch_to_section (text_section);
1257
1258       dbxout_begin_simple_stabs (remap_debug_filename (filename), N_SOL);
1259       dbxout_stab_value_internal_label ("Ltext", &source_label_number);
1260       lastfile = filename;
1261     }
1262 }
1263
1264 /* Output N_BNSYM, line number symbol entry, and local symbol at
1265    function scope  */
1266
1267 static void
1268 dbxout_begin_prologue (unsigned int lineno, const char *filename)
1269 {
1270   if (use_gnu_debug_info_extensions
1271       && !NO_DBX_FUNCTION_END
1272       && !NO_DBX_BNSYM_ENSYM
1273       && !flag_debug_only_used_symbols)
1274     dbxout_stabd (N_BNSYM, 0);
1275
1276   /* pre-increment the scope counter */
1277   scope_labelno++;
1278
1279   dbxout_source_line (lineno, filename, 0, true);
1280   /* Output function begin block at function scope, referenced
1281      by dbxout_block, dbxout_source_line and dbxout_function_end.  */
1282   emit_pending_bincls_if_required ();
1283   targetm.asm_out.internal_label (asm_out_file, "LFBB", scope_labelno);
1284 }
1285
1286 /* Output a line number symbol entry for source file FILENAME and line
1287    number LINENO.  */
1288
1289 static void
1290 dbxout_source_line (unsigned int lineno, const char *filename,
1291                     int discriminator ATTRIBUTE_UNUSED,
1292                     bool is_stmt ATTRIBUTE_UNUSED)
1293 {
1294   dbxout_source_file (filename);
1295
1296 #ifdef DBX_OUTPUT_SOURCE_LINE
1297   DBX_OUTPUT_SOURCE_LINE (asm_out_file, lineno, dbxout_source_line_counter);
1298 #else
1299   if (DBX_LINES_FUNCTION_RELATIVE)
1300     {
1301       char begin_label[20];
1302       dbxout_begin_stabn_sline (lineno);
1303       /* Reference current function start using LFBB.  */
1304       ASM_GENERATE_INTERNAL_LABEL (begin_label, "LFBB", scope_labelno);
1305       dbxout_stab_value_internal_label_diff ("LM", &dbxout_source_line_counter,
1306                                              begin_label);
1307     }
1308   else
1309     dbxout_stabd (N_SLINE, lineno);
1310 #endif
1311 }
1312
1313 /* Describe the beginning of an internal block within a function.  */
1314
1315 static void
1316 dbxout_begin_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int n)
1317 {
1318   emit_pending_bincls_if_required ();
1319   targetm.asm_out.internal_label (asm_out_file, "LBB", n);
1320 }
1321
1322 /* Describe the end line-number of an internal block within a function.  */
1323
1324 static void
1325 dbxout_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int n)
1326 {
1327   emit_pending_bincls_if_required ();
1328   targetm.asm_out.internal_label (asm_out_file, "LBE", n);
1329 }
1330
1331 /* Output dbx data for a function definition.
1332    This includes a definition of the function name itself (a symbol),
1333    definitions of the parameters (locating them in the parameter list)
1334    and then output the block that makes up the function's body
1335    (including all the auto variables of the function).  */
1336
1337 static void
1338 dbxout_function_decl (tree decl)
1339 {
1340   emit_pending_bincls_if_required ();
1341 #ifndef DBX_FUNCTION_FIRST
1342   dbxout_begin_function (decl);
1343 #endif
1344   dbxout_block (DECL_INITIAL (decl), 0, DECL_ARGUMENTS (decl));
1345   dbxout_function_end (decl);
1346 }
1347
1348 #endif /* DBX_DEBUGGING_INFO  */
1349
1350 /* Debug information for a global DECL.  Called from toplev.c after
1351    compilation proper has finished.  */
1352 static void
1353 dbxout_global_decl (tree decl)
1354 {
1355   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
1356     {
1357       int saved_tree_used = TREE_USED (decl);
1358       TREE_USED (decl) = 1;
1359       dbxout_symbol (decl, 0);
1360       TREE_USED (decl) = saved_tree_used;
1361     }
1362 }
1363
1364 /* This is just a function-type adapter; dbxout_symbol does exactly
1365    what we want but returns an int.  */
1366 static void
1367 dbxout_type_decl (tree decl, int local)
1368 {
1369   dbxout_symbol (decl, local);
1370 }
1371
1372 /* At the end of compilation, finish writing the symbol table.
1373    The default is to call debug_free_queue but do nothing else.  */
1374
1375 static void
1376 dbxout_finish (const char *filename ATTRIBUTE_UNUSED)
1377 {
1378 #ifdef DBX_OUTPUT_MAIN_SOURCE_FILE_END
1379   DBX_OUTPUT_MAIN_SOURCE_FILE_END (asm_out_file, filename);
1380 #elif defined DBX_OUTPUT_NULL_N_SO_AT_MAIN_SOURCE_FILE_END
1381  {
1382    switch_to_section (text_section);
1383    dbxout_begin_empty_stabs (N_SO);
1384    dbxout_stab_value_internal_label ("Letext", 0);
1385  }
1386 #endif
1387   debug_free_queue ();
1388 }
1389
1390 /* Output the index of a type.  */
1391
1392 static void
1393 dbxout_type_index (tree type)
1394 {
1395 #ifndef DBX_USE_BINCL
1396   stabstr_D (TYPE_SYMTAB_ADDRESS (type));
1397 #else
1398   struct typeinfo *t = &typevec[TYPE_SYMTAB_ADDRESS (type)];
1399   stabstr_C ('(');
1400   stabstr_D (t->file_number);
1401   stabstr_C (',');
1402   stabstr_D (t->type_number);
1403   stabstr_C (')');
1404 #endif
1405 }
1406
1407 \f
1408
1409 /* Used in several places: evaluates to '0' for a private decl,
1410    '1' for a protected decl, '2' for a public decl.  */
1411 #define DECL_ACCESSIBILITY_CHAR(DECL) \
1412 (TREE_PRIVATE (DECL) ? '0' : TREE_PROTECTED (DECL) ? '1' : '2')
1413
1414 /* Subroutine of `dbxout_type'.  Output the type fields of TYPE.
1415    This must be a separate function because anonymous unions require
1416    recursive calls.  */
1417
1418 static void
1419 dbxout_type_fields (tree type)
1420 {
1421   tree tem;
1422
1423   /* Output the name, type, position (in bits), size (in bits) of each
1424      field that we can support.  */
1425   for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
1426     {
1427       /* If one of the nodes is an error_mark or its type is then
1428          return early.  */
1429       if (tem == error_mark_node || TREE_TYPE (tem) == error_mark_node)
1430         return;
1431
1432       /* Omit here local type decls until we know how to support them.  */
1433       if (TREE_CODE (tem) == TYPE_DECL
1434           /* Omit here the nameless fields that are used to skip bits.  */
1435           || DECL_IGNORED_P (tem)
1436           /* Omit fields whose position or size are variable or too large to
1437              represent.  */
1438           || (TREE_CODE (tem) == FIELD_DECL
1439               && (! host_integerp (bit_position (tem), 0)
1440                   || ! DECL_SIZE (tem)
1441                   || ! host_integerp (DECL_SIZE (tem), 1))))
1442         continue;
1443
1444       else if (TREE_CODE (tem) != CONST_DECL)
1445         {
1446           /* Continue the line if necessary,
1447              but not before the first field.  */
1448           if (tem != TYPE_FIELDS (type))
1449             CONTIN;
1450
1451           if (DECL_NAME (tem))
1452             stabstr_I (DECL_NAME (tem));
1453           stabstr_C (':');
1454
1455           if (use_gnu_debug_info_extensions
1456               && (TREE_PRIVATE (tem) || TREE_PROTECTED (tem)
1457                   || TREE_CODE (tem) != FIELD_DECL))
1458             {
1459               stabstr_C ('/');
1460               stabstr_C (DECL_ACCESSIBILITY_CHAR (tem));
1461             }
1462
1463           dbxout_type ((TREE_CODE (tem) == FIELD_DECL
1464                         && DECL_BIT_FIELD_TYPE (tem))
1465                        ? DECL_BIT_FIELD_TYPE (tem) : TREE_TYPE (tem), 0);
1466
1467           if (TREE_CODE (tem) == VAR_DECL)
1468             {
1469               if (TREE_STATIC (tem) && use_gnu_debug_info_extensions)
1470                 {
1471                   tree name = DECL_ASSEMBLER_NAME (tem);
1472
1473                   stabstr_C (':');
1474                   stabstr_I (name);
1475                   stabstr_C (';');
1476                 }
1477               else
1478                 /* If TEM is non-static, GDB won't understand it.  */
1479                 stabstr_S (",0,0;");
1480             }
1481           else
1482             {
1483               stabstr_C (',');
1484               stabstr_D (int_bit_position (tem));
1485               stabstr_C (',');
1486               stabstr_D (tree_low_cst (DECL_SIZE (tem), 1));
1487               stabstr_C (';');
1488             }
1489         }
1490     }
1491 }
1492 \f
1493 /* Subroutine of `dbxout_type_methods'.  Output debug info about the
1494    method described DECL.  */
1495
1496 static void
1497 dbxout_type_method_1 (tree decl)
1498 {
1499   char c1 = 'A', c2;
1500
1501   if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
1502     c2 = '?';
1503   else /* it's a METHOD_TYPE.  */
1504     {
1505       tree firstarg = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)));
1506       /* A for normal functions.
1507          B for `const' member functions.
1508          C for `volatile' member functions.
1509          D for `const volatile' member functions.  */
1510       if (TYPE_READONLY (TREE_TYPE (firstarg)))
1511         c1 += 1;
1512       if (TYPE_VOLATILE (TREE_TYPE (firstarg)))
1513         c1 += 2;
1514
1515       if (DECL_VINDEX (decl))
1516         c2 = '*';
1517       else
1518         c2 = '.';
1519     }
1520
1521   /* ??? Output the mangled name, which contains an encoding of the
1522      method's type signature.  May not be necessary anymore.  */
1523   stabstr_C (':');
1524   stabstr_I (DECL_ASSEMBLER_NAME (decl));
1525   stabstr_C (';');
1526   stabstr_C (DECL_ACCESSIBILITY_CHAR (decl));
1527   stabstr_C (c1);
1528   stabstr_C (c2);
1529
1530   if (DECL_VINDEX (decl) && host_integerp (DECL_VINDEX (decl), 0))
1531     {
1532       stabstr_D (tree_low_cst (DECL_VINDEX (decl), 0));
1533       stabstr_C (';');
1534       dbxout_type (DECL_CONTEXT (decl), 0);
1535       stabstr_C (';');
1536     }
1537 }
1538 \f
1539 /* Subroutine of `dbxout_type'.  Output debug info about the methods defined
1540    in TYPE.  */
1541
1542 static void
1543 dbxout_type_methods (tree type)
1544 {
1545   /* C++: put out the method names and their parameter lists */
1546   tree methods = TYPE_METHODS (type);
1547   tree fndecl;
1548   tree last;
1549
1550   if (methods == NULL_TREE)
1551     return;
1552
1553   if (TREE_CODE (methods) != TREE_VEC)
1554     fndecl = methods;
1555   else if (TREE_VEC_ELT (methods, 0) != NULL_TREE)
1556     fndecl = TREE_VEC_ELT (methods, 0);
1557   else
1558     fndecl = TREE_VEC_ELT (methods, 1);
1559
1560   while (fndecl)
1561     {
1562       int need_prefix = 1;
1563
1564       /* Group together all the methods for the same operation.
1565          These differ in the types of the arguments.  */
1566       for (last = NULL_TREE;
1567            fndecl && (last == NULL_TREE || DECL_NAME (fndecl) == DECL_NAME (last));
1568            fndecl = TREE_CHAIN (fndecl))
1569         /* Output the name of the field (after overloading), as
1570            well as the name of the field before overloading, along
1571            with its parameter list */
1572         {
1573           /* Skip methods that aren't FUNCTION_DECLs.  (In C++, these
1574              include TEMPLATE_DECLs.)  The debugger doesn't know what
1575              to do with such entities anyhow.  */
1576           if (TREE_CODE (fndecl) != FUNCTION_DECL)
1577             continue;
1578
1579           CONTIN;
1580
1581           last = fndecl;
1582
1583           /* Also ignore abstract methods; those are only interesting to
1584              the DWARF backends.  */
1585           if (DECL_IGNORED_P (fndecl) || DECL_ABSTRACT (fndecl))
1586             continue;
1587
1588           /* Redundantly output the plain name, since that's what gdb
1589              expects.  */
1590           if (need_prefix)
1591             {
1592               stabstr_I (DECL_NAME (fndecl));
1593               stabstr_S ("::");
1594               need_prefix = 0;
1595             }
1596
1597           dbxout_type (TREE_TYPE (fndecl), 0);
1598           dbxout_type_method_1 (fndecl);
1599         }
1600       if (!need_prefix)
1601         stabstr_C (';');
1602     }
1603 }
1604
1605 /* Emit a "range" type specification, which has the form:
1606    "r<index type>;<lower bound>;<upper bound>;".
1607    TYPE is an INTEGER_TYPE, LOW and HIGH are the bounds.  */
1608
1609 static void
1610 dbxout_range_type (tree type, tree low, tree high)
1611 {
1612   stabstr_C ('r');
1613   if (TREE_TYPE (type))
1614     dbxout_type (TREE_TYPE (type), 0);
1615   else if (TREE_CODE (type) != INTEGER_TYPE)
1616     dbxout_type (type, 0); /* E.g. Pascal's ARRAY [BOOLEAN] of INTEGER */
1617   else
1618     {
1619       /* Traditionally, we made sure 'int' was type 1, and builtin types
1620          were defined to be sub-ranges of int.  Unfortunately, this
1621          does not allow us to distinguish true sub-ranges from integer
1622          types.  So, instead we define integer (non-sub-range) types as
1623          sub-ranges of themselves.  This matters for Chill.  If this isn't
1624          a subrange type, then we want to define it in terms of itself.
1625          However, in C, this may be an anonymous integer type, and we don't
1626          want to emit debug info referring to it.  Just calling
1627          dbxout_type_index won't work anyways, because the type hasn't been
1628          defined yet.  We make this work for both cases by checked to see
1629          whether this is a defined type, referring to it if it is, and using
1630          'int' otherwise.  */
1631       if (TYPE_SYMTAB_ADDRESS (type) != 0)
1632         dbxout_type_index (type);
1633       else
1634         dbxout_type_index (integer_type_node);
1635     }
1636
1637   stabstr_C (';');
1638   if (low && host_integerp (low, 0))
1639     {
1640       if (print_int_cst_bounds_in_octal_p (type, low, high))
1641         stabstr_O (low);
1642       else
1643         stabstr_D (tree_low_cst (low, 0));
1644     }
1645   else
1646     stabstr_C ('0');
1647
1648   stabstr_C (';');
1649   if (high && host_integerp (high, 0))
1650     {
1651       if (print_int_cst_bounds_in_octal_p (type, low, high))
1652         stabstr_O (high);
1653       else
1654         stabstr_D (tree_low_cst (high, 0));
1655       stabstr_C (';');
1656     }
1657   else
1658     stabstr_S ("-1;");
1659 }
1660 \f
1661
1662 /* Output a reference to a type.  If the type has not yet been
1663    described in the dbx output, output its definition now.
1664    For a type already defined, just refer to its definition
1665    using the type number.
1666
1667    If FULL is nonzero, and the type has been described only with
1668    a forward-reference, output the definition now.
1669    If FULL is zero in this case, just refer to the forward-reference
1670    using the number previously allocated.  */
1671
1672 static void
1673 dbxout_type (tree type, int full)
1674 {
1675   static int anonymous_type_number = 0;
1676   bool vector_type = false;
1677   tree tem, main_variant, low, high;
1678
1679   if (TREE_CODE (type) == VECTOR_TYPE)
1680     {
1681       /* The frontend feeds us a representation for the vector as a struct
1682          containing an array.  Pull out the array type.  */
1683       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
1684       vector_type = true;
1685     }
1686
1687   if (TREE_CODE (type) == INTEGER_TYPE)
1688     {
1689       if (TREE_TYPE (type) == 0)
1690         {
1691           low = TYPE_MIN_VALUE (type);
1692           high = TYPE_MAX_VALUE (type);
1693         }
1694
1695       else if (subrange_type_for_debug_p (type, &low, &high))
1696         ;
1697
1698       /* If this is a subtype that should not be emitted as a subrange type,
1699          use the base type.  */
1700       else
1701         {
1702           type = TREE_TYPE (type);
1703           low = TYPE_MIN_VALUE (type);
1704           high = TYPE_MAX_VALUE (type);
1705         }
1706     }
1707
1708   /* If there was an input error and we don't really have a type,
1709      avoid crashing and write something that is at least valid
1710      by assuming `int'.  */
1711   if (type == error_mark_node)
1712     type = integer_type_node;
1713   else
1714     {
1715       if (TYPE_NAME (type)
1716           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1717           && TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
1718         full = 0;
1719     }
1720
1721   /* Try to find the "main variant" with the same name.  */
1722   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1723       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
1724     main_variant = TREE_TYPE (TYPE_NAME (type));
1725   else
1726     main_variant = TYPE_MAIN_VARIANT (type);
1727
1728   /* If we are not using extensions, stabs does not distinguish const and
1729      volatile, so there is no need to make them separate types.  */
1730   if (!use_gnu_debug_info_extensions)
1731     type = main_variant;
1732
1733   if (TYPE_SYMTAB_ADDRESS (type) == 0)
1734     {
1735       /* Type has no dbx number assigned.  Assign next available number.  */
1736       TYPE_SYMTAB_ADDRESS (type) = next_type_number++;
1737
1738       /* Make sure type vector is long enough to record about this type.  */
1739
1740       if (next_type_number == typevec_len)
1741         {
1742           typevec = GGC_RESIZEVEC (struct typeinfo, typevec, typevec_len * 2);
1743           memset (typevec + typevec_len, 0, typevec_len * sizeof typevec[0]);
1744           typevec_len *= 2;
1745         }
1746
1747 #ifdef DBX_USE_BINCL
1748       emit_pending_bincls_if_required ();
1749       typevec[TYPE_SYMTAB_ADDRESS (type)].file_number
1750         = current_file->file_number;
1751       typevec[TYPE_SYMTAB_ADDRESS (type)].type_number
1752         = current_file->next_type_number++;
1753 #endif
1754     }
1755
1756   if (flag_debug_only_used_symbols)
1757     {
1758       if ((TREE_CODE (type) == RECORD_TYPE
1759            || TREE_CODE (type) == UNION_TYPE
1760            || TREE_CODE (type) == QUAL_UNION_TYPE
1761            || TREE_CODE (type) == ENUMERAL_TYPE)
1762           && TYPE_STUB_DECL (type)
1763           && DECL_P (TYPE_STUB_DECL (type))
1764           && ! DECL_IGNORED_P (TYPE_STUB_DECL (type)))
1765         debug_queue_symbol (TYPE_STUB_DECL (type));
1766       else if (TYPE_NAME (type)
1767                && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
1768         debug_queue_symbol (TYPE_NAME (type));
1769     }
1770
1771   /* Output the number of this type, to refer to it.  */
1772   dbxout_type_index (type);
1773
1774 #ifdef DBX_TYPE_DEFINED
1775   if (DBX_TYPE_DEFINED (type))
1776     return;
1777 #endif
1778
1779   /* If this type's definition has been output or is now being output,
1780      that is all.  */
1781
1782   switch (typevec[TYPE_SYMTAB_ADDRESS (type)].status)
1783     {
1784     case TYPE_UNSEEN:
1785       break;
1786     case TYPE_XREF:
1787       /* If we have already had a cross reference,
1788          and either that's all we want or that's the best we could do,
1789          don't repeat the cross reference.
1790          Sun dbx crashes if we do.  */
1791       if (! full || !COMPLETE_TYPE_P (type)
1792           /* No way in DBX fmt to describe a variable size.  */
1793           || ! host_integerp (TYPE_SIZE (type), 1))
1794         return;
1795       break;
1796     case TYPE_DEFINED:
1797       return;
1798     }
1799
1800 #ifdef DBX_NO_XREFS
1801   /* For systems where dbx output does not allow the `=xsNAME:' syntax,
1802      leave the type-number completely undefined rather than output
1803      a cross-reference.  If we have already used GNU debug info extensions,
1804      then it is OK to output a cross reference.  This is necessary to get
1805      proper C++ debug output.  */
1806   if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
1807        || TREE_CODE (type) == QUAL_UNION_TYPE
1808        || TREE_CODE (type) == ENUMERAL_TYPE)
1809       && ! use_gnu_debug_info_extensions)
1810     /* We must use the same test here as we use twice below when deciding
1811        whether to emit a cross-reference.  */
1812     if ((TYPE_NAME (type) != 0
1813          && ! (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1814                && DECL_IGNORED_P (TYPE_NAME (type)))
1815          && !full)
1816         || !COMPLETE_TYPE_P (type)
1817         /* No way in DBX fmt to describe a variable size.  */
1818         || ! host_integerp (TYPE_SIZE (type), 1))
1819       {
1820         typevec[TYPE_SYMTAB_ADDRESS (type)].status = TYPE_XREF;
1821         return;
1822       }
1823 #endif
1824
1825   /* Output a definition now.  */
1826   stabstr_C ('=');
1827
1828   /* Mark it as defined, so that if it is self-referent
1829      we will not get into an infinite recursion of definitions.  */
1830
1831   typevec[TYPE_SYMTAB_ADDRESS (type)].status = TYPE_DEFINED;
1832
1833   /* If this type is a variant of some other, hand off.  Types with
1834      different names are usefully distinguished.  We only distinguish
1835      cv-qualified types if we're using extensions.  */
1836   if (TYPE_READONLY (type) > TYPE_READONLY (main_variant))
1837     {
1838       stabstr_C ('k');
1839       dbxout_type (build_type_variant (type, 0, TYPE_VOLATILE (type)), 0);
1840       return;
1841     }
1842   else if (TYPE_VOLATILE (type) > TYPE_VOLATILE (main_variant))
1843     {
1844       stabstr_C ('B');
1845       dbxout_type (build_type_variant (type, TYPE_READONLY (type), 0), 0);
1846       return;
1847     }
1848   else if (main_variant != TYPE_MAIN_VARIANT (type))
1849     {
1850       if (flag_debug_only_used_symbols)
1851         {
1852           tree orig_type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
1853
1854           if ((TREE_CODE (orig_type) == RECORD_TYPE
1855                || TREE_CODE (orig_type) == UNION_TYPE
1856                || TREE_CODE (orig_type) == QUAL_UNION_TYPE
1857                || TREE_CODE (orig_type) == ENUMERAL_TYPE)
1858               && TYPE_STUB_DECL (orig_type)
1859               && ! DECL_IGNORED_P (TYPE_STUB_DECL (orig_type)))
1860             debug_queue_symbol (TYPE_STUB_DECL (orig_type));
1861         }
1862       /* 'type' is a typedef; output the type it refers to.  */
1863       dbxout_type (DECL_ORIGINAL_TYPE (TYPE_NAME (type)), 0);
1864       return;
1865     }
1866   /* else continue.  */
1867
1868   switch (TREE_CODE (type))
1869     {
1870     case VOID_TYPE:
1871     case LANG_TYPE:
1872       /* For a void type, just define it as itself; i.e., "5=5".
1873          This makes us consider it defined
1874          without saying what it is.  The debugger will make it
1875          a void type when the reference is seen, and nothing will
1876          ever override that default.  */
1877       dbxout_type_index (type);
1878       break;
1879
1880     case INTEGER_TYPE:
1881       if (type == char_type_node && ! TYPE_UNSIGNED (type))
1882         {
1883           /* Output the type `char' as a subrange of itself!
1884              I don't understand this definition, just copied it
1885              from the output of pcc.
1886              This used to use `r2' explicitly and we used to
1887              take care to make sure that `char' was type number 2.  */
1888           stabstr_C ('r');
1889           dbxout_type_index (type);
1890           stabstr_S (";0;127;");
1891         }
1892
1893       /* If this is a subtype of another integer type, always prefer to
1894          write it as a subtype.  */
1895       else if (TREE_TYPE (type) != 0
1896                && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
1897         {
1898           /* If the size is non-standard, say what it is if we can use
1899              GDB extensions.  */
1900
1901           if (use_gnu_debug_info_extensions
1902               && TYPE_PRECISION (type) != TYPE_PRECISION (integer_type_node))
1903             {
1904               stabstr_S ("@s");
1905               stabstr_D (TYPE_PRECISION (type));
1906               stabstr_C (';');
1907             }
1908
1909           dbxout_range_type (type, low, high);
1910         }
1911
1912       else
1913         {
1914           /* If the size is non-standard, say what it is if we can use
1915              GDB extensions.  */
1916
1917           if (use_gnu_debug_info_extensions
1918               && TYPE_PRECISION (type) != TYPE_PRECISION (integer_type_node))
1919             {
1920               stabstr_S ("@s");
1921               stabstr_D (TYPE_PRECISION (type));
1922               stabstr_C (';');
1923             }
1924
1925           if (print_int_cst_bounds_in_octal_p (type, low, high))
1926             {
1927               stabstr_C ('r');
1928
1929               /* If this type derives from another type, output type index of
1930                  parent type. This is particularly important when parent type
1931                  is an enumerated type, because not generating the parent type
1932                  index would transform the definition of this enumerated type
1933                  into a plain unsigned type.  */
1934               if (TREE_TYPE (type) != 0)
1935                 dbxout_type_index (TREE_TYPE (type));
1936               else
1937                 dbxout_type_index (type);
1938
1939               stabstr_C (';');
1940               stabstr_O (low);
1941               stabstr_C (';');
1942               stabstr_O (high);
1943               stabstr_C (';');
1944             }
1945
1946           else
1947             /* Output other integer types as subranges of `int'.  */
1948             dbxout_range_type (type, low, high);
1949         }
1950
1951       break;
1952
1953     case REAL_TYPE:
1954     case FIXED_POINT_TYPE:
1955       /* This used to say `r1' and we used to take care
1956          to make sure that `int' was type number 1.  */
1957       stabstr_C ('r');
1958       dbxout_type_index (integer_type_node);
1959       stabstr_C (';');
1960       stabstr_D (int_size_in_bytes (type));
1961       stabstr_S (";0;");
1962       break;
1963
1964     case BOOLEAN_TYPE:
1965       if (use_gnu_debug_info_extensions)
1966         {
1967           stabstr_S ("@s");
1968           stabstr_D (BITS_PER_UNIT * int_size_in_bytes (type));
1969           stabstr_S (";-16;");
1970         }
1971       else /* Define as enumeral type (False, True) */
1972         stabstr_S ("eFalse:0,True:1,;");
1973       break;
1974
1975     case COMPLEX_TYPE:
1976       /* Differs from the REAL_TYPE by its new data type number.
1977          R3 is NF_COMPLEX.  We don't try to use any of the other NF_*
1978          codes since gdb doesn't care anyway.  */
1979
1980       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
1981         {
1982           stabstr_S ("R3;");
1983           stabstr_D (2 * int_size_in_bytes (TREE_TYPE (type)));
1984           stabstr_S (";0;");
1985         }
1986       else
1987         {
1988           /* Output a complex integer type as a structure,
1989              pending some other way to do it.  */
1990           stabstr_C ('s');
1991           stabstr_D (int_size_in_bytes (type));
1992
1993           stabstr_S ("real:");
1994           dbxout_type (TREE_TYPE (type), 0);
1995           stabstr_S (",0,");
1996           stabstr_D (TYPE_PRECISION (TREE_TYPE (type)));
1997
1998           stabstr_S (";imag:");
1999           dbxout_type (TREE_TYPE (type), 0);
2000           stabstr_C (',');
2001           stabstr_D (TYPE_PRECISION (TREE_TYPE (type)));
2002           stabstr_C (',');
2003           stabstr_D (TYPE_PRECISION (TREE_TYPE (type)));
2004           stabstr_S (";;");
2005         }
2006       break;
2007
2008     case ARRAY_TYPE:
2009       /* Make arrays of packed bits look like bitstrings for chill.  */
2010       if (TYPE_PACKED (type) && use_gnu_debug_info_extensions)
2011         {
2012           stabstr_S ("@s");
2013           stabstr_D (BITS_PER_UNIT * int_size_in_bytes (type));
2014           stabstr_S (";@S;S");
2015           dbxout_type (TYPE_DOMAIN (type), 0);
2016           break;
2017         }
2018
2019       if (use_gnu_debug_info_extensions && vector_type)
2020         stabstr_S ("@V;");
2021
2022       /* Output "a" followed by a range type definition
2023          for the index type of the array
2024          followed by a reference to the target-type.
2025          ar1;0;N;M for a C array of type M and size N+1.  */
2026       /* Check if a character string type, which in Chill is
2027          different from an array of characters.  */
2028       if (TYPE_STRING_FLAG (type) && use_gnu_debug_info_extensions)
2029         {
2030           stabstr_S ("@S;");
2031         }
2032       tem = TYPE_DOMAIN (type);
2033       if (tem == NULL)
2034         {
2035           stabstr_S ("ar");
2036           dbxout_type_index (integer_type_node);
2037           stabstr_S (";0;-1;");
2038         }
2039       else
2040         {
2041           stabstr_C ('a');
2042           dbxout_range_type (tem, TYPE_MIN_VALUE (tem), TYPE_MAX_VALUE (tem));
2043         }
2044
2045       dbxout_type (TREE_TYPE (type), 0);
2046       break;
2047
2048     case RECORD_TYPE:
2049     case UNION_TYPE:
2050     case QUAL_UNION_TYPE:
2051       {
2052         tree binfo = TYPE_BINFO (type);
2053
2054         /* Output a structure type.  We must use the same test here as we
2055            use in the DBX_NO_XREFS case above.  */
2056         if ((TYPE_NAME (type) != 0
2057              && ! (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2058                    && DECL_IGNORED_P (TYPE_NAME (type)))
2059              && !full)
2060             || !COMPLETE_TYPE_P (type)
2061             /* No way in DBX fmt to describe a variable size.  */
2062             || ! host_integerp (TYPE_SIZE (type), 1))
2063           {
2064             /* If the type is just a cross reference, output one
2065                and mark the type as partially described.
2066                If it later becomes defined, we will output
2067                its real definition.
2068                If the type has a name, don't nest its definition within
2069                another type's definition; instead, output an xref
2070                and let the definition come when the name is defined.  */
2071             stabstr_S ((TREE_CODE (type) == RECORD_TYPE) ? "xs" : "xu");
2072             if (TYPE_NAME (type) != 0
2073                 /* The C frontend creates for anonymous variable length
2074                    records/unions TYPE_NAME with DECL_NAME NULL.  */
2075                 && (TREE_CODE (TYPE_NAME (type)) != TYPE_DECL
2076                     || DECL_NAME (TYPE_NAME (type))))
2077               dbxout_type_name (type);
2078             else
2079               {
2080                 stabstr_S ("$$");
2081                 stabstr_D (anonymous_type_number++);
2082               }
2083
2084             stabstr_C (':');
2085             typevec[TYPE_SYMTAB_ADDRESS (type)].status = TYPE_XREF;
2086             break;
2087           }
2088
2089         /* Identify record or union, and print its size.  */
2090         stabstr_C ((TREE_CODE (type) == RECORD_TYPE) ? 's' : 'u');
2091         stabstr_D (int_size_in_bytes (type));
2092
2093         if (binfo)
2094           {
2095             int i;
2096             tree child;
2097             VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
2098
2099             if (use_gnu_debug_info_extensions)
2100               {
2101                 if (BINFO_N_BASE_BINFOS (binfo))
2102                   {
2103                     stabstr_C ('!');
2104                     stabstr_U (BINFO_N_BASE_BINFOS (binfo));
2105                     stabstr_C (',');
2106                   }
2107               }
2108             for (i = 0; BINFO_BASE_ITERATE (binfo, i, child); i++)
2109               {
2110                 tree access = (accesses ? VEC_index (tree, accesses, i)
2111                                : access_public_node);
2112
2113                 if (use_gnu_debug_info_extensions)
2114                   {
2115                     stabstr_C (BINFO_VIRTUAL_P (child) ? '1' : '0');
2116                     stabstr_C (access == access_public_node ? '2' :
2117                                    access == access_protected_node
2118                                    ? '1' :'0');
2119                     if (BINFO_VIRTUAL_P (child)
2120                         && (strcmp (lang_hooks.name, "GNU C++") == 0
2121                             || strcmp (lang_hooks.name, "GNU Objective-C++") == 0))
2122                       /* For a virtual base, print the (negative)
2123                          offset within the vtable where we must look
2124                          to find the necessary adjustment.  */
2125                       stabstr_D
2126                         (tree_low_cst (BINFO_VPTR_FIELD (child), 0)
2127                          * BITS_PER_UNIT);
2128                     else
2129                       stabstr_D (tree_low_cst (BINFO_OFFSET (child), 0)
2130                                        * BITS_PER_UNIT);
2131                     stabstr_C (',');
2132                     dbxout_type (BINFO_TYPE (child), 0);
2133                     stabstr_C (';');
2134                   }
2135                 else
2136                   {
2137                     /* Print out the base class information with
2138                        fields which have the same names at the types
2139                        they hold.  */
2140                     dbxout_type_name (BINFO_TYPE (child));
2141                     stabstr_C (':');
2142                     dbxout_type (BINFO_TYPE (child), full);
2143                     stabstr_C (',');
2144                     stabstr_D (tree_low_cst (BINFO_OFFSET (child), 0)
2145                                      * BITS_PER_UNIT);
2146                     stabstr_C (',');
2147                     stabstr_D
2148                       (tree_low_cst (TYPE_SIZE (BINFO_TYPE (child)), 0)
2149                        * BITS_PER_UNIT);
2150                     stabstr_C (';');
2151                   }
2152               }
2153           }
2154       }
2155
2156       /* Write out the field declarations.  */
2157       dbxout_type_fields (type);
2158       if (use_gnu_debug_info_extensions && TYPE_METHODS (type) != NULL_TREE)
2159         {
2160           dbxout_type_methods (type);
2161         }
2162
2163       stabstr_C (';');
2164
2165       if (use_gnu_debug_info_extensions && TREE_CODE (type) == RECORD_TYPE
2166           /* Avoid the ~ if we don't really need it--it confuses dbx.  */
2167           && TYPE_VFIELD (type))
2168         {
2169
2170           /* We need to write out info about what field this class
2171              uses as its "main" vtable pointer field, because if this
2172              field is inherited from a base class, GDB cannot necessarily
2173              figure out which field it's using in time.  */
2174           stabstr_S ("~%");
2175           dbxout_type (DECL_FCONTEXT (TYPE_VFIELD (type)), 0);
2176           stabstr_C (';');
2177         }
2178       break;
2179
2180     case ENUMERAL_TYPE:
2181       /* We must use the same test here as we use in the DBX_NO_XREFS case
2182          above.  We simplify it a bit since an enum will never have a variable
2183          size.  */
2184       if ((TYPE_NAME (type) != 0
2185            && ! (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2186                  && DECL_IGNORED_P (TYPE_NAME (type)))
2187            && !full)
2188           || !COMPLETE_TYPE_P (type))
2189         {
2190           stabstr_S ("xe");
2191           dbxout_type_name (type);
2192           typevec[TYPE_SYMTAB_ADDRESS (type)].status = TYPE_XREF;
2193           stabstr_C (':');
2194           return;
2195         }
2196       if (use_gnu_debug_info_extensions
2197           && TYPE_PRECISION (type) != TYPE_PRECISION (integer_type_node))
2198         {
2199           stabstr_S ("@s");
2200           stabstr_D (TYPE_PRECISION (type));
2201           stabstr_C (';');
2202         }
2203
2204       stabstr_C ('e');
2205       for (tem = TYPE_VALUES (type); tem; tem = TREE_CHAIN (tem))
2206         {
2207           tree value = TREE_VALUE (tem);
2208
2209           stabstr_I (TREE_PURPOSE (tem));
2210           stabstr_C (':');
2211
2212           if (TREE_CODE (value) == CONST_DECL)
2213             value = DECL_INITIAL (value);
2214
2215           if (TREE_INT_CST_HIGH (value) == 0)
2216             stabstr_D (TREE_INT_CST_LOW (value));
2217           else if (TREE_INT_CST_HIGH (value) == -1
2218                    && (HOST_WIDE_INT) TREE_INT_CST_LOW (value) < 0)
2219             stabstr_D (TREE_INT_CST_LOW (value));
2220           else
2221             stabstr_O (value);
2222
2223           stabstr_C (',');
2224           if (TREE_CHAIN (tem) != 0)
2225             CONTIN;
2226         }
2227
2228       stabstr_C (';');
2229       break;
2230
2231     case POINTER_TYPE:
2232       stabstr_C ('*');
2233       dbxout_type (TREE_TYPE (type), 0);
2234       break;
2235
2236     case METHOD_TYPE:
2237       if (use_gnu_debug_info_extensions)
2238         {
2239           stabstr_C ('#');
2240
2241           /* Write the argument types out longhand.  */
2242           dbxout_type (TYPE_METHOD_BASETYPE (type), 0);
2243           stabstr_C (',');
2244           dbxout_type (TREE_TYPE (type), 0);
2245           dbxout_args (TYPE_ARG_TYPES (type));
2246           stabstr_C (';');
2247         }
2248       else
2249         /* Treat it as a function type.  */
2250         dbxout_type (TREE_TYPE (type), 0);
2251       break;
2252
2253     case OFFSET_TYPE:
2254       if (use_gnu_debug_info_extensions)
2255         {
2256           stabstr_C ('@');
2257           dbxout_type (TYPE_OFFSET_BASETYPE (type), 0);
2258           stabstr_C (',');
2259           dbxout_type (TREE_TYPE (type), 0);
2260         }
2261       else
2262         /* Should print as an int, because it is really just an offset.  */
2263         dbxout_type (integer_type_node, 0);
2264       break;
2265
2266     case REFERENCE_TYPE:
2267       if (use_gnu_debug_info_extensions)
2268         {
2269           stabstr_C ('&');
2270         }
2271       else
2272         stabstr_C ('*');
2273       dbxout_type (TREE_TYPE (type), 0);
2274       break;
2275
2276     case FUNCTION_TYPE:
2277       stabstr_C ('f');
2278       dbxout_type (TREE_TYPE (type), 0);
2279       break;
2280
2281     default:
2282       gcc_unreachable ();
2283     }
2284 }
2285
2286 /* Return nonzero if the given type represents an integer whose bounds
2287    should be printed in octal format.  */
2288
2289 static bool
2290 print_int_cst_bounds_in_octal_p (tree type, tree low, tree high)
2291 {
2292   /* If we can use GDB extensions and the size is wider than a long
2293      (the size used by GDB to read them) or we may have trouble writing
2294      the bounds the usual way, write them in octal.  Note the test is for
2295      the *target's* size of "long", not that of the host.  The host test
2296      is just to make sure we can write it out in case the host wide int
2297      is narrower than the target "long".
2298
2299      For unsigned types, we use octal if they are the same size or larger.
2300      This is because we print the bounds as signed decimal, and hence they
2301      can't span same size unsigned types.  */
2302
2303   if (use_gnu_debug_info_extensions
2304       && low && TREE_CODE (low) == INTEGER_CST
2305       && high && TREE_CODE (high) == INTEGER_CST
2306       && (TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)
2307           || ((TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2308               && TYPE_UNSIGNED (type))
2309           || TYPE_PRECISION (type) > HOST_BITS_PER_WIDE_INT
2310           || (TYPE_PRECISION (type) == HOST_BITS_PER_WIDE_INT
2311               && TYPE_UNSIGNED (type))))
2312     return TRUE;
2313   else
2314     return FALSE;
2315 }
2316
2317 /* Output the name of type TYPE, with no punctuation.
2318    Such names can be set up either by typedef declarations
2319    or by struct, enum and union tags.  */
2320
2321 static void
2322 dbxout_type_name (tree type)
2323 {
2324   tree t = TYPE_NAME (type);
2325
2326   gcc_assert (t);
2327   switch (TREE_CODE (t))
2328     {
2329     case IDENTIFIER_NODE:
2330       break;
2331     case TYPE_DECL:
2332       t = DECL_NAME (t);
2333       break;
2334     default:
2335       gcc_unreachable ();
2336     }
2337
2338   stabstr_I (t);
2339 }
2340
2341 /* Output leading leading struct or class names needed for qualifying
2342    type whose scope is limited to a struct or class.  */
2343
2344 static void
2345 dbxout_class_name_qualifiers (tree decl)
2346 {
2347   tree context = decl_type_context (decl);
2348
2349   if (context != NULL_TREE
2350       && TREE_CODE(context) == RECORD_TYPE
2351       && TYPE_NAME (context) != 0
2352       && (TREE_CODE (TYPE_NAME (context)) == IDENTIFIER_NODE
2353           || (DECL_NAME (TYPE_NAME (context)) != 0)))
2354     {
2355       tree name = TYPE_NAME (context);
2356
2357       if (TREE_CODE (name) == TYPE_DECL)
2358         {
2359           dbxout_class_name_qualifiers (name);
2360           name = DECL_NAME (name);
2361         }
2362       stabstr_I (name);
2363       stabstr_S ("::");
2364     }
2365 }
2366 \f
2367 /* This is a specialized subset of expand_expr for use by dbxout_symbol in
2368    evaluating DECL_VALUE_EXPR.  In particular, we stop if we find decls that
2369    haven't been expanded, or if the expression is getting so complex we won't
2370    be able to represent it in stabs anyway.  Returns NULL on failure.  */
2371
2372 static rtx
2373 dbxout_expand_expr (tree expr)
2374 {
2375   switch (TREE_CODE (expr))
2376     {
2377     case VAR_DECL:
2378       /* We can't handle emulated tls variables, because the address is an
2379          offset to the return value of __emutls_get_address, and there is no
2380          way to express that in stabs.  Also, there are name mangling issues
2381          here.  We end up with references to undefined symbols if we don't
2382          disable debug info for these variables.  */
2383       if (!targetm.have_tls && DECL_THREAD_LOCAL_P (expr))
2384         return NULL;
2385       /* FALLTHRU */
2386
2387     case PARM_DECL:
2388       if (DECL_HAS_VALUE_EXPR_P (expr))
2389         return dbxout_expand_expr (DECL_VALUE_EXPR (expr));
2390       /* FALLTHRU */
2391
2392     case CONST_DECL:
2393     case RESULT_DECL:
2394       return DECL_RTL_IF_SET (expr);
2395
2396     case INTEGER_CST:
2397       return expand_expr (expr, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2398
2399     case COMPONENT_REF:
2400     case ARRAY_REF:
2401     case ARRAY_RANGE_REF:
2402     case BIT_FIELD_REF:
2403       {
2404         enum machine_mode mode;
2405         HOST_WIDE_INT bitsize, bitpos;
2406         tree offset, tem;
2407         int volatilep = 0, unsignedp = 0;
2408         rtx x;
2409
2410         tem = get_inner_reference (expr, &bitsize, &bitpos, &offset,
2411                                    &mode, &unsignedp, &volatilep, true);
2412
2413         x = dbxout_expand_expr (tem);
2414         if (x == NULL || !MEM_P (x))
2415           return NULL;
2416         if (offset != NULL)
2417           {
2418             if (!host_integerp (offset, 0))
2419               return NULL;
2420             x = adjust_address_nv (x, mode, tree_low_cst (offset, 0));
2421           }
2422         if (bitpos != 0)
2423           x = adjust_address_nv (x, mode, bitpos / BITS_PER_UNIT);
2424
2425         return x;
2426       }
2427
2428     default:
2429       return NULL;
2430     }
2431 }
2432
2433 /* Helper function for output_used_types.  Queue one entry from the
2434    used types hash to be output.  */
2435
2436 static int
2437 output_used_types_helper (void **slot, void *data)
2438 {
2439   tree type = (tree) *slot;
2440   VEC(tree, heap) **types_p = (VEC(tree, heap) **) data;
2441
2442   if ((TREE_CODE (type) == RECORD_TYPE
2443        || TREE_CODE (type) == UNION_TYPE
2444        || TREE_CODE (type) == QUAL_UNION_TYPE
2445        || TREE_CODE (type) == ENUMERAL_TYPE)
2446       && TYPE_STUB_DECL (type)
2447       && DECL_P (TYPE_STUB_DECL (type))
2448       && ! DECL_IGNORED_P (TYPE_STUB_DECL (type)))
2449     VEC_quick_push (tree, *types_p, TYPE_STUB_DECL (type));
2450   else if (TYPE_NAME (type)
2451            && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2452     VEC_quick_push (tree, *types_p, TYPE_NAME (type));
2453
2454   return 1;
2455 }
2456
2457 /* This is a qsort callback which sorts types and declarations into a
2458    predictable order (types, then declarations, sorted by UID
2459    within).  */
2460
2461 static int
2462 output_types_sort (const void *pa, const void *pb)
2463 {
2464   const tree lhs = *((const tree *)pa);
2465   const tree rhs = *((const tree *)pb);
2466
2467   if (TYPE_P (lhs))
2468     {
2469       if (TYPE_P (rhs))
2470         return TYPE_UID (lhs) - TYPE_UID (rhs);
2471       else
2472         return 1;
2473     }
2474   else
2475     {
2476       if (TYPE_P (rhs))
2477         return -1;
2478       else
2479         return DECL_UID (lhs) - DECL_UID (rhs);
2480     }
2481 }
2482
2483
2484 /* Force all types used by this function to be output in debug
2485    information.  */
2486
2487 static void
2488 output_used_types (void)
2489 {
2490   if (cfun && cfun->used_types_hash)
2491     {
2492       VEC(tree, heap) *types;
2493       int i;
2494       tree type;
2495
2496       types = VEC_alloc (tree, heap, htab_elements (cfun->used_types_hash));
2497       htab_traverse (cfun->used_types_hash, output_used_types_helper, &types);
2498
2499       /* Sort by UID to prevent dependence on hash table ordering.  */
2500       qsort (VEC_address (tree, types), VEC_length (tree, types),
2501              sizeof (tree), output_types_sort);
2502
2503       for (i = 0; VEC_iterate (tree, types, i, type); i++)
2504         debug_queue_symbol (type);
2505
2506       VEC_free (tree, heap, types);
2507     }
2508 }
2509
2510 /* Output a .stabs for the symbol defined by DECL,
2511    which must be a ..._DECL node in the normal namespace.
2512    It may be a CONST_DECL, a FUNCTION_DECL, a PARM_DECL or a VAR_DECL.
2513    LOCAL is nonzero if the scope is less than the entire file.
2514    Return 1 if a stabs might have been emitted.  */
2515
2516 int
2517 dbxout_symbol (tree decl, int local ATTRIBUTE_UNUSED)
2518 {
2519   tree type = TREE_TYPE (decl);
2520   tree context = NULL_TREE;
2521   int result = 0;
2522   rtx decl_rtl;
2523
2524   /* "Intercept" dbxout_symbol() calls like we do all debug_hooks.  */
2525   ++debug_nesting;
2526
2527   /* Ignore nameless syms, but don't ignore type tags.  */
2528
2529   if ((DECL_NAME (decl) == 0 && TREE_CODE (decl) != TYPE_DECL)
2530       || DECL_IGNORED_P (decl))
2531     DBXOUT_DECR_NESTING_AND_RETURN (0);
2532
2533   /* If we are to generate only the symbols actually used then such
2534      symbol nodes are flagged with TREE_USED.  Ignore any that
2535      aren't flagged as TREE_USED.  */
2536
2537   if (flag_debug_only_used_symbols
2538       && (!TREE_USED (decl)
2539           && (TREE_CODE (decl) != VAR_DECL || !DECL_INITIAL (decl))))
2540     DBXOUT_DECR_NESTING_AND_RETURN (0);
2541
2542   /* If dbxout_init has not yet run, queue this symbol for later.  */
2543   if (!typevec)
2544     {
2545       preinit_symbols = tree_cons (0, decl, preinit_symbols);
2546       DBXOUT_DECR_NESTING_AND_RETURN (0);
2547     }
2548
2549   if (flag_debug_only_used_symbols)
2550     {
2551       tree t;
2552
2553       /* We now have a used symbol.  We need to generate the info for
2554          the symbol's type in addition to the symbol itself.  These
2555          type symbols are queued to be generated after were done with
2556          the symbol itself (otherwise they would fight over the
2557          stabstr obstack).
2558
2559          Note, because the TREE_TYPE(type) might be something like a
2560          pointer to a named type we need to look for the first name
2561          we see following the TREE_TYPE chain.  */
2562
2563       t = type;
2564       while (POINTER_TYPE_P (t))
2565         t = TREE_TYPE (t);
2566
2567       /* RECORD_TYPE, UNION_TYPE, QUAL_UNION_TYPE, and ENUMERAL_TYPE
2568          need special treatment.  The TYPE_STUB_DECL field in these
2569          types generally represents the tag name type we want to
2570          output.  In addition there  could be a typedef type with
2571          a different name.  In that case we also want to output
2572          that.  */
2573
2574       if (TREE_CODE (t) == RECORD_TYPE
2575            || TREE_CODE (t) == UNION_TYPE
2576            || TREE_CODE (t) == QUAL_UNION_TYPE
2577            || TREE_CODE (t) == ENUMERAL_TYPE)
2578         {
2579             if (TYPE_STUB_DECL (t)
2580                 && TYPE_STUB_DECL (t) != decl
2581                 && DECL_P (TYPE_STUB_DECL (t))
2582                 && ! DECL_IGNORED_P (TYPE_STUB_DECL (t)))
2583             {
2584               debug_queue_symbol (TYPE_STUB_DECL (t));
2585               if (TYPE_NAME (t)
2586                   && TYPE_NAME (t) != TYPE_STUB_DECL (t)
2587                   && TYPE_NAME (t) != decl
2588                   && DECL_P (TYPE_NAME (t)))
2589                 debug_queue_symbol (TYPE_NAME (t));
2590             }
2591         }
2592       else if (TYPE_NAME (t)
2593                && TYPE_NAME (t) != decl
2594                && DECL_P (TYPE_NAME (t)))
2595         debug_queue_symbol (TYPE_NAME (t));
2596     }
2597
2598   emit_pending_bincls_if_required ();
2599
2600   switch (TREE_CODE (decl))
2601     {
2602     case CONST_DECL:
2603       /* Enum values are defined by defining the enum type.  */
2604       break;
2605
2606     case FUNCTION_DECL:
2607       decl_rtl = DECL_RTL_IF_SET (decl);
2608       if (!decl_rtl)
2609         DBXOUT_DECR_NESTING_AND_RETURN (0);
2610       if (DECL_EXTERNAL (decl))
2611         break;
2612       /* Don't mention a nested function under its parent.  */
2613       context = decl_function_context (decl);
2614       if (context == current_function_decl)
2615         break;
2616       /* Don't mention an inline instance of a nested function.  */
2617       if (context && DECL_FROM_INLINE (decl))
2618         break;
2619       if (!MEM_P (decl_rtl)
2620           || GET_CODE (XEXP (decl_rtl, 0)) != SYMBOL_REF)
2621         break;
2622
2623       if (flag_debug_only_used_symbols)
2624         output_used_types ();
2625
2626       dbxout_begin_complex_stabs ();
2627       stabstr_I (DECL_ASSEMBLER_NAME (decl));
2628       stabstr_S (TREE_PUBLIC (decl) ? ":F" : ":f");
2629       result = 1;
2630
2631       if (TREE_TYPE (type))
2632         dbxout_type (TREE_TYPE (type), 0);
2633       else
2634         dbxout_type (void_type_node, 0);
2635
2636       /* For a nested function, when that function is compiled,
2637          mention the containing function name
2638          as well as (since dbx wants it) our own assembler-name.  */
2639       if (context != 0)
2640         {
2641           stabstr_C (',');
2642           stabstr_I (DECL_ASSEMBLER_NAME (decl));
2643           stabstr_C (',');
2644           stabstr_I (DECL_NAME (context));
2645         }
2646
2647       dbxout_finish_complex_stabs (decl, N_FUN, XEXP (decl_rtl, 0), 0, 0);
2648       break;
2649
2650     case TYPE_DECL:
2651       /* Don't output the same typedef twice.
2652          And don't output what language-specific stuff doesn't want output.  */
2653       if (TREE_ASM_WRITTEN (decl) || TYPE_DECL_SUPPRESS_DEBUG (decl))
2654         DBXOUT_DECR_NESTING_AND_RETURN (0);
2655
2656       /* Don't output typedefs for types with magic type numbers (XCOFF).  */
2657 #ifdef DBX_ASSIGN_FUNDAMENTAL_TYPE_NUMBER
2658       {
2659         int fundamental_type_number =
2660           DBX_ASSIGN_FUNDAMENTAL_TYPE_NUMBER (decl);
2661
2662         if (fundamental_type_number != 0)
2663           {
2664             TREE_ASM_WRITTEN (decl) = 1;
2665             TYPE_SYMTAB_ADDRESS (TREE_TYPE (decl)) = fundamental_type_number;
2666             DBXOUT_DECR_NESTING_AND_RETURN (0);
2667           }
2668       }
2669 #endif
2670       FORCE_TEXT;
2671       result = 1;
2672       {
2673         int tag_needed = 1;
2674         int did_output = 0;
2675
2676         if (DECL_NAME (decl))
2677           {
2678             /* Nonzero means we must output a tag as well as a typedef.  */
2679             tag_needed = 0;
2680
2681             /* Handle the case of a C++ structure or union
2682                where the TYPE_NAME is a TYPE_DECL
2683                which gives both a typedef name and a tag.  */
2684             /* dbx requires the tag first and the typedef second.  */
2685             if ((TREE_CODE (type) == RECORD_TYPE
2686                  || TREE_CODE (type) == UNION_TYPE
2687                  || TREE_CODE (type) == QUAL_UNION_TYPE)
2688                 && TYPE_NAME (type) == decl
2689                 && !use_gnu_debug_info_extensions
2690                 && !TREE_ASM_WRITTEN (TYPE_NAME (type))
2691                 /* Distinguish the implicit typedefs of C++
2692                    from explicit ones that might be found in C.  */
2693                 && DECL_ARTIFICIAL (decl)
2694                 /* Do not generate a tag for incomplete records.  */
2695                 && COMPLETE_TYPE_P (type)
2696                 /* Do not generate a tag for records of variable size,
2697                    since this type can not be properly described in the
2698                    DBX format, and it confuses some tools such as objdump.  */
2699                 && host_integerp (TYPE_SIZE (type), 1))
2700               {
2701                 tree name = TYPE_NAME (type);
2702                 if (TREE_CODE (name) == TYPE_DECL)
2703                   name = DECL_NAME (name);
2704
2705                 dbxout_begin_complex_stabs ();
2706                 stabstr_I (name);
2707                 stabstr_S (":T");
2708                 dbxout_type (type, 1);
2709                 dbxout_finish_complex_stabs (0, DBX_TYPE_DECL_STABS_CODE,
2710                                              0, 0, 0);
2711               }
2712
2713             dbxout_begin_complex_stabs ();
2714
2715             /* Output leading class/struct qualifiers.  */
2716             if (use_gnu_debug_info_extensions)
2717               dbxout_class_name_qualifiers (decl);
2718
2719             /* Output typedef name.  */
2720             stabstr_I (DECL_NAME (decl));
2721             stabstr_C (':');
2722
2723             /* Short cut way to output a tag also.  */
2724             if ((TREE_CODE (type) == RECORD_TYPE
2725                  || TREE_CODE (type) == UNION_TYPE
2726                  || TREE_CODE (type) == QUAL_UNION_TYPE)
2727                 && TYPE_NAME (type) == decl
2728                 /* Distinguish the implicit typedefs of C++
2729                    from explicit ones that might be found in C.  */
2730                 && DECL_ARTIFICIAL (decl))
2731               {
2732                 if (use_gnu_debug_info_extensions)
2733                   {
2734                     stabstr_C ('T');
2735                     TREE_ASM_WRITTEN (TYPE_NAME (type)) = 1;
2736                   }
2737               }
2738
2739             stabstr_C ('t');
2740             dbxout_type (type, 1);
2741             dbxout_finish_complex_stabs (decl, DBX_TYPE_DECL_STABS_CODE,
2742                                          0, 0, 0);
2743             did_output = 1;
2744           }
2745
2746         /* Don't output a tag if this is an incomplete type.  This prevents
2747            the sun4 Sun OS 4.x dbx from crashing.  */
2748
2749         if (tag_needed && TYPE_NAME (type) != 0
2750             && (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
2751                 || (DECL_NAME (TYPE_NAME (type)) != 0))
2752             && COMPLETE_TYPE_P (type)
2753             && !TREE_ASM_WRITTEN (TYPE_NAME (type)))
2754           {
2755             /* For a TYPE_DECL with no name, but the type has a name,
2756                output a tag.
2757                This is what represents `struct foo' with no typedef.  */
2758             /* In C++, the name of a type is the corresponding typedef.
2759                In C, it is an IDENTIFIER_NODE.  */
2760             tree name = TYPE_NAME (type);
2761             if (TREE_CODE (name) == TYPE_DECL)
2762               name = DECL_NAME (name);
2763
2764             dbxout_begin_complex_stabs ();
2765             stabstr_I (name);
2766             stabstr_S (":T");
2767             dbxout_type (type, 1);
2768             dbxout_finish_complex_stabs (0, DBX_TYPE_DECL_STABS_CODE, 0, 0, 0);
2769             did_output = 1;
2770           }
2771
2772         /* If an enum type has no name, it cannot be referred to, but
2773            we must output it anyway, to record the enumeration
2774            constants.  */
2775
2776         if (!did_output && TREE_CODE (type) == ENUMERAL_TYPE)
2777           {
2778             dbxout_begin_complex_stabs ();
2779             /* Some debuggers fail when given NULL names, so give this a
2780                harmless name of " " (Why not "(anon)"?).  */
2781             stabstr_S (" :T");
2782             dbxout_type (type, 1);
2783             dbxout_finish_complex_stabs (0, DBX_TYPE_DECL_STABS_CODE, 0, 0, 0);
2784           }
2785
2786         /* Prevent duplicate output of a typedef.  */
2787         TREE_ASM_WRITTEN (decl) = 1;
2788         break;
2789       }
2790
2791     case PARM_DECL:
2792       if (DECL_HAS_VALUE_EXPR_P (decl))
2793         decl = DECL_VALUE_EXPR (decl);
2794
2795       /* PARM_DECLs go in their own separate chain and are output by
2796          dbxout_reg_parms and dbxout_parms, except for those that are
2797          disguised VAR_DECLs like Out parameters in Ada.  */
2798       gcc_assert (TREE_CODE (decl) == VAR_DECL);
2799
2800       /* ... fall through ...  */
2801
2802     case RESULT_DECL:
2803     case VAR_DECL:
2804       /* Don't mention a variable that is external.
2805          Let the file that defines it describe it.  */
2806       if (DECL_EXTERNAL (decl))
2807         break;
2808
2809       /* If the variable is really a constant
2810          and not written in memory, inform the debugger.
2811
2812          ??? Why do we skip emitting the type and location in this case?  */
2813       if (TREE_STATIC (decl) && TREE_READONLY (decl)
2814           && DECL_INITIAL (decl) != 0
2815           && host_integerp (DECL_INITIAL (decl), 0)
2816           && ! TREE_ASM_WRITTEN (decl)
2817           && (DECL_CONTEXT (decl) == NULL_TREE
2818               || TREE_CODE (DECL_CONTEXT (decl)) == BLOCK
2819               || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
2820           && TREE_PUBLIC (decl) == 0)
2821         {
2822           /* The sun4 assembler does not grok this.  */
2823
2824           if (TREE_CODE (TREE_TYPE (decl)) == INTEGER_TYPE
2825               || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2826             {
2827               HOST_WIDE_INT ival = TREE_INT_CST_LOW (DECL_INITIAL (decl));
2828
2829               dbxout_begin_complex_stabs ();
2830               dbxout_symbol_name (decl, NULL, 'c');
2831               stabstr_S ("=i");
2832               stabstr_D (ival);
2833               dbxout_finish_complex_stabs (0, N_LSYM, 0, 0, 0);
2834               DBXOUT_DECR_NESTING;
2835               return 1;
2836             }
2837           else
2838             break;
2839         }
2840       /* else it is something we handle like a normal variable.  */
2841
2842       decl_rtl = dbxout_expand_expr (decl);
2843       if (!decl_rtl)
2844         DBXOUT_DECR_NESTING_AND_RETURN (0);
2845
2846       decl_rtl = eliminate_regs (decl_rtl, VOIDmode, NULL_RTX);
2847 #ifdef LEAF_REG_REMAP
2848       if (current_function_uses_only_leaf_regs)
2849         leaf_renumber_regs_insn (decl_rtl);
2850 #endif
2851
2852       result = dbxout_symbol_location (decl, type, 0, decl_rtl);
2853       break;
2854
2855     default:
2856       break;
2857     }
2858   DBXOUT_DECR_NESTING;
2859   return result;
2860 }
2861 \f
2862 /* Output the stab for DECL, a VAR_DECL, RESULT_DECL or PARM_DECL.
2863    Add SUFFIX to its name, if SUFFIX is not 0.
2864    Describe the variable as residing in HOME
2865    (usually HOME is DECL_RTL (DECL), but not always).
2866    Returns 1 if the stab was really emitted.  */
2867
2868 static int
2869 dbxout_symbol_location (tree decl, tree type, const char *suffix, rtx home)
2870 {
2871   int letter = 0;
2872   stab_code_type code;
2873   rtx addr = 0;
2874   int number = 0;
2875   int regno = -1;
2876
2877   /* Don't mention a variable at all
2878      if it was completely optimized into nothingness.
2879
2880      If the decl was from an inline function, then its rtl
2881      is not identically the rtl that was used in this
2882      particular compilation.  */
2883   if (GET_CODE (home) == SUBREG)
2884     {
2885       rtx value = home;
2886
2887       while (GET_CODE (value) == SUBREG)
2888         value = SUBREG_REG (value);
2889       if (REG_P (value))
2890         {
2891           if (REGNO (value) >= FIRST_PSEUDO_REGISTER)
2892             return 0;
2893         }
2894       home = alter_subreg (&home);
2895     }
2896   if (REG_P (home))
2897     {
2898       regno = REGNO (home);
2899       if (regno >= FIRST_PSEUDO_REGISTER)
2900         return 0;
2901     }
2902
2903   /* The kind-of-variable letter depends on where
2904      the variable is and on the scope of its name:
2905      G and N_GSYM for static storage and global scope,
2906      S for static storage and file scope,
2907      V for static storage and local scope,
2908      for those two, use N_LCSYM if data is in bss segment,
2909      N_STSYM if in data segment, N_FUN otherwise.
2910      (We used N_FUN originally, then changed to N_STSYM
2911      to please GDB.  However, it seems that confused ld.
2912      Now GDB has been fixed to like N_FUN, says Kingdon.)
2913      no letter at all, and N_LSYM, for auto variable,
2914      r and N_RSYM for register variable.  */
2915
2916   if (MEM_P (home) && GET_CODE (XEXP (home, 0)) == SYMBOL_REF)
2917     {
2918       if (TREE_PUBLIC (decl))
2919         {
2920           int offs;
2921           letter = 'G';
2922           code = N_GSYM;
2923           if (NULL != dbxout_common_check (decl, &offs))
2924             {
2925               letter = 'V';
2926               addr = 0;
2927               number = offs;
2928             }
2929         }
2930       else
2931         {
2932           addr = XEXP (home, 0);
2933
2934           letter = decl_function_context (decl) ? 'V' : 'S';
2935
2936           /* Some ports can transform a symbol ref into a label ref,
2937              because the symbol ref is too far away and has to be
2938              dumped into a constant pool.  Alternatively, the symbol
2939              in the constant pool might be referenced by a different
2940              symbol.  */
2941           if (GET_CODE (addr) == SYMBOL_REF
2942               && CONSTANT_POOL_ADDRESS_P (addr))
2943             {
2944               bool marked;
2945               rtx tmp = get_pool_constant_mark (addr, &marked);
2946
2947               if (GET_CODE (tmp) == SYMBOL_REF)
2948                 {
2949                   addr = tmp;
2950                   if (CONSTANT_POOL_ADDRESS_P (addr))
2951                     get_pool_constant_mark (addr, &marked);
2952                   else
2953                     marked = true;
2954                 }
2955               else if (GET_CODE (tmp) == LABEL_REF)
2956                 {
2957                   addr = tmp;
2958                   marked = true;
2959                 }
2960
2961               /* If all references to the constant pool were optimized
2962                  out, we just ignore the symbol.  */
2963               if (!marked)
2964                 return 0;
2965             }
2966
2967           /* This should be the same condition as in assemble_variable, but
2968              we don't have access to dont_output_data here.  So, instead,
2969              we rely on the fact that error_mark_node initializers always
2970              end up in bss for C++ and never end up in bss for C.  */
2971           if (DECL_INITIAL (decl) == 0
2972               || (!strcmp (lang_hooks.name, "GNU C++")
2973                   && DECL_INITIAL (decl) == error_mark_node))
2974             {
2975               int offs;
2976               code = N_LCSYM;
2977               if (NULL != dbxout_common_check (decl, &offs))
2978                 {
2979                   addr = 0;
2980                   number = offs;
2981                   letter = 'V';
2982                   code = N_GSYM;
2983                 }
2984             }
2985           else if (DECL_IN_TEXT_SECTION (decl))
2986             /* This is not quite right, but it's the closest
2987                of all the codes that Unix defines.  */
2988             code = DBX_STATIC_CONST_VAR_CODE;
2989           else
2990             {
2991               /* Ultrix `as' seems to need this.  */
2992 #ifdef DBX_STATIC_STAB_DATA_SECTION
2993               switch_to_section (data_section);
2994 #endif
2995               code = N_STSYM;
2996             }
2997         }
2998     }
2999   else if (regno >= 0)
3000     {
3001       letter = 'r';
3002       code = N_RSYM;
3003       number = DBX_REGISTER_NUMBER (regno);
3004     }
3005   else if (MEM_P (home)
3006            && (MEM_P (XEXP (home, 0))
3007                || (REG_P (XEXP (home, 0))
3008                    && REGNO (XEXP (home, 0)) != HARD_FRAME_POINTER_REGNUM
3009                    && REGNO (XEXP (home, 0)) != STACK_POINTER_REGNUM
3010 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3011                    && REGNO (XEXP (home, 0)) != ARG_POINTER_REGNUM
3012 #endif
3013                    )))
3014     /* If the value is indirect by memory or by a register
3015        that isn't the frame pointer
3016        then it means the object is variable-sized and address through
3017        that register or stack slot.  DBX has no way to represent this
3018        so all we can do is output the variable as a pointer.
3019        If it's not a parameter, ignore it.  */
3020     {
3021       if (REG_P (XEXP (home, 0)))
3022         {
3023           letter = 'r';
3024           code = N_RSYM;
3025           if (REGNO (XEXP (home, 0)) >= FIRST_PSEUDO_REGISTER)
3026             return 0;
3027           number = DBX_REGISTER_NUMBER (REGNO (XEXP (home, 0)));
3028         }
3029       else
3030         {
3031           code = N_LSYM;
3032           /* RTL looks like (MEM (MEM (PLUS (REG...) (CONST_INT...)))).
3033              We want the value of that CONST_INT.  */
3034           number = DEBUGGER_AUTO_OFFSET (XEXP (XEXP (home, 0), 0));
3035         }
3036
3037       /* Effectively do build_pointer_type, but don't cache this type,
3038          since it might be temporary whereas the type it points to
3039          might have been saved for inlining.  */
3040       /* Don't use REFERENCE_TYPE because dbx can't handle that.  */
3041       type = make_node (POINTER_TYPE);
3042       TREE_TYPE (type) = TREE_TYPE (decl);
3043     }
3044   else if (MEM_P (home)
3045            && REG_P (XEXP (home, 0)))
3046     {
3047       code = N_LSYM;
3048       number = DEBUGGER_AUTO_OFFSET (XEXP (home, 0));
3049     }
3050   else if (MEM_P (home)
3051            && GET_CODE (XEXP (home, 0)) == PLUS
3052            && CONST_INT_P (XEXP (XEXP (home, 0), 1)))
3053     {
3054       code = N_LSYM;
3055       /* RTL looks like (MEM (PLUS (REG...) (CONST_INT...)))
3056          We want the value of that CONST_INT.  */
3057       number = DEBUGGER_AUTO_OFFSET (XEXP (home, 0));
3058     }
3059   else if (MEM_P (home)
3060            && GET_CODE (XEXP (home, 0)) == CONST)
3061     {
3062       /* Handle an obscure case which can arise when optimizing and
3063          when there are few available registers.  (This is *always*
3064          the case for i386/i486 targets).  The RTL looks like
3065          (MEM (CONST ...)) even though this variable is a local `auto'
3066          or a local `register' variable.  In effect, what has happened
3067          is that the reload pass has seen that all assignments and
3068          references for one such a local variable can be replaced by
3069          equivalent assignments and references to some static storage
3070          variable, thereby avoiding the need for a register.  In such
3071          cases we're forced to lie to debuggers and tell them that
3072          this variable was itself `static'.  */
3073       int offs;
3074       code = N_LCSYM;
3075       letter = 'V';
3076       if (NULL == dbxout_common_check (decl, &offs))
3077         addr = XEXP (XEXP (home, 0), 0);
3078       else
3079         {
3080           addr = 0;
3081           number = offs;
3082           code = N_GSYM;
3083         }
3084     }
3085   else if (GET_CODE (home) == CONCAT)
3086     {
3087       tree subtype;
3088
3089       /* If TYPE is not a COMPLEX_TYPE (it might be a RECORD_TYPE,
3090          for example), then there is no easy way to figure out
3091          what SUBTYPE should be.  So, we give up.  */
3092       if (TREE_CODE (type) != COMPLEX_TYPE)
3093         return 0;
3094
3095       subtype = TREE_TYPE (type);
3096
3097       /* If the variable's storage is in two parts,
3098          output each as a separate stab with a modified name.  */
3099       if (WORDS_BIG_ENDIAN)
3100         dbxout_symbol_location (decl, subtype, "$imag", XEXP (home, 0));
3101       else
3102         dbxout_symbol_location (decl, subtype, "$real", XEXP (home, 0));
3103
3104       if (WORDS_BIG_ENDIAN)
3105         dbxout_symbol_location (decl, subtype, "$real", XEXP (home, 1));
3106       else
3107         dbxout_symbol_location (decl, subtype, "$imag", XEXP (home, 1));
3108       return 1;
3109     }
3110   else
3111     /* Address might be a MEM, when DECL is a variable-sized object.
3112        Or it might be const0_rtx, meaning previous passes
3113        want us to ignore this variable.  */
3114     return 0;
3115
3116   /* Ok, start a symtab entry and output the variable name.  */
3117   emit_pending_bincls_if_required ();
3118   FORCE_TEXT;
3119
3120 #ifdef DBX_STATIC_BLOCK_START
3121   DBX_STATIC_BLOCK_START (asm_out_file, code);
3122 #endif
3123
3124   dbxout_begin_complex_stabs_noforcetext ();
3125   dbxout_symbol_name (decl, suffix, letter);
3126   dbxout_type (type, 0);
3127   dbxout_finish_complex_stabs (decl, code, addr, 0, number);
3128
3129 #ifdef DBX_STATIC_BLOCK_END
3130   DBX_STATIC_BLOCK_END (asm_out_file, code);
3131 #endif
3132   return 1;
3133 }
3134 \f
3135 /* Output the symbol name of DECL for a stabs, with suffix SUFFIX.
3136    Then output LETTER to indicate the kind of location the symbol has.  */
3137
3138 static void
3139 dbxout_symbol_name (tree decl, const char *suffix, int letter)
3140 {
3141   tree name;
3142
3143   if (DECL_CONTEXT (decl)
3144       && (TYPE_P (DECL_CONTEXT (decl))
3145           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL))
3146     /* One slight hitch: if this is a VAR_DECL which is a class member
3147        or a namespace member, we must put out the mangled name instead of the
3148        DECL_NAME.  Note also that static member (variable) names DO NOT begin
3149        with underscores in .stabs directives.  */
3150     name = DECL_ASSEMBLER_NAME (decl);
3151   else
3152     /* ...but if we're function-local, we don't want to include the junk
3153        added by ASM_FORMAT_PRIVATE_NAME.  */
3154     name = DECL_NAME (decl);
3155
3156   if (name)
3157     stabstr_I (name);
3158   else
3159     stabstr_S ("(anon)");
3160
3161   if (suffix)
3162     stabstr_S (suffix);
3163   stabstr_C (':');
3164   if (letter)
3165     stabstr_C (letter);
3166 }
3167
3168
3169 /* Output the common block name for DECL in a stabs.
3170
3171    Symbols in global common (.comm) get wrapped with an N_BCOMM/N_ECOMM pair
3172    around each group of symbols in the same .comm area.  The N_GSYM stabs
3173    that are emitted only contain the offset in the common area.  This routine
3174    emits the N_BCOMM and N_ECOMM stabs.  */
3175
3176 static void
3177 dbxout_common_name (tree decl, const char *name, stab_code_type op)
3178 {
3179   dbxout_begin_complex_stabs ();
3180   stabstr_S (name);
3181   dbxout_finish_complex_stabs (decl, op, NULL_RTX, NULL, 0);
3182 }
3183
3184 /* Check decl to determine whether it is a VAR_DECL destined for storage in a
3185    common area.  If it is, the return value will be a non-null string giving
3186    the name of the common storage block it will go into.  If non-null, the
3187    value is the offset into the common block for that symbol's storage.  */
3188
3189 static const char *
3190 dbxout_common_check (tree decl, int *value)
3191 {
3192   rtx home;
3193   rtx sym_addr;
3194   const char *name = NULL;
3195
3196   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
3197      it does not have a value (the offset into the common area), or if it
3198      is thread local (as opposed to global) then it isn't common, and shouldn't
3199      be handled as such.
3200
3201      ??? DECL_THREAD_LOCAL_P check prevents problems with improper .stabs
3202      for thread-local symbols.  Can be handled via same mechanism as used
3203      in dwarf2out.c.  */
3204   if (TREE_CODE (decl) != VAR_DECL
3205       || !TREE_STATIC(decl)
3206       || !DECL_HAS_VALUE_EXPR_P(decl)
3207       || DECL_THREAD_LOCAL_P (decl)
3208       || !is_fortran ())
3209     return NULL;
3210
3211   home = DECL_RTL (decl);
3212   if (home == NULL_RTX || GET_CODE (home) != MEM)
3213     return NULL;
3214
3215   sym_addr = dbxout_expand_expr (DECL_VALUE_EXPR (decl));
3216   if (sym_addr == NULL_RTX || GET_CODE (sym_addr) != MEM)
3217     return NULL;
3218
3219   sym_addr = XEXP (sym_addr, 0);
3220   if (GET_CODE (sym_addr) == CONST)
3221     sym_addr = XEXP (sym_addr, 0);
3222   if ((GET_CODE (sym_addr) == SYMBOL_REF || GET_CODE (sym_addr) == PLUS)
3223       && DECL_INITIAL (decl) == 0)
3224     {
3225
3226       /* We have a sym that will go into a common area, meaning that it
3227          will get storage reserved with a .comm/.lcomm assembler pseudo-op.
3228
3229          Determine name of common area this symbol will be an offset into,
3230          and offset into that area.  Also retrieve the decl for the area
3231          that the symbol is offset into.  */
3232       tree cdecl = NULL;
3233
3234       switch (GET_CODE (sym_addr))
3235         {
3236         case PLUS:
3237           if (CONST_INT_P (XEXP (sym_addr, 0)))
3238             {
3239               name =
3240                 targetm.strip_name_encoding(XSTR (XEXP (sym_addr, 1), 0));
3241               *value = INTVAL (XEXP (sym_addr, 0));
3242               cdecl = SYMBOL_REF_DECL (XEXP (sym_addr, 1));
3243             }
3244           else
3245             {
3246               name =
3247                 targetm.strip_name_encoding(XSTR (XEXP (sym_addr, 0), 0));
3248               *value = INTVAL (XEXP (sym_addr, 1));
3249               cdecl = SYMBOL_REF_DECL (XEXP (sym_addr, 0));
3250             }
3251           break;
3252
3253         case SYMBOL_REF:
3254           name = targetm.strip_name_encoding(XSTR (sym_addr, 0));
3255           *value = 0;
3256           cdecl = SYMBOL_REF_DECL (sym_addr);
3257           break;
3258
3259         default:
3260           error ("common symbol debug info is not structured as "
3261                  "symbol+offset");
3262         }
3263
3264       /* Check area common symbol is offset into.  If this is not public, then
3265          it is not a symbol in a common block.  It must be a .lcomm symbol, not
3266          a .comm symbol.  */
3267       if (cdecl == NULL || !TREE_PUBLIC(cdecl))
3268         name = NULL;
3269     }
3270   else
3271     name = NULL;
3272
3273   return name;
3274 }
3275
3276 /* Output definitions of all the decls in a chain. Return nonzero if
3277    anything was output */
3278
3279 int
3280 dbxout_syms (tree syms)
3281 {
3282   int result = 0;
3283   const char *comm_prev = NULL;
3284   tree syms_prev = NULL;
3285
3286   while (syms)
3287     {
3288       int temp, copen, cclos;
3289       const char *comm_new;
3290
3291       /* Check for common symbol, and then progression into a new/different
3292          block of common symbols.  Emit closing/opening common bracket if
3293          necessary.  */
3294       comm_new = dbxout_common_check (syms, &temp);
3295       copen = comm_new != NULL
3296               && (comm_prev == NULL || strcmp (comm_new, comm_prev));
3297       cclos = comm_prev != NULL
3298               && (comm_new == NULL || strcmp (comm_new, comm_prev));
3299       if (cclos)
3300         dbxout_common_name (syms_prev, comm_prev, N_ECOMM);
3301       if (copen)
3302         {
3303           dbxout_common_name (syms, comm_new, N_BCOMM);
3304           syms_prev = syms;
3305         }
3306       comm_prev = comm_new;
3307
3308       result += dbxout_symbol (syms, 1);
3309       syms = TREE_CHAIN (syms);
3310     }
3311
3312   if (comm_prev != NULL)
3313     dbxout_common_name (syms_prev, comm_prev, N_ECOMM);
3314
3315   return result;
3316 }
3317 \f
3318 /* The following two functions output definitions of function parameters.
3319    Each parameter gets a definition locating it in the parameter list.
3320    Each parameter that is a register variable gets a second definition
3321    locating it in the register.
3322
3323    Printing or argument lists in gdb uses the definitions that
3324    locate in the parameter list.  But reference to the variable in
3325    expressions uses preferentially the definition as a register.  */
3326
3327 /* Output definitions, referring to storage in the parmlist,
3328    of all the parms in PARMS, which is a chain of PARM_DECL nodes.  */
3329
3330 void
3331 dbxout_parms (tree parms)
3332 {
3333   ++debug_nesting;
3334   emit_pending_bincls_if_required ();
3335
3336   for (; parms; parms = TREE_CHAIN (parms))
3337     if (DECL_NAME (parms)
3338         && TREE_TYPE (parms) != error_mark_node
3339         && DECL_RTL_SET_P (parms)
3340         && DECL_INCOMING_RTL (parms))
3341       {
3342         tree eff_type;
3343         char letter;
3344         stab_code_type code;
3345         int number;
3346
3347         /* Perform any necessary register eliminations on the parameter's rtl,
3348            so that the debugging output will be accurate.  */
3349         DECL_INCOMING_RTL (parms)
3350           = eliminate_regs (DECL_INCOMING_RTL (parms), VOIDmode, NULL_RTX);
3351         SET_DECL_RTL (parms,
3352                       eliminate_regs (DECL_RTL (parms), VOIDmode, NULL_RTX));
3353 #ifdef LEAF_REG_REMAP
3354         if (current_function_uses_only_leaf_regs)
3355           {
3356             leaf_renumber_regs_insn (DECL_INCOMING_RTL (parms));
3357             leaf_renumber_regs_insn (DECL_RTL (parms));
3358           }
3359 #endif
3360
3361         if (PARM_PASSED_IN_MEMORY (parms))
3362           {
3363             rtx inrtl = XEXP (DECL_INCOMING_RTL (parms), 0);
3364
3365             /* ??? Here we assume that the parm address is indexed
3366                off the frame pointer or arg pointer.
3367                If that is not true, we produce meaningless results,
3368                but do not crash.  */
3369             if (GET_CODE (inrtl) == PLUS
3370                 && CONST_INT_P (XEXP (inrtl, 1)))
3371               number = INTVAL (XEXP (inrtl, 1));
3372             else
3373               number = 0;
3374
3375             code = N_PSYM;
3376             number = DEBUGGER_ARG_OFFSET (number, inrtl);
3377             letter = 'p';
3378
3379             /* It is quite tempting to use TREE_TYPE (parms) instead
3380                of DECL_ARG_TYPE (parms) for the eff_type, so that gcc
3381                reports the actual type of the parameter, rather than
3382                the promoted type.  This certainly makes GDB's life
3383                easier, at least for some ports.  The change is a bad
3384                idea however, since GDB expects to be able access the
3385                type without performing any conversions.  So for
3386                example, if we were passing a float to an unprototyped
3387                function, gcc will store a double on the stack, but if
3388                we emit a stab saying the type is a float, then gdb
3389                will only read in a single value, and this will produce
3390                an erroneous value.  */
3391             eff_type = DECL_ARG_TYPE (parms);
3392           }
3393         else if (REG_P (DECL_RTL (parms)))
3394           {
3395             rtx best_rtl;
3396
3397             /* Parm passed in registers and lives in registers or nowhere.  */
3398             code = DBX_REGPARM_STABS_CODE;
3399             letter = DBX_REGPARM_STABS_LETTER;
3400
3401             /* For parms passed in registers, it is better to use the
3402                declared type of the variable, not the type it arrived in.  */
3403             eff_type = TREE_TYPE (parms);
3404
3405             /* If parm lives in a register, use that register; pretend
3406                the parm was passed there.  It would be more consistent
3407                to describe the register where the parm was passed, but
3408                in practice that register usually holds something else.
3409                If the parm lives nowhere, use the register where it
3410                was passed.  */
3411             if (REGNO (DECL_RTL (parms)) < FIRST_PSEUDO_REGISTER)
3412               best_rtl = DECL_RTL (parms);
3413             else if (GET_CODE (DECL_INCOMING_RTL (parms)) == PARALLEL)
3414               best_rtl = XEXP (XVECEXP (DECL_INCOMING_RTL (parms), 0, 0), 0);
3415             else
3416               best_rtl = DECL_INCOMING_RTL (parms);
3417
3418             number = DBX_REGISTER_NUMBER (REGNO (best_rtl));
3419           }
3420         else if (MEM_P (DECL_RTL (parms))
3421                  && REG_P (XEXP (DECL_RTL (parms), 0))
3422                  && REGNO (XEXP (DECL_RTL (parms), 0)) != HARD_FRAME_POINTER_REGNUM
3423                  && REGNO (XEXP (DECL_RTL (parms), 0)) != STACK_POINTER_REGNUM
3424 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3425                  && REGNO (XEXP (DECL_RTL (parms), 0)) != ARG_POINTER_REGNUM
3426 #endif
3427                  )
3428           {
3429             /* Parm was passed via invisible reference.
3430                That is, its address was passed in a register.
3431                Output it as if it lived in that register.
3432                The debugger will know from the type
3433                that it was actually passed by invisible reference.  */
3434
3435             code = DBX_REGPARM_STABS_CODE;
3436
3437             /* GDB likes this marked with a special letter.  */
3438             letter = (use_gnu_debug_info_extensions
3439                       ? 'a' : DBX_REGPARM_STABS_LETTER);
3440             eff_type = TREE_TYPE (parms);
3441
3442             /* DECL_RTL looks like (MEM (REG...).  Get the register number.
3443                If it is an unallocated pseudo-reg, then use the register where
3444                it was passed instead.
3445                ??? Why is DBX_REGISTER_NUMBER not used here?  */
3446
3447             if (REGNO (XEXP (DECL_RTL (parms), 0)) < FIRST_PSEUDO_REGISTER)
3448               number = REGNO (XEXP (DECL_RTL (parms), 0));
3449             else
3450               number = REGNO (DECL_INCOMING_RTL (parms));
3451           }
3452         else if (MEM_P (DECL_RTL (parms))
3453                  && MEM_P (XEXP (DECL_RTL (parms), 0)))
3454           {
3455             /* Parm was passed via invisible reference, with the reference
3456                living on the stack.  DECL_RTL looks like
3457                (MEM (MEM (PLUS (REG ...) (CONST_INT ...)))) or it
3458                could look like (MEM (MEM (REG))).  */
3459
3460             code = N_PSYM;
3461             letter = 'v';
3462             eff_type = TREE_TYPE (parms);
3463
3464             if (!REG_P (XEXP (XEXP (DECL_RTL (parms), 0), 0)))
3465               number = INTVAL (XEXP (XEXP (XEXP (DECL_RTL (parms), 0), 0), 1));
3466             else
3467               number = 0;
3468
3469             number = DEBUGGER_ARG_OFFSET (number,
3470                                           XEXP (XEXP (DECL_RTL (parms), 0), 0));
3471           }
3472         else if (MEM_P (DECL_RTL (parms))
3473                  && XEXP (DECL_RTL (parms), 0) != const0_rtx
3474                  /* ??? A constant address for a parm can happen
3475                     when the reg it lives in is equiv to a constant in memory.
3476                     Should make this not happen, after 2.4.  */
3477                  && ! CONSTANT_P (XEXP (DECL_RTL (parms), 0)))
3478           {
3479             /* Parm was passed in registers but lives on the stack.  */
3480
3481             code = N_PSYM;
3482             letter = 'p';
3483             eff_type = TREE_TYPE (parms);
3484
3485             /* DECL_RTL looks like (MEM (PLUS (REG...) (CONST_INT...))),
3486                in which case we want the value of that CONST_INT,
3487                or (MEM (REG ...)),
3488                in which case we use a value of zero.  */
3489             if (!REG_P (XEXP (DECL_RTL (parms), 0)))
3490               number = INTVAL (XEXP (XEXP (DECL_RTL (parms), 0), 1));
3491             else
3492               number = 0;
3493
3494             /* Make a big endian correction if the mode of the type of the
3495                parameter is not the same as the mode of the rtl.  */
3496             if (BYTES_BIG_ENDIAN
3497                 && TYPE_MODE (TREE_TYPE (parms)) != GET_MODE (DECL_RTL (parms))
3498                 && GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parms))) < UNITS_PER_WORD)
3499               number += (GET_MODE_SIZE (GET_MODE (DECL_RTL (parms)))
3500                          - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parms))));
3501           }
3502         else
3503           /* ??? We don't know how to represent this argument.  */
3504           continue;
3505
3506         dbxout_begin_complex_stabs ();
3507
3508         if (DECL_NAME (parms))
3509           {
3510             stabstr_I (DECL_NAME (parms));
3511             stabstr_C (':');
3512           }
3513         else
3514           stabstr_S ("(anon):");
3515         stabstr_C (letter);
3516         dbxout_type (eff_type, 0);
3517         dbxout_finish_complex_stabs (parms, code, 0, 0, number);
3518       }
3519   DBXOUT_DECR_NESTING;
3520 }
3521
3522 /* Output definitions for the places where parms live during the function,
3523    when different from where they were passed, when the parms were passed
3524    in memory.
3525
3526    It is not useful to do this for parms passed in registers
3527    that live during the function in different registers, because it is
3528    impossible to look in the passed register for the passed value,
3529    so we use the within-the-function register to begin with.
3530
3531    PARMS is a chain of PARM_DECL nodes.  */
3532
3533 void
3534 dbxout_reg_parms (tree parms)
3535 {
3536   ++debug_nesting;
3537
3538   for (; parms; parms = TREE_CHAIN (parms))
3539     if (DECL_NAME (parms) && PARM_PASSED_IN_MEMORY (parms))
3540       {
3541         /* Report parms that live in registers during the function
3542            but were passed in memory.  */
3543         if (REG_P (DECL_RTL (parms))
3544             && REGNO (DECL_RTL (parms)) < FIRST_PSEUDO_REGISTER)
3545           dbxout_symbol_location (parms, TREE_TYPE (parms),
3546                                   0, DECL_RTL (parms));
3547         else if (GET_CODE (DECL_RTL (parms)) == CONCAT)
3548           dbxout_symbol_location (parms, TREE_TYPE (parms),
3549                                   0, DECL_RTL (parms));
3550         /* Report parms that live in memory but not where they were passed.  */
3551         else if (MEM_P (DECL_RTL (parms))
3552                  && ! rtx_equal_p (DECL_RTL (parms), DECL_INCOMING_RTL (parms)))
3553           dbxout_symbol_location (parms, TREE_TYPE (parms),
3554                                   0, DECL_RTL (parms));
3555       }
3556   DBXOUT_DECR_NESTING;
3557 }
3558 \f
3559 /* Given a chain of ..._TYPE nodes (as come in a parameter list),
3560    output definitions of those names, in raw form */
3561
3562 static void
3563 dbxout_args (tree args)
3564 {
3565   while (args)
3566     {
3567       stabstr_C (',');
3568       dbxout_type (TREE_VALUE (args), 0);
3569       args = TREE_CHAIN (args);
3570     }
3571 }
3572 \f
3573 #if defined (DBX_DEBUGGING_INFO)
3574
3575 /* Subroutine of dbxout_block.  Emit an N_LBRAC stab referencing LABEL.
3576    BEGIN_LABEL is the name of the beginning of the function, which may
3577    be required.  */
3578 static void
3579 dbx_output_lbrac (const char *label,
3580                   const char *begin_label ATTRIBUTE_UNUSED)
3581 {
3582   dbxout_begin_stabn (N_LBRAC);
3583   if (DBX_BLOCKS_FUNCTION_RELATIVE)
3584     dbxout_stab_value_label_diff (label, begin_label);
3585   else
3586     dbxout_stab_value_label (label);
3587 }
3588
3589 /* Subroutine of dbxout_block.  Emit an N_RBRAC stab referencing LABEL.
3590    BEGIN_LABEL is the name of the beginning of the function, which may
3591    be required.  */
3592 static void
3593 dbx_output_rbrac (const char *label,
3594                   const char *begin_label ATTRIBUTE_UNUSED)
3595 {
3596   dbxout_begin_stabn (N_RBRAC);
3597   if (DBX_BLOCKS_FUNCTION_RELATIVE)
3598     dbxout_stab_value_label_diff (label, begin_label);
3599   else
3600     dbxout_stab_value_label (label);
3601 }
3602
3603 /* Output everything about a symbol block (a BLOCK node
3604    that represents a scope level),
3605    including recursive output of contained blocks.
3606
3607    BLOCK is the BLOCK node.
3608    DEPTH is its depth within containing symbol blocks.
3609    ARGS is usually zero; but for the outermost block of the
3610    body of a function, it is a chain of PARM_DECLs for the function parameters.
3611    We output definitions of all the register parms
3612    as if they were local variables of that block.
3613
3614    If -g1 was used, we count blocks just the same, but output nothing
3615    except for the outermost block.
3616
3617    Actually, BLOCK may be several blocks chained together.
3618    We handle them all in sequence.  */
3619
3620 static void
3621 dbxout_block (tree block, int depth, tree args)
3622 {
3623   char begin_label[20];
3624   /* Reference current function start using LFBB.  */
3625   ASM_GENERATE_INTERNAL_LABEL (begin_label, "LFBB", scope_labelno);
3626
3627   while (block)
3628     {
3629       /* Ignore blocks never expanded or otherwise marked as real.  */
3630       if (TREE_USED (block) && TREE_ASM_WRITTEN (block))
3631         {
3632           int did_output;
3633           int blocknum = BLOCK_NUMBER (block);
3634
3635           /* In dbx format, the syms of a block come before the N_LBRAC.
3636              If nothing is output, we don't need the N_LBRAC, either.  */
3637           did_output = 0;
3638           if (debug_info_level != DINFO_LEVEL_TERSE || depth == 0)
3639             did_output = dbxout_syms (BLOCK_VARS (block));
3640           if (args)
3641             dbxout_reg_parms (args);
3642
3643           /* Now output an N_LBRAC symbol to represent the beginning of
3644              the block.  Use the block's tree-walk order to generate
3645              the assembler symbols LBBn and LBEn
3646              that final will define around the code in this block.  */
3647           if (did_output)
3648             {
3649               char buf[20];
3650               const char *scope_start;
3651
3652               if (depth == 0)
3653                 /* The outermost block doesn't get LBB labels; use
3654                    the LFBB local symbol emitted by dbxout_begin_prologue.  */
3655                 scope_start = begin_label;
3656               else
3657                 {
3658                   ASM_GENERATE_INTERNAL_LABEL (buf, "LBB", blocknum);
3659                   scope_start = buf;
3660                 }
3661
3662               dbx_output_lbrac (scope_start, begin_label);
3663             }
3664
3665           /* Output the subblocks.  */
3666           dbxout_block (BLOCK_SUBBLOCKS (block), depth + 1, NULL_TREE);
3667
3668           /* Refer to the marker for the end of the block.  */
3669           if (did_output)
3670             {
3671               char buf[100];
3672               if (depth == 0)
3673                 /* The outermost block doesn't get LBE labels;
3674                    use the "scope" label which will be emitted
3675                    by dbxout_function_end.  */
3676                 ASM_GENERATE_INTERNAL_LABEL (buf, "Lscope", scope_labelno);
3677               else
3678                 ASM_GENERATE_INTERNAL_LABEL (buf, "LBE", blocknum);
3679
3680               dbx_output_rbrac (buf, begin_label);
3681             }
3682         }
3683       block = BLOCK_CHAIN (block);
3684     }
3685 }
3686
3687 /* Output the information about a function and its arguments and result.
3688    Usually this follows the function's code,
3689    but on some systems, it comes before.  */
3690
3691 static void
3692 dbxout_begin_function (tree decl)
3693 {
3694   int saved_tree_used1;
3695
3696   saved_tree_used1 = TREE_USED (decl);
3697   TREE_USED (decl) = 1;
3698   if (DECL_NAME (DECL_RESULT (decl)) != 0)
3699     {
3700       int saved_tree_used2 = TREE_USED (DECL_RESULT (decl));
3701       TREE_USED (DECL_RESULT (decl)) = 1;
3702       dbxout_symbol (decl, 0);
3703       TREE_USED (DECL_RESULT (decl)) = saved_tree_used2;
3704     }
3705   else
3706     dbxout_symbol (decl, 0);
3707   TREE_USED (decl) = saved_tree_used1;
3708
3709   dbxout_parms (DECL_ARGUMENTS (decl));
3710   if (DECL_NAME (DECL_RESULT (decl)) != 0)
3711     dbxout_symbol (DECL_RESULT (decl), 1);
3712 }
3713 #endif /* DBX_DEBUGGING_INFO */
3714
3715 #endif /* DBX_DEBUGGING_INFO || XCOFF_DEBUGGING_INFO */
3716
3717 #include "gt-dbxout.h"