OSDN Git Service

c93142069b344ac1547b4cadd1f2c00b518e23b4
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from the GNU C compiler.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "flags.h"
43 #include "real.h"
44 #include "rtl.h"
45 #include "hard-reg-set.h"
46 #include "regs.h"
47 #include "insn-config.h"
48 #include "reload.h"
49 #include "function.h"
50 #include "output.h"
51 #include "expr.h"
52 #include "libfuncs.h"
53 #include "except.h"
54 #include "dwarf2.h"
55 #include "dwarf2out.h"
56 #include "dwarf2asm.h"
57 #include "toplev.h"
58 #include "varray.h"
59 #include "ggc.h"
60 #include "md5.h"
61 #include "tm_p.h"
62 #include "diagnostic.h"
63 #include "debug.h"
64 #include "target.h"
65 #include "langhooks.h"
66 #include "hashtable.h"
67 #include "hashtab.h"
68
69 #ifdef DWARF2_DEBUGGING_INFO
70 static void dwarf2out_source_line       PARAMS ((unsigned int, const char *));
71 #endif
72
73 /* DWARF2 Abbreviation Glossary:
74    CFA = Canonical Frame Address
75            a fixed address on the stack which identifies a call frame.
76            We define it to be the value of SP just before the call insn.
77            The CFA register and offset, which may change during the course
78            of the function, are used to calculate its value at runtime.
79    CFI = Call Frame Instruction
80            an instruction for the DWARF2 abstract machine
81    CIE = Common Information Entry
82            information describing information common to one or more FDEs
83    DIE = Debugging Information Entry
84    FDE = Frame Description Entry
85            information describing the stack call frame, in particular,
86            how to restore registers
87
88    DW_CFA_... = DWARF2 CFA call frame instruction
89    DW_TAG_... = DWARF2 DIE tag */
90
91 /* Decide whether we want to emit frame unwind information for the current
92    translation unit.  */
93
94 int
95 dwarf2out_do_frame ()
96 {
97   return (write_symbols == DWARF2_DEBUG
98           || write_symbols == VMS_AND_DWARF2_DEBUG
99 #ifdef DWARF2_FRAME_INFO
100           || DWARF2_FRAME_INFO
101 #endif
102 #ifdef DWARF2_UNWIND_INFO
103           || flag_unwind_tables
104           || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)
105 #endif
106           );
107 }
108
109 /* The size of the target's pointer type.  */
110 #ifndef PTR_SIZE
111 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
112 #endif
113
114 /* Default version of targetm.eh_frame_section.  Note this must appear
115    outside the DWARF2_DEBUGGING_INFO || DWARF2_UNWIND_INFO macro
116    guards.  */
117
118 void
119 default_eh_frame_section ()
120 {
121 #ifdef EH_FRAME_SECTION_NAME
122 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
123   int fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
124   int per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
125   int lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
126   int flags;
127
128   flags = (! flag_pic
129            || ((fde_encoding & 0x70) != DW_EH_PE_absptr
130                && (fde_encoding & 0x70) != DW_EH_PE_aligned
131                && (per_encoding & 0x70) != DW_EH_PE_absptr
132                && (per_encoding & 0x70) != DW_EH_PE_aligned
133                && (lsda_encoding & 0x70) != DW_EH_PE_absptr
134                && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
135           ? 0 : SECTION_WRITE;
136   named_section_flags (EH_FRAME_SECTION_NAME, flags);
137 #else
138   named_section_flags (EH_FRAME_SECTION_NAME, SECTION_WRITE);
139 #endif
140 #else
141   tree label = get_file_function_name ('F');
142
143   data_section ();
144   ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
145   (*targetm.asm_out.globalize_label) (asm_out_file, IDENTIFIER_POINTER (label));
146   ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
147 #endif
148 }
149
150 /* Array of RTXes referenced by the debugging information, which therefore
151    must be kept around forever.  */
152 static GTY(()) varray_type used_rtx_varray;
153
154 /* A pointer to the base of a list of incomplete types which might be
155    completed at some later time.  incomplete_types_list needs to be a VARRAY
156    because we want to tell the garbage collector about it.  */
157 static GTY(()) varray_type incomplete_types;
158
159 /* A pointer to the base of a table of references to declaration
160    scopes.  This table is a display which tracks the nesting
161    of declaration scopes at the current scope and containing
162    scopes.  This table is used to find the proper place to
163    define type declaration DIE's.  */
164 static GTY(()) varray_type decl_scope_table;
165
166 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 typedef union dw_cfi_oprnd_struct
183 {
184   unsigned long dw_cfi_reg_num;
185   long int dw_cfi_offset;
186   const char *dw_cfi_addr;
187   struct dw_loc_descr_struct *dw_cfi_loc;
188 }
189 dw_cfi_oprnd;
190
191 typedef struct dw_cfi_struct
192 {
193   dw_cfi_ref dw_cfi_next;
194   enum dwarf_call_frame_info dw_cfi_opc;
195   dw_cfi_oprnd dw_cfi_oprnd1;
196   dw_cfi_oprnd dw_cfi_oprnd2;
197 }
198 dw_cfi_node;
199
200 /* This is how we define the location of the CFA. We use to handle it
201    as REG + OFFSET all the time,  but now it can be more complex.
202    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
203    Instead of passing around REG and OFFSET, we pass a copy
204    of this structure.  */
205 typedef struct cfa_loc
206 {
207   unsigned long reg;
208   long offset;
209   long base_offset;
210   int indirect;            /* 1 if CFA is accessed via a dereference.  */
211 } dw_cfa_location;
212
213 /* All call frame descriptions (FDE's) in the GCC generated DWARF
214    refer to a single Common Information Entry (CIE), defined at
215    the beginning of the .debug_frame section.  This use of a single
216    CIE obviates the need to keep track of multiple CIE's
217    in the DWARF generation routines below.  */
218
219 typedef struct dw_fde_struct
220 {
221   const char *dw_fde_begin;
222   const char *dw_fde_current_label;
223   const char *dw_fde_end;
224   dw_cfi_ref dw_fde_cfi;
225   unsigned funcdef_number;
226   unsigned all_throwers_are_sibcalls : 1;
227   unsigned nothrow : 1;
228   unsigned uses_eh_lsda : 1;
229 }
230 dw_fde_node;
231
232 /* Maximum size (in bytes) of an artificially generated label.  */
233 #define MAX_ARTIFICIAL_LABEL_BYTES      30
234
235 /* The size of addresses as they appear in the Dwarf 2 data.
236    Some architectures use word addresses to refer to code locations,
237    but Dwarf 2 info always uses byte addresses.  On such machines,
238    Dwarf 2 addresses need to be larger than the architecture's
239    pointers.  */
240 #ifndef DWARF2_ADDR_SIZE
241 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
242 #endif
243
244 /* The size in bytes of a DWARF field indicating an offset or length
245    relative to a debug info section, specified to be 4 bytes in the
246    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
247    as PTR_SIZE.  */
248
249 #ifndef DWARF_OFFSET_SIZE
250 #define DWARF_OFFSET_SIZE 4
251 #endif
252
253 #define DWARF_VERSION 2
254
255 /* Round SIZE up to the nearest BOUNDARY.  */
256 #define DWARF_ROUND(SIZE,BOUNDARY) \
257   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
258
259 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
260 #ifndef DWARF_CIE_DATA_ALIGNMENT
261 #ifdef STACK_GROWS_DOWNWARD
262 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
263 #else
264 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
265 #endif
266 #endif
267
268 /* A pointer to the base of a table that contains frame description
269    information for each routine.  */
270 static dw_fde_ref fde_table;
271
272 /* Number of elements currently allocated for fde_table.  */
273 static unsigned fde_table_allocated;
274
275 /* Number of elements in fde_table currently in use.  */
276 static unsigned fde_table_in_use;
277
278 /* Size (in elements) of increments by which we may expand the
279    fde_table.  */
280 #define FDE_TABLE_INCREMENT 256
281
282 /* A list of call frame insns for the CIE.  */
283 static dw_cfi_ref cie_cfi_head;
284
285 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
286    attribute that accelerates the lookup of the FDE associated
287    with the subprogram.  This variable holds the table index of the FDE
288    associated with the current function (body) definition.  */
289 static unsigned current_funcdef_fde;
290
291 struct ht *debug_str_hash;
292
293 struct indirect_string_node
294 {
295   struct ht_identifier id;
296   unsigned int refcount;
297   unsigned int form;
298   char *label;
299 };
300
301 /* Forward declarations for functions defined in this file.  */
302
303 static char *stripattributes            PARAMS ((const char *));
304 static const char *dwarf_cfi_name       PARAMS ((unsigned));
305 static dw_cfi_ref new_cfi               PARAMS ((void));
306 static void add_cfi                     PARAMS ((dw_cfi_ref *, dw_cfi_ref));
307 static void add_fde_cfi                 PARAMS ((const char *, dw_cfi_ref));
308 static void lookup_cfa_1                PARAMS ((dw_cfi_ref,
309                                                  dw_cfa_location *));
310 static void lookup_cfa                  PARAMS ((dw_cfa_location *));
311 static void reg_save                    PARAMS ((const char *, unsigned,
312                                                  unsigned, long));
313 static void initial_return_save         PARAMS ((rtx));
314 static long stack_adjust_offset         PARAMS ((rtx));
315 static void output_cfi                  PARAMS ((dw_cfi_ref, dw_fde_ref, int));
316 static void output_call_frame_info      PARAMS ((int));
317 static void dwarf2out_stack_adjust      PARAMS ((rtx));
318 static void queue_reg_save              PARAMS ((const char *, rtx, long));
319 static void flush_queued_reg_saves      PARAMS ((void));
320 static bool clobbers_queued_reg_save    PARAMS ((rtx));
321 static void dwarf2out_frame_debug_expr  PARAMS ((rtx, const char *));
322
323 /* Support for complex CFA locations.  */
324 static void output_cfa_loc              PARAMS ((dw_cfi_ref));
325 static void get_cfa_from_loc_descr      PARAMS ((dw_cfa_location *,
326                                                 struct dw_loc_descr_struct *));
327 static struct dw_loc_descr_struct *build_cfa_loc
328                                         PARAMS ((dw_cfa_location *));
329 static void def_cfa_1                   PARAMS ((const char *,
330                                                  dw_cfa_location *));
331
332 /* How to start an assembler comment.  */
333 #ifndef ASM_COMMENT_START
334 #define ASM_COMMENT_START ";#"
335 #endif
336
337 /* Data and reference forms for relocatable data.  */
338 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
339 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
340
341 #ifndef DEBUG_FRAME_SECTION
342 #define DEBUG_FRAME_SECTION     ".debug_frame"
343 #endif
344
345 #ifndef FUNC_BEGIN_LABEL
346 #define FUNC_BEGIN_LABEL        "LFB"
347 #endif
348
349 #ifndef FUNC_END_LABEL
350 #define FUNC_END_LABEL          "LFE"
351 #endif
352
353 #define FRAME_BEGIN_LABEL       "Lframe"
354 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
355 #define CIE_END_LABEL           "LECIE"
356 #define FDE_LABEL               "LSFDE"
357 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
358 #define FDE_END_LABEL           "LEFDE"
359 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
360 #define LINE_NUMBER_END_LABEL   "LELT"
361 #define LN_PROLOG_AS_LABEL      "LASLTP"
362 #define LN_PROLOG_END_LABEL     "LELTP"
363 #define DIE_LABEL_PREFIX        "DW"
364
365 /* The DWARF 2 CFA column which tracks the return address.  Normally this
366    is the column for PC, or the first column after all of the hard
367    registers.  */
368 #ifndef DWARF_FRAME_RETURN_COLUMN
369 #ifdef PC_REGNUM
370 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
371 #else
372 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
373 #endif
374 #endif
375
376 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
377    default, we just provide columns for all registers.  */
378 #ifndef DWARF_FRAME_REGNUM
379 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
380 #endif
381
382 /* The offset from the incoming value of %sp to the top of the stack frame
383    for the current function.  */
384 #ifndef INCOMING_FRAME_SP_OFFSET
385 #define INCOMING_FRAME_SP_OFFSET 0
386 #endif
387 \f
388 /* Hook used by __throw.  */
389
390 rtx
391 expand_builtin_dwarf_fp_regnum ()
392 {
393   return GEN_INT (DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM));
394 }
395
396 /* Return a pointer to a copy of the section string name S with all
397    attributes stripped off, and an asterisk prepended (for assemble_name).  */
398
399 static inline char *
400 stripattributes (s)
401      const char *s;
402 {
403   char *stripped = xmalloc (strlen (s) + 2);
404   char *p = stripped;
405
406   *p++ = '*';
407
408   while (*s && *s != ',')
409     *p++ = *s++;
410
411   *p = '\0';
412   return stripped;
413 }
414
415 /* Generate code to initialize the register size table.  */
416
417 void
418 expand_builtin_init_dwarf_reg_sizes (address)
419      tree address;
420 {
421   int i;
422   enum machine_mode mode = TYPE_MODE (char_type_node);
423   rtx addr = expand_expr (address, NULL_RTX, VOIDmode, 0);
424   rtx mem = gen_rtx_MEM (BLKmode, addr);
425
426   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
427     if (DWARF_FRAME_REGNUM (i) < DWARF_FRAME_REGISTERS)
428       {
429         HOST_WIDE_INT offset = DWARF_FRAME_REGNUM (i) * GET_MODE_SIZE (mode);
430         HOST_WIDE_INT size = GET_MODE_SIZE (reg_raw_mode[i]);
431
432         if (offset < 0)
433           continue;
434
435         emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
436       }
437 }
438
439 /* Convert a DWARF call frame info. operation to its string name */
440
441 static const char *
442 dwarf_cfi_name (cfi_opc)
443      unsigned cfi_opc;
444 {
445   switch (cfi_opc)
446     {
447     case DW_CFA_advance_loc:
448       return "DW_CFA_advance_loc";
449     case DW_CFA_offset:
450       return "DW_CFA_offset";
451     case DW_CFA_restore:
452       return "DW_CFA_restore";
453     case DW_CFA_nop:
454       return "DW_CFA_nop";
455     case DW_CFA_set_loc:
456       return "DW_CFA_set_loc";
457     case DW_CFA_advance_loc1:
458       return "DW_CFA_advance_loc1";
459     case DW_CFA_advance_loc2:
460       return "DW_CFA_advance_loc2";
461     case DW_CFA_advance_loc4:
462       return "DW_CFA_advance_loc4";
463     case DW_CFA_offset_extended:
464       return "DW_CFA_offset_extended";
465     case DW_CFA_restore_extended:
466       return "DW_CFA_restore_extended";
467     case DW_CFA_undefined:
468       return "DW_CFA_undefined";
469     case DW_CFA_same_value:
470       return "DW_CFA_same_value";
471     case DW_CFA_register:
472       return "DW_CFA_register";
473     case DW_CFA_remember_state:
474       return "DW_CFA_remember_state";
475     case DW_CFA_restore_state:
476       return "DW_CFA_restore_state";
477     case DW_CFA_def_cfa:
478       return "DW_CFA_def_cfa";
479     case DW_CFA_def_cfa_register:
480       return "DW_CFA_def_cfa_register";
481     case DW_CFA_def_cfa_offset:
482       return "DW_CFA_def_cfa_offset";
483
484     /* DWARF 3 */
485     case DW_CFA_def_cfa_expression:
486       return "DW_CFA_def_cfa_expression";
487     case DW_CFA_expression:
488       return "DW_CFA_expression";
489     case DW_CFA_offset_extended_sf:
490       return "DW_CFA_offset_extended_sf";
491     case DW_CFA_def_cfa_sf:
492       return "DW_CFA_def_cfa_sf";
493     case DW_CFA_def_cfa_offset_sf:
494       return "DW_CFA_def_cfa_offset_sf";
495
496     /* SGI/MIPS specific */
497     case DW_CFA_MIPS_advance_loc8:
498       return "DW_CFA_MIPS_advance_loc8";
499
500     /* GNU extensions */
501     case DW_CFA_GNU_window_save:
502       return "DW_CFA_GNU_window_save";
503     case DW_CFA_GNU_args_size:
504       return "DW_CFA_GNU_args_size";
505     case DW_CFA_GNU_negative_offset_extended:
506       return "DW_CFA_GNU_negative_offset_extended";
507
508     default:
509       return "DW_CFA_<unknown>";
510     }
511 }
512
513 /* Return a pointer to a newly allocated Call Frame Instruction.  */
514
515 static inline dw_cfi_ref
516 new_cfi ()
517 {
518   dw_cfi_ref cfi = (dw_cfi_ref) xmalloc (sizeof (dw_cfi_node));
519
520   cfi->dw_cfi_next = NULL;
521   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
522   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
523
524   return cfi;
525 }
526
527 /* Add a Call Frame Instruction to list of instructions.  */
528
529 static inline void
530 add_cfi (list_head, cfi)
531      dw_cfi_ref *list_head;
532      dw_cfi_ref cfi;
533 {
534   dw_cfi_ref *p;
535
536   /* Find the end of the chain.  */
537   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
538     ;
539
540   *p = cfi;
541 }
542
543 /* Generate a new label for the CFI info to refer to.  */
544
545 char *
546 dwarf2out_cfi_label ()
547 {
548   static char label[20];
549   static unsigned long label_num = 0;
550
551   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", label_num++);
552   ASM_OUTPUT_LABEL (asm_out_file, label);
553   return label;
554 }
555
556 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
557    or to the CIE if LABEL is NULL.  */
558
559 static void
560 add_fde_cfi (label, cfi)
561      const char *label;
562      dw_cfi_ref cfi;
563 {
564   if (label)
565     {
566       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
567
568       if (*label == 0)
569         label = dwarf2out_cfi_label ();
570
571       if (fde->dw_fde_current_label == NULL
572           || strcmp (label, fde->dw_fde_current_label) != 0)
573         {
574           dw_cfi_ref xcfi;
575
576           fde->dw_fde_current_label = label = xstrdup (label);
577
578           /* Set the location counter to the new label.  */
579           xcfi = new_cfi ();
580           xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
581           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
582           add_cfi (&fde->dw_fde_cfi, xcfi);
583         }
584
585       add_cfi (&fde->dw_fde_cfi, cfi);
586     }
587
588   else
589     add_cfi (&cie_cfi_head, cfi);
590 }
591
592 /* Subroutine of lookup_cfa.  */
593
594 static inline void
595 lookup_cfa_1 (cfi, loc)
596      dw_cfi_ref cfi;
597      dw_cfa_location *loc;
598 {
599   switch (cfi->dw_cfi_opc)
600     {
601     case DW_CFA_def_cfa_offset:
602       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
603       break;
604     case DW_CFA_def_cfa_register:
605       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
606       break;
607     case DW_CFA_def_cfa:
608       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
609       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
610       break;
611     case DW_CFA_def_cfa_expression:
612       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
613       break;
614     default:
615       break;
616     }
617 }
618
619 /* Find the previous value for the CFA.  */
620
621 static void
622 lookup_cfa (loc)
623      dw_cfa_location *loc;
624 {
625   dw_cfi_ref cfi;
626
627   loc->reg = (unsigned long) -1;
628   loc->offset = 0;
629   loc->indirect = 0;
630   loc->base_offset = 0;
631
632   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
633     lookup_cfa_1 (cfi, loc);
634
635   if (fde_table_in_use)
636     {
637       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
638       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
639         lookup_cfa_1 (cfi, loc);
640     }
641 }
642
643 /* The current rule for calculating the DWARF2 canonical frame address.  */
644 static dw_cfa_location cfa;
645
646 /* The register used for saving registers to the stack, and its offset
647    from the CFA.  */
648 static dw_cfa_location cfa_store;
649
650 /* The running total of the size of arguments pushed onto the stack.  */
651 static long args_size;
652
653 /* The last args_size we actually output.  */
654 static long old_args_size;
655
656 /* Entry point to update the canonical frame address (CFA).
657    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
658    calculated from REG+OFFSET.  */
659
660 void
661 dwarf2out_def_cfa (label, reg, offset)
662      const char *label;
663      unsigned reg;
664      long offset;
665 {
666   dw_cfa_location loc;
667   loc.indirect = 0;
668   loc.base_offset = 0;
669   loc.reg = reg;
670   loc.offset = offset;
671   def_cfa_1 (label, &loc);
672 }
673
674 /* This routine does the actual work.  The CFA is now calculated from
675    the dw_cfa_location structure.  */
676
677 static void
678 def_cfa_1 (label, loc_p)
679      const char *label;
680      dw_cfa_location *loc_p;
681 {
682   dw_cfi_ref cfi;
683   dw_cfa_location old_cfa, loc;
684
685   cfa = *loc_p;
686   loc = *loc_p;
687
688   if (cfa_store.reg == loc.reg && loc.indirect == 0)
689     cfa_store.offset = loc.offset;
690
691   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
692   lookup_cfa (&old_cfa);
693
694   /* If nothing changed, no need to issue any call frame instructions.  */
695   if (loc.reg == old_cfa.reg && loc.offset == old_cfa.offset
696       && loc.indirect == old_cfa.indirect
697       && (loc.indirect == 0 || loc.base_offset == old_cfa.base_offset))
698     return;
699
700   cfi = new_cfi ();
701
702   if (loc.reg == old_cfa.reg && !loc.indirect)
703     {
704       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction,
705          indicating the CFA register did not change but the offset
706          did.  */
707       cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
708       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
709     }
710
711 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
712   else if (loc.offset == old_cfa.offset && old_cfa.reg != (unsigned long) -1
713            && !loc.indirect)
714     {
715       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
716          indicating the CFA register has changed to <register> but the
717          offset has not changed.  */
718       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
719       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
720     }
721 #endif
722
723   else if (loc.indirect == 0)
724     {
725       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
726          indicating the CFA register has changed to <register> with
727          the specified offset.  */
728       cfi->dw_cfi_opc = DW_CFA_def_cfa;
729       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
730       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
731     }
732   else
733     {
734       /* Construct a DW_CFA_def_cfa_expression instruction to
735          calculate the CFA using a full location expression since no
736          register-offset pair is available.  */
737       struct dw_loc_descr_struct *loc_list;
738
739       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
740       loc_list = build_cfa_loc (&loc);
741       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
742     }
743
744   add_fde_cfi (label, cfi);
745 }
746
747 /* Add the CFI for saving a register.  REG is the CFA column number.
748    LABEL is passed to add_fde_cfi.
749    If SREG is -1, the register is saved at OFFSET from the CFA;
750    otherwise it is saved in SREG.  */
751
752 static void
753 reg_save (label, reg, sreg, offset)
754      const char *label;
755      unsigned reg;
756      unsigned sreg;
757      long offset;
758 {
759   dw_cfi_ref cfi = new_cfi ();
760
761   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
762
763   /* The following comparison is correct. -1 is used to indicate that
764      the value isn't a register number.  */
765   if (sreg == (unsigned int) -1)
766     {
767       if (reg & ~0x3f)
768         /* The register number won't fit in 6 bits, so we have to use
769            the long form.  */
770         cfi->dw_cfi_opc = DW_CFA_offset_extended;
771       else
772         cfi->dw_cfi_opc = DW_CFA_offset;
773
774 #ifdef ENABLE_CHECKING
775       {
776         /* If we get an offset that is not a multiple of
777            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
778            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
779            description.  */
780         long check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
781
782         if (check_offset * DWARF_CIE_DATA_ALIGNMENT != offset)
783           abort ();
784       }
785 #endif
786       offset /= DWARF_CIE_DATA_ALIGNMENT;
787       if (offset < 0)
788         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
789
790       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
791     }
792   else if (sreg == reg)
793     /* We could emit a DW_CFA_same_value in this case, but don't bother.  */
794     return;
795   else
796     {
797       cfi->dw_cfi_opc = DW_CFA_register;
798       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
799     }
800
801   add_fde_cfi (label, cfi);
802 }
803
804 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
805    This CFI tells the unwinder that it needs to restore the window registers
806    from the previous frame's window save area.
807
808    ??? Perhaps we should note in the CIE where windows are saved (instead of
809    assuming 0(cfa)) and what registers are in the window.  */
810
811 void
812 dwarf2out_window_save (label)
813      const char *label;
814 {
815   dw_cfi_ref cfi = new_cfi ();
816
817   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
818   add_fde_cfi (label, cfi);
819 }
820
821 /* Add a CFI to update the running total of the size of arguments
822    pushed onto the stack.  */
823
824 void
825 dwarf2out_args_size (label, size)
826      const char *label;
827      long size;
828 {
829   dw_cfi_ref cfi;
830
831   if (size == old_args_size)
832     return;
833
834   old_args_size = size;
835
836   cfi = new_cfi ();
837   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
838   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
839   add_fde_cfi (label, cfi);
840 }
841
842 /* Entry point for saving a register to the stack.  REG is the GCC register
843    number.  LABEL and OFFSET are passed to reg_save.  */
844
845 void
846 dwarf2out_reg_save (label, reg, offset)
847      const char *label;
848      unsigned reg;
849      long offset;
850 {
851   reg_save (label, DWARF_FRAME_REGNUM (reg), -1, offset);
852 }
853
854 /* Entry point for saving the return address in the stack.
855    LABEL and OFFSET are passed to reg_save.  */
856
857 void
858 dwarf2out_return_save (label, offset)
859      const char *label;
860      long offset;
861 {
862   reg_save (label, DWARF_FRAME_RETURN_COLUMN, -1, offset);
863 }
864
865 /* Entry point for saving the return address in a register.
866    LABEL and SREG are passed to reg_save.  */
867
868 void
869 dwarf2out_return_reg (label, sreg)
870      const char *label;
871      unsigned sreg;
872 {
873   reg_save (label, DWARF_FRAME_RETURN_COLUMN, sreg, 0);
874 }
875
876 /* Record the initial position of the return address.  RTL is
877    INCOMING_RETURN_ADDR_RTX.  */
878
879 static void
880 initial_return_save (rtl)
881      rtx rtl;
882 {
883   unsigned int reg = (unsigned int) -1;
884   HOST_WIDE_INT offset = 0;
885
886   switch (GET_CODE (rtl))
887     {
888     case REG:
889       /* RA is in a register.  */
890       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
891       break;
892
893     case MEM:
894       /* RA is on the stack.  */
895       rtl = XEXP (rtl, 0);
896       switch (GET_CODE (rtl))
897         {
898         case REG:
899           if (REGNO (rtl) != STACK_POINTER_REGNUM)
900             abort ();
901           offset = 0;
902           break;
903
904         case PLUS:
905           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
906             abort ();
907           offset = INTVAL (XEXP (rtl, 1));
908           break;
909
910         case MINUS:
911           if (REGNO (XEXP (rtl, 0)) != STACK_POINTER_REGNUM)
912             abort ();
913           offset = -INTVAL (XEXP (rtl, 1));
914           break;
915
916         default:
917           abort ();
918         }
919
920       break;
921
922     case PLUS:
923       /* The return address is at some offset from any value we can
924          actually load.  For instance, on the SPARC it is in %i7+8. Just
925          ignore the offset for now; it doesn't matter for unwinding frames.  */
926       if (GET_CODE (XEXP (rtl, 1)) != CONST_INT)
927         abort ();
928       initial_return_save (XEXP (rtl, 0));
929       return;
930
931     default:
932       abort ();
933     }
934
935   reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
936 }
937
938 /* Given a SET, calculate the amount of stack adjustment it
939    contains.  */
940
941 static long
942 stack_adjust_offset (pattern)
943      rtx pattern;
944 {
945   rtx src = SET_SRC (pattern);
946   rtx dest = SET_DEST (pattern);
947   HOST_WIDE_INT offset = 0;
948   enum rtx_code code;
949
950   if (dest == stack_pointer_rtx)
951     {
952       /* (set (reg sp) (plus (reg sp) (const_int))) */
953       code = GET_CODE (src);
954       if (! (code == PLUS || code == MINUS)
955           || XEXP (src, 0) != stack_pointer_rtx
956           || GET_CODE (XEXP (src, 1)) != CONST_INT)
957         return 0;
958
959       offset = INTVAL (XEXP (src, 1));
960       if (code == PLUS)
961         offset = -offset;
962     }
963   else if (GET_CODE (dest) == MEM)
964     {
965       /* (set (mem (pre_dec (reg sp))) (foo)) */
966       src = XEXP (dest, 0);
967       code = GET_CODE (src);
968
969       switch (code)
970         {
971         case PRE_MODIFY:
972         case POST_MODIFY:
973           if (XEXP (src, 0) == stack_pointer_rtx)
974             {
975               rtx val = XEXP (XEXP (src, 1), 1);
976               /* We handle only adjustments by constant amount.  */
977               if (GET_CODE (XEXP (src, 1)) != PLUS ||
978                   GET_CODE (val) != CONST_INT)
979                 abort ();
980               offset = -INTVAL (val);
981               break;
982             }
983           return 0;
984
985         case PRE_DEC:
986         case POST_DEC:
987           if (XEXP (src, 0) == stack_pointer_rtx)
988             {
989               offset = GET_MODE_SIZE (GET_MODE (dest));
990               break;
991             }
992           return 0;
993
994         case PRE_INC:
995         case POST_INC:
996           if (XEXP (src, 0) == stack_pointer_rtx)
997             {
998               offset = -GET_MODE_SIZE (GET_MODE (dest));
999               break;
1000             }
1001           return 0;
1002
1003         default:
1004           return 0;
1005         }
1006     }
1007   else
1008     return 0;
1009
1010   return offset;
1011 }
1012
1013 /* Check INSN to see if it looks like a push or a stack adjustment, and
1014    make a note of it if it does.  EH uses this information to find out how
1015    much extra space it needs to pop off the stack.  */
1016
1017 static void
1018 dwarf2out_stack_adjust (insn)
1019      rtx insn;
1020 {
1021   HOST_WIDE_INT offset;
1022   const char *label;
1023   int i;
1024
1025   if (!flag_asynchronous_unwind_tables && GET_CODE (insn) == CALL_INSN)
1026     {
1027       /* Extract the size of the args from the CALL rtx itself.  */
1028       insn = PATTERN (insn);
1029       if (GET_CODE (insn) == PARALLEL)
1030         insn = XVECEXP (insn, 0, 0);
1031       if (GET_CODE (insn) == SET)
1032         insn = SET_SRC (insn);
1033       if (GET_CODE (insn) != CALL)
1034         abort ();
1035
1036       dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1037       return;
1038     }
1039
1040   /* If only calls can throw, and we have a frame pointer,
1041      save up adjustments until we see the CALL_INSN.  */
1042   else if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1043     return;
1044
1045   if (GET_CODE (insn) == BARRIER)
1046     {
1047       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1048          the compiler will have already emitted a stack adjustment, but
1049          doesn't bother for calls to noreturn functions.  */
1050 #ifdef STACK_GROWS_DOWNWARD
1051       offset = -args_size;
1052 #else
1053       offset = args_size;
1054 #endif
1055     }
1056   else if (GET_CODE (PATTERN (insn)) == SET)
1057     offset = stack_adjust_offset (PATTERN (insn));
1058   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1059            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1060     {
1061       /* There may be stack adjustments inside compound insns.  Search
1062          for them.  */
1063       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1064         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1065           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1066     }
1067   else
1068     return;
1069
1070   if (offset == 0)
1071     return;
1072
1073   if (cfa.reg == STACK_POINTER_REGNUM)
1074     cfa.offset += offset;
1075
1076 #ifndef STACK_GROWS_DOWNWARD
1077   offset = -offset;
1078 #endif
1079
1080   args_size += offset;
1081   if (args_size < 0)
1082     args_size = 0;
1083
1084   label = dwarf2out_cfi_label ();
1085   def_cfa_1 (label, &cfa);
1086   dwarf2out_args_size (label, args_size);
1087 }
1088
1089 /* We delay emitting a register save until either (a) we reach the end
1090    of the prologue or (b) the register is clobbered.  This clusters
1091    register saves so that there are fewer pc advances.  */
1092
1093 struct queued_reg_save
1094 {
1095   struct queued_reg_save *next;
1096   rtx reg;
1097   long cfa_offset;
1098 };
1099
1100 static struct queued_reg_save *queued_reg_saves;
1101 static const char *last_reg_save_label;
1102
1103 static void
1104 queue_reg_save (label, reg, offset)
1105      const char *label;
1106      rtx reg;
1107      long offset;
1108 {
1109   struct queued_reg_save *q = (struct queued_reg_save *) xmalloc (sizeof (*q));
1110
1111   q->next = queued_reg_saves;
1112   q->reg = reg;
1113   q->cfa_offset = offset;
1114   queued_reg_saves = q;
1115
1116   last_reg_save_label = label;
1117 }
1118
1119 static void
1120 flush_queued_reg_saves ()
1121 {
1122   struct queued_reg_save *q, *next;
1123
1124   for (q = queued_reg_saves; q; q = next)
1125     {
1126       dwarf2out_reg_save (last_reg_save_label, REGNO (q->reg), q->cfa_offset);
1127       next = q->next;
1128       free (q);
1129     }
1130
1131   queued_reg_saves = NULL;
1132   last_reg_save_label = NULL;
1133 }
1134
1135 static bool
1136 clobbers_queued_reg_save (insn)
1137      rtx insn;
1138 {
1139   struct queued_reg_save *q;
1140
1141   for (q = queued_reg_saves; q; q = q->next)
1142     if (modified_in_p (q->reg, insn))
1143       return true;
1144
1145   return false;
1146 }
1147
1148
1149 /* A temporary register holding an integral value used in adjusting SP
1150    or setting up the store_reg.  The "offset" field holds the integer
1151    value, not an offset.  */
1152 static dw_cfa_location cfa_temp;
1153
1154 /* Record call frame debugging information for an expression EXPR,
1155    which either sets SP or FP (adjusting how we calculate the frame
1156    address) or saves a register to the stack.  LABEL indicates the
1157    address of EXPR.
1158
1159    This function encodes a state machine mapping rtxes to actions on
1160    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1161    users need not read the source code.
1162
1163   The High-Level Picture
1164
1165   Changes in the register we use to calculate the CFA: Currently we
1166   assume that if you copy the CFA register into another register, we
1167   should take the other one as the new CFA register; this seems to
1168   work pretty well.  If it's wrong for some target, it's simple
1169   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1170
1171   Changes in the register we use for saving registers to the stack:
1172   This is usually SP, but not always.  Again, we deduce that if you
1173   copy SP into another register (and SP is not the CFA register),
1174   then the new register is the one we will be using for register
1175   saves.  This also seems to work.
1176
1177   Register saves: There's not much guesswork about this one; if
1178   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1179   register save, and the register used to calculate the destination
1180   had better be the one we think we're using for this purpose.
1181
1182   Except: If the register being saved is the CFA register, and the
1183   offset is nonzero, we are saving the CFA, so we assume we have to
1184   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1185   the intent is to save the value of SP from the previous frame.
1186
1187   Invariants / Summaries of Rules
1188
1189   cfa          current rule for calculating the CFA.  It usually
1190                consists of a register and an offset.
1191   cfa_store    register used by prologue code to save things to the stack
1192                cfa_store.offset is the offset from the value of
1193                cfa_store.reg to the actual CFA
1194   cfa_temp     register holding an integral value.  cfa_temp.offset
1195                stores the value, which will be used to adjust the
1196                stack pointer.  cfa_temp is also used like cfa_store,
1197                to track stores to the stack via fp or a temp reg.
1198
1199   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1200                with cfa.reg as the first operand changes the cfa.reg and its
1201                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1202                cfa_temp.offset.
1203
1204   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1205                expression yielding a constant.  This sets cfa_temp.reg
1206                and cfa_temp.offset.
1207
1208   Rule 5:      Create a new register cfa_store used to save items to the
1209                stack.
1210
1211   Rules 10-14: Save a register to the stack.  Define offset as the
1212                difference of the original location and cfa_store's
1213                location (or cfa_temp's location if cfa_temp is used).
1214
1215   The Rules
1216
1217   "{a,b}" indicates a choice of a xor b.
1218   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1219
1220   Rule 1:
1221   (set <reg1> <reg2>:cfa.reg)
1222   effects: cfa.reg = <reg1>
1223            cfa.offset unchanged
1224            cfa_temp.reg = <reg1>
1225            cfa_temp.offset = cfa.offset
1226
1227   Rule 2:
1228   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1229                               {<const_int>,<reg>:cfa_temp.reg}))
1230   effects: cfa.reg = sp if fp used
1231            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1232            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1233              if cfa_store.reg==sp
1234
1235   Rule 3:
1236   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1237   effects: cfa.reg = fp
1238            cfa_offset += +/- <const_int>
1239
1240   Rule 4:
1241   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1242   constraints: <reg1> != fp
1243                <reg1> != sp
1244   effects: cfa.reg = <reg1>
1245            cfa_temp.reg = <reg1>
1246            cfa_temp.offset = cfa.offset
1247
1248   Rule 5:
1249   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1250   constraints: <reg1> != fp
1251                <reg1> != sp
1252   effects: cfa_store.reg = <reg1>
1253            cfa_store.offset = cfa.offset - cfa_temp.offset
1254
1255   Rule 6:
1256   (set <reg> <const_int>)
1257   effects: cfa_temp.reg = <reg>
1258            cfa_temp.offset = <const_int>
1259
1260   Rule 7:
1261   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1262   effects: cfa_temp.reg = <reg1>
1263            cfa_temp.offset |= <const_int>
1264
1265   Rule 8:
1266   (set <reg> (high <exp>))
1267   effects: none
1268
1269   Rule 9:
1270   (set <reg> (lo_sum <exp> <const_int>))
1271   effects: cfa_temp.reg = <reg>
1272            cfa_temp.offset = <const_int>
1273
1274   Rule 10:
1275   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1276   effects: cfa_store.offset -= <const_int>
1277            cfa.offset = cfa_store.offset if cfa.reg == sp
1278            cfa.reg = sp
1279            cfa.base_offset = -cfa_store.offset
1280
1281   Rule 11:
1282   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1283   effects: cfa_store.offset += -/+ mode_size(mem)
1284            cfa.offset = cfa_store.offset if cfa.reg == sp
1285            cfa.reg = sp
1286            cfa.base_offset = -cfa_store.offset
1287
1288   Rule 12:
1289   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1290
1291        <reg2>)
1292   effects: cfa.reg = <reg1>
1293            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1294
1295   Rule 13:
1296   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1297   effects: cfa.reg = <reg1>
1298            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1299
1300   Rule 14:
1301   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1302   effects: cfa.reg = <reg1>
1303            cfa.base_offset = -cfa_temp.offset
1304            cfa_temp.offset -= mode_size(mem)  */
1305
1306 static void
1307 dwarf2out_frame_debug_expr (expr, label)
1308      rtx expr;
1309      const char *label;
1310 {
1311   rtx src, dest;
1312   HOST_WIDE_INT offset;
1313
1314   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1315      the PARALLEL independently. The first element is always processed if
1316      it is a SET. This is for backward compatibility.   Other elements
1317      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1318      flag is set in them.  */
1319   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1320     {
1321       int par_index;
1322       int limit = XVECLEN (expr, 0);
1323
1324       for (par_index = 0; par_index < limit; par_index++)
1325         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1326             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1327                 || par_index == 0))
1328           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1329
1330       return;
1331     }
1332
1333   if (GET_CODE (expr) != SET)
1334     abort ();
1335
1336   src = SET_SRC (expr);
1337   dest = SET_DEST (expr);
1338
1339   switch (GET_CODE (dest))
1340     {
1341     case REG:
1342       /* Rule 1 */
1343       /* Update the CFA rule wrt SP or FP.  Make sure src is
1344          relative to the current CFA register.  */
1345       switch (GET_CODE (src))
1346         {
1347           /* Setting FP from SP.  */
1348         case REG:
1349           if (cfa.reg == (unsigned) REGNO (src))
1350             /* OK.  */
1351             ;
1352           else
1353             abort ();
1354
1355           /* We used to require that dest be either SP or FP, but the
1356              ARM copies SP to a temporary register, and from there to
1357              FP.  So we just rely on the backends to only set
1358              RTX_FRAME_RELATED_P on appropriate insns.  */
1359           cfa.reg = REGNO (dest);
1360           cfa_temp.reg = cfa.reg;
1361           cfa_temp.offset = cfa.offset;
1362           break;
1363
1364         case PLUS:
1365         case MINUS:
1366         case LO_SUM:
1367           if (dest == stack_pointer_rtx)
1368             {
1369               /* Rule 2 */
1370               /* Adjusting SP.  */
1371               switch (GET_CODE (XEXP (src, 1)))
1372                 {
1373                 case CONST_INT:
1374                   offset = INTVAL (XEXP (src, 1));
1375                   break;
1376                 case REG:
1377                   if ((unsigned) REGNO (XEXP (src, 1)) != cfa_temp.reg)
1378                     abort ();
1379                   offset = cfa_temp.offset;
1380                   break;
1381                 default:
1382                   abort ();
1383                 }
1384
1385               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1386                 {
1387                   /* Restoring SP from FP in the epilogue.  */
1388                   if (cfa.reg != (unsigned) HARD_FRAME_POINTER_REGNUM)
1389                     abort ();
1390                   cfa.reg = STACK_POINTER_REGNUM;
1391                 }
1392               else if (GET_CODE (src) == LO_SUM)
1393                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1394                 ;
1395               else if (XEXP (src, 0) != stack_pointer_rtx)
1396                 abort ();
1397
1398               if (GET_CODE (src) != MINUS)
1399                 offset = -offset;
1400               if (cfa.reg == STACK_POINTER_REGNUM)
1401                 cfa.offset += offset;
1402               if (cfa_store.reg == STACK_POINTER_REGNUM)
1403                 cfa_store.offset += offset;
1404             }
1405           else if (dest == hard_frame_pointer_rtx)
1406             {
1407               /* Rule 3 */
1408               /* Either setting the FP from an offset of the SP,
1409                  or adjusting the FP */
1410               if (! frame_pointer_needed)
1411                 abort ();
1412
1413               if (GET_CODE (XEXP (src, 0)) == REG
1414                   && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1415                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1416                 {
1417                   offset = INTVAL (XEXP (src, 1));
1418                   if (GET_CODE (src) != MINUS)
1419                     offset = -offset;
1420                   cfa.offset += offset;
1421                   cfa.reg = HARD_FRAME_POINTER_REGNUM;
1422                 }
1423               else
1424                 abort ();
1425             }
1426           else
1427             {
1428               if (GET_CODE (src) == MINUS)
1429                 abort ();
1430
1431               /* Rule 4 */
1432               if (GET_CODE (XEXP (src, 0)) == REG
1433                   && REGNO (XEXP (src, 0)) == cfa.reg
1434                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1435                 {
1436                   /* Setting a temporary CFA register that will be copied
1437                      into the FP later on.  */
1438                   offset = - INTVAL (XEXP (src, 1));
1439                   cfa.offset += offset;
1440                   cfa.reg = REGNO (dest);
1441                   /* Or used to save regs to the stack.  */
1442                   cfa_temp.reg = cfa.reg;
1443                   cfa_temp.offset = cfa.offset;
1444                 }
1445
1446               /* Rule 5 */
1447               else if (GET_CODE (XEXP (src, 0)) == REG
1448                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1449                        && XEXP (src, 1) == stack_pointer_rtx)
1450                 {
1451                   /* Setting a scratch register that we will use instead
1452                      of SP for saving registers to the stack.  */
1453                   if (cfa.reg != STACK_POINTER_REGNUM)
1454                     abort ();
1455                   cfa_store.reg = REGNO (dest);
1456                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1457                 }
1458
1459               /* Rule 9 */
1460               else if (GET_CODE (src) == LO_SUM
1461                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1462                 {
1463                   cfa_temp.reg = REGNO (dest);
1464                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1465                 }
1466               else
1467                 abort ();
1468             }
1469           break;
1470
1471           /* Rule 6 */
1472         case CONST_INT:
1473           cfa_temp.reg = REGNO (dest);
1474           cfa_temp.offset = INTVAL (src);
1475           break;
1476
1477           /* Rule 7 */
1478         case IOR:
1479           if (GET_CODE (XEXP (src, 0)) != REG
1480               || (unsigned) REGNO (XEXP (src, 0)) != cfa_temp.reg
1481               || GET_CODE (XEXP (src, 1)) != CONST_INT)
1482             abort ();
1483
1484           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1485             cfa_temp.reg = REGNO (dest);
1486           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1487           break;
1488
1489           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1490              which will fill in all of the bits.  */
1491           /* Rule 8 */
1492         case HIGH:
1493           break;
1494
1495         default:
1496           abort ();
1497         }
1498
1499       def_cfa_1 (label, &cfa);
1500       break;
1501
1502     case MEM:
1503       if (GET_CODE (src) != REG)
1504         abort ();
1505
1506       /* Saving a register to the stack.  Make sure dest is relative to the
1507          CFA register.  */
1508       switch (GET_CODE (XEXP (dest, 0)))
1509         {
1510           /* Rule 10 */
1511           /* With a push.  */
1512         case PRE_MODIFY:
1513           /* We can't handle variable size modifications.  */
1514           if (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1)) != CONST_INT)
1515             abort ();
1516           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1517
1518           if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1519               || cfa_store.reg != STACK_POINTER_REGNUM)
1520             abort ();
1521
1522           cfa_store.offset += offset;
1523           if (cfa.reg == STACK_POINTER_REGNUM)
1524             cfa.offset = cfa_store.offset;
1525
1526           offset = -cfa_store.offset;
1527           break;
1528
1529           /* Rule 11 */
1530         case PRE_INC:
1531         case PRE_DEC:
1532           offset = GET_MODE_SIZE (GET_MODE (dest));
1533           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1534             offset = -offset;
1535
1536           if (REGNO (XEXP (XEXP (dest, 0), 0)) != STACK_POINTER_REGNUM
1537               || cfa_store.reg != STACK_POINTER_REGNUM)
1538             abort ();
1539
1540           cfa_store.offset += offset;
1541           if (cfa.reg == STACK_POINTER_REGNUM)
1542             cfa.offset = cfa_store.offset;
1543
1544           offset = -cfa_store.offset;
1545           break;
1546
1547           /* Rule 12 */
1548           /* With an offset.  */
1549         case PLUS:
1550         case MINUS:
1551         case LO_SUM:
1552           if (GET_CODE (XEXP (XEXP (dest, 0), 1)) != CONST_INT)
1553             abort ();
1554           offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1555           if (GET_CODE (XEXP (dest, 0)) == MINUS)
1556             offset = -offset;
1557
1558           if (cfa_store.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1559             offset -= cfa_store.offset;
1560           else if (cfa_temp.reg == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1561             offset -= cfa_temp.offset;
1562           else
1563             abort ();
1564           break;
1565
1566           /* Rule 13 */
1567           /* Without an offset.  */
1568         case REG:
1569           if (cfa_store.reg == (unsigned) REGNO (XEXP (dest, 0)))
1570             offset = -cfa_store.offset;
1571           else if (cfa_temp.reg == (unsigned) REGNO (XEXP (dest, 0)))
1572             offset = -cfa_temp.offset;
1573           else
1574             abort ();
1575           break;
1576
1577           /* Rule 14 */
1578         case POST_INC:
1579           if (cfa_temp.reg != (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)))
1580             abort ();
1581           offset = -cfa_temp.offset;
1582           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1583           break;
1584
1585         default:
1586           abort ();
1587         }
1588
1589       if (REGNO (src) != STACK_POINTER_REGNUM
1590           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1591           && (unsigned) REGNO (src) == cfa.reg)
1592         {
1593           /* We're storing the current CFA reg into the stack.  */
1594
1595           if (cfa.offset == 0)
1596             {
1597               /* If the source register is exactly the CFA, assume
1598                  we're saving SP like any other register; this happens
1599                  on the ARM.  */
1600               def_cfa_1 (label, &cfa);
1601               queue_reg_save (label, stack_pointer_rtx, offset);
1602               break;
1603             }
1604           else
1605             {
1606               /* Otherwise, we'll need to look in the stack to
1607                  calculate the CFA.  */
1608               rtx x = XEXP (dest, 0);
1609
1610               if (GET_CODE (x) != REG)
1611                 x = XEXP (x, 0);
1612               if (GET_CODE (x) != REG)
1613                 abort ();
1614
1615               cfa.reg = REGNO (x);
1616               cfa.base_offset = offset;
1617               cfa.indirect = 1;
1618               def_cfa_1 (label, &cfa);
1619               break;
1620             }
1621         }
1622
1623       def_cfa_1 (label, &cfa);
1624       queue_reg_save (label, src, offset);
1625       break;
1626
1627     default:
1628       abort ();
1629     }
1630 }
1631
1632 /* Record call frame debugging information for INSN, which either
1633    sets SP or FP (adjusting how we calculate the frame address) or saves a
1634    register to the stack.  If INSN is NULL_RTX, initialize our state.  */
1635
1636 void
1637 dwarf2out_frame_debug (insn)
1638      rtx insn;
1639 {
1640   const char *label;
1641   rtx src;
1642
1643   if (insn == NULL_RTX)
1644     {
1645       /* Flush any queued register saves.  */
1646       flush_queued_reg_saves ();
1647
1648       /* Set up state for generating call frame debug info.  */
1649       lookup_cfa (&cfa);
1650       if (cfa.reg != (unsigned long) DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM))
1651         abort ();
1652
1653       cfa.reg = STACK_POINTER_REGNUM;
1654       cfa_store = cfa;
1655       cfa_temp.reg = -1;
1656       cfa_temp.offset = 0;
1657       return;
1658     }
1659
1660   if (GET_CODE (insn) != INSN || clobbers_queued_reg_save (insn))
1661     flush_queued_reg_saves ();
1662
1663   if (! RTX_FRAME_RELATED_P (insn))
1664     {
1665       if (!ACCUMULATE_OUTGOING_ARGS)
1666         dwarf2out_stack_adjust (insn);
1667
1668       return;
1669     }
1670
1671   label = dwarf2out_cfi_label ();
1672   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1673   if (src)
1674     insn = XEXP (src, 0);
1675   else
1676     insn = PATTERN (insn);
1677
1678   dwarf2out_frame_debug_expr (insn, label);
1679 }
1680
1681 /* Output a Call Frame Information opcode and its operand(s).  */
1682
1683 static void
1684 output_cfi (cfi, fde, for_eh)
1685      dw_cfi_ref cfi;
1686      dw_fde_ref fde;
1687      int for_eh;
1688 {
1689   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
1690     dw2_asm_output_data (1, (cfi->dw_cfi_opc
1691                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
1692                          "DW_CFA_advance_loc 0x%lx",
1693                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
1694   else if (cfi->dw_cfi_opc == DW_CFA_offset)
1695     {
1696       dw2_asm_output_data (1, (cfi->dw_cfi_opc
1697                                | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1698                            "DW_CFA_offset, column 0x%lx",
1699                            cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1700       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1701     }
1702   else if (cfi->dw_cfi_opc == DW_CFA_restore)
1703     dw2_asm_output_data (1, (cfi->dw_cfi_opc
1704                              | (cfi->dw_cfi_oprnd1.dw_cfi_reg_num & 0x3f)),
1705                          "DW_CFA_restore, column 0x%lx",
1706                          cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
1707   else
1708     {
1709       dw2_asm_output_data (1, cfi->dw_cfi_opc,
1710                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
1711
1712       switch (cfi->dw_cfi_opc)
1713         {
1714         case DW_CFA_set_loc:
1715           if (for_eh)
1716             dw2_asm_output_encoded_addr_rtx (
1717                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
1718                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
1719                 NULL);
1720           else
1721             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
1722                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
1723           break;
1724
1725         case DW_CFA_advance_loc1:
1726           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1727                                 fde->dw_fde_current_label, NULL);
1728           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1729           break;
1730
1731         case DW_CFA_advance_loc2:
1732           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1733                                 fde->dw_fde_current_label, NULL);
1734           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1735           break;
1736
1737         case DW_CFA_advance_loc4:
1738           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1739                                 fde->dw_fde_current_label, NULL);
1740           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1741           break;
1742
1743         case DW_CFA_MIPS_advance_loc8:
1744           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
1745                                 fde->dw_fde_current_label, NULL);
1746           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
1747           break;
1748
1749         case DW_CFA_offset_extended:
1750         case DW_CFA_def_cfa:
1751           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1752                                        NULL);
1753           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1754           break;
1755
1756         case DW_CFA_offset_extended_sf:
1757         case DW_CFA_def_cfa_sf:
1758           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1759                                        NULL);
1760           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
1761           break;
1762
1763         case DW_CFA_restore_extended:
1764         case DW_CFA_undefined:
1765         case DW_CFA_same_value:
1766         case DW_CFA_def_cfa_register:
1767           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1768                                        NULL);
1769           break;
1770
1771         case DW_CFA_register:
1772           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1773                                        NULL);
1774           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_reg_num,
1775                                        NULL);
1776           break;
1777
1778         case DW_CFA_def_cfa_offset:
1779         case DW_CFA_GNU_args_size:
1780           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1781           break;
1782
1783         case DW_CFA_def_cfa_offset_sf:
1784           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
1785           break;
1786
1787         case DW_CFA_GNU_window_save:
1788           break;
1789
1790         case DW_CFA_def_cfa_expression:
1791         case DW_CFA_expression:
1792           output_cfa_loc (cfi);
1793           break;
1794
1795         case DW_CFA_GNU_negative_offset_extended:
1796           /* Obsoleted by DW_CFA_offset_extended_sf.  */
1797           abort ();
1798
1799         default:
1800           break;
1801         }
1802     }
1803 }
1804
1805 /* Output the call frame information used to used to record information
1806    that relates to calculating the frame pointer, and records the
1807    location of saved registers.  */
1808
1809 static void
1810 output_call_frame_info (for_eh)
1811      int for_eh;
1812 {
1813   unsigned int i;
1814   dw_fde_ref fde;
1815   dw_cfi_ref cfi;
1816   char l1[20], l2[20], section_start_label[20];
1817   int any_lsda_needed = 0;
1818   char augmentation[6];
1819   int augmentation_size;
1820   int fde_encoding = DW_EH_PE_absptr;
1821   int per_encoding = DW_EH_PE_absptr;
1822   int lsda_encoding = DW_EH_PE_absptr;
1823
1824   /* Don't emit a CIE if there won't be any FDEs.  */
1825   if (fde_table_in_use == 0)
1826     return;
1827
1828   /* If we don't have any functions we'll want to unwind out of, don't emit any
1829      EH unwind information.  */
1830   if (for_eh)
1831     {
1832       int any_eh_needed = flag_asynchronous_unwind_tables;
1833
1834       for (i = 0; i < fde_table_in_use; i++)
1835         if (fde_table[i].uses_eh_lsda)
1836           any_eh_needed = any_lsda_needed = 1;
1837         else if (! fde_table[i].nothrow)
1838           any_eh_needed = 1;
1839
1840       if (! any_eh_needed)
1841         return;
1842     }
1843
1844   /* We're going to be generating comments, so turn on app.  */
1845   if (flag_debug_asm)
1846     app_enable ();
1847
1848   if (for_eh)
1849     (*targetm.asm_out.eh_frame_section) ();
1850   else
1851     named_section_flags (DEBUG_FRAME_SECTION, SECTION_DEBUG);
1852
1853   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1854   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1855
1856   /* Output the CIE.  */
1857   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1858   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1859   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1860                         "Length of Common Information Entry");
1861   ASM_OUTPUT_LABEL (asm_out_file, l1);
1862
1863   /* Now that the CIE pointer is PC-relative for EH,
1864      use 0 to identify the CIE.  */
1865   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
1866                        (for_eh ? 0 : DW_CIE_ID),
1867                        "CIE Identifier Tag");
1868
1869   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
1870
1871   augmentation[0] = 0;
1872   augmentation_size = 0;
1873   if (for_eh)
1874     {
1875       char *p;
1876
1877       /* Augmentation:
1878          z      Indicates that a uleb128 is present to size the
1879                 augmentation section.
1880          L      Indicates the encoding (and thus presence) of
1881                 an LSDA pointer in the FDE augmentation.
1882          R      Indicates a non-default pointer encoding for
1883                 FDE code pointers.
1884          P      Indicates the presence of an encoding + language
1885                 personality routine in the CIE augmentation.  */
1886
1887       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
1888       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1889       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1890
1891       p = augmentation + 1;
1892       if (eh_personality_libfunc)
1893         {
1894           *p++ = 'P';
1895           augmentation_size += 1 + size_of_encoded_value (per_encoding);
1896         }
1897       if (any_lsda_needed)
1898         {
1899           *p++ = 'L';
1900           augmentation_size += 1;
1901         }
1902       if (fde_encoding != DW_EH_PE_absptr)
1903         {
1904           *p++ = 'R';
1905           augmentation_size += 1;
1906         }
1907       if (p > augmentation + 1)
1908         {
1909           augmentation[0] = 'z';
1910           *p = '\0';
1911         }
1912
1913       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
1914       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
1915         {
1916           int offset = (  4             /* Length */
1917                         + 4             /* CIE Id */
1918                         + 1             /* CIE version */
1919                         + strlen (augmentation) + 1     /* Augmentation */
1920                         + size_of_uleb128 (1)           /* Code alignment */
1921                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
1922                         + 1             /* RA column */
1923                         + 1             /* Augmentation size */
1924                         + 1             /* Personality encoding */ );
1925           int pad = -offset & (PTR_SIZE - 1);
1926
1927           augmentation_size += pad;
1928
1929           /* Augmentations should be small, so there's scarce need to
1930              iterate for a solution.  Die if we exceed one uleb128 byte.  */
1931           if (size_of_uleb128 (augmentation_size) != 1)
1932             abort ();
1933         }
1934     }
1935
1936   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
1937   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
1938   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
1939                                "CIE Data Alignment Factor");
1940   dw2_asm_output_data (1, DWARF_FRAME_RETURN_COLUMN, "CIE RA Column");
1941
1942   if (augmentation[0])
1943     {
1944       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
1945       if (eh_personality_libfunc)
1946         {
1947           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
1948                                eh_data_format_name (per_encoding));
1949           dw2_asm_output_encoded_addr_rtx (per_encoding,
1950                                            eh_personality_libfunc, NULL);
1951         }
1952
1953       if (any_lsda_needed)
1954         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
1955                              eh_data_format_name (lsda_encoding));
1956
1957       if (fde_encoding != DW_EH_PE_absptr)
1958         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
1959                              eh_data_format_name (fde_encoding));
1960     }
1961
1962   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
1963     output_cfi (cfi, NULL, for_eh);
1964
1965   /* Pad the CIE out to an address sized boundary.  */
1966   ASM_OUTPUT_ALIGN (asm_out_file,
1967                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
1968   ASM_OUTPUT_LABEL (asm_out_file, l2);
1969
1970   /* Loop through all of the FDE's.  */
1971   for (i = 0; i < fde_table_in_use; i++)
1972     {
1973       fde = &fde_table[i];
1974
1975       /* Don't emit EH unwind info for leaf functions that don't need it.  */
1976       if (!flag_asynchronous_unwind_tables && for_eh
1977           && (fde->nothrow || fde->all_throwers_are_sibcalls)
1978           && !fde->uses_eh_lsda)
1979         continue;
1980
1981       (*targetm.asm_out.internal_label) (asm_out_file, FDE_LABEL, for_eh + i * 2);
1982       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
1983       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
1984       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1985                             "FDE Length");
1986       ASM_OUTPUT_LABEL (asm_out_file, l1);
1987
1988       if (for_eh)
1989         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
1990       else
1991         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
1992                                "FDE CIE offset");
1993
1994       if (for_eh)
1995         {
1996           dw2_asm_output_encoded_addr_rtx (fde_encoding,
1997                    gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin),
1998                    "FDE initial location");
1999           dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2000                                 fde->dw_fde_end, fde->dw_fde_begin,
2001                                 "FDE address range");
2002         }
2003       else
2004         {
2005           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2006                                "FDE initial location");
2007           dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2008                                 fde->dw_fde_end, fde->dw_fde_begin,
2009                                 "FDE address range");
2010         }
2011
2012       if (augmentation[0])
2013         {
2014           if (any_lsda_needed)
2015             {
2016               int size = size_of_encoded_value (lsda_encoding);
2017
2018               if (lsda_encoding == DW_EH_PE_aligned)
2019                 {
2020                   int offset = (  4             /* Length */
2021                                 + 4             /* CIE offset */
2022                                 + 2 * size_of_encoded_value (fde_encoding)
2023                                 + 1             /* Augmentation size */ );
2024                   int pad = -offset & (PTR_SIZE - 1);
2025
2026                   size += pad;
2027                   if (size_of_uleb128 (size) != 1)
2028                     abort ();
2029                 }
2030
2031               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2032
2033               if (fde->uses_eh_lsda)
2034                 {
2035                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2036                                                fde->funcdef_number);
2037                   dw2_asm_output_encoded_addr_rtx (
2038                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2039                         "Language Specific Data Area");
2040                 }
2041               else
2042                 {
2043                   if (lsda_encoding == DW_EH_PE_aligned)
2044                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2045                   dw2_asm_output_data
2046                     (size_of_encoded_value (lsda_encoding), 0,
2047                      "Language Specific Data Area (none)");
2048                 }
2049             }
2050           else
2051             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2052         }
2053
2054       /* Loop through the Call Frame Instructions associated with
2055          this FDE.  */
2056       fde->dw_fde_current_label = fde->dw_fde_begin;
2057       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2058         output_cfi (cfi, fde, for_eh);
2059
2060       /* Pad the FDE out to an address sized boundary.  */
2061       ASM_OUTPUT_ALIGN (asm_out_file,
2062                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2063       ASM_OUTPUT_LABEL (asm_out_file, l2);
2064     }
2065
2066   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2067     dw2_asm_output_data (4, 0, "End of Table");
2068 #ifdef MIPS_DEBUGGING_INFO
2069   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2070      get a value of 0.  Putting .align 0 after the label fixes it.  */
2071   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2072 #endif
2073
2074   /* Turn off app to make assembly quicker.  */
2075   if (flag_debug_asm)
2076     app_disable ();
2077 }
2078
2079 /* Output a marker (i.e. a label) for the beginning of a function, before
2080    the prologue.  */
2081
2082 void
2083 dwarf2out_begin_prologue (line, file)
2084      unsigned int line ATTRIBUTE_UNUSED;
2085      const char *file ATTRIBUTE_UNUSED;
2086 {
2087   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2088   dw_fde_ref fde;
2089
2090   current_function_func_begin_label = 0;
2091
2092 #ifdef IA64_UNWIND_INFO
2093   /* ??? current_function_func_begin_label is also used by except.c
2094      for call-site information.  We must emit this label if it might
2095      be used.  */
2096   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2097       && ! dwarf2out_do_frame ())
2098     return;
2099 #else
2100   if (! dwarf2out_do_frame ())
2101     return;
2102 #endif
2103
2104   function_section (current_function_decl);
2105   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2106                                current_function_funcdef_no);
2107   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2108                           current_function_funcdef_no);
2109   current_function_func_begin_label = get_identifier (label);
2110
2111 #ifdef IA64_UNWIND_INFO
2112   /* We can elide the fde allocation if we're not emitting debug info.  */
2113   if (! dwarf2out_do_frame ())
2114     return;
2115 #endif
2116
2117   /* Expand the fde table if necessary.  */
2118   if (fde_table_in_use == fde_table_allocated)
2119     {
2120       fde_table_allocated += FDE_TABLE_INCREMENT;
2121       fde_table
2122         = (dw_fde_ref) xrealloc (fde_table,
2123                                  fde_table_allocated * sizeof (dw_fde_node));
2124     }
2125
2126   /* Record the FDE associated with this function.  */
2127   current_funcdef_fde = fde_table_in_use;
2128
2129   /* Add the new FDE at the end of the fde_table.  */
2130   fde = &fde_table[fde_table_in_use++];
2131   fde->dw_fde_begin = xstrdup (label);
2132   fde->dw_fde_current_label = NULL;
2133   fde->dw_fde_end = NULL;
2134   fde->dw_fde_cfi = NULL;
2135   fde->funcdef_number = current_function_funcdef_no;
2136   fde->nothrow = current_function_nothrow;
2137   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2138   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2139
2140   args_size = old_args_size = 0;
2141
2142   /* We only want to output line number information for the genuine dwarf2
2143      prologue case, not the eh frame case.  */
2144 #ifdef DWARF2_DEBUGGING_INFO
2145   if (file)
2146     dwarf2out_source_line (line, file);
2147 #endif
2148 }
2149
2150 /* Output a marker (i.e. a label) for the absolute end of the generated code
2151    for a function definition.  This gets called *after* the epilogue code has
2152    been generated.  */
2153
2154 void
2155 dwarf2out_end_epilogue (line, file)
2156      unsigned int line ATTRIBUTE_UNUSED;
2157      const char *file ATTRIBUTE_UNUSED;
2158 {
2159   dw_fde_ref fde;
2160   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2161
2162   /* Output a label to mark the endpoint of the code generated for this
2163      function.  */
2164   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2165                                current_function_funcdef_no);
2166   ASM_OUTPUT_LABEL (asm_out_file, label);
2167   fde = &fde_table[fde_table_in_use - 1];
2168   fde->dw_fde_end = xstrdup (label);
2169 }
2170
2171 void
2172 dwarf2out_frame_init ()
2173 {
2174   /* Allocate the initial hunk of the fde_table.  */
2175   fde_table = (dw_fde_ref) xcalloc (FDE_TABLE_INCREMENT, sizeof (dw_fde_node));
2176   fde_table_allocated = FDE_TABLE_INCREMENT;
2177   fde_table_in_use = 0;
2178
2179   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2180      sake of lookup_cfa.  */
2181
2182 #ifdef DWARF2_UNWIND_INFO
2183   /* On entry, the Canonical Frame Address is at SP.  */
2184   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2185   initial_return_save (INCOMING_RETURN_ADDR_RTX);
2186 #endif
2187 }
2188
2189 void
2190 dwarf2out_frame_finish ()
2191 {
2192   /* Output call frame information.  */
2193   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
2194     output_call_frame_info (0);
2195
2196   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2197     output_call_frame_info (1);
2198 }
2199 \f
2200 /* And now, the subset of the debugging information support code necessary
2201    for emitting location expressions.  */
2202
2203 /* We need some way to distinguish DW_OP_addr with a direct symbol
2204    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2205 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2206
2207
2208 typedef struct dw_val_struct *dw_val_ref;
2209 typedef struct die_struct *dw_die_ref;
2210 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2211 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2212
2213 /* Each DIE may have a series of attribute/value pairs.  Values
2214    can take on several forms.  The forms that are used in this
2215    implementation are listed below.  */
2216
2217 typedef enum
2218 {
2219   dw_val_class_addr,
2220   dw_val_class_offset,
2221   dw_val_class_loc,
2222   dw_val_class_loc_list,
2223   dw_val_class_range_list,
2224   dw_val_class_const,
2225   dw_val_class_unsigned_const,
2226   dw_val_class_long_long,
2227   dw_val_class_float,
2228   dw_val_class_flag,
2229   dw_val_class_die_ref,
2230   dw_val_class_fde_ref,
2231   dw_val_class_lbl_id,
2232   dw_val_class_lbl_offset,
2233   dw_val_class_str
2234 }
2235 dw_val_class;
2236
2237 /* Describe a double word constant value.  */
2238 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2239
2240 typedef struct dw_long_long_struct
2241 {
2242   unsigned long hi;
2243   unsigned long low;
2244 }
2245 dw_long_long_const;
2246
2247 /* Describe a floating point constant value.  */
2248
2249 typedef struct dw_fp_struct
2250 {
2251   long *array;
2252   unsigned length;
2253 }
2254 dw_float_const;
2255
2256 /* The dw_val_node describes an attribute's value, as it is
2257    represented internally.  */
2258
2259 typedef struct dw_val_struct
2260 {
2261   dw_val_class val_class;
2262   union
2263     {
2264       rtx val_addr;
2265       long unsigned val_offset;
2266       dw_loc_list_ref  val_loc_list;
2267       dw_loc_descr_ref val_loc;
2268       long int val_int;
2269       long unsigned val_unsigned;
2270       dw_long_long_const val_long_long;
2271       dw_float_const val_float;
2272       struct
2273         {
2274           dw_die_ref die;
2275           int external;
2276         } val_die_ref;
2277       unsigned val_fde_index;
2278       struct indirect_string_node *val_str;
2279       char *val_lbl_id;
2280       unsigned char val_flag;
2281     }
2282   v;
2283 }
2284 dw_val_node;
2285
2286 /* Locations in memory are described using a sequence of stack machine
2287    operations.  */
2288
2289 typedef struct dw_loc_descr_struct
2290 {
2291   dw_loc_descr_ref dw_loc_next;
2292   enum dwarf_location_atom dw_loc_opc;
2293   dw_val_node dw_loc_oprnd1;
2294   dw_val_node dw_loc_oprnd2;
2295   int dw_loc_addr;
2296 }
2297 dw_loc_descr_node;
2298
2299 /* Location lists are ranges + location descriptions for that range,
2300    so you can track variables that are in different places over
2301    their entire life.  */
2302 typedef struct dw_loc_list_struct
2303 {
2304   dw_loc_list_ref dw_loc_next;
2305   const char *begin; /* Label for begin address of range */
2306   const char *end;  /* Label for end address of range */
2307   char *ll_symbol; /* Label for beginning of location list.
2308                       Only on head of list */
2309   const char *section; /* Section this loclist is relative to */
2310   dw_loc_descr_ref expr;
2311 } dw_loc_list_node;
2312
2313 static const char *dwarf_stack_op_name  PARAMS ((unsigned));
2314 static dw_loc_descr_ref new_loc_descr   PARAMS ((enum dwarf_location_atom,
2315                                                  unsigned long,
2316                                                  unsigned long));
2317 static void add_loc_descr               PARAMS ((dw_loc_descr_ref *,
2318                                                  dw_loc_descr_ref));
2319 static unsigned long size_of_loc_descr  PARAMS ((dw_loc_descr_ref));
2320 static unsigned long size_of_locs       PARAMS ((dw_loc_descr_ref));
2321 static void output_loc_operands         PARAMS ((dw_loc_descr_ref));
2322 static void output_loc_sequence         PARAMS ((dw_loc_descr_ref));
2323
2324 /* Convert a DWARF stack opcode into its string name.  */
2325
2326 static const char *
2327 dwarf_stack_op_name (op)
2328      unsigned op;
2329 {
2330   switch (op)
2331     {
2332     case DW_OP_addr:
2333     case INTERNAL_DW_OP_tls_addr:
2334       return "DW_OP_addr";
2335     case DW_OP_deref:
2336       return "DW_OP_deref";
2337     case DW_OP_const1u:
2338       return "DW_OP_const1u";
2339     case DW_OP_const1s:
2340       return "DW_OP_const1s";
2341     case DW_OP_const2u:
2342       return "DW_OP_const2u";
2343     case DW_OP_const2s:
2344       return "DW_OP_const2s";
2345     case DW_OP_const4u:
2346       return "DW_OP_const4u";
2347     case DW_OP_const4s:
2348       return "DW_OP_const4s";
2349     case DW_OP_const8u:
2350       return "DW_OP_const8u";
2351     case DW_OP_const8s:
2352       return "DW_OP_const8s";
2353     case DW_OP_constu:
2354       return "DW_OP_constu";
2355     case DW_OP_consts:
2356       return "DW_OP_consts";
2357     case DW_OP_dup:
2358       return "DW_OP_dup";
2359     case DW_OP_drop:
2360       return "DW_OP_drop";
2361     case DW_OP_over:
2362       return "DW_OP_over";
2363     case DW_OP_pick:
2364       return "DW_OP_pick";
2365     case DW_OP_swap:
2366       return "DW_OP_swap";
2367     case DW_OP_rot:
2368       return "DW_OP_rot";
2369     case DW_OP_xderef:
2370       return "DW_OP_xderef";
2371     case DW_OP_abs:
2372       return "DW_OP_abs";
2373     case DW_OP_and:
2374       return "DW_OP_and";
2375     case DW_OP_div:
2376       return "DW_OP_div";
2377     case DW_OP_minus:
2378       return "DW_OP_minus";
2379     case DW_OP_mod:
2380       return "DW_OP_mod";
2381     case DW_OP_mul:
2382       return "DW_OP_mul";
2383     case DW_OP_neg:
2384       return "DW_OP_neg";
2385     case DW_OP_not:
2386       return "DW_OP_not";
2387     case DW_OP_or:
2388       return "DW_OP_or";
2389     case DW_OP_plus:
2390       return "DW_OP_plus";
2391     case DW_OP_plus_uconst:
2392       return "DW_OP_plus_uconst";
2393     case DW_OP_shl:
2394       return "DW_OP_shl";
2395     case DW_OP_shr:
2396       return "DW_OP_shr";
2397     case DW_OP_shra:
2398       return "DW_OP_shra";
2399     case DW_OP_xor:
2400       return "DW_OP_xor";
2401     case DW_OP_bra:
2402       return "DW_OP_bra";
2403     case DW_OP_eq:
2404       return "DW_OP_eq";
2405     case DW_OP_ge:
2406       return "DW_OP_ge";
2407     case DW_OP_gt:
2408       return "DW_OP_gt";
2409     case DW_OP_le:
2410       return "DW_OP_le";
2411     case DW_OP_lt:
2412       return "DW_OP_lt";
2413     case DW_OP_ne:
2414       return "DW_OP_ne";
2415     case DW_OP_skip:
2416       return "DW_OP_skip";
2417     case DW_OP_lit0:
2418       return "DW_OP_lit0";
2419     case DW_OP_lit1:
2420       return "DW_OP_lit1";
2421     case DW_OP_lit2:
2422       return "DW_OP_lit2";
2423     case DW_OP_lit3:
2424       return "DW_OP_lit3";
2425     case DW_OP_lit4:
2426       return "DW_OP_lit4";
2427     case DW_OP_lit5:
2428       return "DW_OP_lit5";
2429     case DW_OP_lit6:
2430       return "DW_OP_lit6";
2431     case DW_OP_lit7:
2432       return "DW_OP_lit7";
2433     case DW_OP_lit8:
2434       return "DW_OP_lit8";
2435     case DW_OP_lit9:
2436       return "DW_OP_lit9";
2437     case DW_OP_lit10:
2438       return "DW_OP_lit10";
2439     case DW_OP_lit11:
2440       return "DW_OP_lit11";
2441     case DW_OP_lit12:
2442       return "DW_OP_lit12";
2443     case DW_OP_lit13:
2444       return "DW_OP_lit13";
2445     case DW_OP_lit14:
2446       return "DW_OP_lit14";
2447     case DW_OP_lit15:
2448       return "DW_OP_lit15";
2449     case DW_OP_lit16:
2450       return "DW_OP_lit16";
2451     case DW_OP_lit17:
2452       return "DW_OP_lit17";
2453     case DW_OP_lit18:
2454       return "DW_OP_lit18";
2455     case DW_OP_lit19:
2456       return "DW_OP_lit19";
2457     case DW_OP_lit20:
2458       return "DW_OP_lit20";
2459     case DW_OP_lit21:
2460       return "DW_OP_lit21";
2461     case DW_OP_lit22:
2462       return "DW_OP_lit22";
2463     case DW_OP_lit23:
2464       return "DW_OP_lit23";
2465     case DW_OP_lit24:
2466       return "DW_OP_lit24";
2467     case DW_OP_lit25:
2468       return "DW_OP_lit25";
2469     case DW_OP_lit26:
2470       return "DW_OP_lit26";
2471     case DW_OP_lit27:
2472       return "DW_OP_lit27";
2473     case DW_OP_lit28:
2474       return "DW_OP_lit28";
2475     case DW_OP_lit29:
2476       return "DW_OP_lit29";
2477     case DW_OP_lit30:
2478       return "DW_OP_lit30";
2479     case DW_OP_lit31:
2480       return "DW_OP_lit31";
2481     case DW_OP_reg0:
2482       return "DW_OP_reg0";
2483     case DW_OP_reg1:
2484       return "DW_OP_reg1";
2485     case DW_OP_reg2:
2486       return "DW_OP_reg2";
2487     case DW_OP_reg3:
2488       return "DW_OP_reg3";
2489     case DW_OP_reg4:
2490       return "DW_OP_reg4";
2491     case DW_OP_reg5:
2492       return "DW_OP_reg5";
2493     case DW_OP_reg6:
2494       return "DW_OP_reg6";
2495     case DW_OP_reg7:
2496       return "DW_OP_reg7";
2497     case DW_OP_reg8:
2498       return "DW_OP_reg8";
2499     case DW_OP_reg9:
2500       return "DW_OP_reg9";
2501     case DW_OP_reg10:
2502       return "DW_OP_reg10";
2503     case DW_OP_reg11:
2504       return "DW_OP_reg11";
2505     case DW_OP_reg12:
2506       return "DW_OP_reg12";
2507     case DW_OP_reg13:
2508       return "DW_OP_reg13";
2509     case DW_OP_reg14:
2510       return "DW_OP_reg14";
2511     case DW_OP_reg15:
2512       return "DW_OP_reg15";
2513     case DW_OP_reg16:
2514       return "DW_OP_reg16";
2515     case DW_OP_reg17:
2516       return "DW_OP_reg17";
2517     case DW_OP_reg18:
2518       return "DW_OP_reg18";
2519     case DW_OP_reg19:
2520       return "DW_OP_reg19";
2521     case DW_OP_reg20:
2522       return "DW_OP_reg20";
2523     case DW_OP_reg21:
2524       return "DW_OP_reg21";
2525     case DW_OP_reg22:
2526       return "DW_OP_reg22";
2527     case DW_OP_reg23:
2528       return "DW_OP_reg23";
2529     case DW_OP_reg24:
2530       return "DW_OP_reg24";
2531     case DW_OP_reg25:
2532       return "DW_OP_reg25";
2533     case DW_OP_reg26:
2534       return "DW_OP_reg26";
2535     case DW_OP_reg27:
2536       return "DW_OP_reg27";
2537     case DW_OP_reg28:
2538       return "DW_OP_reg28";
2539     case DW_OP_reg29:
2540       return "DW_OP_reg29";
2541     case DW_OP_reg30:
2542       return "DW_OP_reg30";
2543     case DW_OP_reg31:
2544       return "DW_OP_reg31";
2545     case DW_OP_breg0:
2546       return "DW_OP_breg0";
2547     case DW_OP_breg1:
2548       return "DW_OP_breg1";
2549     case DW_OP_breg2:
2550       return "DW_OP_breg2";
2551     case DW_OP_breg3:
2552       return "DW_OP_breg3";
2553     case DW_OP_breg4:
2554       return "DW_OP_breg4";
2555     case DW_OP_breg5:
2556       return "DW_OP_breg5";
2557     case DW_OP_breg6:
2558       return "DW_OP_breg6";
2559     case DW_OP_breg7:
2560       return "DW_OP_breg7";
2561     case DW_OP_breg8:
2562       return "DW_OP_breg8";
2563     case DW_OP_breg9:
2564       return "DW_OP_breg9";
2565     case DW_OP_breg10:
2566       return "DW_OP_breg10";
2567     case DW_OP_breg11:
2568       return "DW_OP_breg11";
2569     case DW_OP_breg12:
2570       return "DW_OP_breg12";
2571     case DW_OP_breg13:
2572       return "DW_OP_breg13";
2573     case DW_OP_breg14:
2574       return "DW_OP_breg14";
2575     case DW_OP_breg15:
2576       return "DW_OP_breg15";
2577     case DW_OP_breg16:
2578       return "DW_OP_breg16";
2579     case DW_OP_breg17:
2580       return "DW_OP_breg17";
2581     case DW_OP_breg18:
2582       return "DW_OP_breg18";
2583     case DW_OP_breg19:
2584       return "DW_OP_breg19";
2585     case DW_OP_breg20:
2586       return "DW_OP_breg20";
2587     case DW_OP_breg21:
2588       return "DW_OP_breg21";
2589     case DW_OP_breg22:
2590       return "DW_OP_breg22";
2591     case DW_OP_breg23:
2592       return "DW_OP_breg23";
2593     case DW_OP_breg24:
2594       return "DW_OP_breg24";
2595     case DW_OP_breg25:
2596       return "DW_OP_breg25";
2597     case DW_OP_breg26:
2598       return "DW_OP_breg26";
2599     case DW_OP_breg27:
2600       return "DW_OP_breg27";
2601     case DW_OP_breg28:
2602       return "DW_OP_breg28";
2603     case DW_OP_breg29:
2604       return "DW_OP_breg29";
2605     case DW_OP_breg30:
2606       return "DW_OP_breg30";
2607     case DW_OP_breg31:
2608       return "DW_OP_breg31";
2609     case DW_OP_regx:
2610       return "DW_OP_regx";
2611     case DW_OP_fbreg:
2612       return "DW_OP_fbreg";
2613     case DW_OP_bregx:
2614       return "DW_OP_bregx";
2615     case DW_OP_piece:
2616       return "DW_OP_piece";
2617     case DW_OP_deref_size:
2618       return "DW_OP_deref_size";
2619     case DW_OP_xderef_size:
2620       return "DW_OP_xderef_size";
2621     case DW_OP_nop:
2622       return "DW_OP_nop";
2623     case DW_OP_push_object_address:
2624       return "DW_OP_push_object_address";
2625     case DW_OP_call2:
2626       return "DW_OP_call2";
2627     case DW_OP_call4:
2628       return "DW_OP_call4";
2629     case DW_OP_call_ref:
2630       return "DW_OP_call_ref";
2631     case DW_OP_GNU_push_tls_address:
2632       return "DW_OP_GNU_push_tls_address";
2633     default:
2634       return "OP_<unknown>";
2635     }
2636 }
2637
2638 /* Return a pointer to a newly allocated location description.  Location
2639    descriptions are simple expression terms that can be strung
2640    together to form more complicated location (address) descriptions.  */
2641
2642 static inline dw_loc_descr_ref
2643 new_loc_descr (op, oprnd1, oprnd2)
2644      enum dwarf_location_atom op;
2645      unsigned long oprnd1;
2646      unsigned long oprnd2;
2647 {
2648   /* Use xcalloc here so we clear out all of the long_long constant in
2649      the union.  */
2650   dw_loc_descr_ref descr
2651     = (dw_loc_descr_ref) xcalloc (1, sizeof (dw_loc_descr_node));
2652
2653   descr->dw_loc_opc = op;
2654   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2655   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2656   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2657   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2658
2659   return descr;
2660 }
2661
2662
2663 /* Add a location description term to a location description expression.  */
2664
2665 static inline void
2666 add_loc_descr (list_head, descr)
2667      dw_loc_descr_ref *list_head;
2668      dw_loc_descr_ref descr;
2669 {
2670   dw_loc_descr_ref *d;
2671
2672   /* Find the end of the chain.  */
2673   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2674     ;
2675
2676   *d = descr;
2677 }
2678
2679 /* Return the size of a location descriptor.  */
2680
2681 static unsigned long
2682 size_of_loc_descr (loc)
2683      dw_loc_descr_ref loc;
2684 {
2685   unsigned long size = 1;
2686
2687   switch (loc->dw_loc_opc)
2688     {
2689     case DW_OP_addr:
2690     case INTERNAL_DW_OP_tls_addr:
2691       size += DWARF2_ADDR_SIZE;
2692       break;
2693     case DW_OP_const1u:
2694     case DW_OP_const1s:
2695       size += 1;
2696       break;
2697     case DW_OP_const2u:
2698     case DW_OP_const2s:
2699       size += 2;
2700       break;
2701     case DW_OP_const4u:
2702     case DW_OP_const4s:
2703       size += 4;
2704       break;
2705     case DW_OP_const8u:
2706     case DW_OP_const8s:
2707       size += 8;
2708       break;
2709     case DW_OP_constu:
2710       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2711       break;
2712     case DW_OP_consts:
2713       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2714       break;
2715     case DW_OP_pick:
2716       size += 1;
2717       break;
2718     case DW_OP_plus_uconst:
2719       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2720       break;
2721     case DW_OP_skip:
2722     case DW_OP_bra:
2723       size += 2;
2724       break;
2725     case DW_OP_breg0:
2726     case DW_OP_breg1:
2727     case DW_OP_breg2:
2728     case DW_OP_breg3:
2729     case DW_OP_breg4:
2730     case DW_OP_breg5:
2731     case DW_OP_breg6:
2732     case DW_OP_breg7:
2733     case DW_OP_breg8:
2734     case DW_OP_breg9:
2735     case DW_OP_breg10:
2736     case DW_OP_breg11:
2737     case DW_OP_breg12:
2738     case DW_OP_breg13:
2739     case DW_OP_breg14:
2740     case DW_OP_breg15:
2741     case DW_OP_breg16:
2742     case DW_OP_breg17:
2743     case DW_OP_breg18:
2744     case DW_OP_breg19:
2745     case DW_OP_breg20:
2746     case DW_OP_breg21:
2747     case DW_OP_breg22:
2748     case DW_OP_breg23:
2749     case DW_OP_breg24:
2750     case DW_OP_breg25:
2751     case DW_OP_breg26:
2752     case DW_OP_breg27:
2753     case DW_OP_breg28:
2754     case DW_OP_breg29:
2755     case DW_OP_breg30:
2756     case DW_OP_breg31:
2757       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2758       break;
2759     case DW_OP_regx:
2760       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2761       break;
2762     case DW_OP_fbreg:
2763       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2764       break;
2765     case DW_OP_bregx:
2766       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2767       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2768       break;
2769     case DW_OP_piece:
2770       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2771       break;
2772     case DW_OP_deref_size:
2773     case DW_OP_xderef_size:
2774       size += 1;
2775       break;
2776     case DW_OP_call2:
2777       size += 2;
2778       break;
2779     case DW_OP_call4:
2780       size += 4;
2781       break;
2782     case DW_OP_call_ref:
2783       size += DWARF2_ADDR_SIZE;
2784       break;
2785     default:
2786       break;
2787     }
2788
2789   return size;
2790 }
2791
2792 /* Return the size of a series of location descriptors.  */
2793
2794 static unsigned long
2795 size_of_locs (loc)
2796      dw_loc_descr_ref loc;
2797 {
2798   unsigned long size;
2799
2800   for (size = 0; loc != NULL; loc = loc->dw_loc_next)
2801     {
2802       loc->dw_loc_addr = size;
2803       size += size_of_loc_descr (loc);
2804     }
2805
2806   return size;
2807 }
2808
2809 /* Output location description stack opcode's operands (if any).  */
2810
2811 static void
2812 output_loc_operands (loc)
2813      dw_loc_descr_ref loc;
2814 {
2815   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2816   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2817
2818   switch (loc->dw_loc_opc)
2819     {
2820 #ifdef DWARF2_DEBUGGING_INFO
2821     case DW_OP_addr:
2822       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2823       break;
2824     case DW_OP_const2u:
2825     case DW_OP_const2s:
2826       dw2_asm_output_data (2, val1->v.val_int, NULL);
2827       break;
2828     case DW_OP_const4u:
2829     case DW_OP_const4s:
2830       dw2_asm_output_data (4, val1->v.val_int, NULL);
2831       break;
2832     case DW_OP_const8u:
2833     case DW_OP_const8s:
2834       if (HOST_BITS_PER_LONG < 64)
2835         abort ();
2836       dw2_asm_output_data (8, val1->v.val_int, NULL);
2837       break;
2838     case DW_OP_skip:
2839     case DW_OP_bra:
2840       {
2841         int offset;
2842
2843         if (val1->val_class == dw_val_class_loc)
2844           offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2845         else
2846           abort ();
2847
2848         dw2_asm_output_data (2, offset, NULL);
2849       }
2850       break;
2851 #else
2852     case DW_OP_addr:
2853     case DW_OP_const2u:
2854     case DW_OP_const2s:
2855     case DW_OP_const4u:
2856     case DW_OP_const4s:
2857     case DW_OP_const8u:
2858     case DW_OP_const8s:
2859     case DW_OP_skip:
2860     case DW_OP_bra:
2861       /* We currently don't make any attempt to make sure these are
2862          aligned properly like we do for the main unwind info, so
2863          don't support emitting things larger than a byte if we're
2864          only doing unwinding.  */
2865       abort ();
2866 #endif
2867     case DW_OP_const1u:
2868     case DW_OP_const1s:
2869       dw2_asm_output_data (1, val1->v.val_int, NULL);
2870       break;
2871     case DW_OP_constu:
2872       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2873       break;
2874     case DW_OP_consts:
2875       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2876       break;
2877     case DW_OP_pick:
2878       dw2_asm_output_data (1, val1->v.val_int, NULL);
2879       break;
2880     case DW_OP_plus_uconst:
2881       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2882       break;
2883     case DW_OP_breg0:
2884     case DW_OP_breg1:
2885     case DW_OP_breg2:
2886     case DW_OP_breg3:
2887     case DW_OP_breg4:
2888     case DW_OP_breg5:
2889     case DW_OP_breg6:
2890     case DW_OP_breg7:
2891     case DW_OP_breg8:
2892     case DW_OP_breg9:
2893     case DW_OP_breg10:
2894     case DW_OP_breg11:
2895     case DW_OP_breg12:
2896     case DW_OP_breg13:
2897     case DW_OP_breg14:
2898     case DW_OP_breg15:
2899     case DW_OP_breg16:
2900     case DW_OP_breg17:
2901     case DW_OP_breg18:
2902     case DW_OP_breg19:
2903     case DW_OP_breg20:
2904     case DW_OP_breg21:
2905     case DW_OP_breg22:
2906     case DW_OP_breg23:
2907     case DW_OP_breg24:
2908     case DW_OP_breg25:
2909     case DW_OP_breg26:
2910     case DW_OP_breg27:
2911     case DW_OP_breg28:
2912     case DW_OP_breg29:
2913     case DW_OP_breg30:
2914     case DW_OP_breg31:
2915       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2916       break;
2917     case DW_OP_regx:
2918       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2919       break;
2920     case DW_OP_fbreg:
2921       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2922       break;
2923     case DW_OP_bregx:
2924       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2925       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2926       break;
2927     case DW_OP_piece:
2928       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2929       break;
2930     case DW_OP_deref_size:
2931     case DW_OP_xderef_size:
2932       dw2_asm_output_data (1, val1->v.val_int, NULL);
2933       break;
2934
2935     case INTERNAL_DW_OP_tls_addr:
2936 #ifdef ASM_OUTPUT_DWARF_DTPREL
2937       ASM_OUTPUT_DWARF_DTPREL (asm_out_file, DWARF2_ADDR_SIZE,
2938                                val1->v.val_addr);
2939       fputc ('\n', asm_out_file);
2940 #else
2941       abort ();
2942 #endif
2943       break;
2944
2945     default:
2946       /* Other codes have no operands.  */
2947       break;
2948     }
2949 }
2950
2951 /* Output a sequence of location operations.  */
2952
2953 static void
2954 output_loc_sequence (loc)
2955      dw_loc_descr_ref loc;
2956 {
2957   for (; loc != NULL; loc = loc->dw_loc_next)
2958     {
2959       /* Output the opcode.  */
2960       dw2_asm_output_data (1, loc->dw_loc_opc,
2961                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
2962
2963       /* Output the operand(s) (if any).  */
2964       output_loc_operands (loc);
2965     }
2966 }
2967
2968 /* This routine will generate the correct assembly data for a location
2969    description based on a cfi entry with a complex address.  */
2970
2971 static void
2972 output_cfa_loc (cfi)
2973      dw_cfi_ref cfi;
2974 {
2975   dw_loc_descr_ref loc;
2976   unsigned long size;
2977
2978   /* Output the size of the block.  */
2979   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
2980   size = size_of_locs (loc);
2981   dw2_asm_output_data_uleb128 (size, NULL);
2982
2983   /* Now output the operations themselves.  */
2984   output_loc_sequence (loc);
2985 }
2986
2987 /* This function builds a dwarf location descriptor sequence from
2988    a dw_cfa_location.  */
2989
2990 static struct dw_loc_descr_struct *
2991 build_cfa_loc (cfa)
2992      dw_cfa_location *cfa;
2993 {
2994   struct dw_loc_descr_struct *head, *tmp;
2995
2996   if (cfa->indirect == 0)
2997     abort ();
2998
2999   if (cfa->base_offset)
3000     {
3001       if (cfa->reg <= 31)
3002         head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3003       else
3004         head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3005     }
3006   else if (cfa->reg <= 31)
3007     head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3008   else
3009     head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3010
3011   head->dw_loc_oprnd1.val_class = dw_val_class_const;
3012   tmp = new_loc_descr (DW_OP_deref, 0, 0);
3013   add_loc_descr (&head, tmp);
3014   if (cfa->offset != 0)
3015     {
3016       tmp = new_loc_descr (DW_OP_plus_uconst, cfa->offset, 0);
3017       add_loc_descr (&head, tmp);
3018     }
3019
3020   return head;
3021 }
3022
3023 /* This function fills in aa dw_cfa_location structure from a dwarf location
3024    descriptor sequence.  */
3025
3026 static void
3027 get_cfa_from_loc_descr (cfa, loc)
3028      dw_cfa_location *cfa;
3029      struct dw_loc_descr_struct *loc;
3030 {
3031   struct dw_loc_descr_struct *ptr;
3032   cfa->offset = 0;
3033   cfa->base_offset = 0;
3034   cfa->indirect = 0;
3035   cfa->reg = -1;
3036
3037   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3038     {
3039       enum dwarf_location_atom op = ptr->dw_loc_opc;
3040
3041       switch (op)
3042         {
3043         case DW_OP_reg0:
3044         case DW_OP_reg1:
3045         case DW_OP_reg2:
3046         case DW_OP_reg3:
3047         case DW_OP_reg4:
3048         case DW_OP_reg5:
3049         case DW_OP_reg6:
3050         case DW_OP_reg7:
3051         case DW_OP_reg8:
3052         case DW_OP_reg9:
3053         case DW_OP_reg10:
3054         case DW_OP_reg11:
3055         case DW_OP_reg12:
3056         case DW_OP_reg13:
3057         case DW_OP_reg14:
3058         case DW_OP_reg15:
3059         case DW_OP_reg16:
3060         case DW_OP_reg17:
3061         case DW_OP_reg18:
3062         case DW_OP_reg19:
3063         case DW_OP_reg20:
3064         case DW_OP_reg21:
3065         case DW_OP_reg22:
3066         case DW_OP_reg23:
3067         case DW_OP_reg24:
3068         case DW_OP_reg25:
3069         case DW_OP_reg26:
3070         case DW_OP_reg27:
3071         case DW_OP_reg28:
3072         case DW_OP_reg29:
3073         case DW_OP_reg30:
3074         case DW_OP_reg31:
3075           cfa->reg = op - DW_OP_reg0;
3076           break;
3077         case DW_OP_regx:
3078           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3079           break;
3080         case DW_OP_breg0:
3081         case DW_OP_breg1:
3082         case DW_OP_breg2:
3083         case DW_OP_breg3:
3084         case DW_OP_breg4:
3085         case DW_OP_breg5:
3086         case DW_OP_breg6:
3087         case DW_OP_breg7:
3088         case DW_OP_breg8:
3089         case DW_OP_breg9:
3090         case DW_OP_breg10:
3091         case DW_OP_breg11:
3092         case DW_OP_breg12:
3093         case DW_OP_breg13:
3094         case DW_OP_breg14:
3095         case DW_OP_breg15:
3096         case DW_OP_breg16:
3097         case DW_OP_breg17:
3098         case DW_OP_breg18:
3099         case DW_OP_breg19:
3100         case DW_OP_breg20:
3101         case DW_OP_breg21:
3102         case DW_OP_breg22:
3103         case DW_OP_breg23:
3104         case DW_OP_breg24:
3105         case DW_OP_breg25:
3106         case DW_OP_breg26:
3107         case DW_OP_breg27:
3108         case DW_OP_breg28:
3109         case DW_OP_breg29:
3110         case DW_OP_breg30:
3111         case DW_OP_breg31:
3112           cfa->reg = op - DW_OP_breg0;
3113           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3114           break;
3115         case DW_OP_bregx:
3116           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3117           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3118           break;
3119         case DW_OP_deref:
3120           cfa->indirect = 1;
3121           break;
3122         case DW_OP_plus_uconst:
3123           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3124           break;
3125         default:
3126           internal_error ("DW_LOC_OP %s not implemented\n",
3127                           dwarf_stack_op_name (ptr->dw_loc_opc));
3128         }
3129     }
3130 }
3131 #endif /* .debug_frame support */
3132 \f
3133 /* And now, the support for symbolic debugging information.  */
3134 #ifdef DWARF2_DEBUGGING_INFO
3135
3136 /* .debug_str support.  */
3137 static hashnode indirect_string_alloc   PARAMS ((hash_table *));
3138 static int output_indirect_string       PARAMS ((struct cpp_reader *,
3139                                                  hashnode, const PTR));
3140
3141
3142 static void dwarf2out_init              PARAMS ((const char *));
3143 static void dwarf2out_finish            PARAMS ((const char *));
3144 static void dwarf2out_define            PARAMS ((unsigned int, const char *));
3145 static void dwarf2out_undef             PARAMS ((unsigned int, const char *));
3146 static void dwarf2out_start_source_file PARAMS ((unsigned, const char *));
3147 static void dwarf2out_end_source_file   PARAMS ((unsigned));
3148 static void dwarf2out_begin_block       PARAMS ((unsigned, unsigned));
3149 static void dwarf2out_end_block         PARAMS ((unsigned, unsigned));
3150 static bool dwarf2out_ignore_block      PARAMS ((tree));
3151 static void dwarf2out_global_decl       PARAMS ((tree));
3152 static void dwarf2out_abstract_function PARAMS ((tree));
3153
3154 /* The debug hooks structure.  */
3155
3156 const struct gcc_debug_hooks dwarf2_debug_hooks =
3157 {
3158   dwarf2out_init,
3159   dwarf2out_finish,
3160   dwarf2out_define,
3161   dwarf2out_undef,
3162   dwarf2out_start_source_file,
3163   dwarf2out_end_source_file,
3164   dwarf2out_begin_block,
3165   dwarf2out_end_block,
3166   dwarf2out_ignore_block,
3167   dwarf2out_source_line,
3168   dwarf2out_begin_prologue,
3169   debug_nothing_int_charstar,   /* end_prologue */
3170   dwarf2out_end_epilogue,
3171   debug_nothing_tree,           /* begin_function */
3172   debug_nothing_int,            /* end_function */
3173   dwarf2out_decl,               /* function_decl */
3174   dwarf2out_global_decl,
3175   debug_nothing_tree,           /* deferred_inline_function */
3176   /* The DWARF 2 backend tries to reduce debugging bloat by not
3177      emitting the abstract description of inline functions until
3178      something tries to reference them.  */
3179   dwarf2out_abstract_function,  /* outlining_inline_function */
3180   debug_nothing_rtx             /* label */
3181 };
3182 \f
3183 /* NOTE: In the comments in this file, many references are made to
3184    "Debugging Information Entries".  This term is abbreviated as `DIE'
3185    throughout the remainder of this file.  */
3186
3187 /* An internal representation of the DWARF output is built, and then
3188    walked to generate the DWARF debugging info.  The walk of the internal
3189    representation is done after the entire program has been compiled.
3190    The types below are used to describe the internal representation.  */
3191
3192 /* Various DIE's use offsets relative to the beginning of the
3193    .debug_info section to refer to each other.  */
3194
3195 typedef long int dw_offset;
3196
3197 /* Define typedefs here to avoid circular dependencies.  */
3198
3199 typedef struct dw_attr_struct *dw_attr_ref;
3200 typedef struct dw_line_info_struct *dw_line_info_ref;
3201 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3202 typedef struct pubname_struct *pubname_ref;
3203 typedef struct dw_ranges_struct *dw_ranges_ref;
3204
3205 /* Each entry in the line_info_table maintains the file and
3206    line number associated with the label generated for that
3207    entry.  The label gives the PC value associated with
3208    the line number entry.  */
3209
3210 typedef struct dw_line_info_struct
3211 {
3212   unsigned long dw_file_num;
3213   unsigned long dw_line_num;
3214 }
3215 dw_line_info_entry;
3216
3217 /* Line information for functions in separate sections; each one gets its
3218    own sequence.  */
3219 typedef struct dw_separate_line_info_struct
3220 {
3221   unsigned long dw_file_num;
3222   unsigned long dw_line_num;
3223   unsigned long function;
3224 }
3225 dw_separate_line_info_entry;
3226
3227 /* Each DIE attribute has a field specifying the attribute kind,
3228    a link to the next attribute in the chain, and an attribute value.
3229    Attributes are typically linked below the DIE they modify.  */
3230
3231 typedef struct dw_attr_struct
3232 {
3233   enum dwarf_attribute dw_attr;
3234   dw_attr_ref dw_attr_next;
3235   dw_val_node dw_attr_val;
3236 }
3237 dw_attr_node;
3238
3239 /* The Debugging Information Entry (DIE) structure */
3240
3241 typedef struct die_struct
3242 {
3243   enum dwarf_tag die_tag;
3244   char *die_symbol;
3245   dw_attr_ref die_attr;
3246   dw_die_ref die_parent;
3247   dw_die_ref die_child;
3248   dw_die_ref die_sib;
3249   dw_offset die_offset;
3250   unsigned long die_abbrev;
3251   int die_mark;
3252 }
3253 die_node;
3254
3255 /* The pubname structure */
3256
3257 typedef struct pubname_struct
3258 {
3259   dw_die_ref die;
3260   char *name;
3261 }
3262 pubname_entry;
3263
3264 struct dw_ranges_struct
3265 {
3266   int block_num;
3267 };
3268
3269 /* The limbo die list structure.  */
3270 typedef struct limbo_die_struct
3271 {
3272   dw_die_ref die;
3273   tree created_for;
3274   struct limbo_die_struct *next;
3275 }
3276 limbo_die_node;
3277
3278 /* How to start an assembler comment.  */
3279 #ifndef ASM_COMMENT_START
3280 #define ASM_COMMENT_START ";#"
3281 #endif
3282
3283 /* Define a macro which returns nonzero for a TYPE_DECL which was
3284    implicitly generated for a tagged type.
3285
3286    Note that unlike the gcc front end (which generates a NULL named
3287    TYPE_DECL node for each complete tagged type, each array type, and
3288    each function type node created) the g++ front end generates a
3289    _named_ TYPE_DECL node for each tagged type node created.
3290    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3291    generate a DW_TAG_typedef DIE for them.  */
3292
3293 #define TYPE_DECL_IS_STUB(decl)                         \
3294   (DECL_NAME (decl) == NULL_TREE                        \
3295    || (DECL_ARTIFICIAL (decl)                           \
3296        && is_tagged_type (TREE_TYPE (decl))             \
3297        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3298            /* This is necessary for stub decls that     \
3299               appear in nested inline functions.  */    \
3300            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3301                && (decl_ultimate_origin (decl)          \
3302                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3303
3304 /* Information concerning the compilation unit's programming
3305    language, and compiler version.  */
3306
3307 /* Fixed size portion of the DWARF compilation unit header.  */
3308 #define DWARF_COMPILE_UNIT_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 3)
3309
3310 /* Fixed size portion of public names info.  */
3311 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3312
3313 /* Fixed size portion of the address range info.  */
3314 #define DWARF_ARANGES_HEADER_SIZE                                       \
3315   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2)        \
3316    - DWARF_OFFSET_SIZE)
3317
3318 /* Size of padding portion in the address range info.  It must be
3319    aligned to twice the pointer size.  */
3320 #define DWARF_ARANGES_PAD_SIZE \
3321   (DWARF_ROUND (2 * DWARF_OFFSET_SIZE + 4, DWARF2_ADDR_SIZE * 2) \
3322    - (2 * DWARF_OFFSET_SIZE + 4))
3323
3324 /* Use assembler line directives if available.  */
3325 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3326 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3327 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3328 #else
3329 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3330 #endif
3331 #endif
3332
3333 /* Minimum line offset in a special line info. opcode.
3334    This value was chosen to give a reasonable range of values.  */
3335 #define DWARF_LINE_BASE  -10
3336
3337 /* First special line opcode - leave room for the standard opcodes.  */
3338 #define DWARF_LINE_OPCODE_BASE  10
3339
3340 /* Range of line offsets in a special line info. opcode.  */
3341 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3342
3343 /* Flag that indicates the initial value of the is_stmt_start flag.
3344    In the present implementation, we do not mark any lines as
3345    the beginning of a source statement, because that information
3346    is not made available by the GCC front-end.  */
3347 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3348
3349 /* This location is used by calc_die_sizes() to keep track
3350    the offset of each DIE within the .debug_info section.  */
3351 static unsigned long next_die_offset;
3352
3353 /* Record the root of the DIE's built for the current compilation unit.  */
3354 static dw_die_ref comp_unit_die;
3355
3356 /* We need special handling in dwarf2out_start_source_file if it is
3357    first one.  */
3358 static int is_main_source;
3359
3360 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3361 static limbo_die_node *limbo_die_list = 0;
3362
3363 /* Structure used by lookup_filename to manage sets of filenames.  */
3364 struct file_table
3365 {
3366   char **table;
3367   unsigned allocated;
3368   unsigned in_use;
3369   unsigned last_lookup_index;
3370 };
3371
3372 /* Size (in elements) of increments by which we may expand the filename
3373    table.  */
3374 #define FILE_TABLE_INCREMENT 64
3375
3376 /* Filenames referenced by this compilation unit.  */
3377 static struct file_table file_table;
3378
3379 /* A pointer to the base of a table of references to DIE's that describe
3380    declarations.  The table is indexed by DECL_UID() which is a unique
3381    number identifying each decl.  */
3382 static dw_die_ref *decl_die_table;
3383
3384 /* Number of elements currently allocated for the decl_die_table.  */
3385 static unsigned decl_die_table_allocated;
3386
3387 /* Number of elements in decl_die_table currently in use.  */
3388 static unsigned decl_die_table_in_use;
3389
3390 /* Size (in elements) of increments by which we may expand the
3391    decl_die_table.  */
3392 #define DECL_DIE_TABLE_INCREMENT 256
3393
3394 /* A pointer to the base of a list of references to DIE's that
3395    are uniquely identified by their tag, presence/absence of
3396    children DIE's, and list of attribute/value pairs.  */
3397 static dw_die_ref *abbrev_die_table;
3398
3399 /* Number of elements currently allocated for abbrev_die_table.  */
3400 static unsigned abbrev_die_table_allocated;
3401
3402 /* Number of elements in type_die_table currently in use.  */
3403 static unsigned abbrev_die_table_in_use;
3404
3405 /* Size (in elements) of increments by which we may expand the
3406    abbrev_die_table.  */
3407 #define ABBREV_DIE_TABLE_INCREMENT 256
3408
3409 /* A pointer to the base of a table that contains line information
3410    for each source code line in .text in the compilation unit.  */
3411 static dw_line_info_ref line_info_table;
3412
3413 /* Number of elements currently allocated for line_info_table.  */
3414 static unsigned line_info_table_allocated;
3415
3416 /* Number of elements in separate_line_info_table currently in use.  */
3417 static unsigned separate_line_info_table_in_use;
3418
3419 /* A pointer to the base of a table that contains line information
3420    for each source code line outside of .text in the compilation unit.  */
3421 static dw_separate_line_info_ref separate_line_info_table;
3422
3423 /* Number of elements currently allocated for separate_line_info_table.  */
3424 static unsigned separate_line_info_table_allocated;
3425
3426 /* Number of elements in line_info_table currently in use.  */
3427 static unsigned line_info_table_in_use;
3428
3429 /* Size (in elements) of increments by which we may expand the
3430    line_info_table.  */
3431 #define LINE_INFO_TABLE_INCREMENT 1024
3432
3433 /* A pointer to the base of a table that contains a list of publicly
3434    accessible names.  */
3435 static pubname_ref pubname_table;
3436
3437 /* Number of elements currently allocated for pubname_table.  */
3438 static unsigned pubname_table_allocated;
3439
3440 /* Number of elements in pubname_table currently in use.  */
3441 static unsigned pubname_table_in_use;
3442
3443 /* Size (in elements) of increments by which we may expand the
3444    pubname_table.  */
3445 #define PUBNAME_TABLE_INCREMENT 64
3446
3447 /* Array of dies for which we should generate .debug_arange info.  */
3448 static dw_die_ref *arange_table;
3449
3450 /* Number of elements currently allocated for arange_table.  */
3451 static unsigned arange_table_allocated;
3452
3453 /* Number of elements in arange_table currently in use.  */
3454 static unsigned arange_table_in_use;
3455
3456 /* Size (in elements) of increments by which we may expand the
3457    arange_table.  */
3458 #define ARANGE_TABLE_INCREMENT 64
3459
3460 /* Array of dies for which we should generate .debug_ranges info.  */
3461 static dw_ranges_ref ranges_table;
3462
3463 /* Number of elements currently allocated for ranges_table.  */
3464 static unsigned ranges_table_allocated;
3465
3466 /* Number of elements in ranges_table currently in use.  */
3467 static unsigned ranges_table_in_use;
3468
3469 /* Size (in elements) of increments by which we may expand the
3470    ranges_table.  */
3471 #define RANGES_TABLE_INCREMENT 64
3472
3473 /* Whether we have location lists that need outputting */
3474 static unsigned have_location_lists;
3475
3476 /* Record whether the function being analyzed contains inlined functions.  */
3477 static int current_function_has_inlines;
3478 #if 0 && defined (MIPS_DEBUGGING_INFO)
3479 static int comp_unit_has_inlines;
3480 #endif
3481
3482 /* Forward declarations for functions defined in this file.  */
3483
3484 static int is_pseudo_reg                PARAMS ((rtx));
3485 static tree type_main_variant           PARAMS ((tree));
3486 static int is_tagged_type               PARAMS ((tree));
3487 static const char *dwarf_tag_name       PARAMS ((unsigned));
3488 static const char *dwarf_attr_name      PARAMS ((unsigned));
3489 static const char *dwarf_form_name      PARAMS ((unsigned));
3490 #if 0
3491 static const char *dwarf_type_encoding_name PARAMS ((unsigned));
3492 #endif
3493 static tree decl_ultimate_origin        PARAMS ((tree));
3494 static tree block_ultimate_origin       PARAMS ((tree));
3495 static tree decl_class_context          PARAMS ((tree));
3496 static void add_dwarf_attr              PARAMS ((dw_die_ref, dw_attr_ref));
3497 static inline dw_val_class AT_class     PARAMS ((dw_attr_ref));
3498 static void add_AT_flag                 PARAMS ((dw_die_ref,
3499                                                  enum dwarf_attribute,
3500                                                  unsigned));
3501 static inline unsigned AT_flag          PARAMS ((dw_attr_ref));
3502 static void add_AT_int                  PARAMS ((dw_die_ref,
3503                                                  enum dwarf_attribute, long));
3504 static inline long int AT_int           PARAMS ((dw_attr_ref));
3505 static void add_AT_unsigned             PARAMS ((dw_die_ref,
3506                                                  enum dwarf_attribute,
3507                                                  unsigned long));
3508 static inline unsigned long             AT_unsigned PARAMS ((dw_attr_ref));
3509 static void add_AT_long_long            PARAMS ((dw_die_ref,
3510                                                  enum dwarf_attribute,
3511                                                  unsigned long,
3512                                                  unsigned long));
3513 static void add_AT_float                PARAMS ((dw_die_ref,
3514                                                  enum dwarf_attribute,
3515                                                  unsigned, long *));
3516 static void add_AT_string               PARAMS ((dw_die_ref,
3517                                                  enum dwarf_attribute,
3518                                                  const char *));
3519 static inline const char *AT_string     PARAMS ((dw_attr_ref));
3520 static int AT_string_form               PARAMS ((dw_attr_ref));
3521 static void add_AT_die_ref              PARAMS ((dw_die_ref,
3522                                                  enum dwarf_attribute,
3523                                                  dw_die_ref));
3524 static inline dw_die_ref AT_ref         PARAMS ((dw_attr_ref));
3525 static inline int AT_ref_external       PARAMS ((dw_attr_ref));
3526 static inline void set_AT_ref_external  PARAMS ((dw_attr_ref, int));
3527 static void add_AT_fde_ref              PARAMS ((dw_die_ref,
3528                                                  enum dwarf_attribute,
3529                                                  unsigned));
3530 static void add_AT_loc                  PARAMS ((dw_die_ref,
3531                                                  enum dwarf_attribute,
3532                                                  dw_loc_descr_ref));
3533 static inline dw_loc_descr_ref AT_loc   PARAMS ((dw_attr_ref));
3534 static void add_AT_loc_list             PARAMS ((dw_die_ref,
3535                                                  enum dwarf_attribute,
3536                                                  dw_loc_list_ref));
3537 static inline dw_loc_list_ref AT_loc_list PARAMS ((dw_attr_ref));
3538 static void add_AT_addr                 PARAMS ((dw_die_ref,
3539                                                  enum dwarf_attribute,
3540                                                  rtx));
3541 static inline rtx AT_addr               PARAMS ((dw_attr_ref));
3542 static void add_AT_lbl_id               PARAMS ((dw_die_ref,
3543                                                  enum dwarf_attribute,
3544                                                  const char *));
3545 static void add_AT_lbl_offset           PARAMS ((dw_die_ref,
3546                                                  enum dwarf_attribute,
3547                                                  const char *));
3548 static void add_AT_offset               PARAMS ((dw_die_ref,
3549                                                  enum dwarf_attribute,
3550                                                  unsigned long));
3551 static void add_AT_range_list           PARAMS ((dw_die_ref,
3552                                                  enum dwarf_attribute,
3553                                                  unsigned long));
3554 static inline const char *AT_lbl        PARAMS ((dw_attr_ref));
3555 static dw_attr_ref get_AT               PARAMS ((dw_die_ref,
3556                                                  enum dwarf_attribute));
3557 static const char *get_AT_low_pc        PARAMS ((dw_die_ref));
3558 static const char *get_AT_hi_pc         PARAMS ((dw_die_ref));
3559 static const char *get_AT_string        PARAMS ((dw_die_ref,
3560                                                  enum dwarf_attribute));
3561 static int get_AT_flag                  PARAMS ((dw_die_ref,
3562                                                  enum dwarf_attribute));
3563 static unsigned get_AT_unsigned         PARAMS ((dw_die_ref,
3564                                                  enum dwarf_attribute));
3565 static inline dw_die_ref get_AT_ref     PARAMS ((dw_die_ref,
3566                                                  enum dwarf_attribute));
3567 static int is_c_family                  PARAMS ((void));
3568 static int is_cxx                       PARAMS ((void));
3569 static int is_java                      PARAMS ((void));
3570 static int is_fortran                   PARAMS ((void));
3571 static void remove_AT                   PARAMS ((dw_die_ref,
3572                                                  enum dwarf_attribute));
3573 static inline void free_die             PARAMS ((dw_die_ref));
3574 static void remove_children             PARAMS ((dw_die_ref));
3575 static void add_child_die               PARAMS ((dw_die_ref, dw_die_ref));
3576 static dw_die_ref new_die               PARAMS ((enum dwarf_tag, dw_die_ref,
3577                                                  tree));
3578 static dw_die_ref lookup_type_die       PARAMS ((tree));
3579 static void equate_type_number_to_die   PARAMS ((tree, dw_die_ref));
3580 static dw_die_ref lookup_decl_die       PARAMS ((tree));
3581 static void equate_decl_number_to_die   PARAMS ((tree, dw_die_ref));
3582 static void print_spaces                PARAMS ((FILE *));
3583 static void print_die                   PARAMS ((dw_die_ref, FILE *));
3584 static void print_dwarf_line_table      PARAMS ((FILE *));
3585 static void reverse_die_lists           PARAMS ((dw_die_ref));
3586 static void reverse_all_dies            PARAMS ((dw_die_ref));
3587 static dw_die_ref push_new_compile_unit PARAMS ((dw_die_ref, dw_die_ref));
3588 static dw_die_ref pop_compile_unit      PARAMS ((dw_die_ref));
3589 static void loc_checksum                PARAMS ((dw_loc_descr_ref,
3590                                                  struct md5_ctx *));
3591 static void attr_checksum               PARAMS ((dw_attr_ref,
3592                                                  struct md5_ctx *,
3593                                                  int *));
3594 static void die_checksum                PARAMS ((dw_die_ref,
3595                                                  struct md5_ctx *,
3596                                                  int *));
3597 static int same_loc_p                   PARAMS ((dw_loc_descr_ref,
3598                                                  dw_loc_descr_ref, int *));
3599 static int same_dw_val_p                PARAMS ((dw_val_node *, dw_val_node *,
3600                                                  int *));
3601 static int same_attr_p                  PARAMS ((dw_attr_ref, dw_attr_ref, int *));
3602 static int same_die_p                   PARAMS ((dw_die_ref, dw_die_ref, int *));
3603 static int same_die_p_wrap              PARAMS ((dw_die_ref, dw_die_ref));
3604 static void compute_section_prefix      PARAMS ((dw_die_ref));
3605 static int is_type_die                  PARAMS ((dw_die_ref));
3606 static int is_comdat_die                PARAMS ((dw_die_ref));
3607 static int is_symbol_die                PARAMS ((dw_die_ref));
3608 static void assign_symbol_names         PARAMS ((dw_die_ref));
3609 static void break_out_includes          PARAMS ((dw_die_ref));
3610 static hashval_t htab_cu_hash           PARAMS ((const void *));
3611 static int htab_cu_eq                   PARAMS ((const void *, const void *));
3612 static void htab_cu_del                 PARAMS ((void *));
3613 static int check_duplicate_cu           PARAMS ((dw_die_ref, htab_t, unsigned *));
3614 static void record_comdat_symbol_number PARAMS ((dw_die_ref, htab_t, unsigned));
3615 static void add_sibling_attributes      PARAMS ((dw_die_ref));
3616 static void build_abbrev_table          PARAMS ((dw_die_ref));
3617 static void output_location_lists       PARAMS ((dw_die_ref));
3618 static int constant_size                PARAMS ((long unsigned));
3619 static unsigned long size_of_die        PARAMS ((dw_die_ref));
3620 static void calc_die_sizes              PARAMS ((dw_die_ref));
3621 static void mark_dies                   PARAMS ((dw_die_ref));
3622 static void unmark_dies                 PARAMS ((dw_die_ref));
3623 static void unmark_all_dies             PARAMS ((dw_die_ref));
3624 static unsigned long size_of_pubnames   PARAMS ((void));
3625 static unsigned long size_of_aranges    PARAMS ((void));
3626 static enum dwarf_form value_format     PARAMS ((dw_attr_ref));
3627 static void output_value_format         PARAMS ((dw_attr_ref));
3628 static void output_abbrev_section       PARAMS ((void));
3629 static void output_die_symbol           PARAMS ((dw_die_ref));
3630 static void output_die                  PARAMS ((dw_die_ref));
3631 static void output_compilation_unit_header PARAMS ((void));
3632 static void output_comp_unit            PARAMS ((dw_die_ref, int));
3633 static const char *dwarf2_name          PARAMS ((tree, int));
3634 static void add_pubname                 PARAMS ((tree, dw_die_ref));
3635 static void output_pubnames             PARAMS ((void));
3636 static void add_arange                  PARAMS ((tree, dw_die_ref));
3637 static void output_aranges              PARAMS ((void));
3638 static unsigned int add_ranges          PARAMS ((tree));
3639 static void output_ranges               PARAMS ((void));
3640 static void output_line_info            PARAMS ((void));
3641 static void output_file_names           PARAMS ((void));
3642 static dw_die_ref base_type_die         PARAMS ((tree));
3643 static tree root_type                   PARAMS ((tree));
3644 static int is_base_type                 PARAMS ((tree));
3645 static dw_die_ref modified_type_die     PARAMS ((tree, int, int, dw_die_ref));
3646 static int type_is_enum                 PARAMS ((tree));
3647 static unsigned int reg_number          PARAMS ((rtx));
3648 static dw_loc_descr_ref reg_loc_descriptor PARAMS ((rtx));
3649 static dw_loc_descr_ref int_loc_descriptor PARAMS ((HOST_WIDE_INT));
3650 static dw_loc_descr_ref based_loc_descr PARAMS ((unsigned, long));
3651 static int is_based_loc                 PARAMS ((rtx));
3652 static dw_loc_descr_ref mem_loc_descriptor PARAMS ((rtx, enum machine_mode mode));
3653 static dw_loc_descr_ref concat_loc_descriptor PARAMS ((rtx, rtx));
3654 static dw_loc_descr_ref loc_descriptor  PARAMS ((rtx));
3655 static dw_loc_descr_ref loc_descriptor_from_tree PARAMS ((tree, int));
3656 static HOST_WIDE_INT ceiling            PARAMS ((HOST_WIDE_INT, unsigned int));
3657 static tree field_type                  PARAMS ((tree));
3658 static unsigned int simple_type_align_in_bits PARAMS ((tree));
3659 static unsigned int simple_decl_align_in_bits PARAMS ((tree));
3660 static unsigned HOST_WIDE_INT simple_type_size_in_bits PARAMS ((tree));
3661 static HOST_WIDE_INT field_byte_offset  PARAMS ((tree));
3662 static void add_AT_location_description PARAMS ((dw_die_ref,
3663                                                  enum dwarf_attribute,
3664                                                  dw_loc_descr_ref));
3665 static void add_data_member_location_attribute PARAMS ((dw_die_ref, tree));
3666 static void add_const_value_attribute   PARAMS ((dw_die_ref, rtx));
3667 static rtx rtl_for_decl_location        PARAMS ((tree));
3668 static void add_location_or_const_value_attribute PARAMS ((dw_die_ref, tree));
3669 static void tree_add_const_value_attribute PARAMS ((dw_die_ref, tree));
3670 static void add_name_attribute          PARAMS ((dw_die_ref, const char *));
3671 static void add_bound_info              PARAMS ((dw_die_ref,
3672                                                  enum dwarf_attribute, tree));
3673 static void add_subscript_info          PARAMS ((dw_die_ref, tree));
3674 static void add_byte_size_attribute     PARAMS ((dw_die_ref, tree));
3675 static void add_bit_offset_attribute    PARAMS ((dw_die_ref, tree));
3676 static void add_bit_size_attribute      PARAMS ((dw_die_ref, tree));
3677 static void add_prototyped_attribute    PARAMS ((dw_die_ref, tree));
3678 static void add_abstract_origin_attribute PARAMS ((dw_die_ref, tree));
3679 static void add_pure_or_virtual_attribute PARAMS ((dw_die_ref, tree));
3680 static void add_src_coords_attributes   PARAMS ((dw_die_ref, tree));
3681 static void add_name_and_src_coords_attributes PARAMS ((dw_die_ref, tree));
3682 static void push_decl_scope             PARAMS ((tree));
3683 static void pop_decl_scope              PARAMS ((void));
3684 static dw_die_ref scope_die_for         PARAMS ((tree, dw_die_ref));
3685 static inline int local_scope_p         PARAMS ((dw_die_ref));
3686 static inline int class_scope_p         PARAMS ((dw_die_ref));
3687 static void add_type_attribute          PARAMS ((dw_die_ref, tree, int, int,
3688                                                  dw_die_ref));
3689 static const char *type_tag             PARAMS ((tree));
3690 static tree member_declared_type        PARAMS ((tree));
3691 #if 0
3692 static const char *decl_start_label     PARAMS ((tree));
3693 #endif
3694 static void gen_array_type_die          PARAMS ((tree, dw_die_ref));
3695 static void gen_set_type_die            PARAMS ((tree, dw_die_ref));
3696 #if 0
3697 static void gen_entry_point_die         PARAMS ((tree, dw_die_ref));
3698 #endif
3699 static void gen_inlined_enumeration_type_die PARAMS ((tree, dw_die_ref));
3700 static void gen_inlined_structure_type_die PARAMS ((tree, dw_die_ref));
3701 static void gen_inlined_union_type_die  PARAMS ((tree, dw_die_ref));
3702 static void gen_enumeration_type_die    PARAMS ((tree, dw_die_ref));
3703 static dw_die_ref gen_formal_parameter_die PARAMS ((tree, dw_die_ref));
3704 static void gen_unspecified_parameters_die PARAMS ((tree, dw_die_ref));
3705 static void gen_formal_types_die        PARAMS ((tree, dw_die_ref));
3706 static void gen_subprogram_die          PARAMS ((tree, dw_die_ref));
3707 static void gen_variable_die            PARAMS ((tree, dw_die_ref));
3708 static void gen_label_die               PARAMS ((tree, dw_die_ref));
3709 static void gen_lexical_block_die       PARAMS ((tree, dw_die_ref, int));
3710 static void gen_inlined_subroutine_die  PARAMS ((tree, dw_die_ref, int));
3711 static void gen_field_die               PARAMS ((tree, dw_die_ref));
3712 static void gen_ptr_to_mbr_type_die     PARAMS ((tree, dw_die_ref));
3713 static dw_die_ref gen_compile_unit_die  PARAMS ((const char *));
3714 static void gen_string_type_die         PARAMS ((tree, dw_die_ref));
3715 static void gen_inheritance_die         PARAMS ((tree, dw_die_ref));
3716 static void gen_member_die              PARAMS ((tree, dw_die_ref));
3717 static void gen_struct_or_union_type_die PARAMS ((tree, dw_die_ref));
3718 static void gen_subroutine_type_die     PARAMS ((tree, dw_die_ref));
3719 static void gen_typedef_die             PARAMS ((tree, dw_die_ref));
3720 static void gen_type_die                PARAMS ((tree, dw_die_ref));
3721 static void gen_tagged_type_instantiation_die PARAMS ((tree, dw_die_ref));
3722 static void gen_block_die               PARAMS ((tree, dw_die_ref, int));
3723 static void decls_for_scope             PARAMS ((tree, dw_die_ref, int));
3724 static int is_redundant_typedef         PARAMS ((tree));
3725 static void gen_decl_die                PARAMS ((tree, dw_die_ref));
3726 static unsigned lookup_filename         PARAMS ((const char *));
3727 static void init_file_table             PARAMS ((void));
3728 static void retry_incomplete_types      PARAMS ((void));
3729 static void gen_type_die_for_member     PARAMS ((tree, tree, dw_die_ref));
3730 static void splice_child_die            PARAMS ((dw_die_ref, dw_die_ref));
3731 static int file_info_cmp                PARAMS ((const void *, const void *));
3732 static dw_loc_list_ref new_loc_list     PARAMS ((dw_loc_descr_ref,
3733                                                  const char *, const char *,
3734                                                  const char *, unsigned));
3735 static void add_loc_descr_to_loc_list   PARAMS ((dw_loc_list_ref *,
3736                                                  dw_loc_descr_ref,
3737                                                  const char *, const char *, const char *));
3738 static void output_loc_list             PARAMS ((dw_loc_list_ref));
3739 static char *gen_internal_sym           PARAMS ((const char *));
3740 static void mark_limbo_die_list         PARAMS ((void *));
3741
3742 /* Section names used to hold DWARF debugging information.  */
3743 #ifndef DEBUG_INFO_SECTION
3744 #define DEBUG_INFO_SECTION      ".debug_info"
3745 #endif
3746 #ifndef DEBUG_ABBREV_SECTION
3747 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3748 #endif
3749 #ifndef DEBUG_ARANGES_SECTION
3750 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3751 #endif
3752 #ifndef DEBUG_MACINFO_SECTION
3753 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3754 #endif
3755 #ifndef DEBUG_LINE_SECTION
3756 #define DEBUG_LINE_SECTION      ".debug_line"
3757 #endif
3758 #ifndef DEBUG_LOC_SECTION
3759 #define DEBUG_LOC_SECTION       ".debug_loc"
3760 #endif
3761 #ifndef DEBUG_PUBNAMES_SECTION
3762 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3763 #endif
3764 #ifndef DEBUG_STR_SECTION
3765 #define DEBUG_STR_SECTION       ".debug_str"
3766 #endif
3767 #ifndef DEBUG_RANGES_SECTION
3768 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3769 #endif
3770
3771 /* Standard ELF section names for compiled code and data.  */
3772 #ifndef TEXT_SECTION_NAME
3773 #define TEXT_SECTION_NAME       ".text"
3774 #endif
3775
3776 /* Section flags for .debug_str section.  */
3777 #ifdef HAVE_GAS_SHF_MERGE
3778 #define DEBUG_STR_SECTION_FLAGS \
3779   (SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1)
3780 #else
3781 #define DEBUG_STR_SECTION_FLAGS SECTION_DEBUG
3782 #endif
3783
3784 /* Labels we insert at beginning sections we can reference instead of
3785    the section names themselves.  */
3786
3787 #ifndef TEXT_SECTION_LABEL
3788 #define TEXT_SECTION_LABEL              "Ltext"
3789 #endif
3790 #ifndef DEBUG_LINE_SECTION_LABEL
3791 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3792 #endif
3793 #ifndef DEBUG_INFO_SECTION_LABEL
3794 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3795 #endif
3796 #ifndef DEBUG_ABBREV_SECTION_LABEL
3797 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3798 #endif
3799 #ifndef DEBUG_LOC_SECTION_LABEL
3800 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3801 #endif
3802 #ifndef DEBUG_RANGES_SECTION_LABEL
3803 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3804 #endif
3805 #ifndef DEBUG_MACINFO_SECTION_LABEL
3806 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3807 #endif
3808
3809 /* Definitions of defaults for formats and names of various special
3810    (artificial) labels which may be generated within this file (when the -g
3811    options is used and DWARF_DEBUGGING_INFO is in effect.
3812    If necessary, these may be overridden from within the tm.h file, but
3813    typically, overriding these defaults is unnecessary.  */
3814
3815 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3816 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3817 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3818 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3819 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3820 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3821 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3822 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3823
3824 #ifndef TEXT_END_LABEL
3825 #define TEXT_END_LABEL          "Letext"
3826 #endif
3827 #ifndef BLOCK_BEGIN_LABEL
3828 #define BLOCK_BEGIN_LABEL       "LBB"
3829 #endif
3830 #ifndef BLOCK_END_LABEL
3831 #define BLOCK_END_LABEL         "LBE"
3832 #endif
3833 #ifndef LINE_CODE_LABEL
3834 #define LINE_CODE_LABEL         "LM"
3835 #endif
3836 #ifndef SEPARATE_LINE_CODE_LABEL
3837 #define SEPARATE_LINE_CODE_LABEL        "LSM"
3838 #endif
3839 \f
3840 /* We allow a language front-end to designate a function that is to be
3841    called to "demangle" any name before it it put into a DIE.  */
3842
3843 static const char *(*demangle_name_func) PARAMS ((const char *));
3844
3845 void
3846 dwarf2out_set_demangle_name_func (func)
3847      const char *(*func) PARAMS ((const char *));
3848 {
3849   demangle_name_func = func;
3850 }
3851
3852 /* Test if rtl node points to a pseudo register.  */
3853
3854 static inline int
3855 is_pseudo_reg (rtl)
3856      rtx rtl;
3857 {
3858   return ((GET_CODE (rtl) == REG && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3859           || (GET_CODE (rtl) == SUBREG
3860               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3861 }
3862
3863 /* Return a reference to a type, with its const and volatile qualifiers
3864    removed.  */
3865
3866 static inline tree
3867 type_main_variant (type)
3868      tree type;
3869 {
3870   type = TYPE_MAIN_VARIANT (type);
3871
3872   /* ??? There really should be only one main variant among any group of
3873      variants of a given type (and all of the MAIN_VARIANT values for all
3874      members of the group should point to that one type) but sometimes the C
3875      front-end messes this up for array types, so we work around that bug
3876      here.  */
3877   if (TREE_CODE (type) == ARRAY_TYPE)
3878     while (type != TYPE_MAIN_VARIANT (type))
3879       type = TYPE_MAIN_VARIANT (type);
3880
3881   return type;
3882 }
3883
3884 /* Return nonzero if the given type node represents a tagged type.  */
3885
3886 static inline int
3887 is_tagged_type (type)
3888      tree type;
3889 {
3890   enum tree_code code = TREE_CODE (type);
3891
3892   return (code == RECORD_TYPE || code == UNION_TYPE
3893           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3894 }
3895
3896 /* Convert a DIE tag into its string name.  */
3897
3898 static const char *
3899 dwarf_tag_name (tag)
3900      unsigned tag;
3901 {
3902   switch (tag)
3903     {
3904     case DW_TAG_padding:
3905       return "DW_TAG_padding";
3906     case DW_TAG_array_type:
3907       return "DW_TAG_array_type";
3908     case DW_TAG_class_type:
3909       return "DW_TAG_class_type";
3910     case DW_TAG_entry_point:
3911       return "DW_TAG_entry_point";
3912     case DW_TAG_enumeration_type:
3913       return "DW_TAG_enumeration_type";
3914     case DW_TAG_formal_parameter:
3915       return "DW_TAG_formal_parameter";
3916     case DW_TAG_imported_declaration:
3917       return "DW_TAG_imported_declaration";
3918     case DW_TAG_label:
3919       return "DW_TAG_label";
3920     case DW_TAG_lexical_block:
3921       return "DW_TAG_lexical_block";
3922     case DW_TAG_member:
3923       return "DW_TAG_member";
3924     case DW_TAG_pointer_type:
3925       return "DW_TAG_pointer_type";
3926     case DW_TAG_reference_type:
3927       return "DW_TAG_reference_type";
3928     case DW_TAG_compile_unit:
3929       return "DW_TAG_compile_unit";
3930     case DW_TAG_string_type:
3931       return "DW_TAG_string_type";
3932     case DW_TAG_structure_type:
3933       return "DW_TAG_structure_type";
3934     case DW_TAG_subroutine_type:
3935       return "DW_TAG_subroutine_type";
3936     case DW_TAG_typedef:
3937       return "DW_TAG_typedef";
3938     case DW_TAG_union_type:
3939       return "DW_TAG_union_type";
3940     case DW_TAG_unspecified_parameters:
3941       return "DW_TAG_unspecified_parameters";
3942     case DW_TAG_variant:
3943       return "DW_TAG_variant";
3944     case DW_TAG_common_block:
3945       return "DW_TAG_common_block";
3946     case DW_TAG_common_inclusion:
3947       return "DW_TAG_common_inclusion";
3948     case DW_TAG_inheritance:
3949       return "DW_TAG_inheritance";
3950     case DW_TAG_inlined_subroutine:
3951       return "DW_TAG_inlined_subroutine";
3952     case DW_TAG_module:
3953       return "DW_TAG_module";
3954     case DW_TAG_ptr_to_member_type:
3955       return "DW_TAG_ptr_to_member_type";
3956     case DW_TAG_set_type:
3957       return "DW_TAG_set_type";
3958     case DW_TAG_subrange_type:
3959       return "DW_TAG_subrange_type";
3960     case DW_TAG_with_stmt:
3961       return "DW_TAG_with_stmt";
3962     case DW_TAG_access_declaration:
3963       return "DW_TAG_access_declaration";
3964     case DW_TAG_base_type:
3965       return "DW_TAG_base_type";
3966     case DW_TAG_catch_block:
3967       return "DW_TAG_catch_block";
3968     case DW_TAG_const_type:
3969       return "DW_TAG_const_type";
3970     case DW_TAG_constant:
3971       return "DW_TAG_constant";
3972     case DW_TAG_enumerator:
3973       return "DW_TAG_enumerator";
3974     case DW_TAG_file_type:
3975       return "DW_TAG_file_type";
3976     case DW_TAG_friend:
3977       return "DW_TAG_friend";
3978     case DW_TAG_namelist:
3979       return "DW_TAG_namelist";
3980     case DW_TAG_namelist_item:
3981       return "DW_TAG_namelist_item";
3982     case DW_TAG_packed_type:
3983       return "DW_TAG_packed_type";
3984     case DW_TAG_subprogram:
3985       return "DW_TAG_subprogram";
3986     case DW_TAG_template_type_param:
3987       return "DW_TAG_template_type_param";
3988     case DW_TAG_template_value_param:
3989       return "DW_TAG_template_value_param";
3990     case DW_TAG_thrown_type:
3991       return "DW_TAG_thrown_type";
3992     case DW_TAG_try_block:
3993       return "DW_TAG_try_block";
3994     case DW_TAG_variant_part:
3995       return "DW_TAG_variant_part";
3996     case DW_TAG_variable:
3997       return "DW_TAG_variable";
3998     case DW_TAG_volatile_type:
3999       return "DW_TAG_volatile_type";
4000     case DW_TAG_MIPS_loop:
4001       return "DW_TAG_MIPS_loop";
4002     case DW_TAG_format_label:
4003       return "DW_TAG_format_label";
4004     case DW_TAG_function_template:
4005       return "DW_TAG_function_template";
4006     case DW_TAG_class_template:
4007       return "DW_TAG_class_template";
4008     case DW_TAG_GNU_BINCL:
4009       return "DW_TAG_GNU_BINCL";
4010     case DW_TAG_GNU_EINCL:
4011       return "DW_TAG_GNU_EINCL";
4012     default:
4013       return "DW_TAG_<unknown>";
4014     }
4015 }
4016
4017 /* Convert a DWARF attribute code into its string name.  */
4018
4019 static const char *
4020 dwarf_attr_name (attr)
4021      unsigned attr;
4022 {
4023   switch (attr)
4024     {
4025     case DW_AT_sibling:
4026       return "DW_AT_sibling";
4027     case DW_AT_location:
4028       return "DW_AT_location";
4029     case DW_AT_name:
4030       return "DW_AT_name";
4031     case DW_AT_ordering:
4032       return "DW_AT_ordering";
4033     case DW_AT_subscr_data:
4034       return "DW_AT_subscr_data";
4035     case DW_AT_byte_size:
4036       return "DW_AT_byte_size";
4037     case DW_AT_bit_offset:
4038       return "DW_AT_bit_offset";
4039     case DW_AT_bit_size:
4040       return "DW_AT_bit_size";
4041     case DW_AT_element_list:
4042       return "DW_AT_element_list";
4043     case DW_AT_stmt_list:
4044       return "DW_AT_stmt_list";
4045     case DW_AT_low_pc:
4046       return "DW_AT_low_pc";
4047     case DW_AT_high_pc:
4048       return "DW_AT_high_pc";
4049     case DW_AT_language:
4050       return "DW_AT_language";
4051     case DW_AT_member:
4052       return "DW_AT_member";
4053     case DW_AT_discr:
4054       return "DW_AT_discr";
4055     case DW_AT_discr_value:
4056       return "DW_AT_discr_value";
4057     case DW_AT_visibility:
4058       return "DW_AT_visibility";
4059     case DW_AT_import:
4060       return "DW_AT_import";
4061     case DW_AT_string_length:
4062       return "DW_AT_string_length";
4063     case DW_AT_common_reference:
4064       return "DW_AT_common_reference";
4065     case DW_AT_comp_dir:
4066       return "DW_AT_comp_dir";
4067     case DW_AT_const_value:
4068       return "DW_AT_const_value";
4069     case DW_AT_containing_type:
4070       return "DW_AT_containing_type";
4071     case DW_AT_default_value:
4072       return "DW_AT_default_value";
4073     case DW_AT_inline:
4074       return "DW_AT_inline";
4075     case DW_AT_is_optional:
4076       return "DW_AT_is_optional";
4077     case DW_AT_lower_bound:
4078       return "DW_AT_lower_bound";
4079     case DW_AT_producer:
4080       return "DW_AT_producer";
4081     case DW_AT_prototyped:
4082       return "DW_AT_prototyped";
4083     case DW_AT_return_addr:
4084       return "DW_AT_return_addr";
4085     case DW_AT_start_scope:
4086       return "DW_AT_start_scope";
4087     case DW_AT_stride_size:
4088       return "DW_AT_stride_size";
4089     case DW_AT_upper_bound:
4090       return "DW_AT_upper_bound";
4091     case DW_AT_abstract_origin:
4092       return "DW_AT_abstract_origin";
4093     case DW_AT_accessibility:
4094       return "DW_AT_accessibility";
4095     case DW_AT_address_class:
4096       return "DW_AT_address_class";
4097     case DW_AT_artificial:
4098       return "DW_AT_artificial";
4099     case DW_AT_base_types:
4100       return "DW_AT_base_types";
4101     case DW_AT_calling_convention:
4102       return "DW_AT_calling_convention";
4103     case DW_AT_count:
4104       return "DW_AT_count";
4105     case DW_AT_data_member_location:
4106       return "DW_AT_data_member_location";
4107     case DW_AT_decl_column:
4108       return "DW_AT_decl_column";
4109     case DW_AT_decl_file:
4110       return "DW_AT_decl_file";
4111     case DW_AT_decl_line:
4112       return "DW_AT_decl_line";
4113     case DW_AT_declaration:
4114       return "DW_AT_declaration";
4115     case DW_AT_discr_list:
4116       return "DW_AT_discr_list";
4117     case DW_AT_encoding:
4118       return "DW_AT_encoding";
4119     case DW_AT_external:
4120       return "DW_AT_external";
4121     case DW_AT_frame_base:
4122       return "DW_AT_frame_base";
4123     case DW_AT_friend:
4124       return "DW_AT_friend";
4125     case DW_AT_identifier_case:
4126       return "DW_AT_identifier_case";
4127     case DW_AT_macro_info:
4128       return "DW_AT_macro_info";
4129     case DW_AT_namelist_items:
4130       return "DW_AT_namelist_items";
4131     case DW_AT_priority:
4132       return "DW_AT_priority";
4133     case DW_AT_segment:
4134       return "DW_AT_segment";
4135     case DW_AT_specification:
4136       return "DW_AT_specification";
4137     case DW_AT_static_link:
4138       return "DW_AT_static_link";
4139     case DW_AT_type:
4140       return "DW_AT_type";
4141     case DW_AT_use_location:
4142       return "DW_AT_use_location";
4143     case DW_AT_variable_parameter:
4144       return "DW_AT_variable_parameter";
4145     case DW_AT_virtuality:
4146       return "DW_AT_virtuality";
4147     case DW_AT_vtable_elem_location:
4148       return "DW_AT_vtable_elem_location";
4149
4150     case DW_AT_allocated:
4151       return "DW_AT_allocated";
4152     case DW_AT_associated:
4153       return "DW_AT_associated";
4154     case DW_AT_data_location:
4155       return "DW_AT_data_location";
4156     case DW_AT_stride:
4157       return "DW_AT_stride";
4158     case DW_AT_entry_pc:
4159       return "DW_AT_entry_pc";
4160     case DW_AT_use_UTF8:
4161       return "DW_AT_use_UTF8";
4162     case DW_AT_extension:
4163       return "DW_AT_extension";
4164     case DW_AT_ranges:
4165       return "DW_AT_ranges";
4166     case DW_AT_trampoline:
4167       return "DW_AT_trampoline";
4168     case DW_AT_call_column:
4169       return "DW_AT_call_column";
4170     case DW_AT_call_file:
4171       return "DW_AT_call_file";
4172     case DW_AT_call_line:
4173       return "DW_AT_call_line";
4174
4175     case DW_AT_MIPS_fde:
4176       return "DW_AT_MIPS_fde";
4177     case DW_AT_MIPS_loop_begin:
4178       return "DW_AT_MIPS_loop_begin";
4179     case DW_AT_MIPS_tail_loop_begin:
4180       return "DW_AT_MIPS_tail_loop_begin";
4181     case DW_AT_MIPS_epilog_begin:
4182       return "DW_AT_MIPS_epilog_begin";
4183     case DW_AT_MIPS_loop_unroll_factor:
4184       return "DW_AT_MIPS_loop_unroll_factor";
4185     case DW_AT_MIPS_software_pipeline_depth:
4186       return "DW_AT_MIPS_software_pipeline_depth";
4187     case DW_AT_MIPS_linkage_name:
4188       return "DW_AT_MIPS_linkage_name";
4189     case DW_AT_MIPS_stride:
4190       return "DW_AT_MIPS_stride";
4191     case DW_AT_MIPS_abstract_name:
4192       return "DW_AT_MIPS_abstract_name";
4193     case DW_AT_MIPS_clone_origin:
4194       return "DW_AT_MIPS_clone_origin";
4195     case DW_AT_MIPS_has_inlines:
4196       return "DW_AT_MIPS_has_inlines";
4197
4198     case DW_AT_sf_names:
4199       return "DW_AT_sf_names";
4200     case DW_AT_src_info:
4201       return "DW_AT_src_info";
4202     case DW_AT_mac_info:
4203       return "DW_AT_mac_info";
4204     case DW_AT_src_coords:
4205       return "DW_AT_src_coords";
4206     case DW_AT_body_begin:
4207       return "DW_AT_body_begin";
4208     case DW_AT_body_end:
4209       return "DW_AT_body_end";
4210     case DW_AT_GNU_vector:
4211       return "DW_AT_GNU_vector";
4212
4213     case DW_AT_VMS_rtnbeg_pd_address:
4214       return "DW_AT_VMS_rtnbeg_pd_address";
4215
4216     default:
4217       return "DW_AT_<unknown>";
4218     }
4219 }
4220
4221 /* Convert a DWARF value form code into its string name.  */
4222
4223 static const char *
4224 dwarf_form_name (form)
4225      unsigned form;
4226 {
4227   switch (form)
4228     {
4229     case DW_FORM_addr:
4230       return "DW_FORM_addr";
4231     case DW_FORM_block2:
4232       return "DW_FORM_block2";
4233     case DW_FORM_block4:
4234       return "DW_FORM_block4";
4235     case DW_FORM_data2:
4236       return "DW_FORM_data2";
4237     case DW_FORM_data4:
4238       return "DW_FORM_data4";
4239     case DW_FORM_data8:
4240       return "DW_FORM_data8";
4241     case DW_FORM_string:
4242       return "DW_FORM_string";
4243     case DW_FORM_block:
4244       return "DW_FORM_block";
4245     case DW_FORM_block1:
4246       return "DW_FORM_block1";
4247     case DW_FORM_data1:
4248       return "DW_FORM_data1";
4249     case DW_FORM_flag:
4250       return "DW_FORM_flag";
4251     case DW_FORM_sdata:
4252       return "DW_FORM_sdata";
4253     case DW_FORM_strp:
4254       return "DW_FORM_strp";
4255     case DW_FORM_udata:
4256       return "DW_FORM_udata";
4257     case DW_FORM_ref_addr:
4258       return "DW_FORM_ref_addr";
4259     case DW_FORM_ref1:
4260       return "DW_FORM_ref1";
4261     case DW_FORM_ref2:
4262       return "DW_FORM_ref2";
4263     case DW_FORM_ref4:
4264       return "DW_FORM_ref4";
4265     case DW_FORM_ref8:
4266       return "DW_FORM_ref8";
4267     case DW_FORM_ref_udata:
4268       return "DW_FORM_ref_udata";
4269     case DW_FORM_indirect:
4270       return "DW_FORM_indirect";
4271     default:
4272       return "DW_FORM_<unknown>";
4273     }
4274 }
4275
4276 /* Convert a DWARF type code into its string name.  */
4277
4278 #if 0
4279 static const char *
4280 dwarf_type_encoding_name (enc)
4281      unsigned enc;
4282 {
4283   switch (enc)
4284     {
4285     case DW_ATE_address:
4286       return "DW_ATE_address";
4287     case DW_ATE_boolean:
4288       return "DW_ATE_boolean";
4289     case DW_ATE_complex_float:
4290       return "DW_ATE_complex_float";
4291     case DW_ATE_float:
4292       return "DW_ATE_float";
4293     case DW_ATE_signed:
4294       return "DW_ATE_signed";
4295     case DW_ATE_signed_char:
4296       return "DW_ATE_signed_char";
4297     case DW_ATE_unsigned:
4298       return "DW_ATE_unsigned";
4299     case DW_ATE_unsigned_char:
4300       return "DW_ATE_unsigned_char";
4301     default:
4302       return "DW_ATE_<unknown>";
4303     }
4304 }
4305 #endif
4306 \f
4307 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4308    instance of an inlined instance of a decl which is local to an inline
4309    function, so we have to trace all of the way back through the origin chain
4310    to find out what sort of node actually served as the original seed for the
4311    given block.  */
4312
4313 static tree
4314 decl_ultimate_origin (decl)
4315      tree decl;
4316 {
4317   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4318      nodes in the function to point to themselves; ignore that if
4319      we're trying to output the abstract instance of this function.  */
4320   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4321     return NULL_TREE;
4322
4323 #ifdef ENABLE_CHECKING
4324   if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
4325     /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4326        most distant ancestor, this should never happen.  */
4327     abort ();
4328 #endif
4329
4330   return DECL_ABSTRACT_ORIGIN (decl);
4331 }
4332
4333 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4334    instance of an inlined instance of a block which is local to an inline
4335    function, so we have to trace all of the way back through the origin chain
4336    to find out what sort of node actually served as the original seed for the
4337    given block.  */
4338
4339 static tree
4340 block_ultimate_origin (block)
4341      tree block;
4342 {
4343   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4344
4345   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4346      nodes in the function to point to themselves; ignore that if
4347      we're trying to output the abstract instance of this function.  */
4348   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4349     return NULL_TREE;
4350
4351   if (immediate_origin == NULL_TREE)
4352     return NULL_TREE;
4353   else
4354     {
4355       tree ret_val;
4356       tree lookahead = immediate_origin;
4357
4358       do
4359         {
4360           ret_val = lookahead;
4361           lookahead = (TREE_CODE (ret_val) == BLOCK
4362                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4363         }
4364       while (lookahead != NULL && lookahead != ret_val);
4365
4366       return ret_val;
4367     }
4368 }
4369
4370 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4371    of a virtual function may refer to a base class, so we check the 'this'
4372    parameter.  */
4373
4374 static tree
4375 decl_class_context (decl)
4376      tree decl;
4377 {
4378   tree context = NULL_TREE;
4379
4380   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4381     context = DECL_CONTEXT (decl);
4382   else
4383     context = TYPE_MAIN_VARIANT
4384       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4385
4386   if (context && !TYPE_P (context))
4387     context = NULL_TREE;
4388
4389   return context;
4390 }
4391 \f
4392 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4393    addition order, and correct that in reverse_all_dies.  */
4394
4395 static inline void
4396 add_dwarf_attr (die, attr)
4397      dw_die_ref die;
4398      dw_attr_ref attr;
4399 {
4400   if (die != NULL && attr != NULL)
4401     {
4402       attr->dw_attr_next = die->die_attr;
4403       die->die_attr = attr;
4404     }
4405 }
4406
4407 static inline dw_val_class
4408 AT_class (a)
4409      dw_attr_ref a;
4410 {
4411   return a->dw_attr_val.val_class;
4412 }
4413
4414 /* Add a flag value attribute to a DIE.  */
4415
4416 static inline void
4417 add_AT_flag (die, attr_kind, flag)
4418      dw_die_ref die;
4419      enum dwarf_attribute attr_kind;
4420      unsigned flag;
4421 {
4422   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4423
4424   attr->dw_attr_next = NULL;
4425   attr->dw_attr = attr_kind;
4426   attr->dw_attr_val.val_class = dw_val_class_flag;
4427   attr->dw_attr_val.v.val_flag = flag;
4428   add_dwarf_attr (die, attr);
4429 }
4430
4431 static inline unsigned
4432 AT_flag (a)
4433      dw_attr_ref a;
4434 {
4435   if (a && AT_class (a) == dw_val_class_flag)
4436     return a->dw_attr_val.v.val_flag;
4437
4438   abort ();
4439 }
4440
4441 /* Add a signed integer attribute value to a DIE.  */
4442
4443 static inline void
4444 add_AT_int (die, attr_kind, int_val)
4445      dw_die_ref die;
4446      enum dwarf_attribute attr_kind;
4447      long int int_val;
4448 {
4449   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4450
4451   attr->dw_attr_next = NULL;
4452   attr->dw_attr = attr_kind;
4453   attr->dw_attr_val.val_class = dw_val_class_const;
4454   attr->dw_attr_val.v.val_int = int_val;
4455   add_dwarf_attr (die, attr);
4456 }
4457
4458 static inline long int
4459 AT_int (a)
4460      dw_attr_ref a;
4461 {
4462   if (a && AT_class (a) == dw_val_class_const)
4463     return a->dw_attr_val.v.val_int;
4464
4465   abort ();
4466 }
4467
4468 /* Add an unsigned integer attribute value to a DIE.  */
4469
4470 static inline void
4471 add_AT_unsigned (die, attr_kind, unsigned_val)
4472      dw_die_ref die;
4473      enum dwarf_attribute attr_kind;
4474      unsigned long unsigned_val;
4475 {
4476   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4477
4478   attr->dw_attr_next = NULL;
4479   attr->dw_attr = attr_kind;
4480   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4481   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4482   add_dwarf_attr (die, attr);
4483 }
4484
4485 static inline unsigned long
4486 AT_unsigned (a)
4487      dw_attr_ref a;
4488 {
4489   if (a && AT_class (a) == dw_val_class_unsigned_const)
4490     return a->dw_attr_val.v.val_unsigned;
4491
4492   abort ();
4493 }
4494
4495 /* Add an unsigned double integer attribute value to a DIE.  */
4496
4497 static inline void
4498 add_AT_long_long (die, attr_kind, val_hi, val_low)
4499      dw_die_ref die;
4500      enum dwarf_attribute attr_kind;
4501      unsigned long val_hi;
4502      unsigned long val_low;
4503 {
4504   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4505
4506   attr->dw_attr_next = NULL;
4507   attr->dw_attr = attr_kind;
4508   attr->dw_attr_val.val_class = dw_val_class_long_long;
4509   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4510   attr->dw_attr_val.v.val_long_long.low = val_low;
4511   add_dwarf_attr (die, attr);
4512 }
4513
4514 /* Add a floating point attribute value to a DIE and return it.  */
4515
4516 static inline void
4517 add_AT_float (die, attr_kind, length, array)
4518      dw_die_ref die;
4519      enum dwarf_attribute attr_kind;
4520      unsigned length;
4521      long *array;
4522 {
4523   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4524
4525   attr->dw_attr_next = NULL;
4526   attr->dw_attr = attr_kind;
4527   attr->dw_attr_val.val_class = dw_val_class_float;
4528   attr->dw_attr_val.v.val_float.length = length;
4529   attr->dw_attr_val.v.val_float.array = array;
4530   add_dwarf_attr (die, attr);
4531 }
4532
4533 /* Add a string attribute value to a DIE.  */
4534
4535 static inline void
4536 add_AT_string (die, attr_kind, str)
4537      dw_die_ref die;
4538      enum dwarf_attribute attr_kind;
4539      const char *str;
4540 {
4541   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4542   struct indirect_string_node *node;
4543
4544   if (! debug_str_hash)
4545     {
4546       debug_str_hash = ht_create (10);
4547       debug_str_hash->alloc_node = indirect_string_alloc;
4548     }
4549
4550   node = (struct indirect_string_node *)
4551          ht_lookup (debug_str_hash, (const unsigned char *) str,
4552                     strlen (str), HT_ALLOC);
4553   node->refcount++;
4554
4555   attr->dw_attr_next = NULL;
4556   attr->dw_attr = attr_kind;
4557   attr->dw_attr_val.val_class = dw_val_class_str;
4558   attr->dw_attr_val.v.val_str = node;
4559   add_dwarf_attr (die, attr);
4560 }
4561
4562 static inline const char *
4563 AT_string (a)
4564      dw_attr_ref a;
4565 {
4566   if (a && AT_class (a) == dw_val_class_str)
4567     return (const char *) HT_STR (&a->dw_attr_val.v.val_str->id);
4568
4569   abort ();
4570 }
4571
4572 /* Find out whether a string should be output inline in DIE
4573    or out-of-line in .debug_str section.  */
4574
4575 static int
4576 AT_string_form (a)
4577      dw_attr_ref a;
4578 {
4579   if (a && AT_class (a) == dw_val_class_str)
4580     {
4581       struct indirect_string_node *node;
4582       unsigned int len;
4583       extern int const_labelno;
4584       char label[32];
4585
4586       node = a->dw_attr_val.v.val_str;
4587       if (node->form)
4588         return node->form;
4589
4590       len = HT_LEN (&node->id) + 1;
4591
4592       /* If the string is shorter or equal to the size of the reference, it is
4593          always better to put it inline.  */
4594       if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4595         return node->form = DW_FORM_string;
4596
4597       /* If we cannot expect the linker to merge strings in .debug_str
4598          section, only put it into .debug_str if it is worth even in this
4599          single module.  */
4600       if ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) == 0
4601           && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
4602         return node->form = DW_FORM_string;
4603
4604       ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4605       ++const_labelno;
4606       node->label = xstrdup (label);
4607
4608       return node->form = DW_FORM_strp;
4609     }
4610
4611   abort ();
4612 }
4613
4614 /* Add a DIE reference attribute value to a DIE.  */
4615
4616 static inline void
4617 add_AT_die_ref (die, attr_kind, targ_die)
4618      dw_die_ref die;
4619      enum dwarf_attribute attr_kind;
4620      dw_die_ref targ_die;
4621 {
4622   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4623
4624   attr->dw_attr_next = NULL;
4625   attr->dw_attr = attr_kind;
4626   attr->dw_attr_val.val_class = dw_val_class_die_ref;
4627   attr->dw_attr_val.v.val_die_ref.die = targ_die;
4628   attr->dw_attr_val.v.val_die_ref.external = 0;
4629   add_dwarf_attr (die, attr);
4630 }
4631
4632 static inline dw_die_ref
4633 AT_ref (a)
4634      dw_attr_ref a;
4635 {
4636   if (a && AT_class (a) == dw_val_class_die_ref)
4637     return a->dw_attr_val.v.val_die_ref.die;
4638
4639   abort ();
4640 }
4641
4642 static inline int
4643 AT_ref_external (a)
4644      dw_attr_ref a;
4645 {
4646   if (a && AT_class (a) == dw_val_class_die_ref)
4647     return a->dw_attr_val.v.val_die_ref.external;
4648
4649   return 0;
4650 }
4651
4652 static inline void
4653 set_AT_ref_external (a, i)
4654      dw_attr_ref a;
4655      int i;
4656 {
4657   if (a && AT_class (a) == dw_val_class_die_ref)
4658     a->dw_attr_val.v.val_die_ref.external = i;
4659   else
4660     abort ();
4661 }
4662
4663 /* Add an FDE reference attribute value to a DIE.  */
4664
4665 static inline void
4666 add_AT_fde_ref (die, attr_kind, targ_fde)
4667      dw_die_ref die;
4668      enum dwarf_attribute attr_kind;
4669      unsigned targ_fde;
4670 {
4671   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4672
4673   attr->dw_attr_next = NULL;
4674   attr->dw_attr = attr_kind;
4675   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
4676   attr->dw_attr_val.v.val_fde_index = targ_fde;
4677   add_dwarf_attr (die, attr);
4678 }
4679
4680 /* Add a location description attribute value to a DIE.  */
4681
4682 static inline void
4683 add_AT_loc (die, attr_kind, loc)
4684      dw_die_ref die;
4685      enum dwarf_attribute attr_kind;
4686      dw_loc_descr_ref loc;
4687 {
4688   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4689
4690   attr->dw_attr_next = NULL;
4691   attr->dw_attr = attr_kind;
4692   attr->dw_attr_val.val_class = dw_val_class_loc;
4693   attr->dw_attr_val.v.val_loc = loc;
4694   add_dwarf_attr (die, attr);
4695 }
4696
4697 static inline dw_loc_descr_ref
4698 AT_loc (a)
4699      dw_attr_ref a;
4700 {
4701   if (a && AT_class (a) == dw_val_class_loc)
4702     return a->dw_attr_val.v.val_loc;
4703
4704   abort ();
4705 }
4706
4707 static inline void
4708 add_AT_loc_list (die, attr_kind, loc_list)
4709      dw_die_ref die;
4710      enum dwarf_attribute attr_kind;
4711      dw_loc_list_ref loc_list;
4712 {
4713   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4714
4715   attr->dw_attr_next = NULL;
4716   attr->dw_attr = attr_kind;
4717   attr->dw_attr_val.val_class = dw_val_class_loc_list;
4718   attr->dw_attr_val.v.val_loc_list = loc_list;
4719   add_dwarf_attr (die, attr);
4720   have_location_lists = 1;
4721 }
4722
4723 static inline dw_loc_list_ref
4724 AT_loc_list (a)
4725      dw_attr_ref a;
4726 {
4727   if (a && AT_class (a) == dw_val_class_loc_list)
4728     return a->dw_attr_val.v.val_loc_list;
4729
4730   abort ();
4731 }
4732
4733 /* Add an address constant attribute value to a DIE.  */
4734
4735 static inline void
4736 add_AT_addr (die, attr_kind, addr)
4737      dw_die_ref die;
4738      enum dwarf_attribute attr_kind;
4739      rtx addr;
4740 {
4741   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4742
4743   attr->dw_attr_next = NULL;
4744   attr->dw_attr = attr_kind;
4745   attr->dw_attr_val.val_class = dw_val_class_addr;
4746   attr->dw_attr_val.v.val_addr = addr;
4747   add_dwarf_attr (die, attr);
4748 }
4749
4750 static inline rtx
4751 AT_addr (a)
4752      dw_attr_ref a;
4753 {
4754   if (a && AT_class (a) == dw_val_class_addr)
4755     return a->dw_attr_val.v.val_addr;
4756
4757   abort ();
4758 }
4759
4760 /* Add a label identifier attribute value to a DIE.  */
4761
4762 static inline void
4763 add_AT_lbl_id (die, attr_kind, lbl_id)
4764      dw_die_ref die;
4765      enum dwarf_attribute attr_kind;
4766      const char *lbl_id;
4767 {
4768   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4769
4770   attr->dw_attr_next = NULL;
4771   attr->dw_attr = attr_kind;
4772   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
4773   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4774   add_dwarf_attr (die, attr);
4775 }
4776
4777 /* Add a section offset attribute value to a DIE.  */
4778
4779 static inline void
4780 add_AT_lbl_offset (die, attr_kind, label)
4781      dw_die_ref die;
4782      enum dwarf_attribute attr_kind;
4783      const char *label;
4784 {
4785   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4786
4787   attr->dw_attr_next = NULL;
4788   attr->dw_attr = attr_kind;
4789   attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
4790   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
4791   add_dwarf_attr (die, attr);
4792 }
4793
4794 /* Add an offset attribute value to a DIE.  */
4795
4796 static inline void
4797 add_AT_offset (die, attr_kind, offset)
4798      dw_die_ref die;
4799      enum dwarf_attribute attr_kind;
4800      unsigned long offset;
4801 {
4802   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4803
4804   attr->dw_attr_next = NULL;
4805   attr->dw_attr = attr_kind;
4806   attr->dw_attr_val.val_class = dw_val_class_offset;
4807   attr->dw_attr_val.v.val_offset = offset;
4808   add_dwarf_attr (die, attr);
4809 }
4810
4811 /* Add an range_list attribute value to a DIE.  */
4812
4813 static void
4814 add_AT_range_list (die, attr_kind, offset)
4815      dw_die_ref die;
4816      enum dwarf_attribute attr_kind;
4817      unsigned long offset;
4818 {
4819   dw_attr_ref attr = (dw_attr_ref) xmalloc (sizeof (dw_attr_node));
4820
4821   attr->dw_attr_next = NULL;
4822   attr->dw_attr = attr_kind;
4823   attr->dw_attr_val.val_class = dw_val_class_range_list;
4824   attr->dw_attr_val.v.val_offset = offset;
4825   add_dwarf_attr (die, attr);
4826 }
4827
4828 static inline const char *
4829 AT_lbl (a)
4830      dw_attr_ref a;
4831 {
4832   if (a && (AT_class (a) == dw_val_class_lbl_id
4833             || AT_class (a) == dw_val_class_lbl_offset))
4834     return a->dw_attr_val.v.val_lbl_id;
4835
4836   abort ();
4837 }
4838
4839 /* Get the attribute of type attr_kind.  */
4840
4841 static inline dw_attr_ref
4842 get_AT (die, attr_kind)
4843      dw_die_ref die;
4844      enum dwarf_attribute attr_kind;
4845 {
4846   dw_attr_ref a;
4847   dw_die_ref spec = NULL;
4848
4849   if (die != NULL)
4850     {
4851       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
4852         if (a->dw_attr == attr_kind)
4853           return a;
4854         else if (a->dw_attr == DW_AT_specification
4855                  || a->dw_attr == DW_AT_abstract_origin)
4856           spec = AT_ref (a);
4857
4858       if (spec)
4859         return get_AT (spec, attr_kind);
4860     }
4861
4862   return NULL;
4863 }
4864
4865 /* Return the "low pc" attribute value, typically associated with a subprogram
4866    DIE.  Return null if the "low pc" attribute is either not present, or if it
4867    cannot be represented as an assembler label identifier.  */
4868
4869 static inline const char *
4870 get_AT_low_pc (die)
4871      dw_die_ref die;
4872 {
4873   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4874
4875   return a ? AT_lbl (a) : NULL;
4876 }
4877
4878 /* Return the "high pc" attribute value, typically associated with a subprogram
4879    DIE.  Return null if the "high pc" attribute is either not present, or if it
4880    cannot be represented as an assembler label identifier.  */
4881
4882 static inline const char *
4883 get_AT_hi_pc (die)
4884      dw_die_ref die;
4885 {
4886   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4887
4888   return a ? AT_lbl (a) : NULL;
4889 }
4890
4891 /* Return the value of the string attribute designated by ATTR_KIND, or
4892    NULL if it is not present.  */
4893
4894 static inline const char *
4895 get_AT_string (die, attr_kind)
4896      dw_die_ref die;
4897      enum dwarf_attribute attr_kind;
4898 {
4899   dw_attr_ref a = get_AT (die, attr_kind);
4900
4901   return a ? AT_string (a) : NULL;
4902 }
4903
4904 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4905    if it is not present.  */
4906
4907 static inline int
4908 get_AT_flag (die, attr_kind)
4909      dw_die_ref die;
4910      enum dwarf_attribute attr_kind;
4911 {
4912   dw_attr_ref a = get_AT (die, attr_kind);
4913
4914   return a ? AT_flag (a) : 0;
4915 }
4916
4917 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4918    if it is not present.  */
4919
4920 static inline unsigned
4921 get_AT_unsigned (die, attr_kind)
4922      dw_die_ref die;
4923      enum dwarf_attribute attr_kind;
4924 {
4925   dw_attr_ref a = get_AT (die, attr_kind);
4926
4927   return a ? AT_unsigned (a) : 0;
4928 }
4929
4930 static inline dw_die_ref
4931 get_AT_ref (die, attr_kind)
4932      dw_die_ref die;
4933      enum dwarf_attribute attr_kind;
4934 {
4935   dw_attr_ref a = get_AT (die, attr_kind);
4936
4937   return a ? AT_ref (a) : NULL;
4938 }
4939
4940 static inline int
4941 is_c_family ()
4942 {
4943   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4944
4945   return (lang == DW_LANG_C || lang == DW_LANG_C89
4946           || lang == DW_LANG_C_plus_plus);
4947 }
4948
4949 static inline int
4950 is_cxx ()
4951 {
4952   return (get_AT_unsigned (comp_unit_die, DW_AT_language)
4953           == DW_LANG_C_plus_plus);
4954 }
4955
4956 static inline int
4957 is_fortran ()
4958 {
4959   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4960
4961   return (lang == DW_LANG_Fortran77 || lang == DW_LANG_Fortran90);
4962 }
4963
4964 static inline int
4965 is_java ()
4966 {
4967   unsigned lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
4968
4969   return (lang == DW_LANG_Java);
4970 }
4971
4972 /* Free up the memory used by A.  */
4973
4974 static inline void free_AT PARAMS ((dw_attr_ref));
4975 static inline void
4976 free_AT (a)
4977      dw_attr_ref a;
4978 {
4979   switch (AT_class (a))
4980     {
4981     case dw_val_class_str:
4982       if (a->dw_attr_val.v.val_str->refcount)
4983         a->dw_attr_val.v.val_str->refcount--;
4984       break;
4985
4986     case dw_val_class_lbl_id:
4987     case dw_val_class_lbl_offset:
4988       free (a->dw_attr_val.v.val_lbl_id);
4989       break;
4990
4991     case dw_val_class_float:
4992       free (a->dw_attr_val.v.val_float.array);
4993       break;
4994
4995     default:
4996       break;
4997     }
4998
4999   free (a);
5000 }
5001
5002 /* Remove the specified attribute if present.  */
5003
5004 static void
5005 remove_AT (die, attr_kind)
5006      dw_die_ref die;
5007      enum dwarf_attribute attr_kind;
5008 {
5009   dw_attr_ref *p;
5010   dw_attr_ref removed = NULL;
5011
5012   if (die != NULL)
5013     {
5014       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5015         if ((*p)->dw_attr == attr_kind)
5016           {
5017             removed = *p;
5018             *p = (*p)->dw_attr_next;
5019             break;
5020           }
5021
5022       if (removed != 0)
5023         free_AT (removed);
5024     }
5025 }
5026
5027 /* Free up the memory used by DIE.  */
5028
5029 static inline void
5030 free_die (die)
5031      dw_die_ref die;
5032 {
5033   remove_children (die);
5034   free (die);
5035 }
5036
5037 /* Discard the children of this DIE.  */
5038
5039 static void
5040 remove_children (die)
5041      dw_die_ref die;
5042 {
5043   dw_die_ref child_die = die->die_child;
5044
5045   die->die_child = NULL;
5046
5047   while (child_die != NULL)
5048     {
5049       dw_die_ref tmp_die = child_die;
5050       dw_attr_ref a;
5051
5052       child_die = child_die->die_sib;
5053
5054       for (a = tmp_die->die_attr; a != NULL;)
5055         {
5056           dw_attr_ref tmp_a = a;
5057
5058           a = a->dw_attr_next;
5059           free_AT (tmp_a);
5060         }
5061
5062       free_die (tmp_die);
5063     }
5064 }
5065
5066 /* Add a child DIE below its parent.  We build the lists up in reverse
5067    addition order, and correct that in reverse_all_dies.  */
5068
5069 static inline void
5070 add_child_die (die, child_die)
5071      dw_die_ref die;
5072      dw_die_ref child_die;
5073 {
5074   if (die != NULL && child_die != NULL)
5075     {
5076       if (die == child_die)
5077         abort ();
5078
5079       child_die->die_parent = die;
5080       child_die->die_sib = die->die_child;
5081       die->die_child = child_die;
5082     }
5083 }
5084
5085 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5086    is the specification, to the front of PARENT's list of children.  */
5087
5088 static void
5089 splice_child_die (parent, child)
5090      dw_die_ref parent, child;
5091 {
5092   dw_die_ref *p;
5093
5094   /* We want the declaration DIE from inside the class, not the
5095      specification DIE at toplevel.  */
5096   if (child->die_parent != parent)
5097     {
5098       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5099
5100       if (tmp)
5101         child = tmp;
5102     }
5103
5104   if (child->die_parent != parent
5105       && child->die_parent != get_AT_ref (parent, DW_AT_specification))
5106     abort ();
5107
5108   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5109     if (*p == child)
5110       {
5111         *p = child->die_sib;
5112         break;
5113       }
5114
5115   child->die_sib = parent->die_child;
5116   parent->die_child = child;
5117 }
5118
5119 /* Return a pointer to a newly created DIE node.  */
5120
5121 static inline dw_die_ref
5122 new_die (tag_value, parent_die, t)
5123      enum dwarf_tag tag_value;
5124      dw_die_ref parent_die;
5125      tree t;
5126 {
5127   dw_die_ref die = (dw_die_ref) xcalloc (1, sizeof (die_node));
5128
5129   die->die_tag = tag_value;
5130
5131   if (parent_die != NULL)
5132     add_child_die (parent_die, die);
5133   else
5134     {
5135       limbo_die_node *limbo_node;
5136
5137       limbo_node = (limbo_die_node *) xmalloc (sizeof (limbo_die_node));
5138       limbo_node->die = die;
5139       limbo_node->created_for = t;
5140       limbo_node->next = limbo_die_list;
5141       limbo_die_list = limbo_node;
5142     }
5143
5144   return die;
5145 }
5146
5147 /* Return the DIE associated with the given type specifier.  */
5148
5149 static inline dw_die_ref
5150 lookup_type_die (type)
5151      tree type;
5152 {
5153   return TYPE_SYMTAB_DIE (type);
5154 }
5155
5156 /* Equate a DIE to a given type specifier.  */
5157
5158 static inline void
5159 equate_type_number_to_die (type, type_die)
5160      tree type;
5161      dw_die_ref type_die;
5162 {
5163   TYPE_SYMTAB_DIE (type) = type_die;
5164 }
5165
5166 /* Return the DIE associated with a given declaration.  */
5167
5168 static inline dw_die_ref
5169 lookup_decl_die (decl)
5170      tree decl;
5171 {
5172   unsigned decl_id = DECL_UID (decl);
5173
5174   return (decl_id < decl_die_table_in_use ? decl_die_table[decl_id] : NULL);
5175 }
5176
5177 /* Equate a DIE to a particular declaration.  */
5178
5179 static void
5180 equate_decl_number_to_die (decl, decl_die)
5181      tree decl;
5182      dw_die_ref decl_die;
5183 {
5184   unsigned int decl_id = DECL_UID (decl);
5185   unsigned int num_allocated;
5186
5187   if (decl_id >= decl_die_table_allocated)
5188     {
5189       num_allocated
5190         = ((decl_id + 1 + DECL_DIE_TABLE_INCREMENT - 1)
5191            / DECL_DIE_TABLE_INCREMENT)
5192           * DECL_DIE_TABLE_INCREMENT;
5193
5194       decl_die_table
5195         = (dw_die_ref *) xrealloc (decl_die_table,
5196                                    sizeof (dw_die_ref) * num_allocated);
5197
5198       memset ((char *) &decl_die_table[decl_die_table_allocated], 0,
5199              (num_allocated - decl_die_table_allocated) * sizeof (dw_die_ref));
5200       decl_die_table_allocated = num_allocated;
5201     }
5202
5203   if (decl_id >= decl_die_table_in_use)
5204     decl_die_table_in_use = (decl_id + 1);
5205
5206   decl_die_table[decl_id] = decl_die;
5207 }
5208 \f
5209 /* Keep track of the number of spaces used to indent the
5210    output of the debugging routines that print the structure of
5211    the DIE internal representation.  */
5212 static int print_indent;
5213
5214 /* Indent the line the number of spaces given by print_indent.  */
5215
5216 static inline void
5217 print_spaces (outfile)
5218      FILE *outfile;
5219 {
5220   fprintf (outfile, "%*s", print_indent, "");
5221 }
5222
5223 /* Print the information associated with a given DIE, and its children.
5224    This routine is a debugging aid only.  */
5225
5226 static void
5227 print_die (die, outfile)
5228      dw_die_ref die;
5229      FILE *outfile;
5230 {
5231   dw_attr_ref a;
5232   dw_die_ref c;
5233
5234   print_spaces (outfile);
5235   fprintf (outfile, "DIE %4lu: %s\n",
5236            die->die_offset, dwarf_tag_name (die->die_tag));
5237   print_spaces (outfile);
5238   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5239   fprintf (outfile, " offset: %lu\n", die->die_offset);
5240
5241   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5242     {
5243       print_spaces (outfile);
5244       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5245
5246       switch (AT_class (a))
5247         {
5248         case dw_val_class_addr:
5249           fprintf (outfile, "address");
5250           break;
5251         case dw_val_class_offset:
5252           fprintf (outfile, "offset");
5253           break;
5254         case dw_val_class_loc:
5255           fprintf (outfile, "location descriptor");
5256           break;
5257         case dw_val_class_loc_list:
5258           fprintf (outfile, "location list -> label:%s",
5259                    AT_loc_list (a)->ll_symbol);
5260           break;
5261         case dw_val_class_range_list:
5262           fprintf (outfile, "range list");
5263           break;
5264         case dw_val_class_const:
5265           fprintf (outfile, "%ld", AT_int (a));
5266           break;
5267         case dw_val_class_unsigned_const:
5268           fprintf (outfile, "%lu", AT_unsigned (a));
5269           break;
5270         case dw_val_class_long_long:
5271           fprintf (outfile, "constant (%lu,%lu)",
5272                    a->dw_attr_val.v.val_long_long.hi,
5273                    a->dw_attr_val.v.val_long_long.low);
5274           break;
5275         case dw_val_class_float:
5276           fprintf (outfile, "floating-point constant");
5277           break;
5278         case dw_val_class_flag:
5279           fprintf (outfile, "%u", AT_flag (a));
5280           break;
5281         case dw_val_class_die_ref:
5282           if (AT_ref (a) != NULL)
5283             {
5284               if (AT_ref (a)->die_symbol)
5285                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5286               else
5287                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5288             }
5289           else
5290             fprintf (outfile, "die -> <null>");
5291           break;
5292         case dw_val_class_lbl_id:
5293         case dw_val_class_lbl_offset:
5294           fprintf (outfile, "label: %s", AT_lbl (a));
5295           break;
5296         case dw_val_class_str:
5297           if (AT_string (a) != NULL)
5298             fprintf (outfile, "\"%s\"", AT_string (a));
5299           else
5300             fprintf (outfile, "<null>");
5301           break;
5302         default:
5303           break;
5304         }
5305
5306       fprintf (outfile, "\n");
5307     }
5308
5309   if (die->die_child != NULL)
5310     {
5311       print_indent += 4;
5312       for (c = die->die_child; c != NULL; c = c->die_sib)
5313         print_die (c, outfile);
5314
5315       print_indent -= 4;
5316     }
5317   if (print_indent == 0)
5318     fprintf (outfile, "\n");
5319 }
5320
5321 /* Print the contents of the source code line number correspondence table.
5322    This routine is a debugging aid only.  */
5323
5324 static void
5325 print_dwarf_line_table (outfile)
5326      FILE *outfile;
5327 {
5328   unsigned i;
5329   dw_line_info_ref line_info;
5330
5331   fprintf (outfile, "\n\nDWARF source line information\n");
5332   for (i = 1; i < line_info_table_in_use; i++)
5333     {
5334       line_info = &line_info_table[i];
5335       fprintf (outfile, "%5d: ", i);
5336       fprintf (outfile, "%-20s", file_table.table[line_info->dw_file_num]);
5337       fprintf (outfile, "%6ld", line_info->dw_line_num);
5338       fprintf (outfile, "\n");
5339     }
5340
5341   fprintf (outfile, "\n\n");
5342 }
5343
5344 /* Print the information collected for a given DIE.  */
5345
5346 void
5347 debug_dwarf_die (die)
5348      dw_die_ref die;
5349 {
5350   print_die (die, stderr);
5351 }
5352
5353 /* Print all DWARF information collected for the compilation unit.
5354    This routine is a debugging aid only.  */
5355
5356 void
5357 debug_dwarf ()
5358 {
5359   print_indent = 0;
5360   print_die (comp_unit_die, stderr);
5361   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5362     print_dwarf_line_table (stderr);
5363 }
5364 \f
5365 /* We build up the lists of children and attributes by pushing new ones
5366    onto the beginning of the list.  Reverse the lists for DIE so that
5367    they are in order of addition.  */
5368
5369 static void
5370 reverse_die_lists (die)
5371      dw_die_ref die;
5372 {
5373   dw_die_ref c, cp, cn;
5374   dw_attr_ref a, ap, an;
5375
5376   for (a = die->die_attr, ap = 0; a; a = an)
5377     {
5378       an = a->dw_attr_next;
5379       a->dw_attr_next = ap;
5380       ap = a;
5381     }
5382
5383   die->die_attr = ap;
5384
5385   for (c = die->die_child, cp = 0; c; c = cn)
5386     {
5387       cn = c->die_sib;
5388       c->die_sib = cp;
5389       cp = c;
5390     }
5391
5392   die->die_child = cp;
5393 }
5394
5395 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5396    reverse all dies in add_sibling_attributes, which runs through all the dies,
5397    it would reverse all the dies.  Now, however, since we don't call
5398    reverse_die_lists in add_sibling_attributes, we need a routine to
5399    recursively reverse all the dies. This is that routine.  */
5400
5401 static void
5402 reverse_all_dies (die)
5403      dw_die_ref die;
5404 {
5405   dw_die_ref c;
5406
5407   reverse_die_lists (die);
5408
5409   for (c = die->die_child; c; c = c->die_sib)
5410     reverse_all_dies (c);
5411 }
5412
5413 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5414    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5415    DIE that marks the start of the DIEs for this include file.  */
5416
5417 static dw_die_ref
5418 push_new_compile_unit (old_unit, bincl_die)
5419      dw_die_ref old_unit, bincl_die;
5420 {
5421   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5422   dw_die_ref new_unit = gen_compile_unit_die (filename);
5423
5424   new_unit->die_sib = old_unit;
5425   return new_unit;
5426 }
5427
5428 /* Close an include-file CU and reopen the enclosing one.  */
5429
5430 static dw_die_ref
5431 pop_compile_unit (old_unit)
5432      dw_die_ref old_unit;
5433 {
5434   dw_die_ref new_unit = old_unit->die_sib;
5435
5436   old_unit->die_sib = NULL;
5437   return new_unit;
5438 }
5439
5440 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5441 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5442
5443 /* Calculate the checksum of a location expression.  */
5444
5445 static inline void
5446 loc_checksum (loc, ctx)
5447      dw_loc_descr_ref loc;
5448      struct md5_ctx *ctx;
5449 {
5450   CHECKSUM (loc->dw_loc_opc);
5451   CHECKSUM (loc->dw_loc_oprnd1);
5452   CHECKSUM (loc->dw_loc_oprnd2);
5453 }
5454
5455 /* Calculate the checksum of an attribute.  */
5456
5457 static void
5458 attr_checksum (at, ctx, mark)
5459      dw_attr_ref at;
5460      struct md5_ctx *ctx;
5461      int *mark;
5462 {
5463   dw_loc_descr_ref loc;
5464   rtx r;
5465
5466   CHECKSUM (at->dw_attr);
5467
5468   /* We don't care about differences in file numbering.  */
5469   if (at->dw_attr == DW_AT_decl_file
5470       /* Or that this was compiled with a different compiler snapshot; if
5471          the output is the same, that's what matters.  */
5472       || at->dw_attr == DW_AT_producer)
5473     return;
5474
5475   switch (AT_class (at))
5476     {
5477     case dw_val_class_const:
5478       CHECKSUM (at->dw_attr_val.v.val_int);
5479       break;
5480     case dw_val_class_unsigned_const:
5481       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5482       break;
5483     case dw_val_class_long_long:
5484       CHECKSUM (at->dw_attr_val.v.val_long_long);
5485       break;
5486     case dw_val_class_float:
5487       CHECKSUM (at->dw_attr_val.v.val_float);
5488       break;
5489     case dw_val_class_flag:
5490       CHECKSUM (at->dw_attr_val.v.val_flag);
5491       break;
5492     case dw_val_class_str:
5493       CHECKSUM_STRING (AT_string (at));
5494       break;
5495
5496     case dw_val_class_addr:
5497       r = AT_addr (at);
5498       switch (GET_CODE (r))
5499         {
5500         case SYMBOL_REF:
5501           CHECKSUM_STRING (XSTR (r, 0));
5502           break;
5503
5504         default:
5505           abort ();
5506         }
5507       break;
5508
5509     case dw_val_class_offset:
5510       CHECKSUM (at->dw_attr_val.v.val_offset);
5511       break;
5512
5513     case dw_val_class_loc:
5514       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5515         loc_checksum (loc, ctx);
5516       break;
5517
5518     case dw_val_class_die_ref:
5519       die_checksum (AT_ref (at), ctx, mark);
5520       break;
5521
5522     case dw_val_class_fde_ref:
5523     case dw_val_class_lbl_id:
5524     case dw_val_class_lbl_offset:
5525       break;
5526
5527     default:
5528       break;
5529     }
5530 }
5531
5532 /* Calculate the checksum of a DIE.  */
5533
5534 static void
5535 die_checksum (die, ctx, mark)
5536      dw_die_ref die;
5537      struct md5_ctx *ctx;
5538      int *mark;
5539 {
5540   dw_die_ref c;
5541   dw_attr_ref a;
5542
5543   /* To avoid infinite recursion.  */
5544   if (die->die_mark)
5545     {
5546       CHECKSUM (die->die_mark);
5547       return;
5548     }
5549   die->die_mark = ++(*mark);
5550
5551   CHECKSUM (die->die_tag);
5552
5553   for (a = die->die_attr; a; a = a->dw_attr_next)
5554     attr_checksum (a, ctx, mark);
5555
5556   for (c = die->die_child; c; c = c->die_sib)
5557     die_checksum (c, ctx, mark);
5558 }
5559
5560 #undef CHECKSUM
5561 #undef CHECKSUM_STRING
5562
5563 /* Do the location expressions look same?  */
5564 static inline int
5565 same_loc_p (loc1, loc2, mark)
5566      dw_loc_descr_ref loc1;
5567      dw_loc_descr_ref loc2;
5568      int *mark;
5569 {
5570   return loc1->dw_loc_opc == loc2->dw_loc_opc
5571          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5572          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5573 }
5574
5575 /* Do the values look the same?  */
5576 static int
5577 same_dw_val_p (v1, v2, mark)
5578      dw_val_node *v1;
5579      dw_val_node *v2;
5580      int *mark;
5581 {
5582   dw_loc_descr_ref loc1, loc2;
5583   rtx r1, r2;
5584   unsigned i;
5585
5586   if (v1->val_class != v2->val_class)
5587     return 0;
5588
5589   switch (v1->val_class)
5590     {
5591     case dw_val_class_const:
5592       return v1->v.val_int == v2->v.val_int;
5593     case dw_val_class_unsigned_const:
5594       return v1->v.val_unsigned == v2->v.val_unsigned;
5595     case dw_val_class_long_long:
5596       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
5597              && v1->v.val_long_long.low == v2->v.val_long_long.low;
5598     case dw_val_class_float:
5599       if (v1->v.val_float.length != v2->v.val_float.length)
5600         return 0;
5601       for (i = 0; i < v1->v.val_float.length; i++)
5602         if (v1->v.val_float.array[i] != v2->v.val_float.array[i])
5603           return 0;
5604       return 1;
5605     case dw_val_class_flag:
5606       return v1->v.val_flag == v2->v.val_flag;
5607     case dw_val_class_str:
5608       return !strcmp((const char *) HT_STR (&v1->v.val_str->id),
5609                      (const char *) HT_STR (&v2->v.val_str->id));
5610
5611     case dw_val_class_addr:
5612       r1 = v1->v.val_addr;
5613       r2 = v2->v.val_addr;
5614       if (GET_CODE (r1) != GET_CODE (r2))
5615         return 0;
5616       switch (GET_CODE (r1))
5617         {
5618         case SYMBOL_REF:
5619           return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
5620
5621         default:
5622           abort ();
5623         }
5624
5625     case dw_val_class_offset:
5626       return v1->v.val_offset == v2->v.val_offset;
5627
5628     case dw_val_class_loc:
5629       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5630            loc1 && loc2;
5631            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5632         if (!same_loc_p (loc1, loc2, mark))
5633           return 0;
5634       return !loc1 && !loc2;
5635
5636     case dw_val_class_die_ref:
5637       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5638
5639     case dw_val_class_fde_ref:
5640     case dw_val_class_lbl_id:
5641     case dw_val_class_lbl_offset:
5642       return 1;
5643
5644     default:
5645       return 1;
5646     }
5647 }
5648
5649 /* Do the attributes look the same?  */
5650
5651 static int
5652 same_attr_p (at1, at2, mark)
5653      dw_attr_ref at1;
5654      dw_attr_ref at2;
5655      int *mark;
5656 {
5657   if (at1->dw_attr != at2->dw_attr)
5658     return 0;
5659
5660   /* We don't care about differences in file numbering.  */
5661   if (at1->dw_attr == DW_AT_decl_file
5662       /* Or that this was compiled with a different compiler snapshot; if
5663          the output is the same, that's what matters.  */
5664       || at1->dw_attr == DW_AT_producer)
5665     return 1;
5666
5667   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5668 }
5669
5670 /* Do the dies look the same?  */
5671
5672 static int
5673 same_die_p (die1, die2, mark)
5674      dw_die_ref die1;
5675      dw_die_ref die2;
5676      int *mark;
5677 {
5678   dw_die_ref c1, c2;
5679   dw_attr_ref a1, a2;
5680
5681   /* To avoid infinite recursion.  */
5682   if (die1->die_mark)
5683     return die1->die_mark == die2->die_mark;
5684   die1->die_mark = die2->die_mark = ++(*mark);
5685
5686   if (die1->die_tag != die2->die_tag)
5687     return 0;
5688
5689   for (a1 = die1->die_attr, a2 = die2->die_attr;
5690        a1 && a2;
5691        a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
5692     if (!same_attr_p (a1, a2, mark))
5693       return 0;
5694   if (a1 || a2)
5695     return 0;
5696
5697   for (c1 = die1->die_child, c2 = die2->die_child;
5698        c1 && c2;
5699        c1 = c1->die_sib, c2 = c2->die_sib)
5700     if (!same_die_p (c1, c2, mark))
5701       return 0;
5702   if (c1 || c2)
5703     return 0;
5704
5705   return 1;
5706 }
5707
5708 /* Do the dies look the same?  Wrapper around same_die_p.  */
5709
5710 static int
5711 same_die_p_wrap (die1, die2)
5712      dw_die_ref die1;
5713      dw_die_ref die2;
5714 {
5715   int mark = 0;
5716   int ret = same_die_p (die1, die2, &mark);
5717
5718   unmark_all_dies (die1);
5719   unmark_all_dies (die2);
5720
5721   return ret;
5722 }
5723
5724 /* The prefix to attach to symbols on DIEs in the current comdat debug
5725    info section.  */
5726 static char *comdat_symbol_id;
5727
5728 /* The index of the current symbol within the current comdat CU.  */
5729 static unsigned int comdat_symbol_number;
5730
5731 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5732    children, and set comdat_symbol_id accordingly.  */
5733
5734 static void
5735 compute_section_prefix (unit_die)
5736      dw_die_ref unit_die;
5737 {
5738   const char *die_name = get_AT_string (unit_die, DW_AT_name);
5739   const char *base = die_name ? lbasename (die_name) : "anonymous";
5740   char *name = (char *) alloca (strlen (base) + 64);
5741   char *p;
5742   int i, mark;
5743   unsigned char checksum[16];
5744   struct md5_ctx ctx;
5745
5746   /* Compute the checksum of the DIE, then append part of it as hex digits to
5747      the name filename of the unit.  */
5748
5749   md5_init_ctx (&ctx);
5750   mark = 0;
5751   die_checksum (unit_die, &ctx, &mark);
5752   unmark_all_dies (unit_die);
5753   md5_finish_ctx (&ctx, checksum);
5754
5755   sprintf (name, "%s.", base);
5756   clean_symbol_name (name);
5757
5758   p = name + strlen (name);
5759   for (i = 0; i < 4; i++)
5760     {
5761       sprintf (p, "%.2x", checksum[i]);
5762       p += 2;
5763     }
5764
5765   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
5766   comdat_symbol_number = 0;
5767 }
5768
5769 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
5770
5771 static int
5772 is_type_die (die)
5773      dw_die_ref die;
5774 {
5775   switch (die->die_tag)
5776     {
5777     case DW_TAG_array_type:
5778     case DW_TAG_class_type:
5779     case DW_TAG_enumeration_type:
5780     case DW_TAG_pointer_type:
5781     case DW_TAG_reference_type:
5782     case DW_TAG_string_type:
5783     case DW_TAG_structure_type:
5784     case DW_TAG_subroutine_type:
5785     case DW_TAG_union_type:
5786     case DW_TAG_ptr_to_member_type:
5787     case DW_TAG_set_type:
5788     case DW_TAG_subrange_type:
5789     case DW_TAG_base_type:
5790     case DW_TAG_const_type:
5791     case DW_TAG_file_type:
5792     case DW_TAG_packed_type:
5793     case DW_TAG_volatile_type:
5794     case DW_TAG_typedef:
5795       return 1;
5796     default:
5797       return 0;
5798     }
5799 }
5800
5801 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5802    Basically, we want to choose the bits that are likely to be shared between
5803    compilations (types) and leave out the bits that are specific to individual
5804    compilations (functions).  */
5805
5806 static int
5807 is_comdat_die (c)
5808      dw_die_ref c;
5809 {
5810   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5811      we do for stabs.  The advantage is a greater likelihood of sharing between
5812      objects that don't include headers in the same order (and therefore would
5813      put the base types in a different comdat).  jason 8/28/00 */
5814
5815   if (c->die_tag == DW_TAG_base_type)
5816     return 0;
5817
5818   if (c->die_tag == DW_TAG_pointer_type
5819       || c->die_tag == DW_TAG_reference_type
5820       || c->die_tag == DW_TAG_const_type
5821       || c->die_tag == DW_TAG_volatile_type)
5822     {
5823       dw_die_ref t = get_AT_ref (c, DW_AT_type);
5824
5825       return t ? is_comdat_die (t) : 0;
5826     }
5827
5828   return is_type_die (c);
5829 }
5830
5831 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5832    compilation unit.  */
5833
5834 static int
5835 is_symbol_die (c)
5836      dw_die_ref c;
5837 {
5838   return (is_type_die (c)
5839           || (get_AT (c, DW_AT_declaration)
5840               && !get_AT (c, DW_AT_specification)));
5841 }
5842
5843 static char *
5844 gen_internal_sym (prefix)
5845      const char *prefix;
5846 {
5847   char buf[256];
5848   static int label_num;
5849
5850   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
5851   return xstrdup (buf);
5852 }
5853
5854 /* Assign symbols to all worthy DIEs under DIE.  */
5855
5856 static void
5857 assign_symbol_names (die)
5858      dw_die_ref die;
5859 {
5860   dw_die_ref c;
5861
5862   if (is_symbol_die (die))
5863     {
5864       if (comdat_symbol_id)
5865         {
5866           char *p = alloca (strlen (comdat_symbol_id) + 64);
5867
5868           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
5869                    comdat_symbol_id, comdat_symbol_number++);
5870           die->die_symbol = xstrdup (p);
5871         }
5872       else
5873         die->die_symbol = gen_internal_sym ("LDIE");
5874     }
5875
5876   for (c = die->die_child; c != NULL; c = c->die_sib)
5877     assign_symbol_names (c);
5878 }
5879
5880 struct cu_hash_table_entry
5881 {
5882   dw_die_ref cu;
5883   unsigned min_comdat_num, max_comdat_num;
5884   struct cu_hash_table_entry *next;
5885 };
5886
5887 /* Routines to manipulate hash table of CUs.  */
5888 static hashval_t
5889 htab_cu_hash (of)
5890      const void *of;
5891 {
5892   const struct cu_hash_table_entry *entry = of;
5893
5894   return htab_hash_string (entry->cu->die_symbol);
5895 }
5896
5897 static int
5898 htab_cu_eq (of1, of2)
5899      const void *of1;
5900      const void *of2;
5901 {
5902   const struct cu_hash_table_entry *entry1 = of1;
5903   const struct die_struct *entry2 = of2;
5904
5905   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
5906 }
5907
5908 static void
5909 htab_cu_del (what)
5910      void *what;
5911 {
5912   struct cu_hash_table_entry *next, *entry = what;
5913
5914   while (entry)
5915     {
5916       next = entry->next;
5917       free (entry);
5918       entry = next;
5919     }
5920 }
5921
5922 /* Check whether we have already seen this CU and set up SYM_NUM
5923    accordingly.  */
5924 static int
5925 check_duplicate_cu (cu, htable, sym_num)
5926      dw_die_ref cu;
5927      htab_t htable;
5928      unsigned *sym_num;
5929 {
5930   struct cu_hash_table_entry dummy;
5931   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
5932
5933   dummy.max_comdat_num = 0;
5934
5935   slot = (struct cu_hash_table_entry **)
5936     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
5937         INSERT);
5938   entry = *slot;
5939
5940   for (; entry; last = entry, entry = entry->next)
5941     {
5942       if (same_die_p_wrap (cu, entry->cu))
5943         break;
5944     }
5945
5946   if (entry)
5947     {
5948       *sym_num = entry->min_comdat_num;
5949       return 1;
5950     }
5951
5952   entry = xcalloc (1, sizeof (struct cu_hash_table_entry));
5953   entry->cu = cu;
5954   entry->min_comdat_num = *sym_num = last->max_comdat_num;
5955   entry->next = *slot;
5956   *slot = entry;
5957
5958   return 0;
5959 }
5960
5961 /* Record SYM_NUM to record of CU in HTABLE.  */
5962 static void
5963 record_comdat_symbol_number (cu, htable, sym_num)
5964      dw_die_ref cu;
5965      htab_t htable;
5966      unsigned sym_num;
5967 {
5968   struct cu_hash_table_entry **slot, *entry;
5969
5970   slot = (struct cu_hash_table_entry **)
5971     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
5972         NO_INSERT);
5973   entry = *slot;
5974
5975   entry->max_comdat_num = sym_num;
5976 }
5977
5978 /* Traverse the DIE (which is always comp_unit_die), and set up
5979    additional compilation units for each of the include files we see
5980    bracketed by BINCL/EINCL.  */
5981
5982 static void
5983 break_out_includes (die)
5984      dw_die_ref die;
5985 {
5986   dw_die_ref *ptr;
5987   dw_die_ref unit = NULL;
5988   limbo_die_node *node, **pnode;
5989   htab_t cu_hash_table;
5990
5991   for (ptr = &(die->die_child); *ptr;)
5992     {
5993       dw_die_ref c = *ptr;
5994
5995       if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
5996           || (unit && is_comdat_die (c)))
5997         {
5998           /* This DIE is for a secondary CU; remove it from the main one.  */
5999           *ptr = c->die_sib;
6000
6001           if (c->die_tag == DW_TAG_GNU_BINCL)
6002             {
6003               unit = push_new_compile_unit (unit, c);
6004               free_die (c);
6005             }
6006           else if (c->die_tag == DW_TAG_GNU_EINCL)
6007             {
6008               unit = pop_compile_unit (unit);
6009               free_die (c);
6010             }
6011           else
6012             add_child_die (unit, c);
6013         }
6014       else
6015         {
6016           /* Leave this DIE in the main CU.  */
6017           ptr = &(c->die_sib);
6018           continue;
6019         }
6020     }
6021
6022 #if 0
6023   /* We can only use this in debugging, since the frontend doesn't check
6024      to make sure that we leave every include file we enter.  */
6025   if (unit != NULL)
6026     abort ();
6027 #endif
6028
6029   assign_symbol_names (die);
6030   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6031   for (node = limbo_die_list, pnode = &limbo_die_list;
6032        node;
6033        node = node->next)
6034     {
6035       int is_dupl;
6036
6037       compute_section_prefix (node->die);
6038       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6039                         &comdat_symbol_number);
6040       assign_symbol_names (node->die);
6041       if (is_dupl)
6042         *pnode = node->next;
6043       else
6044         {
6045           pnode = &node->next;
6046           record_comdat_symbol_number (node->die, cu_hash_table,
6047                 comdat_symbol_number);
6048         }
6049     }
6050   htab_delete (cu_hash_table);
6051 }
6052
6053 /* Traverse the DIE and add a sibling attribute if it may have the
6054    effect of speeding up access to siblings.  To save some space,
6055    avoid generating sibling attributes for DIE's without children.  */
6056
6057 static void
6058 add_sibling_attributes (die)
6059      dw_die_ref die;
6060 {
6061   dw_die_ref c;
6062
6063   if (die->die_tag != DW_TAG_compile_unit
6064       && die->die_sib && die->die_child != NULL)
6065     /* Add the sibling link to the front of the attribute list.  */
6066     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6067
6068   for (c = die->die_child; c != NULL; c = c->die_sib)
6069     add_sibling_attributes (c);
6070 }
6071
6072 /* Output all location lists for the DIE and its children.  */
6073
6074 static void
6075 output_location_lists (die)
6076      dw_die_ref die;
6077 {
6078   dw_die_ref c;
6079   dw_attr_ref d_attr;
6080
6081   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6082     if (AT_class (d_attr) == dw_val_class_loc_list)
6083       output_loc_list (AT_loc_list (d_attr));
6084
6085   for (c = die->die_child; c != NULL; c = c->die_sib)
6086     output_location_lists (c);
6087
6088 }
6089
6090 /* The format of each DIE (and its attribute value pairs) is encoded in an
6091    abbreviation table.  This routine builds the abbreviation table and assigns
6092    a unique abbreviation id for each abbreviation entry.  The children of each
6093    die are visited recursively.  */
6094
6095 static void
6096 build_abbrev_table (die)
6097      dw_die_ref die;
6098 {
6099   unsigned long abbrev_id;
6100   unsigned int n_alloc;
6101   dw_die_ref c;
6102   dw_attr_ref d_attr, a_attr;
6103
6104   /* Scan the DIE references, and mark as external any that refer to
6105      DIEs from other CUs (i.e. those which are not marked).  */
6106   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6107     if (AT_class (d_attr) == dw_val_class_die_ref
6108         && AT_ref (d_attr)->die_mark == 0)
6109       {
6110         if (AT_ref (d_attr)->die_symbol == 0)
6111           abort ();
6112
6113         set_AT_ref_external (d_attr, 1);
6114       }
6115
6116   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6117     {
6118       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6119
6120       if (abbrev->die_tag == die->die_tag)
6121         {
6122           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6123             {
6124               a_attr = abbrev->die_attr;
6125               d_attr = die->die_attr;
6126
6127               while (a_attr != NULL && d_attr != NULL)
6128                 {
6129                   if ((a_attr->dw_attr != d_attr->dw_attr)
6130                       || (value_format (a_attr) != value_format (d_attr)))
6131                     break;
6132
6133                   a_attr = a_attr->dw_attr_next;
6134                   d_attr = d_attr->dw_attr_next;
6135                 }
6136
6137               if (a_attr == NULL && d_attr == NULL)
6138                 break;
6139             }
6140         }
6141     }
6142
6143   if (abbrev_id >= abbrev_die_table_in_use)
6144     {
6145       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6146         {
6147           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6148           abbrev_die_table
6149             = (dw_die_ref *) xrealloc (abbrev_die_table,
6150                                        sizeof (dw_die_ref) * n_alloc);
6151
6152           memset ((char *) &abbrev_die_table[abbrev_die_table_allocated], 0,
6153                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6154           abbrev_die_table_allocated = n_alloc;
6155         }
6156
6157       ++abbrev_die_table_in_use;
6158       abbrev_die_table[abbrev_id] = die;
6159     }
6160
6161   die->die_abbrev = abbrev_id;
6162   for (c = die->die_child; c != NULL; c = c->die_sib)
6163     build_abbrev_table (c);
6164 }
6165 \f
6166 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6167
6168 static int
6169 constant_size (value)
6170      long unsigned value;
6171 {
6172   int log;
6173
6174   if (value == 0)
6175     log = 0;
6176   else
6177     log = floor_log2 (value);
6178
6179   log = log / 8;
6180   log = 1 << (floor_log2 (log) + 1);
6181
6182   return log;
6183 }
6184
6185 /* Return the size of a DIE as it is represented in the
6186    .debug_info section.  */
6187
6188 static unsigned long
6189 size_of_die (die)
6190      dw_die_ref die;
6191 {
6192   unsigned long size = 0;
6193   dw_attr_ref a;
6194
6195   size += size_of_uleb128 (die->die_abbrev);
6196   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6197     {
6198       switch (AT_class (a))
6199         {
6200         case dw_val_class_addr:
6201           size += DWARF2_ADDR_SIZE;
6202           break;
6203         case dw_val_class_offset:
6204           size += DWARF_OFFSET_SIZE;
6205           break;
6206         case dw_val_class_loc:
6207           {
6208             unsigned long lsize = size_of_locs (AT_loc (a));
6209
6210             /* Block length.  */
6211             size += constant_size (lsize);
6212             size += lsize;
6213           }
6214           break;
6215         case dw_val_class_loc_list:
6216           size += DWARF_OFFSET_SIZE;
6217           break;
6218         case dw_val_class_range_list:
6219           size += DWARF_OFFSET_SIZE;
6220           break;
6221         case dw_val_class_const:
6222           size += size_of_sleb128 (AT_int (a));
6223           break;
6224         case dw_val_class_unsigned_const:
6225           size += constant_size (AT_unsigned (a));
6226           break;
6227         case dw_val_class_long_long:
6228           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6229           break;
6230         case dw_val_class_float:
6231           size += 1 + a->dw_attr_val.v.val_float.length * 4; /* block */
6232           break;
6233         case dw_val_class_flag:
6234           size += 1;
6235           break;
6236         case dw_val_class_die_ref:
6237           size += DWARF_OFFSET_SIZE;
6238           break;
6239         case dw_val_class_fde_ref:
6240           size += DWARF_OFFSET_SIZE;
6241           break;
6242         case dw_val_class_lbl_id:
6243           size += DWARF2_ADDR_SIZE;
6244           break;
6245         case dw_val_class_lbl_offset:
6246           size += DWARF_OFFSET_SIZE;
6247           break;
6248         case dw_val_class_str:
6249           if (AT_string_form (a) == DW_FORM_strp)
6250             size += DWARF_OFFSET_SIZE;
6251           else
6252             size += HT_LEN (&a->dw_attr_val.v.val_str->id) + 1;
6253           break;
6254         default:
6255           abort ();
6256         }
6257     }
6258
6259   return size;
6260 }
6261
6262 /* Size the debugging information associated with a given DIE.  Visits the
6263    DIE's children recursively.  Updates the global variable next_die_offset, on
6264    each time through.  Uses the current value of next_die_offset to update the
6265    die_offset field in each DIE.  */
6266
6267 static void
6268 calc_die_sizes (die)
6269      dw_die_ref die;
6270 {
6271   dw_die_ref c;
6272
6273   die->die_offset = next_die_offset;
6274   next_die_offset += size_of_die (die);
6275
6276   for (c = die->die_child; c != NULL; c = c->die_sib)
6277     calc_die_sizes (c);
6278
6279   if (die->die_child != NULL)
6280     /* Count the null byte used to terminate sibling lists.  */
6281     next_die_offset += 1;
6282 }
6283
6284 /* Set the marks for a die and its children.  We do this so
6285    that we know whether or not a reference needs to use FORM_ref_addr; only
6286    DIEs in the same CU will be marked.  We used to clear out the offset
6287    and use that as the flag, but ran into ordering problems.  */
6288
6289 static void
6290 mark_dies (die)
6291      dw_die_ref die;
6292 {
6293   dw_die_ref c;
6294
6295   if (die->die_mark)
6296     abort ();
6297   
6298   die->die_mark = 1;
6299   for (c = die->die_child; c; c = c->die_sib)
6300     mark_dies (c);
6301 }
6302
6303 /* Clear the marks for a die and its children.  */
6304
6305 static void
6306 unmark_dies (die)
6307      dw_die_ref die;
6308 {
6309   dw_die_ref c;
6310
6311   if (!die->die_mark)
6312     abort ();
6313   
6314   die->die_mark = 0;
6315   for (c = die->die_child; c; c = c->die_sib)
6316     unmark_dies (c);
6317 }
6318
6319 /* Clear the marks for a die, its children and referred dies.  */
6320
6321 static void
6322 unmark_all_dies (die)
6323      dw_die_ref die;
6324 {
6325   dw_die_ref c;
6326   dw_attr_ref a;
6327
6328   if (!die->die_mark)
6329     return;
6330   die->die_mark = 0;
6331
6332   for (c = die->die_child; c; c = c->die_sib)
6333     unmark_all_dies (c);
6334
6335   for (a = die->die_attr; a; a = a->dw_attr_next)
6336     if (AT_class (a) == dw_val_class_die_ref)
6337       unmark_all_dies (AT_ref (a));
6338 }
6339
6340 /* Return the size of the .debug_pubnames table  generated for the
6341    compilation unit.  */
6342
6343 static unsigned long
6344 size_of_pubnames ()
6345 {
6346   unsigned long size;
6347   unsigned i;
6348
6349   size = DWARF_PUBNAMES_HEADER_SIZE;
6350   for (i = 0; i < pubname_table_in_use; i++)
6351     {
6352       pubname_ref p = &pubname_table[i];
6353       size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6354     }
6355
6356   size += DWARF_OFFSET_SIZE;
6357   return size;
6358 }
6359
6360 /* Return the size of the information in the .debug_aranges section.  */
6361
6362 static unsigned long
6363 size_of_aranges ()
6364 {
6365   unsigned long size;
6366
6367   size = DWARF_ARANGES_HEADER_SIZE;
6368
6369   /* Count the address/length pair for this compilation unit.  */
6370   size += 2 * DWARF2_ADDR_SIZE;
6371   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6372
6373   /* Count the two zero words used to terminated the address range table.  */
6374   size += 2 * DWARF2_ADDR_SIZE;
6375   return size;
6376 }
6377 \f
6378 /* Select the encoding of an attribute value.  */
6379
6380 static enum dwarf_form
6381 value_format (a)
6382      dw_attr_ref a;
6383 {
6384   switch (a->dw_attr_val.val_class)
6385     {
6386     case dw_val_class_addr:
6387       return DW_FORM_addr;
6388     case dw_val_class_range_list:
6389     case dw_val_class_offset:
6390       if (DWARF_OFFSET_SIZE == 4)
6391         return DW_FORM_data4;
6392       if (DWARF_OFFSET_SIZE == 8)
6393         return DW_FORM_data8;
6394       abort ();
6395     case dw_val_class_loc_list:
6396       /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6397          .debug_loc section */
6398       return DW_FORM_data4;
6399     case dw_val_class_loc:
6400       switch (constant_size (size_of_locs (AT_loc (a))))
6401         {
6402         case 1:
6403           return DW_FORM_block1;
6404         case 2:
6405           return DW_FORM_block2;
6406         default:
6407           abort ();
6408         }
6409     case dw_val_class_const:
6410       return DW_FORM_sdata;
6411     case dw_val_class_unsigned_const:
6412       switch (constant_size (AT_unsigned (a)))
6413         {
6414         case 1:
6415           return DW_FORM_data1;
6416         case 2:
6417           return DW_FORM_data2;
6418         case 4:
6419           return DW_FORM_data4;
6420         case 8:
6421           return DW_FORM_data8;
6422         default:
6423           abort ();
6424         }
6425     case dw_val_class_long_long:
6426       return DW_FORM_block1;
6427     case dw_val_class_float:
6428       return DW_FORM_block1;
6429     case dw_val_class_flag:
6430       return DW_FORM_flag;
6431     case dw_val_class_die_ref:
6432       if (AT_ref_external (a))
6433         return DW_FORM_ref_addr;
6434       else
6435         return DW_FORM_ref;
6436     case dw_val_class_fde_ref:
6437       return DW_FORM_data;
6438     case dw_val_class_lbl_id:
6439       return DW_FORM_addr;
6440     case dw_val_class_lbl_offset:
6441       return DW_FORM_data;
6442     case dw_val_class_str:
6443       return AT_string_form (a);
6444
6445     default:
6446       abort ();
6447     }
6448 }
6449
6450 /* Output the encoding of an attribute value.  */
6451
6452 static void
6453 output_value_format (a)
6454      dw_attr_ref a;
6455 {
6456   enum dwarf_form form = value_format (a);
6457
6458   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6459 }
6460
6461 /* Output the .debug_abbrev section which defines the DIE abbreviation
6462    table.  */
6463
6464 static void
6465 output_abbrev_section ()
6466 {
6467   unsigned long abbrev_id;
6468
6469   dw_attr_ref a_attr;
6470
6471   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6472     {
6473       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6474
6475       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6476       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6477                                    dwarf_tag_name (abbrev->die_tag));
6478
6479       if (abbrev->die_child != NULL)
6480         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6481       else
6482         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6483
6484       for (a_attr = abbrev->die_attr; a_attr != NULL;
6485            a_attr = a_attr->dw_attr_next)
6486         {
6487           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6488                                        dwarf_attr_name (a_attr->dw_attr));
6489           output_value_format (a_attr);
6490         }
6491
6492       dw2_asm_output_data (1, 0, NULL);
6493       dw2_asm_output_data (1, 0, NULL);
6494     }
6495
6496   /* Terminate the table.  */
6497   dw2_asm_output_data (1, 0, NULL);
6498 }
6499
6500 /* Output a symbol we can use to refer to this DIE from another CU.  */
6501
6502 static inline void
6503 output_die_symbol (die)
6504      dw_die_ref die;
6505 {
6506   char *sym = die->die_symbol;
6507
6508   if (sym == 0)
6509     return;
6510
6511   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6512     /* We make these global, not weak; if the target doesn't support
6513        .linkonce, it doesn't support combining the sections, so debugging
6514        will break.  */
6515     (*targetm.asm_out.globalize_label) (asm_out_file, sym);
6516
6517   ASM_OUTPUT_LABEL (asm_out_file, sym);
6518 }
6519
6520 /* Return a new location list, given the begin and end range, and the
6521    expression. gensym tells us whether to generate a new internal symbol for
6522    this location list node, which is done for the head of the list only.  */
6523
6524 static inline dw_loc_list_ref
6525 new_loc_list (expr, begin, end, section, gensym)
6526      dw_loc_descr_ref expr;
6527      const char *begin;
6528      const char *end;
6529      const char *section;
6530      unsigned gensym;
6531 {
6532   dw_loc_list_ref retlist
6533     = (dw_loc_list_ref) xcalloc (1, sizeof (dw_loc_list_node));
6534
6535   retlist->begin = begin;
6536   retlist->end = end;
6537   retlist->expr = expr;
6538   retlist->section = section;
6539   if (gensym)
6540     retlist->ll_symbol = gen_internal_sym ("LLST");
6541
6542   return retlist;
6543 }
6544
6545 /* Add a location description expression to a location list */
6546
6547 static inline void
6548 add_loc_descr_to_loc_list (list_head, descr, begin, end, section)
6549      dw_loc_list_ref *list_head;
6550      dw_loc_descr_ref descr;
6551      const char *begin;
6552      const char *end;
6553      const char *section;
6554 {
6555   dw_loc_list_ref *d;
6556
6557   /* Find the end of the chain.  */
6558   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6559     ;
6560
6561   /* Add a new location list node to the list */
6562   *d = new_loc_list (descr, begin, end, section, 0);
6563 }
6564
6565 /* Output the location list given to us */
6566
6567 static void
6568 output_loc_list (list_head)
6569      dw_loc_list_ref list_head;
6570 {
6571   dw_loc_list_ref curr = list_head;
6572
6573   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6574
6575   /* ??? This shouldn't be needed now that we've forced the
6576      compilation unit base address to zero when there is code
6577      in more than one section.  */
6578   if (strcmp (curr->section, ".text") == 0)
6579     {
6580       /* dw2_asm_output_data will mask off any extra bits in the ~0.  */
6581       dw2_asm_output_data (DWARF2_ADDR_SIZE, ~(unsigned HOST_WIDE_INT) 0,
6582                            "Location list base address specifier fake entry");
6583       dw2_asm_output_offset (DWARF2_ADDR_SIZE, curr->section,
6584                              "Location list base address specifier base");
6585     }
6586
6587   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6588     {
6589       unsigned long size;
6590
6591       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6592                             "Location list begin address (%s)",
6593                             list_head->ll_symbol);
6594       dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6595                             "Location list end address (%s)",
6596                             list_head->ll_symbol);
6597       size = size_of_locs (curr->expr);
6598
6599       /* Output the block length for this list of location operations.  */
6600       if (size > 0xffff)
6601         abort ();
6602       dw2_asm_output_data (2, size, "%s", "Location expression size");
6603
6604       output_loc_sequence (curr->expr);
6605     }
6606
6607   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6608                        "Location list terminator begin (%s)",
6609                        list_head->ll_symbol);
6610   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0,
6611                        "Location list terminator end (%s)",
6612                        list_head->ll_symbol);
6613 }
6614
6615 /* Output the DIE and its attributes.  Called recursively to generate
6616    the definitions of each child DIE.  */
6617
6618 static void
6619 output_die (die)
6620      dw_die_ref die;
6621 {
6622   dw_attr_ref a;
6623   dw_die_ref c;
6624   unsigned long size;
6625
6626   /* If someone in another CU might refer to us, set up a symbol for
6627      them to point to.  */
6628   if (die->die_symbol)
6629     output_die_symbol (die);
6630
6631   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
6632                                die->die_offset, dwarf_tag_name (die->die_tag));
6633
6634   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6635     {
6636       const char *name = dwarf_attr_name (a->dw_attr);
6637
6638       switch (AT_class (a))
6639         {
6640         case dw_val_class_addr:
6641           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
6642           break;
6643
6644         case dw_val_class_offset:
6645           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
6646                                "%s", name);
6647           break;
6648
6649         case dw_val_class_range_list:
6650           {
6651             char *p = strchr (ranges_section_label, '\0');
6652
6653             sprintf (p, "+0x%lx", a->dw_attr_val.v.val_offset);
6654             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
6655                                    "%s", name);
6656             *p = '\0';
6657           }
6658           break;
6659
6660         case dw_val_class_loc:
6661           size = size_of_locs (AT_loc (a));
6662
6663           /* Output the block length for this list of location operations.  */
6664           dw2_asm_output_data (constant_size (size), size, "%s", name);
6665
6666           output_loc_sequence (AT_loc (a));
6667           break;
6668
6669         case dw_val_class_const:
6670           /* ??? It would be slightly more efficient to use a scheme like is
6671              used for unsigned constants below, but gdb 4.x does not sign
6672              extend.  Gdb 5.x does sign extend.  */
6673           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
6674           break;
6675
6676         case dw_val_class_unsigned_const:
6677           dw2_asm_output_data (constant_size (AT_unsigned (a)),
6678                                AT_unsigned (a), "%s", name);
6679           break;
6680
6681         case dw_val_class_long_long:
6682           {
6683             unsigned HOST_WIDE_INT first, second;
6684
6685             dw2_asm_output_data (1,
6686                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6687                                  "%s", name);
6688
6689             if (WORDS_BIG_ENDIAN)
6690               {
6691                 first = a->dw_attr_val.v.val_long_long.hi;
6692                 second = a->dw_attr_val.v.val_long_long.low;
6693               }
6694             else
6695               {
6696                 first = a->dw_attr_val.v.val_long_long.low;
6697                 second = a->dw_attr_val.v.val_long_long.hi;
6698               }
6699
6700             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6701                                  first, "long long constant");
6702             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
6703                                  second, NULL);
6704           }
6705           break;
6706
6707         case dw_val_class_float:
6708           {
6709             unsigned int i;
6710
6711             dw2_asm_output_data (1, a->dw_attr_val.v.val_float.length * 4,
6712                                  "%s", name);
6713
6714             for (i = 0; i < a->dw_attr_val.v.val_float.length; i++)
6715               dw2_asm_output_data (4, a->dw_attr_val.v.val_float.array[i],
6716                                    "fp constant word %u", i);
6717             break;
6718           }
6719
6720         case dw_val_class_flag:
6721           dw2_asm_output_data (1, AT_flag (a), "%s", name);
6722           break;
6723
6724         case dw_val_class_loc_list:
6725           {
6726             char *sym = AT_loc_list (a)->ll_symbol;
6727
6728             if (sym == 0)
6729               abort ();
6730             dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym,
6731                                   loc_section_label, "%s", name);
6732           }
6733           break;
6734
6735         case dw_val_class_die_ref:
6736           if (AT_ref_external (a))
6737             {
6738               char *sym = AT_ref (a)->die_symbol;
6739
6740               if (sym == 0)
6741                 abort ();
6742               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
6743             }
6744           else if (AT_ref (a)->die_offset == 0)
6745             abort ();
6746           else
6747             dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
6748                                  "%s", name);
6749           break;
6750
6751         case dw_val_class_fde_ref:
6752           {
6753             char l1[20];
6754
6755             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
6756                                          a->dw_attr_val.v.val_fde_index * 2);
6757             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
6758           }
6759           break;
6760
6761         case dw_val_class_lbl_id:
6762           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
6763           break;
6764
6765         case dw_val_class_lbl_offset:
6766           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
6767           break;
6768
6769         case dw_val_class_str:
6770           if (AT_string_form (a) == DW_FORM_strp)
6771             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
6772                                    a->dw_attr_val.v.val_str->label,
6773                                    "%s: \"%s\"", name, AT_string (a));
6774           else
6775             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
6776           break;
6777
6778         default:
6779           abort ();
6780         }
6781     }
6782
6783   for (c = die->die_child; c != NULL; c = c->die_sib)
6784     output_die (c);
6785
6786   /* Add null byte to terminate sibling list.  */
6787   if (die->die_child != NULL)
6788     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
6789                          die->die_offset);
6790 }
6791
6792 /* Output the compilation unit that appears at the beginning of the
6793    .debug_info section, and precedes the DIE descriptions.  */
6794
6795 static void
6796 output_compilation_unit_header ()
6797 {
6798   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset - DWARF_OFFSET_SIZE,
6799                        "Length of Compilation Unit Info");
6800   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
6801   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
6802                          "Offset Into Abbrev. Section");
6803   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
6804 }
6805
6806 /* Output the compilation unit DIE and its children.  */
6807
6808 static void
6809 output_comp_unit (die, output_if_empty)
6810      dw_die_ref die;
6811      int output_if_empty;
6812 {
6813   const char *secname;
6814   char *oldsym, *tmp;
6815
6816   /* Unless we are outputting main CU, we may throw away empty ones.  */
6817   if (!output_if_empty && die->die_child == NULL)
6818     return;
6819
6820   /* Even if there are no children of this DIE, we must output the information
6821      about the compilation unit.  Otherwise, on an empty translation unit, we
6822      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
6823      will then complain when examining the file.  First mark all the DIEs in
6824      this CU so we know which get local refs.  */
6825   mark_dies (die);
6826
6827   build_abbrev_table (die);
6828
6829   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
6830   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
6831   calc_die_sizes (die);
6832
6833   oldsym = die->die_symbol;
6834   if (oldsym)
6835     {
6836       tmp = (char *) alloca (strlen (oldsym) + 24);
6837
6838       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
6839       secname = tmp;
6840       die->die_symbol = NULL;
6841     }
6842   else
6843     secname = (const char *) DEBUG_INFO_SECTION;
6844
6845   /* Output debugging information.  */
6846   named_section_flags (secname, SECTION_DEBUG);
6847   output_compilation_unit_header ();
6848   output_die (die);
6849
6850   /* Leave the marks on the main CU, so we can check them in
6851      output_pubnames.  */
6852   if (oldsym)
6853     {
6854       unmark_dies (die);
6855       die->die_symbol = oldsym;
6856     }
6857 }
6858
6859 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The
6860    output of lang_hooks.decl_printable_name for C++ looks like
6861    "A::f(int)".  Let's drop the argument list, and maybe the scope.  */
6862
6863 static const char *
6864 dwarf2_name (decl, scope)
6865      tree decl;
6866      int scope;
6867 {
6868   return (*lang_hooks.decl_printable_name) (decl, scope ? 1 : 0);
6869 }
6870
6871 /* Add a new entry to .debug_pubnames if appropriate.  */
6872
6873 static void
6874 add_pubname (decl, die)
6875      tree decl;
6876      dw_die_ref die;
6877 {
6878   pubname_ref p;
6879
6880   if (! TREE_PUBLIC (decl))
6881     return;
6882
6883   if (pubname_table_in_use == pubname_table_allocated)
6884     {
6885       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
6886       pubname_table
6887         = (pubname_ref) xrealloc (pubname_table,
6888                                   (pubname_table_allocated
6889                                    * sizeof (pubname_entry)));
6890     }
6891
6892   p = &pubname_table[pubname_table_in_use++];
6893   p->die = die;
6894   p->name = xstrdup (dwarf2_name (decl, 1));
6895 }
6896
6897 /* Output the public names table used to speed up access to externally
6898    visible names.  For now, only generate entries for externally
6899    visible procedures.  */
6900
6901 static void
6902 output_pubnames ()
6903 {
6904   unsigned i;
6905   unsigned long pubnames_length = size_of_pubnames ();
6906
6907   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
6908                        "Length of Public Names Info");
6909   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6910   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6911                          "Offset of Compilation Unit Info");
6912   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
6913                        "Compilation Unit Length");
6914
6915   for (i = 0; i < pubname_table_in_use; i++)
6916     {
6917       pubname_ref pub = &pubname_table[i];
6918
6919       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
6920       if (pub->die->die_mark == 0)
6921         abort ();
6922
6923       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
6924                            "DIE offset");
6925
6926       dw2_asm_output_nstring (pub->name, -1, "external name");
6927     }
6928
6929   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
6930 }
6931
6932 /* Add a new entry to .debug_aranges if appropriate.  */
6933
6934 static void
6935 add_arange (decl, die)
6936      tree decl;
6937      dw_die_ref die;
6938 {
6939   if (! DECL_SECTION_NAME (decl))
6940     return;
6941
6942   if (arange_table_in_use == arange_table_allocated)
6943     {
6944       arange_table_allocated += ARANGE_TABLE_INCREMENT;
6945       arange_table = (dw_die_ref *)
6946         xrealloc (arange_table, arange_table_allocated * sizeof (dw_die_ref));
6947     }
6948
6949   arange_table[arange_table_in_use++] = die;
6950 }
6951
6952 /* Output the information that goes into the .debug_aranges table.
6953    Namely, define the beginning and ending address range of the
6954    text section generated for this compilation unit.  */
6955
6956 static void
6957 output_aranges ()
6958 {
6959   unsigned i;
6960   unsigned long aranges_length = size_of_aranges ();
6961
6962   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
6963                        "Length of Address Ranges Info");
6964   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
6965   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
6966                          "Offset of Compilation Unit Info");
6967   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
6968   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
6969
6970   /* We need to align to twice the pointer size here.  */
6971   if (DWARF_ARANGES_PAD_SIZE)
6972     {
6973       /* Pad using a 2 byte words so that padding is correct for any
6974          pointer size.  */
6975       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
6976                            2 * DWARF2_ADDR_SIZE);
6977       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
6978         dw2_asm_output_data (2, 0, NULL);
6979     }
6980
6981   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
6982   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
6983                         text_section_label, "Length");
6984
6985   for (i = 0; i < arange_table_in_use; i++)
6986     {
6987       dw_die_ref die = arange_table[i];
6988
6989       /* We shouldn't see aranges for DIEs outside of the main CU.  */
6990       if (die->die_mark == 0)
6991         abort ();
6992
6993       if (die->die_tag == DW_TAG_subprogram)
6994         {
6995           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
6996                                "Address");
6997           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
6998                                 get_AT_low_pc (die), "Length");
6999         }
7000       else
7001         {
7002           /* A static variable; extract the symbol from DW_AT_location.
7003              Note that this code isn't currently hit, as we only emit
7004              aranges for functions (jason 9/23/99).  */
7005           dw_attr_ref a = get_AT (die, DW_AT_location);
7006           dw_loc_descr_ref loc;
7007
7008           if (! a || AT_class (a) != dw_val_class_loc)
7009             abort ();
7010
7011           loc = AT_loc (a);
7012           if (loc->dw_loc_opc != DW_OP_addr)
7013             abort ();
7014
7015           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7016                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7017           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7018                                get_AT_unsigned (die, DW_AT_byte_size),
7019                                "Length");
7020         }
7021     }
7022
7023   /* Output the terminator words.  */
7024   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7025   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7026 }
7027
7028 /* Add a new entry to .debug_ranges.  Return the offset at which it
7029    was placed.  */
7030
7031 static unsigned int
7032 add_ranges (block)
7033      tree block;
7034 {
7035   unsigned int in_use = ranges_table_in_use;
7036
7037   if (in_use == ranges_table_allocated)
7038     {
7039       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7040       ranges_table = (dw_ranges_ref)
7041         xrealloc (ranges_table, (ranges_table_allocated
7042                                  * sizeof (struct dw_ranges_struct)));
7043     }
7044
7045   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7046   ranges_table_in_use = in_use + 1;
7047
7048   return in_use * 2 * DWARF2_ADDR_SIZE;
7049 }
7050
7051 static void
7052 output_ranges ()
7053 {
7054   unsigned i;
7055   static const char *const start_fmt = "Offset 0x%x";
7056   const char *fmt = start_fmt;
7057
7058   for (i = 0; i < ranges_table_in_use; i++)
7059     {
7060       int block_num = ranges_table[i].block_num;
7061
7062       if (block_num)
7063         {
7064           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7065           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7066
7067           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7068           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7069
7070           /* If all code is in the text section, then the compilation
7071              unit base address defaults to DW_AT_low_pc, which is the
7072              base of the text section.  */
7073           if (separate_line_info_table_in_use == 0)
7074             {
7075               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7076                                     text_section_label,
7077                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7078               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7079                                     text_section_label, NULL);
7080             }
7081
7082           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7083              compilation unit base address to zero, which allows us to
7084              use absolute addresses, and not worry about whether the
7085              target supports cross-section arithmetic.  */
7086           else
7087             {
7088               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7089                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7090               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7091             }
7092
7093           fmt = NULL;
7094         }
7095       else
7096         {
7097           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7098           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7099           fmt = start_fmt;
7100         }
7101     }
7102 }
7103
7104 /* Data structure containing information about input files.  */
7105 struct file_info
7106 {
7107   char *path;           /* Complete file name.  */
7108   char *fname;          /* File name part.  */
7109   int length;           /* Length of entire string.  */
7110   int file_idx;         /* Index in input file table.  */
7111   int dir_idx;          /* Index in directory table.  */
7112 };
7113
7114 /* Data structure containing information about directories with source
7115    files.  */
7116 struct dir_info
7117 {
7118   char *path;           /* Path including directory name.  */
7119   int length;           /* Path length.  */
7120   int prefix;           /* Index of directory entry which is a prefix.  */
7121   int count;            /* Number of files in this directory.  */
7122   int dir_idx;          /* Index of directory used as base.  */
7123   int used;             /* Used in the end?  */
7124 };
7125
7126 /* Callback function for file_info comparison.  We sort by looking at
7127    the directories in the path.  */
7128
7129 static int
7130 file_info_cmp (p1, p2)
7131      const void *p1;
7132      const void *p2;
7133 {
7134   const struct file_info *s1 = p1;
7135   const struct file_info *s2 = p2;
7136   unsigned char *cp1;
7137   unsigned char *cp2;
7138
7139   /* Take care of file names without directories.  We need to make sure that
7140      we return consistent values to qsort since some will get confused if
7141      we return the same value when identical operands are passed in opposite
7142      orders.  So if neither has a directory, return 0 and otherwise return
7143      1 or -1 depending on which one has the directory.  */
7144   if ((s1->path == s1->fname || s2->path == s2->fname))
7145     return (s2->path == s2->fname) - (s1->path == s1->fname);
7146
7147   cp1 = (unsigned char *) s1->path;
7148   cp2 = (unsigned char *) s2->path;
7149
7150   while (1)
7151     {
7152       ++cp1;
7153       ++cp2;
7154       /* Reached the end of the first path?  If so, handle like above.  */
7155       if ((cp1 == (unsigned char *) s1->fname)
7156           || (cp2 == (unsigned char *) s2->fname))
7157         return ((cp2 == (unsigned char *) s2->fname)
7158                 - (cp1 == (unsigned char *) s1->fname));
7159
7160       /* Character of current path component the same?  */
7161       else if (*cp1 != *cp2)
7162         return *cp1 - *cp2;
7163     }
7164 }
7165
7166 /* Output the directory table and the file name table.  We try to minimize
7167    the total amount of memory needed.  A heuristic is used to avoid large
7168    slowdowns with many input files.  */
7169
7170 static void
7171 output_file_names ()
7172 {
7173   struct file_info *files;
7174   struct dir_info *dirs;
7175   int *saved;
7176   int *savehere;
7177   int *backmap;
7178   int ndirs;
7179   int idx_offset;
7180   int i;
7181   int idx;
7182
7183   /* Allocate the various arrays we need.  */
7184   files = (struct file_info *) alloca (file_table.in_use
7185                                        * sizeof (struct file_info));
7186   dirs = (struct dir_info *) alloca (file_table.in_use
7187                                      * sizeof (struct dir_info));
7188
7189   /* Sort the file names.  */
7190   for (i = 1; i < (int) file_table.in_use; i++)
7191     {
7192       char *f;
7193
7194       /* Skip all leading "./".  */
7195       f = file_table.table[i];
7196       while (f[0] == '.' && f[1] == '/')
7197         f += 2;
7198
7199       /* Create a new array entry.  */
7200       files[i].path = f;
7201       files[i].length = strlen (f);
7202       files[i].file_idx = i;
7203
7204       /* Search for the file name part.  */
7205       f = strrchr (f, '/');
7206       files[i].fname = f == NULL ? files[i].path : f + 1;
7207     }
7208
7209   qsort (files + 1, file_table.in_use - 1, sizeof (files[0]), file_info_cmp);
7210
7211   /* Find all the different directories used.  */
7212   dirs[0].path = files[1].path;
7213   dirs[0].length = files[1].fname - files[1].path;
7214   dirs[0].prefix = -1;
7215   dirs[0].count = 1;
7216   dirs[0].dir_idx = 0;
7217   dirs[0].used = 0;
7218   files[1].dir_idx = 0;
7219   ndirs = 1;
7220
7221   for (i = 2; i < (int) file_table.in_use; i++)
7222     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7223         && memcmp (dirs[ndirs - 1].path, files[i].path,
7224                    dirs[ndirs - 1].length) == 0)
7225       {
7226         /* Same directory as last entry.  */
7227         files[i].dir_idx = ndirs - 1;
7228         ++dirs[ndirs - 1].count;
7229       }
7230     else
7231       {
7232         int j;
7233
7234         /* This is a new directory.  */
7235         dirs[ndirs].path = files[i].path;
7236         dirs[ndirs].length = files[i].fname - files[i].path;
7237         dirs[ndirs].count = 1;
7238         dirs[ndirs].dir_idx = ndirs;
7239         dirs[ndirs].used = 0;
7240         files[i].dir_idx = ndirs;
7241
7242         /* Search for a prefix.  */
7243         dirs[ndirs].prefix = -1;
7244         for (j = 0; j < ndirs; j++)
7245           if (dirs[j].length < dirs[ndirs].length
7246               && dirs[j].length > 1
7247               && (dirs[ndirs].prefix == -1
7248                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7249               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7250             dirs[ndirs].prefix = j;
7251
7252         ++ndirs;
7253       }
7254
7255   /* Now to the actual work.  We have to find a subset of the directories which
7256      allow expressing the file name using references to the directory table
7257      with the least amount of characters.  We do not do an exhaustive search
7258      where we would have to check out every combination of every single
7259      possible prefix.  Instead we use a heuristic which provides nearly optimal
7260      results in most cases and never is much off.  */
7261   saved = (int *) alloca (ndirs * sizeof (int));
7262   savehere = (int *) alloca (ndirs * sizeof (int));
7263
7264   memset (saved, '\0', ndirs * sizeof (saved[0]));
7265   for (i = 0; i < ndirs; i++)
7266     {
7267       int j;
7268       int total;
7269
7270       /* We can always save some space for the current directory.  But this
7271          does not mean it will be enough to justify adding the directory.  */
7272       savehere[i] = dirs[i].length;
7273       total = (savehere[i] - saved[i]) * dirs[i].count;
7274
7275       for (j = i + 1; j < ndirs; j++)
7276         {
7277           savehere[j] = 0;
7278           if (saved[j] < dirs[i].length)
7279             {
7280               /* Determine whether the dirs[i] path is a prefix of the
7281                  dirs[j] path.  */
7282               int k;
7283
7284               k = dirs[j].prefix;
7285               while (k != -1 && k != i)
7286                 k = dirs[k].prefix;
7287
7288               if (k == i)
7289                 {
7290                   /* Yes it is.  We can possibly safe some memory but
7291                      writing the filenames in dirs[j] relative to
7292                      dirs[i].  */
7293                   savehere[j] = dirs[i].length;
7294                   total += (savehere[j] - saved[j]) * dirs[j].count;
7295                 }
7296             }
7297         }
7298
7299       /* Check whether we can safe enough to justify adding the dirs[i]
7300          directory.  */
7301       if (total > dirs[i].length + 1)
7302         {
7303           /* It's worthwhile adding.  */
7304           for (j = i; j < ndirs; j++)
7305             if (savehere[j] > 0)
7306               {
7307                 /* Remember how much we saved for this directory so far.  */
7308                 saved[j] = savehere[j];
7309
7310                 /* Remember the prefix directory.  */
7311                 dirs[j].dir_idx = i;
7312               }
7313         }
7314     }
7315
7316   /* We have to emit them in the order they appear in the file_table array
7317      since the index is used in the debug info generation.  To do this
7318      efficiently we generate a back-mapping of the indices first.  */
7319   backmap = (int *) alloca (file_table.in_use * sizeof (int));
7320   for (i = 1; i < (int) file_table.in_use; i++)
7321     {
7322       backmap[files[i].file_idx] = i;
7323
7324       /* Mark this directory as used.  */
7325       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7326     }
7327
7328   /* That was it.  We are ready to emit the information.  First emit the
7329      directory name table.  We have to make sure the first actually emitted
7330      directory name has index one; zero is reserved for the current working
7331      directory.  Make sure we do not confuse these indices with the one for the
7332      constructed table (even though most of the time they are identical).  */
7333   idx = 1;
7334   idx_offset = dirs[0].length > 0 ? 1 : 0;
7335   for (i = 1 - idx_offset; i < ndirs; i++)
7336     if (dirs[i].used != 0)
7337       {
7338         dirs[i].used = idx++;
7339         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7340                                 "Directory Entry: 0x%x", dirs[i].used);
7341       }
7342
7343   dw2_asm_output_data (1, 0, "End directory table");
7344
7345   /* Correct the index for the current working directory entry if it
7346      exists.  */
7347   if (idx_offset == 0)
7348     dirs[0].used = 0;
7349
7350   /* Now write all the file names.  */
7351   for (i = 1; i < (int) file_table.in_use; i++)
7352     {
7353       int file_idx = backmap[i];
7354       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7355
7356       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7357                               "File Entry: 0x%x", i);
7358
7359       /* Include directory index.  */
7360       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7361
7362       /* Modification time.  */
7363       dw2_asm_output_data_uleb128 (0, NULL);
7364
7365       /* File length in bytes.  */
7366       dw2_asm_output_data_uleb128 (0, NULL);
7367     }
7368
7369   dw2_asm_output_data (1, 0, "End file name table");
7370 }
7371
7372
7373 /* Output the source line number correspondence information.  This
7374    information goes into the .debug_line section.  */
7375
7376 static void
7377 output_line_info ()
7378 {
7379   char l1[20], l2[20], p1[20], p2[20];
7380   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7381   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7382   unsigned opc;
7383   unsigned n_op_args;
7384   unsigned long lt_index;
7385   unsigned long current_line;
7386   long line_offset;
7387   long line_delta;
7388   unsigned long current_file;
7389   unsigned long function;
7390
7391   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7392   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7393   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7394   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7395
7396   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7397                         "Length of Source Line Info");
7398   ASM_OUTPUT_LABEL (asm_out_file, l1);
7399
7400   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7401   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7402   ASM_OUTPUT_LABEL (asm_out_file, p1);
7403
7404   /* Define the architecture-dependent minimum instruction length (in
7405    bytes).  In this implementation of DWARF, this field is used for
7406    information purposes only.  Since GCC generates assembly language,
7407    we have no a priori knowledge of how many instruction bytes are
7408    generated for each source line, and therefore can use only the
7409    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7410    commands.  Accordingly, we fix this as `1', which is "correct
7411    enough" for all architectures, and don't let the target override.  */
7412   dw2_asm_output_data (1, 1,
7413                        "Minimum Instruction Length");
7414
7415   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7416                        "Default is_stmt_start flag");
7417   dw2_asm_output_data (1, DWARF_LINE_BASE,
7418                        "Line Base Value (Special Opcodes)");
7419   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7420                        "Line Range Value (Special Opcodes)");
7421   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7422                        "Special Opcode Base");
7423
7424   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7425     {
7426       switch (opc)
7427         {
7428         case DW_LNS_advance_pc:
7429         case DW_LNS_advance_line:
7430         case DW_LNS_set_file:
7431         case DW_LNS_set_column:
7432         case DW_LNS_fixed_advance_pc:
7433           n_op_args = 1;
7434           break;
7435         default:
7436           n_op_args = 0;
7437           break;
7438         }
7439
7440       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7441                            opc, n_op_args);
7442     }
7443
7444   /* Write out the information about the files we use.  */
7445   output_file_names ();
7446   ASM_OUTPUT_LABEL (asm_out_file, p2);
7447
7448   /* We used to set the address register to the first location in the text
7449      section here, but that didn't accomplish anything since we already
7450      have a line note for the opening brace of the first function.  */
7451
7452   /* Generate the line number to PC correspondence table, encoded as
7453      a series of state machine operations.  */
7454   current_file = 1;
7455   current_line = 1;
7456   strcpy (prev_line_label, text_section_label);
7457   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7458     {
7459       dw_line_info_ref line_info = &line_info_table[lt_index];
7460
7461 #if 0
7462       /* Disable this optimization for now; GDB wants to see two line notes
7463          at the beginning of a function so it can find the end of the
7464          prologue.  */
7465
7466       /* Don't emit anything for redundant notes.  Just updating the
7467          address doesn't accomplish anything, because we already assume
7468          that anything after the last address is this line.  */
7469       if (line_info->dw_line_num == current_line
7470           && line_info->dw_file_num == current_file)
7471         continue;
7472 #endif
7473
7474       /* Emit debug info for the address of the current line.
7475
7476          Unfortunately, we have little choice here currently, and must always
7477          use the most general form.  GCC does not know the address delta
7478          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7479          attributes which will give an upper bound on the address range.  We
7480          could perhaps use length attributes to determine when it is safe to
7481          use DW_LNS_fixed_advance_pc.  */
7482
7483       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7484       if (0)
7485         {
7486           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7487           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7488                                "DW_LNS_fixed_advance_pc");
7489           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7490         }
7491       else
7492         {
7493           /* This can handle any delta.  This takes
7494              4+DWARF2_ADDR_SIZE bytes.  */
7495           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7496           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7497           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7498           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7499         }
7500
7501       strcpy (prev_line_label, line_label);
7502
7503       /* Emit debug info for the source file of the current line, if
7504          different from the previous line.  */
7505       if (line_info->dw_file_num != current_file)
7506         {
7507           current_file = line_info->dw_file_num;
7508           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7509           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7510                                        file_table.table[current_file]);
7511         }
7512
7513       /* Emit debug info for the current line number, choosing the encoding
7514          that uses the least amount of space.  */
7515       if (line_info->dw_line_num != current_line)
7516         {
7517           line_offset = line_info->dw_line_num - current_line;
7518           line_delta = line_offset - DWARF_LINE_BASE;
7519           current_line = line_info->dw_line_num;
7520           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7521             /* This can handle deltas from -10 to 234, using the current
7522                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7523                takes 1 byte.  */
7524             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7525                                  "line %lu", current_line);
7526           else
7527             {
7528               /* This can handle any delta.  This takes at least 4 bytes,
7529                  depending on the value being encoded.  */
7530               dw2_asm_output_data (1, DW_LNS_advance_line,
7531                                    "advance to line %lu", current_line);
7532               dw2_asm_output_data_sleb128 (line_offset, NULL);
7533               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7534             }
7535         }
7536       else
7537         /* We still need to start a new row, so output a copy insn.  */
7538         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7539     }
7540
7541   /* Emit debug info for the address of the end of the function.  */
7542   if (0)
7543     {
7544       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7545                            "DW_LNS_fixed_advance_pc");
7546       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7547     }
7548   else
7549     {
7550       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7551       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7552       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7553       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7554     }
7555
7556   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7557   dw2_asm_output_data_uleb128 (1, NULL);
7558   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7559
7560   function = 0;
7561   current_file = 1;
7562   current_line = 1;
7563   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7564     {
7565       dw_separate_line_info_ref line_info
7566         = &separate_line_info_table[lt_index];
7567
7568 #if 0
7569       /* Don't emit anything for redundant notes.  */
7570       if (line_info->dw_line_num == current_line
7571           && line_info->dw_file_num == current_file
7572           && line_info->function == function)
7573         goto cont;
7574 #endif
7575
7576       /* Emit debug info for the address of the current line.  If this is
7577          a new function, or the first line of a function, then we need
7578          to handle it differently.  */
7579       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7580                                    lt_index);
7581       if (function != line_info->function)
7582         {
7583           function = line_info->function;
7584
7585           /* Set the address register to the first line in the function */
7586           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7587           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7588           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7589           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7590         }
7591       else
7592         {
7593           /* ??? See the DW_LNS_advance_pc comment above.  */
7594           if (0)
7595             {
7596               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7597                                    "DW_LNS_fixed_advance_pc");
7598               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7599             }
7600           else
7601             {
7602               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7603               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7604               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7605               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7606             }
7607         }
7608
7609       strcpy (prev_line_label, line_label);
7610
7611       /* Emit debug info for the source file of the current line, if
7612          different from the previous line.  */
7613       if (line_info->dw_file_num != current_file)
7614         {
7615           current_file = line_info->dw_file_num;
7616           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7617           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7618                                        file_table.table[current_file]);
7619         }
7620
7621       /* Emit debug info for the current line number, choosing the encoding
7622          that uses the least amount of space.  */
7623       if (line_info->dw_line_num != current_line)
7624         {
7625           line_offset = line_info->dw_line_num - current_line;
7626           line_delta = line_offset - DWARF_LINE_BASE;
7627           current_line = line_info->dw_line_num;
7628           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7629             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7630                                  "line %lu", current_line);
7631           else
7632             {
7633               dw2_asm_output_data (1, DW_LNS_advance_line,
7634                                    "advance to line %lu", current_line);
7635               dw2_asm_output_data_sleb128 (line_offset, NULL);
7636               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7637             }
7638         }
7639       else
7640         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7641
7642 #if 0
7643     cont:
7644 #endif
7645
7646       lt_index++;
7647
7648       /* If we're done with a function, end its sequence.  */
7649       if (lt_index == separate_line_info_table_in_use
7650           || separate_line_info_table[lt_index].function != function)
7651         {
7652           current_file = 1;
7653           current_line = 1;
7654
7655           /* Emit debug info for the address of the end of the function.  */
7656           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
7657           if (0)
7658             {
7659               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7660                                    "DW_LNS_fixed_advance_pc");
7661               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7662             }
7663           else
7664             {
7665               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7666               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7667               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7668               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7669             }
7670
7671           /* Output the marker for the end of this sequence.  */
7672           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7673           dw2_asm_output_data_uleb128 (1, NULL);
7674           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7675         }
7676     }
7677
7678   /* Output the marker for the end of the line number info.  */
7679   ASM_OUTPUT_LABEL (asm_out_file, l2);
7680 }
7681 \f
7682 /* Given a pointer to a tree node for some base type, return a pointer to
7683    a DIE that describes the given type.
7684
7685    This routine must only be called for GCC type nodes that correspond to
7686    Dwarf base (fundamental) types.  */
7687
7688 static dw_die_ref
7689 base_type_die (type)
7690      tree type;
7691 {
7692   dw_die_ref base_type_result;
7693   const char *type_name;
7694   enum dwarf_type encoding;
7695   tree name = TYPE_NAME (type);
7696
7697   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
7698     return 0;
7699
7700   if (name)
7701     {
7702       if (TREE_CODE (name) == TYPE_DECL)
7703         name = DECL_NAME (name);
7704
7705       type_name = IDENTIFIER_POINTER (name);
7706     }
7707   else
7708     type_name = "__unknown__";
7709
7710   switch (TREE_CODE (type))
7711     {
7712     case INTEGER_TYPE:
7713       /* Carefully distinguish the C character types, without messing
7714          up if the language is not C. Note that we check only for the names
7715          that contain spaces; other names might occur by coincidence in other
7716          languages.  */
7717       if (! (TYPE_PRECISION (type) == CHAR_TYPE_SIZE
7718              && (type == char_type_node
7719                  || ! strcmp (type_name, "signed char")
7720                  || ! strcmp (type_name, "unsigned char"))))
7721         {
7722           if (TREE_UNSIGNED (type))
7723             encoding = DW_ATE_unsigned;
7724           else
7725             encoding = DW_ATE_signed;
7726           break;
7727         }
7728       /* else fall through.  */
7729
7730     case CHAR_TYPE:
7731       /* GNU Pascal/Ada CHAR type.  Not used in C.  */
7732       if (TREE_UNSIGNED (type))
7733         encoding = DW_ATE_unsigned_char;
7734       else
7735         encoding = DW_ATE_signed_char;
7736       break;
7737
7738     case REAL_TYPE:
7739       encoding = DW_ATE_float;
7740       break;
7741
7742       /* Dwarf2 doesn't know anything about complex ints, so use
7743          a user defined type for it.  */
7744     case COMPLEX_TYPE:
7745       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
7746         encoding = DW_ATE_complex_float;
7747       else
7748         encoding = DW_ATE_lo_user;
7749       break;
7750
7751     case BOOLEAN_TYPE:
7752       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
7753       encoding = DW_ATE_boolean;
7754       break;
7755
7756     default:
7757       /* No other TREE_CODEs are Dwarf fundamental types.  */
7758       abort ();
7759     }
7760
7761   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
7762   if (demangle_name_func)
7763     type_name = (*demangle_name_func) (type_name);
7764
7765   add_AT_string (base_type_result, DW_AT_name, type_name);
7766   add_AT_unsigned (base_type_result, DW_AT_byte_size,
7767                    int_size_in_bytes (type));
7768   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
7769
7770   return base_type_result;
7771 }
7772
7773 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
7774    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
7775    a given type is generally the same as the given type, except that if the
7776    given type is a pointer or reference type, then the root type of the given
7777    type is the root type of the "basis" type for the pointer or reference
7778    type.  (This definition of the "root" type is recursive.) Also, the root
7779    type of a `const' qualified type or a `volatile' qualified type is the
7780    root type of the given type without the qualifiers.  */
7781
7782 static tree
7783 root_type (type)
7784      tree type;
7785 {
7786   if (TREE_CODE (type) == ERROR_MARK)
7787     return error_mark_node;
7788
7789   switch (TREE_CODE (type))
7790     {
7791     case ERROR_MARK:
7792       return error_mark_node;
7793
7794     case POINTER_TYPE:
7795     case REFERENCE_TYPE:
7796       return type_main_variant (root_type (TREE_TYPE (type)));
7797
7798     default:
7799       return type_main_variant (type);
7800     }
7801 }
7802
7803 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
7804    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
7805
7806 static inline int
7807 is_base_type (type)
7808      tree type;
7809 {
7810   switch (TREE_CODE (type))
7811     {
7812     case ERROR_MARK:
7813     case VOID_TYPE:
7814     case INTEGER_TYPE:
7815     case REAL_TYPE:
7816     case COMPLEX_TYPE:
7817     case BOOLEAN_TYPE:
7818     case CHAR_TYPE:
7819       return 1;
7820
7821     case SET_TYPE:
7822     case ARRAY_TYPE:
7823     case RECORD_TYPE:
7824     case UNION_TYPE:
7825     case QUAL_UNION_TYPE:
7826     case ENUMERAL_TYPE:
7827     case FUNCTION_TYPE:
7828     case METHOD_TYPE:
7829     case POINTER_TYPE:
7830     case REFERENCE_TYPE:
7831     case FILE_TYPE:
7832     case OFFSET_TYPE:
7833     case LANG_TYPE:
7834     case VECTOR_TYPE:
7835       return 0;
7836
7837     default:
7838       abort ();
7839     }
7840
7841   return 0;
7842 }
7843
7844 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
7845    node, return the size in bits for the type if it is a constant, or else
7846    return the alignment for the type if the type's size is not constant, or
7847    else return BITS_PER_WORD if the type actually turns out to be an
7848    ERROR_MARK node.  */
7849
7850 static inline unsigned HOST_WIDE_INT
7851 simple_type_size_in_bits (type)
7852      tree type;
7853 {
7854
7855   if (TREE_CODE (type) == ERROR_MARK)
7856     return BITS_PER_WORD;
7857   else if (TYPE_SIZE (type) == NULL_TREE)
7858     return 0;
7859   else if (host_integerp (TYPE_SIZE (type), 1))
7860     return tree_low_cst (TYPE_SIZE (type), 1);
7861   else
7862     return TYPE_ALIGN (type);
7863 }
7864
7865 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
7866    entry that chains various modifiers in front of the given type.  */
7867
7868 static dw_die_ref
7869 modified_type_die (type, is_const_type, is_volatile_type, context_die)
7870      tree type;
7871      int is_const_type;
7872      int is_volatile_type;
7873      dw_die_ref context_die;
7874 {
7875   enum tree_code code = TREE_CODE (type);
7876   dw_die_ref mod_type_die = NULL;
7877   dw_die_ref sub_die = NULL;
7878   tree item_type = NULL;
7879
7880   if (code != ERROR_MARK)
7881     {
7882       tree qualified_type;
7883
7884       /* See if we already have the appropriately qualified variant of
7885          this type.  */
7886       qualified_type
7887         = get_qualified_type (type,
7888                               ((is_const_type ? TYPE_QUAL_CONST : 0)
7889                                | (is_volatile_type
7890                                   ? TYPE_QUAL_VOLATILE : 0)));
7891
7892       /* If we do, then we can just use its DIE, if it exists.  */
7893       if (qualified_type)
7894         {
7895           mod_type_die = lookup_type_die (qualified_type);
7896           if (mod_type_die)
7897             return mod_type_die;
7898         }
7899
7900       /* Handle C typedef types.  */
7901       if (qualified_type && TYPE_NAME (qualified_type)
7902           && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL
7903           && DECL_ORIGINAL_TYPE (TYPE_NAME (qualified_type)))
7904         {
7905           tree type_name = TYPE_NAME (qualified_type);
7906           tree dtype = TREE_TYPE (type_name);
7907
7908           if (qualified_type == dtype)
7909             {
7910               /* For a named type, use the typedef.  */
7911               gen_type_die (qualified_type, context_die);
7912               mod_type_die = lookup_type_die (qualified_type);
7913             }
7914           else if (is_const_type < TYPE_READONLY (dtype)
7915                    || is_volatile_type < TYPE_VOLATILE (dtype))
7916             /* cv-unqualified version of named type.  Just use the unnamed
7917                type to which it refers.  */
7918             mod_type_die
7919               = modified_type_die (DECL_ORIGINAL_TYPE (type_name),
7920                                    is_const_type, is_volatile_type,
7921                                    context_die);
7922
7923           /* Else cv-qualified version of named type; fall through.  */
7924         }
7925
7926       if (mod_type_die)
7927         /* OK.  */
7928         ;
7929       else if (is_const_type)
7930         {
7931           mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
7932           sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
7933         }
7934       else if (is_volatile_type)
7935         {
7936           mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
7937           sub_die = modified_type_die (type, 0, 0, context_die);
7938         }
7939       else if (code == POINTER_TYPE)
7940         {
7941           mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
7942           add_AT_unsigned (mod_type_die, DW_AT_byte_size,
7943                            simple_type_size_in_bits (type) / BITS_PER_UNIT);
7944 #if 0
7945           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7946 #endif
7947           item_type = TREE_TYPE (type);
7948         }
7949       else if (code == REFERENCE_TYPE)
7950         {
7951           mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
7952           add_AT_unsigned (mod_type_die, DW_AT_byte_size,
7953                            simple_type_size_in_bits (type) / BITS_PER_UNIT);
7954 #if 0
7955           add_AT_unsigned (mod_type_die, DW_AT_address_class, 0);
7956 #endif
7957           item_type = TREE_TYPE (type);
7958         }
7959       else if (is_base_type (type))
7960         mod_type_die = base_type_die (type);
7961       else
7962         {
7963           gen_type_die (type, context_die);
7964
7965           /* We have to get the type_main_variant here (and pass that to the
7966              `lookup_type_die' routine) because the ..._TYPE node we have
7967              might simply be a *copy* of some original type node (where the
7968              copy was created to help us keep track of typedef names) and
7969              that copy might have a different TYPE_UID from the original
7970              ..._TYPE node.  */
7971           if (TREE_CODE (type) != VECTOR_TYPE)
7972             mod_type_die = lookup_type_die (type_main_variant (type));
7973           else
7974             /* Vectors have the debugging information in the type,
7975                not the main variant.  */
7976             mod_type_die = lookup_type_die (type);
7977           if (mod_type_die == NULL)
7978             abort ();
7979         }
7980
7981       /* We want to equate the qualified type to the die below.  */
7982       type = qualified_type;
7983     }
7984
7985   if (type)
7986     equate_type_number_to_die (type, mod_type_die);
7987   if (item_type)
7988     /* We must do this after the equate_type_number_to_die call, in case
7989        this is a recursive type.  This ensures that the modified_type_die
7990        recursion will terminate even if the type is recursive.  Recursive
7991        types are possible in Ada.  */
7992     sub_die = modified_type_die (item_type,
7993                                  TYPE_READONLY (item_type),
7994                                  TYPE_VOLATILE (item_type),
7995                                  context_die);
7996
7997   if (sub_die != NULL)
7998     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
7999
8000   return mod_type_die;
8001 }
8002
8003 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8004    an enumerated type.  */
8005
8006 static inline int
8007 type_is_enum (type)
8008      tree type;
8009 {
8010   return TREE_CODE (type) == ENUMERAL_TYPE;
8011 }
8012
8013 /* Return the register number described by a given RTL node.  */
8014
8015 static unsigned int
8016 reg_number (rtl)
8017      rtx rtl;
8018 {
8019   unsigned regno = REGNO (rtl);
8020
8021   if (regno >= FIRST_PSEUDO_REGISTER)
8022     abort ();
8023
8024   return DBX_REGISTER_NUMBER (regno);
8025 }
8026
8027 /* Return a location descriptor that designates a machine register or
8028    zero if there is no such.  */
8029
8030 static dw_loc_descr_ref
8031 reg_loc_descriptor (rtl)
8032      rtx rtl;
8033 {
8034   dw_loc_descr_ref loc_result = NULL;
8035   unsigned reg;
8036
8037   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8038     return 0;
8039
8040   reg = reg_number (rtl);
8041   if (reg <= 31)
8042     loc_result = new_loc_descr (DW_OP_reg0 + reg, 0, 0);
8043   else
8044     loc_result = new_loc_descr (DW_OP_regx, reg, 0);
8045
8046   return loc_result;
8047 }
8048
8049 /* Return a location descriptor that designates a constant.  */
8050
8051 static dw_loc_descr_ref
8052 int_loc_descriptor (i)
8053      HOST_WIDE_INT i;
8054 {
8055   enum dwarf_location_atom op;
8056
8057   /* Pick the smallest representation of a constant, rather than just
8058      defaulting to the LEB encoding.  */
8059   if (i >= 0)
8060     {
8061       if (i <= 31)
8062         op = DW_OP_lit0 + i;
8063       else if (i <= 0xff)
8064         op = DW_OP_const1u;
8065       else if (i <= 0xffff)
8066         op = DW_OP_const2u;
8067       else if (HOST_BITS_PER_WIDE_INT == 32
8068                || i <= 0xffffffff)
8069         op = DW_OP_const4u;
8070       else
8071         op = DW_OP_constu;
8072     }
8073   else
8074     {
8075       if (i >= -0x80)
8076         op = DW_OP_const1s;
8077       else if (i >= -0x8000)
8078         op = DW_OP_const2s;
8079       else if (HOST_BITS_PER_WIDE_INT == 32
8080                || i >= -0x80000000)
8081         op = DW_OP_const4s;
8082       else
8083         op = DW_OP_consts;
8084     }
8085
8086   return new_loc_descr (op, i, 0);
8087 }
8088
8089 /* Return a location descriptor that designates a base+offset location.  */
8090
8091 static dw_loc_descr_ref
8092 based_loc_descr (reg, offset)
8093      unsigned reg;
8094      long int offset;
8095 {
8096   dw_loc_descr_ref loc_result;
8097   /* For the "frame base", we use the frame pointer or stack pointer
8098      registers, since the RTL for local variables is relative to one of
8099      them.  */
8100   unsigned fp_reg = DBX_REGISTER_NUMBER (frame_pointer_needed
8101                                          ? HARD_FRAME_POINTER_REGNUM
8102                                          : STACK_POINTER_REGNUM);
8103
8104   if (reg == fp_reg)
8105     loc_result = new_loc_descr (DW_OP_fbreg, offset, 0);
8106   else if (reg <= 31)
8107     loc_result = new_loc_descr (DW_OP_breg0 + reg, offset, 0);
8108   else
8109     loc_result = new_loc_descr (DW_OP_bregx, reg, offset);
8110
8111   return loc_result;
8112 }
8113
8114 /* Return true if this RTL expression describes a base+offset calculation.  */
8115
8116 static inline int
8117 is_based_loc (rtl)
8118      rtx rtl;
8119 {
8120   return (GET_CODE (rtl) == PLUS
8121           && ((GET_CODE (XEXP (rtl, 0)) == REG
8122                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8123                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8124 }
8125
8126 /* The following routine converts the RTL for a variable or parameter
8127    (resident in memory) into an equivalent Dwarf representation of a
8128    mechanism for getting the address of that same variable onto the top of a
8129    hypothetical "address evaluation" stack.
8130
8131    When creating memory location descriptors, we are effectively transforming
8132    the RTL for a memory-resident object into its Dwarf postfix expression
8133    equivalent.  This routine recursively descends an RTL tree, turning
8134    it into Dwarf postfix code as it goes.
8135
8136    MODE is the mode of the memory reference, needed to handle some
8137    autoincrement addressing modes.
8138
8139    Return 0 if we can't represent the location.  */
8140
8141 static dw_loc_descr_ref
8142 mem_loc_descriptor (rtl, mode)
8143      rtx rtl;
8144      enum machine_mode mode;
8145 {
8146   dw_loc_descr_ref mem_loc_result = NULL;
8147
8148   /* Note that for a dynamically sized array, the location we will generate a
8149      description of here will be the lowest numbered location which is
8150      actually within the array.  That's *not* necessarily the same as the
8151      zeroth element of the array.  */
8152
8153 #ifdef ASM_SIMPLIFY_DWARF_ADDR
8154   rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
8155 #endif
8156
8157   switch (GET_CODE (rtl))
8158     {
8159     case POST_INC:
8160     case POST_DEC:
8161     case POST_MODIFY:
8162       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8163          just fall into the SUBREG code.  */
8164
8165       /* ... fall through ...  */
8166
8167     case SUBREG:
8168       /* The case of a subreg may arise when we have a local (register)
8169          variable or a formal (register) parameter which doesn't quite fill
8170          up an entire register.  For now, just assume that it is
8171          legitimate to make the Dwarf info refer to the whole register which
8172          contains the given subreg.  */
8173       rtl = SUBREG_REG (rtl);
8174
8175       /* ... fall through ...  */
8176
8177     case REG:
8178       /* Whenever a register number forms a part of the description of the
8179          method for calculating the (dynamic) address of a memory resident
8180          object, DWARF rules require the register number be referred to as
8181          a "base register".  This distinction is not based in any way upon
8182          what category of register the hardware believes the given register
8183          belongs to.  This is strictly DWARF terminology we're dealing with
8184          here. Note that in cases where the location of a memory-resident
8185          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8186          OP_CONST (0)) the actual DWARF location descriptor that we generate
8187          may just be OP_BASEREG (basereg).  This may look deceptively like
8188          the object in question was allocated to a register (rather than in
8189          memory) so DWARF consumers need to be aware of the subtle
8190          distinction between OP_REG and OP_BASEREG.  */
8191       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8192         mem_loc_result = based_loc_descr (reg_number (rtl), 0);
8193       break;
8194
8195     case MEM:
8196       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8197       if (mem_loc_result != 0)
8198         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8199       break;
8200
8201     case LABEL_REF:
8202       /* Some ports can transform a symbol ref into a label ref, because
8203          the symbol ref is too far away and has to be dumped into a constant
8204          pool.  */
8205     case CONST:
8206     case SYMBOL_REF:
8207       /* Alternatively, the symbol in the constant pool might be referenced
8208          by a different symbol.  */
8209       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8210         {
8211           bool marked;
8212           rtx tmp = get_pool_constant_mark (rtl, &marked);
8213
8214           if (GET_CODE (tmp) == SYMBOL_REF)
8215             {
8216               rtl = tmp;
8217               if (CONSTANT_POOL_ADDRESS_P (tmp))
8218                 get_pool_constant_mark (tmp, &marked);
8219               else
8220                 marked = true;
8221             }
8222
8223           /* If all references to this pool constant were optimized away,
8224              it was not output and thus we can't represent it.
8225              FIXME: might try to use DW_OP_const_value here, though
8226              DW_OP_piece complicates it.  */
8227           if (!marked)
8228             return 0;
8229         }
8230
8231       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8232       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8233       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8234       VARRAY_PUSH_RTX (used_rtx_varray, rtl);
8235       break;
8236
8237     case PRE_MODIFY:
8238       /* Extract the PLUS expression nested inside and fall into
8239          PLUS code below.  */
8240       rtl = XEXP (rtl, 1);
8241       goto plus;
8242
8243     case PRE_INC:
8244     case PRE_DEC:
8245       /* Turn these into a PLUS expression and fall into the PLUS code
8246          below.  */
8247       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8248                           GEN_INT (GET_CODE (rtl) == PRE_INC
8249                                    ? GET_MODE_UNIT_SIZE (mode)
8250                                    : -GET_MODE_UNIT_SIZE (mode)));
8251
8252       /* ... fall through ...  */
8253
8254     case PLUS:
8255     plus:
8256       if (is_based_loc (rtl))
8257         mem_loc_result = based_loc_descr (reg_number (XEXP (rtl, 0)),
8258                                           INTVAL (XEXP (rtl, 1)));
8259       else
8260         {
8261           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8262           if (mem_loc_result == 0)
8263             break;
8264
8265           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8266               && INTVAL (XEXP (rtl, 1)) >= 0)
8267             add_loc_descr (&mem_loc_result,
8268                            new_loc_descr (DW_OP_plus_uconst,
8269                                           INTVAL (XEXP (rtl, 1)), 0));
8270           else
8271             {
8272               add_loc_descr (&mem_loc_result,
8273                              mem_loc_descriptor (XEXP (rtl, 1), mode));
8274               add_loc_descr (&mem_loc_result,
8275                              new_loc_descr (DW_OP_plus, 0, 0));
8276             }
8277         }
8278       break;
8279
8280     case MULT:
8281       {
8282         /* If a pseudo-reg is optimized away, it is possible for it to
8283            be replaced with a MEM containing a multiply.  */
8284         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8285         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8286
8287         if (op0 == 0 || op1 == 0)
8288           break;
8289
8290         mem_loc_result = op0;
8291         add_loc_descr (&mem_loc_result, op1);
8292         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
8293         break;
8294       }
8295
8296     case CONST_INT:
8297       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8298       break;
8299
8300     case ADDRESSOF:
8301       /* If this is a MEM, return its address.  Otherwise, we can't
8302          represent this.  */
8303       if (GET_CODE (XEXP (rtl, 0)) == MEM)
8304         return mem_loc_descriptor (XEXP (XEXP (rtl, 0), 0), mode);
8305       else
8306         return 0;
8307
8308     default:
8309       abort ();
8310     }
8311
8312   return mem_loc_result;
8313 }
8314
8315 /* Return a descriptor that describes the concatenation of two locations.
8316    This is typically a complex variable.  */
8317
8318 static dw_loc_descr_ref
8319 concat_loc_descriptor (x0, x1)
8320      rtx x0, x1;
8321 {
8322   dw_loc_descr_ref cc_loc_result = NULL;
8323   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8324   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8325
8326   if (x0_ref == 0 || x1_ref == 0)
8327     return 0;
8328
8329   cc_loc_result = x0_ref;
8330   add_loc_descr (&cc_loc_result,
8331                  new_loc_descr (DW_OP_piece,
8332                                 GET_MODE_SIZE (GET_MODE (x0)), 0));
8333
8334   add_loc_descr (&cc_loc_result, x1_ref);
8335   add_loc_descr (&cc_loc_result,
8336                  new_loc_descr (DW_OP_piece,
8337                                 GET_MODE_SIZE (GET_MODE (x1)), 0));
8338
8339   return cc_loc_result;
8340 }
8341
8342 /* Output a proper Dwarf location descriptor for a variable or parameter
8343    which is either allocated in a register or in a memory location.  For a
8344    register, we just generate an OP_REG and the register number.  For a
8345    memory location we provide a Dwarf postfix expression describing how to
8346    generate the (dynamic) address of the object onto the address stack.
8347
8348    If we don't know how to describe it, return 0.  */
8349
8350 static dw_loc_descr_ref
8351 loc_descriptor (rtl)
8352      rtx rtl;
8353 {
8354   dw_loc_descr_ref loc_result = NULL;
8355
8356   switch (GET_CODE (rtl))
8357     {
8358     case SUBREG:
8359       /* The case of a subreg may arise when we have a local (register)
8360          variable or a formal (register) parameter which doesn't quite fill
8361          up an entire register.  For now, just assume that it is
8362          legitimate to make the Dwarf info refer to the whole register which
8363          contains the given subreg.  */
8364       rtl = SUBREG_REG (rtl);
8365
8366       /* ... fall through ...  */
8367
8368     case REG:
8369       loc_result = reg_loc_descriptor (rtl);
8370       break;
8371
8372     case MEM:
8373       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8374       break;
8375
8376     case CONCAT:
8377       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8378       break;
8379
8380     default:
8381       abort ();
8382     }
8383
8384   return loc_result;
8385 }
8386
8387 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
8388    up particularly with variable length arrays.  If ADDRESSP is nonzero, we are
8389    looking for an address.  Otherwise, we return a value.  If we can't make a
8390    descriptor, return 0.  */
8391
8392 static dw_loc_descr_ref
8393 loc_descriptor_from_tree (loc, addressp)
8394      tree loc;
8395      int addressp;
8396 {
8397   dw_loc_descr_ref ret, ret1;
8398   int indirect_p = 0;
8399   int unsignedp = TREE_UNSIGNED (TREE_TYPE (loc));
8400   enum dwarf_location_atom op;
8401
8402   /* ??? Most of the time we do not take proper care for sign/zero
8403      extending the values properly.  Hopefully this won't be a real
8404      problem...  */
8405
8406   switch (TREE_CODE (loc))
8407     {
8408     case ERROR_MARK:
8409       return 0;
8410
8411     case WITH_RECORD_EXPR:
8412     case PLACEHOLDER_EXPR:
8413       /* This case involves extracting fields from an object to determine the
8414          position of other fields.  We don't try to encode this here.  The
8415          only user of this is Ada, which encodes the needed information using
8416          the names of types.  */
8417       return 0;
8418
8419     case CALL_EXPR:
8420       return 0;
8421
8422     case ADDR_EXPR:
8423       /* We can support this only if we can look through conversions and
8424          find an INDIRECT_EXPR.  */
8425       for (loc = TREE_OPERAND (loc, 0);
8426            TREE_CODE (loc) == CONVERT_EXPR || TREE_CODE (loc) == NOP_EXPR
8427            || TREE_CODE (loc) == NON_LVALUE_EXPR
8428            || TREE_CODE (loc) == VIEW_CONVERT_EXPR
8429            || TREE_CODE (loc) == SAVE_EXPR;
8430            loc = TREE_OPERAND (loc, 0))
8431         ;
8432
8433        return (TREE_CODE (loc) == INDIRECT_REF
8434                ? loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp)
8435                : 0);
8436
8437     case VAR_DECL:
8438       if (DECL_THREAD_LOCAL (loc))
8439         {
8440           rtx rtl;
8441
8442 #ifndef ASM_OUTPUT_DWARF_DTPREL
8443           /* If this is not defined, we have no way to emit the data.  */
8444           return 0;
8445 #endif
8446
8447           /* The way DW_OP_GNU_push_tls_address is specified, we can only
8448              look up addresses of objects in the current module.  */
8449           if (DECL_EXTERNAL (loc))
8450             return 0;
8451
8452           rtl = rtl_for_decl_location (loc);
8453           if (rtl == NULL_RTX)
8454             return 0;
8455
8456           if (GET_CODE (rtl) != MEM)
8457             return 0;
8458           rtl = XEXP (rtl, 0);
8459           if (! CONSTANT_P (rtl))
8460             return 0;
8461
8462           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
8463           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8464           ret->dw_loc_oprnd1.v.val_addr = rtl;
8465
8466           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
8467           add_loc_descr (&ret, ret1);
8468
8469           indirect_p = 1;
8470           break;
8471         }
8472       /* FALLTHRU */
8473
8474     case PARM_DECL:
8475       {
8476         rtx rtl = rtl_for_decl_location (loc);
8477
8478         if (rtl == NULL_RTX)
8479           return 0;
8480         else if (CONSTANT_P (rtl))
8481           {
8482             ret = new_loc_descr (DW_OP_addr, 0, 0);
8483             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
8484             ret->dw_loc_oprnd1.v.val_addr = rtl;
8485             indirect_p = 1;
8486           }
8487         else
8488           {
8489             enum machine_mode mode = GET_MODE (rtl);
8490
8491             if (GET_CODE (rtl) == MEM)
8492               {
8493                 indirect_p = 1;
8494                 rtl = XEXP (rtl, 0);
8495               }
8496
8497             ret = mem_loc_descriptor (rtl, mode);
8498           }
8499       }
8500       break;
8501
8502     case INDIRECT_REF:
8503       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8504       indirect_p = 1;
8505       break;
8506
8507     case COMPOUND_EXPR:
8508       return loc_descriptor_from_tree (TREE_OPERAND (loc, 1), addressp);
8509
8510     case NOP_EXPR:
8511     case CONVERT_EXPR:
8512     case NON_LVALUE_EXPR:
8513     case VIEW_CONVERT_EXPR:
8514     case SAVE_EXPR:
8515       return loc_descriptor_from_tree (TREE_OPERAND (loc, 0), addressp);
8516
8517     case COMPONENT_REF:
8518     case BIT_FIELD_REF:
8519     case ARRAY_REF:
8520     case ARRAY_RANGE_REF:
8521       {
8522         tree obj, offset;
8523         HOST_WIDE_INT bitsize, bitpos, bytepos;
8524         enum machine_mode mode;
8525         int volatilep;
8526
8527         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
8528                                    &unsignedp, &volatilep);
8529
8530         if (obj == loc)
8531           return 0;
8532
8533         ret = loc_descriptor_from_tree (obj, 1);
8534         if (ret == 0
8535             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
8536           return 0;
8537
8538         if (offset != NULL_TREE)
8539           {
8540             /* Variable offset.  */
8541             add_loc_descr (&ret, loc_descriptor_from_tree (offset, 0));
8542             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8543           }
8544
8545         if (!addressp)
8546           indirect_p = 1;
8547
8548         bytepos = bitpos / BITS_PER_UNIT;
8549         if (bytepos > 0)
8550           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
8551         else if (bytepos < 0)
8552           {
8553             add_loc_descr (&ret, int_loc_descriptor (bytepos));
8554             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
8555           }
8556         break;
8557       }
8558
8559     case INTEGER_CST:
8560       if (host_integerp (loc, 0))
8561         ret = int_loc_descriptor (tree_low_cst (loc, 0));
8562       else
8563         return 0;
8564       break;
8565
8566     case TRUTH_AND_EXPR:
8567     case TRUTH_ANDIF_EXPR:
8568     case BIT_AND_EXPR:
8569       op = DW_OP_and;
8570       goto do_binop;
8571
8572     case TRUTH_XOR_EXPR:
8573     case BIT_XOR_EXPR:
8574       op = DW_OP_xor;
8575       goto do_binop;
8576
8577     case TRUTH_OR_EXPR:
8578     case TRUTH_ORIF_EXPR:
8579     case BIT_IOR_EXPR:
8580       op = DW_OP_or;
8581       goto do_binop;
8582
8583     case TRUNC_DIV_EXPR:
8584       op = DW_OP_div;
8585       goto do_binop;
8586
8587     case MINUS_EXPR:
8588       op = DW_OP_minus;
8589       goto do_binop;
8590
8591     case TRUNC_MOD_EXPR:
8592       op = DW_OP_mod;
8593       goto do_binop;
8594
8595     case MULT_EXPR:
8596       op = DW_OP_mul;
8597       goto do_binop;
8598
8599     case LSHIFT_EXPR:
8600       op = DW_OP_shl;
8601       goto do_binop;
8602
8603     case RSHIFT_EXPR:
8604       op = (unsignedp ? DW_OP_shr : DW_OP_shra);
8605       goto do_binop;
8606
8607     case PLUS_EXPR:
8608       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
8609           && host_integerp (TREE_OPERAND (loc, 1), 0))
8610         {
8611           ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8612           if (ret == 0)
8613             return 0;
8614
8615           add_loc_descr (&ret,
8616                          new_loc_descr (DW_OP_plus_uconst,
8617                                         tree_low_cst (TREE_OPERAND (loc, 1),
8618                                                       0),
8619                                         0));
8620           break;
8621         }
8622
8623       op = DW_OP_plus;
8624       goto do_binop;
8625
8626     case LE_EXPR:
8627       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8628         return 0;
8629
8630       op = DW_OP_le;
8631       goto do_binop;
8632
8633     case GE_EXPR:
8634       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8635         return 0;
8636
8637       op = DW_OP_ge;
8638       goto do_binop;
8639
8640     case LT_EXPR:
8641       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8642         return 0;
8643
8644       op = DW_OP_lt;
8645       goto do_binop;
8646
8647     case GT_EXPR:
8648       if (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
8649         return 0;
8650
8651       op = DW_OP_gt;
8652       goto do_binop;
8653
8654     case EQ_EXPR:
8655       op = DW_OP_eq;
8656       goto do_binop;
8657
8658     case NE_EXPR:
8659       op = DW_OP_ne;
8660       goto do_binop;
8661
8662     do_binop:
8663       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8664       ret1 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8665       if (ret == 0 || ret1 == 0)
8666         return 0;
8667
8668       add_loc_descr (&ret, ret1);
8669       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8670       break;
8671
8672     case TRUTH_NOT_EXPR:
8673     case BIT_NOT_EXPR:
8674       op = DW_OP_not;
8675       goto do_unop;
8676
8677     case ABS_EXPR:
8678       op = DW_OP_abs;
8679       goto do_unop;
8680
8681     case NEGATE_EXPR:
8682       op = DW_OP_neg;
8683       goto do_unop;
8684
8685     do_unop:
8686       ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8687       if (ret == 0)
8688         return 0;
8689
8690       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
8691       break;
8692
8693     case MAX_EXPR:
8694       loc = build (COND_EXPR, TREE_TYPE (loc),
8695                    build (LT_EXPR, integer_type_node,
8696                           TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
8697                    TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
8698
8699       /* ... fall through ...  */
8700
8701     case COND_EXPR:
8702       {
8703         dw_loc_descr_ref lhs
8704           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
8705         dw_loc_descr_ref rhs
8706           = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0);
8707         dw_loc_descr_ref bra_node, jump_node, tmp;
8708
8709         ret = loc_descriptor_from_tree (TREE_OPERAND (loc, 0), 0);
8710         if (ret == 0 || lhs == 0 || rhs == 0)
8711           return 0;
8712
8713         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
8714         add_loc_descr (&ret, bra_node);
8715
8716         add_loc_descr (&ret, rhs);
8717         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
8718         add_loc_descr (&ret, jump_node);
8719
8720         add_loc_descr (&ret, lhs);
8721         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8722         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
8723
8724         /* ??? Need a node to point the skip at.  Use a nop.  */
8725         tmp = new_loc_descr (DW_OP_nop, 0, 0);
8726         add_loc_descr (&ret, tmp);
8727         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
8728         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
8729       }
8730       break;
8731
8732     default:
8733       abort ();
8734     }
8735
8736   /* Show if we can't fill the request for an address.  */
8737   if (addressp && indirect_p == 0)
8738     return 0;
8739
8740   /* If we've got an address and don't want one, dereference.  */
8741   if (!addressp && indirect_p > 0)
8742     {
8743       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
8744
8745       if (size > DWARF2_ADDR_SIZE || size == -1)
8746         return 0;
8747       else if (size == DWARF2_ADDR_SIZE)
8748         op = DW_OP_deref;
8749       else
8750         op = DW_OP_deref_size;
8751
8752       add_loc_descr (&ret, new_loc_descr (op, size, 0));
8753     }
8754
8755   return ret;
8756 }
8757
8758 /* Given a value, round it up to the lowest multiple of `boundary'
8759    which is not less than the value itself.  */
8760
8761 static inline HOST_WIDE_INT
8762 ceiling (value, boundary)
8763      HOST_WIDE_INT value;
8764      unsigned int boundary;
8765 {
8766   return (((value + boundary - 1) / boundary) * boundary);
8767 }
8768
8769 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
8770    pointer to the declared type for the relevant field variable, or return
8771    `integer_type_node' if the given node turns out to be an
8772    ERROR_MARK node.  */
8773
8774 static inline tree
8775 field_type (decl)
8776      tree decl;
8777 {
8778   tree type;
8779
8780   if (TREE_CODE (decl) == ERROR_MARK)
8781     return integer_type_node;
8782
8783   type = DECL_BIT_FIELD_TYPE (decl);
8784   if (type == NULL_TREE)
8785     type = TREE_TYPE (decl);
8786
8787   return type;
8788 }
8789
8790 /* Given a pointer to a tree node, return the alignment in bits for
8791    it, or else return BITS_PER_WORD if the node actually turns out to
8792    be an ERROR_MARK node.  */
8793
8794 static inline unsigned
8795 simple_type_align_in_bits (type)
8796      tree type;
8797 {
8798   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
8799 }
8800
8801 static inline unsigned
8802 simple_decl_align_in_bits (decl)
8803      tree decl;
8804 {
8805   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
8806 }
8807
8808 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
8809    lowest addressed byte of the "containing object" for the given FIELD_DECL,
8810    or return 0 if we are unable to determine what that offset is, either
8811    because the argument turns out to be a pointer to an ERROR_MARK node, or
8812    because the offset is actually variable.  (We can't handle the latter case
8813    just yet).  */
8814
8815 static HOST_WIDE_INT
8816 field_byte_offset (decl)
8817      tree decl;
8818 {
8819   unsigned int type_align_in_bits;
8820   unsigned int decl_align_in_bits;
8821   unsigned HOST_WIDE_INT type_size_in_bits;
8822   HOST_WIDE_INT object_offset_in_bits;
8823   tree type;
8824   tree field_size_tree;
8825   HOST_WIDE_INT bitpos_int;
8826   HOST_WIDE_INT deepest_bitpos;
8827   unsigned HOST_WIDE_INT field_size_in_bits;
8828
8829   if (TREE_CODE (decl) == ERROR_MARK)
8830     return 0;
8831   else if (TREE_CODE (decl) != FIELD_DECL)
8832     abort ();
8833
8834   type = field_type (decl);
8835   field_size_tree = DECL_SIZE (decl);
8836
8837   /* The size could be unspecified if there was an error, or for
8838      a flexible array member.  */
8839   if (! field_size_tree)
8840     field_size_tree = bitsize_zero_node;
8841
8842   /* We cannot yet cope with fields whose positions are variable, so
8843      for now, when we see such things, we simply return 0.  Someday, we may
8844      be able to handle such cases, but it will be damn difficult.  */
8845   if (! host_integerp (bit_position (decl), 0))
8846     return 0;
8847
8848   bitpos_int = int_bit_position (decl);
8849
8850   /* If we don't know the size of the field, pretend it's a full word.  */
8851   if (host_integerp (field_size_tree, 1))
8852     field_size_in_bits = tree_low_cst (field_size_tree, 1);
8853   else
8854     field_size_in_bits = BITS_PER_WORD;
8855
8856   type_size_in_bits = simple_type_size_in_bits (type);
8857   type_align_in_bits = simple_type_align_in_bits (type);
8858   decl_align_in_bits = simple_decl_align_in_bits (decl);
8859
8860   /* The GCC front-end doesn't make any attempt to keep track of the starting
8861      bit offset (relative to the start of the containing structure type) of the
8862      hypothetical "containing object" for a bit-field.  Thus, when computing
8863      the byte offset value for the start of the "containing object" of a
8864      bit-field, we must deduce this information on our own. This can be rather
8865      tricky to do in some cases.  For example, handling the following structure
8866      type definition when compiling for an i386/i486 target (which only aligns
8867      long long's to 32-bit boundaries) can be very tricky:
8868
8869          struct S { int field1; long long field2:31; };
8870
8871      Fortunately, there is a simple rule-of-thumb which can be used in such
8872      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
8873      structure shown above.  It decides to do this based upon one simple rule
8874      for bit-field allocation.  GCC allocates each "containing object" for each
8875      bit-field at the first (i.e. lowest addressed) legitimate alignment
8876      boundary (based upon the required minimum alignment for the declared type
8877      of the field) which it can possibly use, subject to the condition that
8878      there is still enough available space remaining in the containing object
8879      (when allocated at the selected point) to fully accommodate all of the
8880      bits of the bit-field itself.
8881
8882      This simple rule makes it obvious why GCC allocates 8 bytes for each
8883      object of the structure type shown above.  When looking for a place to
8884      allocate the "containing object" for `field2', the compiler simply tries
8885      to allocate a 64-bit "containing object" at each successive 32-bit
8886      boundary (starting at zero) until it finds a place to allocate that 64-
8887      bit field such that at least 31 contiguous (and previously unallocated)
8888      bits remain within that selected 64 bit field.  (As it turns out, for the
8889      example above, the compiler finds it is OK to allocate the "containing
8890      object" 64-bit field at bit-offset zero within the structure type.)
8891
8892      Here we attempt to work backwards from the limited set of facts we're
8893      given, and we try to deduce from those facts, where GCC must have believed
8894      that the containing object started (within the structure type). The value
8895      we deduce is then used (by the callers of this routine) to generate
8896      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
8897      and, in the case of DW_AT_location, regular fields as well).  */
8898
8899   /* Figure out the bit-distance from the start of the structure to the
8900      "deepest" bit of the bit-field.  */
8901   deepest_bitpos = bitpos_int + field_size_in_bits;
8902
8903   /* This is the tricky part.  Use some fancy footwork to deduce where the
8904      lowest addressed bit of the containing object must be.  */
8905   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8906
8907   /* Round up to type_align by default.  This works best for bitfields.  */
8908   object_offset_in_bits += type_align_in_bits - 1;
8909   object_offset_in_bits /= type_align_in_bits;
8910   object_offset_in_bits *= type_align_in_bits;
8911
8912   if (object_offset_in_bits > bitpos_int)
8913     {
8914       /* Sigh, the decl must be packed.  */
8915       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
8916
8917       /* Round up to decl_align instead.  */
8918       object_offset_in_bits += decl_align_in_bits - 1;
8919       object_offset_in_bits /= decl_align_in_bits;
8920       object_offset_in_bits *= decl_align_in_bits;
8921     }
8922
8923   return object_offset_in_bits / BITS_PER_UNIT;
8924 }
8925 \f
8926 /* The following routines define various Dwarf attributes and any data
8927    associated with them.  */
8928
8929 /* Add a location description attribute value to a DIE.
8930
8931    This emits location attributes suitable for whole variables and
8932    whole parameters.  Note that the location attributes for struct fields are
8933    generated by the routine `data_member_location_attribute' below.  */
8934
8935 static inline void
8936 add_AT_location_description (die, attr_kind, descr)
8937      dw_die_ref die;
8938      enum dwarf_attribute attr_kind;
8939      dw_loc_descr_ref descr;
8940 {
8941   if (descr != 0)
8942     add_AT_loc (die, attr_kind, descr);
8943 }
8944
8945 /* Attach the specialized form of location attribute used for data members of
8946    struct and union types.  In the special case of a FIELD_DECL node which
8947    represents a bit-field, the "offset" part of this special location
8948    descriptor must indicate the distance in bytes from the lowest-addressed
8949    byte of the containing struct or union type to the lowest-addressed byte of
8950    the "containing object" for the bit-field.  (See the `field_byte_offset'
8951    function above).
8952
8953    For any given bit-field, the "containing object" is a hypothetical object
8954    (of some integral or enum type) within which the given bit-field lives.  The
8955    type of this hypothetical "containing object" is always the same as the
8956    declared type of the individual bit-field itself (for GCC anyway... the
8957    DWARF spec doesn't actually mandate this).  Note that it is the size (in
8958    bytes) of the hypothetical "containing object" which will be given in the
8959    DW_AT_byte_size attribute for this bit-field.  (See the
8960    `byte_size_attribute' function below.)  It is also used when calculating the
8961    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
8962    function below.)  */
8963
8964 static void
8965 add_data_member_location_attribute (die, decl)
8966      dw_die_ref die;
8967      tree decl;
8968 {
8969   long offset;
8970   dw_loc_descr_ref loc_descr = 0;
8971
8972   if (TREE_CODE (decl) == TREE_VEC)
8973     {
8974       /* We're working on the TAG_inheritance for a base class.  */
8975       if (TREE_VIA_VIRTUAL (decl) && is_cxx ())
8976         {
8977           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
8978              aren't at a fixed offset from all (sub)objects of the same
8979              type.  We need to extract the appropriate offset from our
8980              vtable.  The following dwarf expression means
8981
8982                BaseAddr = ObAddr + *((*ObAddr) - Offset)
8983
8984              This is specific to the V3 ABI, of course.  */
8985
8986           dw_loc_descr_ref tmp;
8987
8988           /* Make a copy of the object address.  */
8989           tmp = new_loc_descr (DW_OP_dup, 0, 0);
8990           add_loc_descr (&loc_descr, tmp);
8991
8992           /* Extract the vtable address.  */
8993           tmp = new_loc_descr (DW_OP_deref, 0, 0);
8994           add_loc_descr (&loc_descr, tmp);
8995
8996           /* Calculate the address of the offset.  */
8997           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
8998           if (offset >= 0)
8999             abort ();
9000
9001           tmp = int_loc_descriptor (-offset);
9002           add_loc_descr (&loc_descr, tmp);
9003           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9004           add_loc_descr (&loc_descr, tmp);
9005
9006           /* Extract the offset.  */
9007           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9008           add_loc_descr (&loc_descr, tmp);
9009
9010           /* Add it to the object address.  */
9011           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9012           add_loc_descr (&loc_descr, tmp);
9013         }
9014       else
9015         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9016     }
9017   else
9018     offset = field_byte_offset (decl);
9019
9020   if (! loc_descr)
9021     {
9022       enum dwarf_location_atom op;
9023
9024       /* The DWARF2 standard says that we should assume that the structure
9025          address is already on the stack, so we can specify a structure field
9026          address by using DW_OP_plus_uconst.  */
9027
9028 #ifdef MIPS_DEBUGGING_INFO
9029       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9030          operator correctly.  It works only if we leave the offset on the
9031          stack.  */
9032       op = DW_OP_constu;
9033 #else
9034       op = DW_OP_plus_uconst;
9035 #endif
9036
9037       loc_descr = new_loc_descr (op, offset, 0);
9038     }
9039
9040   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9041 }
9042
9043 /* Attach an DW_AT_const_value attribute for a variable or a parameter which
9044    does not have a "location" either in memory or in a register.  These
9045    things can arise in GNU C when a constant is passed as an actual parameter
9046    to an inlined function.  They can also arise in C++ where declared
9047    constants do not necessarily get memory "homes".  */
9048
9049 static void
9050 add_const_value_attribute (die, rtl)
9051      dw_die_ref die;
9052      rtx rtl;
9053 {
9054   switch (GET_CODE (rtl))
9055     {
9056     case CONST_INT:
9057       /* Note that a CONST_INT rtx could represent either an integer
9058          or a floating-point constant.  A CONST_INT is used whenever
9059          the constant will fit into a single word.  In all such
9060          cases, the original mode of the constant value is wiped
9061          out, and the CONST_INT rtx is assigned VOIDmode.  */
9062       {
9063         HOST_WIDE_INT val = INTVAL (rtl);
9064
9065         /* ??? We really should be using HOST_WIDE_INT throughout.  */
9066         if (val < 0 && (long) val == val)
9067           add_AT_int (die, DW_AT_const_value, (long) val);
9068         else if ((unsigned long) val == (unsigned HOST_WIDE_INT) val)
9069           add_AT_unsigned (die, DW_AT_const_value, (unsigned long) val);
9070         else
9071           {
9072 #if HOST_BITS_PER_LONG * 2 == HOST_BITS_PER_WIDE_INT
9073             add_AT_long_long (die, DW_AT_const_value,
9074                               val >> HOST_BITS_PER_LONG, val);
9075 #else
9076             abort ();
9077 #endif
9078           }
9079       }
9080       break;
9081
9082     case CONST_DOUBLE:
9083       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9084          floating-point constant.  A CONST_DOUBLE is used whenever the
9085          constant requires more than one word in order to be adequately
9086          represented.  We output CONST_DOUBLEs as blocks.  */
9087       {
9088         enum machine_mode mode = GET_MODE (rtl);
9089
9090         if (GET_MODE_CLASS (mode) == MODE_FLOAT)
9091           {
9092             unsigned length = GET_MODE_SIZE (mode) / 4;
9093             long *array = (long *) xmalloc (sizeof (long) * length);
9094             REAL_VALUE_TYPE rv;
9095
9096             REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9097             switch (mode)
9098               {
9099               case SFmode:
9100                 REAL_VALUE_TO_TARGET_SINGLE (rv, array[0]);
9101                 break;
9102
9103               case DFmode:
9104                 REAL_VALUE_TO_TARGET_DOUBLE (rv, array);
9105                 break;
9106
9107               case XFmode:
9108               case TFmode:
9109                 REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, array);
9110                 break;
9111
9112               default:
9113                 abort ();
9114               }
9115
9116             add_AT_float (die, DW_AT_const_value, length, array);
9117           }
9118         else
9119           {
9120             /* ??? We really should be using HOST_WIDE_INT throughout.  */
9121             if (HOST_BITS_PER_LONG != HOST_BITS_PER_WIDE_INT)
9122               abort ();
9123
9124             add_AT_long_long (die, DW_AT_const_value,
9125                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9126           }
9127       }
9128       break;
9129
9130     case CONST_STRING:
9131       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9132       break;
9133
9134     case SYMBOL_REF:
9135     case LABEL_REF:
9136     case CONST:
9137       add_AT_addr (die, DW_AT_const_value, rtl);
9138       VARRAY_PUSH_RTX (used_rtx_varray, rtl);
9139       break;
9140
9141     case PLUS:
9142       /* In cases where an inlined instance of an inline function is passed
9143          the address of an `auto' variable (which is local to the caller) we
9144          can get a situation where the DECL_RTL of the artificial local
9145          variable (for the inlining) which acts as a stand-in for the
9146          corresponding formal parameter (of the inline function) will look
9147          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9148          exactly a compile-time constant expression, but it isn't the address
9149          of the (artificial) local variable either.  Rather, it represents the
9150          *value* which the artificial local variable always has during its
9151          lifetime.  We currently have no way to represent such quasi-constant
9152          values in Dwarf, so for now we just punt and generate nothing.  */
9153       break;
9154
9155     default:
9156       /* No other kinds of rtx should be possible here.  */
9157       abort ();
9158     }
9159
9160 }
9161
9162 static rtx
9163 rtl_for_decl_location (decl)
9164      tree decl;
9165 {
9166   rtx rtl;
9167
9168   /* Here we have to decide where we are going to say the parameter "lives"
9169      (as far as the debugger is concerned).  We only have a couple of
9170      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9171
9172      DECL_RTL normally indicates where the parameter lives during most of the
9173      activation of the function.  If optimization is enabled however, this
9174      could be either NULL or else a pseudo-reg.  Both of those cases indicate
9175      that the parameter doesn't really live anywhere (as far as the code
9176      generation parts of GCC are concerned) during most of the function's
9177      activation.  That will happen (for example) if the parameter is never
9178      referenced within the function.
9179
9180      We could just generate a location descriptor here for all non-NULL
9181      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9182      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9183      where DECL_RTL is NULL or is a pseudo-reg.
9184
9185      Note however that we can only get away with using DECL_INCOMING_RTL as
9186      a backup substitute for DECL_RTL in certain limited cases.  In cases
9187      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9188      we can be sure that the parameter was passed using the same type as it is
9189      declared to have within the function, and that its DECL_INCOMING_RTL
9190      points us to a place where a value of that type is passed.
9191
9192      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9193      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9194      because in these cases DECL_INCOMING_RTL points us to a value of some
9195      type which is *different* from the type of the parameter itself.  Thus,
9196      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9197      such cases, the debugger would end up (for example) trying to fetch a
9198      `float' from a place which actually contains the first part of a
9199      `double'.  That would lead to really incorrect and confusing
9200      output at debug-time.
9201
9202      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9203      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
9204      are a couple of exceptions however.  On little-endian machines we can
9205      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9206      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9207      an integral type that is smaller than TREE_TYPE (decl). These cases arise
9208      when (on a little-endian machine) a non-prototyped function has a
9209      parameter declared to be of type `short' or `char'.  In such cases,
9210      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9211      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9212      passed `int' value.  If the debugger then uses that address to fetch
9213      a `short' or a `char' (on a little-endian machine) the result will be
9214      the correct data, so we allow for such exceptional cases below.
9215
9216      Note that our goal here is to describe the place where the given formal
9217      parameter lives during most of the function's activation (i.e. between the
9218      end of the prologue and the start of the epilogue).  We'll do that as best
9219      as we can. Note however that if the given formal parameter is modified
9220      sometime during the execution of the function, then a stack backtrace (at
9221      debug-time) will show the function as having been called with the *new*
9222      value rather than the value which was originally passed in.  This happens
9223      rarely enough that it is not a major problem, but it *is* a problem, and
9224      I'd like to fix it.
9225
9226      A future version of dwarf2out.c may generate two additional attributes for
9227      any given DW_TAG_formal_parameter DIE which will describe the "passed
9228      type" and the "passed location" for the given formal parameter in addition
9229      to the attributes we now generate to indicate the "declared type" and the
9230      "active location" for each parameter.  This additional set of attributes
9231      could be used by debuggers for stack backtraces. Separately, note that
9232      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
9233      This happens (for example) for inlined-instances of inline function formal
9234      parameters which are never referenced.  This really shouldn't be
9235      happening.  All PARM_DECL nodes should get valid non-NULL
9236      DECL_INCOMING_RTL values, but integrate.c doesn't currently generate these
9237      values for inlined instances of inline function parameters, so when we see
9238      such cases, we are just out-of-luck for the time being (until integrate.c
9239      gets fixed).  */
9240
9241   /* Use DECL_RTL as the "location" unless we find something better.  */
9242   rtl = DECL_RTL_IF_SET (decl);
9243
9244   /* When generating abstract instances, ignore everything except
9245      constants and symbols living in memory.  */
9246   if (! reload_completed)
9247     {
9248       if (rtl
9249           && (CONSTANT_P (rtl)
9250               || (GET_CODE (rtl) == MEM
9251                   && CONSTANT_P (XEXP (rtl, 0)))))
9252         {
9253 #ifdef ASM_SIMPLIFY_DWARF_ADDR
9254           rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
9255 #endif
9256           return rtl;
9257         }
9258       rtl = NULL_RTX;
9259     }
9260   else if (TREE_CODE (decl) == PARM_DECL)
9261     {
9262       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
9263         {
9264           tree declared_type = type_main_variant (TREE_TYPE (decl));
9265           tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
9266
9267           /* This decl represents a formal parameter which was optimized out.
9268              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
9269              all cases where (rtl == NULL_RTX) just below.  */
9270           if (declared_type == passed_type)
9271             rtl = DECL_INCOMING_RTL (decl);
9272           else if (! BYTES_BIG_ENDIAN
9273                    && TREE_CODE (declared_type) == INTEGER_TYPE
9274                    && (GET_MODE_SIZE (TYPE_MODE (declared_type))
9275                        <= GET_MODE_SIZE (TYPE_MODE (passed_type))))
9276             rtl = DECL_INCOMING_RTL (decl);
9277         }
9278
9279       /* If the parm was passed in registers, but lives on the stack, then
9280          make a big endian correction if the mode of the type of the
9281          parameter is not the same as the mode of the rtl.  */
9282       /* ??? This is the same series of checks that are made in dbxout.c before
9283          we reach the big endian correction code there.  It isn't clear if all
9284          of these checks are necessary here, but keeping them all is the safe
9285          thing to do.  */
9286       else if (GET_CODE (rtl) == MEM
9287                && XEXP (rtl, 0) != const0_rtx
9288                && ! CONSTANT_P (XEXP (rtl, 0))
9289                /* Not passed in memory.  */
9290                && GET_CODE (DECL_INCOMING_RTL (decl)) != MEM
9291                /* Not passed by invisible reference.  */
9292                && (GET_CODE (XEXP (rtl, 0)) != REG
9293                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
9294                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
9295 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
9296                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
9297 #endif
9298                      )
9299                /* Big endian correction check.  */
9300                && BYTES_BIG_ENDIAN
9301                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
9302                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
9303                    < UNITS_PER_WORD))
9304         {
9305           int offset = (UNITS_PER_WORD
9306                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
9307
9308           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
9309                              plus_constant (XEXP (rtl, 0), offset));
9310         }
9311     }
9312
9313   if (rtl != NULL_RTX)
9314     {
9315       rtl = eliminate_regs (rtl, 0, NULL_RTX);
9316 #ifdef LEAF_REG_REMAP
9317       if (current_function_uses_only_leaf_regs)
9318         leaf_renumber_regs_insn (rtl);
9319 #endif
9320     }
9321
9322   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
9323      and will have been substituted directly into all expressions that use it.
9324      C does not have such a concept, but C++ and other languages do.  */
9325   else if (TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
9326     {
9327       /* If a variable is initialized with a string constant without embedded
9328          zeros, build CONST_STRING.  */
9329       if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
9330           && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9331         {
9332           tree arrtype = TREE_TYPE (decl);
9333           tree enttype = TREE_TYPE (arrtype);
9334           tree domain = TYPE_DOMAIN (arrtype);
9335           tree init = DECL_INITIAL (decl);
9336           enum machine_mode mode = TYPE_MODE (enttype);
9337
9338           if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9339               && domain
9340               && integer_zerop (TYPE_MIN_VALUE (domain))
9341               && compare_tree_int (TYPE_MAX_VALUE (domain),
9342                                    TREE_STRING_LENGTH (init) - 1) == 0
9343               && ((size_t) TREE_STRING_LENGTH (init)
9344                   == strlen (TREE_STRING_POINTER (init)) + 1))
9345             rtl = gen_rtx_CONST_STRING (VOIDmode, TREE_STRING_POINTER (init));
9346         }
9347       /* If the initializer is something that we know will expand into an
9348          immediate RTL constant, expand it now.  Expanding anything else
9349          tends to produce unresolved symbols; see debug/5770 and c++/6381.  */
9350       else if (TREE_CODE (DECL_INITIAL (decl)) == INTEGER_CST
9351                || TREE_CODE (DECL_INITIAL (decl)) == REAL_CST)
9352         {
9353           rtl = expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
9354                              EXPAND_INITIALIZER);
9355           /* If expand_expr returns a MEM, it wasn't immediate.  */
9356           if (rtl && GET_CODE (rtl) == MEM)
9357             abort ();
9358         }
9359     }
9360
9361 #ifdef ASM_SIMPLIFY_DWARF_ADDR
9362   if (rtl)
9363     rtl = ASM_SIMPLIFY_DWARF_ADDR (rtl);
9364 #endif
9365
9366   /* If we don't look past the constant pool, we risk emitting a
9367      reference to a constant pool entry that isn't referenced from
9368      code, and thus is not emitted.  */
9369   if (rtl)
9370     rtl = avoid_constant_pool_reference (rtl);
9371
9372   return rtl;
9373 }
9374
9375 /* Generate *either* an DW_AT_location attribute or else an DW_AT_const_value
9376    data attribute for a variable or a parameter.  We generate the
9377    DW_AT_const_value attribute only in those cases where the given variable
9378    or parameter does not have a true "location" either in memory or in a
9379    register.  This can happen (for example) when a constant is passed as an
9380    actual argument in a call to an inline function.  (It's possible that
9381    these things can crop up in other ways also.)  Note that one type of
9382    constant value which can be passed into an inlined function is a constant
9383    pointer.  This can happen for example if an actual argument in an inlined
9384    function call evaluates to a compile-time constant address.  */
9385
9386 static void
9387 add_location_or_const_value_attribute (die, decl)
9388      dw_die_ref die;
9389      tree decl;
9390 {
9391   rtx rtl;
9392   dw_loc_descr_ref descr;
9393
9394   if (TREE_CODE (decl) == ERROR_MARK)
9395     return;
9396   else if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != PARM_DECL)
9397     abort ();
9398
9399   rtl = rtl_for_decl_location (decl);
9400   if (rtl == NULL_RTX)
9401     return;
9402
9403   switch (GET_CODE (rtl))
9404     {
9405     case ADDRESSOF:
9406       /* The address of a variable that was optimized away;
9407          don't emit anything.  */
9408       break;
9409
9410     case CONST_INT:
9411     case CONST_DOUBLE:
9412     case CONST_STRING:
9413     case SYMBOL_REF:
9414     case LABEL_REF:
9415     case CONST:
9416     case PLUS:
9417       /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
9418       add_const_value_attribute (die, rtl);
9419       break;
9420
9421     case MEM:
9422       if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
9423         {
9424           /* Need loc_descriptor_from_tree since that's where we know
9425              how to handle TLS variables.  Want the object's address
9426              since the top-level DW_AT_location assumes such.  See
9427              the confusion in loc_descriptor for reference.  */
9428           descr = loc_descriptor_from_tree (decl, 1);
9429         }
9430       else
9431         {
9432         case REG:
9433         case SUBREG:
9434         case CONCAT:
9435           descr = loc_descriptor (rtl);
9436         }
9437       add_AT_location_description (die, DW_AT_location, descr);
9438       break;
9439         
9440     default:
9441       abort ();
9442     }
9443 }
9444
9445 /* If we don't have a copy of this variable in memory for some reason (such
9446    as a C++ member constant that doesn't have an out-of-line definition),
9447    we should tell the debugger about the constant value.  */
9448
9449 static void
9450 tree_add_const_value_attribute (var_die, decl)
9451      dw_die_ref var_die;
9452      tree decl;
9453 {
9454   tree init = DECL_INITIAL (decl);
9455   tree type = TREE_TYPE (decl);
9456
9457   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init
9458       && initializer_constant_valid_p (init, type) == null_pointer_node)
9459     /* OK */;
9460   else
9461     return;
9462
9463   switch (TREE_CODE (type))
9464     {
9465     case INTEGER_TYPE:
9466       if (host_integerp (init, 0))
9467         add_AT_unsigned (var_die, DW_AT_const_value,
9468                          tree_low_cst (init, 0));
9469       else
9470         add_AT_long_long (var_die, DW_AT_const_value,
9471                           TREE_INT_CST_HIGH (init),
9472                           TREE_INT_CST_LOW (init));
9473       break;
9474
9475     default:;
9476     }
9477 }
9478
9479 /* Generate an DW_AT_name attribute given some string value to be included as
9480    the value of the attribute.  */
9481
9482 static inline void
9483 add_name_attribute (die, name_string)
9484      dw_die_ref die;
9485      const char *name_string;
9486 {
9487   if (name_string != NULL && *name_string != 0)
9488     {
9489       if (demangle_name_func)
9490         name_string = (*demangle_name_func) (name_string);
9491
9492       add_AT_string (die, DW_AT_name, name_string);
9493     }
9494 }
9495
9496 /* Given a tree node describing an array bound (either lower or upper) output
9497    a representation for that bound.  */
9498
9499 static void
9500 add_bound_info (subrange_die, bound_attr, bound)
9501      dw_die_ref subrange_die;
9502      enum dwarf_attribute bound_attr;
9503      tree bound;
9504 {
9505   switch (TREE_CODE (bound))
9506     {
9507     case ERROR_MARK:
9508       return;
9509
9510     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
9511     case INTEGER_CST:
9512       if (! host_integerp (bound, 0)
9513           || (bound_attr == DW_AT_lower_bound
9514               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
9515                   || (is_fortran () && integer_onep (bound)))))
9516         /* use the default */
9517         ;
9518       else
9519         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
9520       break;
9521
9522     case CONVERT_EXPR:
9523     case NOP_EXPR:
9524     case NON_LVALUE_EXPR:
9525     case VIEW_CONVERT_EXPR:
9526       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
9527       break;
9528
9529     case SAVE_EXPR:
9530       /* If optimization is turned on, the SAVE_EXPRs that describe how to
9531          access the upper bound values may be bogus.  If they refer to a
9532          register, they may only describe how to get at these values at the
9533          points in the generated code right after they have just been
9534          computed.  Worse yet, in the typical case, the upper bound values
9535          will not even *be* computed in the optimized code (though the
9536          number of elements will), so these SAVE_EXPRs are entirely
9537          bogus. In order to compensate for this fact, we check here to see
9538          if optimization is enabled, and if so, we don't add an attribute
9539          for the (unknown and unknowable) upper bound.  This should not
9540          cause too much trouble for existing (stupid?)  debuggers because
9541          they have to deal with empty upper bounds location descriptions
9542          anyway in order to be able to deal with incomplete array types.
9543          Of course an intelligent debugger (GDB?)  should be able to
9544          comprehend that a missing upper bound specification in an array
9545          type used for a storage class `auto' local array variable
9546          indicates that the upper bound is both unknown (at compile- time)
9547          and unknowable (at run-time) due to optimization.
9548
9549          We assume that a MEM rtx is safe because gcc wouldn't put the
9550          value there unless it was going to be used repeatedly in the
9551          function, i.e. for cleanups.  */
9552       if (SAVE_EXPR_RTL (bound)
9553           && (! optimize || GET_CODE (SAVE_EXPR_RTL (bound)) == MEM))
9554         {
9555           dw_die_ref ctx = lookup_decl_die (current_function_decl);
9556           dw_die_ref decl_die = new_die (DW_TAG_variable, ctx, bound);
9557           rtx loc = SAVE_EXPR_RTL (bound);
9558
9559           /* If the RTL for the SAVE_EXPR is memory, handle the case where
9560              it references an outer function's frame.  */
9561           if (GET_CODE (loc) == MEM)
9562             {
9563               rtx new_addr = fix_lexical_addr (XEXP (loc, 0), bound);
9564
9565               if (XEXP (loc, 0) != new_addr)
9566                 loc = gen_rtx_MEM (GET_MODE (loc), new_addr);
9567             }
9568
9569           add_AT_flag (decl_die, DW_AT_artificial, 1);
9570           add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9571           add_AT_location_description (decl_die, DW_AT_location,
9572                                        loc_descriptor (loc));
9573           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9574         }
9575
9576       /* Else leave out the attribute.  */
9577       break;
9578
9579     case VAR_DECL:
9580     case PARM_DECL:
9581       {
9582         dw_die_ref decl_die = lookup_decl_die (bound);
9583
9584         /* ??? Can this happen, or should the variable have been bound
9585            first?  Probably it can, since I imagine that we try to create
9586            the types of parameters in the order in which they exist in
9587            the list, and won't have created a forward reference to a
9588            later parameter.  */
9589         if (decl_die != NULL)
9590           add_AT_die_ref (subrange_die, bound_attr, decl_die);
9591         break;
9592       }
9593
9594     default:
9595       {
9596         /* Otherwise try to create a stack operation procedure to
9597            evaluate the value of the array bound.  */
9598
9599         dw_die_ref ctx, decl_die;
9600         dw_loc_descr_ref loc;
9601
9602         loc = loc_descriptor_from_tree (bound, 0);
9603         if (loc == NULL)
9604           break;
9605
9606         if (current_function_decl == 0)
9607           ctx = comp_unit_die;
9608         else
9609           ctx = lookup_decl_die (current_function_decl);
9610
9611         /* If we weren't able to find a context, it's most likely the case
9612            that we are processing the return type of the function.  So
9613            make a SAVE_EXPR to point to it and have the limbo DIE code
9614            find the proper die.  The save_expr function doesn't always
9615            make a SAVE_EXPR, so do it ourselves.  */
9616         if (ctx == 0)
9617           bound = build (SAVE_EXPR, TREE_TYPE (bound), bound,
9618                          current_function_decl, NULL_TREE);
9619
9620         decl_die = new_die (DW_TAG_variable, ctx, bound);
9621         add_AT_flag (decl_die, DW_AT_artificial, 1);
9622         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
9623         add_AT_loc (decl_die, DW_AT_location, loc);
9624
9625         add_AT_die_ref (subrange_die, bound_attr, decl_die);
9626         break;
9627       }
9628     }
9629 }
9630
9631 /* Note that the block of subscript information for an array type also
9632    includes information about the element type of type given array type.  */
9633
9634 static void
9635 add_subscript_info (type_die, type)
9636      dw_die_ref type_die;
9637      tree type;
9638 {
9639 #ifndef MIPS_DEBUGGING_INFO
9640   unsigned dimension_number;
9641 #endif
9642   tree lower, upper;
9643   dw_die_ref subrange_die;
9644
9645   /* The GNU compilers represent multidimensional array types as sequences of
9646      one dimensional array types whose element types are themselves array
9647      types.  Here we squish that down, so that each multidimensional array
9648      type gets only one array_type DIE in the Dwarf debugging info. The draft
9649      Dwarf specification say that we are allowed to do this kind of
9650      compression in C (because there is no difference between an array or
9651      arrays and a multidimensional array in C) but for other source languages
9652      (e.g. Ada) we probably shouldn't do this.  */
9653
9654   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
9655      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
9656      We work around this by disabling this feature.  See also
9657      gen_array_type_die.  */
9658 #ifndef MIPS_DEBUGGING_INFO
9659   for (dimension_number = 0;
9660        TREE_CODE (type) == ARRAY_TYPE;
9661        type = TREE_TYPE (type), dimension_number++)
9662 #endif
9663     {
9664       tree domain = TYPE_DOMAIN (type);
9665
9666       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
9667          and (in GNU C only) variable bounds.  Handle all three forms
9668          here.  */
9669       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
9670       if (domain)
9671         {
9672           /* We have an array type with specified bounds.  */
9673           lower = TYPE_MIN_VALUE (domain);
9674           upper = TYPE_MAX_VALUE (domain);
9675
9676           /* define the index type.  */
9677           if (TREE_TYPE (domain))
9678             {
9679               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
9680                  TREE_TYPE field.  We can't emit debug info for this
9681                  because it is an unnamed integral type.  */
9682               if (TREE_CODE (domain) == INTEGER_TYPE
9683                   && TYPE_NAME (domain) == NULL_TREE
9684                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
9685                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
9686                 ;
9687               else
9688                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
9689                                     type_die);
9690             }
9691
9692           /* ??? If upper is NULL, the array has unspecified length,
9693              but it does have a lower bound.  This happens with Fortran
9694                dimension arr(N:*)
9695              Since the debugger is definitely going to need to know N
9696              to produce useful results, go ahead and output the lower
9697              bound solo, and hope the debugger can cope.  */
9698
9699           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
9700           if (upper)
9701             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
9702         }
9703
9704       /* Otherwise we have an array type with an unspecified length.  The
9705          DWARF-2 spec does not say how to handle this; let's just leave out the
9706          bounds.  */
9707     }
9708 }
9709
9710 static void
9711 add_byte_size_attribute (die, tree_node)
9712      dw_die_ref die;
9713      tree tree_node;
9714 {
9715   unsigned size;
9716
9717   switch (TREE_CODE (tree_node))
9718     {
9719     case ERROR_MARK:
9720       size = 0;
9721       break;
9722     case ENUMERAL_TYPE:
9723     case RECORD_TYPE:
9724     case UNION_TYPE:
9725     case QUAL_UNION_TYPE:
9726       size = int_size_in_bytes (tree_node);
9727       break;
9728     case FIELD_DECL:
9729       /* For a data member of a struct or union, the DW_AT_byte_size is
9730          generally given as the number of bytes normally allocated for an
9731          object of the *declared* type of the member itself.  This is true
9732          even for bit-fields.  */
9733       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
9734       break;
9735     default:
9736       abort ();
9737     }
9738
9739   /* Note that `size' might be -1 when we get to this point.  If it is, that
9740      indicates that the byte size of the entity in question is variable.  We
9741      have no good way of expressing this fact in Dwarf at the present time,
9742      so just let the -1 pass on through.  */
9743   add_AT_unsigned (die, DW_AT_byte_size, size);
9744 }
9745
9746 /* For a FIELD_DECL node which represents a bit-field, output an attribute
9747    which specifies the distance in bits from the highest order bit of the
9748    "containing object" for the bit-field to the highest order bit of the
9749    bit-field itself.
9750
9751    For any given bit-field, the "containing object" is a hypothetical object
9752    (of some integral or enum type) within which the given bit-field lives.  The
9753    type of this hypothetical "containing object" is always the same as the
9754    declared type of the individual bit-field itself.  The determination of the
9755    exact location of the "containing object" for a bit-field is rather
9756    complicated.  It's handled by the `field_byte_offset' function (above).
9757
9758    Note that it is the size (in bytes) of the hypothetical "containing object"
9759    which will be given in the DW_AT_byte_size attribute for this bit-field.
9760    (See `byte_size_attribute' above).  */
9761
9762 static inline void
9763 add_bit_offset_attribute (die, decl)
9764      dw_die_ref die;
9765      tree decl;
9766 {
9767   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
9768   tree type = DECL_BIT_FIELD_TYPE (decl);
9769   HOST_WIDE_INT bitpos_int;
9770   HOST_WIDE_INT highest_order_object_bit_offset;
9771   HOST_WIDE_INT highest_order_field_bit_offset;
9772   HOST_WIDE_INT unsigned bit_offset;
9773
9774   /* Must be a field and a bit field.  */
9775   if (!type
9776       || TREE_CODE (decl) != FIELD_DECL)
9777     abort ();
9778
9779   /* We can't yet handle bit-fields whose offsets are variable, so if we
9780      encounter such things, just return without generating any attribute
9781      whatsoever.  Likewise for variable or too large size.  */
9782   if (! host_integerp (bit_position (decl), 0)
9783       || ! host_integerp (DECL_SIZE (decl), 1))
9784     return;
9785
9786   bitpos_int = int_bit_position (decl);
9787
9788   /* Note that the bit offset is always the distance (in bits) from the
9789      highest-order bit of the "containing object" to the highest-order bit of
9790      the bit-field itself.  Since the "high-order end" of any object or field
9791      is different on big-endian and little-endian machines, the computation
9792      below must take account of these differences.  */
9793   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
9794   highest_order_field_bit_offset = bitpos_int;
9795
9796   if (! BYTES_BIG_ENDIAN)
9797     {
9798       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
9799       highest_order_object_bit_offset += simple_type_size_in_bits (type);
9800     }
9801
9802   bit_offset
9803     = (! BYTES_BIG_ENDIAN
9804        ? highest_order_object_bit_offset - highest_order_field_bit_offset
9805        : highest_order_field_bit_offset - highest_order_object_bit_offset);
9806
9807   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
9808 }
9809
9810 /* For a FIELD_DECL node which represents a bit field, output an attribute
9811    which specifies the length in bits of the given field.  */
9812
9813 static inline void
9814 add_bit_size_attribute (die, decl)
9815      dw_die_ref die;
9816      tree decl;
9817 {
9818   /* Must be a field and a bit field.  */
9819   if (TREE_CODE (decl) != FIELD_DECL
9820       || ! DECL_BIT_FIELD_TYPE (decl))
9821     abort ();
9822
9823   if (host_integerp (DECL_SIZE (decl), 1))
9824     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
9825 }
9826
9827 /* If the compiled language is ANSI C, then add a 'prototyped'
9828    attribute, if arg types are given for the parameters of a function.  */
9829
9830 static inline void
9831 add_prototyped_attribute (die, func_type)
9832      dw_die_ref die;
9833      tree func_type;
9834 {
9835   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
9836       && TYPE_ARG_TYPES (func_type) != NULL)
9837     add_AT_flag (die, DW_AT_prototyped, 1);
9838 }
9839
9840 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
9841    by looking in either the type declaration or object declaration
9842    equate table.  */
9843
9844 static inline void
9845 add_abstract_origin_attribute (die, origin)
9846      dw_die_ref die;
9847      tree origin;
9848 {
9849   dw_die_ref origin_die = NULL;
9850
9851   if (TREE_CODE (origin) != FUNCTION_DECL)
9852     {
9853       /* We may have gotten separated from the block for the inlined
9854          function, if we're in an exception handler or some such; make
9855          sure that the abstract function has been written out.
9856
9857          Doing this for nested functions is wrong, however; functions are
9858          distinct units, and our context might not even be inline.  */
9859       tree fn = origin;
9860
9861       if (TYPE_P (fn))
9862         fn = TYPE_STUB_DECL (fn);
9863
9864       fn = decl_function_context (fn);
9865       if (fn)
9866         dwarf2out_abstract_function (fn);
9867     }
9868
9869   if (DECL_P (origin))
9870     origin_die = lookup_decl_die (origin);
9871   else if (TYPE_P (origin))
9872     origin_die = lookup_type_die (origin);
9873
9874   if (origin_die == NULL)
9875     abort ();
9876
9877   add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
9878 }
9879
9880 /* We do not currently support the pure_virtual attribute.  */
9881
9882 static inline void
9883 add_pure_or_virtual_attribute (die, func_decl)
9884      dw_die_ref die;
9885      tree func_decl;
9886 {
9887   if (DECL_VINDEX (func_decl))
9888     {
9889       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
9890
9891       if (host_integerp (DECL_VINDEX (func_decl), 0))
9892         add_AT_loc (die, DW_AT_vtable_elem_location,
9893                     new_loc_descr (DW_OP_constu,
9894                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
9895                                    0));
9896
9897       /* GNU extension: Record what type this method came from originally.  */
9898       if (debug_info_level > DINFO_LEVEL_TERSE)
9899         add_AT_die_ref (die, DW_AT_containing_type,
9900                         lookup_type_die (DECL_CONTEXT (func_decl)));
9901     }
9902 }
9903 \f
9904 /* Add source coordinate attributes for the given decl.  */
9905
9906 static void
9907 add_src_coords_attributes (die, decl)
9908      dw_die_ref die;
9909      tree decl;
9910 {
9911   unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
9912
9913   add_AT_unsigned (die, DW_AT_decl_file, file_index);
9914   add_AT_unsigned (die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
9915 }
9916
9917 /* Add an DW_AT_name attribute and source coordinate attribute for the
9918    given decl, but only if it actually has a name.  */
9919
9920 static void
9921 add_name_and_src_coords_attributes (die, decl)
9922      dw_die_ref die;
9923      tree decl;
9924 {
9925   tree decl_name;
9926
9927   decl_name = DECL_NAME (decl);
9928   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
9929     {
9930       add_name_attribute (die, dwarf2_name (decl, 0));
9931       if (! DECL_ARTIFICIAL (decl))
9932         add_src_coords_attributes (die, decl);
9933
9934       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
9935           && TREE_PUBLIC (decl)
9936           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
9937           && !DECL_ABSTRACT (decl))
9938         add_AT_string (die, DW_AT_MIPS_linkage_name,
9939                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
9940     }
9941
9942 #ifdef VMS_DEBUGGING_INFO
9943   /* Get the function's name, as described by its RTL.  This may be different
9944      from the DECL_NAME name used in the source file.  */
9945   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
9946     {
9947       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
9948                    XEXP (DECL_RTL (decl), 0));
9949       VARRAY_PUSH_RTX (used_rtx_varray, XEXP (DECL_RTL (decl), 0));
9950     }
9951 #endif
9952 }
9953
9954 /* Push a new declaration scope.  */
9955
9956 static void
9957 push_decl_scope (scope)
9958      tree scope;
9959 {
9960   VARRAY_PUSH_TREE (decl_scope_table, scope);
9961 }
9962
9963 /* Pop a declaration scope.  */
9964
9965 static inline void
9966 pop_decl_scope ()
9967 {
9968   if (VARRAY_ACTIVE_SIZE (decl_scope_table) <= 0)
9969     abort ();
9970
9971   VARRAY_POP (decl_scope_table);
9972 }
9973
9974 /* Return the DIE for the scope that immediately contains this type.
9975    Non-named types get global scope.  Named types nested in other
9976    types get their containing scope if it's open, or global scope
9977    otherwise.  All other types (i.e. function-local named types) get
9978    the current active scope.  */
9979
9980 static dw_die_ref
9981 scope_die_for (t, context_die)
9982      tree t;
9983      dw_die_ref context_die;
9984 {
9985   dw_die_ref scope_die = NULL;
9986   tree containing_scope;
9987   int i;
9988
9989   /* Non-types always go in the current scope.  */
9990   if (! TYPE_P (t))
9991     abort ();
9992
9993   containing_scope = TYPE_CONTEXT (t);
9994
9995   /* Ignore namespaces for the moment.  */
9996   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
9997     containing_scope = NULL_TREE;
9998
9999   /* Ignore function type "scopes" from the C frontend.  They mean that
10000      a tagged type is local to a parmlist of a function declarator, but
10001      that isn't useful to DWARF.  */
10002   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10003     containing_scope = NULL_TREE;
10004
10005   if (containing_scope == NULL_TREE)
10006     scope_die = comp_unit_die;
10007   else if (TYPE_P (containing_scope))
10008     {
10009       /* For types, we can just look up the appropriate DIE.  But
10010          first we check to see if we're in the middle of emitting it
10011          so we know where the new DIE should go.  */
10012       for (i = VARRAY_ACTIVE_SIZE (decl_scope_table) - 1; i >= 0; --i)
10013         if (VARRAY_TREE (decl_scope_table, i) == containing_scope)
10014           break;
10015
10016       if (i < 0)
10017         {
10018           if (debug_info_level > DINFO_LEVEL_TERSE
10019               && !TREE_ASM_WRITTEN (containing_scope))
10020             abort ();
10021
10022           /* If none of the current dies are suitable, we get file scope.  */
10023           scope_die = comp_unit_die;
10024         }
10025       else
10026         scope_die = lookup_type_die (containing_scope);
10027     }
10028   else
10029     scope_die = context_die;
10030
10031   return scope_die;
10032 }
10033
10034 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
10035
10036 static inline int
10037 local_scope_p (context_die)
10038      dw_die_ref context_die;
10039 {
10040   for (; context_die; context_die = context_die->die_parent)
10041     if (context_die->die_tag == DW_TAG_inlined_subroutine
10042         || context_die->die_tag == DW_TAG_subprogram)
10043       return 1;
10044
10045   return 0;
10046 }
10047
10048 /* Returns nonzero if CONTEXT_DIE is a class.  */
10049
10050 static inline int
10051 class_scope_p (context_die)
10052      dw_die_ref context_die;
10053 {
10054   return (context_die
10055           && (context_die->die_tag == DW_TAG_structure_type
10056               || context_die->die_tag == DW_TAG_union_type));
10057 }
10058
10059 /* Many forms of DIEs require a "type description" attribute.  This
10060    routine locates the proper "type descriptor" die for the type given
10061    by 'type', and adds an DW_AT_type attribute below the given die.  */
10062
10063 static void
10064 add_type_attribute (object_die, type, decl_const, decl_volatile, context_die)
10065      dw_die_ref object_die;
10066      tree type;
10067      int decl_const;
10068      int decl_volatile;
10069      dw_die_ref context_die;
10070 {
10071   enum tree_code code  = TREE_CODE (type);
10072   dw_die_ref type_die  = NULL;
10073
10074   /* ??? If this type is an unnamed subrange type of an integral or
10075      floating-point type, use the inner type.  This is because we have no
10076      support for unnamed types in base_type_die.  This can happen if this is
10077      an Ada subrange type.  Correct solution is emit a subrange type die.  */
10078   if ((code == INTEGER_TYPE || code == REAL_TYPE)
10079       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10080     type = TREE_TYPE (type), code = TREE_CODE (type);
10081
10082   if (code == ERROR_MARK
10083       /* Handle a special case.  For functions whose return type is void, we
10084          generate *no* type attribute.  (Note that no object may have type
10085          `void', so this only applies to function return types).  */
10086       || code == VOID_TYPE)
10087     return;
10088
10089   type_die = modified_type_die (type,
10090                                 decl_const || TYPE_READONLY (type),
10091                                 decl_volatile || TYPE_VOLATILE (type),
10092                                 context_die);
10093
10094   if (type_die != NULL)
10095     add_AT_die_ref (object_die, DW_AT_type, type_die);
10096 }
10097
10098 /* Given a tree pointer to a struct, class, union, or enum type node, return
10099    a pointer to the (string) tag name for the given type, or zero if the type
10100    was declared without a tag.  */
10101
10102 static const char *
10103 type_tag (type)
10104      tree type;
10105 {
10106   const char *name = 0;
10107
10108   if (TYPE_NAME (type) != 0)
10109     {
10110       tree t = 0;
10111
10112       /* Find the IDENTIFIER_NODE for the type name.  */
10113       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
10114         t = TYPE_NAME (type);
10115
10116       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
10117          a TYPE_DECL node, regardless of whether or not a `typedef' was
10118          involved.  */
10119       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10120                && ! DECL_IGNORED_P (TYPE_NAME (type)))
10121         t = DECL_NAME (TYPE_NAME (type));
10122
10123       /* Now get the name as a string, or invent one.  */
10124       if (t != 0)
10125         name = IDENTIFIER_POINTER (t);
10126     }
10127
10128   return (name == 0 || *name == '\0') ? 0 : name;
10129 }
10130
10131 /* Return the type associated with a data member, make a special check
10132    for bit field types.  */
10133
10134 static inline tree
10135 member_declared_type (member)
10136      tree member;
10137 {
10138   return (DECL_BIT_FIELD_TYPE (member)
10139           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
10140 }
10141
10142 /* Get the decl's label, as described by its RTL. This may be different
10143    from the DECL_NAME name used in the source file.  */
10144
10145 #if 0
10146 static const char *
10147 decl_start_label (decl)
10148      tree decl;
10149 {
10150   rtx x;
10151   const char *fnname;
10152
10153   x = DECL_RTL (decl);
10154   if (GET_CODE (x) != MEM)
10155     abort ();
10156
10157   x = XEXP (x, 0);
10158   if (GET_CODE (x) != SYMBOL_REF)
10159     abort ();
10160
10161   fnname = XSTR (x, 0);
10162   return fnname;
10163 }
10164 #endif
10165 \f
10166 /* These routines generate the internal representation of the DIE's for
10167    the compilation unit.  Debugging information is collected by walking
10168    the declaration trees passed in from dwarf2out_decl().  */
10169
10170 static void
10171 gen_array_type_die (type, context_die)
10172      tree type;
10173      dw_die_ref context_die;
10174 {
10175   dw_die_ref scope_die = scope_die_for (type, context_die);
10176   dw_die_ref array_die;
10177   tree element_type;
10178
10179   /* ??? The SGI dwarf reader fails for array of array of enum types unless
10180      the inner array type comes before the outer array type.  Thus we must
10181      call gen_type_die before we call new_die.  See below also.  */
10182 #ifdef MIPS_DEBUGGING_INFO
10183   gen_type_die (TREE_TYPE (type), context_die);
10184 #endif
10185
10186   array_die = new_die (DW_TAG_array_type, scope_die, type);
10187   add_name_attribute (array_die, type_tag (type));
10188   equate_type_number_to_die (type, array_die);
10189
10190   if (TREE_CODE (type) == VECTOR_TYPE)
10191     {
10192       /* The frontend feeds us a representation for the vector as a struct
10193          containing an array.  Pull out the array type.  */
10194       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
10195       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
10196     }
10197
10198 #if 0
10199   /* We default the array ordering.  SDB will probably do
10200      the right things even if DW_AT_ordering is not present.  It's not even
10201      an issue until we start to get into multidimensional arrays anyway.  If
10202      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
10203      then we'll have to put the DW_AT_ordering attribute back in.  (But if
10204      and when we find out that we need to put these in, we will only do so
10205      for multidimensional arrays.  */
10206   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
10207 #endif
10208
10209 #ifdef MIPS_DEBUGGING_INFO
10210   /* The SGI compilers handle arrays of unknown bound by setting
10211      AT_declaration and not emitting any subrange DIEs.  */
10212   if (! TYPE_DOMAIN (type))
10213     add_AT_unsigned (array_die, DW_AT_declaration, 1);
10214   else
10215 #endif
10216     add_subscript_info (array_die, type);
10217
10218   /* Add representation of the type of the elements of this array type.  */
10219   element_type = TREE_TYPE (type);
10220
10221   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10222      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10223      We work around this by disabling this feature.  See also
10224      add_subscript_info.  */
10225 #ifndef MIPS_DEBUGGING_INFO
10226   while (TREE_CODE (element_type) == ARRAY_TYPE)
10227     element_type = TREE_TYPE (element_type);
10228
10229   gen_type_die (element_type, context_die);
10230 #endif
10231
10232   add_type_attribute (array_die, element_type, 0, 0, context_die);
10233 }
10234
10235 static void
10236 gen_set_type_die (type, context_die)
10237      tree type;
10238      dw_die_ref context_die;
10239 {
10240   dw_die_ref type_die
10241     = new_die (DW_TAG_set_type, scope_die_for (type, context_die), type);
10242
10243   equate_type_number_to_die (type, type_die);
10244   add_type_attribute (type_die, TREE_TYPE (type), 0, 0, context_die);
10245 }
10246
10247 #if 0
10248 static void
10249 gen_entry_point_die (decl, context_die)
10250      tree decl;
10251      dw_die_ref context_die;
10252 {
10253   tree origin = decl_ultimate_origin (decl);
10254   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
10255
10256   if (origin != NULL)
10257     add_abstract_origin_attribute (decl_die, origin);
10258   else
10259     {
10260       add_name_and_src_coords_attributes (decl_die, decl);
10261       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
10262                           0, 0, context_die);
10263     }
10264
10265   if (DECL_ABSTRACT (decl))
10266     equate_decl_number_to_die (decl, decl_die);
10267   else
10268     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
10269 }
10270 #endif
10271
10272 /* Walk through the list of incomplete types again, trying once more to
10273    emit full debugging info for them.  */
10274
10275 static void
10276 retry_incomplete_types ()
10277 {
10278   int i;
10279
10280   for (i = VARRAY_ACTIVE_SIZE (incomplete_types) - 1; i >= 0; i--)
10281     gen_type_die (VARRAY_TREE (incomplete_types, i), comp_unit_die);
10282 }
10283
10284 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
10285
10286 static void
10287 gen_inlined_enumeration_type_die (type, context_die)
10288      tree type;
10289      dw_die_ref context_die;
10290 {
10291   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
10292
10293   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10294      be incomplete and such types are not marked.  */
10295   add_abstract_origin_attribute (type_die, type);
10296 }
10297
10298 /* Generate a DIE to represent an inlined instance of a structure type.  */
10299
10300 static void
10301 gen_inlined_structure_type_die (type, context_die)
10302      tree type;
10303      dw_die_ref context_die;
10304 {
10305   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
10306
10307   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10308      be incomplete and such types are not marked.  */
10309   add_abstract_origin_attribute (type_die, type);
10310 }
10311
10312 /* Generate a DIE to represent an inlined instance of a union type.  */
10313
10314 static void
10315 gen_inlined_union_type_die (type, context_die)
10316      tree type;
10317      dw_die_ref context_die;
10318 {
10319   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
10320
10321   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
10322      be incomplete and such types are not marked.  */
10323   add_abstract_origin_attribute (type_die, type);
10324 }
10325
10326 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
10327    include all of the information about the enumeration values also. Each
10328    enumerated type name/value is listed as a child of the enumerated type
10329    DIE.  */
10330
10331 static void
10332 gen_enumeration_type_die (type, context_die)
10333      tree type;
10334      dw_die_ref context_die;
10335 {
10336   dw_die_ref type_die = lookup_type_die (type);
10337
10338   if (type_die == NULL)
10339     {
10340       type_die = new_die (DW_TAG_enumeration_type,
10341                           scope_die_for (type, context_die), type);
10342       equate_type_number_to_die (type, type_die);
10343       add_name_attribute (type_die, type_tag (type));
10344     }
10345   else if (! TYPE_SIZE (type))
10346     return;
10347   else
10348     remove_AT (type_die, DW_AT_declaration);
10349
10350   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
10351      given enum type is incomplete, do not generate the DW_AT_byte_size
10352      attribute or the DW_AT_element_list attribute.  */
10353   if (TYPE_SIZE (type))
10354     {
10355       tree link;
10356
10357       TREE_ASM_WRITTEN (type) = 1;
10358       add_byte_size_attribute (type_die, type);
10359       if (TYPE_STUB_DECL (type) != NULL_TREE)
10360         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
10361
10362       /* If the first reference to this type was as the return type of an
10363          inline function, then it may not have a parent.  Fix this now.  */
10364       if (type_die->die_parent == NULL)
10365         add_child_die (scope_die_for (type, context_die), type_die);
10366
10367       for (link = TYPE_FIELDS (type);
10368            link != NULL; link = TREE_CHAIN (link))
10369         {
10370           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
10371
10372           add_name_attribute (enum_die,
10373                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
10374
10375           if (host_integerp (TREE_VALUE (link), 0))
10376             {
10377               if (tree_int_cst_sgn (TREE_VALUE (link)) < 0)
10378                 add_AT_int (enum_die, DW_AT_const_value,
10379                             tree_low_cst (TREE_VALUE (link), 0));
10380               else
10381                 add_AT_unsigned (enum_die, DW_AT_const_value,
10382                                  tree_low_cst (TREE_VALUE (link), 0));
10383             }
10384         }
10385     }
10386   else
10387     add_AT_flag (type_die, DW_AT_declaration, 1);
10388 }
10389
10390 /* Generate a DIE to represent either a real live formal parameter decl or to
10391    represent just the type of some formal parameter position in some function
10392    type.
10393
10394    Note that this routine is a bit unusual because its argument may be a
10395    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
10396    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
10397    node.  If it's the former then this function is being called to output a
10398    DIE to represent a formal parameter object (or some inlining thereof).  If
10399    it's the latter, then this function is only being called to output a
10400    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
10401    argument type of some subprogram type.  */
10402
10403 static dw_die_ref
10404 gen_formal_parameter_die (node, context_die)
10405      tree node;
10406      dw_die_ref context_die;
10407 {
10408   dw_die_ref parm_die
10409     = new_die (DW_TAG_formal_parameter, context_die, node);
10410   tree origin;
10411
10412   switch (TREE_CODE_CLASS (TREE_CODE (node)))
10413     {
10414     case 'd':
10415       origin = decl_ultimate_origin (node);
10416       if (origin != NULL)
10417         add_abstract_origin_attribute (parm_die, origin);
10418       else
10419         {
10420           add_name_and_src_coords_attributes (parm_die, node);
10421           add_type_attribute (parm_die, TREE_TYPE (node),
10422                               TREE_READONLY (node),
10423                               TREE_THIS_VOLATILE (node),
10424                               context_die);
10425           if (DECL_ARTIFICIAL (node))
10426             add_AT_flag (parm_die, DW_AT_artificial, 1);
10427         }
10428
10429       equate_decl_number_to_die (node, parm_die);
10430       if (! DECL_ABSTRACT (node))
10431         add_location_or_const_value_attribute (parm_die, node);
10432
10433       break;
10434
10435     case 't':
10436       /* We were called with some kind of a ..._TYPE node.  */
10437       add_type_attribute (parm_die, node, 0, 0, context_die);
10438       break;
10439
10440     default:
10441       abort ();
10442     }
10443
10444   return parm_die;
10445 }
10446
10447 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
10448    at the end of an (ANSI prototyped) formal parameters list.  */
10449
10450 static void
10451 gen_unspecified_parameters_die (decl_or_type, context_die)
10452      tree decl_or_type;
10453      dw_die_ref context_die;
10454 {
10455   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
10456 }
10457
10458 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
10459    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
10460    parameters as specified in some function type specification (except for
10461    those which appear as part of a function *definition*).  */
10462
10463 static void
10464 gen_formal_types_die (function_or_method_type, context_die)
10465      tree function_or_method_type;
10466      dw_die_ref context_die;
10467 {
10468   tree link;
10469   tree formal_type = NULL;
10470   tree first_parm_type;
10471   tree arg;
10472
10473   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
10474     {
10475       arg = DECL_ARGUMENTS (function_or_method_type);
10476       function_or_method_type = TREE_TYPE (function_or_method_type);
10477     }
10478   else
10479     arg = NULL_TREE;
10480
10481   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
10482
10483   /* Make our first pass over the list of formal parameter types and output a
10484      DW_TAG_formal_parameter DIE for each one.  */
10485   for (link = first_parm_type; link; )
10486     {
10487       dw_die_ref parm_die;
10488
10489       formal_type = TREE_VALUE (link);
10490       if (formal_type == void_type_node)
10491         break;
10492
10493       /* Output a (nameless) DIE to represent the formal parameter itself.  */
10494       parm_die = gen_formal_parameter_die (formal_type, context_die);
10495       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
10496            && link == first_parm_type)
10497           || (arg && DECL_ARTIFICIAL (arg)))
10498         add_AT_flag (parm_die, DW_AT_artificial, 1);
10499
10500       link = TREE_CHAIN (link);
10501       if (arg)
10502         arg = TREE_CHAIN (arg);
10503     }
10504
10505   /* If this function type has an ellipsis, add a
10506      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
10507   if (formal_type != void_type_node)
10508     gen_unspecified_parameters_die (function_or_method_type, context_die);
10509
10510   /* Make our second (and final) pass over the list of formal parameter types
10511      and output DIEs to represent those types (as necessary).  */
10512   for (link = TYPE_ARG_TYPES (function_or_method_type);
10513        link && TREE_VALUE (link);
10514        link = TREE_CHAIN (link))
10515     gen_type_die (TREE_VALUE (link), context_die);
10516 }
10517
10518 /* We want to generate the DIE for TYPE so that we can generate the
10519    die for MEMBER, which has been defined; we will need to refer back
10520    to the member declaration nested within TYPE.  If we're trying to
10521    generate minimal debug info for TYPE, processing TYPE won't do the
10522    trick; we need to attach the member declaration by hand.  */
10523
10524 static void
10525 gen_type_die_for_member (type, member, context_die)
10526      tree type, member;
10527      dw_die_ref context_die;
10528 {
10529   gen_type_die (type, context_die);
10530
10531   /* If we're trying to avoid duplicate debug info, we may not have
10532      emitted the member decl for this function.  Emit it now.  */
10533   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
10534       && ! lookup_decl_die (member))
10535     {
10536       if (decl_ultimate_origin (member))
10537         abort ();
10538
10539       push_decl_scope (type);
10540       if (TREE_CODE (member) == FUNCTION_DECL)
10541         gen_subprogram_die (member, lookup_type_die (type));
10542       else
10543         gen_variable_die (member, lookup_type_die (type));
10544
10545       pop_decl_scope ();
10546     }
10547 }
10548
10549 /* Generate the DWARF2 info for the "abstract" instance of a function which we
10550    may later generate inlined and/or out-of-line instances of.  */
10551
10552 static void
10553 dwarf2out_abstract_function (decl)
10554      tree decl;
10555 {
10556   dw_die_ref old_die;
10557   tree save_fn;
10558   tree context;
10559   int was_abstract = DECL_ABSTRACT (decl);
10560
10561   /* Make sure we have the actual abstract inline, not a clone.  */
10562   decl = DECL_ORIGIN (decl);
10563
10564   old_die = lookup_decl_die (decl);
10565   if (old_die && get_AT_unsigned (old_die, DW_AT_inline))
10566     /* We've already generated the abstract instance.  */
10567     return;
10568
10569   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
10570      we don't get confused by DECL_ABSTRACT.  */
10571   if (debug_info_level > DINFO_LEVEL_TERSE)
10572     {
10573       context = decl_class_context (decl);
10574       if (context)
10575         gen_type_die_for_member
10576           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
10577     }
10578
10579   /* Pretend we've just finished compiling this function.  */
10580   save_fn = current_function_decl;
10581   current_function_decl = decl;
10582
10583   set_decl_abstract_flags (decl, 1);
10584   dwarf2out_decl (decl);
10585   if (! was_abstract)
10586     set_decl_abstract_flags (decl, 0);
10587
10588   current_function_decl = save_fn;
10589 }
10590
10591 /* Generate a DIE to represent a declared function (either file-scope or
10592    block-local).  */
10593
10594 static void
10595 gen_subprogram_die (decl, context_die)
10596      tree decl;
10597      dw_die_ref context_die;
10598 {
10599   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10600   tree origin = decl_ultimate_origin (decl);
10601   dw_die_ref subr_die;
10602   rtx fp_reg;
10603   tree fn_arg_types;
10604   tree outer_scope;
10605   dw_die_ref old_die = lookup_decl_die (decl);
10606   int declaration = (current_function_decl != decl
10607                      || class_scope_p (context_die));
10608
10609   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
10610      started to generate the abstract instance of an inline, decided to output
10611      its containing class, and proceeded to emit the declaration of the inline
10612      from the member list for the class.  If so, DECLARATION takes priority;
10613      we'll get back to the abstract instance when done with the class.  */
10614
10615   /* The class-scope declaration DIE must be the primary DIE.  */
10616   if (origin && declaration && class_scope_p (context_die))
10617     {
10618       origin = NULL;
10619       if (old_die)
10620         abort ();
10621     }
10622
10623   if (origin != NULL)
10624     {
10625       if (declaration && ! local_scope_p (context_die))
10626         abort ();
10627
10628       /* Fixup die_parent for the abstract instance of a nested
10629          inline function.  */
10630       if (old_die && old_die->die_parent == NULL)
10631         add_child_die (context_die, old_die);
10632
10633       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10634       add_abstract_origin_attribute (subr_die, origin);
10635     }
10636   else if (old_die)
10637     {
10638       unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10639
10640       if (!get_AT_flag (old_die, DW_AT_declaration)
10641           /* We can have a normal definition following an inline one in the
10642              case of redefinition of GNU C extern inlines.
10643              It seems reasonable to use AT_specification in this case.  */
10644           && !get_AT_unsigned (old_die, DW_AT_inline))
10645         {
10646           /* ??? This can happen if there is a bug in the program, for
10647              instance, if it has duplicate function definitions.  Ideally,
10648              we should detect this case and ignore it.  For now, if we have
10649              already reported an error, any error at all, then assume that
10650              we got here because of an input error, not a dwarf2 bug.  */
10651           if (errorcount)
10652             return;
10653           abort ();
10654         }
10655
10656       /* If the definition comes from the same place as the declaration,
10657          maybe use the old DIE.  We always want the DIE for this function
10658          that has the *_pc attributes to be under comp_unit_die so the
10659          debugger can find it.  We also need to do this for abstract
10660          instances of inlines, since the spec requires the out-of-line copy
10661          to have the same parent.  For local class methods, this doesn't
10662          apply; we just use the old DIE.  */
10663       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
10664           && (DECL_ARTIFICIAL (decl)
10665               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
10666                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
10667                       == (unsigned) DECL_SOURCE_LINE (decl)))))
10668         {
10669           subr_die = old_die;
10670
10671           /* Clear out the declaration attribute and the parm types.  */
10672           remove_AT (subr_die, DW_AT_declaration);
10673           remove_children (subr_die);
10674         }
10675       else
10676         {
10677           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10678           add_AT_die_ref (subr_die, DW_AT_specification, old_die);
10679           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10680             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
10681           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10682               != (unsigned) DECL_SOURCE_LINE (decl))
10683             add_AT_unsigned
10684               (subr_die, DW_AT_decl_line, DECL_SOURCE_LINE (decl));
10685         }
10686     }
10687   else
10688     {
10689       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
10690
10691       if (TREE_PUBLIC (decl))
10692         add_AT_flag (subr_die, DW_AT_external, 1);
10693
10694       add_name_and_src_coords_attributes (subr_die, decl);
10695       if (debug_info_level > DINFO_LEVEL_TERSE)
10696         {
10697           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
10698           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
10699                               0, 0, context_die);
10700         }
10701
10702       add_pure_or_virtual_attribute (subr_die, decl);
10703       if (DECL_ARTIFICIAL (decl))
10704         add_AT_flag (subr_die, DW_AT_artificial, 1);
10705
10706       if (TREE_PROTECTED (decl))
10707         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
10708       else if (TREE_PRIVATE (decl))
10709         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
10710     }
10711
10712   if (declaration)
10713     {
10714       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10715         {
10716           add_AT_flag (subr_die, DW_AT_declaration, 1);
10717
10718           /* The first time we see a member function, it is in the context of
10719              the class to which it belongs.  We make sure of this by emitting
10720              the class first.  The next time is the definition, which is
10721              handled above.  The two may come from the same source text.  */
10722           if (DECL_CONTEXT (decl) || DECL_ABSTRACT (decl))
10723             equate_decl_number_to_die (decl, subr_die);
10724         }
10725     }
10726   else if (DECL_ABSTRACT (decl))
10727     {
10728       if (DECL_INLINE (decl) && !flag_no_inline)
10729         {
10730           /* ??? Checking DECL_DEFER_OUTPUT is correct for static
10731              inline functions, but not for extern inline functions.
10732              We can't get this completely correct because information
10733              about whether the function was declared inline is not
10734              saved anywhere.  */
10735           if (DECL_DEFER_OUTPUT (decl))
10736             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
10737           else
10738             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
10739         }
10740       else
10741         add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
10742
10743       equate_decl_number_to_die (decl, subr_die);
10744     }
10745   else if (!DECL_EXTERNAL (decl))
10746     {
10747       if (!old_die || !get_AT_unsigned (old_die, DW_AT_inline))
10748         equate_decl_number_to_die (decl, subr_die);
10749
10750       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
10751                                    current_function_funcdef_no);
10752       add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
10753       ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10754                                    current_function_funcdef_no);
10755       add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
10756
10757       add_pubname (decl, subr_die);
10758       add_arange (decl, subr_die);
10759
10760 #ifdef MIPS_DEBUGGING_INFO
10761       /* Add a reference to the FDE for this routine.  */
10762       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
10763 #endif
10764
10765       /* Define the "frame base" location for this routine.  We use the
10766          frame pointer or stack pointer registers, since the RTL for local
10767          variables is relative to one of them.  */
10768       fp_reg
10769         = frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx;
10770       add_AT_loc (subr_die, DW_AT_frame_base, reg_loc_descriptor (fp_reg));
10771
10772 #if 0
10773       /* ??? This fails for nested inline functions, because context_display
10774          is not part of the state saved/restored for inline functions.  */
10775       if (current_function_needs_context)
10776         add_AT_location_description (subr_die, DW_AT_static_link,
10777                              loc_descriptor (lookup_static_chain (decl)));
10778 #endif
10779     }
10780
10781   /* Now output descriptions of the arguments for this function. This gets
10782      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
10783      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
10784      `...' at the end of the formal parameter list.  In order to find out if
10785      there was a trailing ellipsis or not, we must instead look at the type
10786      associated with the FUNCTION_DECL.  This will be a node of type
10787      FUNCTION_TYPE. If the chain of type nodes hanging off of this
10788      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
10789      an ellipsis at the end.  */
10790
10791   /* In the case where we are describing a mere function declaration, all we
10792      need to do here (and all we *can* do here) is to describe the *types* of
10793      its formal parameters.  */
10794   if (debug_info_level <= DINFO_LEVEL_TERSE)
10795     ;
10796   else if (declaration)
10797     gen_formal_types_die (decl, subr_die);
10798   else
10799     {
10800       /* Generate DIEs to represent all known formal parameters */
10801       tree arg_decls = DECL_ARGUMENTS (decl);
10802       tree parm;
10803
10804       /* When generating DIEs, generate the unspecified_parameters DIE
10805          instead if we come across the arg "__builtin_va_alist" */
10806       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
10807         if (TREE_CODE (parm) == PARM_DECL)
10808           {
10809             if (DECL_NAME (parm)
10810                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
10811                             "__builtin_va_alist"))
10812               gen_unspecified_parameters_die (parm, subr_die);
10813             else
10814               gen_decl_die (parm, subr_die);
10815           }
10816
10817       /* Decide whether we need an unspecified_parameters DIE at the end.
10818          There are 2 more cases to do this for: 1) the ansi ... declaration -
10819          this is detectable when the end of the arg list is not a
10820          void_type_node 2) an unprototyped function declaration (not a
10821          definition).  This just means that we have no info about the
10822          parameters at all.  */
10823       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
10824       if (fn_arg_types != NULL)
10825         {
10826           /* this is the prototyped case, check for ...  */
10827           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
10828             gen_unspecified_parameters_die (decl, subr_die);
10829         }
10830       else if (DECL_INITIAL (decl) == NULL_TREE)
10831         gen_unspecified_parameters_die (decl, subr_die);
10832     }
10833
10834   /* Output Dwarf info for all of the stuff within the body of the function
10835      (if it has one - it may be just a declaration).  */
10836   outer_scope = DECL_INITIAL (decl);
10837
10838   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
10839      a function.  This BLOCK actually represents the outermost binding contour
10840      for the function, i.e. the contour in which the function's formal
10841      parameters and labels get declared. Curiously, it appears that the front
10842      end doesn't actually put the PARM_DECL nodes for the current function onto
10843      the BLOCK_VARS list for this outer scope, but are strung off of the
10844      DECL_ARGUMENTS list for the function instead.
10845
10846      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
10847      the LABEL_DECL nodes for the function however, and we output DWARF info
10848      for those in decls_for_scope.  Just within the `outer_scope' there will be
10849      a BLOCK node representing the function's outermost pair of curly braces,
10850      and any blocks used for the base and member initializers of a C++
10851      constructor function.  */
10852   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
10853     {
10854       current_function_has_inlines = 0;
10855       decls_for_scope (outer_scope, subr_die, 0);
10856
10857 #if 0 && defined (MIPS_DEBUGGING_INFO)
10858       if (current_function_has_inlines)
10859         {
10860           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
10861           if (! comp_unit_has_inlines)
10862             {
10863               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
10864               comp_unit_has_inlines = 1;
10865             }
10866         }
10867 #endif
10868     }
10869 }
10870
10871 /* Generate a DIE to represent a declared data object.  */
10872
10873 static void
10874 gen_variable_die (decl, context_die)
10875      tree decl;
10876      dw_die_ref context_die;
10877 {
10878   tree origin = decl_ultimate_origin (decl);
10879   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
10880
10881   dw_die_ref old_die = lookup_decl_die (decl);
10882   int declaration = (DECL_EXTERNAL (decl)
10883                      || class_scope_p (context_die));
10884
10885   if (origin != NULL)
10886     add_abstract_origin_attribute (var_die, origin);
10887
10888   /* Loop unrolling can create multiple blocks that refer to the same
10889      static variable, so we must test for the DW_AT_declaration flag.
10890
10891      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
10892      copy decls and set the DECL_ABSTRACT flag on them instead of
10893      sharing them.
10894
10895      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
10896   else if (old_die && TREE_STATIC (decl)
10897            && get_AT_flag (old_die, DW_AT_declaration) == 1)
10898     {
10899       /* This is a definition of a C++ class level static.  */
10900       add_AT_die_ref (var_die, DW_AT_specification, old_die);
10901       if (DECL_NAME (decl))
10902         {
10903           unsigned file_index = lookup_filename (DECL_SOURCE_FILE (decl));
10904
10905           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
10906             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
10907
10908           if (get_AT_unsigned (old_die, DW_AT_decl_line)
10909               != (unsigned) DECL_SOURCE_LINE (decl))
10910
10911             add_AT_unsigned (var_die, DW_AT_decl_line,
10912                              DECL_SOURCE_LINE (decl));
10913         }
10914     }
10915   else
10916     {
10917       add_name_and_src_coords_attributes (var_die, decl);
10918       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
10919                           TREE_THIS_VOLATILE (decl), context_die);
10920
10921       if (TREE_PUBLIC (decl))
10922         add_AT_flag (var_die, DW_AT_external, 1);
10923
10924       if (DECL_ARTIFICIAL (decl))
10925         add_AT_flag (var_die, DW_AT_artificial, 1);
10926
10927       if (TREE_PROTECTED (decl))
10928         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
10929       else if (TREE_PRIVATE (decl))
10930         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
10931     }
10932
10933   if (declaration)
10934     add_AT_flag (var_die, DW_AT_declaration, 1);
10935
10936   if (class_scope_p (context_die) || DECL_ABSTRACT (decl))
10937     equate_decl_number_to_die (decl, var_die);
10938
10939   if (! declaration && ! DECL_ABSTRACT (decl))
10940     {
10941       add_location_or_const_value_attribute (var_die, decl);
10942       add_pubname (decl, var_die);
10943     }
10944   else
10945     tree_add_const_value_attribute (var_die, decl);
10946 }
10947
10948 /* Generate a DIE to represent a label identifier.  */
10949
10950 static void
10951 gen_label_die (decl, context_die)
10952      tree decl;
10953      dw_die_ref context_die;
10954 {
10955   tree origin = decl_ultimate_origin (decl);
10956   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
10957   rtx insn;
10958   char label[MAX_ARTIFICIAL_LABEL_BYTES];
10959
10960   if (origin != NULL)
10961     add_abstract_origin_attribute (lbl_die, origin);
10962   else
10963     add_name_and_src_coords_attributes (lbl_die, decl);
10964
10965   if (DECL_ABSTRACT (decl))
10966     equate_decl_number_to_die (decl, lbl_die);
10967   else
10968     {
10969       insn = DECL_RTL (decl);
10970
10971       /* Deleted labels are programmer specified labels which have been
10972          eliminated because of various optimisations.  We still emit them
10973          here so that it is possible to put breakpoints on them.  */
10974       if (GET_CODE (insn) == CODE_LABEL
10975           || ((GET_CODE (insn) == NOTE
10976                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
10977         {
10978           /* When optimization is enabled (via -O) some parts of the compiler
10979              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
10980              represent source-level labels which were explicitly declared by
10981              the user.  This really shouldn't be happening though, so catch
10982              it if it ever does happen.  */
10983           if (INSN_DELETED_P (insn))
10984             abort ();
10985
10986           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
10987           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
10988         }
10989     }
10990 }
10991
10992 /* Generate a DIE for a lexical block.  */
10993
10994 static void
10995 gen_lexical_block_die (stmt, context_die, depth)
10996      tree stmt;
10997      dw_die_ref context_die;
10998      int depth;
10999 {
11000   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11001   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11002
11003   if (! BLOCK_ABSTRACT (stmt))
11004     {
11005       if (BLOCK_FRAGMENT_CHAIN (stmt))
11006         {
11007           tree chain;
11008
11009           add_AT_range_list (stmt_die, DW_AT_ranges, add_ranges (stmt));
11010
11011           chain = BLOCK_FRAGMENT_CHAIN (stmt);
11012           do
11013             {
11014               add_ranges (chain);
11015               chain = BLOCK_FRAGMENT_CHAIN (chain);
11016             }
11017           while (chain);
11018           add_ranges (NULL);
11019         }
11020       else
11021         {
11022           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11023                                        BLOCK_NUMBER (stmt));
11024           add_AT_lbl_id (stmt_die, DW_AT_low_pc, label);
11025           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11026                                        BLOCK_NUMBER (stmt));
11027           add_AT_lbl_id (stmt_die, DW_AT_high_pc, label);
11028         }
11029     }
11030
11031   decls_for_scope (stmt, stmt_die, depth);
11032 }
11033
11034 /* Generate a DIE for an inlined subprogram.  */
11035
11036 static void
11037 gen_inlined_subroutine_die (stmt, context_die, depth)
11038      tree stmt;
11039      dw_die_ref context_die;
11040      int depth;
11041 {
11042   if (! BLOCK_ABSTRACT (stmt))
11043     {
11044       dw_die_ref subr_die
11045         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
11046       tree decl = block_ultimate_origin (stmt);
11047       char label[MAX_ARTIFICIAL_LABEL_BYTES];
11048
11049       /* Emit info for the abstract instance first, if we haven't yet.  */
11050       dwarf2out_abstract_function (decl);
11051
11052       add_abstract_origin_attribute (subr_die, decl);
11053       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11054                                    BLOCK_NUMBER (stmt));
11055       add_AT_lbl_id (subr_die, DW_AT_low_pc, label);
11056       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11057                                    BLOCK_NUMBER (stmt));
11058       add_AT_lbl_id (subr_die, DW_AT_high_pc, label);
11059       decls_for_scope (stmt, subr_die, depth);
11060       current_function_has_inlines = 1;
11061     }
11062   else
11063     /* We may get here if we're the outer block of function A that was
11064        inlined into function B that was inlined into function C.  When
11065        generating debugging info for C, dwarf2out_abstract_function(B)
11066        would mark all inlined blocks as abstract, including this one.
11067        So, we wouldn't (and shouldn't) expect labels to be generated
11068        for this one.  Instead, just emit debugging info for
11069        declarations within the block.  This is particularly important
11070        in the case of initializers of arguments passed from B to us:
11071        if they're statement expressions containing declarations, we
11072        wouldn't generate dies for their abstract variables, and then,
11073        when generating dies for the real variables, we'd die (pun
11074        intended :-)  */
11075     gen_lexical_block_die (stmt, context_die, depth);
11076 }
11077
11078 /* Generate a DIE for a field in a record, or structure.  */
11079
11080 static void
11081 gen_field_die (decl, context_die)
11082      tree decl;
11083      dw_die_ref context_die;
11084 {
11085   dw_die_ref decl_die = new_die (DW_TAG_member, context_die, decl);
11086
11087   add_name_and_src_coords_attributes (decl_die, decl);
11088   add_type_attribute (decl_die, member_declared_type (decl),
11089                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
11090                       context_die);
11091
11092   if (DECL_BIT_FIELD_TYPE (decl))
11093     {
11094       add_byte_size_attribute (decl_die, decl);
11095       add_bit_size_attribute (decl_die, decl);
11096       add_bit_offset_attribute (decl_die, decl);
11097     }
11098
11099   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
11100     add_data_member_location_attribute (decl_die, decl);
11101
11102   if (DECL_ARTIFICIAL (decl))
11103     add_AT_flag (decl_die, DW_AT_artificial, 1);
11104
11105   if (TREE_PROTECTED (decl))
11106     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
11107   else if (TREE_PRIVATE (decl))
11108     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
11109 }
11110
11111 #if 0
11112 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11113    Use modified_type_die instead.
11114    We keep this code here just in case these types of DIEs may be needed to
11115    represent certain things in other languages (e.g. Pascal) someday.  */
11116
11117 static void
11118 gen_pointer_type_die (type, context_die)
11119      tree type;
11120      dw_die_ref context_die;
11121 {
11122   dw_die_ref ptr_die
11123     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
11124
11125   equate_type_number_to_die (type, ptr_die);
11126   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11127   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11128 }
11129
11130 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
11131    Use modified_type_die instead.
11132    We keep this code here just in case these types of DIEs may be needed to
11133    represent certain things in other languages (e.g. Pascal) someday.  */
11134
11135 static void
11136 gen_reference_type_die (type, context_die)
11137      tree type;
11138      dw_die_ref context_die;
11139 {
11140   dw_die_ref ref_die
11141     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
11142
11143   equate_type_number_to_die (type, ref_die);
11144   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
11145   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
11146 }
11147 #endif
11148
11149 /* Generate a DIE for a pointer to a member type.  */
11150
11151 static void
11152 gen_ptr_to_mbr_type_die (type, context_die)
11153      tree type;
11154      dw_die_ref context_die;
11155 {
11156   dw_die_ref ptr_die
11157     = new_die (DW_TAG_ptr_to_member_type,
11158                scope_die_for (type, context_die), type);
11159
11160   equate_type_number_to_die (type, ptr_die);
11161   add_AT_die_ref (ptr_die, DW_AT_containing_type,
11162                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
11163   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
11164 }
11165
11166 /* Generate the DIE for the compilation unit.  */
11167
11168 static dw_die_ref
11169 gen_compile_unit_die (filename)
11170      const char *filename;
11171 {
11172   dw_die_ref die;
11173   char producer[250];
11174   const char *wd = getpwd ();
11175   const char *language_string = lang_hooks.name;
11176   int language;
11177
11178   die = new_die (DW_TAG_compile_unit, NULL, NULL);
11179   add_name_attribute (die, filename);
11180
11181   if (wd != NULL && filename[0] != DIR_SEPARATOR)
11182     add_AT_string (die, DW_AT_comp_dir, wd);
11183
11184   sprintf (producer, "%s %s", language_string, version_string);
11185
11186 #ifdef MIPS_DEBUGGING_INFO
11187   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
11188      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
11189      not appear in the producer string, the debugger reaches the conclusion
11190      that the object file is stripped and has no debugging information.
11191      To get the MIPS/SGI debugger to believe that there is debugging
11192      information in the object file, we add a -g to the producer string.  */
11193   if (debug_info_level > DINFO_LEVEL_TERSE)
11194     strcat (producer, " -g");
11195 #endif
11196
11197   add_AT_string (die, DW_AT_producer, producer);
11198
11199   if (strcmp (language_string, "GNU C++") == 0)
11200     language = DW_LANG_C_plus_plus;
11201   else if (strcmp (language_string, "GNU Ada") == 0)
11202     language = DW_LANG_Ada83;
11203   else if (strcmp (language_string, "GNU F77") == 0)
11204     language = DW_LANG_Fortran77;
11205   else if (strcmp (language_string, "GNU Pascal") == 0)
11206     language = DW_LANG_Pascal83;
11207   else if (strcmp (language_string, "GNU Java") == 0)
11208     language = DW_LANG_Java;
11209   else
11210     language = DW_LANG_C89;
11211
11212   add_AT_unsigned (die, DW_AT_language, language);
11213   return die;
11214 }
11215
11216 /* Generate a DIE for a string type.  */
11217
11218 static void
11219 gen_string_type_die (type, context_die)
11220      tree type;
11221      dw_die_ref context_die;
11222 {
11223   dw_die_ref type_die
11224     = new_die (DW_TAG_string_type, scope_die_for (type, context_die), type);
11225
11226   equate_type_number_to_die (type, type_die);
11227
11228   /* ??? Fudge the string length attribute for now.
11229      TODO: add string length info.  */
11230 #if 0
11231   string_length_attribute (TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
11232   bound_representation (upper_bound, 0, 'u');
11233 #endif
11234 }
11235
11236 /* Generate the DIE for a base class.  */
11237
11238 static void
11239 gen_inheritance_die (binfo, context_die)
11240      tree binfo;
11241      dw_die_ref context_die;
11242 {
11243   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
11244
11245   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
11246   add_data_member_location_attribute (die, binfo);
11247
11248   if (TREE_VIA_VIRTUAL (binfo))
11249     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11250
11251   if (TREE_VIA_PUBLIC (binfo))
11252     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
11253   else if (TREE_VIA_PROTECTED (binfo))
11254     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
11255 }
11256
11257 /* Generate a DIE for a class member.  */
11258
11259 static void
11260 gen_member_die (type, context_die)
11261      tree type;
11262      dw_die_ref context_die;
11263 {
11264   tree member;
11265   dw_die_ref child;
11266
11267   /* If this is not an incomplete type, output descriptions of each of its
11268      members. Note that as we output the DIEs necessary to represent the
11269      members of this record or union type, we will also be trying to output
11270      DIEs to represent the *types* of those members. However the `type'
11271      function (above) will specifically avoid generating type DIEs for member
11272      types *within* the list of member DIEs for this (containing) type except
11273      for those types (of members) which are explicitly marked as also being
11274      members of this (containing) type themselves.  The g++ front- end can
11275      force any given type to be treated as a member of some other (containing)
11276      type by setting the TYPE_CONTEXT of the given (member) type to point to
11277      the TREE node representing the appropriate (containing) type.  */
11278
11279   /* First output info about the base classes.  */
11280   if (TYPE_BINFO (type) && TYPE_BINFO_BASETYPES (type))
11281     {
11282       tree bases = TYPE_BINFO_BASETYPES (type);
11283       int n_bases = TREE_VEC_LENGTH (bases);
11284       int i;
11285
11286       for (i = 0; i < n_bases; i++)
11287         gen_inheritance_die (TREE_VEC_ELT (bases, i), context_die);
11288     }
11289
11290   /* Now output info about the data members and type members.  */
11291   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
11292     {
11293       /* If we thought we were generating minimal debug info for TYPE
11294          and then changed our minds, some of the member declarations
11295          may have already been defined.  Don't define them again, but
11296          do put them in the right order.  */
11297
11298       child = lookup_decl_die (member);
11299       if (child)
11300         splice_child_die (context_die, child);
11301       else
11302         gen_decl_die (member, context_die);
11303     }
11304
11305   /* Now output info about the function members (if any).  */
11306   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
11307     {
11308       /* Don't include clones in the member list.  */
11309       if (DECL_ABSTRACT_ORIGIN (member))
11310         continue;
11311
11312       child = lookup_decl_die (member);
11313       if (child)
11314         splice_child_die (context_die, child);
11315       else
11316         gen_decl_die (member, context_die);
11317     }
11318 }
11319
11320 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
11321    is set, we pretend that the type was never defined, so we only get the
11322    member DIEs needed by later specification DIEs.  */
11323
11324 static void
11325 gen_struct_or_union_type_die (type, context_die)
11326      tree type;
11327      dw_die_ref context_die;
11328 {
11329   dw_die_ref type_die = lookup_type_die (type);
11330   dw_die_ref scope_die = 0;
11331   int nested = 0;
11332   int complete = (TYPE_SIZE (type)
11333                   && (! TYPE_STUB_DECL (type)
11334                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
11335
11336   if (type_die && ! complete)
11337     return;
11338
11339   if (TYPE_CONTEXT (type) != NULL_TREE
11340       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type)))
11341     nested = 1;
11342
11343   scope_die = scope_die_for (type, context_die);
11344
11345   if (! type_die || (nested && scope_die == comp_unit_die))
11346     /* First occurrence of type or toplevel definition of nested class.  */
11347     {
11348       dw_die_ref old_die = type_die;
11349
11350       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
11351                           ? DW_TAG_structure_type : DW_TAG_union_type,
11352                           scope_die, type);
11353       equate_type_number_to_die (type, type_die);
11354       if (old_die)
11355         add_AT_die_ref (type_die, DW_AT_specification, old_die);
11356       else
11357         add_name_attribute (type_die, type_tag (type));
11358     }
11359   else
11360     remove_AT (type_die, DW_AT_declaration);
11361
11362   /* If this type has been completed, then give it a byte_size attribute and
11363      then give a list of members.  */
11364   if (complete)
11365     {
11366       /* Prevent infinite recursion in cases where the type of some member of
11367          this type is expressed in terms of this type itself.  */
11368       TREE_ASM_WRITTEN (type) = 1;
11369       add_byte_size_attribute (type_die, type);
11370       if (TYPE_STUB_DECL (type) != NULL_TREE)
11371         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11372
11373       /* If the first reference to this type was as the return type of an
11374          inline function, then it may not have a parent.  Fix this now.  */
11375       if (type_die->die_parent == NULL)
11376         add_child_die (scope_die, type_die);
11377
11378       push_decl_scope (type);
11379       gen_member_die (type, type_die);
11380       pop_decl_scope ();
11381
11382       /* GNU extension: Record what type our vtable lives in.  */
11383       if (TYPE_VFIELD (type))
11384         {
11385           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
11386
11387           gen_type_die (vtype, context_die);
11388           add_AT_die_ref (type_die, DW_AT_containing_type,
11389                           lookup_type_die (vtype));
11390         }
11391     }
11392   else
11393     {
11394       add_AT_flag (type_die, DW_AT_declaration, 1);
11395
11396       /* We don't need to do this for function-local types.  */
11397       if (TYPE_STUB_DECL (type)
11398           && ! decl_function_context (TYPE_STUB_DECL (type)))
11399         VARRAY_PUSH_TREE (incomplete_types, type);
11400     }
11401 }
11402
11403 /* Generate a DIE for a subroutine _type_.  */
11404
11405 static void
11406 gen_subroutine_type_die (type, context_die)
11407      tree type;
11408      dw_die_ref context_die;
11409 {
11410   tree return_type = TREE_TYPE (type);
11411   dw_die_ref subr_die
11412     = new_die (DW_TAG_subroutine_type,
11413                scope_die_for (type, context_die), type);
11414
11415   equate_type_number_to_die (type, subr_die);
11416   add_prototyped_attribute (subr_die, type);
11417   add_type_attribute (subr_die, return_type, 0, 0, context_die);
11418   gen_formal_types_die (type, subr_die);
11419 }
11420
11421 /* Generate a DIE for a type definition */
11422
11423 static void
11424 gen_typedef_die (decl, context_die)
11425      tree decl;
11426      dw_die_ref context_die;
11427 {
11428   dw_die_ref type_die;
11429   tree origin;
11430
11431   if (TREE_ASM_WRITTEN (decl))
11432     return;
11433
11434   TREE_ASM_WRITTEN (decl) = 1;
11435   type_die = new_die (DW_TAG_typedef, context_die, decl);
11436   origin = decl_ultimate_origin (decl);
11437   if (origin != NULL)
11438     add_abstract_origin_attribute (type_die, origin);
11439   else
11440     {
11441       tree type;
11442
11443       add_name_and_src_coords_attributes (type_die, decl);
11444       if (DECL_ORIGINAL_TYPE (decl))
11445         {
11446           type = DECL_ORIGINAL_TYPE (decl);
11447
11448           if (type == TREE_TYPE (decl))
11449             abort ();
11450           else
11451             equate_type_number_to_die (TREE_TYPE (decl), type_die);
11452         }
11453       else
11454         type = TREE_TYPE (decl);
11455
11456       add_type_attribute (type_die, type, TREE_READONLY (decl),
11457                           TREE_THIS_VOLATILE (decl), context_die);
11458     }
11459
11460   if (DECL_ABSTRACT (decl))
11461     equate_decl_number_to_die (decl, type_die);
11462 }
11463
11464 /* Generate a type description DIE.  */
11465
11466 static void
11467 gen_type_die (type, context_die)
11468      tree type;
11469      dw_die_ref context_die;
11470 {
11471   int need_pop;
11472
11473   if (type == NULL_TREE || type == error_mark_node)
11474     return;
11475
11476   /* We are going to output a DIE to represent the unqualified version
11477      of this type (i.e. without any const or volatile qualifiers) so
11478      get the main variant (i.e. the unqualified version) of this type
11479      now.  (Vectors are special because the debugging info is in the
11480      cloned type itself).  */
11481   if (TREE_CODE (type) != VECTOR_TYPE)
11482     type = type_main_variant (type);
11483
11484   if (TREE_ASM_WRITTEN (type))
11485     return;
11486
11487   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11488       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
11489     {
11490       /* Prevent broken recursion; we can't hand off to the same type.  */
11491       if (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) == type)
11492         abort ();
11493
11494       TREE_ASM_WRITTEN (type) = 1;
11495       gen_decl_die (TYPE_NAME (type), context_die);
11496       return;
11497     }
11498
11499   switch (TREE_CODE (type))
11500     {
11501     case ERROR_MARK:
11502       break;
11503
11504     case POINTER_TYPE:
11505     case REFERENCE_TYPE:
11506       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
11507          ensures that the gen_type_die recursion will terminate even if the
11508          type is recursive.  Recursive types are possible in Ada.  */
11509       /* ??? We could perhaps do this for all types before the switch
11510          statement.  */
11511       TREE_ASM_WRITTEN (type) = 1;
11512
11513       /* For these types, all that is required is that we output a DIE (or a
11514          set of DIEs) to represent the "basis" type.  */
11515       gen_type_die (TREE_TYPE (type), context_die);
11516       break;
11517
11518     case OFFSET_TYPE:
11519       /* This code is used for C++ pointer-to-data-member types.
11520          Output a description of the relevant class type.  */
11521       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
11522
11523       /* Output a description of the type of the object pointed to.  */
11524       gen_type_die (TREE_TYPE (type), context_die);
11525
11526       /* Now output a DIE to represent this pointer-to-data-member type
11527          itself.  */
11528       gen_ptr_to_mbr_type_die (type, context_die);
11529       break;
11530
11531     case SET_TYPE:
11532       gen_type_die (TYPE_DOMAIN (type), context_die);
11533       gen_set_type_die (type, context_die);
11534       break;
11535
11536     case FILE_TYPE:
11537       gen_type_die (TREE_TYPE (type), context_die);
11538       abort ();                 /* No way to represent these in Dwarf yet!  */
11539       break;
11540
11541     case FUNCTION_TYPE:
11542       /* Force out return type (in case it wasn't forced out already).  */
11543       gen_type_die (TREE_TYPE (type), context_die);
11544       gen_subroutine_type_die (type, context_die);
11545       break;
11546
11547     case METHOD_TYPE:
11548       /* Force out return type (in case it wasn't forced out already).  */
11549       gen_type_die (TREE_TYPE (type), context_die);
11550       gen_subroutine_type_die (type, context_die);
11551       break;
11552
11553     case ARRAY_TYPE:
11554       if (TYPE_STRING_FLAG (type) && TREE_CODE (TREE_TYPE (type)) == CHAR_TYPE)
11555         {
11556           gen_type_die (TREE_TYPE (type), context_die);
11557           gen_string_type_die (type, context_die);
11558         }
11559       else
11560         gen_array_type_die (type, context_die);
11561       break;
11562
11563     case VECTOR_TYPE:
11564       gen_array_type_die (type, context_die);
11565       break;
11566
11567     case ENUMERAL_TYPE:
11568     case RECORD_TYPE:
11569     case UNION_TYPE:
11570     case QUAL_UNION_TYPE:
11571       /* If this is a nested type whose containing class hasn't been written
11572          out yet, writing it out will cover this one, too.  This does not apply
11573          to instantiations of member class templates; they need to be added to
11574          the containing class as they are generated.  FIXME: This hurts the
11575          idea of combining type decls from multiple TUs, since we can't predict
11576          what set of template instantiations we'll get.  */
11577       if (TYPE_CONTEXT (type)
11578           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
11579           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
11580         {
11581           gen_type_die (TYPE_CONTEXT (type), context_die);
11582
11583           if (TREE_ASM_WRITTEN (type))
11584             return;
11585
11586           /* If that failed, attach ourselves to the stub.  */
11587           push_decl_scope (TYPE_CONTEXT (type));
11588           context_die = lookup_type_die (TYPE_CONTEXT (type));
11589           need_pop = 1;
11590         }
11591       else
11592         need_pop = 0;
11593
11594       if (TREE_CODE (type) == ENUMERAL_TYPE)
11595         gen_enumeration_type_die (type, context_die);
11596       else
11597         gen_struct_or_union_type_die (type, context_die);
11598
11599       if (need_pop)
11600         pop_decl_scope ();
11601
11602       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
11603          it up if it is ever completed.  gen_*_type_die will set it for us
11604          when appropriate.  */
11605       return;
11606
11607     case VOID_TYPE:
11608     case INTEGER_TYPE:
11609     case REAL_TYPE:
11610     case COMPLEX_TYPE:
11611     case BOOLEAN_TYPE:
11612     case CHAR_TYPE:
11613       /* No DIEs needed for fundamental types.  */
11614       break;
11615
11616     case LANG_TYPE:
11617       /* No Dwarf representation currently defined.  */
11618       break;
11619
11620     default:
11621       abort ();
11622     }
11623
11624   TREE_ASM_WRITTEN (type) = 1;
11625 }
11626
11627 /* Generate a DIE for a tagged type instantiation.  */
11628
11629 static void
11630 gen_tagged_type_instantiation_die (type, context_die)
11631      tree type;
11632      dw_die_ref context_die;
11633 {
11634   if (type == NULL_TREE || type == error_mark_node)
11635     return;
11636
11637   /* We are going to output a DIE to represent the unqualified version of
11638      this type (i.e. without any const or volatile qualifiers) so make sure
11639      that we have the main variant (i.e. the unqualified version) of this
11640      type now.  */
11641   if (type != type_main_variant (type))
11642     abort ();
11643
11644   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
11645      an instance of an unresolved type.  */
11646
11647   switch (TREE_CODE (type))
11648     {
11649     case ERROR_MARK:
11650       break;
11651
11652     case ENUMERAL_TYPE:
11653       gen_inlined_enumeration_type_die (type, context_die);
11654       break;
11655
11656     case RECORD_TYPE:
11657       gen_inlined_structure_type_die (type, context_die);
11658       break;
11659
11660     case UNION_TYPE:
11661     case QUAL_UNION_TYPE:
11662       gen_inlined_union_type_die (type, context_die);
11663       break;
11664
11665     default:
11666       abort ();
11667     }
11668 }
11669
11670 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
11671    things which are local to the given block.  */
11672
11673 static void
11674 gen_block_die (stmt, context_die, depth)
11675      tree stmt;
11676      dw_die_ref context_die;
11677      int depth;
11678 {
11679   int must_output_die = 0;
11680   tree origin;
11681   tree decl;
11682   enum tree_code origin_code;
11683
11684   /* Ignore blocks never really used to make RTL.  */
11685   if (stmt == NULL_TREE || !TREE_USED (stmt)
11686       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
11687     return;
11688
11689   /* If the block is one fragment of a non-contiguous block, do not
11690      process the variables, since they will have been done by the
11691      origin block.  Do process subblocks.  */
11692   if (BLOCK_FRAGMENT_ORIGIN (stmt))
11693     {
11694       tree sub;
11695
11696       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
11697         gen_block_die (sub, context_die, depth + 1);
11698
11699       return;
11700     }
11701
11702   /* Determine the "ultimate origin" of this block.  This block may be an
11703      inlined instance of an inlined instance of inline function, so we have
11704      to trace all of the way back through the origin chain to find out what
11705      sort of node actually served as the original seed for the creation of
11706      the current block.  */
11707   origin = block_ultimate_origin (stmt);
11708   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
11709
11710   /* Determine if we need to output any Dwarf DIEs at all to represent this
11711      block.  */
11712   if (origin_code == FUNCTION_DECL)
11713     /* The outer scopes for inlinings *must* always be represented.  We
11714        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
11715     must_output_die = 1;
11716   else
11717     {
11718       /* In the case where the current block represents an inlining of the
11719          "body block" of an inline function, we must *NOT* output any DIE for
11720          this block because we have already output a DIE to represent the whole
11721          inlined function scope and the "body block" of any function doesn't
11722          really represent a different scope according to ANSI C rules.  So we
11723          check here to make sure that this block does not represent a "body
11724          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
11725       if (! is_body_block (origin ? origin : stmt))
11726         {
11727           /* Determine if this block directly contains any "significant"
11728              local declarations which we will need to output DIEs for.  */
11729           if (debug_info_level > DINFO_LEVEL_TERSE)
11730             /* We are not in terse mode so *any* local declaration counts
11731                as being a "significant" one.  */
11732             must_output_die = (BLOCK_VARS (stmt) != NULL);
11733           else
11734             /* We are in terse mode, so only local (nested) function
11735                definitions count as "significant" local declarations.  */
11736             for (decl = BLOCK_VARS (stmt);
11737                  decl != NULL; decl = TREE_CHAIN (decl))
11738               if (TREE_CODE (decl) == FUNCTION_DECL
11739                   && DECL_INITIAL (decl))
11740                 {
11741                   must_output_die = 1;
11742                   break;
11743                 }
11744         }
11745     }
11746
11747   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
11748      DIE for any block which contains no significant local declarations at
11749      all.  Rather, in such cases we just call `decls_for_scope' so that any
11750      needed Dwarf info for any sub-blocks will get properly generated. Note
11751      that in terse mode, our definition of what constitutes a "significant"
11752      local declaration gets restricted to include only inlined function
11753      instances and local (nested) function definitions.  */
11754   if (must_output_die)
11755     {
11756       if (origin_code == FUNCTION_DECL)
11757         gen_inlined_subroutine_die (stmt, context_die, depth);
11758       else
11759         gen_lexical_block_die (stmt, context_die, depth);
11760     }
11761   else
11762     decls_for_scope (stmt, context_die, depth);
11763 }
11764
11765 /* Generate all of the decls declared within a given scope and (recursively)
11766    all of its sub-blocks.  */
11767
11768 static void
11769 decls_for_scope (stmt, context_die, depth)
11770      tree stmt;
11771      dw_die_ref context_die;
11772      int depth;
11773 {
11774   tree decl;
11775   tree subblocks;
11776
11777   /* Ignore blocks never really used to make RTL.  */
11778   if (stmt == NULL_TREE || ! TREE_USED (stmt))
11779     return;
11780
11781   /* Output the DIEs to represent all of the data objects and typedefs
11782      declared directly within this block but not within any nested
11783      sub-blocks.  Also, nested function and tag DIEs have been
11784      generated with a parent of NULL; fix that up now.  */
11785   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
11786     {
11787       dw_die_ref die;
11788
11789       if (TREE_CODE (decl) == FUNCTION_DECL)
11790         die = lookup_decl_die (decl);
11791       else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
11792         die = lookup_type_die (TREE_TYPE (decl));
11793       else
11794         die = NULL;
11795
11796       if (die != NULL && die->die_parent == NULL)
11797         add_child_die (context_die, die);
11798       else
11799         gen_decl_die (decl, context_die);
11800     }
11801
11802   /* Output the DIEs to represent all sub-blocks (and the items declared
11803      therein) of this block.  */
11804   for (subblocks = BLOCK_SUBBLOCKS (stmt);
11805        subblocks != NULL;
11806        subblocks = BLOCK_CHAIN (subblocks))
11807     gen_block_die (subblocks, context_die, depth + 1);
11808 }
11809
11810 /* Is this a typedef we can avoid emitting?  */
11811
11812 static inline int
11813 is_redundant_typedef (decl)
11814      tree decl;
11815 {
11816   if (TYPE_DECL_IS_STUB (decl))
11817     return 1;
11818
11819   if (DECL_ARTIFICIAL (decl)
11820       && DECL_CONTEXT (decl)
11821       && is_tagged_type (DECL_CONTEXT (decl))
11822       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
11823       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
11824     /* Also ignore the artificial member typedef for the class name.  */
11825     return 1;
11826
11827   return 0;
11828 }
11829
11830 /* Generate Dwarf debug information for a decl described by DECL.  */
11831
11832 static void
11833 gen_decl_die (decl, context_die)
11834      tree decl;
11835      dw_die_ref context_die;
11836 {
11837   tree origin;
11838
11839   if (DECL_P (decl) && DECL_IGNORED_P (decl))
11840     return;
11841
11842   switch (TREE_CODE (decl))
11843     {
11844     case ERROR_MARK:
11845       break;
11846
11847     case CONST_DECL:
11848       /* The individual enumerators of an enum type get output when we output
11849          the Dwarf representation of the relevant enum type itself.  */
11850       break;
11851
11852     case FUNCTION_DECL:
11853       /* Don't output any DIEs to represent mere function declarations,
11854          unless they are class members or explicit block externs.  */
11855       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
11856           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
11857         break;
11858
11859       /* If we're emitting a clone, emit info for the abstract instance.  */
11860       if (DECL_ORIGIN (decl) != decl)
11861         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
11862
11863       /* If we're emitting an out-of-line copy of an inline function,
11864          emit info for the abstract instance and set up to refer to it.  */
11865       else if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
11866                && ! class_scope_p (context_die)
11867                /* dwarf2out_abstract_function won't emit a die if this is just
11868                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
11869                   that case, because that works only if we have a die.  */
11870                && DECL_INITIAL (decl) != NULL_TREE)
11871         {
11872           dwarf2out_abstract_function (decl);
11873           set_decl_origin_self (decl);
11874         }
11875
11876       /* Otherwise we're emitting the primary DIE for this decl.  */
11877       else if (debug_info_level > DINFO_LEVEL_TERSE)
11878         {
11879           /* Before we describe the FUNCTION_DECL itself, make sure that we
11880              have described its return type.  */
11881           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
11882
11883           /* And its virtual context.  */
11884           if (DECL_VINDEX (decl) != NULL_TREE)
11885             gen_type_die (DECL_CONTEXT (decl), context_die);
11886
11887           /* And its containing type.  */
11888           origin = decl_class_context (decl);
11889           if (origin != NULL_TREE)
11890             gen_type_die_for_member (origin, decl, context_die);
11891         }
11892
11893       /* Now output a DIE to represent the function itself.  */
11894       gen_subprogram_die (decl, context_die);
11895       break;
11896
11897     case TYPE_DECL:
11898       /* If we are in terse mode, don't generate any DIEs to represent any
11899          actual typedefs.  */
11900       if (debug_info_level <= DINFO_LEVEL_TERSE)
11901         break;
11902
11903       /* In the special case of a TYPE_DECL node representing the declaration
11904          of some type tag, if the given TYPE_DECL is marked as having been
11905          instantiated from some other (original) TYPE_DECL node (e.g. one which
11906          was generated within the original definition of an inline function) we
11907          have to generate a special (abbreviated) DW_TAG_structure_type,
11908          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
11909       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
11910         {
11911           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
11912           break;
11913         }
11914
11915       if (is_redundant_typedef (decl))
11916         gen_type_die (TREE_TYPE (decl), context_die);
11917       else
11918         /* Output a DIE to represent the typedef itself.  */
11919         gen_typedef_die (decl, context_die);
11920       break;
11921
11922     case LABEL_DECL:
11923       if (debug_info_level >= DINFO_LEVEL_NORMAL)
11924         gen_label_die (decl, context_die);
11925       break;
11926
11927     case VAR_DECL:
11928       /* If we are in terse mode, don't generate any DIEs to represent any
11929          variable declarations or definitions.  */
11930       if (debug_info_level <= DINFO_LEVEL_TERSE)
11931         break;
11932
11933       /* Output any DIEs that are needed to specify the type of this data
11934          object.  */
11935       gen_type_die (TREE_TYPE (decl), context_die);
11936
11937       /* And its containing type.  */
11938       origin = decl_class_context (decl);
11939       if (origin != NULL_TREE)
11940         gen_type_die_for_member (origin, decl, context_die);
11941
11942       /* Now output the DIE to represent the data object itself.  This gets
11943          complicated because of the possibility that the VAR_DECL really
11944          represents an inlined instance of a formal parameter for an inline
11945          function.  */
11946       origin = decl_ultimate_origin (decl);
11947       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
11948         gen_formal_parameter_die (decl, context_die);
11949       else
11950         gen_variable_die (decl, context_die);
11951       break;
11952
11953     case FIELD_DECL:
11954       /* Ignore the nameless fields that are used to skip bits but handle C++
11955          anonymous unions.  */
11956       if (DECL_NAME (decl) != NULL_TREE
11957           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
11958         {
11959           gen_type_die (member_declared_type (decl), context_die);
11960           gen_field_die (decl, context_die);
11961         }
11962       break;
11963
11964     case PARM_DECL:
11965       gen_type_die (TREE_TYPE (decl), context_die);
11966       gen_formal_parameter_die (decl, context_die);
11967       break;
11968
11969     case NAMESPACE_DECL:
11970       /* Ignore for now.  */
11971       break;
11972
11973     default:
11974       abort ();
11975     }
11976 }
11977
11978 static void
11979 mark_limbo_die_list (ptr)
11980      void *ptr ATTRIBUTE_UNUSED;
11981 {
11982   limbo_die_node *node;
11983   for (node = limbo_die_list; node; node = node->next)
11984     ggc_mark_tree (node->created_for);
11985 }
11986 \f
11987 /* Add Ada "use" clause information for SGI Workshop debugger.  */
11988
11989 void
11990 dwarf2out_add_library_unit_info (filename, context_list)
11991      const char *filename;
11992      const char *context_list;
11993 {
11994   unsigned int file_index;
11995
11996   if (filename != NULL)
11997     {
11998       dw_die_ref unit_die = new_die (DW_TAG_module, comp_unit_die, NULL);
11999       tree context_list_decl
12000         = build_decl (LABEL_DECL, get_identifier (context_list),
12001                       void_type_node);
12002
12003       TREE_PUBLIC (context_list_decl) = TRUE;
12004       add_name_attribute (unit_die, context_list);
12005       file_index = lookup_filename (filename);
12006       add_AT_unsigned (unit_die, DW_AT_decl_file, file_index);
12007       add_pubname (context_list_decl, unit_die);
12008     }
12009 }
12010
12011 /* Output debug information for global decl DECL.  Called from toplev.c after
12012    compilation proper has finished.  */
12013
12014 static void
12015 dwarf2out_global_decl (decl)
12016      tree decl;
12017 {
12018   /* Output DWARF2 information for file-scope tentative data object
12019      declarations, file-scope (extern) function declarations (which had no
12020      corresponding body) and file-scope tagged type declarations and
12021      definitions which have not yet been forced out.  */
12022   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
12023     dwarf2out_decl (decl);
12024 }
12025
12026 /* Write the debugging output for DECL.  */
12027
12028 void
12029 dwarf2out_decl (decl)
12030      tree decl;
12031 {
12032   dw_die_ref context_die = comp_unit_die;
12033
12034   switch (TREE_CODE (decl))
12035     {
12036     case ERROR_MARK:
12037       return;
12038
12039     case FUNCTION_DECL:
12040       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of a
12041          builtin function.  Explicit programmer-supplied declarations of
12042          these same functions should NOT be ignored however.  */
12043       if (DECL_EXTERNAL (decl) && DECL_BUILT_IN (decl))
12044         return;
12045
12046       /* What we would really like to do here is to filter out all mere
12047          file-scope declarations of file-scope functions which are never
12048          referenced later within this translation unit (and keep all of ones
12049          that *are* referenced later on) but we aren't clairvoyant, so we have
12050          no idea which functions will be referenced in the future (i.e. later
12051          on within the current translation unit). So here we just ignore all
12052          file-scope function declarations which are not also definitions.  If
12053          and when the debugger needs to know something about these functions,
12054          it will have to hunt around and find the DWARF information associated
12055          with the definition of the function.
12056
12057          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
12058          nodes represent definitions and which ones represent mere
12059          declarations.  We have to check DECL_INITIAL instead. That's because
12060          the C front-end supports some weird semantics for "extern inline"
12061          function definitions.  These can get inlined within the current
12062          translation unit (an thus, we need to generate Dwarf info for their
12063          abstract instances so that the Dwarf info for the concrete inlined
12064          instances can have something to refer to) but the compiler never
12065          generates any out-of-lines instances of such things (despite the fact
12066          that they *are* definitions).
12067
12068          The important point is that the C front-end marks these "extern
12069          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
12070          them anyway. Note that the C++ front-end also plays some similar games
12071          for inline function definitions appearing within include files which
12072          also contain `#pragma interface' pragmas.  */
12073       if (DECL_INITIAL (decl) == NULL_TREE)
12074         return;
12075
12076       /* If we're a nested function, initially use a parent of NULL; if we're
12077          a plain function, this will be fixed up in decls_for_scope.  If
12078          we're a method, it will be ignored, since we already have a DIE.  */
12079       if (decl_function_context (decl))
12080         context_die = NULL;
12081       break;
12082
12083     case VAR_DECL:
12084       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
12085          declaration and if the declaration was never even referenced from
12086          within this entire compilation unit.  We suppress these DIEs in
12087          order to save space in the .debug section (by eliminating entries
12088          which are probably useless).  Note that we must not suppress
12089          block-local extern declarations (whether used or not) because that
12090          would screw-up the debugger's name lookup mechanism and cause it to
12091          miss things which really ought to be in scope at a given point.  */
12092       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
12093         return;
12094
12095       /* If we are in terse mode, don't generate any DIEs to represent any
12096          variable declarations or definitions.  */
12097       if (debug_info_level <= DINFO_LEVEL_TERSE)
12098         return;
12099       break;
12100
12101     case TYPE_DECL:
12102       /* Don't emit stubs for types unless they are needed by other DIEs.  */
12103       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
12104         return;
12105
12106       /* Don't bother trying to generate any DIEs to represent any of the
12107          normal built-in types for the language we are compiling.  */
12108       if (DECL_SOURCE_LINE (decl) == 0)
12109         {
12110           /* OK, we need to generate one for `bool' so GDB knows what type
12111              comparisons have.  */
12112           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
12113                == DW_LANG_C_plus_plus)
12114               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
12115               && ! DECL_IGNORED_P (decl))
12116             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
12117
12118           return;
12119         }
12120
12121       /* If we are in terse mode, don't generate any DIEs for types.  */
12122       if (debug_info_level <= DINFO_LEVEL_TERSE)
12123         return;
12124
12125       /* If we're a function-scope tag, initially use a parent of NULL;
12126          this will be fixed up in decls_for_scope.  */
12127       if (decl_function_context (decl))
12128         context_die = NULL;
12129
12130       break;
12131
12132     default:
12133       return;
12134     }
12135
12136   gen_decl_die (decl, context_die);
12137 }
12138
12139 /* Output a marker (i.e. a label) for the beginning of the generated code for
12140    a lexical block.  */
12141
12142 static void
12143 dwarf2out_begin_block (line, blocknum)
12144      unsigned int line ATTRIBUTE_UNUSED;
12145      unsigned int blocknum;
12146 {
12147   function_section (current_function_decl);
12148   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
12149 }
12150
12151 /* Output a marker (i.e. a label) for the end of the generated code for a
12152    lexical block.  */
12153
12154 static void
12155 dwarf2out_end_block (line, blocknum)
12156      unsigned int line ATTRIBUTE_UNUSED;
12157      unsigned int blocknum;
12158 {
12159   function_section (current_function_decl);
12160   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
12161 }
12162
12163 /* Returns nonzero if it is appropriate not to emit any debugging
12164    information for BLOCK, because it doesn't contain any instructions.
12165
12166    Don't allow this for blocks with nested functions or local classes
12167    as we would end up with orphans, and in the presence of scheduling
12168    we may end up calling them anyway.  */
12169
12170 static bool
12171 dwarf2out_ignore_block (block)
12172      tree block;
12173 {
12174   tree decl;
12175
12176   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
12177     if (TREE_CODE (decl) == FUNCTION_DECL
12178         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
12179       return 0;
12180
12181   return 1;
12182 }
12183
12184 /* Lookup FILE_NAME (in the list of filenames that we know about here in
12185    dwarf2out.c) and return its "index".  The index of each (known) filename is
12186    just a unique number which is associated with only that one filename.  We
12187    need such numbers for the sake of generating labels (in the .debug_sfnames
12188    section) and references to those files numbers (in the .debug_srcinfo
12189    and.debug_macinfo sections).  If the filename given as an argument is not
12190    found in our current list, add it to the list and assign it the next
12191    available unique index number.  In order to speed up searches, we remember
12192    the index of the filename was looked up last.  This handles the majority of
12193    all searches.  */
12194
12195 static unsigned
12196 lookup_filename (file_name)
12197      const char *file_name;
12198 {
12199   unsigned i;
12200
12201   /* ??? Why isn't DECL_SOURCE_FILE left null instead.  */
12202   if (strcmp (file_name, "<internal>") == 0
12203       || strcmp (file_name, "<built-in>") == 0)
12204     return 0;
12205
12206   /* Check to see if the file name that was searched on the previous
12207      call matches this file name.  If so, return the index.  */
12208   if (file_table.last_lookup_index != 0)
12209     if (0 == strcmp (file_name,
12210                      file_table.table[file_table.last_lookup_index]))
12211       return file_table.last_lookup_index;
12212
12213   /* Didn't match the previous lookup, search the table */
12214   for (i = 1; i < file_table.in_use; i++)
12215     if (strcmp (file_name, file_table.table[i]) == 0)
12216       {
12217         file_table.last_lookup_index = i;
12218         return i;
12219       }
12220
12221   /* Prepare to add a new table entry by making sure there is enough space in
12222      the table to do so.  If not, expand the current table.  */
12223   if (i == file_table.allocated)
12224     {
12225       file_table.allocated = i + FILE_TABLE_INCREMENT;
12226       file_table.table = (char **)
12227         xrealloc (file_table.table, file_table.allocated * sizeof (char *));
12228     }
12229
12230   /* Add the new entry to the end of the filename table.  */
12231   file_table.table[i] = xstrdup (file_name);
12232   file_table.in_use = i + 1;
12233   file_table.last_lookup_index = i;
12234
12235   if (DWARF2_ASM_LINE_DEBUG_INFO)
12236     {
12237       fprintf (asm_out_file, "\t.file %u ", i);
12238       output_quoted_string (asm_out_file, file_name);
12239       fputc ('\n', asm_out_file);
12240     }
12241
12242   return i;
12243 }
12244
12245 static void
12246 init_file_table ()
12247 {
12248   /* Allocate the initial hunk of the file_table.  */
12249   file_table.table = (char **) xcalloc (FILE_TABLE_INCREMENT, sizeof (char *));
12250   file_table.allocated = FILE_TABLE_INCREMENT;
12251
12252   /* Skip the first entry - file numbers begin at 1.  */
12253   file_table.in_use = 1;
12254   file_table.last_lookup_index = 0;
12255 }
12256
12257 /* Output a label to mark the beginning of a source code line entry
12258    and record information relating to this source line, in
12259    'line_info_table' for later output of the .debug_line section.  */
12260
12261 static void
12262 dwarf2out_source_line (line, filename)
12263      unsigned int line;
12264      const char *filename;
12265 {
12266   if (debug_info_level >= DINFO_LEVEL_NORMAL)
12267     {
12268       function_section (current_function_decl);
12269
12270       /* If requested, emit something human-readable.  */
12271       if (flag_debug_asm)
12272         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
12273                  filename, line);
12274
12275       if (DWARF2_ASM_LINE_DEBUG_INFO)
12276         {
12277           unsigned file_num = lookup_filename (filename);
12278
12279           /* Emit the .loc directive understood by GNU as.  */
12280           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
12281
12282           /* Indicate that line number info exists.  */
12283           line_info_table_in_use++;
12284
12285           /* Indicate that multiple line number tables exist.  */
12286           if (DECL_SECTION_NAME (current_function_decl))
12287             separate_line_info_table_in_use++;
12288         }
12289       else if (DECL_SECTION_NAME (current_function_decl))
12290         {
12291           dw_separate_line_info_ref line_info;
12292           (*targetm.asm_out.internal_label) (asm_out_file, SEPARATE_LINE_CODE_LABEL,
12293                                      separate_line_info_table_in_use);
12294
12295           /* expand the line info table if necessary */
12296           if (separate_line_info_table_in_use
12297               == separate_line_info_table_allocated)
12298             {
12299               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
12300               separate_line_info_table
12301                 = (dw_separate_line_info_ref)
12302                   xrealloc (separate_line_info_table,
12303                             separate_line_info_table_allocated
12304                             * sizeof (dw_separate_line_info_entry));
12305             }
12306
12307           /* Add the new entry at the end of the line_info_table.  */
12308           line_info
12309             = &separate_line_info_table[separate_line_info_table_in_use++];
12310           line_info->dw_file_num = lookup_filename (filename);
12311           line_info->dw_line_num = line;
12312           line_info->function = current_function_funcdef_no;
12313         }
12314       else
12315         {
12316           dw_line_info_ref line_info;
12317
12318           (*targetm.asm_out.internal_label) (asm_out_file, LINE_CODE_LABEL,
12319                                      line_info_table_in_use);
12320
12321           /* Expand the line info table if necessary.  */
12322           if (line_info_table_in_use == line_info_table_allocated)
12323             {
12324               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
12325               line_info_table
12326                 = (dw_line_info_ref)
12327                   xrealloc (line_info_table,
12328                             (line_info_table_allocated
12329                              * sizeof (dw_line_info_entry)));
12330             }
12331
12332           /* Add the new entry at the end of the line_info_table.  */
12333           line_info = &line_info_table[line_info_table_in_use++];
12334           line_info->dw_file_num = lookup_filename (filename);
12335           line_info->dw_line_num = line;
12336         }
12337     }
12338 }
12339
12340 /* Record the beginning of a new source file.  */
12341
12342 static void
12343 dwarf2out_start_source_file (lineno, filename)
12344      unsigned int lineno;
12345      const char *filename;
12346 {
12347   if (flag_eliminate_dwarf2_dups && !is_main_source)
12348     {
12349       /* Record the beginning of the file for break_out_includes.  */
12350       dw_die_ref bincl_die;
12351
12352       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
12353       add_AT_string (bincl_die, DW_AT_name, filename);
12354     }
12355
12356   is_main_source = 0;
12357
12358   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12359     {
12360       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12361       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
12362       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
12363                                    lineno);
12364       dw2_asm_output_data_uleb128 (lookup_filename (filename),
12365                                    "Filename we just started");
12366     }
12367 }
12368
12369 /* Record the end of a source file.  */
12370
12371 static void
12372 dwarf2out_end_source_file (lineno)
12373      unsigned int lineno ATTRIBUTE_UNUSED;
12374 {
12375   if (flag_eliminate_dwarf2_dups)
12376     /* Record the end of the file for break_out_includes.  */
12377     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
12378
12379   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12380     {
12381       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12382       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
12383     }
12384 }
12385
12386 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
12387    the tail part of the directive line, i.e. the part which is past the
12388    initial whitespace, #, whitespace, directive-name, whitespace part.  */
12389
12390 static void
12391 dwarf2out_define (lineno, buffer)
12392      unsigned lineno ATTRIBUTE_UNUSED;
12393      const char *buffer ATTRIBUTE_UNUSED;
12394 {
12395   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12396     {
12397       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12398       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
12399       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
12400       dw2_asm_output_nstring (buffer, -1, "The macro");
12401     }
12402 }
12403
12404 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
12405    the tail part of the directive line, i.e. the part which is past the
12406    initial whitespace, #, whitespace, directive-name, whitespace part.  */
12407
12408 static void
12409 dwarf2out_undef (lineno, buffer)
12410      unsigned lineno ATTRIBUTE_UNUSED;
12411      const char *buffer ATTRIBUTE_UNUSED;
12412 {
12413   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12414     {
12415       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12416       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
12417       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
12418       dw2_asm_output_nstring (buffer, -1, "The macro");
12419     }
12420 }
12421
12422 /* Set up for Dwarf output at the start of compilation.  */
12423
12424 static void
12425 dwarf2out_init (main_input_filename)
12426      const char *main_input_filename;
12427 {
12428   init_file_table ();
12429
12430   /* Add the name of the primary input file to the file table first,
12431      under the assumption that we'll be emitting line number data for
12432      it first, which avoids having to add an initial DW_LNS_set_file.  */
12433   lookup_filename (main_input_filename);
12434
12435   /* Allocate the initial hunk of the decl_die_table.  */
12436   decl_die_table
12437     = (dw_die_ref *) xcalloc (DECL_DIE_TABLE_INCREMENT, sizeof (dw_die_ref));
12438   decl_die_table_allocated = DECL_DIE_TABLE_INCREMENT;
12439   decl_die_table_in_use = 0;
12440
12441   /* Allocate the initial hunk of the decl_scope_table.  */
12442   VARRAY_TREE_INIT (decl_scope_table, 256, "decl_scope_table");
12443
12444   /* Allocate the initial hunk of the abbrev_die_table.  */
12445   abbrev_die_table
12446     = (dw_die_ref *) xcalloc (ABBREV_DIE_TABLE_INCREMENT,
12447                               sizeof (dw_die_ref));
12448   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
12449   /* Zero-th entry is allocated, but unused */
12450   abbrev_die_table_in_use = 1;
12451
12452   /* Allocate the initial hunk of the line_info_table.  */
12453   line_info_table
12454     = (dw_line_info_ref) xcalloc (LINE_INFO_TABLE_INCREMENT,
12455                                   sizeof (dw_line_info_entry));
12456   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
12457
12458   /* Zero-th entry is allocated, but unused */
12459   line_info_table_in_use = 1;
12460
12461   /* Generate the initial DIE for the .debug section.  Note that the (string)
12462      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
12463      will (typically) be a relative pathname and that this pathname should be
12464      taken as being relative to the directory from which the compiler was
12465      invoked when the given (base) source file was compiled.  */
12466   comp_unit_die = gen_compile_unit_die (main_input_filename);
12467   is_main_source = 1;
12468
12469   VARRAY_TREE_INIT (incomplete_types, 64, "incomplete_types");
12470
12471   VARRAY_RTX_INIT (used_rtx_varray, 32, "used_rtx_varray");
12472
12473   ggc_add_root (&limbo_die_list, 1, 1, mark_limbo_die_list);
12474
12475   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
12476   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
12477                                DEBUG_ABBREV_SECTION_LABEL, 0);
12478   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
12479     ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
12480   else
12481     strcpy (text_section_label, stripattributes (TEXT_SECTION_NAME));
12482
12483   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
12484                                DEBUG_INFO_SECTION_LABEL, 0);
12485   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
12486                                DEBUG_LINE_SECTION_LABEL, 0);
12487   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
12488                                DEBUG_RANGES_SECTION_LABEL, 0);
12489   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
12490   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
12491   named_section_flags (DEBUG_INFO_SECTION, SECTION_DEBUG);
12492   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
12493   named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
12494   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
12495
12496   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12497     {
12498       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12499       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
12500                                    DEBUG_MACINFO_SECTION_LABEL, 0);
12501       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
12502     }
12503
12504   if (DWARF2_GENERATE_TEXT_SECTION_LABEL)
12505     {
12506       text_section ();
12507       ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
12508     }
12509 }
12510
12511 /* Allocate a string in .debug_str hash table.  */
12512
12513 static hashnode
12514 indirect_string_alloc (tab)
12515      hash_table *tab ATTRIBUTE_UNUSED;
12516 {
12517   struct indirect_string_node *node;
12518
12519   node = xmalloc (sizeof (struct indirect_string_node));
12520   node->refcount = 0;
12521   node->form = 0;
12522   node->label = NULL;
12523
12524   return (hashnode) node;
12525 }
12526
12527 /* A helper function for dwarf2out_finish called through
12528    ht_forall.  Emit one queued .debug_str string.  */
12529
12530 static int
12531 output_indirect_string (pfile, h, v)
12532      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
12533      hashnode h;
12534      const PTR v ATTRIBUTE_UNUSED;
12535 {
12536   struct indirect_string_node *node = (struct indirect_string_node *) h;
12537
12538   if (node->form == DW_FORM_strp)
12539     {
12540       named_section_flags (DEBUG_STR_SECTION, DEBUG_STR_SECTION_FLAGS);
12541       ASM_OUTPUT_LABEL (asm_out_file, node->label);
12542       assemble_string ((const char *) HT_STR (&node->id),
12543                        HT_LEN (&node->id) + 1);
12544     }
12545
12546   return 1;
12547 }
12548
12549 /* Output stuff that dwarf requires at the end of every file,
12550    and generate the DWARF-2 debugging info.  */
12551
12552 static void
12553 dwarf2out_finish (input_filename)
12554      const char *input_filename ATTRIBUTE_UNUSED;
12555 {
12556   limbo_die_node *node, *next_node;
12557   dw_die_ref die = 0;
12558
12559   /* Traverse the limbo die list, and add parent/child links.  The only
12560      dies without parents that should be here are concrete instances of
12561      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
12562      For concrete instances, we can get the parent die from the abstract
12563      instance.  */
12564   for (node = limbo_die_list; node; node = next_node)
12565     {
12566       next_node = node->next;
12567       die = node->die;
12568
12569       if (die->die_parent == NULL)
12570         {
12571           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
12572           tree context;
12573
12574           if (origin)
12575             add_child_die (origin->die_parent, die);
12576           else if (die == comp_unit_die)
12577             ;
12578           /* If this was an expression for a bound involved in a function
12579              return type, it may be a SAVE_EXPR for which we weren't able
12580              to find a DIE previously.  So try now.  */
12581           else if (node->created_for
12582                    && TREE_CODE (node->created_for) == SAVE_EXPR
12583                    && 0 != (origin = (lookup_decl_die
12584                                       (SAVE_EXPR_CONTEXT
12585                                        (node->created_for)))))
12586             add_child_die (origin, die);
12587           else if (errorcount > 0 || sorrycount > 0)
12588             /* It's OK to be confused by errors in the input.  */
12589             add_child_die (comp_unit_die, die);
12590           else if (node->created_for
12591                    && ((DECL_P (node->created_for)
12592                         && (context = DECL_CONTEXT (node->created_for)))
12593                        || (TYPE_P (node->created_for)
12594                            && (context = TYPE_CONTEXT (node->created_for))))
12595                    && TREE_CODE (context) == FUNCTION_DECL)
12596             {
12597               /* In certain situations, the lexical block containing a
12598                  nested function can be optimized away, which results
12599                  in the nested function die being orphaned.  Likewise
12600                  with the return type of that nested function.  Force
12601                  this to be a child of the containing function.  */
12602               origin = lookup_decl_die (context);
12603               if (! origin)
12604                 abort ();
12605               add_child_die (origin, die);
12606             }
12607           else
12608             abort ();
12609         }
12610
12611       free (node);
12612     }
12613
12614   limbo_die_list = NULL;
12615
12616   /* Walk through the list of incomplete types again, trying once more to
12617      emit full debugging info for them.  */
12618   retry_incomplete_types ();
12619
12620   /* We need to reverse all the dies before break_out_includes, or
12621      we'll see the end of an include file before the beginning.  */
12622   reverse_all_dies (comp_unit_die);
12623
12624   /* Generate separate CUs for each of the include files we've seen.
12625      They will go into limbo_die_list.  */
12626   if (flag_eliminate_dwarf2_dups)
12627     break_out_includes (comp_unit_die);
12628
12629   /* Traverse the DIE's and add add sibling attributes to those DIE's
12630      that have children.  */
12631   add_sibling_attributes (comp_unit_die);
12632   for (node = limbo_die_list; node; node = node->next)
12633     add_sibling_attributes (node->die);
12634
12635   /* Output a terminator label for the .text section.  */
12636   text_section ();
12637   (*targetm.asm_out.internal_label) (asm_out_file, TEXT_END_LABEL, 0);
12638
12639   /* Output the source line correspondence table.  We must do this
12640      even if there is no line information.  Otherwise, on an empty
12641      translation unit, we will generate a present, but empty,
12642      .debug_info section.  IRIX 6.5 `nm' will then complain when
12643      examining the file.  */
12644   if (! DWARF2_ASM_LINE_DEBUG_INFO)
12645     {
12646       named_section_flags (DEBUG_LINE_SECTION, SECTION_DEBUG);
12647       output_line_info ();
12648     }
12649
12650   /* Output location list section if necessary.  */
12651   if (have_location_lists)
12652     {
12653       /* Output the location lists info.  */
12654       named_section_flags (DEBUG_LOC_SECTION, SECTION_DEBUG);
12655       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
12656                                    DEBUG_LOC_SECTION_LABEL, 0);
12657       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
12658       output_location_lists (die);
12659       have_location_lists = 0;
12660     }
12661
12662   /* We can only use the low/high_pc attributes if all of the code was
12663      in .text.  */
12664   if (separate_line_info_table_in_use == 0)
12665     {
12666       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
12667       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
12668     }
12669
12670   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
12671      "base address".  Use zero so that these addresses become absolute.  */
12672   else if (have_location_lists || ranges_table_in_use)
12673     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
12674
12675   if (debug_info_level >= DINFO_LEVEL_NORMAL)
12676     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
12677                        debug_line_section_label);
12678
12679   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12680     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
12681
12682   /* Output all of the compilation units.  We put the main one last so that
12683      the offsets are available to output_pubnames.  */
12684   for (node = limbo_die_list; node; node = node->next)
12685     output_comp_unit (node->die, 0);
12686
12687   output_comp_unit (comp_unit_die, 0);
12688
12689   /* Output the abbreviation table.  */
12690   named_section_flags (DEBUG_ABBREV_SECTION, SECTION_DEBUG);
12691   output_abbrev_section ();
12692
12693   /* Output public names table if necessary.  */
12694   if (pubname_table_in_use)
12695     {
12696       named_section_flags (DEBUG_PUBNAMES_SECTION, SECTION_DEBUG);
12697       output_pubnames ();
12698     }
12699
12700   /* Output the address range information.  We only put functions in the arange
12701      table, so don't write it out if we don't have any.  */
12702   if (fde_table_in_use)
12703     {
12704       named_section_flags (DEBUG_ARANGES_SECTION, SECTION_DEBUG);
12705       output_aranges ();
12706     }
12707
12708   /* Output ranges section if necessary.  */
12709   if (ranges_table_in_use)
12710     {
12711       named_section_flags (DEBUG_RANGES_SECTION, SECTION_DEBUG);
12712       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12713       output_ranges ();
12714     }
12715
12716   /* Have to end the primary source file.  */
12717   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
12718     {
12719       named_section_flags (DEBUG_MACINFO_SECTION, SECTION_DEBUG);
12720       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
12721       dw2_asm_output_data (1, 0, "End compilation unit");
12722     }
12723
12724   /* If we emitted any DW_FORM_strp form attribute, output the string
12725      table too.  */
12726   if (debug_str_hash)
12727     ht_forall (debug_str_hash, output_indirect_string, NULL);
12728 }
12729 #else
12730
12731 /* This should never be used, but its address is needed for comparisons.  */
12732 const struct gcc_debug_hooks dwarf2_debug_hooks;
12733
12734 #endif /* DWARF2_DEBUGGING_INFO */
12735
12736 #include "gt-dwarf2out.h"