OSDN Git Service

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