OSDN Git Service

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