Import gcc-4.4.2
[dragonfly.git] / contrib / gcc-4.4 / 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, 2007, 2008, 2009 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 3, 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 COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Save the result of dwarf2out_do_frame across PCH.  */
114 static GTY(()) bool saved_do_cfi_asm = 0;
115
116 /* Decide whether we want to emit frame unwind information for the current
117    translation unit.  */
118
119 int
120 dwarf2out_do_frame (void)
121 {
122   /* We want to emit correct CFA location expressions or lists, so we
123      have to return true if we're going to output debug info, even if
124      we're not going to output frame or unwind info.  */
125   return (write_symbols == DWARF2_DEBUG
126           || write_symbols == VMS_AND_DWARF2_DEBUG
127           || DWARF2_FRAME_INFO || saved_do_cfi_asm
128 #ifdef DWARF2_UNWIND_INFO
129           || (DWARF2_UNWIND_INFO
130               && (flag_unwind_tables
131                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
132 #endif
133           );
134 }
135
136 /* Decide whether to emit frame unwind via assembler directives.  */
137
138 int
139 dwarf2out_do_cfi_asm (void)
140 {
141   int enc;
142
143 #ifdef MIPS_DEBUGGING_INFO
144   return false;
145 #endif
146   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147     return false;
148   if (saved_do_cfi_asm || !eh_personality_libfunc)
149     return true;
150   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151     return false;
152
153   /* Make sure the personality encoding is one the assembler can support.
154      In particular, aligned addresses can't be handled.  */
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160     return false;
161
162   saved_do_cfi_asm = true;
163   return true;
164 }
165
166 /* The size of the target's pointer type.  */
167 #ifndef PTR_SIZE
168 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169 #endif
170
171 /* Array of RTXes referenced by the debugging information, which therefore
172    must be kept around forever.  */
173 static GTY(()) VEC(rtx,gc) *used_rtx_array;
174
175 /* A pointer to the base of a list of incomplete types which might be
176    completed at some later time.  incomplete_types_list needs to be a
177    VEC(tree,gc) because we want to tell the garbage collector about
178    it.  */
179 static GTY(()) VEC(tree,gc) *incomplete_types;
180
181 /* A pointer to the base of a table of references to declaration
182    scopes.  This table is a display which tracks the nesting
183    of declaration scopes at the current scope and containing
184    scopes.  This table is used to find the proper place to
185    define type declaration DIE's.  */
186 static GTY(()) VEC(tree,gc) *decl_scope_table;
187
188 /* Pointers to various DWARF2 sections.  */
189 static GTY(()) section *debug_info_section;
190 static GTY(()) section *debug_abbrev_section;
191 static GTY(()) section *debug_aranges_section;
192 static GTY(()) section *debug_macinfo_section;
193 static GTY(()) section *debug_line_section;
194 static GTY(()) section *debug_loc_section;
195 static GTY(()) section *debug_pubnames_section;
196 static GTY(()) section *debug_pubtypes_section;
197 static GTY(()) section *debug_str_section;
198 static GTY(()) section *debug_ranges_section;
199 static GTY(()) section *debug_frame_section;
200
201 /* How to start an assembler comment.  */
202 #ifndef ASM_COMMENT_START
203 #define ASM_COMMENT_START ";#"
204 #endif
205
206 typedef struct dw_cfi_struct *dw_cfi_ref;
207 typedef struct dw_fde_struct *dw_fde_ref;
208 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
209
210 /* Call frames are described using a sequence of Call Frame
211    Information instructions.  The register number, offset
212    and address fields are provided as possible operands;
213    their use is selected by the opcode field.  */
214
215 enum dw_cfi_oprnd_type {
216   dw_cfi_oprnd_unused,
217   dw_cfi_oprnd_reg_num,
218   dw_cfi_oprnd_offset,
219   dw_cfi_oprnd_addr,
220   dw_cfi_oprnd_loc
221 };
222
223 typedef union dw_cfi_oprnd_struct GTY(())
224 {
225   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
226   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
227   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
228   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
229 }
230 dw_cfi_oprnd;
231
232 typedef struct dw_cfi_struct GTY(())
233 {
234   dw_cfi_ref dw_cfi_next;
235   enum dwarf_call_frame_info dw_cfi_opc;
236   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
237     dw_cfi_oprnd1;
238   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
239     dw_cfi_oprnd2;
240 }
241 dw_cfi_node;
242
243 /* This is how we define the location of the CFA. We use to handle it
244    as REG + OFFSET all the time,  but now it can be more complex.
245    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
246    Instead of passing around REG and OFFSET, we pass a copy
247    of this structure.  */
248 typedef struct cfa_loc GTY(())
249 {
250   HOST_WIDE_INT offset;
251   HOST_WIDE_INT base_offset;
252   unsigned int reg;
253   int indirect;            /* 1 if CFA is accessed via a dereference.  */
254 } dw_cfa_location;
255
256 /* All call frame descriptions (FDE's) in the GCC generated DWARF
257    refer to a single Common Information Entry (CIE), defined at
258    the beginning of the .debug_frame section.  This use of a single
259    CIE obviates the need to keep track of multiple CIE's
260    in the DWARF generation routines below.  */
261
262 typedef struct dw_fde_struct GTY(())
263 {
264   tree decl;
265   const char *dw_fde_begin;
266   const char *dw_fde_current_label;
267   const char *dw_fde_end;
268   const char *dw_fde_hot_section_label;
269   const char *dw_fde_hot_section_end_label;
270   const char *dw_fde_unlikely_section_label;
271   const char *dw_fde_unlikely_section_end_label;
272   bool dw_fde_switched_sections;
273   dw_cfi_ref dw_fde_cfi;
274   unsigned funcdef_number;
275   HOST_WIDE_INT stack_realignment;
276   /* Dynamic realign argument pointer register.  */
277   unsigned int drap_reg;
278   /* Virtual dynamic realign argument pointer register.  */
279   unsigned int vdrap_reg;
280   unsigned all_throwers_are_sibcalls : 1;
281   unsigned nothrow : 1;
282   unsigned uses_eh_lsda : 1;
283   /* Whether we did stack realign in this call frame.  */
284   unsigned stack_realign : 1;
285   /* Whether dynamic realign argument pointer register has been saved.  */
286   unsigned drap_reg_saved: 1;
287 }
288 dw_fde_node;
289
290 /* Maximum size (in bytes) of an artificially generated label.  */
291 #define MAX_ARTIFICIAL_LABEL_BYTES      30
292
293 /* The size of addresses as they appear in the Dwarf 2 data.
294    Some architectures use word addresses to refer to code locations,
295    but Dwarf 2 info always uses byte addresses.  On such machines,
296    Dwarf 2 addresses need to be larger than the architecture's
297    pointers.  */
298 #ifndef DWARF2_ADDR_SIZE
299 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
300 #endif
301
302 /* The size in bytes of a DWARF field indicating an offset or length
303    relative to a debug info section, specified to be 4 bytes in the
304    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
305    as PTR_SIZE.  */
306
307 #ifndef DWARF_OFFSET_SIZE
308 #define DWARF_OFFSET_SIZE 4
309 #endif
310
311 /* According to the (draft) DWARF 3 specification, the initial length
312    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
313    bytes are 0xffffffff, followed by the length stored in the next 8
314    bytes.
315
316    However, the SGI/MIPS ABI uses an initial length which is equal to
317    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
318
319 #ifndef DWARF_INITIAL_LENGTH_SIZE
320 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
321 #endif
322
323 #define DWARF_VERSION 2
324
325 /* Round SIZE up to the nearest BOUNDARY.  */
326 #define DWARF_ROUND(SIZE,BOUNDARY) \
327   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
328
329 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
330 #ifndef DWARF_CIE_DATA_ALIGNMENT
331 #ifdef STACK_GROWS_DOWNWARD
332 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
333 #else
334 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
335 #endif
336 #endif
337
338 /* CIE identifier.  */
339 #if HOST_BITS_PER_WIDE_INT >= 64
340 #define DWARF_CIE_ID \
341   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
342 #else
343 #define DWARF_CIE_ID DW_CIE_ID
344 #endif
345
346 /* A pointer to the base of a table that contains frame description
347    information for each routine.  */
348 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
349
350 /* Number of elements currently allocated for fde_table.  */
351 static GTY(()) unsigned fde_table_allocated;
352
353 /* Number of elements in fde_table currently in use.  */
354 static GTY(()) unsigned fde_table_in_use;
355
356 /* Size (in elements) of increments by which we may expand the
357    fde_table.  */
358 #define FDE_TABLE_INCREMENT 256
359
360 /* Get the current fde_table entry we should use.  */
361
362 static inline dw_fde_ref
363 current_fde (void)
364 {
365   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
366 }
367
368 /* A list of call frame insns for the CIE.  */
369 static GTY(()) dw_cfi_ref cie_cfi_head;
370
371 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
372 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
373    attribute that accelerates the lookup of the FDE associated
374    with the subprogram.  This variable holds the table index of the FDE
375    associated with the current function (body) definition.  */
376 static unsigned current_funcdef_fde;
377 #endif
378
379 struct indirect_string_node GTY(())
380 {
381   const char *str;
382   unsigned int refcount;
383   unsigned int form;
384   char *label;
385 };
386
387 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
388
389 static GTY(()) int dw2_string_counter;
390 static GTY(()) unsigned long dwarf2out_cfi_label_num;
391
392 /* True if the compilation unit places functions in more than one section.  */
393 static GTY(()) bool have_multiple_function_sections = false;
394
395 /* Whether the default text and cold text sections have been used at all.  */
396
397 static GTY(()) bool text_section_used = false;
398 static GTY(()) bool cold_text_section_used = false;
399
400 /* The default cold text section.  */
401 static GTY(()) section *cold_text_section;
402
403 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
404
405 /* Forward declarations for functions defined in this file.  */
406
407 static char *stripattributes (const char *);
408 static const char *dwarf_cfi_name (unsigned);
409 static dw_cfi_ref new_cfi (void);
410 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
411 static void add_fde_cfi (const char *, dw_cfi_ref);
412 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
413 static void lookup_cfa (dw_cfa_location *);
414 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
415 #ifdef DWARF2_UNWIND_INFO
416 static void initial_return_save (rtx);
417 #endif
418 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
419                                           HOST_WIDE_INT);
420 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
421 static void output_cfi_directive (dw_cfi_ref);
422 static void output_call_frame_info (int);
423 static void dwarf2out_note_section_used (void);
424 static void dwarf2out_stack_adjust (rtx, bool);
425 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
426 static void flush_queued_reg_saves (void);
427 static bool clobbers_queued_reg_save (const_rtx);
428 static void dwarf2out_frame_debug_expr (rtx, const char *);
429
430 /* Support for complex CFA locations.  */
431 static void output_cfa_loc (dw_cfi_ref);
432 static void output_cfa_loc_raw (dw_cfi_ref);
433 static void get_cfa_from_loc_descr (dw_cfa_location *,
434                                     struct dw_loc_descr_struct *);
435 static struct dw_loc_descr_struct *build_cfa_loc
436   (dw_cfa_location *, HOST_WIDE_INT);
437 static struct dw_loc_descr_struct *build_cfa_aligned_loc
438   (HOST_WIDE_INT, HOST_WIDE_INT);
439 static void def_cfa_1 (const char *, dw_cfa_location *);
440
441 /* How to start an assembler comment.  */
442 #ifndef ASM_COMMENT_START
443 #define ASM_COMMENT_START ";#"
444 #endif
445
446 /* Data and reference forms for relocatable data.  */
447 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
448 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
449
450 #ifndef DEBUG_FRAME_SECTION
451 #define DEBUG_FRAME_SECTION     ".debug_frame"
452 #endif
453
454 #ifndef FUNC_BEGIN_LABEL
455 #define FUNC_BEGIN_LABEL        "LFB"
456 #endif
457
458 #ifndef FUNC_END_LABEL
459 #define FUNC_END_LABEL          "LFE"
460 #endif
461
462 #ifndef FRAME_BEGIN_LABEL
463 #define FRAME_BEGIN_LABEL       "Lframe"
464 #endif
465 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
466 #define CIE_END_LABEL           "LECIE"
467 #define FDE_LABEL               "LSFDE"
468 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
469 #define FDE_END_LABEL           "LEFDE"
470 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
471 #define LINE_NUMBER_END_LABEL   "LELT"
472 #define LN_PROLOG_AS_LABEL      "LASLTP"
473 #define LN_PROLOG_END_LABEL     "LELTP"
474 #define DIE_LABEL_PREFIX        "DW"
475
476 /* The DWARF 2 CFA column which tracks the return address.  Normally this
477    is the column for PC, or the first column after all of the hard
478    registers.  */
479 #ifndef DWARF_FRAME_RETURN_COLUMN
480 #ifdef PC_REGNUM
481 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
482 #else
483 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
484 #endif
485 #endif
486
487 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
488    default, we just provide columns for all registers.  */
489 #ifndef DWARF_FRAME_REGNUM
490 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
491 #endif
492 \f
493 /* Hook used by __throw.  */
494
495 rtx
496 expand_builtin_dwarf_sp_column (void)
497 {
498   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
499   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
500 }
501
502 /* Return a pointer to a copy of the section string name S with all
503    attributes stripped off, and an asterisk prepended (for assemble_name).  */
504
505 static inline char *
506 stripattributes (const char *s)
507 {
508   char *stripped = XNEWVEC (char, strlen (s) + 2);
509   char *p = stripped;
510
511   *p++ = '*';
512
513   while (*s && *s != ',')
514     *p++ = *s++;
515
516   *p = '\0';
517   return stripped;
518 }
519
520 /* MEM is a memory reference for the register size table, each element of
521    which has mode MODE.  Initialize column C as a return address column.  */
522
523 static void
524 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
525 {
526   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
527   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
528   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
529 }
530
531 /* Generate code to initialize the register size table.  */
532
533 void
534 expand_builtin_init_dwarf_reg_sizes (tree address)
535 {
536   unsigned int i;
537   enum machine_mode mode = TYPE_MODE (char_type_node);
538   rtx addr = expand_normal (address);
539   rtx mem = gen_rtx_MEM (BLKmode, addr);
540   bool wrote_return_column = false;
541
542   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
543     {
544       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
545
546       if (rnum < DWARF_FRAME_REGISTERS)
547         {
548           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
549           enum machine_mode save_mode = reg_raw_mode[i];
550           HOST_WIDE_INT size;
551
552           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
553             save_mode = choose_hard_reg_mode (i, 1, true);
554           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
555             {
556               if (save_mode == VOIDmode)
557                 continue;
558               wrote_return_column = true;
559             }
560           size = GET_MODE_SIZE (save_mode);
561           if (offset < 0)
562             continue;
563
564           emit_move_insn (adjust_address (mem, mode, offset),
565                           gen_int_mode (size, mode));
566         }
567     }
568
569   if (!wrote_return_column)
570     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
571
572 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
573   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
574 #endif
575
576   targetm.init_dwarf_reg_sizes_extra (address);
577 }
578
579 /* Convert a DWARF call frame info. operation to its string name */
580
581 static const char *
582 dwarf_cfi_name (unsigned int cfi_opc)
583 {
584   switch (cfi_opc)
585     {
586     case DW_CFA_advance_loc:
587       return "DW_CFA_advance_loc";
588     case DW_CFA_offset:
589       return "DW_CFA_offset";
590     case DW_CFA_restore:
591       return "DW_CFA_restore";
592     case DW_CFA_nop:
593       return "DW_CFA_nop";
594     case DW_CFA_set_loc:
595       return "DW_CFA_set_loc";
596     case DW_CFA_advance_loc1:
597       return "DW_CFA_advance_loc1";
598     case DW_CFA_advance_loc2:
599       return "DW_CFA_advance_loc2";
600     case DW_CFA_advance_loc4:
601       return "DW_CFA_advance_loc4";
602     case DW_CFA_offset_extended:
603       return "DW_CFA_offset_extended";
604     case DW_CFA_restore_extended:
605       return "DW_CFA_restore_extended";
606     case DW_CFA_undefined:
607       return "DW_CFA_undefined";
608     case DW_CFA_same_value:
609       return "DW_CFA_same_value";
610     case DW_CFA_register:
611       return "DW_CFA_register";
612     case DW_CFA_remember_state:
613       return "DW_CFA_remember_state";
614     case DW_CFA_restore_state:
615       return "DW_CFA_restore_state";
616     case DW_CFA_def_cfa:
617       return "DW_CFA_def_cfa";
618     case DW_CFA_def_cfa_register:
619       return "DW_CFA_def_cfa_register";
620     case DW_CFA_def_cfa_offset:
621       return "DW_CFA_def_cfa_offset";
622
623     /* DWARF 3 */
624     case DW_CFA_def_cfa_expression:
625       return "DW_CFA_def_cfa_expression";
626     case DW_CFA_expression:
627       return "DW_CFA_expression";
628     case DW_CFA_offset_extended_sf:
629       return "DW_CFA_offset_extended_sf";
630     case DW_CFA_def_cfa_sf:
631       return "DW_CFA_def_cfa_sf";
632     case DW_CFA_def_cfa_offset_sf:
633       return "DW_CFA_def_cfa_offset_sf";
634
635     /* SGI/MIPS specific */
636     case DW_CFA_MIPS_advance_loc8:
637       return "DW_CFA_MIPS_advance_loc8";
638
639     /* GNU extensions */
640     case DW_CFA_GNU_window_save:
641       return "DW_CFA_GNU_window_save";
642     case DW_CFA_GNU_args_size:
643       return "DW_CFA_GNU_args_size";
644     case DW_CFA_GNU_negative_offset_extended:
645       return "DW_CFA_GNU_negative_offset_extended";
646
647     default:
648       return "DW_CFA_<unknown>";
649     }
650 }
651
652 /* Return a pointer to a newly allocated Call Frame Instruction.  */
653
654 static inline dw_cfi_ref
655 new_cfi (void)
656 {
657   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
658
659   cfi->dw_cfi_next = NULL;
660   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
661   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
662
663   return cfi;
664 }
665
666 /* Add a Call Frame Instruction to list of instructions.  */
667
668 static inline void
669 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
670 {
671   dw_cfi_ref *p;
672   dw_fde_ref fde = current_fde ();
673
674   /* When DRAP is used, CFA is defined with an expression.  Redefine
675      CFA may lead to a different CFA value.   */
676   if (fde && fde->drap_reg != INVALID_REGNUM)
677     switch (cfi->dw_cfi_opc)
678       {
679         case DW_CFA_def_cfa_register:
680         case DW_CFA_def_cfa_offset:
681         case DW_CFA_def_cfa_offset_sf:
682         case DW_CFA_def_cfa:
683         case DW_CFA_def_cfa_sf:
684           gcc_unreachable ();
685
686         default:
687           break;
688       }
689
690   /* Find the end of the chain.  */
691   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
692     ;
693
694   *p = cfi;
695 }
696
697 /* Generate a new label for the CFI info to refer to.  FORCE is true
698    if a label needs to be output even when using .cfi_* directives.  */
699
700 char *
701 dwarf2out_cfi_label (bool force)
702 {
703   static char label[20];
704
705   if (!force && dwarf2out_do_cfi_asm ())
706     {
707       /* In this case, we will be emitting the asm directive instead of
708          the label, so just return a placeholder to keep the rest of the
709          interfaces happy.  */
710       strcpy (label, "<do not output>");
711     }
712   else
713     {
714       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
715       ASM_OUTPUT_LABEL (asm_out_file, label);
716     }
717
718   return label;
719 }
720
721 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
722    or to the CIE if LABEL is NULL.  */
723
724 static void
725 add_fde_cfi (const char *label, dw_cfi_ref cfi)
726 {
727   dw_cfi_ref *list_head = &cie_cfi_head;
728
729   if (dwarf2out_do_cfi_asm ())
730     {
731       if (label)
732         {
733           dw_fde_ref fde = current_fde ();
734
735           gcc_assert (fde != NULL);
736
737           /* We still have to add the cfi to the list so that
738              lookup_cfa works later on.  When -g2 and above we
739              even need to force emitting of CFI labels and
740              add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
741              purposes.  */
742           switch (cfi->dw_cfi_opc)
743             {
744             case DW_CFA_def_cfa_offset:
745             case DW_CFA_def_cfa_offset_sf:
746             case DW_CFA_def_cfa_register:
747             case DW_CFA_def_cfa:
748             case DW_CFA_def_cfa_sf:
749             case DW_CFA_def_cfa_expression:
750             case DW_CFA_restore_state:
751               if (write_symbols != DWARF2_DEBUG
752                   && write_symbols != VMS_AND_DWARF2_DEBUG)
753                 break;
754               if (debug_info_level <= DINFO_LEVEL_TERSE)
755                 break;
756
757               if (*label == 0 || strcmp (label, "<do not output>") == 0)
758                 label = dwarf2out_cfi_label (true);
759
760               if (fde->dw_fde_current_label == NULL
761                   || strcmp (label, fde->dw_fde_current_label) != 0)
762                 {
763                   dw_cfi_ref xcfi;
764
765                   label = xstrdup (label);
766
767                   /* Set the location counter to the new label.  */
768                   xcfi = new_cfi ();
769                   /* It doesn't metter whether DW_CFA_set_loc
770                      or DW_CFA_advance_loc4 is added here, those aren't
771                      emitted into assembly, only looked up by
772                      convert_cfa_to_fb_loc_list.  */
773                   xcfi->dw_cfi_opc = DW_CFA_set_loc;
774                   xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
775                   add_cfi (&fde->dw_fde_cfi, xcfi);
776                   fde->dw_fde_current_label = label;
777                 }
778               break;
779             default:
780               break;
781             }
782
783           output_cfi_directive (cfi);
784
785           list_head = &fde->dw_fde_cfi;
786         }
787       /* ??? If this is a CFI for the CIE, we don't emit.  This
788          assumes that the standard CIE contents that the assembler
789          uses matches the standard CIE contents that the compiler
790          uses.  This is probably a bad assumption.  I'm not quite
791          sure how to address this for now.  */
792     }
793   else if (label)
794     {
795       dw_fde_ref fde = current_fde ();
796
797       gcc_assert (fde != NULL);
798
799       if (*label == 0)
800         label = dwarf2out_cfi_label (false);
801
802       if (fde->dw_fde_current_label == NULL
803           || strcmp (label, fde->dw_fde_current_label) != 0)
804         {
805           dw_cfi_ref xcfi;
806
807           label = xstrdup (label);
808
809           /* Set the location counter to the new label.  */
810           xcfi = new_cfi ();
811           /* If we have a current label, advance from there, otherwise
812              set the location directly using set_loc.  */
813           xcfi->dw_cfi_opc = fde->dw_fde_current_label
814                              ? DW_CFA_advance_loc4
815                              : DW_CFA_set_loc;
816           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
817           add_cfi (&fde->dw_fde_cfi, xcfi);
818
819           fde->dw_fde_current_label = label;
820         }
821
822       list_head = &fde->dw_fde_cfi;
823     }
824
825   add_cfi (list_head, cfi);
826 }
827
828 /* Subroutine of lookup_cfa.  */
829
830 static void
831 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
832 {
833   switch (cfi->dw_cfi_opc)
834     {
835     case DW_CFA_def_cfa_offset:
836     case DW_CFA_def_cfa_offset_sf:
837       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
838       break;
839     case DW_CFA_def_cfa_register:
840       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
841       break;
842     case DW_CFA_def_cfa:
843     case DW_CFA_def_cfa_sf:
844       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
845       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
846       break;
847     case DW_CFA_def_cfa_expression:
848       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
849       break;
850     default:
851       break;
852     }
853 }
854
855 /* Find the previous value for the CFA.  */
856
857 static void
858 lookup_cfa (dw_cfa_location *loc)
859 {
860   dw_cfi_ref cfi;
861   dw_fde_ref fde;
862
863   loc->reg = INVALID_REGNUM;
864   loc->offset = 0;
865   loc->indirect = 0;
866   loc->base_offset = 0;
867
868   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
869     lookup_cfa_1 (cfi, loc);
870
871   fde = current_fde ();
872   if (fde)
873     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
874       lookup_cfa_1 (cfi, loc);
875 }
876
877 /* The current rule for calculating the DWARF2 canonical frame address.  */
878 static dw_cfa_location cfa;
879
880 /* The register used for saving registers to the stack, and its offset
881    from the CFA.  */
882 static dw_cfa_location cfa_store;
883
884 /* The running total of the size of arguments pushed onto the stack.  */
885 static HOST_WIDE_INT args_size;
886
887 /* The last args_size we actually output.  */
888 static HOST_WIDE_INT old_args_size;
889
890 /* Entry point to update the canonical frame address (CFA).
891    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
892    calculated from REG+OFFSET.  */
893
894 void
895 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
896 {
897   dw_cfa_location loc;
898   loc.indirect = 0;
899   loc.base_offset = 0;
900   loc.reg = reg;
901   loc.offset = offset;
902   def_cfa_1 (label, &loc);
903 }
904
905 /* Determine if two dw_cfa_location structures define the same data.  */
906
907 static bool
908 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
909 {
910   return (loc1->reg == loc2->reg
911           && loc1->offset == loc2->offset
912           && loc1->indirect == loc2->indirect
913           && (loc1->indirect == 0
914               || loc1->base_offset == loc2->base_offset));
915 }
916
917 /* This routine does the actual work.  The CFA is now calculated from
918    the dw_cfa_location structure.  */
919
920 static void
921 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
922 {
923   dw_cfi_ref cfi;
924   dw_cfa_location old_cfa, loc;
925
926   cfa = *loc_p;
927   loc = *loc_p;
928
929   if (cfa_store.reg == loc.reg && loc.indirect == 0)
930     cfa_store.offset = loc.offset;
931
932   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
933   lookup_cfa (&old_cfa);
934
935   /* If nothing changed, no need to issue any call frame instructions.  */
936   if (cfa_equal_p (&loc, &old_cfa))
937     return;
938
939   cfi = new_cfi ();
940
941   if (loc.reg == old_cfa.reg && !loc.indirect)
942     {
943       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
944          the CFA register did not change but the offset did.  The data 
945          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
946          in the assembler via the .cfi_def_cfa_offset directive.  */
947       if (loc.offset < 0)
948         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
949       else
950         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
951       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
952     }
953
954 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
955   else if (loc.offset == old_cfa.offset
956            && old_cfa.reg != INVALID_REGNUM
957            && !loc.indirect)
958     {
959       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
960          indicating the CFA register has changed to <register> but the
961          offset has not changed.  */
962       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
963       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
964     }
965 #endif
966
967   else if (loc.indirect == 0)
968     {
969       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
970          indicating the CFA register has changed to <register> with
971          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
972          happens in output_cfi, or in the assembler via the .cfi_def_cfa
973          directive.  */
974       if (loc.offset < 0)
975         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
976       else
977         cfi->dw_cfi_opc = DW_CFA_def_cfa;
978       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
979       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
980     }
981   else
982     {
983       /* Construct a DW_CFA_def_cfa_expression instruction to
984          calculate the CFA using a full location expression since no
985          register-offset pair is available.  */
986       struct dw_loc_descr_struct *loc_list;
987
988       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
989       loc_list = build_cfa_loc (&loc, 0);
990       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
991     }
992
993   add_fde_cfi (label, cfi);
994 }
995
996 /* Add the CFI for saving a register.  REG is the CFA column number.
997    LABEL is passed to add_fde_cfi.
998    If SREG is -1, the register is saved at OFFSET from the CFA;
999    otherwise it is saved in SREG.  */
1000
1001 static void
1002 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1003 {
1004   dw_cfi_ref cfi = new_cfi ();
1005   dw_fde_ref fde = current_fde ();
1006
1007   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1008
1009   /* When stack is aligned, store REG using DW_CFA_expression with
1010      FP.  */
1011   if (fde
1012       && fde->stack_realign
1013       && sreg == INVALID_REGNUM)
1014     {
1015       cfi->dw_cfi_opc = DW_CFA_expression;
1016       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1017       cfi->dw_cfi_oprnd1.dw_cfi_loc
1018         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1019     }
1020   else if (sreg == INVALID_REGNUM)
1021     {
1022       if (offset < 0)
1023         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1024       else if (reg & ~0x3f)
1025         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1026       else
1027         cfi->dw_cfi_opc = DW_CFA_offset;
1028       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1029     }
1030   else if (sreg == reg)
1031     cfi->dw_cfi_opc = DW_CFA_same_value;
1032   else
1033     {
1034       cfi->dw_cfi_opc = DW_CFA_register;
1035       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1036     }
1037
1038   add_fde_cfi (label, cfi);
1039 }
1040
1041 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1042    This CFI tells the unwinder that it needs to restore the window registers
1043    from the previous frame's window save area.
1044
1045    ??? Perhaps we should note in the CIE where windows are saved (instead of
1046    assuming 0(cfa)) and what registers are in the window.  */
1047
1048 void
1049 dwarf2out_window_save (const char *label)
1050 {
1051   dw_cfi_ref cfi = new_cfi ();
1052
1053   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1054   add_fde_cfi (label, cfi);
1055 }
1056
1057 /* Add a CFI to update the running total of the size of arguments
1058    pushed onto the stack.  */
1059
1060 void
1061 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1062 {
1063   dw_cfi_ref cfi;
1064
1065   if (size == old_args_size)
1066     return;
1067
1068   old_args_size = size;
1069
1070   cfi = new_cfi ();
1071   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1072   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1073   add_fde_cfi (label, cfi);
1074 }
1075
1076 /* Entry point for saving a register to the stack.  REG is the GCC register
1077    number.  LABEL and OFFSET are passed to reg_save.  */
1078
1079 void
1080 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1081 {
1082   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1083 }
1084
1085 /* Entry point for saving the return address in the stack.
1086    LABEL and OFFSET are passed to reg_save.  */
1087
1088 void
1089 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1090 {
1091   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1092 }
1093
1094 /* Entry point for saving the return address in a register.
1095    LABEL and SREG are passed to reg_save.  */
1096
1097 void
1098 dwarf2out_return_reg (const char *label, unsigned int sreg)
1099 {
1100   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1101 }
1102
1103 #ifdef DWARF2_UNWIND_INFO
1104 /* Record the initial position of the return address.  RTL is
1105    INCOMING_RETURN_ADDR_RTX.  */
1106
1107 static void
1108 initial_return_save (rtx rtl)
1109 {
1110   unsigned int reg = INVALID_REGNUM;
1111   HOST_WIDE_INT offset = 0;
1112
1113   switch (GET_CODE (rtl))
1114     {
1115     case REG:
1116       /* RA is in a register.  */
1117       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1118       break;
1119
1120     case MEM:
1121       /* RA is on the stack.  */
1122       rtl = XEXP (rtl, 0);
1123       switch (GET_CODE (rtl))
1124         {
1125         case REG:
1126           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1127           offset = 0;
1128           break;
1129
1130         case PLUS:
1131           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1132           offset = INTVAL (XEXP (rtl, 1));
1133           break;
1134
1135         case MINUS:
1136           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1137           offset = -INTVAL (XEXP (rtl, 1));
1138           break;
1139
1140         default:
1141           gcc_unreachable ();
1142         }
1143
1144       break;
1145
1146     case PLUS:
1147       /* The return address is at some offset from any value we can
1148          actually load.  For instance, on the SPARC it is in %i7+8. Just
1149          ignore the offset for now; it doesn't matter for unwinding frames.  */
1150       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1151       initial_return_save (XEXP (rtl, 0));
1152       return;
1153
1154     default:
1155       gcc_unreachable ();
1156     }
1157
1158   if (reg != DWARF_FRAME_RETURN_COLUMN)
1159     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1160 }
1161 #endif
1162
1163 /* Given a SET, calculate the amount of stack adjustment it
1164    contains.  */
1165
1166 static HOST_WIDE_INT
1167 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1168                      HOST_WIDE_INT cur_offset)
1169 {
1170   const_rtx src = SET_SRC (pattern);
1171   const_rtx dest = SET_DEST (pattern);
1172   HOST_WIDE_INT offset = 0;
1173   enum rtx_code code;
1174
1175   if (dest == stack_pointer_rtx)
1176     {
1177       code = GET_CODE (src);
1178
1179       /* Assume (set (reg sp) (reg whatever)) sets args_size
1180          level to 0.  */
1181       if (code == REG && src != stack_pointer_rtx)
1182         {
1183           offset = -cur_args_size;
1184 #ifndef STACK_GROWS_DOWNWARD
1185           offset = -offset;
1186 #endif
1187           return offset - cur_offset;
1188         }
1189
1190       if (! (code == PLUS || code == MINUS)
1191           || XEXP (src, 0) != stack_pointer_rtx
1192           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1193         return 0;
1194
1195       /* (set (reg sp) (plus (reg sp) (const_int))) */
1196       offset = INTVAL (XEXP (src, 1));
1197       if (code == PLUS)
1198         offset = -offset;
1199       return offset;
1200     }
1201
1202   if (MEM_P (src) && !MEM_P (dest))
1203     dest = src;
1204   if (MEM_P (dest))
1205     {
1206       /* (set (mem (pre_dec (reg sp))) (foo)) */
1207       src = XEXP (dest, 0);
1208       code = GET_CODE (src);
1209
1210       switch (code)
1211         {
1212         case PRE_MODIFY:
1213         case POST_MODIFY:
1214           if (XEXP (src, 0) == stack_pointer_rtx)
1215             {
1216               rtx val = XEXP (XEXP (src, 1), 1);
1217               /* We handle only adjustments by constant amount.  */
1218               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1219                           && GET_CODE (val) == CONST_INT);
1220               offset = -INTVAL (val);
1221               break;
1222             }
1223           return 0;
1224
1225         case PRE_DEC:
1226         case POST_DEC:
1227           if (XEXP (src, 0) == stack_pointer_rtx)
1228             {
1229               offset = GET_MODE_SIZE (GET_MODE (dest));
1230               break;
1231             }
1232           return 0;
1233
1234         case PRE_INC:
1235         case POST_INC:
1236           if (XEXP (src, 0) == stack_pointer_rtx)
1237             {
1238               offset = -GET_MODE_SIZE (GET_MODE (dest));
1239               break;
1240             }
1241           return 0;
1242
1243         default:
1244           return 0;
1245         }
1246     }
1247   else
1248     return 0;
1249
1250   return offset;
1251 }
1252
1253 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1254    indexed by INSN_UID.  */
1255
1256 static HOST_WIDE_INT *barrier_args_size;
1257
1258 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1259
1260 static HOST_WIDE_INT
1261 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1262                              VEC (rtx, heap) **next)
1263 {
1264   HOST_WIDE_INT offset = 0;
1265   int i;
1266
1267   if (! RTX_FRAME_RELATED_P (insn))
1268     {
1269       if (prologue_epilogue_contains (insn)
1270           || sibcall_epilogue_contains (insn))
1271         /* Nothing */;
1272       else if (GET_CODE (PATTERN (insn)) == SET)
1273         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1274       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1275                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1276         {
1277           /* There may be stack adjustments inside compound insns.  Search
1278              for them.  */
1279           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1280             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1281               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1282                                              cur_args_size, offset);
1283         }
1284     }
1285   else
1286     {
1287       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1288
1289       if (expr)
1290         {
1291           expr = XEXP (expr, 0);
1292           if (GET_CODE (expr) == PARALLEL
1293               || GET_CODE (expr) == SEQUENCE)
1294             for (i = 1; i < XVECLEN (expr, 0); i++)
1295               {
1296                 rtx elem = XVECEXP (expr, 0, i);
1297
1298                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1299                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1300               }
1301         }
1302     }
1303
1304 #ifndef STACK_GROWS_DOWNWARD
1305   offset = -offset;
1306 #endif
1307
1308   cur_args_size += offset;
1309   if (cur_args_size < 0)
1310     cur_args_size = 0;
1311
1312   if (JUMP_P (insn))
1313     {
1314       rtx dest = JUMP_LABEL (insn);
1315
1316       if (dest)
1317         {
1318           if (barrier_args_size [INSN_UID (dest)] < 0)
1319             {
1320               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1321               VEC_safe_push (rtx, heap, *next, dest);
1322             }
1323         }
1324     }
1325
1326   return cur_args_size;
1327 }
1328
1329 /* Walk the whole function and compute args_size on BARRIERs.  */
1330
1331 static void
1332 compute_barrier_args_size (void)
1333 {
1334   int max_uid = get_max_uid (), i;
1335   rtx insn;
1336   VEC (rtx, heap) *worklist, *next, *tmp;
1337
1338   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1339   for (i = 0; i < max_uid; i++)
1340     barrier_args_size[i] = -1;
1341
1342   worklist = VEC_alloc (rtx, heap, 20);
1343   next = VEC_alloc (rtx, heap, 20);
1344   insn = get_insns ();
1345   barrier_args_size[INSN_UID (insn)] = 0;
1346   VEC_quick_push (rtx, worklist, insn);
1347   for (;;)
1348     {
1349       while (!VEC_empty (rtx, worklist))
1350         {
1351           rtx prev, body, first_insn;
1352           HOST_WIDE_INT cur_args_size;
1353
1354           first_insn = insn = VEC_pop (rtx, worklist);
1355           cur_args_size = barrier_args_size[INSN_UID (insn)];
1356           prev = prev_nonnote_insn (insn);
1357           if (prev && BARRIER_P (prev))
1358             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1359
1360           for (; insn; insn = NEXT_INSN (insn))
1361             {
1362               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1363                 continue;
1364               if (BARRIER_P (insn))
1365                 break;
1366
1367               if (LABEL_P (insn))
1368                 {
1369                   if (insn == first_insn)
1370                     continue;
1371                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1372                     {
1373                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1374                       continue;
1375                     }
1376                   else
1377                     {
1378                       /* The insns starting with this label have been
1379                          already scanned or are in the worklist.  */
1380                       break;
1381                     }
1382                 }
1383
1384               body = PATTERN (insn);
1385               if (GET_CODE (body) == SEQUENCE)
1386                 {
1387                   HOST_WIDE_INT dest_args_size = cur_args_size;
1388                   for (i = 1; i < XVECLEN (body, 0); i++)
1389                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1390                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1391                       dest_args_size
1392                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1393                                                        dest_args_size, &next);
1394                     else
1395                       cur_args_size
1396                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1397                                                        cur_args_size, &next);
1398
1399                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1400                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1401                                                  dest_args_size, &next);
1402                   else
1403                     cur_args_size
1404                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1405                                                      cur_args_size, &next);
1406                 }
1407               else
1408                 cur_args_size
1409                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1410             }
1411         }
1412
1413       if (VEC_empty (rtx, next))
1414         break;
1415
1416       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1417       tmp = next;
1418       next = worklist;
1419       worklist = tmp;
1420       VEC_truncate (rtx, next, 0);
1421     }
1422
1423   VEC_free (rtx, heap, worklist);
1424   VEC_free (rtx, heap, next);
1425 }
1426
1427
1428 /* Check INSN to see if it looks like a push or a stack adjustment, and
1429    make a note of it if it does.  EH uses this information to find out how
1430    much extra space it needs to pop off the stack.  */
1431
1432 static void
1433 dwarf2out_stack_adjust (rtx insn, bool after_p)
1434 {
1435   HOST_WIDE_INT offset;
1436   const char *label;
1437   int i;
1438
1439   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1440      with this function.  Proper support would require all frame-related
1441      insns to be marked, and to be able to handle saving state around
1442      epilogues textually in the middle of the function.  */
1443   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1444     return;
1445
1446   /* If INSN is an instruction from target of an annulled branch, the
1447      effects are for the target only and so current argument size
1448      shouldn't change at all.  */
1449   if (final_sequence
1450       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1451       && INSN_FROM_TARGET_P (insn))
1452     return;
1453
1454   /* If only calls can throw, and we have a frame pointer,
1455      save up adjustments until we see the CALL_INSN.  */
1456   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1457     {
1458       if (CALL_P (insn) && !after_p)
1459         {
1460           /* Extract the size of the args from the CALL rtx itself.  */
1461           insn = PATTERN (insn);
1462           if (GET_CODE (insn) == PARALLEL)
1463             insn = XVECEXP (insn, 0, 0);
1464           if (GET_CODE (insn) == SET)
1465             insn = SET_SRC (insn);
1466           gcc_assert (GET_CODE (insn) == CALL);
1467           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1468         }
1469       return;
1470     }
1471
1472   if (CALL_P (insn) && !after_p)
1473     {
1474       if (!flag_asynchronous_unwind_tables)
1475         dwarf2out_args_size ("", args_size);
1476       return;
1477     }
1478   else if (BARRIER_P (insn))
1479     {
1480       /* Don't call compute_barrier_args_size () if the only
1481          BARRIER is at the end of function.  */
1482       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1483         compute_barrier_args_size ();
1484       if (barrier_args_size == NULL)
1485         offset = 0;
1486       else
1487         {
1488           offset = barrier_args_size[INSN_UID (insn)];
1489           if (offset < 0)
1490             offset = 0;
1491         }
1492
1493       offset -= args_size;
1494 #ifndef STACK_GROWS_DOWNWARD
1495       offset = -offset;
1496 #endif
1497     }
1498   else if (GET_CODE (PATTERN (insn)) == SET)
1499     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1500   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1501            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1502     {
1503       /* There may be stack adjustments inside compound insns.  Search
1504          for them.  */
1505       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1506         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1507           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1508                                          args_size, offset);
1509     }
1510   else
1511     return;
1512
1513   if (offset == 0)
1514     return;
1515
1516   label = dwarf2out_cfi_label (false);
1517   dwarf2out_args_size_adjust (offset, label);
1518 }
1519
1520 /* Adjust args_size based on stack adjustment OFFSET.  */
1521
1522 static void
1523 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1524 {
1525   if (cfa.reg == STACK_POINTER_REGNUM)
1526     cfa.offset += offset;
1527
1528   if (cfa_store.reg == STACK_POINTER_REGNUM)
1529     cfa_store.offset += offset;
1530
1531 #ifndef STACK_GROWS_DOWNWARD
1532   offset = -offset;
1533 #endif
1534
1535   args_size += offset;
1536   if (args_size < 0)
1537     args_size = 0;
1538
1539   def_cfa_1 (label, &cfa);
1540   if (flag_asynchronous_unwind_tables)
1541     dwarf2out_args_size (label, args_size);
1542 }
1543
1544 #endif
1545
1546 /* We delay emitting a register save until either (a) we reach the end
1547    of the prologue or (b) the register is clobbered.  This clusters
1548    register saves so that there are fewer pc advances.  */
1549
1550 struct queued_reg_save GTY(())
1551 {
1552   struct queued_reg_save *next;
1553   rtx reg;
1554   HOST_WIDE_INT cfa_offset;
1555   rtx saved_reg;
1556 };
1557
1558 static GTY(()) struct queued_reg_save *queued_reg_saves;
1559
1560 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1561 struct reg_saved_in_data GTY(()) {
1562   rtx orig_reg;
1563   rtx saved_in_reg;
1564 };
1565
1566 /* A list of registers saved in other registers.
1567    The list intentionally has a small maximum capacity of 4; if your
1568    port needs more than that, you might consider implementing a
1569    more efficient data structure.  */
1570 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1571 static GTY(()) size_t num_regs_saved_in_regs;
1572
1573 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1574 static const char *last_reg_save_label;
1575
1576 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1577    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1578
1579 static void
1580 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1581 {
1582   struct queued_reg_save *q;
1583
1584   /* Duplicates waste space, but it's also necessary to remove them
1585      for correctness, since the queue gets output in reverse
1586      order.  */
1587   for (q = queued_reg_saves; q != NULL; q = q->next)
1588     if (REGNO (q->reg) == REGNO (reg))
1589       break;
1590
1591   if (q == NULL)
1592     {
1593       q = GGC_NEW (struct queued_reg_save);
1594       q->next = queued_reg_saves;
1595       queued_reg_saves = q;
1596     }
1597
1598   q->reg = reg;
1599   q->cfa_offset = offset;
1600   q->saved_reg = sreg;
1601
1602   last_reg_save_label = label;
1603 }
1604
1605 /* Output all the entries in QUEUED_REG_SAVES.  */
1606
1607 static void
1608 flush_queued_reg_saves (void)
1609 {
1610   struct queued_reg_save *q;
1611
1612   for (q = queued_reg_saves; q; q = q->next)
1613     {
1614       size_t i;
1615       unsigned int reg, sreg;
1616
1617       for (i = 0; i < num_regs_saved_in_regs; i++)
1618         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1619           break;
1620       if (q->saved_reg && i == num_regs_saved_in_regs)
1621         {
1622           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1623           num_regs_saved_in_regs++;
1624         }
1625       if (i != num_regs_saved_in_regs)
1626         {
1627           regs_saved_in_regs[i].orig_reg = q->reg;
1628           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1629         }
1630
1631       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1632       if (q->saved_reg)
1633         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1634       else
1635         sreg = INVALID_REGNUM;
1636       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1637     }
1638
1639   queued_reg_saves = NULL;
1640   last_reg_save_label = NULL;
1641 }
1642
1643 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1644    location for?  Or, does it clobber a register which we've previously
1645    said that some other register is saved in, and for which we now
1646    have a new location for?  */
1647
1648 static bool
1649 clobbers_queued_reg_save (const_rtx insn)
1650 {
1651   struct queued_reg_save *q;
1652
1653   for (q = queued_reg_saves; q; q = q->next)
1654     {
1655       size_t i;
1656       if (modified_in_p (q->reg, insn))
1657         return true;
1658       for (i = 0; i < num_regs_saved_in_regs; i++)
1659         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1660             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1661           return true;
1662     }
1663
1664   return false;
1665 }
1666
1667 /* Entry point for saving the first register into the second.  */
1668
1669 void
1670 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1671 {
1672   size_t i;
1673   unsigned int regno, sregno;
1674
1675   for (i = 0; i < num_regs_saved_in_regs; i++)
1676     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1677       break;
1678   if (i == num_regs_saved_in_regs)
1679     {
1680       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1681       num_regs_saved_in_regs++;
1682     }
1683   regs_saved_in_regs[i].orig_reg = reg;
1684   regs_saved_in_regs[i].saved_in_reg = sreg;
1685
1686   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1687   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1688   reg_save (label, regno, sregno, 0);
1689 }
1690
1691 /* What register, if any, is currently saved in REG?  */
1692
1693 static rtx
1694 reg_saved_in (rtx reg)
1695 {
1696   unsigned int regn = REGNO (reg);
1697   size_t i;
1698   struct queued_reg_save *q;
1699
1700   for (q = queued_reg_saves; q; q = q->next)
1701     if (q->saved_reg && regn == REGNO (q->saved_reg))
1702       return q->reg;
1703
1704   for (i = 0; i < num_regs_saved_in_regs; i++)
1705     if (regs_saved_in_regs[i].saved_in_reg
1706         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1707       return regs_saved_in_regs[i].orig_reg;
1708
1709   return NULL_RTX;
1710 }
1711
1712
1713 /* A temporary register holding an integral value used in adjusting SP
1714    or setting up the store_reg.  The "offset" field holds the integer
1715    value, not an offset.  */
1716 static dw_cfa_location cfa_temp;
1717
1718 /* Record call frame debugging information for an expression EXPR,
1719    which either sets SP or FP (adjusting how we calculate the frame
1720    address) or saves a register to the stack or another register.
1721    LABEL indicates the address of EXPR.
1722
1723    This function encodes a state machine mapping rtxes to actions on
1724    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1725    users need not read the source code.
1726
1727   The High-Level Picture
1728
1729   Changes in the register we use to calculate the CFA: Currently we
1730   assume that if you copy the CFA register into another register, we
1731   should take the other one as the new CFA register; this seems to
1732   work pretty well.  If it's wrong for some target, it's simple
1733   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1734
1735   Changes in the register we use for saving registers to the stack:
1736   This is usually SP, but not always.  Again, we deduce that if you
1737   copy SP into another register (and SP is not the CFA register),
1738   then the new register is the one we will be using for register
1739   saves.  This also seems to work.
1740
1741   Register saves: There's not much guesswork about this one; if
1742   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1743   register save, and the register used to calculate the destination
1744   had better be the one we think we're using for this purpose.
1745   It's also assumed that a copy from a call-saved register to another
1746   register is saving that register if RTX_FRAME_RELATED_P is set on
1747   that instruction.  If the copy is from a call-saved register to
1748   the *same* register, that means that the register is now the same
1749   value as in the caller.
1750
1751   Except: If the register being saved is the CFA register, and the
1752   offset is nonzero, we are saving the CFA, so we assume we have to
1753   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1754   the intent is to save the value of SP from the previous frame.
1755
1756   In addition, if a register has previously been saved to a different
1757   register,
1758
1759   Invariants / Summaries of Rules
1760
1761   cfa          current rule for calculating the CFA.  It usually
1762                consists of a register and an offset.
1763   cfa_store    register used by prologue code to save things to the stack
1764                cfa_store.offset is the offset from the value of
1765                cfa_store.reg to the actual CFA
1766   cfa_temp     register holding an integral value.  cfa_temp.offset
1767                stores the value, which will be used to adjust the
1768                stack pointer.  cfa_temp is also used like cfa_store,
1769                to track stores to the stack via fp or a temp reg.
1770
1771   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1772                with cfa.reg as the first operand changes the cfa.reg and its
1773                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1774                cfa_temp.offset.
1775
1776   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1777                expression yielding a constant.  This sets cfa_temp.reg
1778                and cfa_temp.offset.
1779
1780   Rule 5:      Create a new register cfa_store used to save items to the
1781                stack.
1782
1783   Rules 10-14: Save a register to the stack.  Define offset as the
1784                difference of the original location and cfa_store's
1785                location (or cfa_temp's location if cfa_temp is used).
1786
1787   Rules 16-20: If AND operation happens on sp in prologue, we assume
1788                stack is realigned.  We will use a group of DW_OP_XXX
1789                expressions to represent the location of the stored
1790                register instead of CFA+offset.
1791
1792   The Rules
1793
1794   "{a,b}" indicates a choice of a xor b.
1795   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1796
1797   Rule 1:
1798   (set <reg1> <reg2>:cfa.reg)
1799   effects: cfa.reg = <reg1>
1800            cfa.offset unchanged
1801            cfa_temp.reg = <reg1>
1802            cfa_temp.offset = cfa.offset
1803
1804   Rule 2:
1805   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1806                               {<const_int>,<reg>:cfa_temp.reg}))
1807   effects: cfa.reg = sp if fp used
1808            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1809            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1810              if cfa_store.reg==sp
1811
1812   Rule 3:
1813   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1814   effects: cfa.reg = fp
1815            cfa_offset += +/- <const_int>
1816
1817   Rule 4:
1818   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1819   constraints: <reg1> != fp
1820                <reg1> != sp
1821   effects: cfa.reg = <reg1>
1822            cfa_temp.reg = <reg1>
1823            cfa_temp.offset = cfa.offset
1824
1825   Rule 5:
1826   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1827   constraints: <reg1> != fp
1828                <reg1> != sp
1829   effects: cfa_store.reg = <reg1>
1830            cfa_store.offset = cfa.offset - cfa_temp.offset
1831
1832   Rule 6:
1833   (set <reg> <const_int>)
1834   effects: cfa_temp.reg = <reg>
1835            cfa_temp.offset = <const_int>
1836
1837   Rule 7:
1838   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1839   effects: cfa_temp.reg = <reg1>
1840            cfa_temp.offset |= <const_int>
1841
1842   Rule 8:
1843   (set <reg> (high <exp>))
1844   effects: none
1845
1846   Rule 9:
1847   (set <reg> (lo_sum <exp> <const_int>))
1848   effects: cfa_temp.reg = <reg>
1849            cfa_temp.offset = <const_int>
1850
1851   Rule 10:
1852   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1853   effects: cfa_store.offset -= <const_int>
1854            cfa.offset = cfa_store.offset if cfa.reg == sp
1855            cfa.reg = sp
1856            cfa.base_offset = -cfa_store.offset
1857
1858   Rule 11:
1859   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1860   effects: cfa_store.offset += -/+ mode_size(mem)
1861            cfa.offset = cfa_store.offset if cfa.reg == sp
1862            cfa.reg = sp
1863            cfa.base_offset = -cfa_store.offset
1864
1865   Rule 12:
1866   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1867
1868        <reg2>)
1869   effects: cfa.reg = <reg1>
1870            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1871
1872   Rule 13:
1873   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1874   effects: cfa.reg = <reg1>
1875            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1876
1877   Rule 14:
1878   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1879   effects: cfa.reg = <reg1>
1880            cfa.base_offset = -cfa_temp.offset
1881            cfa_temp.offset -= mode_size(mem)
1882
1883   Rule 15:
1884   (set <reg> {unspec, unspec_volatile})
1885   effects: target-dependent
1886
1887   Rule 16:
1888   (set sp (and: sp <const_int>))
1889   constraints: cfa_store.reg == sp
1890   effects: current_fde.stack_realign = 1
1891            cfa_store.offset = 0
1892            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1893
1894   Rule 17:
1895   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1896   effects: cfa_store.offset += -/+ mode_size(mem)
1897
1898   Rule 18:
1899   (set (mem ({pre_inc, pre_dec} sp)) fp)
1900   constraints: fde->stack_realign == 1
1901   effects: cfa_store.offset = 0
1902            cfa.reg != HARD_FRAME_POINTER_REGNUM
1903
1904   Rule 19:
1905   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1906   constraints: fde->stack_realign == 1
1907                && cfa.offset == 0
1908                && cfa.indirect == 0
1909                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1910   effects: Use DW_CFA_def_cfa_expression to define cfa
1911            cfa.reg == fde->drap_reg
1912
1913   Rule 20:
1914   (set reg fde->drap_reg)
1915   constraints: fde->vdrap_reg == INVALID_REGNUM
1916   effects: fde->vdrap_reg = reg.
1917   (set mem fde->drap_reg)
1918   constraints: fde->drap_reg_saved == 1
1919   effects: none.  */
1920
1921 static void
1922 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1923 {
1924   rtx src, dest, span;
1925   HOST_WIDE_INT offset;
1926   dw_fde_ref fde;
1927
1928   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1929      the PARALLEL independently. The first element is always processed if
1930      it is a SET. This is for backward compatibility.   Other elements
1931      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1932      flag is set in them.  */
1933   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1934     {
1935       int par_index;
1936       int limit = XVECLEN (expr, 0);
1937       rtx elem;
1938
1939       /* PARALLELs have strict read-modify-write semantics, so we
1940          ought to evaluate every rvalue before changing any lvalue.
1941          It's cumbersome to do that in general, but there's an
1942          easy approximation that is enough for all current users:
1943          handle register saves before register assignments.  */
1944       if (GET_CODE (expr) == PARALLEL)
1945         for (par_index = 0; par_index < limit; par_index++)
1946           {
1947             elem = XVECEXP (expr, 0, par_index);
1948             if (GET_CODE (elem) == SET
1949                 && MEM_P (SET_DEST (elem))
1950                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1951               dwarf2out_frame_debug_expr (elem, label);
1952           }
1953
1954       for (par_index = 0; par_index < limit; par_index++)
1955         {
1956           elem = XVECEXP (expr, 0, par_index);
1957           if (GET_CODE (elem) == SET
1958               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1959               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1960             dwarf2out_frame_debug_expr (elem, label);
1961           else if (GET_CODE (elem) == SET
1962                    && par_index != 0
1963                    && !RTX_FRAME_RELATED_P (elem))
1964             {
1965               /* Stack adjustment combining might combine some post-prologue
1966                  stack adjustment into a prologue stack adjustment.  */
1967               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1968
1969               if (offset != 0)
1970                 dwarf2out_args_size_adjust (offset, label);
1971             }
1972         }
1973       return;
1974     }
1975
1976   gcc_assert (GET_CODE (expr) == SET);
1977
1978   src = SET_SRC (expr);
1979   dest = SET_DEST (expr);
1980
1981   if (REG_P (src))
1982     {
1983       rtx rsi = reg_saved_in (src);
1984       if (rsi)
1985         src = rsi;
1986     }
1987
1988   fde = current_fde ();
1989
1990   if (GET_CODE (src) == REG
1991       && fde
1992       && fde->drap_reg == REGNO (src)
1993       && (fde->drap_reg_saved
1994           || GET_CODE (dest) == REG))
1995     {
1996       /* Rule 20 */
1997       /* If we are saving dynamic realign argument pointer to a
1998          register, the destination is virtual dynamic realign
1999          argument pointer.  It may be used to access argument.  */
2000       if (GET_CODE (dest) == REG)
2001         {
2002           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2003           fde->vdrap_reg = REGNO (dest);
2004         }
2005       return;
2006     }
2007
2008   switch (GET_CODE (dest))
2009     {
2010     case REG:
2011       switch (GET_CODE (src))
2012         {
2013           /* Setting FP from SP.  */
2014         case REG:
2015           if (cfa.reg == (unsigned) REGNO (src))
2016             {
2017               /* Rule 1 */
2018               /* Update the CFA rule wrt SP or FP.  Make sure src is
2019                  relative to the current CFA register.
2020
2021                  We used to require that dest be either SP or FP, but the
2022                  ARM copies SP to a temporary register, and from there to
2023                  FP.  So we just rely on the backends to only set
2024                  RTX_FRAME_RELATED_P on appropriate insns.  */
2025               cfa.reg = REGNO (dest);
2026               cfa_temp.reg = cfa.reg;
2027               cfa_temp.offset = cfa.offset;
2028             }
2029           else
2030             {
2031               /* Saving a register in a register.  */
2032               gcc_assert (!fixed_regs [REGNO (dest)]
2033                           /* For the SPARC and its register window.  */
2034                           || (DWARF_FRAME_REGNUM (REGNO (src))
2035                               == DWARF_FRAME_RETURN_COLUMN));
2036
2037               /* After stack is aligned, we can only save SP in FP
2038                  if drap register is used.  In this case, we have
2039                  to restore stack pointer with the CFA value and we
2040                  don't generate this DWARF information.  */
2041               if (fde
2042                   && fde->stack_realign
2043                   && REGNO (src) == STACK_POINTER_REGNUM)
2044                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2045                             && fde->drap_reg != INVALID_REGNUM
2046                             && cfa.reg != REGNO (src));
2047               else
2048                 queue_reg_save (label, src, dest, 0);
2049             }
2050           break;
2051
2052         case PLUS:
2053         case MINUS:
2054         case LO_SUM:
2055           if (dest == stack_pointer_rtx)
2056             {
2057               /* Rule 2 */
2058               /* Adjusting SP.  */
2059               switch (GET_CODE (XEXP (src, 1)))
2060                 {
2061                 case CONST_INT:
2062                   offset = INTVAL (XEXP (src, 1));
2063                   break;
2064                 case REG:
2065                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2066                               == cfa_temp.reg);
2067                   offset = cfa_temp.offset;
2068                   break;
2069                 default:
2070                   gcc_unreachable ();
2071                 }
2072
2073               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2074                 {
2075                   /* Restoring SP from FP in the epilogue.  */
2076                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2077                   cfa.reg = STACK_POINTER_REGNUM;
2078                 }
2079               else if (GET_CODE (src) == LO_SUM)
2080                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2081                 ;
2082               else
2083                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2084
2085               if (GET_CODE (src) != MINUS)
2086                 offset = -offset;
2087               if (cfa.reg == STACK_POINTER_REGNUM)
2088                 cfa.offset += offset;
2089               if (cfa_store.reg == STACK_POINTER_REGNUM)
2090                 cfa_store.offset += offset;
2091             }
2092           else if (dest == hard_frame_pointer_rtx)
2093             {
2094               /* Rule 3 */
2095               /* Either setting the FP from an offset of the SP,
2096                  or adjusting the FP */
2097               gcc_assert (frame_pointer_needed);
2098
2099               gcc_assert (REG_P (XEXP (src, 0))
2100                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2101                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2102               offset = INTVAL (XEXP (src, 1));
2103               if (GET_CODE (src) != MINUS)
2104                 offset = -offset;
2105               cfa.offset += offset;
2106               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2107             }
2108           else
2109             {
2110               gcc_assert (GET_CODE (src) != MINUS);
2111
2112               /* Rule 4 */
2113               if (REG_P (XEXP (src, 0))
2114                   && REGNO (XEXP (src, 0)) == cfa.reg
2115                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2116                 {
2117                   /* Setting a temporary CFA register that will be copied
2118                      into the FP later on.  */
2119                   offset = - INTVAL (XEXP (src, 1));
2120                   cfa.offset += offset;
2121                   cfa.reg = REGNO (dest);
2122                   /* Or used to save regs to the stack.  */
2123                   cfa_temp.reg = cfa.reg;
2124                   cfa_temp.offset = cfa.offset;
2125                 }
2126
2127               /* Rule 5 */
2128               else if (REG_P (XEXP (src, 0))
2129                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2130                        && XEXP (src, 1) == stack_pointer_rtx)
2131                 {
2132                   /* Setting a scratch register that we will use instead
2133                      of SP for saving registers to the stack.  */
2134                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2135                   cfa_store.reg = REGNO (dest);
2136                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2137                 }
2138
2139               /* Rule 9 */
2140               else if (GET_CODE (src) == LO_SUM
2141                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2142                 {
2143                   cfa_temp.reg = REGNO (dest);
2144                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2145                 }
2146               else
2147                 gcc_unreachable ();
2148             }
2149           break;
2150
2151           /* Rule 6 */
2152         case CONST_INT:
2153           cfa_temp.reg = REGNO (dest);
2154           cfa_temp.offset = INTVAL (src);
2155           break;
2156
2157           /* Rule 7 */
2158         case IOR:
2159           gcc_assert (REG_P (XEXP (src, 0))
2160                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2161                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2162
2163           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2164             cfa_temp.reg = REGNO (dest);
2165           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2166           break;
2167
2168           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2169              which will fill in all of the bits.  */
2170           /* Rule 8 */
2171         case HIGH:
2172           break;
2173
2174           /* Rule 15 */
2175         case UNSPEC:
2176         case UNSPEC_VOLATILE:
2177           gcc_assert (targetm.dwarf_handle_frame_unspec);
2178           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2179           return;
2180
2181           /* Rule 16 */
2182         case AND:
2183           /* If this AND operation happens on stack pointer in prologue,
2184              we assume the stack is realigned and we extract the
2185              alignment.  */
2186           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2187             {
2188               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2189               fde->stack_realign = 1;
2190               fde->stack_realignment = INTVAL (XEXP (src, 1));
2191               cfa_store.offset = 0;
2192
2193               if (cfa.reg != STACK_POINTER_REGNUM
2194                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2195                 fde->drap_reg = cfa.reg;
2196             }
2197           return;
2198
2199         default:
2200           gcc_unreachable ();
2201         }
2202
2203       def_cfa_1 (label, &cfa);
2204       break;
2205
2206     case MEM:
2207
2208       /* Saving a register to the stack.  Make sure dest is relative to the
2209          CFA register.  */
2210       switch (GET_CODE (XEXP (dest, 0)))
2211         {
2212           /* Rule 10 */
2213           /* With a push.  */
2214         case PRE_MODIFY:
2215           /* We can't handle variable size modifications.  */
2216           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2217                       == CONST_INT);
2218           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2219
2220           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2221                       && cfa_store.reg == STACK_POINTER_REGNUM);
2222
2223           cfa_store.offset += offset;
2224           if (cfa.reg == STACK_POINTER_REGNUM)
2225             cfa.offset = cfa_store.offset;
2226
2227           offset = -cfa_store.offset;
2228           break;
2229
2230           /* Rule 11 */
2231         case PRE_INC:
2232         case PRE_DEC:
2233           offset = GET_MODE_SIZE (GET_MODE (dest));
2234           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2235             offset = -offset;
2236
2237           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2238                        == STACK_POINTER_REGNUM)
2239                       && cfa_store.reg == STACK_POINTER_REGNUM);
2240
2241           cfa_store.offset += offset;
2242
2243           /* Rule 18: If stack is aligned, we will use FP as a
2244              reference to represent the address of the stored
2245              regiser.  */
2246           if (fde
2247               && fde->stack_realign
2248               && src == hard_frame_pointer_rtx)
2249             {
2250               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2251               cfa_store.offset = 0;
2252             }
2253
2254           if (cfa.reg == STACK_POINTER_REGNUM)
2255             cfa.offset = cfa_store.offset;
2256
2257           offset = -cfa_store.offset;
2258           break;
2259
2260           /* Rule 12 */
2261           /* With an offset.  */
2262         case PLUS:
2263         case MINUS:
2264         case LO_SUM:
2265           {
2266             int regno;
2267
2268             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2269                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2270             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2271             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2272               offset = -offset;
2273
2274             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2275
2276             if (cfa_store.reg == (unsigned) regno)
2277               offset -= cfa_store.offset;
2278             else
2279               {
2280                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2281                 offset -= cfa_temp.offset;
2282               }
2283           }
2284           break;
2285
2286           /* Rule 13 */
2287           /* Without an offset.  */
2288         case REG:
2289           {
2290             int regno = REGNO (XEXP (dest, 0));
2291
2292             if (cfa_store.reg == (unsigned) regno)
2293               offset = -cfa_store.offset;
2294             else
2295               {
2296                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2297                 offset = -cfa_temp.offset;
2298               }
2299           }
2300           break;
2301
2302           /* Rule 14 */
2303         case POST_INC:
2304           gcc_assert (cfa_temp.reg
2305                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2306           offset = -cfa_temp.offset;
2307           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2308           break;
2309
2310         default:
2311           gcc_unreachable ();
2312         }
2313
2314         /* Rule 17 */
2315         /* If the source operand of this MEM operation is not a
2316            register, basically the source is return address.  Here
2317            we only care how much stack grew and we don't save it.  */
2318       if (!REG_P (src))
2319         break;
2320
2321       if (REGNO (src) != STACK_POINTER_REGNUM
2322           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2323           && (unsigned) REGNO (src) == cfa.reg)
2324         {
2325           /* We're storing the current CFA reg into the stack.  */
2326
2327           if (cfa.offset == 0)
2328             {
2329               /* Rule 19 */
2330               /* If stack is aligned, putting CFA reg into stack means
2331                  we can no longer use reg + offset to represent CFA.
2332                  Here we use DW_CFA_def_cfa_expression instead.  The
2333                  result of this expression equals to the original CFA
2334                  value.  */
2335               if (fde
2336                   && fde->stack_realign
2337                   && cfa.indirect == 0
2338                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2339                 {
2340                   dw_cfa_location cfa_exp;
2341
2342                   gcc_assert (fde->drap_reg == cfa.reg);
2343
2344                   cfa_exp.indirect = 1;
2345                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2346                   cfa_exp.base_offset = offset;
2347                   cfa_exp.offset = 0;
2348
2349                   fde->drap_reg_saved = 1;
2350
2351                   def_cfa_1 (label, &cfa_exp);
2352                   break;
2353                 }
2354
2355               /* If the source register is exactly the CFA, assume
2356                  we're saving SP like any other register; this happens
2357                  on the ARM.  */
2358               def_cfa_1 (label, &cfa);
2359               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2360               break;
2361             }
2362           else
2363             {
2364               /* Otherwise, we'll need to look in the stack to
2365                  calculate the CFA.  */
2366               rtx x = XEXP (dest, 0);
2367
2368               if (!REG_P (x))
2369                 x = XEXP (x, 0);
2370               gcc_assert (REG_P (x));
2371
2372               cfa.reg = REGNO (x);
2373               cfa.base_offset = offset;
2374               cfa.indirect = 1;
2375               def_cfa_1 (label, &cfa);
2376               break;
2377             }
2378         }
2379
2380       def_cfa_1 (label, &cfa);
2381       {
2382         span = targetm.dwarf_register_span (src);
2383
2384         if (!span)
2385           queue_reg_save (label, src, NULL_RTX, offset);
2386         else
2387           {
2388             /* We have a PARALLEL describing where the contents of SRC
2389                live.  Queue register saves for each piece of the
2390                PARALLEL.  */
2391             int par_index;
2392             int limit;
2393             HOST_WIDE_INT span_offset = offset;
2394
2395             gcc_assert (GET_CODE (span) == PARALLEL);
2396
2397             limit = XVECLEN (span, 0);
2398             for (par_index = 0; par_index < limit; par_index++)
2399               {
2400                 rtx elem = XVECEXP (span, 0, par_index);
2401
2402                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2403                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2404               }
2405           }
2406       }
2407       break;
2408
2409     default:
2410       gcc_unreachable ();
2411     }
2412 }
2413
2414 /* Record call frame debugging information for INSN, which either
2415    sets SP or FP (adjusting how we calculate the frame address) or saves a
2416    register to the stack.  If INSN is NULL_RTX, initialize our state.
2417
2418    If AFTER_P is false, we're being called before the insn is emitted,
2419    otherwise after.  Call instructions get invoked twice.  */
2420
2421 void
2422 dwarf2out_frame_debug (rtx insn, bool after_p)
2423 {
2424   const char *label;
2425   rtx src;
2426
2427   if (insn == NULL_RTX)
2428     {
2429       size_t i;
2430
2431       /* Flush any queued register saves.  */
2432       flush_queued_reg_saves ();
2433
2434       /* Set up state for generating call frame debug info.  */
2435       lookup_cfa (&cfa);
2436       gcc_assert (cfa.reg
2437                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2438
2439       cfa.reg = STACK_POINTER_REGNUM;
2440       cfa_store = cfa;
2441       cfa_temp.reg = -1;
2442       cfa_temp.offset = 0;
2443
2444       for (i = 0; i < num_regs_saved_in_regs; i++)
2445         {
2446           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2447           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2448         }
2449       num_regs_saved_in_regs = 0;
2450
2451       if (barrier_args_size)
2452         {
2453           XDELETEVEC (barrier_args_size);
2454           barrier_args_size = NULL;
2455         }
2456       return;
2457     }
2458
2459   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2460     flush_queued_reg_saves ();
2461
2462   if (! RTX_FRAME_RELATED_P (insn))
2463     {
2464       if (!ACCUMULATE_OUTGOING_ARGS)
2465         dwarf2out_stack_adjust (insn, after_p);
2466       return;
2467     }
2468
2469   label = dwarf2out_cfi_label (false);
2470   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2471   if (src)
2472     insn = XEXP (src, 0);
2473   else
2474     insn = PATTERN (insn);
2475
2476   dwarf2out_frame_debug_expr (insn, label);
2477 }
2478
2479 #endif
2480
2481 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2482 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2483  (enum dwarf_call_frame_info cfi);
2484
2485 static enum dw_cfi_oprnd_type
2486 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2487 {
2488   switch (cfi)
2489     {
2490     case DW_CFA_nop:
2491     case DW_CFA_GNU_window_save:
2492       return dw_cfi_oprnd_unused;
2493
2494     case DW_CFA_set_loc:
2495     case DW_CFA_advance_loc1:
2496     case DW_CFA_advance_loc2:
2497     case DW_CFA_advance_loc4:
2498     case DW_CFA_MIPS_advance_loc8:
2499       return dw_cfi_oprnd_addr;
2500
2501     case DW_CFA_offset:
2502     case DW_CFA_offset_extended:
2503     case DW_CFA_def_cfa:
2504     case DW_CFA_offset_extended_sf:
2505     case DW_CFA_def_cfa_sf:
2506     case DW_CFA_restore_extended:
2507     case DW_CFA_undefined:
2508     case DW_CFA_same_value:
2509     case DW_CFA_def_cfa_register:
2510     case DW_CFA_register:
2511       return dw_cfi_oprnd_reg_num;
2512
2513     case DW_CFA_def_cfa_offset:
2514     case DW_CFA_GNU_args_size:
2515     case DW_CFA_def_cfa_offset_sf:
2516       return dw_cfi_oprnd_offset;
2517
2518     case DW_CFA_def_cfa_expression:
2519     case DW_CFA_expression:
2520       return dw_cfi_oprnd_loc;
2521
2522     default:
2523       gcc_unreachable ();
2524     }
2525 }
2526
2527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2528 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2529  (enum dwarf_call_frame_info cfi);
2530
2531 static enum dw_cfi_oprnd_type
2532 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2533 {
2534   switch (cfi)
2535     {
2536     case DW_CFA_def_cfa:
2537     case DW_CFA_def_cfa_sf:
2538     case DW_CFA_offset:
2539     case DW_CFA_offset_extended_sf:
2540     case DW_CFA_offset_extended:
2541       return dw_cfi_oprnd_offset;
2542
2543     case DW_CFA_register:
2544       return dw_cfi_oprnd_reg_num;
2545
2546     default:
2547       return dw_cfi_oprnd_unused;
2548     }
2549 }
2550
2551 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2552
2553 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2554    switch to the data section instead, and write out a synthetic label
2555    for collect2.  */
2556
2557 static void
2558 switch_to_eh_frame_section (void)
2559 {
2560   tree label;
2561
2562 #ifdef EH_FRAME_SECTION_NAME
2563   if (eh_frame_section == 0)
2564     {
2565       int flags;
2566
2567       if (EH_TABLES_CAN_BE_READ_ONLY)
2568         {
2569           int fde_encoding;
2570           int per_encoding;
2571           int lsda_encoding;
2572
2573           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2574                                                        /*global=*/0);
2575           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2576                                                        /*global=*/1);
2577           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2578                                                         /*global=*/0);
2579           flags = ((! flag_pic
2580                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2581                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2582                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2583                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2584                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2585                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2586                    ? 0 : SECTION_WRITE);
2587         }
2588       else
2589         flags = SECTION_WRITE;
2590       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2591     }
2592 #endif
2593
2594   if (eh_frame_section)
2595     switch_to_section (eh_frame_section);
2596   else
2597     {
2598       /* We have no special eh_frame section.  Put the information in
2599          the data section and emit special labels to guide collect2.  */
2600       switch_to_section (data_section);
2601       label = get_file_function_name ("F");
2602       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2603       targetm.asm_out.globalize_label (asm_out_file,
2604                                        IDENTIFIER_POINTER (label));
2605       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2606     }
2607 }
2608
2609 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2610
2611 static HOST_WIDE_INT
2612 div_data_align (HOST_WIDE_INT off)
2613 {
2614   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2615   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2616   return r;
2617 }
2618
2619 /* Output a Call Frame Information opcode and its operand(s).  */
2620
2621 static void
2622 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2623 {
2624   unsigned long r;
2625   HOST_WIDE_INT off;
2626
2627   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2628     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2629                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2630                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2631                          ((unsigned HOST_WIDE_INT)
2632                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2633   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2634     {
2635       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2636       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2637                            "DW_CFA_offset, column 0x%lx", r);
2638       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2639       dw2_asm_output_data_uleb128 (off, NULL);
2640     }
2641   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2642     {
2643       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2644       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2645                            "DW_CFA_restore, column 0x%lx", r);
2646     }
2647   else
2648     {
2649       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2650                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2651
2652       switch (cfi->dw_cfi_opc)
2653         {
2654         case DW_CFA_set_loc:
2655           if (for_eh)
2656             dw2_asm_output_encoded_addr_rtx (
2657                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2658                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2659                 false, NULL);
2660           else
2661             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2662                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2663           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2664           break;
2665
2666         case DW_CFA_advance_loc1:
2667           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2668                                 fde->dw_fde_current_label, NULL);
2669           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2670           break;
2671
2672         case DW_CFA_advance_loc2:
2673           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2674                                 fde->dw_fde_current_label, NULL);
2675           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2676           break;
2677
2678         case DW_CFA_advance_loc4:
2679           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2680                                 fde->dw_fde_current_label, NULL);
2681           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2682           break;
2683
2684         case DW_CFA_MIPS_advance_loc8:
2685           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2686                                 fde->dw_fde_current_label, NULL);
2687           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2688           break;
2689
2690         case DW_CFA_offset_extended:
2691           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2692           dw2_asm_output_data_uleb128 (r, NULL);
2693           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2694           dw2_asm_output_data_uleb128 (off, NULL);
2695           break;
2696
2697         case DW_CFA_def_cfa:
2698           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2699           dw2_asm_output_data_uleb128 (r, NULL);
2700           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2701           break;
2702
2703         case DW_CFA_offset_extended_sf:
2704           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2705           dw2_asm_output_data_uleb128 (r, NULL);
2706           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2707           dw2_asm_output_data_sleb128 (off, NULL);
2708           break;
2709
2710         case DW_CFA_def_cfa_sf:
2711           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2712           dw2_asm_output_data_uleb128 (r, NULL);
2713           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2714           dw2_asm_output_data_sleb128 (off, NULL);
2715           break;
2716
2717         case DW_CFA_restore_extended:
2718         case DW_CFA_undefined:
2719         case DW_CFA_same_value:
2720         case DW_CFA_def_cfa_register:
2721           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2722           dw2_asm_output_data_uleb128 (r, NULL);
2723           break;
2724
2725         case DW_CFA_register:
2726           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2727           dw2_asm_output_data_uleb128 (r, NULL);
2728           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2729           dw2_asm_output_data_uleb128 (r, NULL);
2730           break;
2731
2732         case DW_CFA_def_cfa_offset:
2733         case DW_CFA_GNU_args_size:
2734           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2735           break;
2736
2737         case DW_CFA_def_cfa_offset_sf:
2738           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2739           dw2_asm_output_data_sleb128 (off, NULL);
2740           break;
2741
2742         case DW_CFA_GNU_window_save:
2743           break;
2744
2745         case DW_CFA_def_cfa_expression:
2746         case DW_CFA_expression:
2747           output_cfa_loc (cfi);
2748           break;
2749
2750         case DW_CFA_GNU_negative_offset_extended:
2751           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2752           gcc_unreachable ();
2753
2754         default:
2755           break;
2756         }
2757     }
2758 }
2759
2760 /* Similar, but do it via assembler directives instead.  */
2761
2762 static void
2763 output_cfi_directive (dw_cfi_ref cfi)
2764 {
2765   unsigned long r, r2;
2766
2767   switch (cfi->dw_cfi_opc)
2768     {
2769     case DW_CFA_advance_loc:
2770     case DW_CFA_advance_loc1:
2771     case DW_CFA_advance_loc2:
2772     case DW_CFA_advance_loc4:
2773     case DW_CFA_MIPS_advance_loc8:
2774     case DW_CFA_set_loc:
2775       /* Should only be created by add_fde_cfi in a code path not
2776          followed when emitting via directives.  The assembler is
2777          going to take care of this for us.  */
2778       gcc_unreachable ();
2779
2780     case DW_CFA_offset:
2781     case DW_CFA_offset_extended:
2782     case DW_CFA_offset_extended_sf:
2783       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2784       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2785                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2786       break;
2787
2788     case DW_CFA_restore:
2789     case DW_CFA_restore_extended:
2790       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2791       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2792       break;
2793
2794     case DW_CFA_undefined:
2795       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2796       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2797       break;
2798
2799     case DW_CFA_same_value:
2800       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2801       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2802       break;
2803
2804     case DW_CFA_def_cfa:
2805     case DW_CFA_def_cfa_sf:
2806       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2807       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2808                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2809       break;
2810
2811     case DW_CFA_def_cfa_register:
2812       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2813       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2814       break;
2815
2816     case DW_CFA_register:
2817       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2818       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
2819       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2820       break;
2821
2822     case DW_CFA_def_cfa_offset:
2823     case DW_CFA_def_cfa_offset_sf:
2824       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2825                HOST_WIDE_INT_PRINT_DEC"\n",
2826                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2827       break;
2828
2829     case DW_CFA_GNU_args_size:
2830       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2831       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2832       if (flag_debug_asm)
2833         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2834                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2835       fputc ('\n', asm_out_file);
2836       break;
2837
2838     case DW_CFA_GNU_window_save:
2839       fprintf (asm_out_file, "\t.cfi_window_save\n");
2840       break;
2841
2842     case DW_CFA_def_cfa_expression:
2843     case DW_CFA_expression:
2844       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2845       output_cfa_loc_raw (cfi);
2846       fputc ('\n', asm_out_file);
2847       break;
2848
2849     default:
2850       gcc_unreachable ();
2851     }
2852 }
2853
2854 /* Output the call frame information used to record information
2855    that relates to calculating the frame pointer, and records the
2856    location of saved registers.  */
2857
2858 static void
2859 output_call_frame_info (int for_eh)
2860 {
2861   unsigned int i;
2862   dw_fde_ref fde;
2863   dw_cfi_ref cfi;
2864   char l1[20], l2[20], section_start_label[20];
2865   bool any_lsda_needed = false;
2866   char augmentation[6];
2867   int augmentation_size;
2868   int fde_encoding = DW_EH_PE_absptr;
2869   int per_encoding = DW_EH_PE_absptr;
2870   int lsda_encoding = DW_EH_PE_absptr;
2871   int return_reg;
2872
2873   /* Don't emit a CIE if there won't be any FDEs.  */
2874   if (fde_table_in_use == 0)
2875     return;
2876
2877   /* Nothing to do if the assembler's doing it all.  */
2878   if (dwarf2out_do_cfi_asm ())
2879     return;
2880
2881   /* If we make FDEs linkonce, we may have to emit an empty label for
2882      an FDE that wouldn't otherwise be emitted.  We want to avoid
2883      having an FDE kept around when the function it refers to is
2884      discarded.  Example where this matters: a primary function
2885      template in C++ requires EH information, but an explicit
2886      specialization doesn't.  */
2887   if (TARGET_USES_WEAK_UNWIND_INFO
2888       && ! flag_asynchronous_unwind_tables
2889       && flag_exceptions
2890       && for_eh)
2891     for (i = 0; i < fde_table_in_use; i++)
2892       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2893           && !fde_table[i].uses_eh_lsda
2894           && ! DECL_WEAK (fde_table[i].decl))
2895         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2896                                       for_eh, /* empty */ 1);
2897
2898   /* If we don't have any functions we'll want to unwind out of, don't
2899      emit any EH unwind information.  Note that if exceptions aren't
2900      enabled, we won't have collected nothrow information, and if we
2901      asked for asynchronous tables, we always want this info.  */
2902   if (for_eh)
2903     {
2904       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2905
2906       for (i = 0; i < fde_table_in_use; i++)
2907         if (fde_table[i].uses_eh_lsda)
2908           any_eh_needed = any_lsda_needed = true;
2909         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2910           any_eh_needed = true;
2911         else if (! fde_table[i].nothrow
2912                  && ! fde_table[i].all_throwers_are_sibcalls)
2913           any_eh_needed = true;
2914
2915       if (! any_eh_needed)
2916         return;
2917     }
2918
2919   /* We're going to be generating comments, so turn on app.  */
2920   if (flag_debug_asm)
2921     app_enable ();
2922
2923   if (for_eh)
2924     switch_to_eh_frame_section ();
2925   else
2926     {
2927       if (!debug_frame_section)
2928         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2929                                            SECTION_DEBUG, NULL);
2930       switch_to_section (debug_frame_section);
2931     }
2932
2933   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2934   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2935
2936   /* Output the CIE.  */
2937   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2938   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2939   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2940     dw2_asm_output_data (4, 0xffffffff,
2941       "Initial length escape value indicating 64-bit DWARF extension");
2942   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2943                         "Length of Common Information Entry");
2944   ASM_OUTPUT_LABEL (asm_out_file, l1);
2945
2946   /* Now that the CIE pointer is PC-relative for EH,
2947      use 0 to identify the CIE.  */
2948   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2949                        (for_eh ? 0 : DWARF_CIE_ID),
2950                        "CIE Identifier Tag");
2951
2952   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2953
2954   augmentation[0] = 0;
2955   augmentation_size = 0;
2956   if (for_eh)
2957     {
2958       char *p;
2959
2960       /* Augmentation:
2961          z      Indicates that a uleb128 is present to size the
2962                 augmentation section.
2963          L      Indicates the encoding (and thus presence) of
2964                 an LSDA pointer in the FDE augmentation.
2965          R      Indicates a non-default pointer encoding for
2966                 FDE code pointers.
2967          P      Indicates the presence of an encoding + language
2968                 personality routine in the CIE augmentation.  */
2969
2970       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2971       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2972       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2973
2974       p = augmentation + 1;
2975       if (eh_personality_libfunc)
2976         {
2977           *p++ = 'P';
2978           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2979           assemble_external_libcall (eh_personality_libfunc);
2980         }
2981       if (any_lsda_needed)
2982         {
2983           *p++ = 'L';
2984           augmentation_size += 1;
2985         }
2986       if (fde_encoding != DW_EH_PE_absptr)
2987         {
2988           *p++ = 'R';
2989           augmentation_size += 1;
2990         }
2991       if (p > augmentation + 1)
2992         {
2993           augmentation[0] = 'z';
2994           *p = '\0';
2995         }
2996
2997       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2998       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2999         {
3000           int offset = (  4             /* Length */
3001                         + 4             /* CIE Id */
3002                         + 1             /* CIE version */
3003                         + strlen (augmentation) + 1     /* Augmentation */
3004                         + size_of_uleb128 (1)           /* Code alignment */
3005                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3006                         + 1             /* RA column */
3007                         + 1             /* Augmentation size */
3008                         + 1             /* Personality encoding */ );
3009           int pad = -offset & (PTR_SIZE - 1);
3010
3011           augmentation_size += pad;
3012
3013           /* Augmentations should be small, so there's scarce need to
3014              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3015           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3016         }
3017     }
3018
3019   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3020   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3021   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3022                                "CIE Data Alignment Factor");
3023
3024   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3025   if (DW_CIE_VERSION == 1)
3026     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3027   else
3028     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3029
3030   if (augmentation[0])
3031     {
3032       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3033       if (eh_personality_libfunc)
3034         {
3035           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3036                                eh_data_format_name (per_encoding));
3037           dw2_asm_output_encoded_addr_rtx (per_encoding,
3038                                            eh_personality_libfunc,
3039                                            true, NULL);
3040         }
3041
3042       if (any_lsda_needed)
3043         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3044                              eh_data_format_name (lsda_encoding));
3045
3046       if (fde_encoding != DW_EH_PE_absptr)
3047         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3048                              eh_data_format_name (fde_encoding));
3049     }
3050
3051   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3052     output_cfi (cfi, NULL, for_eh);
3053
3054   /* Pad the CIE out to an address sized boundary.  */
3055   ASM_OUTPUT_ALIGN (asm_out_file,
3056                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3057   ASM_OUTPUT_LABEL (asm_out_file, l2);
3058
3059   /* Loop through all of the FDE's.  */
3060   for (i = 0; i < fde_table_in_use; i++)
3061     {
3062       fde = &fde_table[i];
3063
3064       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3065       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3066           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3067           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3068           && !fde->uses_eh_lsda)
3069         continue;
3070
3071       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3072       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3073       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3074       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3075       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3076         dw2_asm_output_data (4, 0xffffffff,
3077                              "Initial length escape value indicating 64-bit DWARF extension");
3078       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3079                             "FDE Length");
3080       ASM_OUTPUT_LABEL (asm_out_file, l1);
3081
3082       if (for_eh)
3083         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3084       else
3085         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3086                                debug_frame_section, "FDE CIE offset");
3087
3088       if (for_eh)
3089         {
3090           if (fde->dw_fde_switched_sections)
3091             {
3092               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3093                                       fde->dw_fde_unlikely_section_label);
3094               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3095                                       fde->dw_fde_hot_section_label);
3096               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3097               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3098               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3099                                                "FDE initial location");
3100               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3101                                     fde->dw_fde_hot_section_end_label,
3102                                     fde->dw_fde_hot_section_label,
3103                                     "FDE address range");
3104               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3105                                                "FDE initial location");
3106               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3107                                     fde->dw_fde_unlikely_section_end_label,
3108                                     fde->dw_fde_unlikely_section_label,
3109                                     "FDE address range");
3110             }
3111           else
3112             {
3113               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3114               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3115               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3116                                                sym_ref,
3117                                                false,
3118                                                "FDE initial location");
3119               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3120                                     fde->dw_fde_end, fde->dw_fde_begin,
3121                                     "FDE address range");
3122             }
3123         }
3124       else
3125         {
3126           if (fde->dw_fde_switched_sections)
3127             {
3128               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3129                                    fde->dw_fde_hot_section_label,
3130                                    "FDE initial location");
3131               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3132                                     fde->dw_fde_hot_section_end_label,
3133                                     fde->dw_fde_hot_section_label,
3134                                     "FDE address range");
3135               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3136                                    fde->dw_fde_unlikely_section_label,
3137                                    "FDE initial location");
3138               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3139                                     fde->dw_fde_unlikely_section_end_label,
3140                                     fde->dw_fde_unlikely_section_label,
3141                                     "FDE address range");
3142             }
3143           else
3144             {
3145               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3146                                    "FDE initial location");
3147               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3148                                     fde->dw_fde_end, fde->dw_fde_begin,
3149                                     "FDE address range");
3150             }
3151         }
3152
3153       if (augmentation[0])
3154         {
3155           if (any_lsda_needed)
3156             {
3157               int size = size_of_encoded_value (lsda_encoding);
3158
3159               if (lsda_encoding == DW_EH_PE_aligned)
3160                 {
3161                   int offset = (  4             /* Length */
3162                                 + 4             /* CIE offset */
3163                                 + 2 * size_of_encoded_value (fde_encoding)
3164                                 + 1             /* Augmentation size */ );
3165                   int pad = -offset & (PTR_SIZE - 1);
3166
3167                   size += pad;
3168                   gcc_assert (size_of_uleb128 (size) == 1);
3169                 }
3170
3171               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3172
3173               if (fde->uses_eh_lsda)
3174                 {
3175                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3176                                                fde->funcdef_number);
3177                   dw2_asm_output_encoded_addr_rtx (
3178                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3179                         false, "Language Specific Data Area");
3180                 }
3181               else
3182                 {
3183                   if (lsda_encoding == DW_EH_PE_aligned)
3184                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3185                   dw2_asm_output_data
3186                     (size_of_encoded_value (lsda_encoding), 0,
3187                      "Language Specific Data Area (none)");
3188                 }
3189             }
3190           else
3191             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3192         }
3193
3194       /* Loop through the Call Frame Instructions associated with
3195          this FDE.  */
3196       fde->dw_fde_current_label = fde->dw_fde_begin;
3197       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3198         output_cfi (cfi, fde, for_eh);
3199
3200       /* Pad the FDE out to an address sized boundary.  */
3201       ASM_OUTPUT_ALIGN (asm_out_file,
3202                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3203       ASM_OUTPUT_LABEL (asm_out_file, l2);
3204     }
3205
3206   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3207     dw2_asm_output_data (4, 0, "End of Table");
3208 #ifdef MIPS_DEBUGGING_INFO
3209   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3210      get a value of 0.  Putting .align 0 after the label fixes it.  */
3211   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3212 #endif
3213
3214   /* Turn off app to make assembly quicker.  */
3215   if (flag_debug_asm)
3216     app_disable ();
3217 }
3218
3219 /* Output a marker (i.e. a label) for the beginning of a function, before
3220    the prologue.  */
3221
3222 void
3223 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3224                           const char *file ATTRIBUTE_UNUSED)
3225 {
3226   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3227   char * dup_label;
3228   dw_fde_ref fde;
3229
3230   current_function_func_begin_label = NULL;
3231
3232 #ifdef TARGET_UNWIND_INFO
3233   /* ??? current_function_func_begin_label is also used by except.c
3234      for call-site information.  We must emit this label if it might
3235      be used.  */
3236   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3237       && ! dwarf2out_do_frame ())
3238     return;
3239 #else
3240   if (! dwarf2out_do_frame ())
3241     return;
3242 #endif
3243
3244   switch_to_section (function_section (current_function_decl));
3245   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3246                                current_function_funcdef_no);
3247   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3248                           current_function_funcdef_no);
3249   dup_label = xstrdup (label);
3250   current_function_func_begin_label = dup_label;
3251
3252 #ifdef TARGET_UNWIND_INFO
3253   /* We can elide the fde allocation if we're not emitting debug info.  */
3254   if (! dwarf2out_do_frame ())
3255     return;
3256 #endif
3257
3258   /* Expand the fde table if necessary.  */
3259   if (fde_table_in_use == fde_table_allocated)
3260     {
3261       fde_table_allocated += FDE_TABLE_INCREMENT;
3262       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3263       memset (fde_table + fde_table_in_use, 0,
3264               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3265     }
3266
3267   /* Record the FDE associated with this function.  */
3268   current_funcdef_fde = fde_table_in_use;
3269
3270   /* Add the new FDE at the end of the fde_table.  */
3271   fde = &fde_table[fde_table_in_use++];
3272   fde->decl = current_function_decl;
3273   fde->dw_fde_begin = dup_label;
3274   fde->dw_fde_current_label = dup_label;
3275   fde->dw_fde_hot_section_label = NULL;
3276   fde->dw_fde_hot_section_end_label = NULL;
3277   fde->dw_fde_unlikely_section_label = NULL;
3278   fde->dw_fde_unlikely_section_end_label = NULL;
3279   fde->dw_fde_switched_sections = false;
3280   fde->dw_fde_end = NULL;
3281   fde->dw_fde_cfi = NULL;
3282   fde->funcdef_number = current_function_funcdef_no;
3283   fde->nothrow = TREE_NOTHROW (current_function_decl);
3284   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3285   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3286   fde->drap_reg = INVALID_REGNUM;
3287   fde->vdrap_reg = INVALID_REGNUM;
3288
3289   args_size = old_args_size = 0;
3290
3291   /* We only want to output line number information for the genuine dwarf2
3292      prologue case, not the eh frame case.  */
3293 #ifdef DWARF2_DEBUGGING_INFO
3294   if (file)
3295     dwarf2out_source_line (line, file);
3296 #endif
3297
3298   if (dwarf2out_do_cfi_asm ())
3299     {
3300       int enc;
3301       rtx ref;
3302
3303       fprintf (asm_out_file, "\t.cfi_startproc\n");
3304
3305       if (eh_personality_libfunc)
3306         {
3307           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3308           ref = eh_personality_libfunc;
3309
3310           /* ??? The GAS support isn't entirely consistent.  We have to
3311              handle indirect support ourselves, but PC-relative is done
3312              in the assembler.  Further, the assembler can't handle any
3313              of the weirder relocation types.  */
3314           if (enc & DW_EH_PE_indirect)
3315             ref = dw2_force_const_mem (ref, true);
3316
3317           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3318           output_addr_const (asm_out_file, ref);
3319           fputc ('\n', asm_out_file);
3320         }
3321
3322       if (crtl->uses_eh_lsda)
3323         {
3324           char lab[20];
3325
3326           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3327           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3328                                        current_function_funcdef_no);
3329           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3330           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3331
3332           if (enc & DW_EH_PE_indirect)
3333             ref = dw2_force_const_mem (ref, true);
3334
3335           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3336           output_addr_const (asm_out_file, ref);
3337           fputc ('\n', asm_out_file);
3338         }
3339     }
3340 }
3341
3342 /* Output a marker (i.e. a label) for the absolute end of the generated code
3343    for a function definition.  This gets called *after* the epilogue code has
3344    been generated.  */
3345
3346 void
3347 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3348                         const char *file ATTRIBUTE_UNUSED)
3349 {
3350   dw_fde_ref fde;
3351   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3352
3353   if (dwarf2out_do_cfi_asm ())
3354     fprintf (asm_out_file, "\t.cfi_endproc\n");
3355
3356   /* Output a label to mark the endpoint of the code generated for this
3357      function.  */
3358   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3359                                current_function_funcdef_no);
3360   ASM_OUTPUT_LABEL (asm_out_file, label);
3361   fde = current_fde ();
3362   gcc_assert (fde != NULL);
3363   fde->dw_fde_end = xstrdup (label);
3364 }
3365
3366 void
3367 dwarf2out_frame_init (void)
3368 {
3369   /* Allocate the initial hunk of the fde_table.  */
3370   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3371   fde_table_allocated = FDE_TABLE_INCREMENT;
3372   fde_table_in_use = 0;
3373
3374   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3375      sake of lookup_cfa.  */
3376
3377   /* On entry, the Canonical Frame Address is at SP.  */
3378   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3379
3380 #ifdef DWARF2_UNWIND_INFO
3381   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3382     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3383 #endif
3384 }
3385
3386 void
3387 dwarf2out_frame_finish (void)
3388 {
3389   /* Output call frame information.  */
3390   if (DWARF2_FRAME_INFO)
3391     output_call_frame_info (0);
3392
3393 #ifndef TARGET_UNWIND_INFO
3394   /* Output another copy for the unwinder.  */
3395   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3396     output_call_frame_info (1);
3397 #endif
3398 }
3399
3400 /* Note that the current function section is being used for code.  */
3401
3402 static void
3403 dwarf2out_note_section_used (void)
3404 {
3405   section *sec = current_function_section ();
3406   if (sec == text_section)
3407     text_section_used = true;
3408   else if (sec == cold_text_section)
3409     cold_text_section_used = true;
3410 }
3411
3412 void
3413 dwarf2out_switch_text_section (void)
3414 {
3415   dw_fde_ref fde = current_fde ();
3416
3417   gcc_assert (cfun && fde);
3418
3419   fde->dw_fde_switched_sections = true;
3420   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3421   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3422   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3423   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3424   have_multiple_function_sections = true;
3425
3426   /* Reset the current label on switching text sections, so that we
3427      don't attempt to advance_loc4 between labels in different sections.  */
3428   fde->dw_fde_current_label = NULL;
3429
3430   /* There is no need to mark used sections when not debugging.  */
3431   if (cold_text_section != NULL)
3432     dwarf2out_note_section_used ();
3433 }
3434 #endif
3435 \f
3436 /* And now, the subset of the debugging information support code necessary
3437    for emitting location expressions.  */
3438
3439 /* Data about a single source file.  */
3440 struct dwarf_file_data GTY(())
3441 {
3442   const char * filename;
3443   int emitted_number;
3444 };
3445
3446 /* We need some way to distinguish DW_OP_addr with a direct symbol
3447    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3448 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3449
3450
3451 typedef struct dw_val_struct *dw_val_ref;
3452 typedef struct die_struct *dw_die_ref;
3453 typedef const struct die_struct *const_dw_die_ref;
3454 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3455 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3456
3457 typedef struct deferred_locations_struct GTY(()) 
3458 {
3459   tree variable;
3460   dw_die_ref die;
3461 } deferred_locations;
3462
3463 DEF_VEC_O(deferred_locations);
3464 DEF_VEC_ALLOC_O(deferred_locations,gc);
3465
3466 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3467
3468 /* Each DIE may have a series of attribute/value pairs.  Values
3469    can take on several forms.  The forms that are used in this
3470    implementation are listed below.  */
3471
3472 enum dw_val_class
3473 {
3474   dw_val_class_addr,
3475   dw_val_class_offset,
3476   dw_val_class_loc,
3477   dw_val_class_loc_list,
3478   dw_val_class_range_list,
3479   dw_val_class_const,
3480   dw_val_class_unsigned_const,
3481   dw_val_class_long_long,
3482   dw_val_class_vec,
3483   dw_val_class_flag,
3484   dw_val_class_die_ref,
3485   dw_val_class_fde_ref,
3486   dw_val_class_lbl_id,
3487   dw_val_class_lineptr,
3488   dw_val_class_str,
3489   dw_val_class_macptr,
3490   dw_val_class_file
3491 };
3492
3493 /* Describe a double word constant value.  */
3494 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3495
3496 typedef struct dw_long_long_struct GTY(())
3497 {
3498   unsigned long hi;
3499   unsigned long low;
3500 }
3501 dw_long_long_const;
3502
3503 /* Describe a floating point constant value, or a vector constant value.  */
3504
3505 typedef struct dw_vec_struct GTY(())
3506 {
3507   unsigned char * GTY((length ("%h.length"))) array;
3508   unsigned length;
3509   unsigned elt_size;
3510 }
3511 dw_vec_const;
3512
3513 /* The dw_val_node describes an attribute's value, as it is
3514    represented internally.  */
3515
3516 typedef struct dw_val_struct GTY(())
3517 {
3518   enum dw_val_class val_class;
3519   union dw_val_struct_union
3520     {
3521       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3522       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3523       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3524       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3525       HOST_WIDE_INT GTY ((default)) val_int;
3526       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3527       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3528       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3529       struct dw_val_die_union
3530         {
3531           dw_die_ref die;
3532           int external;
3533         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3534       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3535       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3536       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3537       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3538       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3539     }
3540   GTY ((desc ("%1.val_class"))) v;
3541 }
3542 dw_val_node;
3543
3544 /* Locations in memory are described using a sequence of stack machine
3545    operations.  */
3546
3547 typedef struct dw_loc_descr_struct GTY(())
3548 {
3549   dw_loc_descr_ref dw_loc_next;
3550   enum dwarf_location_atom dw_loc_opc;
3551   int dw_loc_addr;
3552   dw_val_node dw_loc_oprnd1;
3553   dw_val_node dw_loc_oprnd2;
3554 }
3555 dw_loc_descr_node;
3556
3557 /* Location lists are ranges + location descriptions for that range,
3558    so you can track variables that are in different places over
3559    their entire life.  */
3560 typedef struct dw_loc_list_struct GTY(())
3561 {
3562   dw_loc_list_ref dw_loc_next;
3563   const char *begin; /* Label for begin address of range */
3564   const char *end;  /* Label for end address of range */
3565   char *ll_symbol; /* Label for beginning of location list.
3566                       Only on head of list */
3567   const char *section; /* Section this loclist is relative to */
3568   dw_loc_descr_ref expr;
3569 } dw_loc_list_node;
3570
3571 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3572
3573 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3574
3575 /* Convert a DWARF stack opcode into its string name.  */
3576
3577 static const char *
3578 dwarf_stack_op_name (unsigned int op)
3579 {
3580   switch (op)
3581     {
3582     case DW_OP_addr:
3583     case INTERNAL_DW_OP_tls_addr:
3584       return "DW_OP_addr";
3585     case DW_OP_deref:
3586       return "DW_OP_deref";
3587     case DW_OP_const1u:
3588       return "DW_OP_const1u";
3589     case DW_OP_const1s:
3590       return "DW_OP_const1s";
3591     case DW_OP_const2u:
3592       return "DW_OP_const2u";
3593     case DW_OP_const2s:
3594       return "DW_OP_const2s";
3595     case DW_OP_const4u:
3596       return "DW_OP_const4u";
3597     case DW_OP_const4s:
3598       return "DW_OP_const4s";
3599     case DW_OP_const8u:
3600       return "DW_OP_const8u";
3601     case DW_OP_const8s:
3602       return "DW_OP_const8s";
3603     case DW_OP_constu:
3604       return "DW_OP_constu";
3605     case DW_OP_consts:
3606       return "DW_OP_consts";
3607     case DW_OP_dup:
3608       return "DW_OP_dup";
3609     case DW_OP_drop:
3610       return "DW_OP_drop";
3611     case DW_OP_over:
3612       return "DW_OP_over";
3613     case DW_OP_pick:
3614       return "DW_OP_pick";
3615     case DW_OP_swap:
3616       return "DW_OP_swap";
3617     case DW_OP_rot:
3618       return "DW_OP_rot";
3619     case DW_OP_xderef:
3620       return "DW_OP_xderef";
3621     case DW_OP_abs:
3622       return "DW_OP_abs";
3623     case DW_OP_and:
3624       return "DW_OP_and";
3625     case DW_OP_div:
3626       return "DW_OP_div";
3627     case DW_OP_minus:
3628       return "DW_OP_minus";
3629     case DW_OP_mod:
3630       return "DW_OP_mod";
3631     case DW_OP_mul:
3632       return "DW_OP_mul";
3633     case DW_OP_neg:
3634       return "DW_OP_neg";
3635     case DW_OP_not:
3636       return "DW_OP_not";
3637     case DW_OP_or:
3638       return "DW_OP_or";
3639     case DW_OP_plus:
3640       return "DW_OP_plus";
3641     case DW_OP_plus_uconst:
3642       return "DW_OP_plus_uconst";
3643     case DW_OP_shl:
3644       return "DW_OP_shl";
3645     case DW_OP_shr:
3646       return "DW_OP_shr";
3647     case DW_OP_shra:
3648       return "DW_OP_shra";
3649     case DW_OP_xor:
3650       return "DW_OP_xor";
3651     case DW_OP_bra:
3652       return "DW_OP_bra";
3653     case DW_OP_eq:
3654       return "DW_OP_eq";
3655     case DW_OP_ge:
3656       return "DW_OP_ge";
3657     case DW_OP_gt:
3658       return "DW_OP_gt";
3659     case DW_OP_le:
3660       return "DW_OP_le";
3661     case DW_OP_lt:
3662       return "DW_OP_lt";
3663     case DW_OP_ne:
3664       return "DW_OP_ne";
3665     case DW_OP_skip:
3666       return "DW_OP_skip";
3667     case DW_OP_lit0:
3668       return "DW_OP_lit0";
3669     case DW_OP_lit1:
3670       return "DW_OP_lit1";
3671     case DW_OP_lit2:
3672       return "DW_OP_lit2";
3673     case DW_OP_lit3:
3674       return "DW_OP_lit3";
3675     case DW_OP_lit4:
3676       return "DW_OP_lit4";
3677     case DW_OP_lit5:
3678       return "DW_OP_lit5";
3679     case DW_OP_lit6:
3680       return "DW_OP_lit6";
3681     case DW_OP_lit7:
3682       return "DW_OP_lit7";
3683     case DW_OP_lit8:
3684       return "DW_OP_lit8";
3685     case DW_OP_lit9:
3686       return "DW_OP_lit9";
3687     case DW_OP_lit10:
3688       return "DW_OP_lit10";
3689     case DW_OP_lit11:
3690       return "DW_OP_lit11";
3691     case DW_OP_lit12:
3692       return "DW_OP_lit12";
3693     case DW_OP_lit13:
3694       return "DW_OP_lit13";
3695     case DW_OP_lit14:
3696       return "DW_OP_lit14";
3697     case DW_OP_lit15:
3698       return "DW_OP_lit15";
3699     case DW_OP_lit16:
3700       return "DW_OP_lit16";
3701     case DW_OP_lit17:
3702       return "DW_OP_lit17";
3703     case DW_OP_lit18:
3704       return "DW_OP_lit18";
3705     case DW_OP_lit19:
3706       return "DW_OP_lit19";
3707     case DW_OP_lit20:
3708       return "DW_OP_lit20";
3709     case DW_OP_lit21:
3710       return "DW_OP_lit21";
3711     case DW_OP_lit22:
3712       return "DW_OP_lit22";
3713     case DW_OP_lit23:
3714       return "DW_OP_lit23";
3715     case DW_OP_lit24:
3716       return "DW_OP_lit24";
3717     case DW_OP_lit25:
3718       return "DW_OP_lit25";
3719     case DW_OP_lit26:
3720       return "DW_OP_lit26";
3721     case DW_OP_lit27:
3722       return "DW_OP_lit27";
3723     case DW_OP_lit28:
3724       return "DW_OP_lit28";
3725     case DW_OP_lit29:
3726       return "DW_OP_lit29";
3727     case DW_OP_lit30:
3728       return "DW_OP_lit30";
3729     case DW_OP_lit31:
3730       return "DW_OP_lit31";
3731     case DW_OP_reg0:
3732       return "DW_OP_reg0";
3733     case DW_OP_reg1:
3734       return "DW_OP_reg1";
3735     case DW_OP_reg2:
3736       return "DW_OP_reg2";
3737     case DW_OP_reg3:
3738       return "DW_OP_reg3";
3739     case DW_OP_reg4:
3740       return "DW_OP_reg4";
3741     case DW_OP_reg5:
3742       return "DW_OP_reg5";
3743     case DW_OP_reg6:
3744       return "DW_OP_reg6";
3745     case DW_OP_reg7:
3746       return "DW_OP_reg7";
3747     case DW_OP_reg8:
3748       return "DW_OP_reg8";
3749     case DW_OP_reg9:
3750       return "DW_OP_reg9";
3751     case DW_OP_reg10:
3752       return "DW_OP_reg10";
3753     case DW_OP_reg11:
3754       return "DW_OP_reg11";
3755     case DW_OP_reg12:
3756       return "DW_OP_reg12";
3757     case DW_OP_reg13:
3758       return "DW_OP_reg13";
3759     case DW_OP_reg14:
3760       return "DW_OP_reg14";
3761     case DW_OP_reg15:
3762       return "DW_OP_reg15";
3763     case DW_OP_reg16:
3764       return "DW_OP_reg16";
3765     case DW_OP_reg17:
3766       return "DW_OP_reg17";
3767     case DW_OP_reg18:
3768       return "DW_OP_reg18";
3769     case DW_OP_reg19:
3770       return "DW_OP_reg19";
3771     case DW_OP_reg20:
3772       return "DW_OP_reg20";
3773     case DW_OP_reg21:
3774       return "DW_OP_reg21";
3775     case DW_OP_reg22:
3776       return "DW_OP_reg22";
3777     case DW_OP_reg23:
3778       return "DW_OP_reg23";
3779     case DW_OP_reg24:
3780       return "DW_OP_reg24";
3781     case DW_OP_reg25:
3782       return "DW_OP_reg25";
3783     case DW_OP_reg26:
3784       return "DW_OP_reg26";
3785     case DW_OP_reg27:
3786       return "DW_OP_reg27";
3787     case DW_OP_reg28:
3788       return "DW_OP_reg28";
3789     case DW_OP_reg29:
3790       return "DW_OP_reg29";
3791     case DW_OP_reg30:
3792       return "DW_OP_reg30";
3793     case DW_OP_reg31:
3794       return "DW_OP_reg31";
3795     case DW_OP_breg0:
3796       return "DW_OP_breg0";
3797     case DW_OP_breg1:
3798       return "DW_OP_breg1";
3799     case DW_OP_breg2:
3800       return "DW_OP_breg2";
3801     case DW_OP_breg3:
3802       return "DW_OP_breg3";
3803     case DW_OP_breg4:
3804       return "DW_OP_breg4";
3805     case DW_OP_breg5:
3806       return "DW_OP_breg5";
3807     case DW_OP_breg6:
3808       return "DW_OP_breg6";
3809     case DW_OP_breg7:
3810       return "DW_OP_breg7";
3811     case DW_OP_breg8:
3812       return "DW_OP_breg8";
3813     case DW_OP_breg9:
3814       return "DW_OP_breg9";
3815     case DW_OP_breg10:
3816       return "DW_OP_breg10";
3817     case DW_OP_breg11:
3818       return "DW_OP_breg11";
3819     case DW_OP_breg12:
3820       return "DW_OP_breg12";
3821     case DW_OP_breg13:
3822       return "DW_OP_breg13";
3823     case DW_OP_breg14:
3824       return "DW_OP_breg14";
3825     case DW_OP_breg15:
3826       return "DW_OP_breg15";
3827     case DW_OP_breg16:
3828       return "DW_OP_breg16";
3829     case DW_OP_breg17:
3830       return "DW_OP_breg17";
3831     case DW_OP_breg18:
3832       return "DW_OP_breg18";
3833     case DW_OP_breg19:
3834       return "DW_OP_breg19";
3835     case DW_OP_breg20:
3836       return "DW_OP_breg20";
3837     case DW_OP_breg21:
3838       return "DW_OP_breg21";
3839     case DW_OP_breg22:
3840       return "DW_OP_breg22";
3841     case DW_OP_breg23:
3842       return "DW_OP_breg23";
3843     case DW_OP_breg24:
3844       return "DW_OP_breg24";
3845     case DW_OP_breg25:
3846       return "DW_OP_breg25";
3847     case DW_OP_breg26:
3848       return "DW_OP_breg26";
3849     case DW_OP_breg27:
3850       return "DW_OP_breg27";
3851     case DW_OP_breg28:
3852       return "DW_OP_breg28";
3853     case DW_OP_breg29:
3854       return "DW_OP_breg29";
3855     case DW_OP_breg30:
3856       return "DW_OP_breg30";
3857     case DW_OP_breg31:
3858       return "DW_OP_breg31";
3859     case DW_OP_regx:
3860       return "DW_OP_regx";
3861     case DW_OP_fbreg:
3862       return "DW_OP_fbreg";
3863     case DW_OP_bregx:
3864       return "DW_OP_bregx";
3865     case DW_OP_piece:
3866       return "DW_OP_piece";
3867     case DW_OP_deref_size:
3868       return "DW_OP_deref_size";
3869     case DW_OP_xderef_size:
3870       return "DW_OP_xderef_size";
3871     case DW_OP_nop:
3872       return "DW_OP_nop";
3873     case DW_OP_push_object_address:
3874       return "DW_OP_push_object_address";
3875     case DW_OP_call2:
3876       return "DW_OP_call2";
3877     case DW_OP_call4:
3878       return "DW_OP_call4";
3879     case DW_OP_call_ref:
3880       return "DW_OP_call_ref";
3881     case DW_OP_GNU_push_tls_address:
3882       return "DW_OP_GNU_push_tls_address";
3883     case DW_OP_GNU_uninit:
3884       return "DW_OP_GNU_uninit";
3885     default:
3886       return "OP_<unknown>";
3887     }
3888 }
3889
3890 /* Return a pointer to a newly allocated location description.  Location
3891    descriptions are simple expression terms that can be strung
3892    together to form more complicated location (address) descriptions.  */
3893
3894 static inline dw_loc_descr_ref
3895 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3896                unsigned HOST_WIDE_INT oprnd2)
3897 {
3898   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3899
3900   descr->dw_loc_opc = op;
3901   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3902   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3903   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3904   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3905
3906   return descr;
3907 }
3908
3909 /* Return a pointer to a newly allocated location description for
3910    REG and OFFSET.  */
3911
3912 static inline dw_loc_descr_ref
3913 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3914 {
3915   if (offset)
3916     {
3917       if (reg <= 31)
3918         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3919       else
3920         return new_loc_descr (DW_OP_bregx, reg, offset);
3921     }
3922   else if (reg <= 31)
3923     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3924   else
3925    return new_loc_descr (DW_OP_regx, reg, 0);
3926 }
3927
3928 /* Add a location description term to a location description expression.  */
3929
3930 static inline void
3931 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3932 {
3933   dw_loc_descr_ref *d;
3934
3935   /* Find the end of the chain.  */
3936   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3937     ;
3938
3939   *d = descr;
3940 }
3941
3942 /* Return the size of a location descriptor.  */
3943
3944 static unsigned long
3945 size_of_loc_descr (dw_loc_descr_ref loc)
3946 {
3947   unsigned long size = 1;
3948
3949   switch (loc->dw_loc_opc)
3950     {
3951     case DW_OP_addr:
3952     case INTERNAL_DW_OP_tls_addr:
3953       size += DWARF2_ADDR_SIZE;
3954       break;
3955     case DW_OP_const1u:
3956     case DW_OP_const1s:
3957       size += 1;
3958       break;
3959     case DW_OP_const2u:
3960     case DW_OP_const2s:
3961       size += 2;
3962       break;
3963     case DW_OP_const4u:
3964     case DW_OP_const4s:
3965       size += 4;
3966       break;
3967     case DW_OP_const8u:
3968     case DW_OP_const8s:
3969       size += 8;
3970       break;
3971     case DW_OP_constu:
3972       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3973       break;
3974     case DW_OP_consts:
3975       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3976       break;
3977     case DW_OP_pick:
3978       size += 1;
3979       break;
3980     case DW_OP_plus_uconst:
3981       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3982       break;
3983     case DW_OP_skip:
3984     case DW_OP_bra:
3985       size += 2;
3986       break;
3987     case DW_OP_breg0:
3988     case DW_OP_breg1:
3989     case DW_OP_breg2:
3990     case DW_OP_breg3:
3991     case DW_OP_breg4:
3992     case DW_OP_breg5:
3993     case DW_OP_breg6:
3994     case DW_OP_breg7:
3995     case DW_OP_breg8:
3996     case DW_OP_breg9:
3997     case DW_OP_breg10:
3998     case DW_OP_breg11:
3999     case DW_OP_breg12:
4000     case DW_OP_breg13:
4001     case DW_OP_breg14:
4002     case DW_OP_breg15:
4003     case DW_OP_breg16:
4004     case DW_OP_breg17:
4005     case DW_OP_breg18:
4006     case DW_OP_breg19:
4007     case DW_OP_breg20:
4008     case DW_OP_breg21:
4009     case DW_OP_breg22:
4010     case DW_OP_breg23:
4011     case DW_OP_breg24:
4012     case DW_OP_breg25:
4013     case DW_OP_breg26:
4014     case DW_OP_breg27:
4015     case DW_OP_breg28:
4016     case DW_OP_breg29:
4017     case DW_OP_breg30:
4018     case DW_OP_breg31:
4019       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4020       break;
4021     case DW_OP_regx:
4022       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4023       break;
4024     case DW_OP_fbreg:
4025       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4026       break;
4027     case DW_OP_bregx:
4028       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4029       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4030       break;
4031     case DW_OP_piece:
4032       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4033       break;
4034     case DW_OP_deref_size:
4035     case DW_OP_xderef_size:
4036       size += 1;
4037       break;
4038     case DW_OP_call2:
4039       size += 2;
4040       break;
4041     case DW_OP_call4:
4042       size += 4;
4043       break;
4044     case DW_OP_call_ref:
4045       size += DWARF2_ADDR_SIZE;
4046       break;
4047     default:
4048       break;
4049     }
4050
4051   return size;
4052 }
4053
4054 /* Return the size of a series of location descriptors.  */
4055
4056 static unsigned long
4057 size_of_locs (dw_loc_descr_ref loc)
4058 {
4059   dw_loc_descr_ref l;
4060   unsigned long size;
4061
4062   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4063      field, to avoid writing to a PCH file.  */
4064   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4065     {
4066       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4067         break;
4068       size += size_of_loc_descr (l);
4069     }
4070   if (! l)
4071     return size;
4072
4073   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4074     {
4075       l->dw_loc_addr = size;
4076       size += size_of_loc_descr (l);
4077     }
4078
4079   return size;
4080 }
4081
4082 /* Output location description stack opcode's operands (if any).  */
4083
4084 static void
4085 output_loc_operands (dw_loc_descr_ref loc)
4086 {
4087   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4088   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4089
4090   switch (loc->dw_loc_opc)
4091     {
4092 #ifdef DWARF2_DEBUGGING_INFO
4093     case DW_OP_addr:
4094       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4095       break;
4096     case DW_OP_const2u:
4097     case DW_OP_const2s:
4098       dw2_asm_output_data (2, val1->v.val_int, NULL);
4099       break;
4100     case DW_OP_const4u:
4101     case DW_OP_const4s:
4102       dw2_asm_output_data (4, val1->v.val_int, NULL);
4103       break;
4104     case DW_OP_const8u:
4105     case DW_OP_const8s:
4106       gcc_assert (HOST_BITS_PER_LONG >= 64);
4107       dw2_asm_output_data (8, val1->v.val_int, NULL);
4108       break;
4109     case DW_OP_skip:
4110     case DW_OP_bra:
4111       {
4112         int offset;
4113
4114         gcc_assert (val1->val_class == dw_val_class_loc);
4115         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4116
4117         dw2_asm_output_data (2, offset, NULL);
4118       }
4119       break;
4120 #else
4121     case DW_OP_addr:
4122     case DW_OP_const2u:
4123     case DW_OP_const2s:
4124     case DW_OP_const4u:
4125     case DW_OP_const4s:
4126     case DW_OP_const8u:
4127     case DW_OP_const8s:
4128     case DW_OP_skip:
4129     case DW_OP_bra:
4130       /* We currently don't make any attempt to make sure these are
4131          aligned properly like we do for the main unwind info, so
4132          don't support emitting things larger than a byte if we're
4133          only doing unwinding.  */
4134       gcc_unreachable ();
4135 #endif
4136     case DW_OP_const1u:
4137     case DW_OP_const1s:
4138       dw2_asm_output_data (1, val1->v.val_int, NULL);
4139       break;
4140     case DW_OP_constu:
4141       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4142       break;
4143     case DW_OP_consts:
4144       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4145       break;
4146     case DW_OP_pick:
4147       dw2_asm_output_data (1, val1->v.val_int, NULL);
4148       break;
4149     case DW_OP_plus_uconst:
4150       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4151       break;
4152     case DW_OP_breg0:
4153     case DW_OP_breg1:
4154     case DW_OP_breg2:
4155     case DW_OP_breg3:
4156     case DW_OP_breg4:
4157     case DW_OP_breg5:
4158     case DW_OP_breg6:
4159     case DW_OP_breg7:
4160     case DW_OP_breg8:
4161     case DW_OP_breg9:
4162     case DW_OP_breg10:
4163     case DW_OP_breg11:
4164     case DW_OP_breg12:
4165     case DW_OP_breg13:
4166     case DW_OP_breg14:
4167     case DW_OP_breg15:
4168     case DW_OP_breg16:
4169     case DW_OP_breg17:
4170     case DW_OP_breg18:
4171     case DW_OP_breg19:
4172     case DW_OP_breg20:
4173     case DW_OP_breg21:
4174     case DW_OP_breg22:
4175     case DW_OP_breg23:
4176     case DW_OP_breg24:
4177     case DW_OP_breg25:
4178     case DW_OP_breg26:
4179     case DW_OP_breg27:
4180     case DW_OP_breg28:
4181     case DW_OP_breg29:
4182     case DW_OP_breg30:
4183     case DW_OP_breg31:
4184       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4185       break;
4186     case DW_OP_regx:
4187       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4188       break;
4189     case DW_OP_fbreg:
4190       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4191       break;
4192     case DW_OP_bregx:
4193       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4194       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4195       break;
4196     case DW_OP_piece:
4197       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4198       break;
4199     case DW_OP_deref_size:
4200     case DW_OP_xderef_size:
4201       dw2_asm_output_data (1, val1->v.val_int, NULL);
4202       break;
4203
4204     case INTERNAL_DW_OP_tls_addr:
4205       if (targetm.asm_out.output_dwarf_dtprel)
4206         {
4207           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4208                                                DWARF2_ADDR_SIZE,
4209                                                val1->v.val_addr);
4210           fputc ('\n', asm_out_file);
4211         }
4212       else
4213         gcc_unreachable ();
4214       break;
4215
4216     default:
4217       /* Other codes have no operands.  */
4218       break;
4219     }
4220 }
4221
4222 /* Output a sequence of location operations.  */
4223
4224 static void
4225 output_loc_sequence (dw_loc_descr_ref loc)
4226 {
4227   for (; loc != NULL; loc = loc->dw_loc_next)
4228     {
4229       /* Output the opcode.  */
4230       dw2_asm_output_data (1, loc->dw_loc_opc,
4231                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4232
4233       /* Output the operand(s) (if any).  */
4234       output_loc_operands (loc);
4235     }
4236 }
4237
4238 /* Output location description stack opcode's operands (if any).
4239    The output is single bytes on a line, suitable for .cfi_escape.  */
4240
4241 static void
4242 output_loc_operands_raw (dw_loc_descr_ref loc)
4243 {
4244   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4245   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4246
4247   switch (loc->dw_loc_opc)
4248     {
4249     case DW_OP_addr:
4250       /* We cannot output addresses in .cfi_escape, only bytes.  */
4251       gcc_unreachable ();
4252
4253     case DW_OP_const1u:
4254     case DW_OP_const1s:
4255     case DW_OP_pick:
4256     case DW_OP_deref_size:
4257     case DW_OP_xderef_size:
4258       fputc (',', asm_out_file);
4259       dw2_asm_output_data_raw (1, val1->v.val_int);
4260       break;
4261
4262     case DW_OP_const2u:
4263     case DW_OP_const2s:
4264       fputc (',', asm_out_file);
4265       dw2_asm_output_data_raw (2, val1->v.val_int);
4266       break;
4267
4268     case DW_OP_const4u:
4269     case DW_OP_const4s:
4270       fputc (',', asm_out_file);
4271       dw2_asm_output_data_raw (4, val1->v.val_int);
4272       break;
4273
4274     case DW_OP_const8u:
4275     case DW_OP_const8s:
4276       gcc_assert (HOST_BITS_PER_LONG >= 64);
4277       fputc (',', asm_out_file);
4278       dw2_asm_output_data_raw (8, val1->v.val_int);
4279       break;
4280
4281     case DW_OP_skip:
4282     case DW_OP_bra:
4283       {
4284         int offset;
4285
4286         gcc_assert (val1->val_class == dw_val_class_loc);
4287         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4288
4289         fputc (',', asm_out_file);
4290         dw2_asm_output_data_raw (2, offset);
4291       }
4292       break;
4293
4294     case DW_OP_constu:
4295     case DW_OP_plus_uconst:
4296     case DW_OP_regx:
4297     case DW_OP_piece:
4298       fputc (',', asm_out_file);
4299       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4300       break;
4301
4302     case DW_OP_consts:
4303     case DW_OP_breg0:
4304     case DW_OP_breg1:
4305     case DW_OP_breg2:
4306     case DW_OP_breg3:
4307     case DW_OP_breg4:
4308     case DW_OP_breg5:
4309     case DW_OP_breg6:
4310     case DW_OP_breg7:
4311     case DW_OP_breg8:
4312     case DW_OP_breg9:
4313     case DW_OP_breg10:
4314     case DW_OP_breg11:
4315     case DW_OP_breg12:
4316     case DW_OP_breg13:
4317     case DW_OP_breg14:
4318     case DW_OP_breg15:
4319     case DW_OP_breg16:
4320     case DW_OP_breg17:
4321     case DW_OP_breg18:
4322     case DW_OP_breg19:
4323     case DW_OP_breg20:
4324     case DW_OP_breg21:
4325     case DW_OP_breg22:
4326     case DW_OP_breg23:
4327     case DW_OP_breg24:
4328     case DW_OP_breg25:
4329     case DW_OP_breg26:
4330     case DW_OP_breg27:
4331     case DW_OP_breg28:
4332     case DW_OP_breg29:
4333     case DW_OP_breg30:
4334     case DW_OP_breg31:
4335     case DW_OP_fbreg:
4336       fputc (',', asm_out_file);
4337       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4338       break;
4339
4340     case DW_OP_bregx:
4341       fputc (',', asm_out_file);
4342       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4343       fputc (',', asm_out_file);
4344       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4345       break;
4346
4347     case INTERNAL_DW_OP_tls_addr:
4348       gcc_unreachable ();
4349
4350     default:
4351       /* Other codes have no operands.  */
4352       break;
4353     }
4354 }
4355
4356 static void
4357 output_loc_sequence_raw (dw_loc_descr_ref loc)
4358 {
4359   while (1)
4360     {
4361       /* Output the opcode.  */
4362       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4363       output_loc_operands_raw (loc);
4364
4365       if (!loc->dw_loc_next)
4366         break;
4367       loc = loc->dw_loc_next;
4368
4369       fputc (',', asm_out_file);
4370     }
4371 }
4372
4373 /* This routine will generate the correct assembly data for a location
4374    description based on a cfi entry with a complex address.  */
4375
4376 static void
4377 output_cfa_loc (dw_cfi_ref cfi)
4378 {
4379   dw_loc_descr_ref loc;
4380   unsigned long size;
4381
4382   if (cfi->dw_cfi_opc == DW_CFA_expression)
4383     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4384
4385   /* Output the size of the block.  */
4386   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4387   size = size_of_locs (loc);
4388   dw2_asm_output_data_uleb128 (size, NULL);
4389
4390   /* Now output the operations themselves.  */
4391   output_loc_sequence (loc);
4392 }
4393
4394 /* Similar, but used for .cfi_escape.  */
4395
4396 static void
4397 output_cfa_loc_raw (dw_cfi_ref cfi)
4398 {
4399   dw_loc_descr_ref loc;
4400   unsigned long size;
4401
4402   if (cfi->dw_cfi_opc == DW_CFA_expression)
4403     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4404
4405   /* Output the size of the block.  */
4406   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4407   size = size_of_locs (loc);
4408   dw2_asm_output_data_uleb128_raw (size);
4409   fputc (',', asm_out_file);
4410
4411   /* Now output the operations themselves.  */
4412   output_loc_sequence_raw (loc);
4413 }
4414
4415 /* This function builds a dwarf location descriptor sequence from a
4416    dw_cfa_location, adding the given OFFSET to the result of the
4417    expression.  */
4418
4419 static struct dw_loc_descr_struct *
4420 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4421 {
4422   struct dw_loc_descr_struct *head, *tmp;
4423
4424   offset += cfa->offset;
4425
4426   if (cfa->indirect)
4427     {
4428       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4429       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4430       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4431       add_loc_descr (&head, tmp);
4432       if (offset != 0)
4433         {
4434           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4435           add_loc_descr (&head, tmp);
4436         }
4437     }
4438   else
4439     head = new_reg_loc_descr (cfa->reg, offset);
4440
4441   return head;
4442 }
4443
4444 /* This function builds a dwarf location descriptor sequence for
4445    the address at OFFSET from the CFA when stack is aligned to
4446    ALIGNMENT byte.  */
4447
4448 static struct dw_loc_descr_struct *
4449 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4450 {
4451   struct dw_loc_descr_struct *head;
4452   unsigned int dwarf_fp
4453     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4454
4455  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4456   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4457     {
4458       head = new_reg_loc_descr (dwarf_fp, 0);
4459       add_loc_descr (&head, int_loc_descriptor (alignment));
4460       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4461
4462       add_loc_descr (&head, int_loc_descriptor (offset));
4463       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4464     }
4465   else
4466     head = new_reg_loc_descr (dwarf_fp, offset);
4467   return head;
4468 }
4469
4470 /* This function fills in aa dw_cfa_location structure from a dwarf location
4471    descriptor sequence.  */
4472
4473 static void
4474 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4475 {
4476   struct dw_loc_descr_struct *ptr;
4477   cfa->offset = 0;
4478   cfa->base_offset = 0;
4479   cfa->indirect = 0;
4480   cfa->reg = -1;
4481
4482   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4483     {
4484       enum dwarf_location_atom op = ptr->dw_loc_opc;
4485
4486       switch (op)
4487         {
4488         case DW_OP_reg0:
4489         case DW_OP_reg1:
4490         case DW_OP_reg2:
4491         case DW_OP_reg3:
4492         case DW_OP_reg4:
4493         case DW_OP_reg5:
4494         case DW_OP_reg6:
4495         case DW_OP_reg7:
4496         case DW_OP_reg8:
4497         case DW_OP_reg9:
4498         case DW_OP_reg10:
4499         case DW_OP_reg11:
4500         case DW_OP_reg12:
4501         case DW_OP_reg13:
4502         case DW_OP_reg14:
4503         case DW_OP_reg15:
4504         case DW_OP_reg16:
4505         case DW_OP_reg17:
4506         case DW_OP_reg18:
4507         case DW_OP_reg19:
4508         case DW_OP_reg20:
4509         case DW_OP_reg21:
4510         case DW_OP_reg22:
4511         case DW_OP_reg23:
4512         case DW_OP_reg24:
4513         case DW_OP_reg25:
4514         case DW_OP_reg26:
4515         case DW_OP_reg27:
4516         case DW_OP_reg28:
4517         case DW_OP_reg29:
4518         case DW_OP_reg30:
4519         case DW_OP_reg31:
4520           cfa->reg = op - DW_OP_reg0;
4521           break;
4522         case DW_OP_regx:
4523           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4524           break;
4525         case DW_OP_breg0:
4526         case DW_OP_breg1:
4527         case DW_OP_breg2:
4528         case DW_OP_breg3:
4529         case DW_OP_breg4:
4530         case DW_OP_breg5:
4531         case DW_OP_breg6:
4532         case DW_OP_breg7:
4533         case DW_OP_breg8:
4534         case DW_OP_breg9:
4535         case DW_OP_breg10:
4536         case DW_OP_breg11:
4537         case DW_OP_breg12:
4538         case DW_OP_breg13:
4539         case DW_OP_breg14:
4540         case DW_OP_breg15:
4541         case DW_OP_breg16:
4542         case DW_OP_breg17:
4543         case DW_OP_breg18:
4544         case DW_OP_breg19:
4545         case DW_OP_breg20:
4546         case DW_OP_breg21:
4547         case DW_OP_breg22:
4548         case DW_OP_breg23:
4549         case DW_OP_breg24:
4550         case DW_OP_breg25:
4551         case DW_OP_breg26:
4552         case DW_OP_breg27:
4553         case DW_OP_breg28:
4554         case DW_OP_breg29:
4555         case DW_OP_breg30:
4556         case DW_OP_breg31:
4557           cfa->reg = op - DW_OP_breg0;
4558           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4559           break;
4560         case DW_OP_bregx:
4561           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4562           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4563           break;
4564         case DW_OP_deref:
4565           cfa->indirect = 1;
4566           break;
4567         case DW_OP_plus_uconst:
4568           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4569           break;
4570         default:
4571           internal_error ("DW_LOC_OP %s not implemented",
4572                           dwarf_stack_op_name (ptr->dw_loc_opc));
4573         }
4574     }
4575 }
4576 #endif /* .debug_frame support */
4577 \f
4578 /* And now, the support for symbolic debugging information.  */
4579 #ifdef DWARF2_DEBUGGING_INFO
4580
4581 /* .debug_str support.  */
4582 static int output_indirect_string (void **, void *);
4583
4584 static void dwarf2out_init (const char *);
4585 static void dwarf2out_finish (const char *);
4586 static void dwarf2out_define (unsigned int, const char *);
4587 static void dwarf2out_undef (unsigned int, const char *);
4588 static void dwarf2out_start_source_file (unsigned, const char *);
4589 static void dwarf2out_end_source_file (unsigned);
4590 static void dwarf2out_begin_block (unsigned, unsigned);
4591 static void dwarf2out_end_block (unsigned, unsigned);
4592 static bool dwarf2out_ignore_block (const_tree);
4593 static void dwarf2out_global_decl (tree);
4594 static void dwarf2out_type_decl (tree, int);
4595 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4596 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4597                                                  dw_die_ref);
4598 static void dwarf2out_abstract_function (tree);
4599 static void dwarf2out_var_location (rtx);
4600 static void dwarf2out_begin_function (tree);
4601
4602 /* The debug hooks structure.  */
4603
4604 const struct gcc_debug_hooks dwarf2_debug_hooks =
4605 {
4606   dwarf2out_init,
4607   dwarf2out_finish,
4608   dwarf2out_define,
4609   dwarf2out_undef,
4610   dwarf2out_start_source_file,
4611   dwarf2out_end_source_file,
4612   dwarf2out_begin_block,
4613   dwarf2out_end_block,
4614   dwarf2out_ignore_block,
4615   dwarf2out_source_line,
4616   dwarf2out_begin_prologue,
4617   debug_nothing_int_charstar,   /* end_prologue */
4618   dwarf2out_end_epilogue,
4619   dwarf2out_begin_function,
4620   debug_nothing_int,            /* end_function */
4621   dwarf2out_decl,               /* function_decl */
4622   dwarf2out_global_decl,
4623   dwarf2out_type_decl,          /* type_decl */
4624   dwarf2out_imported_module_or_decl,
4625   debug_nothing_tree,           /* deferred_inline_function */
4626   /* The DWARF 2 backend tries to reduce debugging bloat by not
4627      emitting the abstract description of inline functions until
4628      something tries to reference them.  */
4629   dwarf2out_abstract_function,  /* outlining_inline_function */
4630   debug_nothing_rtx,            /* label */
4631   debug_nothing_int,            /* handle_pch */
4632   dwarf2out_var_location,
4633   dwarf2out_switch_text_section,
4634   1                             /* start_end_main_source_file */
4635 };
4636 #endif
4637 \f
4638 /* NOTE: In the comments in this file, many references are made to
4639    "Debugging Information Entries".  This term is abbreviated as `DIE'
4640    throughout the remainder of this file.  */
4641
4642 /* An internal representation of the DWARF output is built, and then
4643    walked to generate the DWARF debugging info.  The walk of the internal
4644    representation is done after the entire program has been compiled.
4645    The types below are used to describe the internal representation.  */
4646
4647 /* Various DIE's use offsets relative to the beginning of the
4648    .debug_info section to refer to each other.  */
4649
4650 typedef long int dw_offset;
4651
4652 /* Define typedefs here to avoid circular dependencies.  */
4653
4654 typedef struct dw_attr_struct *dw_attr_ref;
4655 typedef struct dw_line_info_struct *dw_line_info_ref;
4656 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4657 typedef struct pubname_struct *pubname_ref;
4658 typedef struct dw_ranges_struct *dw_ranges_ref;
4659 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4660
4661 /* Each entry in the line_info_table maintains the file and
4662    line number associated with the label generated for that
4663    entry.  The label gives the PC value associated with
4664    the line number entry.  */
4665
4666 typedef struct dw_line_info_struct GTY(())
4667 {
4668   unsigned long dw_file_num;
4669   unsigned long dw_line_num;
4670 }
4671 dw_line_info_entry;
4672
4673 /* Line information for functions in separate sections; each one gets its
4674    own sequence.  */
4675 typedef struct dw_separate_line_info_struct GTY(())
4676 {
4677   unsigned long dw_file_num;
4678   unsigned long dw_line_num;
4679   unsigned long function;
4680 }
4681 dw_separate_line_info_entry;
4682
4683 /* Each DIE attribute has a field specifying the attribute kind,
4684    a link to the next attribute in the chain, and an attribute value.
4685    Attributes are typically linked below the DIE they modify.  */
4686
4687 typedef struct dw_attr_struct GTY(())
4688 {
4689   enum dwarf_attribute dw_attr;
4690   dw_val_node dw_attr_val;
4691 }
4692 dw_attr_node;
4693
4694 DEF_VEC_O(dw_attr_node);
4695 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4696
4697 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4698    The children of each node form a circular list linked by
4699    die_sib.  die_child points to the node *before* the "first" child node.  */
4700
4701 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4702 {
4703   enum dwarf_tag die_tag;
4704   char *die_symbol;
4705   VEC(dw_attr_node,gc) * die_attr;
4706   dw_die_ref die_parent;
4707   dw_die_ref die_child;
4708   dw_die_ref die_sib;
4709   dw_die_ref die_definition; /* ref from a specification to its definition */
4710   dw_offset die_offset;
4711   unsigned long die_abbrev;
4712   int die_mark;
4713   /* Die is used and must not be pruned as unused.  */
4714   int die_perennial_p;
4715   unsigned int decl_id;
4716 }
4717 die_node;
4718
4719 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4720 #define FOR_EACH_CHILD(die, c, expr) do {       \
4721   c = die->die_child;                           \
4722   if (c) do {                                   \
4723     c = c->die_sib;                             \
4724     expr;                                       \
4725   } while (c != die->die_child);                \
4726 } while (0)
4727
4728 /* The pubname structure */
4729
4730 typedef struct pubname_struct GTY(())
4731 {
4732   dw_die_ref die;
4733   const char *name;
4734 }
4735 pubname_entry;
4736
4737 DEF_VEC_O(pubname_entry);
4738 DEF_VEC_ALLOC_O(pubname_entry, gc);
4739
4740 struct dw_ranges_struct GTY(())
4741 {
4742   /* If this is positive, it's a block number, otherwise it's a
4743      bitwise-negated index into dw_ranges_by_label.  */
4744   int num;
4745 };
4746
4747 struct dw_ranges_by_label_struct GTY(())
4748 {
4749   const char *begin;
4750   const char *end;
4751 };
4752
4753 /* The limbo die list structure.  */
4754 typedef struct limbo_die_struct GTY(())
4755 {
4756   dw_die_ref die;
4757   tree created_for;
4758   struct limbo_die_struct *next;
4759 }
4760 limbo_die_node;
4761
4762 /* How to start an assembler comment.  */
4763 #ifndef ASM_COMMENT_START
4764 #define ASM_COMMENT_START ";#"
4765 #endif
4766
4767 /* Define a macro which returns nonzero for a TYPE_DECL which was
4768    implicitly generated for a tagged type.
4769
4770    Note that unlike the gcc front end (which generates a NULL named
4771    TYPE_DECL node for each complete tagged type, each array type, and
4772    each function type node created) the g++ front end generates a
4773    _named_ TYPE_DECL node for each tagged type node created.
4774    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4775    generate a DW_TAG_typedef DIE for them.  */
4776
4777 #define TYPE_DECL_IS_STUB(decl)                         \
4778   (DECL_NAME (decl) == NULL_TREE                        \
4779    || (DECL_ARTIFICIAL (decl)                           \
4780        && is_tagged_type (TREE_TYPE (decl))             \
4781        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4782            /* This is necessary for stub decls that     \
4783               appear in nested inline functions.  */    \
4784            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4785                && (decl_ultimate_origin (decl)          \
4786                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4787
4788 /* Information concerning the compilation unit's programming
4789    language, and compiler version.  */
4790
4791 /* Fixed size portion of the DWARF compilation unit header.  */
4792 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4793   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4794
4795 /* Fixed size portion of public names info.  */
4796 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4797
4798 /* Fixed size portion of the address range info.  */
4799 #define DWARF_ARANGES_HEADER_SIZE                                       \
4800   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4801                 DWARF2_ADDR_SIZE * 2)                                   \
4802    - DWARF_INITIAL_LENGTH_SIZE)
4803
4804 /* Size of padding portion in the address range info.  It must be
4805    aligned to twice the pointer size.  */
4806 #define DWARF_ARANGES_PAD_SIZE \
4807   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4808                 DWARF2_ADDR_SIZE * 2)                              \
4809    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4810
4811 /* Use assembler line directives if available.  */
4812 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4813 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4814 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4815 #else
4816 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4817 #endif
4818 #endif
4819
4820 /* Minimum line offset in a special line info. opcode.
4821    This value was chosen to give a reasonable range of values.  */
4822 #define DWARF_LINE_BASE  -10
4823
4824 /* First special line opcode - leave room for the standard opcodes.  */
4825 #define DWARF_LINE_OPCODE_BASE  10
4826
4827 /* Range of line offsets in a special line info. opcode.  */
4828 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4829
4830 /* Flag that indicates the initial value of the is_stmt_start flag.
4831    In the present implementation, we do not mark any lines as
4832    the beginning of a source statement, because that information
4833    is not made available by the GCC front-end.  */
4834 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4835
4836 #ifdef DWARF2_DEBUGGING_INFO
4837 /* This location is used by calc_die_sizes() to keep track
4838    the offset of each DIE within the .debug_info section.  */
4839 static unsigned long next_die_offset;
4840 #endif
4841
4842 /* Record the root of the DIE's built for the current compilation unit.  */
4843 static GTY(()) dw_die_ref comp_unit_die;
4844
4845 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4846 static GTY(()) limbo_die_node *limbo_die_list;
4847
4848 /* Filenames referenced by this compilation unit.  */
4849 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4850
4851 /* A hash table of references to DIE's that describe declarations.
4852    The key is a DECL_UID() which is a unique number identifying each decl.  */
4853 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4854
4855 /* A hash table of references to DIE's that describe COMMON blocks.
4856    The key is DECL_UID() ^ die_parent.  */
4857 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4858
4859 /* Node of the variable location list.  */
4860 struct var_loc_node GTY ((chain_next ("%h.next")))
4861 {
4862   rtx GTY (()) var_loc_note;
4863   const char * GTY (()) label;
4864   const char * GTY (()) section_label;
4865   struct var_loc_node * GTY (()) next;
4866 };
4867
4868 /* Variable location list.  */
4869 struct var_loc_list_def GTY (())
4870 {
4871   struct var_loc_node * GTY (()) first;
4872
4873   /* Do not mark the last element of the chained list because
4874      it is marked through the chain.  */
4875   struct var_loc_node * GTY ((skip ("%h"))) last;
4876
4877   /* DECL_UID of the variable decl.  */
4878   unsigned int decl_id;
4879 };
4880 typedef struct var_loc_list_def var_loc_list;
4881
4882
4883 /* Table of decl location linked lists.  */
4884 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4885
4886 /* A pointer to the base of a list of references to DIE's that
4887    are uniquely identified by their tag, presence/absence of
4888    children DIE's, and list of attribute/value pairs.  */
4889 static GTY((length ("abbrev_die_table_allocated")))
4890   dw_die_ref *abbrev_die_table;
4891
4892 /* Number of elements currently allocated for abbrev_die_table.  */
4893 static GTY(()) unsigned abbrev_die_table_allocated;
4894
4895 /* Number of elements in type_die_table currently in use.  */
4896 static GTY(()) unsigned abbrev_die_table_in_use;
4897
4898 /* Size (in elements) of increments by which we may expand the
4899    abbrev_die_table.  */
4900 #define ABBREV_DIE_TABLE_INCREMENT 256
4901
4902 /* A pointer to the base of a table that contains line information
4903    for each source code line in .text in the compilation unit.  */
4904 static GTY((length ("line_info_table_allocated")))
4905      dw_line_info_ref line_info_table;
4906
4907 /* Number of elements currently allocated for line_info_table.  */
4908 static GTY(()) unsigned line_info_table_allocated;
4909
4910 /* Number of elements in line_info_table currently in use.  */
4911 static GTY(()) unsigned line_info_table_in_use;
4912
4913 /* A pointer to the base of a table that contains line information
4914    for each source code line outside of .text in the compilation unit.  */
4915 static GTY ((length ("separate_line_info_table_allocated")))
4916      dw_separate_line_info_ref separate_line_info_table;
4917
4918 /* Number of elements currently allocated for separate_line_info_table.  */
4919 static GTY(()) unsigned separate_line_info_table_allocated;
4920
4921 /* Number of elements in separate_line_info_table currently in use.  */
4922 static GTY(()) unsigned separate_line_info_table_in_use;
4923
4924 /* Size (in elements) of increments by which we may expand the
4925    line_info_table.  */
4926 #define LINE_INFO_TABLE_INCREMENT 1024
4927
4928 /* A pointer to the base of a table that contains a list of publicly
4929    accessible names.  */
4930 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4931
4932 /* A pointer to the base of a table that contains a list of publicly
4933    accessible types.  */
4934 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4935
4936 /* Array of dies for which we should generate .debug_arange info.  */
4937 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4938
4939 /* Number of elements currently allocated for arange_table.  */
4940 static GTY(()) unsigned arange_table_allocated;
4941
4942 /* Number of elements in arange_table currently in use.  */
4943 static GTY(()) unsigned arange_table_in_use;
4944
4945 /* Size (in elements) of increments by which we may expand the
4946    arange_table.  */
4947 #define ARANGE_TABLE_INCREMENT 64
4948
4949 /* Array of dies for which we should generate .debug_ranges info.  */
4950 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4951
4952 /* Number of elements currently allocated for ranges_table.  */
4953 static GTY(()) unsigned ranges_table_allocated;
4954
4955 /* Number of elements in ranges_table currently in use.  */
4956 static GTY(()) unsigned ranges_table_in_use;
4957
4958 /* Array of pairs of labels referenced in ranges_table.  */
4959 static GTY ((length ("ranges_by_label_allocated")))
4960      dw_ranges_by_label_ref ranges_by_label;
4961
4962 /* Number of elements currently allocated for ranges_by_label.  */
4963 static GTY(()) unsigned ranges_by_label_allocated;
4964
4965 /* Number of elements in ranges_by_label currently in use.  */
4966 static GTY(()) unsigned ranges_by_label_in_use;
4967
4968 /* Size (in elements) of increments by which we may expand the
4969    ranges_table.  */
4970 #define RANGES_TABLE_INCREMENT 64
4971
4972 /* Whether we have location lists that need outputting */
4973 static GTY(()) bool have_location_lists;
4974
4975 /* Unique label counter.  */
4976 static GTY(()) unsigned int loclabel_num;
4977
4978 #ifdef DWARF2_DEBUGGING_INFO
4979 /* Record whether the function being analyzed contains inlined functions.  */
4980 static int current_function_has_inlines;
4981 #endif
4982 #if 0 && defined (MIPS_DEBUGGING_INFO)
4983 static int comp_unit_has_inlines;
4984 #endif
4985
4986 /* The last file entry emitted by maybe_emit_file().  */
4987 static GTY(()) struct dwarf_file_data * last_emitted_file;
4988
4989 /* Number of internal labels generated by gen_internal_sym().  */
4990 static GTY(()) int label_num;
4991
4992 /* Cached result of previous call to lookup_filename.  */
4993 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4994
4995 #ifdef DWARF2_DEBUGGING_INFO
4996
4997 /* Offset from the "steady-state frame pointer" to the frame base,
4998    within the current function.  */
4999 static HOST_WIDE_INT frame_pointer_fb_offset;
5000
5001 /* Forward declarations for functions defined in this file.  */
5002
5003 static int is_pseudo_reg (const_rtx);
5004 static tree type_main_variant (tree);
5005 static int is_tagged_type (const_tree);
5006 static const char *dwarf_tag_name (unsigned);
5007 static const char *dwarf_attr_name (unsigned);
5008 static const char *dwarf_form_name (unsigned);
5009 static tree decl_ultimate_origin (const_tree);
5010 static tree decl_class_context (tree);
5011 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5012 static inline enum dw_val_class AT_class (dw_attr_ref);
5013 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5014 static inline unsigned AT_flag (dw_attr_ref);
5015 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5016 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5017 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5018 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5019 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5020                               unsigned long);
5021 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5022                                unsigned int, unsigned char *);
5023 static hashval_t debug_str_do_hash (const void *);
5024 static int debug_str_eq (const void *, const void *);
5025 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5026 static inline const char *AT_string (dw_attr_ref);
5027 static int AT_string_form (dw_attr_ref);
5028 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5029 static void add_AT_specification (dw_die_ref, dw_die_ref);
5030 static inline dw_die_ref AT_ref (dw_attr_ref);
5031 static inline int AT_ref_external (dw_attr_ref);
5032 static inline void set_AT_ref_external (dw_attr_ref, int);
5033 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5034 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5035 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5036 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5037                              dw_loc_list_ref);
5038 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5039 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5040 static inline rtx AT_addr (dw_attr_ref);
5041 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5042 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5043 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5044 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5045                            unsigned HOST_WIDE_INT);
5046 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5047                                unsigned long);
5048 static inline const char *AT_lbl (dw_attr_ref);
5049 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5050 static const char *get_AT_low_pc (dw_die_ref);
5051 static const char *get_AT_hi_pc (dw_die_ref);
5052 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5053 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5054 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5055 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5056 static bool is_c_family (void);
5057 static bool is_cxx (void);
5058 static bool is_java (void);
5059 static bool is_fortran (void);
5060 static bool is_ada (void);
5061 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5062 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5063 static void add_child_die (dw_die_ref, dw_die_ref);
5064 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5065 static dw_die_ref lookup_type_die (tree);
5066 static void equate_type_number_to_die (tree, dw_die_ref);
5067 static hashval_t decl_die_table_hash (const void *);
5068 static int decl_die_table_eq (const void *, const void *);
5069 static dw_die_ref lookup_decl_die (tree);
5070 static hashval_t common_block_die_table_hash (const void *);
5071 static int common_block_die_table_eq (const void *, const void *);
5072 static hashval_t decl_loc_table_hash (const void *);
5073 static int decl_loc_table_eq (const void *, const void *);
5074 static var_loc_list *lookup_decl_loc (const_tree);
5075 static void equate_decl_number_to_die (tree, dw_die_ref);
5076 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5077 static void print_spaces (FILE *);
5078 static void print_die (dw_die_ref, FILE *);
5079 static void print_dwarf_line_table (FILE *);
5080 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5081 static dw_die_ref pop_compile_unit (dw_die_ref);
5082 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5083 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5084 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5085 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5086 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5087 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5088 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5089 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5090 static void compute_section_prefix (dw_die_ref);
5091 static int is_type_die (dw_die_ref);
5092 static int is_comdat_die (dw_die_ref);
5093 static int is_symbol_die (dw_die_ref);
5094 static void assign_symbol_names (dw_die_ref);
5095 static void break_out_includes (dw_die_ref);
5096 static hashval_t htab_cu_hash (const void *);
5097 static int htab_cu_eq (const void *, const void *);
5098 static void htab_cu_del (void *);
5099 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5100 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5101 static void add_sibling_attributes (dw_die_ref);
5102 static void build_abbrev_table (dw_die_ref);
5103 static void output_location_lists (dw_die_ref);
5104 static int constant_size (unsigned HOST_WIDE_INT);
5105 static unsigned long size_of_die (dw_die_ref);
5106 static void calc_die_sizes (dw_die_ref);
5107 static void mark_dies (dw_die_ref);
5108 static void unmark_dies (dw_die_ref);
5109 static void unmark_all_dies (dw_die_ref);
5110 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5111 static unsigned long size_of_aranges (void);
5112 static enum dwarf_form value_format (dw_attr_ref);
5113 static void output_value_format (dw_attr_ref);
5114 static void output_abbrev_section (void);
5115 static void output_die_symbol (dw_die_ref);
5116 static void output_die (dw_die_ref);
5117 static void output_compilation_unit_header (void);
5118 static void output_comp_unit (dw_die_ref, int);
5119 static const char *dwarf2_name (tree, int);
5120 static void add_pubname (tree, dw_die_ref);
5121 static void add_pubname_string (const char *, dw_die_ref);
5122 static void add_pubtype (tree, dw_die_ref);
5123 static void output_pubnames (VEC (pubname_entry,gc) *);
5124 static void add_arange (tree, dw_die_ref);
5125 static void output_aranges (void);
5126 static unsigned int add_ranges_num (int);
5127 static unsigned int add_ranges (const_tree);
5128 static unsigned int add_ranges_by_labels (const char *, const char *);
5129 static void output_ranges (void);
5130 static void output_line_info (void);
5131 static void output_file_names (void);
5132 static dw_die_ref base_type_die (tree);
5133 static int is_base_type (tree);
5134 static bool is_subrange_type (const_tree);
5135 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5136 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5137 static int type_is_enum (const_tree);
5138 static unsigned int dbx_reg_number (const_rtx);
5139 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5140 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5141 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5142                                                 enum var_init_status);
5143 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5144                                                      enum var_init_status);
5145 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5146                                          enum var_init_status);
5147 static int is_based_loc (const_rtx);
5148 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5149                                             enum var_init_status);
5150 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5151                                                enum var_init_status);
5152 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5153 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5154 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5155 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5156 static tree field_type (const_tree);
5157 static unsigned int simple_type_align_in_bits (const_tree);
5158 static unsigned int simple_decl_align_in_bits (const_tree);
5159 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5160 static HOST_WIDE_INT field_byte_offset (const_tree);
5161 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5162                                          dw_loc_descr_ref);
5163 static void add_data_member_location_attribute (dw_die_ref, tree);
5164 static void add_const_value_attribute (dw_die_ref, rtx);
5165 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5166 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5167 static void insert_float (const_rtx, unsigned char *);
5168 static rtx rtl_for_decl_location (tree);
5169 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5170                                                    enum dwarf_attribute);
5171 static void tree_add_const_value_attribute (dw_die_ref, tree);
5172 static void add_name_attribute (dw_die_ref, const char *);
5173 static void add_comp_dir_attribute (dw_die_ref);
5174 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5175 static void add_subscript_info (dw_die_ref, tree, bool);
5176 static void add_byte_size_attribute (dw_die_ref, tree);
5177 static void add_bit_offset_attribute (dw_die_ref, tree);
5178 static void add_bit_size_attribute (dw_die_ref, tree);
5179 static void add_prototyped_attribute (dw_die_ref, tree);
5180 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5181 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5182 static void add_src_coords_attributes (dw_die_ref, tree);
5183 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5184 static void push_decl_scope (tree);
5185 static void pop_decl_scope (void);
5186 static dw_die_ref scope_die_for (tree, dw_die_ref);
5187 static inline int local_scope_p (dw_die_ref);
5188 static inline int class_or_namespace_scope_p (dw_die_ref);
5189 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5190 static void add_calling_convention_attribute (dw_die_ref, tree);
5191 static const char *type_tag (const_tree);
5192 static tree member_declared_type (const_tree);
5193 #if 0
5194 static const char *decl_start_label (tree);
5195 #endif
5196 static void gen_array_type_die (tree, dw_die_ref);
5197 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5198 #if 0
5199 static void gen_entry_point_die (tree, dw_die_ref);
5200 #endif
5201 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5202 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5203 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5204 static void gen_formal_types_die (tree, dw_die_ref);
5205 static void gen_subprogram_die (tree, dw_die_ref);
5206 static void gen_variable_die (tree, tree, dw_die_ref);
5207 static void gen_const_die (tree, dw_die_ref);
5208 static void gen_label_die (tree, dw_die_ref);
5209 static void gen_lexical_block_die (tree, dw_die_ref, int);
5210 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5211 static void gen_field_die (tree, dw_die_ref);
5212 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5213 static dw_die_ref gen_compile_unit_die (const char *);
5214 static void gen_inheritance_die (tree, tree, dw_die_ref);
5215 static void gen_member_die (tree, dw_die_ref);
5216 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5217                                                 enum debug_info_usage);
5218 static void gen_subroutine_type_die (tree, dw_die_ref);
5219 static void gen_typedef_die (tree, dw_die_ref);
5220 static void gen_type_die (tree, dw_die_ref);
5221 static void gen_block_die (tree, dw_die_ref, int);
5222 static void decls_for_scope (tree, dw_die_ref, int);
5223 static int is_redundant_typedef (const_tree);
5224 static void gen_namespace_die (tree, dw_die_ref);
5225 static void gen_decl_die (tree, tree, dw_die_ref);
5226 static dw_die_ref force_decl_die (tree);
5227 static dw_die_ref force_type_die (tree);
5228 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5229 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5230 static struct dwarf_file_data * lookup_filename (const char *);
5231 static void retry_incomplete_types (void);
5232 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5233 static void splice_child_die (dw_die_ref, dw_die_ref);
5234 static int file_info_cmp (const void *, const void *);
5235 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5236                                      const char *, const char *, unsigned);
5237 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5238                                        const char *, const char *,
5239                                        const char *);
5240 static void output_loc_list (dw_loc_list_ref);
5241 static char *gen_internal_sym (const char *);
5242
5243 static void prune_unmark_dies (dw_die_ref);
5244 static void prune_unused_types_mark (dw_die_ref, int);
5245 static void prune_unused_types_walk (dw_die_ref);
5246 static void prune_unused_types_walk_attribs (dw_die_ref);
5247 static void prune_unused_types_prune (dw_die_ref);
5248 static void prune_unused_types (void);
5249 static int maybe_emit_file (struct dwarf_file_data *fd);
5250
5251 /* Section names used to hold DWARF debugging information.  */
5252 #ifndef DEBUG_INFO_SECTION
5253 #define DEBUG_INFO_SECTION      ".debug_info"
5254 #endif
5255 #ifndef DEBUG_ABBREV_SECTION
5256 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5257 #endif
5258 #ifndef DEBUG_ARANGES_SECTION
5259 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5260 #endif
5261 #ifndef DEBUG_MACINFO_SECTION
5262 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5263 #endif
5264 #ifndef DEBUG_LINE_SECTION
5265 #define DEBUG_LINE_SECTION      ".debug_line"
5266 #endif
5267 #ifndef DEBUG_LOC_SECTION
5268 #define DEBUG_LOC_SECTION       ".debug_loc"
5269 #endif
5270 #ifndef DEBUG_PUBNAMES_SECTION
5271 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5272 #endif
5273 #ifndef DEBUG_STR_SECTION
5274 #define DEBUG_STR_SECTION       ".debug_str"
5275 #endif
5276 #ifndef DEBUG_RANGES_SECTION
5277 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5278 #endif
5279
5280 /* Standard ELF section names for compiled code and data.  */
5281 #ifndef TEXT_SECTION_NAME
5282 #define TEXT_SECTION_NAME       ".text"
5283 #endif
5284
5285 /* Section flags for .debug_str section.  */
5286 #define DEBUG_STR_SECTION_FLAGS \
5287   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5288    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5289    : SECTION_DEBUG)
5290
5291 /* Labels we insert at beginning sections we can reference instead of
5292    the section names themselves.  */
5293
5294 #ifndef TEXT_SECTION_LABEL
5295 #define TEXT_SECTION_LABEL              "Ltext"
5296 #endif
5297 #ifndef COLD_TEXT_SECTION_LABEL
5298 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5299 #endif
5300 #ifndef DEBUG_LINE_SECTION_LABEL
5301 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5302 #endif
5303 #ifndef DEBUG_INFO_SECTION_LABEL
5304 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5305 #endif
5306 #ifndef DEBUG_ABBREV_SECTION_LABEL
5307 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5308 #endif
5309 #ifndef DEBUG_LOC_SECTION_LABEL
5310 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5311 #endif
5312 #ifndef DEBUG_RANGES_SECTION_LABEL
5313 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5314 #endif
5315 #ifndef DEBUG_MACINFO_SECTION_LABEL
5316 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5317 #endif
5318
5319 /* Definitions of defaults for formats and names of various special
5320    (artificial) labels which may be generated within this file (when the -g
5321    options is used and DWARF2_DEBUGGING_INFO is in effect.
5322    If necessary, these may be overridden from within the tm.h file, but
5323    typically, overriding these defaults is unnecessary.  */
5324
5325 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5326 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5327 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5328 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5329 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5330 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5331 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5332 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5333 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5334 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5335
5336 #ifndef TEXT_END_LABEL
5337 #define TEXT_END_LABEL          "Letext"
5338 #endif
5339 #ifndef COLD_END_LABEL
5340 #define COLD_END_LABEL          "Letext_cold"
5341 #endif
5342 #ifndef BLOCK_BEGIN_LABEL
5343 #define BLOCK_BEGIN_LABEL       "LBB"
5344 #endif
5345 #ifndef BLOCK_END_LABEL
5346 #define BLOCK_END_LABEL         "LBE"
5347 #endif
5348 #ifndef LINE_CODE_LABEL
5349 #define LINE_CODE_LABEL         "LM"
5350 #endif
5351 #ifndef SEPARATE_LINE_CODE_LABEL
5352 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5353 #endif
5354
5355 \f
5356 /* We allow a language front-end to designate a function that is to be
5357    called to "demangle" any name before it is put into a DIE.  */
5358
5359 static const char *(*demangle_name_func) (const char *);
5360
5361 void
5362 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5363 {
5364   demangle_name_func = func;
5365 }
5366
5367 /* Test if rtl node points to a pseudo register.  */
5368
5369 static inline int
5370 is_pseudo_reg (const_rtx rtl)
5371 {
5372   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5373           || (GET_CODE (rtl) == SUBREG
5374               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5375 }
5376
5377 /* Return a reference to a type, with its const and volatile qualifiers
5378    removed.  */
5379
5380 static inline tree
5381 type_main_variant (tree type)
5382 {
5383   type = TYPE_MAIN_VARIANT (type);
5384
5385   /* ??? There really should be only one main variant among any group of
5386      variants of a given type (and all of the MAIN_VARIANT values for all
5387      members of the group should point to that one type) but sometimes the C
5388      front-end messes this up for array types, so we work around that bug
5389      here.  */
5390   if (TREE_CODE (type) == ARRAY_TYPE)
5391     while (type != TYPE_MAIN_VARIANT (type))
5392       type = TYPE_MAIN_VARIANT (type);
5393
5394   return type;
5395 }
5396
5397 /* Return nonzero if the given type node represents a tagged type.  */
5398
5399 static inline int
5400 is_tagged_type (const_tree type)
5401 {
5402   enum tree_code code = TREE_CODE (type);
5403
5404   return (code == RECORD_TYPE || code == UNION_TYPE
5405           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5406 }
5407
5408 /* Convert a DIE tag into its string name.  */
5409
5410 static const char *
5411 dwarf_tag_name (unsigned int tag)
5412 {
5413   switch (tag)
5414     {
5415     case DW_TAG_padding:
5416       return "DW_TAG_padding";
5417     case DW_TAG_array_type:
5418       return "DW_TAG_array_type";
5419     case DW_TAG_class_type:
5420       return "DW_TAG_class_type";
5421     case DW_TAG_entry_point:
5422       return "DW_TAG_entry_point";
5423     case DW_TAG_enumeration_type:
5424       return "DW_TAG_enumeration_type";
5425     case DW_TAG_formal_parameter:
5426       return "DW_TAG_formal_parameter";
5427     case DW_TAG_imported_declaration:
5428       return "DW_TAG_imported_declaration";
5429     case DW_TAG_label:
5430       return "DW_TAG_label";
5431     case DW_TAG_lexical_block:
5432       return "DW_TAG_lexical_block";
5433     case DW_TAG_member:
5434       return "DW_TAG_member";
5435     case DW_TAG_pointer_type:
5436       return "DW_TAG_pointer_type";
5437     case DW_TAG_reference_type:
5438       return "DW_TAG_reference_type";
5439     case DW_TAG_compile_unit:
5440       return "DW_TAG_compile_unit";
5441     case DW_TAG_string_type:
5442       return "DW_TAG_string_type";
5443     case DW_TAG_structure_type:
5444       return "DW_TAG_structure_type";
5445     case DW_TAG_subroutine_type:
5446       return "DW_TAG_subroutine_type";
5447     case DW_TAG_typedef:
5448       return "DW_TAG_typedef";
5449     case DW_TAG_union_type:
5450       return "DW_TAG_union_type";
5451     case DW_TAG_unspecified_parameters:
5452       return "DW_TAG_unspecified_parameters";
5453     case DW_TAG_variant:
5454       return "DW_TAG_variant";
5455     case DW_TAG_common_block:
5456       return "DW_TAG_common_block";
5457     case DW_TAG_common_inclusion:
5458       return "DW_TAG_common_inclusion";
5459     case DW_TAG_inheritance:
5460       return "DW_TAG_inheritance";
5461     case DW_TAG_inlined_subroutine:
5462       return "DW_TAG_inlined_subroutine";
5463     case DW_TAG_module:
5464       return "DW_TAG_module";
5465     case DW_TAG_ptr_to_member_type:
5466       return "DW_TAG_ptr_to_member_type";
5467     case DW_TAG_set_type:
5468       return "DW_TAG_set_type";
5469     case DW_TAG_subrange_type:
5470       return "DW_TAG_subrange_type";
5471     case DW_TAG_with_stmt:
5472       return "DW_TAG_with_stmt";
5473     case DW_TAG_access_declaration:
5474       return "DW_TAG_access_declaration";
5475     case DW_TAG_base_type:
5476       return "DW_TAG_base_type";
5477     case DW_TAG_catch_block:
5478       return "DW_TAG_catch_block";
5479     case DW_TAG_const_type:
5480       return "DW_TAG_const_type";
5481     case DW_TAG_constant:
5482       return "DW_TAG_constant";
5483     case DW_TAG_enumerator:
5484       return "DW_TAG_enumerator";
5485     case DW_TAG_file_type:
5486       return "DW_TAG_file_type";
5487     case DW_TAG_friend:
5488       return "DW_TAG_friend";
5489     case DW_TAG_namelist:
5490       return "DW_TAG_namelist";
5491     case DW_TAG_namelist_item:
5492       return "DW_TAG_namelist_item";
5493     case DW_TAG_packed_type:
5494       return "DW_TAG_packed_type";
5495     case DW_TAG_subprogram:
5496       return "DW_TAG_subprogram";
5497     case DW_TAG_template_type_param:
5498       return "DW_TAG_template_type_param";
5499     case DW_TAG_template_value_param:
5500       return "DW_TAG_template_value_param";
5501     case DW_TAG_thrown_type:
5502       return "DW_TAG_thrown_type";
5503     case DW_TAG_try_block:
5504       return "DW_TAG_try_block";
5505     case DW_TAG_variant_part:
5506       return "DW_TAG_variant_part";
5507     case DW_TAG_variable:
5508       return "DW_TAG_variable";
5509     case DW_TAG_volatile_type:
5510       return "DW_TAG_volatile_type";
5511     case DW_TAG_dwarf_procedure:
5512       return "DW_TAG_dwarf_procedure";
5513     case DW_TAG_restrict_type:
5514       return "DW_TAG_restrict_type";
5515     case DW_TAG_interface_type:
5516       return "DW_TAG_interface_type";
5517     case DW_TAG_namespace:
5518       return "DW_TAG_namespace";
5519     case DW_TAG_imported_module:
5520       return "DW_TAG_imported_module";
5521     case DW_TAG_unspecified_type:
5522       return "DW_TAG_unspecified_type";
5523     case DW_TAG_partial_unit:
5524       return "DW_TAG_partial_unit";
5525     case DW_TAG_imported_unit:
5526       return "DW_TAG_imported_unit";
5527     case DW_TAG_condition:
5528       return "DW_TAG_condition";
5529     case DW_TAG_shared_type:
5530       return "DW_TAG_shared_type";
5531     case DW_TAG_MIPS_loop:
5532       return "DW_TAG_MIPS_loop";
5533     case DW_TAG_format_label:
5534       return "DW_TAG_format_label";
5535     case DW_TAG_function_template:
5536       return "DW_TAG_function_template";
5537     case DW_TAG_class_template:
5538       return "DW_TAG_class_template";
5539     case DW_TAG_GNU_BINCL:
5540       return "DW_TAG_GNU_BINCL";
5541     case DW_TAG_GNU_EINCL:
5542       return "DW_TAG_GNU_EINCL";
5543     default:
5544       return "DW_TAG_<unknown>";
5545     }
5546 }
5547
5548 /* Convert a DWARF attribute code into its string name.  */
5549
5550 static const char *
5551 dwarf_attr_name (unsigned int attr)
5552 {
5553   switch (attr)
5554     {
5555     case DW_AT_sibling:
5556       return "DW_AT_sibling";
5557     case DW_AT_location:
5558       return "DW_AT_location";
5559     case DW_AT_name:
5560       return "DW_AT_name";
5561     case DW_AT_ordering:
5562       return "DW_AT_ordering";
5563     case DW_AT_subscr_data:
5564       return "DW_AT_subscr_data";
5565     case DW_AT_byte_size:
5566       return "DW_AT_byte_size";
5567     case DW_AT_bit_offset:
5568       return "DW_AT_bit_offset";
5569     case DW_AT_bit_size:
5570       return "DW_AT_bit_size";
5571     case DW_AT_element_list:
5572       return "DW_AT_element_list";
5573     case DW_AT_stmt_list:
5574       return "DW_AT_stmt_list";
5575     case DW_AT_low_pc:
5576       return "DW_AT_low_pc";
5577     case DW_AT_high_pc:
5578       return "DW_AT_high_pc";
5579     case DW_AT_language:
5580       return "DW_AT_language";
5581     case DW_AT_member:
5582       return "DW_AT_member";
5583     case DW_AT_discr:
5584       return "DW_AT_discr";
5585     case DW_AT_discr_value:
5586       return "DW_AT_discr_value";
5587     case DW_AT_visibility:
5588       return "DW_AT_visibility";
5589     case DW_AT_import:
5590       return "DW_AT_import";
5591     case DW_AT_string_length:
5592       return "DW_AT_string_length";
5593     case DW_AT_common_reference:
5594       return "DW_AT_common_reference";
5595     case DW_AT_comp_dir:
5596       return "DW_AT_comp_dir";
5597     case DW_AT_const_value:
5598       return "DW_AT_const_value";
5599     case DW_AT_containing_type:
5600       return "DW_AT_containing_type";
5601     case DW_AT_default_value:
5602       return "DW_AT_default_value";
5603     case DW_AT_inline:
5604       return "DW_AT_inline";
5605     case DW_AT_is_optional:
5606       return "DW_AT_is_optional";
5607     case DW_AT_lower_bound:
5608       return "DW_AT_lower_bound";
5609     case DW_AT_producer:
5610       return "DW_AT_producer";
5611     case DW_AT_prototyped:
5612       return "DW_AT_prototyped";
5613     case DW_AT_return_addr:
5614       return "DW_AT_return_addr";
5615     case DW_AT_start_scope:
5616       return "DW_AT_start_scope";
5617     case DW_AT_bit_stride:
5618       return "DW_AT_bit_stride";
5619     case DW_AT_upper_bound:
5620       return "DW_AT_upper_bound";
5621     case DW_AT_abstract_origin:
5622       return "DW_AT_abstract_origin";
5623     case DW_AT_accessibility:
5624       return "DW_AT_accessibility";
5625     case DW_AT_address_class:
5626       return "DW_AT_address_class";
5627     case DW_AT_artificial:
5628       return "DW_AT_artificial";
5629     case DW_AT_base_types:
5630       return "DW_AT_base_types";
5631     case DW_AT_calling_convention:
5632       return "DW_AT_calling_convention";
5633     case DW_AT_count:
5634       return "DW_AT_count";
5635     case DW_AT_data_member_location:
5636       return "DW_AT_data_member_location";
5637     case DW_AT_decl_column:
5638       return "DW_AT_decl_column";
5639     case DW_AT_decl_file:
5640       return "DW_AT_decl_file";
5641     case DW_AT_decl_line:
5642       return "DW_AT_decl_line";
5643     case DW_AT_declaration:
5644       return "DW_AT_declaration";
5645     case DW_AT_discr_list:
5646       return "DW_AT_discr_list";
5647     case DW_AT_encoding:
5648       return "DW_AT_encoding";
5649     case DW_AT_external:
5650       return "DW_AT_external";
5651     case DW_AT_frame_base:
5652       return "DW_AT_frame_base";
5653     case DW_AT_friend:
5654       return "DW_AT_friend";
5655     case DW_AT_identifier_case:
5656       return "DW_AT_identifier_case";
5657     case DW_AT_macro_info:
5658       return "DW_AT_macro_info";
5659     case DW_AT_namelist_items:
5660       return "DW_AT_namelist_items";
5661     case DW_AT_priority:
5662       return "DW_AT_priority";
5663     case DW_AT_segment:
5664       return "DW_AT_segment";
5665     case DW_AT_specification:
5666       return "DW_AT_specification";
5667     case DW_AT_static_link:
5668       return "DW_AT_static_link";
5669     case DW_AT_type:
5670       return "DW_AT_type";
5671     case DW_AT_use_location:
5672       return "DW_AT_use_location";
5673     case DW_AT_variable_parameter:
5674       return "DW_AT_variable_parameter";
5675     case DW_AT_virtuality:
5676       return "DW_AT_virtuality";
5677     case DW_AT_vtable_elem_location:
5678       return "DW_AT_vtable_elem_location";
5679
5680     case DW_AT_allocated:
5681       return "DW_AT_allocated";
5682     case DW_AT_associated:
5683       return "DW_AT_associated";
5684     case DW_AT_data_location:
5685       return "DW_AT_data_location";
5686     case DW_AT_byte_stride:
5687       return "DW_AT_byte_stride";
5688     case DW_AT_entry_pc:
5689       return "DW_AT_entry_pc";
5690     case DW_AT_use_UTF8:
5691       return "DW_AT_use_UTF8";
5692     case DW_AT_extension:
5693       return "DW_AT_extension";
5694     case DW_AT_ranges:
5695       return "DW_AT_ranges";
5696     case DW_AT_trampoline:
5697       return "DW_AT_trampoline";
5698     case DW_AT_call_column:
5699       return "DW_AT_call_column";
5700     case DW_AT_call_file:
5701       return "DW_AT_call_file";
5702     case DW_AT_call_line:
5703       return "DW_AT_call_line";
5704
5705     case DW_AT_MIPS_fde:
5706       return "DW_AT_MIPS_fde";
5707     case DW_AT_MIPS_loop_begin:
5708       return "DW_AT_MIPS_loop_begin";
5709     case DW_AT_MIPS_tail_loop_begin:
5710       return "DW_AT_MIPS_tail_loop_begin";
5711     case DW_AT_MIPS_epilog_begin:
5712       return "DW_AT_MIPS_epilog_begin";
5713     case DW_AT_MIPS_loop_unroll_factor:
5714       return "DW_AT_MIPS_loop_unroll_factor";
5715     case DW_AT_MIPS_software_pipeline_depth:
5716       return "DW_AT_MIPS_software_pipeline_depth";
5717     case DW_AT_MIPS_linkage_name:
5718       return "DW_AT_MIPS_linkage_name";
5719     case DW_AT_MIPS_stride:
5720       return "DW_AT_MIPS_stride";
5721     case DW_AT_MIPS_abstract_name:
5722       return "DW_AT_MIPS_abstract_name";
5723     case DW_AT_MIPS_clone_origin:
5724       return "DW_AT_MIPS_clone_origin";
5725     case DW_AT_MIPS_has_inlines:
5726       return "DW_AT_MIPS_has_inlines";
5727
5728     case DW_AT_sf_names:
5729       return "DW_AT_sf_names";
5730     case DW_AT_src_info:
5731       return "DW_AT_src_info";
5732     case DW_AT_mac_info:
5733       return "DW_AT_mac_info";
5734     case DW_AT_src_coords:
5735       return "DW_AT_src_coords";
5736     case DW_AT_body_begin:
5737       return "DW_AT_body_begin";
5738     case DW_AT_body_end:
5739       return "DW_AT_body_end";
5740     case DW_AT_GNU_vector:
5741       return "DW_AT_GNU_vector";
5742
5743     case DW_AT_VMS_rtnbeg_pd_address:
5744       return "DW_AT_VMS_rtnbeg_pd_address";
5745
5746     default:
5747       return "DW_AT_<unknown>";
5748     }
5749 }
5750
5751 /* Convert a DWARF value form code into its string name.  */
5752
5753 static const char *
5754 dwarf_form_name (unsigned int form)
5755 {
5756   switch (form)
5757     {
5758     case DW_FORM_addr:
5759       return "DW_FORM_addr";
5760     case DW_FORM_block2:
5761       return "DW_FORM_block2";
5762     case DW_FORM_block4:
5763       return "DW_FORM_block4";
5764     case DW_FORM_data2:
5765       return "DW_FORM_data2";
5766     case DW_FORM_data4:
5767       return "DW_FORM_data4";
5768     case DW_FORM_data8:
5769       return "DW_FORM_data8";
5770     case DW_FORM_string:
5771       return "DW_FORM_string";
5772     case DW_FORM_block:
5773       return "DW_FORM_block";
5774     case DW_FORM_block1:
5775       return "DW_FORM_block1";
5776     case DW_FORM_data1:
5777       return "DW_FORM_data1";
5778     case DW_FORM_flag:
5779       return "DW_FORM_flag";
5780     case DW_FORM_sdata:
5781       return "DW_FORM_sdata";
5782     case DW_FORM_strp:
5783       return "DW_FORM_strp";
5784     case DW_FORM_udata:
5785       return "DW_FORM_udata";
5786     case DW_FORM_ref_addr:
5787       return "DW_FORM_ref_addr";
5788     case DW_FORM_ref1:
5789       return "DW_FORM_ref1";
5790     case DW_FORM_ref2:
5791       return "DW_FORM_ref2";
5792     case DW_FORM_ref4:
5793       return "DW_FORM_ref4";
5794     case DW_FORM_ref8:
5795       return "DW_FORM_ref8";
5796     case DW_FORM_ref_udata:
5797       return "DW_FORM_ref_udata";
5798     case DW_FORM_indirect:
5799       return "DW_FORM_indirect";
5800     default:
5801       return "DW_FORM_<unknown>";
5802     }
5803 }
5804 \f
5805 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5806    instance of an inlined instance of a decl which is local to an inline
5807    function, so we have to trace all of the way back through the origin chain
5808    to find out what sort of node actually served as the original seed for the
5809    given block.  */
5810
5811 static tree
5812 decl_ultimate_origin (const_tree decl)
5813 {
5814   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5815     return NULL_TREE;
5816
5817   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5818      nodes in the function to point to themselves; ignore that if
5819      we're trying to output the abstract instance of this function.  */
5820   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5821     return NULL_TREE;
5822
5823   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5824      most distant ancestor, this should never happen.  */
5825   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5826
5827   return DECL_ABSTRACT_ORIGIN (decl);
5828 }
5829
5830 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5831    of a virtual function may refer to a base class, so we check the 'this'
5832    parameter.  */
5833
5834 static tree
5835 decl_class_context (tree decl)
5836 {
5837   tree context = NULL_TREE;
5838
5839   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5840     context = DECL_CONTEXT (decl);
5841   else
5842     context = TYPE_MAIN_VARIANT
5843       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5844
5845   if (context && !TYPE_P (context))
5846     context = NULL_TREE;
5847
5848   return context;
5849 }
5850 \f
5851 /* Add an attribute/value pair to a DIE.  */
5852
5853 static inline void
5854 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5855 {
5856   /* Maybe this should be an assert?  */
5857   if (die == NULL)
5858     return;
5859
5860   if (die->die_attr == NULL)
5861     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5862   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5863 }
5864
5865 static inline enum dw_val_class
5866 AT_class (dw_attr_ref a)
5867 {
5868   return a->dw_attr_val.val_class;
5869 }
5870
5871 /* Add a flag value attribute to a DIE.  */
5872
5873 static inline void
5874 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5875 {
5876   dw_attr_node attr;
5877
5878   attr.dw_attr = attr_kind;
5879   attr.dw_attr_val.val_class = dw_val_class_flag;
5880   attr.dw_attr_val.v.val_flag = flag;
5881   add_dwarf_attr (die, &attr);
5882 }
5883
5884 static inline unsigned
5885 AT_flag (dw_attr_ref a)
5886 {
5887   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5888   return a->dw_attr_val.v.val_flag;
5889 }
5890
5891 /* Add a signed integer attribute value to a DIE.  */
5892
5893 static inline void
5894 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5895 {
5896   dw_attr_node attr;
5897
5898   attr.dw_attr = attr_kind;
5899   attr.dw_attr_val.val_class = dw_val_class_const;
5900   attr.dw_attr_val.v.val_int = int_val;
5901   add_dwarf_attr (die, &attr);
5902 }
5903
5904 static inline HOST_WIDE_INT
5905 AT_int (dw_attr_ref a)
5906 {
5907   gcc_assert (a && AT_class (a) == dw_val_class_const);
5908   return a->dw_attr_val.v.val_int;
5909 }
5910
5911 /* Add an unsigned integer attribute value to a DIE.  */
5912
5913 static inline void
5914 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5915                  unsigned HOST_WIDE_INT unsigned_val)
5916 {
5917   dw_attr_node attr;
5918
5919   attr.dw_attr = attr_kind;
5920   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5921   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5922   add_dwarf_attr (die, &attr);
5923 }
5924
5925 static inline unsigned HOST_WIDE_INT
5926 AT_unsigned (dw_attr_ref a)
5927 {
5928   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5929   return a->dw_attr_val.v.val_unsigned;
5930 }
5931
5932 /* Add an unsigned double integer attribute value to a DIE.  */
5933
5934 static inline void
5935 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5936                   long unsigned int val_hi, long unsigned int val_low)
5937 {
5938   dw_attr_node attr;
5939
5940   attr.dw_attr = attr_kind;
5941   attr.dw_attr_val.val_class = dw_val_class_long_long;
5942   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5943   attr.dw_attr_val.v.val_long_long.low = val_low;
5944   add_dwarf_attr (die, &attr);
5945 }
5946
5947 /* Add a floating point attribute value to a DIE and return it.  */
5948
5949 static inline void
5950 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5951             unsigned int length, unsigned int elt_size, unsigned char *array)
5952 {
5953   dw_attr_node attr;
5954
5955   attr.dw_attr = attr_kind;
5956   attr.dw_attr_val.val_class = dw_val_class_vec;
5957   attr.dw_attr_val.v.val_vec.length = length;
5958   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5959   attr.dw_attr_val.v.val_vec.array = array;
5960   add_dwarf_attr (die, &attr);
5961 }
5962
5963 /* Hash and equality functions for debug_str_hash.  */
5964
5965 static hashval_t
5966 debug_str_do_hash (const void *x)
5967 {
5968   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5969 }
5970
5971 static int
5972 debug_str_eq (const void *x1, const void *x2)
5973 {
5974   return strcmp ((((const struct indirect_string_node *)x1)->str),
5975                  (const char *)x2) == 0;
5976 }
5977
5978 /* Add a string attribute value to a DIE.  */
5979
5980 static inline void
5981 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5982 {
5983   dw_attr_node attr;
5984   struct indirect_string_node *node;
5985   void **slot;
5986
5987   if (! debug_str_hash)
5988     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5989                                       debug_str_eq, NULL);
5990
5991   slot = htab_find_slot_with_hash (debug_str_hash, str,
5992                                    htab_hash_string (str), INSERT);
5993   if (*slot == NULL)
5994     {
5995       node = (struct indirect_string_node *)
5996                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5997       node->str = ggc_strdup (str);
5998       *slot = node;
5999     }
6000   else
6001     node = (struct indirect_string_node *) *slot;
6002
6003   node->refcount++;
6004
6005   attr.dw_attr = attr_kind;
6006   attr.dw_attr_val.val_class = dw_val_class_str;
6007   attr.dw_attr_val.v.val_str = node;
6008   add_dwarf_attr (die, &attr);
6009 }
6010
6011 static inline const char *
6012 AT_string (dw_attr_ref a)
6013 {
6014   gcc_assert (a && AT_class (a) == dw_val_class_str);
6015   return a->dw_attr_val.v.val_str->str;
6016 }
6017
6018 /* Find out whether a string should be output inline in DIE
6019    or out-of-line in .debug_str section.  */
6020
6021 static int
6022 AT_string_form (dw_attr_ref a)
6023 {
6024   struct indirect_string_node *node;
6025   unsigned int len;
6026   char label[32];
6027
6028   gcc_assert (a && AT_class (a) == dw_val_class_str);
6029
6030   node = a->dw_attr_val.v.val_str;
6031   if (node->form)
6032     return node->form;
6033
6034   len = strlen (node->str) + 1;
6035
6036   /* If the string is shorter or equal to the size of the reference, it is
6037      always better to put it inline.  */
6038   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6039     return node->form = DW_FORM_string;
6040
6041   /* If we cannot expect the linker to merge strings in .debug_str
6042      section, only put it into .debug_str if it is worth even in this
6043      single module.  */
6044   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6045       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6046     return node->form = DW_FORM_string;
6047
6048   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6049   ++dw2_string_counter;
6050   node->label = xstrdup (label);
6051
6052   return node->form = DW_FORM_strp;
6053 }
6054
6055 /* Add a DIE reference attribute value to a DIE.  */
6056
6057 static inline void
6058 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6059 {
6060   dw_attr_node attr;
6061
6062   attr.dw_attr = attr_kind;
6063   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6064   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6065   attr.dw_attr_val.v.val_die_ref.external = 0;
6066   add_dwarf_attr (die, &attr);
6067 }
6068
6069 /* Add an AT_specification attribute to a DIE, and also make the back
6070    pointer from the specification to the definition.  */
6071
6072 static inline void
6073 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6074 {
6075   add_AT_die_ref (die, DW_AT_specification, targ_die);
6076   gcc_assert (!targ_die->die_definition);
6077   targ_die->die_definition = die;
6078 }
6079
6080 static inline dw_die_ref
6081 AT_ref (dw_attr_ref a)
6082 {
6083   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6084   return a->dw_attr_val.v.val_die_ref.die;
6085 }
6086
6087 static inline int
6088 AT_ref_external (dw_attr_ref a)
6089 {
6090   if (a && AT_class (a) == dw_val_class_die_ref)
6091     return a->dw_attr_val.v.val_die_ref.external;
6092
6093   return 0;
6094 }
6095
6096 static inline void
6097 set_AT_ref_external (dw_attr_ref a, int i)
6098 {
6099   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6100   a->dw_attr_val.v.val_die_ref.external = i;
6101 }
6102
6103 /* Add an FDE reference attribute value to a DIE.  */
6104
6105 static inline void
6106 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6107 {
6108   dw_attr_node attr;
6109
6110   attr.dw_attr = attr_kind;
6111   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6112   attr.dw_attr_val.v.val_fde_index = targ_fde;
6113   add_dwarf_attr (die, &attr);
6114 }
6115
6116 /* Add a location description attribute value to a DIE.  */
6117
6118 static inline void
6119 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6120 {
6121   dw_attr_node attr;
6122
6123   attr.dw_attr = attr_kind;
6124   attr.dw_attr_val.val_class = dw_val_class_loc;
6125   attr.dw_attr_val.v.val_loc = loc;
6126   add_dwarf_attr (die, &attr);
6127 }
6128
6129 static inline dw_loc_descr_ref
6130 AT_loc (dw_attr_ref a)
6131 {
6132   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6133   return a->dw_attr_val.v.val_loc;
6134 }
6135
6136 static inline void
6137 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6138 {
6139   dw_attr_node attr;
6140
6141   attr.dw_attr = attr_kind;
6142   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6143   attr.dw_attr_val.v.val_loc_list = loc_list;
6144   add_dwarf_attr (die, &attr);
6145   have_location_lists = true;
6146 }
6147
6148 static inline dw_loc_list_ref
6149 AT_loc_list (dw_attr_ref a)
6150 {
6151   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6152   return a->dw_attr_val.v.val_loc_list;
6153 }
6154
6155 /* Add an address constant attribute value to a DIE.  */
6156
6157 static inline void
6158 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6159 {
6160   dw_attr_node attr;
6161
6162   attr.dw_attr = attr_kind;
6163   attr.dw_attr_val.val_class = dw_val_class_addr;
6164   attr.dw_attr_val.v.val_addr = addr;
6165   add_dwarf_attr (die, &attr);
6166 }
6167
6168 /* Get the RTX from to an address DIE attribute.  */
6169
6170 static inline rtx
6171 AT_addr (dw_attr_ref a)
6172 {
6173   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6174   return a->dw_attr_val.v.val_addr;
6175 }
6176
6177 /* Add a file attribute value to a DIE.  */
6178
6179 static inline void
6180 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6181              struct dwarf_file_data *fd)
6182 {
6183   dw_attr_node attr;
6184
6185   attr.dw_attr = attr_kind;
6186   attr.dw_attr_val.val_class = dw_val_class_file;
6187   attr.dw_attr_val.v.val_file = fd;
6188   add_dwarf_attr (die, &attr);
6189 }
6190
6191 /* Get the dwarf_file_data from a file DIE attribute.  */
6192
6193 static inline struct dwarf_file_data *
6194 AT_file (dw_attr_ref a)
6195 {
6196   gcc_assert (a && AT_class (a) == dw_val_class_file);
6197   return a->dw_attr_val.v.val_file;
6198 }
6199
6200 /* Add a label identifier attribute value to a DIE.  */
6201
6202 static inline void
6203 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6204 {
6205   dw_attr_node attr;
6206
6207   attr.dw_attr = attr_kind;
6208   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6209   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6210   add_dwarf_attr (die, &attr);
6211 }
6212
6213 /* Add a section offset attribute value to a DIE, an offset into the
6214    debug_line section.  */
6215
6216 static inline void
6217 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6218                 const char *label)
6219 {
6220   dw_attr_node attr;
6221
6222   attr.dw_attr = attr_kind;
6223   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6224   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6225   add_dwarf_attr (die, &attr);
6226 }
6227
6228 /* Add a section offset attribute value to a DIE, an offset into the
6229    debug_macinfo section.  */
6230
6231 static inline void
6232 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6233                const char *label)
6234 {
6235   dw_attr_node attr;
6236
6237   attr.dw_attr = attr_kind;
6238   attr.dw_attr_val.val_class = dw_val_class_macptr;
6239   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6240   add_dwarf_attr (die, &attr);
6241 }
6242
6243 /* Add an offset attribute value to a DIE.  */
6244
6245 static inline void
6246 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6247                unsigned HOST_WIDE_INT offset)
6248 {
6249   dw_attr_node attr;
6250
6251   attr.dw_attr = attr_kind;
6252   attr.dw_attr_val.val_class = dw_val_class_offset;
6253   attr.dw_attr_val.v.val_offset = offset;
6254   add_dwarf_attr (die, &attr);
6255 }
6256
6257 /* Add an range_list attribute value to a DIE.  */
6258
6259 static void
6260 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6261                    long unsigned int offset)
6262 {
6263   dw_attr_node attr;
6264
6265   attr.dw_attr = attr_kind;
6266   attr.dw_attr_val.val_class = dw_val_class_range_list;
6267   attr.dw_attr_val.v.val_offset = offset;
6268   add_dwarf_attr (die, &attr);
6269 }
6270
6271 static inline const char *
6272 AT_lbl (dw_attr_ref a)
6273 {
6274   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6275                     || AT_class (a) == dw_val_class_lineptr
6276                     || AT_class (a) == dw_val_class_macptr));
6277   return a->dw_attr_val.v.val_lbl_id;
6278 }
6279
6280 /* Get the attribute of type attr_kind.  */
6281
6282 static dw_attr_ref
6283 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6284 {
6285   dw_attr_ref a;
6286   unsigned ix;
6287   dw_die_ref spec = NULL;
6288
6289   if (! die)
6290     return NULL;
6291
6292   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6293     if (a->dw_attr == attr_kind)
6294       return a;
6295     else if (a->dw_attr == DW_AT_specification
6296              || a->dw_attr == DW_AT_abstract_origin)
6297       spec = AT_ref (a);
6298
6299   if (spec)
6300     return get_AT (spec, attr_kind);
6301
6302   return NULL;
6303 }
6304
6305 /* Return the "low pc" attribute value, typically associated with a subprogram
6306    DIE.  Return null if the "low pc" attribute is either not present, or if it
6307    cannot be represented as an assembler label identifier.  */
6308
6309 static inline const char *
6310 get_AT_low_pc (dw_die_ref die)
6311 {
6312   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6313
6314   return a ? AT_lbl (a) : NULL;
6315 }
6316
6317 /* Return the "high pc" attribute value, typically associated with a subprogram
6318    DIE.  Return null if the "high pc" attribute is either not present, or if it
6319    cannot be represented as an assembler label identifier.  */
6320
6321 static inline const char *
6322 get_AT_hi_pc (dw_die_ref die)
6323 {
6324   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6325
6326   return a ? AT_lbl (a) : NULL;
6327 }
6328
6329 /* Return the value of the string attribute designated by ATTR_KIND, or
6330    NULL if it is not present.  */
6331
6332 static inline const char *
6333 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6334 {
6335   dw_attr_ref a = get_AT (die, attr_kind);
6336
6337   return a ? AT_string (a) : NULL;
6338 }
6339
6340 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6341    if it is not present.  */
6342
6343 static inline int
6344 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6345 {
6346   dw_attr_ref a = get_AT (die, attr_kind);
6347
6348   return a ? AT_flag (a) : 0;
6349 }
6350
6351 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6352    if it is not present.  */
6353
6354 static inline unsigned
6355 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6356 {
6357   dw_attr_ref a = get_AT (die, attr_kind);
6358
6359   return a ? AT_unsigned (a) : 0;
6360 }
6361
6362 static inline dw_die_ref
6363 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6364 {
6365   dw_attr_ref a = get_AT (die, attr_kind);
6366
6367   return a ? AT_ref (a) : NULL;
6368 }
6369
6370 static inline struct dwarf_file_data *
6371 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6372 {
6373   dw_attr_ref a = get_AT (die, attr_kind);
6374
6375   return a ? AT_file (a) : NULL;
6376 }
6377
6378 /* Return TRUE if the language is C or C++.  */
6379
6380 static inline bool
6381 is_c_family (void)
6382 {
6383   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6384
6385   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6386           || lang == DW_LANG_C99
6387           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6388 }
6389
6390 /* Return TRUE if the language is C++.  */
6391
6392 static inline bool
6393 is_cxx (void)
6394 {
6395   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6396
6397   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6398 }
6399
6400 /* Return TRUE if the language is Fortran.  */
6401
6402 static inline bool
6403 is_fortran (void)
6404 {
6405   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6406
6407   return (lang == DW_LANG_Fortran77
6408           || lang == DW_LANG_Fortran90
6409           || lang == DW_LANG_Fortran95);
6410 }
6411
6412 /* Return TRUE if the language is Java.  */
6413
6414 static inline bool
6415 is_java (void)
6416 {
6417   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6418
6419   return lang == DW_LANG_Java;
6420 }
6421
6422 /* Return TRUE if the language is Ada.  */
6423
6424 static inline bool
6425 is_ada (void)
6426 {
6427   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6428
6429   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6430 }
6431
6432 /* Remove the specified attribute if present.  */
6433
6434 static void
6435 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6436 {
6437   dw_attr_ref a;
6438   unsigned ix;
6439
6440   if (! die)
6441     return;
6442
6443   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6444     if (a->dw_attr == attr_kind)
6445       {
6446         if (AT_class (a) == dw_val_class_str)
6447           if (a->dw_attr_val.v.val_str->refcount)
6448             a->dw_attr_val.v.val_str->refcount--;
6449
6450         /* VEC_ordered_remove should help reduce the number of abbrevs
6451            that are needed.  */
6452         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6453         return;
6454       }
6455 }
6456
6457 /* Remove CHILD from its parent.  PREV must have the property that
6458    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6459
6460 static void
6461 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6462 {
6463   gcc_assert (child->die_parent == prev->die_parent);
6464   gcc_assert (prev->die_sib == child);
6465   if (prev == child)
6466     {
6467       gcc_assert (child->die_parent->die_child == child);
6468       prev = NULL;
6469     }
6470   else
6471     prev->die_sib = child->die_sib;
6472   if (child->die_parent->die_child == child)
6473     child->die_parent->die_child = prev;
6474 }
6475
6476 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6477    matches TAG.  */
6478
6479 static void
6480 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6481 {
6482   dw_die_ref c;
6483
6484   c = die->die_child;
6485   if (c) do {
6486     dw_die_ref prev = c;
6487     c = c->die_sib;
6488     while (c->die_tag == tag)
6489       {
6490         remove_child_with_prev (c, prev);
6491         /* Might have removed every child.  */
6492         if (c == c->die_sib)
6493           return;
6494         c = c->die_sib;
6495       }
6496   } while (c != die->die_child);
6497 }
6498
6499 /* Add a CHILD_DIE as the last child of DIE.  */
6500
6501 static void
6502 add_child_die (dw_die_ref die, dw_die_ref child_die)
6503 {
6504   /* FIXME this should probably be an assert.  */
6505   if (! die || ! child_die)
6506     return;
6507   gcc_assert (die != child_die);
6508
6509   child_die->die_parent = die;
6510   if (die->die_child)
6511     {
6512       child_die->die_sib = die->die_child->die_sib;
6513       die->die_child->die_sib = child_die;
6514     }
6515   else
6516     child_die->die_sib = child_die;
6517   die->die_child = child_die;
6518 }
6519
6520 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6521    is the specification, to the end of PARENT's list of children.
6522    This is done by removing and re-adding it.  */
6523
6524 static void
6525 splice_child_die (dw_die_ref parent, dw_die_ref child)
6526 {
6527   dw_die_ref p;
6528
6529   /* We want the declaration DIE from inside the class, not the
6530      specification DIE at toplevel.  */
6531   if (child->die_parent != parent)
6532     {
6533       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6534
6535       if (tmp)
6536         child = tmp;
6537     }
6538
6539   gcc_assert (child->die_parent == parent
6540               || (child->die_parent
6541                   == get_AT_ref (parent, DW_AT_specification)));
6542
6543   for (p = child->die_parent->die_child; ; p = p->die_sib)
6544     if (p->die_sib == child)
6545       {
6546         remove_child_with_prev (child, p);
6547         break;
6548       }
6549
6550   add_child_die (parent, child);
6551 }
6552
6553 /* Return a pointer to a newly created DIE node.  */
6554
6555 static inline dw_die_ref
6556 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6557 {
6558   dw_die_ref die = GGC_CNEW (die_node);
6559
6560   die->die_tag = tag_value;
6561
6562   if (parent_die != NULL)
6563     add_child_die (parent_die, die);
6564   else
6565     {
6566       limbo_die_node *limbo_node;
6567
6568       limbo_node = GGC_CNEW (limbo_die_node);
6569       limbo_node->die = die;
6570       limbo_node->created_for = t;
6571       limbo_node->next = limbo_die_list;
6572       limbo_die_list = limbo_node;
6573     }
6574
6575   return die;
6576 }
6577
6578 /* Return the DIE associated with the given type specifier.  */
6579
6580 static inline dw_die_ref
6581 lookup_type_die (tree type)
6582 {
6583   return TYPE_SYMTAB_DIE (type);
6584 }
6585
6586 /* Equate a DIE to a given type specifier.  */
6587
6588 static inline void
6589 equate_type_number_to_die (tree type, dw_die_ref type_die)
6590 {
6591   TYPE_SYMTAB_DIE (type) = type_die;
6592 }
6593
6594 /* Returns a hash value for X (which really is a die_struct).  */
6595
6596 static hashval_t
6597 decl_die_table_hash (const void *x)
6598 {
6599   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6600 }
6601
6602 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6603
6604 static int
6605 decl_die_table_eq (const void *x, const void *y)
6606 {
6607   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6608 }
6609
6610 /* Return the DIE associated with a given declaration.  */
6611
6612 static inline dw_die_ref
6613 lookup_decl_die (tree decl)
6614 {
6615   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6616 }
6617
6618 /* Returns a hash value for X (which really is a var_loc_list).  */
6619
6620 static hashval_t
6621 decl_loc_table_hash (const void *x)
6622 {
6623   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6624 }
6625
6626 /* Return nonzero if decl_id of var_loc_list X is the same as
6627    UID of decl *Y.  */
6628
6629 static int
6630 decl_loc_table_eq (const void *x, const void *y)
6631 {
6632   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6633 }
6634
6635 /* Return the var_loc list associated with a given declaration.  */
6636
6637 static inline var_loc_list *
6638 lookup_decl_loc (const_tree decl)
6639 {
6640   return (var_loc_list *)
6641     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6642 }
6643
6644 /* Equate a DIE to a particular declaration.  */
6645
6646 static void
6647 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6648 {
6649   unsigned int decl_id = DECL_UID (decl);
6650   void **slot;
6651
6652   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6653   *slot = decl_die;
6654   decl_die->decl_id = decl_id;
6655 }
6656
6657 /* Add a variable location node to the linked list for DECL.  */
6658
6659 static void
6660 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6661 {
6662   unsigned int decl_id = DECL_UID (decl);
6663   var_loc_list *temp;
6664   void **slot;
6665
6666   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6667   if (*slot == NULL)
6668     {
6669       temp = GGC_CNEW (var_loc_list);
6670       temp->decl_id = decl_id;
6671       *slot = temp;
6672     }
6673   else
6674     temp = (var_loc_list *) *slot;
6675
6676   if (temp->last)
6677     {
6678       /* If the current location is the same as the end of the list,
6679          and either both or neither of the locations is uninitialized,
6680          we have nothing to do.  */
6681       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6682                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6683           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6684                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6685               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6686                    == VAR_INIT_STATUS_UNINITIALIZED)
6687                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6688                       == VAR_INIT_STATUS_UNINITIALIZED))))
6689         {
6690           /* Add LOC to the end of list and update LAST.  */
6691           temp->last->next = loc;
6692           temp->last = loc;
6693         }
6694     }
6695   /* Do not add empty location to the beginning of the list.  */
6696   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6697     {
6698       temp->first = loc;
6699       temp->last = loc;
6700     }
6701 }
6702 \f
6703 /* Keep track of the number of spaces used to indent the
6704    output of the debugging routines that print the structure of
6705    the DIE internal representation.  */
6706 static int print_indent;
6707
6708 /* Indent the line the number of spaces given by print_indent.  */
6709
6710 static inline void
6711 print_spaces (FILE *outfile)
6712 {
6713   fprintf (outfile, "%*s", print_indent, "");
6714 }
6715
6716 /* Print the information associated with a given DIE, and its children.
6717    This routine is a debugging aid only.  */
6718
6719 static void
6720 print_die (dw_die_ref die, FILE *outfile)
6721 {
6722   dw_attr_ref a;
6723   dw_die_ref c;
6724   unsigned ix;
6725
6726   print_spaces (outfile);
6727   fprintf (outfile, "DIE %4ld: %s\n",
6728            die->die_offset, dwarf_tag_name (die->die_tag));
6729   print_spaces (outfile);
6730   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6731   fprintf (outfile, " offset: %ld\n", die->die_offset);
6732
6733   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6734     {
6735       print_spaces (outfile);
6736       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6737
6738       switch (AT_class (a))
6739         {
6740         case dw_val_class_addr:
6741           fprintf (outfile, "address");
6742           break;
6743         case dw_val_class_offset:
6744           fprintf (outfile, "offset");
6745           break;
6746         case dw_val_class_loc:
6747           fprintf (outfile, "location descriptor");
6748           break;
6749         case dw_val_class_loc_list:
6750           fprintf (outfile, "location list -> label:%s",
6751                    AT_loc_list (a)->ll_symbol);
6752           break;
6753         case dw_val_class_range_list:
6754           fprintf (outfile, "range list");
6755           break;
6756         case dw_val_class_const:
6757           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6758           break;
6759         case dw_val_class_unsigned_const:
6760           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6761           break;
6762         case dw_val_class_long_long:
6763           fprintf (outfile, "constant (%lu,%lu)",
6764                    a->dw_attr_val.v.val_long_long.hi,
6765                    a->dw_attr_val.v.val_long_long.low);
6766           break;
6767         case dw_val_class_vec:
6768           fprintf (outfile, "floating-point or vector constant");
6769           break;
6770         case dw_val_class_flag:
6771           fprintf (outfile, "%u", AT_flag (a));
6772           break;
6773         case dw_val_class_die_ref:
6774           if (AT_ref (a) != NULL)
6775             {
6776               if (AT_ref (a)->die_symbol)
6777                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6778               else
6779                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6780             }
6781           else
6782             fprintf (outfile, "die -> <null>");
6783           break;
6784         case dw_val_class_lbl_id:
6785         case dw_val_class_lineptr:
6786         case dw_val_class_macptr:
6787           fprintf (outfile, "label: %s", AT_lbl (a));
6788           break;
6789         case dw_val_class_str:
6790           if (AT_string (a) != NULL)
6791             fprintf (outfile, "\"%s\"", AT_string (a));
6792           else
6793             fprintf (outfile, "<null>");
6794           break;
6795         case dw_val_class_file:
6796           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6797                    AT_file (a)->emitted_number);
6798           break;
6799         default:
6800           break;
6801         }
6802
6803       fprintf (outfile, "\n");
6804     }
6805
6806   if (die->die_child != NULL)
6807     {
6808       print_indent += 4;
6809       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6810       print_indent -= 4;
6811     }
6812   if (print_indent == 0)
6813     fprintf (outfile, "\n");
6814 }
6815
6816 /* Print the contents of the source code line number correspondence table.
6817    This routine is a debugging aid only.  */
6818
6819 static void
6820 print_dwarf_line_table (FILE *outfile)
6821 {
6822   unsigned i;
6823   dw_line_info_ref line_info;
6824
6825   fprintf (outfile, "\n\nDWARF source line information\n");
6826   for (i = 1; i < line_info_table_in_use; i++)
6827     {
6828       line_info = &line_info_table[i];
6829       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6830                line_info->dw_file_num,
6831                line_info->dw_line_num);
6832     }
6833
6834   fprintf (outfile, "\n\n");
6835 }
6836
6837 /* Print the information collected for a given DIE.  */
6838
6839 void
6840 debug_dwarf_die (dw_die_ref die)
6841 {
6842   print_die (die, stderr);
6843 }
6844
6845 /* Print all DWARF information collected for the compilation unit.
6846    This routine is a debugging aid only.  */
6847
6848 void
6849 debug_dwarf (void)
6850 {
6851   print_indent = 0;
6852   print_die (comp_unit_die, stderr);
6853   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6854     print_dwarf_line_table (stderr);
6855 }
6856 \f
6857 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6858    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6859    DIE that marks the start of the DIEs for this include file.  */
6860
6861 static dw_die_ref
6862 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6863 {
6864   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6865   dw_die_ref new_unit = gen_compile_unit_die (filename);
6866
6867   new_unit->die_sib = old_unit;
6868   return new_unit;
6869 }
6870
6871 /* Close an include-file CU and reopen the enclosing one.  */
6872
6873 static dw_die_ref
6874 pop_compile_unit (dw_die_ref old_unit)
6875 {
6876   dw_die_ref new_unit = old_unit->die_sib;
6877
6878   old_unit->die_sib = NULL;
6879   return new_unit;
6880 }
6881
6882 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6883 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6884
6885 /* Calculate the checksum of a location expression.  */
6886
6887 static inline void
6888 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6889 {
6890   CHECKSUM (loc->dw_loc_opc);
6891   CHECKSUM (loc->dw_loc_oprnd1);
6892   CHECKSUM (loc->dw_loc_oprnd2);
6893 }
6894
6895 /* Calculate the checksum of an attribute.  */
6896
6897 static void
6898 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6899 {
6900   dw_loc_descr_ref loc;
6901   rtx r;
6902
6903   CHECKSUM (at->dw_attr);
6904
6905   /* We don't care that this was compiled with a different compiler
6906      snapshot; if the output is the same, that's what matters.  */
6907   if (at->dw_attr == DW_AT_producer)
6908     return;
6909
6910   switch (AT_class (at))
6911     {
6912     case dw_val_class_const:
6913       CHECKSUM (at->dw_attr_val.v.val_int);
6914       break;
6915     case dw_val_class_unsigned_const:
6916       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6917       break;
6918     case dw_val_class_long_long:
6919       CHECKSUM (at->dw_attr_val.v.val_long_long);
6920       break;
6921     case dw_val_class_vec:
6922       CHECKSUM (at->dw_attr_val.v.val_vec);
6923       break;
6924     case dw_val_class_flag:
6925       CHECKSUM (at->dw_attr_val.v.val_flag);
6926       break;
6927     case dw_val_class_str:
6928       CHECKSUM_STRING (AT_string (at));
6929       break;
6930
6931     case dw_val_class_addr:
6932       r = AT_addr (at);
6933       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6934       CHECKSUM_STRING (XSTR (r, 0));
6935       break;
6936
6937     case dw_val_class_offset:
6938       CHECKSUM (at->dw_attr_val.v.val_offset);
6939       break;
6940
6941     case dw_val_class_loc:
6942       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6943         loc_checksum (loc, ctx);
6944       break;
6945
6946     case dw_val_class_die_ref:
6947       die_checksum (AT_ref (at), ctx, mark);
6948       break;
6949
6950     case dw_val_class_fde_ref:
6951     case dw_val_class_lbl_id:
6952     case dw_val_class_lineptr:
6953     case dw_val_class_macptr:
6954       break;
6955
6956     case dw_val_class_file:
6957       CHECKSUM_STRING (AT_file (at)->filename);
6958       break;
6959
6960     default:
6961       break;
6962     }
6963 }
6964
6965 /* Calculate the checksum of a DIE.  */
6966
6967 static void
6968 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6969 {
6970   dw_die_ref c;
6971   dw_attr_ref a;
6972   unsigned ix;
6973
6974   /* To avoid infinite recursion.  */
6975   if (die->die_mark)
6976     {
6977       CHECKSUM (die->die_mark);
6978       return;
6979     }
6980   die->die_mark = ++(*mark);
6981
6982   CHECKSUM (die->die_tag);
6983
6984   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6985     attr_checksum (a, ctx, mark);
6986
6987   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6988 }
6989
6990 #undef CHECKSUM
6991 #undef CHECKSUM_STRING
6992
6993 /* Do the location expressions look same?  */
6994 static inline int
6995 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6996 {
6997   return loc1->dw_loc_opc == loc2->dw_loc_opc
6998          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6999          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7000 }
7001
7002 /* Do the values look the same?  */
7003 static int
7004 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7005 {
7006   dw_loc_descr_ref loc1, loc2;
7007   rtx r1, r2;
7008
7009   if (v1->val_class != v2->val_class)
7010     return 0;
7011
7012   switch (v1->val_class)
7013     {
7014     case dw_val_class_const:
7015       return v1->v.val_int == v2->v.val_int;
7016     case dw_val_class_unsigned_const:
7017       return v1->v.val_unsigned == v2->v.val_unsigned;
7018     case dw_val_class_long_long:
7019       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7020              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7021     case dw_val_class_vec:
7022       if (v1->v.val_vec.length != v2->v.val_vec.length
7023           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7024         return 0;
7025       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7026                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7027         return 0;
7028       return 1;
7029     case dw_val_class_flag:
7030       return v1->v.val_flag == v2->v.val_flag;
7031     case dw_val_class_str:
7032       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7033
7034     case dw_val_class_addr:
7035       r1 = v1->v.val_addr;
7036       r2 = v2->v.val_addr;
7037       if (GET_CODE (r1) != GET_CODE (r2))
7038         return 0;
7039       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7040       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7041
7042     case dw_val_class_offset:
7043       return v1->v.val_offset == v2->v.val_offset;
7044
7045     case dw_val_class_loc:
7046       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7047            loc1 && loc2;
7048            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7049         if (!same_loc_p (loc1, loc2, mark))
7050           return 0;
7051       return !loc1 && !loc2;
7052
7053     case dw_val_class_die_ref:
7054       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7055
7056     case dw_val_class_fde_ref:
7057     case dw_val_class_lbl_id:
7058     case dw_val_class_lineptr:
7059     case dw_val_class_macptr:
7060       return 1;
7061
7062     case dw_val_class_file:
7063       return v1->v.val_file == v2->v.val_file;
7064
7065     default:
7066       return 1;
7067     }
7068 }
7069
7070 /* Do the attributes look the same?  */
7071
7072 static int
7073 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7074 {
7075   if (at1->dw_attr != at2->dw_attr)
7076     return 0;
7077
7078   /* We don't care that this was compiled with a different compiler
7079      snapshot; if the output is the same, that's what matters. */
7080   if (at1->dw_attr == DW_AT_producer)
7081     return 1;
7082
7083   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7084 }
7085
7086 /* Do the dies look the same?  */
7087
7088 static int
7089 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7090 {
7091   dw_die_ref c1, c2;
7092   dw_attr_ref a1;
7093   unsigned ix;
7094
7095   /* To avoid infinite recursion.  */
7096   if (die1->die_mark)
7097     return die1->die_mark == die2->die_mark;
7098   die1->die_mark = die2->die_mark = ++(*mark);
7099
7100   if (die1->die_tag != die2->die_tag)
7101     return 0;
7102
7103   if (VEC_length (dw_attr_node, die1->die_attr)
7104       != VEC_length (dw_attr_node, die2->die_attr))
7105     return 0;
7106
7107   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7108     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7109       return 0;
7110
7111   c1 = die1->die_child;
7112   c2 = die2->die_child;
7113   if (! c1)
7114     {
7115       if (c2)
7116         return 0;
7117     }
7118   else
7119     for (;;)
7120       {
7121         if (!same_die_p (c1, c2, mark))
7122           return 0;
7123         c1 = c1->die_sib;
7124         c2 = c2->die_sib;
7125         if (c1 == die1->die_child)
7126           {
7127             if (c2 == die2->die_child)
7128               break;
7129             else
7130               return 0;
7131           }
7132     }
7133
7134   return 1;
7135 }
7136
7137 /* Do the dies look the same?  Wrapper around same_die_p.  */
7138
7139 static int
7140 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7141 {
7142   int mark = 0;
7143   int ret = same_die_p (die1, die2, &mark);
7144
7145   unmark_all_dies (die1);
7146   unmark_all_dies (die2);
7147
7148   return ret;
7149 }
7150
7151 /* The prefix to attach to symbols on DIEs in the current comdat debug
7152    info section.  */
7153 static char *comdat_symbol_id;
7154
7155 /* The index of the current symbol within the current comdat CU.  */
7156 static unsigned int comdat_symbol_number;
7157
7158 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7159    children, and set comdat_symbol_id accordingly.  */
7160
7161 static void
7162 compute_section_prefix (dw_die_ref unit_die)
7163 {
7164   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7165   const char *base = die_name ? lbasename (die_name) : "anonymous";
7166   char *name = XALLOCAVEC (char, strlen (base) + 64);
7167   char *p;
7168   int i, mark;
7169   unsigned char checksum[16];
7170   struct md5_ctx ctx;
7171
7172   /* Compute the checksum of the DIE, then append part of it as hex digits to
7173      the name filename of the unit.  */
7174
7175   md5_init_ctx (&ctx);
7176   mark = 0;
7177   die_checksum (unit_die, &ctx, &mark);
7178   unmark_all_dies (unit_die);
7179   md5_finish_ctx (&ctx, checksum);
7180
7181   sprintf (name, "%s.", base);
7182   clean_symbol_name (name);
7183
7184   p = name + strlen (name);
7185   for (i = 0; i < 4; i++)
7186     {
7187       sprintf (p, "%.2x", checksum[i]);
7188       p += 2;
7189     }
7190
7191   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7192   comdat_symbol_number = 0;
7193 }
7194
7195 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7196
7197 static int
7198 is_type_die (dw_die_ref die)
7199 {
7200   switch (die->die_tag)
7201     {
7202     case DW_TAG_array_type:
7203     case DW_TAG_class_type:
7204     case DW_TAG_interface_type:
7205     case DW_TAG_enumeration_type:
7206     case DW_TAG_pointer_type:
7207     case DW_TAG_reference_type:
7208     case DW_TAG_string_type:
7209     case DW_TAG_structure_type:
7210     case DW_TAG_subroutine_type:
7211     case DW_TAG_union_type:
7212     case DW_TAG_ptr_to_member_type:
7213     case DW_TAG_set_type:
7214     case DW_TAG_subrange_type:
7215     case DW_TAG_base_type:
7216     case DW_TAG_const_type:
7217     case DW_TAG_file_type:
7218     case DW_TAG_packed_type:
7219     case DW_TAG_volatile_type:
7220     case DW_TAG_typedef:
7221       return 1;
7222     default:
7223       return 0;
7224     }
7225 }
7226
7227 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7228    Basically, we want to choose the bits that are likely to be shared between
7229    compilations (types) and leave out the bits that are specific to individual
7230    compilations (functions).  */
7231
7232 static int
7233 is_comdat_die (dw_die_ref c)
7234 {
7235   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7236      we do for stabs.  The advantage is a greater likelihood of sharing between
7237      objects that don't include headers in the same order (and therefore would
7238      put the base types in a different comdat).  jason 8/28/00 */
7239
7240   if (c->die_tag == DW_TAG_base_type)
7241     return 0;
7242
7243   if (c->die_tag == DW_TAG_pointer_type
7244       || c->die_tag == DW_TAG_reference_type
7245       || c->die_tag == DW_TAG_const_type
7246       || c->die_tag == DW_TAG_volatile_type)
7247     {
7248       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7249
7250       return t ? is_comdat_die (t) : 0;
7251     }
7252
7253   return is_type_die (c);
7254 }
7255
7256 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7257    compilation unit.  */
7258
7259 static int
7260 is_symbol_die (dw_die_ref c)
7261 {
7262   return (is_type_die (c)
7263           || (get_AT (c, DW_AT_declaration)
7264               && !get_AT (c, DW_AT_specification))
7265           || c->die_tag == DW_TAG_namespace
7266           || c->die_tag == DW_TAG_module);
7267 }
7268
7269 static char *
7270 gen_internal_sym (const char *prefix)
7271 {
7272   char buf[256];
7273
7274   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7275   return xstrdup (buf);
7276 }
7277
7278 /* Assign symbols to all worthy DIEs under DIE.  */
7279
7280 static void
7281 assign_symbol_names (dw_die_ref die)
7282 {
7283   dw_die_ref c;
7284
7285   if (is_symbol_die (die))
7286     {
7287       if (comdat_symbol_id)
7288         {
7289           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7290
7291           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7292                    comdat_symbol_id, comdat_symbol_number++);
7293           die->die_symbol = xstrdup (p);
7294         }
7295       else
7296         die->die_symbol = gen_internal_sym ("LDIE");
7297     }
7298
7299   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7300 }
7301
7302 struct cu_hash_table_entry
7303 {
7304   dw_die_ref cu;
7305   unsigned min_comdat_num, max_comdat_num;
7306   struct cu_hash_table_entry *next;
7307 };
7308
7309 /* Routines to manipulate hash table of CUs.  */
7310 static hashval_t
7311 htab_cu_hash (const void *of)
7312 {
7313   const struct cu_hash_table_entry *const entry =
7314     (const struct cu_hash_table_entry *) of;
7315
7316   return htab_hash_string (entry->cu->die_symbol);
7317 }
7318
7319 static int
7320 htab_cu_eq (const void *of1, const void *of2)
7321 {
7322   const struct cu_hash_table_entry *const entry1 =
7323     (const struct cu_hash_table_entry *) of1;
7324   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7325
7326   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7327 }
7328
7329 static void
7330 htab_cu_del (void *what)
7331 {
7332   struct cu_hash_table_entry *next,
7333     *entry = (struct cu_hash_table_entry *) what;
7334
7335   while (entry)
7336     {
7337       next = entry->next;
7338       free (entry);
7339       entry = next;
7340     }
7341 }
7342
7343 /* Check whether we have already seen this CU and set up SYM_NUM
7344    accordingly.  */
7345 static int
7346 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7347 {
7348   struct cu_hash_table_entry dummy;
7349   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7350
7351   dummy.max_comdat_num = 0;
7352
7353   slot = (struct cu_hash_table_entry **)
7354     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7355         INSERT);
7356   entry = *slot;
7357
7358   for (; entry; last = entry, entry = entry->next)
7359     {
7360       if (same_die_p_wrap (cu, entry->cu))
7361         break;
7362     }
7363
7364   if (entry)
7365     {
7366       *sym_num = entry->min_comdat_num;
7367       return 1;
7368     }
7369
7370   entry = XCNEW (struct cu_hash_table_entry);
7371   entry->cu = cu;
7372   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7373   entry->next = *slot;
7374   *slot = entry;
7375
7376   return 0;
7377 }
7378
7379 /* Record SYM_NUM to record of CU in HTABLE.  */
7380 static void
7381 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7382 {
7383   struct cu_hash_table_entry **slot, *entry;
7384
7385   slot = (struct cu_hash_table_entry **)
7386     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7387         NO_INSERT);
7388   entry = *slot;
7389
7390   entry->max_comdat_num = sym_num;
7391 }
7392
7393 /* Traverse the DIE (which is always comp_unit_die), and set up
7394    additional compilation units for each of the include files we see
7395    bracketed by BINCL/EINCL.  */
7396
7397 static void
7398 break_out_includes (dw_die_ref die)
7399 {
7400   dw_die_ref c;
7401   dw_die_ref unit = NULL;
7402   limbo_die_node *node, **pnode;
7403   htab_t cu_hash_table;
7404
7405   c = die->die_child;
7406   if (c) do {
7407     dw_die_ref prev = c;
7408     c = c->die_sib;
7409     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7410            || (unit && is_comdat_die (c)))
7411       {
7412         dw_die_ref next = c->die_sib;
7413
7414         /* This DIE is for a secondary CU; remove it from the main one.  */
7415         remove_child_with_prev (c, prev);
7416
7417         if (c->die_tag == DW_TAG_GNU_BINCL)
7418           unit = push_new_compile_unit (unit, c);
7419         else if (c->die_tag == DW_TAG_GNU_EINCL)
7420           unit = pop_compile_unit (unit);
7421         else
7422           add_child_die (unit, c);
7423         c = next;
7424         if (c == die->die_child)
7425           break;
7426       }
7427   } while (c != die->die_child);
7428
7429 #if 0
7430   /* We can only use this in debugging, since the frontend doesn't check
7431      to make sure that we leave every include file we enter.  */
7432   gcc_assert (!unit);
7433 #endif
7434
7435   assign_symbol_names (die);
7436   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7437   for (node = limbo_die_list, pnode = &limbo_die_list;
7438        node;
7439        node = node->next)
7440     {
7441       int is_dupl;
7442
7443       compute_section_prefix (node->die);
7444       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7445                         &comdat_symbol_number);
7446       assign_symbol_names (node->die);
7447       if (is_dupl)
7448         *pnode = node->next;
7449       else
7450         {
7451           pnode = &node->next;
7452           record_comdat_symbol_number (node->die, cu_hash_table,
7453                 comdat_symbol_number);
7454         }
7455     }
7456   htab_delete (cu_hash_table);
7457 }
7458
7459 /* Traverse the DIE and add a sibling attribute if it may have the
7460    effect of speeding up access to siblings.  To save some space,
7461    avoid generating sibling attributes for DIE's without children.  */
7462
7463 static void
7464 add_sibling_attributes (dw_die_ref die)
7465 {
7466   dw_die_ref c;
7467
7468   if (! die->die_child)
7469     return;
7470
7471   if (die->die_parent && die != die->die_parent->die_child)
7472     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7473
7474   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7475 }
7476
7477 /* Output all location lists for the DIE and its children.  */
7478
7479 static void
7480 output_location_lists (dw_die_ref die)
7481 {
7482   dw_die_ref c;
7483   dw_attr_ref a;
7484   unsigned ix;
7485
7486   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7487     if (AT_class (a) == dw_val_class_loc_list)
7488       output_loc_list (AT_loc_list (a));
7489
7490   FOR_EACH_CHILD (die, c, output_location_lists (c));
7491 }
7492
7493 /* The format of each DIE (and its attribute value pairs) is encoded in an
7494    abbreviation table.  This routine builds the abbreviation table and assigns
7495    a unique abbreviation id for each abbreviation entry.  The children of each
7496    die are visited recursively.  */
7497
7498 static void
7499 build_abbrev_table (dw_die_ref die)
7500 {
7501   unsigned long abbrev_id;
7502   unsigned int n_alloc;
7503   dw_die_ref c;
7504   dw_attr_ref a;
7505   unsigned ix;
7506
7507   /* Scan the DIE references, and mark as external any that refer to
7508      DIEs from other CUs (i.e. those which are not marked).  */
7509   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7510     if (AT_class (a) == dw_val_class_die_ref
7511         && AT_ref (a)->die_mark == 0)
7512       {
7513         gcc_assert (AT_ref (a)->die_symbol);
7514
7515         set_AT_ref_external (a, 1);
7516       }
7517
7518   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7519     {
7520       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7521       dw_attr_ref die_a, abbrev_a;
7522       unsigned ix;
7523       bool ok = true;
7524
7525       if (abbrev->die_tag != die->die_tag)
7526         continue;
7527       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7528         continue;
7529
7530       if (VEC_length (dw_attr_node, abbrev->die_attr)
7531           != VEC_length (dw_attr_node, die->die_attr))
7532         continue;
7533
7534       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7535         {
7536           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7537           if ((abbrev_a->dw_attr != die_a->dw_attr)
7538               || (value_format (abbrev_a) != value_format (die_a)))
7539             {
7540               ok = false;
7541               break;
7542             }
7543         }
7544       if (ok)
7545         break;
7546     }
7547
7548   if (abbrev_id >= abbrev_die_table_in_use)
7549     {
7550       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7551         {
7552           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7553           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7554                                             n_alloc);
7555
7556           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7557                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7558           abbrev_die_table_allocated = n_alloc;
7559         }
7560
7561       ++abbrev_die_table_in_use;
7562       abbrev_die_table[abbrev_id] = die;
7563     }
7564
7565   die->die_abbrev = abbrev_id;
7566   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7567 }
7568 \f
7569 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7570
7571 static int
7572 constant_size (unsigned HOST_WIDE_INT value)
7573 {
7574   int log;
7575
7576   if (value == 0)
7577     log = 0;
7578   else
7579     log = floor_log2 (value);
7580
7581   log = log / 8;
7582   log = 1 << (floor_log2 (log) + 1);
7583
7584   return log;
7585 }
7586
7587 /* Return the size of a DIE as it is represented in the
7588    .debug_info section.  */
7589
7590 static unsigned long
7591 size_of_die (dw_die_ref die)
7592 {
7593   unsigned long size = 0;
7594   dw_attr_ref a;
7595   unsigned ix;
7596
7597   size += size_of_uleb128 (die->die_abbrev);
7598   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7599     {
7600       switch (AT_class (a))
7601         {
7602         case dw_val_class_addr:
7603           size += DWARF2_ADDR_SIZE;
7604           break;
7605         case dw_val_class_offset:
7606           size += DWARF_OFFSET_SIZE;
7607           break;
7608         case dw_val_class_loc:
7609           {
7610             unsigned long lsize = size_of_locs (AT_loc (a));
7611
7612             /* Block length.  */
7613             size += constant_size (lsize);
7614             size += lsize;
7615           }
7616           break;
7617         case dw_val_class_loc_list:
7618           size += DWARF_OFFSET_SIZE;
7619           break;
7620         case dw_val_class_range_list:
7621           size += DWARF_OFFSET_SIZE;
7622           break;
7623         case dw_val_class_const:
7624           size += size_of_sleb128 (AT_int (a));
7625           break;
7626         case dw_val_class_unsigned_const:
7627           size += constant_size (AT_unsigned (a));
7628           break;
7629         case dw_val_class_long_long:
7630           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7631           break;
7632         case dw_val_class_vec:
7633           size += constant_size (a->dw_attr_val.v.val_vec.length
7634                                  * a->dw_attr_val.v.val_vec.elt_size)
7635                   + a->dw_attr_val.v.val_vec.length
7636                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7637           break;
7638         case dw_val_class_flag:
7639           size += 1;
7640           break;
7641         case dw_val_class_die_ref:
7642           if (AT_ref_external (a))
7643             size += DWARF2_ADDR_SIZE;
7644           else
7645             size += DWARF_OFFSET_SIZE;
7646           break;
7647         case dw_val_class_fde_ref:
7648           size += DWARF_OFFSET_SIZE;
7649           break;
7650         case dw_val_class_lbl_id:
7651           size += DWARF2_ADDR_SIZE;
7652           break;
7653         case dw_val_class_lineptr:
7654         case dw_val_class_macptr:
7655           size += DWARF_OFFSET_SIZE;
7656           break;
7657         case dw_val_class_str:
7658           if (AT_string_form (a) == DW_FORM_strp)
7659             size += DWARF_OFFSET_SIZE;
7660           else
7661             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7662           break;
7663         case dw_val_class_file:
7664           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7665           break;
7666         default:
7667           gcc_unreachable ();
7668         }
7669     }
7670
7671   return size;
7672 }
7673
7674 /* Size the debugging information associated with a given DIE.  Visits the
7675    DIE's children recursively.  Updates the global variable next_die_offset, on
7676    each time through.  Uses the current value of next_die_offset to update the
7677    die_offset field in each DIE.  */
7678
7679 static void
7680 calc_die_sizes (dw_die_ref die)
7681 {
7682   dw_die_ref c;
7683
7684   die->die_offset = next_die_offset;
7685   next_die_offset += size_of_die (die);
7686
7687   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7688
7689   if (die->die_child != NULL)
7690     /* Count the null byte used to terminate sibling lists.  */
7691     next_die_offset += 1;
7692 }
7693
7694 /* Set the marks for a die and its children.  We do this so
7695    that we know whether or not a reference needs to use FORM_ref_addr; only
7696    DIEs in the same CU will be marked.  We used to clear out the offset
7697    and use that as the flag, but ran into ordering problems.  */
7698
7699 static void
7700 mark_dies (dw_die_ref die)
7701 {
7702   dw_die_ref c;
7703
7704   gcc_assert (!die->die_mark);
7705
7706   die->die_mark = 1;
7707   FOR_EACH_CHILD (die, c, mark_dies (c));
7708 }
7709
7710 /* Clear the marks for a die and its children.  */
7711
7712 static void
7713 unmark_dies (dw_die_ref die)
7714 {
7715   dw_die_ref c;
7716
7717   gcc_assert (die->die_mark);
7718
7719   die->die_mark = 0;
7720   FOR_EACH_CHILD (die, c, unmark_dies (c));
7721 }
7722
7723 /* Clear the marks for a die, its children and referred dies.  */
7724
7725 static void
7726 unmark_all_dies (dw_die_ref die)
7727 {
7728   dw_die_ref c;
7729   dw_attr_ref a;
7730   unsigned ix;
7731
7732   if (!die->die_mark)
7733     return;
7734   die->die_mark = 0;
7735
7736   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7737
7738   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7739     if (AT_class (a) == dw_val_class_die_ref)
7740       unmark_all_dies (AT_ref (a));
7741 }
7742
7743 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7744    generated for the compilation unit.  */
7745
7746 static unsigned long
7747 size_of_pubnames (VEC (pubname_entry, gc) * names)
7748 {
7749   unsigned long size;
7750   unsigned i;
7751   pubname_ref p;
7752
7753   size = DWARF_PUBNAMES_HEADER_SIZE;
7754   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7755     if (names != pubtype_table
7756         || p->die->die_offset != 0
7757         || !flag_eliminate_unused_debug_types)
7758       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7759
7760   size += DWARF_OFFSET_SIZE;
7761   return size;
7762 }
7763
7764 /* Return the size of the information in the .debug_aranges section.  */
7765
7766 static unsigned long
7767 size_of_aranges (void)
7768 {
7769   unsigned long size;
7770
7771   size = DWARF_ARANGES_HEADER_SIZE;
7772
7773   /* Count the address/length pair for this compilation unit.  */
7774   if (text_section_used)
7775     size += 2 * DWARF2_ADDR_SIZE;
7776   if (cold_text_section_used)
7777     size += 2 * DWARF2_ADDR_SIZE;
7778   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7779
7780   /* Count the two zero words used to terminated the address range table.  */
7781   size += 2 * DWARF2_ADDR_SIZE;
7782   return size;
7783 }
7784 \f
7785 /* Select the encoding of an attribute value.  */
7786
7787 static enum dwarf_form
7788 value_format (dw_attr_ref a)
7789 {
7790   switch (a->dw_attr_val.val_class)
7791     {
7792     case dw_val_class_addr:
7793       return DW_FORM_addr;
7794     case dw_val_class_range_list:
7795     case dw_val_class_offset:
7796     case dw_val_class_loc_list:
7797       switch (DWARF_OFFSET_SIZE)
7798         {
7799         case 4:
7800           return DW_FORM_data4;
7801         case 8:
7802           return DW_FORM_data8;
7803         default:
7804           gcc_unreachable ();
7805         }
7806     case dw_val_class_loc:
7807       switch (constant_size (size_of_locs (AT_loc (a))))
7808         {
7809         case 1:
7810           return DW_FORM_block1;
7811         case 2:
7812           return DW_FORM_block2;
7813         default:
7814           gcc_unreachable ();
7815         }
7816     case dw_val_class_const:
7817       return DW_FORM_sdata;
7818     case dw_val_class_unsigned_const:
7819       switch (constant_size (AT_unsigned (a)))
7820         {
7821         case 1:
7822           return DW_FORM_data1;
7823         case 2:
7824           return DW_FORM_data2;
7825         case 4:
7826           return DW_FORM_data4;
7827         case 8:
7828           return DW_FORM_data8;
7829         default:
7830           gcc_unreachable ();
7831         }
7832     case dw_val_class_long_long:
7833       return DW_FORM_block1;
7834     case dw_val_class_vec:
7835       switch (constant_size (a->dw_attr_val.v.val_vec.length
7836                              * a->dw_attr_val.v.val_vec.elt_size))
7837         {
7838         case 1:
7839           return DW_FORM_block1;
7840         case 2:
7841           return DW_FORM_block2;
7842         case 4:
7843           return DW_FORM_block4;
7844         default:
7845           gcc_unreachable ();
7846         }
7847     case dw_val_class_flag:
7848       return DW_FORM_flag;
7849     case dw_val_class_die_ref:
7850       if (AT_ref_external (a))
7851         return DW_FORM_ref_addr;
7852       else
7853         return DW_FORM_ref;
7854     case dw_val_class_fde_ref:
7855       return DW_FORM_data;
7856     case dw_val_class_lbl_id:
7857       return DW_FORM_addr;
7858     case dw_val_class_lineptr:
7859     case dw_val_class_macptr:
7860       return DW_FORM_data;
7861     case dw_val_class_str:
7862       return AT_string_form (a);
7863     case dw_val_class_file:
7864       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7865         {
7866         case 1:
7867           return DW_FORM_data1;
7868         case 2:
7869           return DW_FORM_data2;
7870         case 4:
7871           return DW_FORM_data4;
7872         default:
7873           gcc_unreachable ();
7874         }
7875
7876     default:
7877       gcc_unreachable ();
7878     }
7879 }
7880
7881 /* Output the encoding of an attribute value.  */
7882
7883 static void
7884 output_value_format (dw_attr_ref a)
7885 {
7886   enum dwarf_form form = value_format (a);
7887
7888   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7889 }
7890
7891 /* Output the .debug_abbrev section which defines the DIE abbreviation
7892    table.  */
7893
7894 static void
7895 output_abbrev_section (void)
7896 {
7897   unsigned long abbrev_id;
7898
7899   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7900     {
7901       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7902       unsigned ix;
7903       dw_attr_ref a_attr;
7904
7905       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7906       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7907                                    dwarf_tag_name (abbrev->die_tag));
7908
7909       if (abbrev->die_child != NULL)
7910         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7911       else
7912         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7913
7914       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7915            ix++)
7916         {
7917           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7918                                        dwarf_attr_name (a_attr->dw_attr));
7919           output_value_format (a_attr);
7920         }
7921
7922       dw2_asm_output_data (1, 0, NULL);
7923       dw2_asm_output_data (1, 0, NULL);
7924     }
7925
7926   /* Terminate the table.  */
7927   dw2_asm_output_data (1, 0, NULL);
7928 }
7929
7930 /* Output a symbol we can use to refer to this DIE from another CU.  */
7931
7932 static inline void
7933 output_die_symbol (dw_die_ref die)
7934 {
7935   char *sym = die->die_symbol;
7936
7937   if (sym == 0)
7938     return;
7939
7940   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7941     /* We make these global, not weak; if the target doesn't support
7942        .linkonce, it doesn't support combining the sections, so debugging
7943        will break.  */
7944     targetm.asm_out.globalize_label (asm_out_file, sym);
7945
7946   ASM_OUTPUT_LABEL (asm_out_file, sym);
7947 }
7948
7949 /* Return a new location list, given the begin and end range, and the
7950    expression. gensym tells us whether to generate a new internal symbol for
7951    this location list node, which is done for the head of the list only.  */
7952
7953 static inline dw_loc_list_ref
7954 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7955               const char *section, unsigned int gensym)
7956 {
7957   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7958
7959   retlist->begin = begin;
7960   retlist->end = end;
7961   retlist->expr = expr;
7962   retlist->section = section;
7963   if (gensym)
7964     retlist->ll_symbol = gen_internal_sym ("LLST");
7965
7966   return retlist;
7967 }
7968
7969 /* Add a location description expression to a location list.  */
7970
7971 static inline void
7972 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7973                            const char *begin, const char *end,
7974                            const char *section)
7975 {
7976   dw_loc_list_ref *d;
7977
7978   /* Find the end of the chain.  */
7979   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7980     ;
7981
7982   /* Add a new location list node to the list.  */
7983   *d = new_loc_list (descr, begin, end, section, 0);
7984 }
7985
7986 /* Output the location list given to us.  */
7987
7988 static void
7989 output_loc_list (dw_loc_list_ref list_head)
7990 {
7991   dw_loc_list_ref curr = list_head;
7992
7993   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7994
7995   /* Walk the location list, and output each range + expression.  */
7996   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7997     {
7998       unsigned long size;
7999       /* Don't output an entry that starts and ends at the same address.  */
8000       if (strcmp (curr->begin, curr->end) == 0)
8001         continue;
8002       if (!have_multiple_function_sections)
8003         {
8004           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8005                                 "Location list begin address (%s)",
8006                                 list_head->ll_symbol);
8007           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8008                                 "Location list end address (%s)",
8009                                 list_head->ll_symbol);
8010         }
8011       else
8012         {
8013           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8014                                "Location list begin address (%s)",
8015                                list_head->ll_symbol);
8016           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8017                                "Location list end address (%s)",
8018                                list_head->ll_symbol);
8019         }
8020       size = size_of_locs (curr->expr);
8021
8022       /* Output the block length for this list of location operations.  */
8023       gcc_assert (size <= 0xffff);
8024       dw2_asm_output_data (2, size, "%s", "Location expression size");
8025
8026       output_loc_sequence (curr->expr);
8027     }
8028
8029   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8030                        "Location list terminator begin (%s)",
8031                        list_head->ll_symbol);
8032   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8033                        "Location list terminator end (%s)",
8034                        list_head->ll_symbol);
8035 }
8036
8037 /* Output the DIE and its attributes.  Called recursively to generate
8038    the definitions of each child DIE.  */
8039
8040 static void
8041 output_die (dw_die_ref die)
8042 {
8043   dw_attr_ref a;
8044   dw_die_ref c;
8045   unsigned long size;
8046   unsigned ix;
8047
8048   /* If someone in another CU might refer to us, set up a symbol for
8049      them to point to.  */
8050   if (die->die_symbol)
8051     output_die_symbol (die);
8052
8053   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8054                                (unsigned long)die->die_offset,
8055                                dwarf_tag_name (die->die_tag));
8056
8057   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8058     {
8059       const char *name = dwarf_attr_name (a->dw_attr);
8060
8061       switch (AT_class (a))
8062         {
8063         case dw_val_class_addr:
8064           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8065           break;
8066
8067         case dw_val_class_offset:
8068           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8069                                "%s", name);
8070           break;
8071
8072         case dw_val_class_range_list:
8073           {
8074             char *p = strchr (ranges_section_label, '\0');
8075
8076             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8077                      a->dw_attr_val.v.val_offset);
8078             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8079                                    debug_ranges_section, "%s", name);
8080             *p = '\0';
8081           }
8082           break;
8083
8084         case dw_val_class_loc:
8085           size = size_of_locs (AT_loc (a));
8086
8087           /* Output the block length for this list of location operations.  */
8088           dw2_asm_output_data (constant_size (size), size, "%s", name);
8089
8090           output_loc_sequence (AT_loc (a));
8091           break;
8092
8093         case dw_val_class_const:
8094           /* ??? It would be slightly more efficient to use a scheme like is
8095              used for unsigned constants below, but gdb 4.x does not sign
8096              extend.  Gdb 5.x does sign extend.  */
8097           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8098           break;
8099
8100         case dw_val_class_unsigned_const:
8101           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8102                                AT_unsigned (a), "%s", name);
8103           break;
8104
8105         case dw_val_class_long_long:
8106           {
8107             unsigned HOST_WIDE_INT first, second;
8108
8109             dw2_asm_output_data (1,
8110                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8111                                  "%s", name);
8112
8113             if (WORDS_BIG_ENDIAN)
8114               {
8115                 first = a->dw_attr_val.v.val_long_long.hi;
8116                 second = a->dw_attr_val.v.val_long_long.low;
8117               }
8118             else
8119               {
8120                 first = a->dw_attr_val.v.val_long_long.low;
8121                 second = a->dw_attr_val.v.val_long_long.hi;
8122               }
8123
8124             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8125                                  first, "long long constant");
8126             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8127                                  second, NULL);
8128           }
8129           break;
8130
8131         case dw_val_class_vec:
8132           {
8133             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8134             unsigned int len = a->dw_attr_val.v.val_vec.length;
8135             unsigned int i;
8136             unsigned char *p;
8137
8138             dw2_asm_output_data (constant_size (len * elt_size),
8139                                  len * elt_size, "%s", name);
8140             if (elt_size > sizeof (HOST_WIDE_INT))
8141               {
8142                 elt_size /= 2;
8143                 len *= 2;
8144               }
8145             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8146                  i < len;
8147                  i++, p += elt_size)
8148               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8149                                    "fp or vector constant word %u", i);
8150             break;
8151           }
8152
8153         case dw_val_class_flag:
8154           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8155           break;
8156
8157         case dw_val_class_loc_list:
8158           {
8159             char *sym = AT_loc_list (a)->ll_symbol;
8160
8161             gcc_assert (sym);
8162             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8163                                    "%s", name);
8164           }
8165           break;
8166
8167         case dw_val_class_die_ref:
8168           if (AT_ref_external (a))
8169             {
8170               char *sym = AT_ref (a)->die_symbol;
8171
8172               gcc_assert (sym);
8173               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8174                                      "%s", name);
8175             }
8176           else
8177             {
8178               gcc_assert (AT_ref (a)->die_offset);
8179               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8180                                    "%s", name);
8181             }
8182           break;
8183
8184         case dw_val_class_fde_ref:
8185           {
8186             char l1[20];
8187
8188             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8189                                          a->dw_attr_val.v.val_fde_index * 2);
8190             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8191                                    "%s", name);
8192           }
8193           break;
8194
8195         case dw_val_class_lbl_id:
8196           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8197           break;
8198
8199         case dw_val_class_lineptr:
8200           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8201                                  debug_line_section, "%s", name);
8202           break;
8203
8204         case dw_val_class_macptr:
8205           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8206                                  debug_macinfo_section, "%s", name);
8207           break;
8208
8209         case dw_val_class_str:
8210           if (AT_string_form (a) == DW_FORM_strp)
8211             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8212                                    a->dw_attr_val.v.val_str->label,
8213                                    debug_str_section,
8214                                    "%s: \"%s\"", name, AT_string (a));
8215           else
8216             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8217           break;
8218
8219         case dw_val_class_file:
8220           {
8221             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8222
8223             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8224                                  a->dw_attr_val.v.val_file->filename);
8225             break;
8226           }
8227
8228         default:
8229           gcc_unreachable ();
8230         }
8231     }
8232
8233   FOR_EACH_CHILD (die, c, output_die (c));
8234
8235   /* Add null byte to terminate sibling list.  */
8236   if (die->die_child != NULL)
8237     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8238                          (unsigned long) die->die_offset);
8239 }
8240
8241 /* Output the compilation unit that appears at the beginning of the
8242    .debug_info section, and precedes the DIE descriptions.  */
8243
8244 static void
8245 output_compilation_unit_header (void)
8246 {
8247   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8248     dw2_asm_output_data (4, 0xffffffff,
8249       "Initial length escape value indicating 64-bit DWARF extension");
8250   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8251                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8252                        "Length of Compilation Unit Info");
8253   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8254   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8255                          debug_abbrev_section,
8256                          "Offset Into Abbrev. Section");
8257   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8258 }
8259
8260 /* Output the compilation unit DIE and its children.  */
8261
8262 static void
8263 output_comp_unit (dw_die_ref die, int output_if_empty)
8264 {
8265   const char *secname;
8266   char *oldsym, *tmp;
8267
8268   /* Unless we are outputting main CU, we may throw away empty ones.  */
8269   if (!output_if_empty && die->die_child == NULL)
8270     return;
8271
8272   /* Even if there are no children of this DIE, we must output the information
8273      about the compilation unit.  Otherwise, on an empty translation unit, we
8274      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8275      will then complain when examining the file.  First mark all the DIEs in
8276      this CU so we know which get local refs.  */
8277   mark_dies (die);
8278
8279   build_abbrev_table (die);
8280
8281   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8282   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8283   calc_die_sizes (die);
8284
8285   oldsym = die->die_symbol;
8286   if (oldsym)
8287     {
8288       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8289
8290       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8291       secname = tmp;
8292       die->die_symbol = NULL;
8293       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8294     }
8295   else
8296     switch_to_section (debug_info_section);
8297
8298   /* Output debugging information.  */
8299   output_compilation_unit_header ();
8300   output_die (die);
8301
8302   /* Leave the marks on the main CU, so we can check them in
8303      output_pubnames.  */
8304   if (oldsym)
8305     {
8306       unmark_dies (die);
8307       die->die_symbol = oldsym;
8308     }
8309 }
8310
8311 /* Return the DWARF2/3 pubname associated with a decl.  */
8312
8313 static const char *
8314 dwarf2_name (tree decl, int scope)
8315 {
8316   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8317 }
8318
8319 /* Add a new entry to .debug_pubnames if appropriate.  */
8320
8321 static void
8322 add_pubname_string (const char *str, dw_die_ref die)
8323 {
8324   pubname_entry e;
8325
8326   e.die = die;
8327   e.name = xstrdup (str);
8328   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8329 }
8330
8331 static void
8332 add_pubname (tree decl, dw_die_ref die)
8333 {
8334
8335   if (TREE_PUBLIC (decl))
8336     add_pubname_string (dwarf2_name (decl, 1), die);
8337 }
8338
8339 /* Add a new entry to .debug_pubtypes if appropriate.  */
8340
8341 static void
8342 add_pubtype (tree decl, dw_die_ref die)
8343 {
8344   pubname_entry e;
8345
8346   e.name = NULL;
8347   if ((TREE_PUBLIC (decl)
8348        || die->die_parent == comp_unit_die)
8349       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8350     {
8351       e.die = die;
8352       if (TYPE_P (decl))
8353         {
8354           if (TYPE_NAME (decl))
8355             {
8356               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8357                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8358               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8359                        && DECL_NAME (TYPE_NAME (decl)))
8360                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8361               else
8362                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8363             }
8364         }
8365       else
8366         e.name = xstrdup (dwarf2_name (decl, 1));
8367
8368       /* If we don't have a name for the type, there's no point in adding
8369          it to the table.  */
8370       if (e.name && e.name[0] != '\0')
8371         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8372     }
8373 }
8374
8375 /* Output the public names table used to speed up access to externally
8376    visible names; or the public types table used to find type definitions.  */
8377
8378 static void
8379 output_pubnames (VEC (pubname_entry, gc) * names)
8380 {
8381   unsigned i;
8382   unsigned long pubnames_length = size_of_pubnames (names);
8383   pubname_ref pub;
8384
8385   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8386     dw2_asm_output_data (4, 0xffffffff,
8387       "Initial length escape value indicating 64-bit DWARF extension");
8388   if (names == pubname_table)
8389     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8390                          "Length of Public Names Info");
8391   else
8392     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8393                          "Length of Public Type Names Info");
8394   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8395   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8396                          debug_info_section,
8397                          "Offset of Compilation Unit Info");
8398   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8399                        "Compilation Unit Length");
8400
8401   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8402     {
8403       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8404       if (names == pubname_table)
8405         gcc_assert (pub->die->die_mark);
8406
8407       if (names != pubtype_table
8408           || pub->die->die_offset != 0
8409           || !flag_eliminate_unused_debug_types)
8410         {
8411           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8412                                "DIE offset");
8413
8414           dw2_asm_output_nstring (pub->name, -1, "external name");
8415         }
8416     }
8417
8418   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8419 }
8420
8421 /* Add a new entry to .debug_aranges if appropriate.  */
8422
8423 static void
8424 add_arange (tree decl, dw_die_ref die)
8425 {
8426   if (! DECL_SECTION_NAME (decl))
8427     return;
8428
8429   if (arange_table_in_use == arange_table_allocated)
8430     {
8431       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8432       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8433                                     arange_table_allocated);
8434       memset (arange_table + arange_table_in_use, 0,
8435               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8436     }
8437
8438   arange_table[arange_table_in_use++] = die;
8439 }
8440
8441 /* Output the information that goes into the .debug_aranges table.
8442    Namely, define the beginning and ending address range of the
8443    text section generated for this compilation unit.  */
8444
8445 static void
8446 output_aranges (void)
8447 {
8448   unsigned i;
8449   unsigned long aranges_length = size_of_aranges ();
8450
8451   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8452     dw2_asm_output_data (4, 0xffffffff,
8453       "Initial length escape value indicating 64-bit DWARF extension");
8454   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8455                        "Length of Address Ranges Info");
8456   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8457   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8458                          debug_info_section,
8459                          "Offset of Compilation Unit Info");
8460   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8461   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8462
8463   /* We need to align to twice the pointer size here.  */
8464   if (DWARF_ARANGES_PAD_SIZE)
8465     {
8466       /* Pad using a 2 byte words so that padding is correct for any
8467          pointer size.  */
8468       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8469                            2 * DWARF2_ADDR_SIZE);
8470       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8471         dw2_asm_output_data (2, 0, NULL);
8472     }
8473
8474   /* It is necessary not to output these entries if the sections were
8475      not used; if the sections were not used, the length will be 0 and
8476      the address may end up as 0 if the section is discarded by ld
8477      --gc-sections, leaving an invalid (0, 0) entry that can be
8478      confused with the terminator.  */
8479   if (text_section_used)
8480     {
8481       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8482       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8483                             text_section_label, "Length");
8484     }
8485   if (cold_text_section_used)
8486     {
8487       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8488                            "Address");
8489       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8490                             cold_text_section_label, "Length");
8491     }
8492
8493   for (i = 0; i < arange_table_in_use; i++)
8494     {
8495       dw_die_ref die = arange_table[i];
8496
8497       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8498       gcc_assert (die->die_mark);
8499
8500       if (die->die_tag == DW_TAG_subprogram)
8501         {
8502           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8503                                "Address");
8504           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8505                                 get_AT_low_pc (die), "Length");
8506         }
8507       else
8508         {
8509           /* A static variable; extract the symbol from DW_AT_location.
8510              Note that this code isn't currently hit, as we only emit
8511              aranges for functions (jason 9/23/99).  */
8512           dw_attr_ref a = get_AT (die, DW_AT_location);
8513           dw_loc_descr_ref loc;
8514
8515           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8516
8517           loc = AT_loc (a);
8518           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8519
8520           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8521                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8522           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8523                                get_AT_unsigned (die, DW_AT_byte_size),
8524                                "Length");
8525         }
8526     }
8527
8528   /* Output the terminator words.  */
8529   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8530   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8531 }
8532
8533 /* Add a new entry to .debug_ranges.  Return the offset at which it
8534    was placed.  */
8535
8536 static unsigned int
8537 add_ranges_num (int num)
8538 {
8539   unsigned int in_use = ranges_table_in_use;
8540
8541   if (in_use == ranges_table_allocated)
8542     {
8543       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8544       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8545                                     ranges_table_allocated);
8546       memset (ranges_table + ranges_table_in_use, 0,
8547               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8548     }
8549
8550   ranges_table[in_use].num = num;
8551   ranges_table_in_use = in_use + 1;
8552
8553   return in_use * 2 * DWARF2_ADDR_SIZE;
8554 }
8555
8556 /* Add a new entry to .debug_ranges corresponding to a block, or a
8557    range terminator if BLOCK is NULL.  */
8558
8559 static unsigned int
8560 add_ranges (const_tree block)
8561 {
8562   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8563 }
8564
8565 /* Add a new entry to .debug_ranges corresponding to a pair of
8566    labels.  */
8567
8568 static unsigned int
8569 add_ranges_by_labels (const char *begin, const char *end)
8570 {
8571   unsigned int in_use = ranges_by_label_in_use;
8572
8573   if (in_use == ranges_by_label_allocated)
8574     {
8575       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8576       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8577                                        ranges_by_label,
8578                                        ranges_by_label_allocated);
8579       memset (ranges_by_label + ranges_by_label_in_use, 0,
8580               RANGES_TABLE_INCREMENT
8581               * sizeof (struct dw_ranges_by_label_struct));
8582     }
8583
8584   ranges_by_label[in_use].begin = begin;
8585   ranges_by_label[in_use].end = end;
8586   ranges_by_label_in_use = in_use + 1;
8587
8588   return add_ranges_num (-(int)in_use - 1);
8589 }
8590
8591 static void
8592 output_ranges (void)
8593 {
8594   unsigned i;
8595   static const char *const start_fmt = "Offset 0x%x";
8596   const char *fmt = start_fmt;
8597
8598   for (i = 0; i < ranges_table_in_use; i++)
8599     {
8600       int block_num = ranges_table[i].num;
8601
8602       if (block_num > 0)
8603         {
8604           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8605           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8606
8607           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8608           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8609
8610           /* If all code is in the text section, then the compilation
8611              unit base address defaults to DW_AT_low_pc, which is the
8612              base of the text section.  */
8613           if (!have_multiple_function_sections)
8614             {
8615               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8616                                     text_section_label,
8617                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8618               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8619                                     text_section_label, NULL);
8620             }
8621
8622           /* Otherwise, the compilation unit base address is zero,
8623              which allows us to use absolute addresses, and not worry
8624              about whether the target supports cross-section
8625              arithmetic.  */
8626           else
8627             {
8628               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8629                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8630               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8631             }
8632
8633           fmt = NULL;
8634         }
8635
8636       /* Negative block_num stands for an index into ranges_by_label.  */
8637       else if (block_num < 0)
8638         {
8639           int lab_idx = - block_num - 1;
8640
8641           if (!have_multiple_function_sections)
8642             {
8643               gcc_unreachable ();
8644 #if 0
8645               /* If we ever use add_ranges_by_labels () for a single
8646                  function section, all we have to do is to take out
8647                  the #if 0 above.  */
8648               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8649                                     ranges_by_label[lab_idx].begin,
8650                                     text_section_label,
8651                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8652               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8653                                     ranges_by_label[lab_idx].end,
8654                                     text_section_label, NULL);
8655 #endif
8656             }
8657           else
8658             {
8659               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8660                                    ranges_by_label[lab_idx].begin,
8661                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8662               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8663                                    ranges_by_label[lab_idx].end,
8664                                    NULL);
8665             }
8666         }
8667       else
8668         {
8669           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8670           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8671           fmt = start_fmt;
8672         }
8673     }
8674 }
8675
8676 /* Data structure containing information about input files.  */
8677 struct file_info
8678 {
8679   const char *path;     /* Complete file name.  */
8680   const char *fname;    /* File name part.  */
8681   int length;           /* Length of entire string.  */
8682   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8683   int dir_idx;          /* Index in directory table.  */
8684 };
8685
8686 /* Data structure containing information about directories with source
8687    files.  */
8688 struct dir_info
8689 {
8690   const char *path;     /* Path including directory name.  */
8691   int length;           /* Path length.  */
8692   int prefix;           /* Index of directory entry which is a prefix.  */
8693   int count;            /* Number of files in this directory.  */
8694   int dir_idx;          /* Index of directory used as base.  */
8695 };
8696
8697 /* Callback function for file_info comparison.  We sort by looking at
8698    the directories in the path.  */
8699
8700 static int
8701 file_info_cmp (const void *p1, const void *p2)
8702 {
8703   const struct file_info *const s1 = (const struct file_info *) p1;
8704   const struct file_info *const s2 = (const struct file_info *) p2;
8705   const unsigned char *cp1;
8706   const unsigned char *cp2;
8707
8708   /* Take care of file names without directories.  We need to make sure that
8709      we return consistent values to qsort since some will get confused if
8710      we return the same value when identical operands are passed in opposite
8711      orders.  So if neither has a directory, return 0 and otherwise return
8712      1 or -1 depending on which one has the directory.  */
8713   if ((s1->path == s1->fname || s2->path == s2->fname))
8714     return (s2->path == s2->fname) - (s1->path == s1->fname);
8715
8716   cp1 = (const unsigned char *) s1->path;
8717   cp2 = (const unsigned char *) s2->path;
8718
8719   while (1)
8720     {
8721       ++cp1;
8722       ++cp2;
8723       /* Reached the end of the first path?  If so, handle like above.  */
8724       if ((cp1 == (const unsigned char *) s1->fname)
8725           || (cp2 == (const unsigned char *) s2->fname))
8726         return ((cp2 == (const unsigned char *) s2->fname)
8727                 - (cp1 == (const unsigned char *) s1->fname));
8728
8729       /* Character of current path component the same?  */
8730       else if (*cp1 != *cp2)
8731         return *cp1 - *cp2;
8732     }
8733 }
8734
8735 struct file_name_acquire_data
8736 {
8737   struct file_info *files;
8738   int used_files;
8739   int max_files;
8740 };
8741
8742 /* Traversal function for the hash table.  */
8743
8744 static int
8745 file_name_acquire (void ** slot, void *data)
8746 {
8747   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8748   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8749   struct file_info *fi;
8750   const char *f;
8751
8752   gcc_assert (fnad->max_files >= d->emitted_number);
8753
8754   if (! d->emitted_number)
8755     return 1;
8756
8757   gcc_assert (fnad->max_files != fnad->used_files);
8758
8759   fi = fnad->files + fnad->used_files++;
8760
8761   /* Skip all leading "./".  */
8762   f = d->filename;
8763   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8764     f += 2;
8765
8766   /* Create a new array entry.  */
8767   fi->path = f;
8768   fi->length = strlen (f);
8769   fi->file_idx = d;
8770
8771   /* Search for the file name part.  */
8772   f = strrchr (f, DIR_SEPARATOR);
8773 #if defined (DIR_SEPARATOR_2)
8774   {
8775     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8776
8777     if (g != NULL)
8778       {
8779         if (f == NULL || f < g)
8780           f = g;
8781       }
8782   }
8783 #endif
8784
8785   fi->fname = f == NULL ? fi->path : f + 1;
8786   return 1;
8787 }
8788
8789 /* Output the directory table and the file name table.  We try to minimize
8790    the total amount of memory needed.  A heuristic is used to avoid large
8791    slowdowns with many input files.  */
8792
8793 static void
8794 output_file_names (void)
8795 {
8796   struct file_name_acquire_data fnad;
8797   int numfiles;
8798   struct file_info *files;
8799   struct dir_info *dirs;
8800   int *saved;
8801   int *savehere;
8802   int *backmap;
8803   int ndirs;
8804   int idx_offset;
8805   int i;
8806   int idx;
8807
8808   if (!last_emitted_file)
8809     {
8810       dw2_asm_output_data (1, 0, "End directory table");
8811       dw2_asm_output_data (1, 0, "End file name table");
8812       return;
8813     }
8814
8815   numfiles = last_emitted_file->emitted_number;
8816
8817   /* Allocate the various arrays we need.  */
8818   files = XALLOCAVEC (struct file_info, numfiles);
8819   dirs = XALLOCAVEC (struct dir_info, numfiles);
8820
8821   fnad.files = files;
8822   fnad.used_files = 0;
8823   fnad.max_files = numfiles;
8824   htab_traverse (file_table, file_name_acquire, &fnad);
8825   gcc_assert (fnad.used_files == fnad.max_files);
8826
8827   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8828
8829   /* Find all the different directories used.  */
8830   dirs[0].path = files[0].path;
8831   dirs[0].length = files[0].fname - files[0].path;
8832   dirs[0].prefix = -1;
8833   dirs[0].count = 1;
8834   dirs[0].dir_idx = 0;
8835   files[0].dir_idx = 0;
8836   ndirs = 1;
8837
8838   for (i = 1; i < numfiles; i++)
8839     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8840         && memcmp (dirs[ndirs - 1].path, files[i].path,
8841                    dirs[ndirs - 1].length) == 0)
8842       {
8843         /* Same directory as last entry.  */
8844         files[i].dir_idx = ndirs - 1;
8845         ++dirs[ndirs - 1].count;
8846       }
8847     else
8848       {
8849         int j;
8850
8851         /* This is a new directory.  */
8852         dirs[ndirs].path = files[i].path;
8853         dirs[ndirs].length = files[i].fname - files[i].path;
8854         dirs[ndirs].count = 1;
8855         dirs[ndirs].dir_idx = ndirs;
8856         files[i].dir_idx = ndirs;
8857
8858         /* Search for a prefix.  */
8859         dirs[ndirs].prefix = -1;
8860         for (j = 0; j < ndirs; j++)
8861           if (dirs[j].length < dirs[ndirs].length
8862               && dirs[j].length > 1
8863               && (dirs[ndirs].prefix == -1
8864                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8865               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8866             dirs[ndirs].prefix = j;
8867
8868         ++ndirs;
8869       }
8870
8871   /* Now to the actual work.  We have to find a subset of the directories which
8872      allow expressing the file name using references to the directory table
8873      with the least amount of characters.  We do not do an exhaustive search
8874      where we would have to check out every combination of every single
8875      possible prefix.  Instead we use a heuristic which provides nearly optimal
8876      results in most cases and never is much off.  */
8877   saved = XALLOCAVEC (int, ndirs);
8878   savehere = XALLOCAVEC (int, ndirs);
8879
8880   memset (saved, '\0', ndirs * sizeof (saved[0]));
8881   for (i = 0; i < ndirs; i++)
8882     {
8883       int j;
8884       int total;
8885
8886       /* We can always save some space for the current directory.  But this
8887          does not mean it will be enough to justify adding the directory.  */
8888       savehere[i] = dirs[i].length;
8889       total = (savehere[i] - saved[i]) * dirs[i].count;
8890
8891       for (j = i + 1; j < ndirs; j++)
8892         {
8893           savehere[j] = 0;
8894           if (saved[j] < dirs[i].length)
8895             {
8896               /* Determine whether the dirs[i] path is a prefix of the
8897                  dirs[j] path.  */
8898               int k;
8899
8900               k = dirs[j].prefix;
8901               while (k != -1 && k != (int) i)
8902                 k = dirs[k].prefix;
8903
8904               if (k == (int) i)
8905                 {
8906                   /* Yes it is.  We can possibly save some memory by
8907                      writing the filenames in dirs[j] relative to
8908                      dirs[i].  */
8909                   savehere[j] = dirs[i].length;
8910                   total += (savehere[j] - saved[j]) * dirs[j].count;
8911                 }
8912             }
8913         }
8914
8915       /* Check whether we can save enough to justify adding the dirs[i]
8916          directory.  */
8917       if (total > dirs[i].length + 1)
8918         {
8919           /* It's worthwhile adding.  */
8920           for (j = i; j < ndirs; j++)
8921             if (savehere[j] > 0)
8922               {
8923                 /* Remember how much we saved for this directory so far.  */
8924                 saved[j] = savehere[j];
8925
8926                 /* Remember the prefix directory.  */
8927                 dirs[j].dir_idx = i;
8928               }
8929         }
8930     }
8931
8932   /* Emit the directory name table.  */
8933   idx = 1;
8934   idx_offset = dirs[0].length > 0 ? 1 : 0;
8935   for (i = 1 - idx_offset; i < ndirs; i++)
8936     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8937                             "Directory Entry: 0x%x", i + idx_offset);
8938
8939   dw2_asm_output_data (1, 0, "End directory table");
8940
8941   /* We have to emit them in the order of emitted_number since that's
8942      used in the debug info generation.  To do this efficiently we
8943      generate a back-mapping of the indices first.  */
8944   backmap = XALLOCAVEC (int, numfiles);
8945   for (i = 0; i < numfiles; i++)
8946     backmap[files[i].file_idx->emitted_number - 1] = i;
8947
8948   /* Now write all the file names.  */
8949   for (i = 0; i < numfiles; i++)
8950     {
8951       int file_idx = backmap[i];
8952       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8953
8954       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8955                               "File Entry: 0x%x", (unsigned) i + 1);
8956
8957       /* Include directory index.  */
8958       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8959
8960       /* Modification time.  */
8961       dw2_asm_output_data_uleb128 (0, NULL);
8962
8963       /* File length in bytes.  */
8964       dw2_asm_output_data_uleb128 (0, NULL);
8965     }
8966
8967   dw2_asm_output_data (1, 0, "End file name table");
8968 }
8969
8970
8971 /* Output the source line number correspondence information.  This
8972    information goes into the .debug_line section.  */
8973
8974 static void
8975 output_line_info (void)
8976 {
8977   char l1[20], l2[20], p1[20], p2[20];
8978   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8979   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8980   unsigned opc;
8981   unsigned n_op_args;
8982   unsigned long lt_index;
8983   unsigned long current_line;
8984   long line_offset;
8985   long line_delta;
8986   unsigned long current_file;
8987   unsigned long function;
8988
8989   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8990   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8991   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8992   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8993
8994   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8995     dw2_asm_output_data (4, 0xffffffff,
8996       "Initial length escape value indicating 64-bit DWARF extension");
8997   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8998                         "Length of Source Line Info");
8999   ASM_OUTPUT_LABEL (asm_out_file, l1);
9000
9001   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9002   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9003   ASM_OUTPUT_LABEL (asm_out_file, p1);
9004
9005   /* Define the architecture-dependent minimum instruction length (in
9006    bytes).  In this implementation of DWARF, this field is used for
9007    information purposes only.  Since GCC generates assembly language,
9008    we have no a priori knowledge of how many instruction bytes are
9009    generated for each source line, and therefore can use only the
9010    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9011    commands.  Accordingly, we fix this as `1', which is "correct
9012    enough" for all architectures, and don't let the target override.  */
9013   dw2_asm_output_data (1, 1,
9014                        "Minimum Instruction Length");
9015
9016   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9017                        "Default is_stmt_start flag");
9018   dw2_asm_output_data (1, DWARF_LINE_BASE,
9019                        "Line Base Value (Special Opcodes)");
9020   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9021                        "Line Range Value (Special Opcodes)");
9022   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9023                        "Special Opcode Base");
9024
9025   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9026     {
9027       switch (opc)
9028         {
9029         case DW_LNS_advance_pc:
9030         case DW_LNS_advance_line:
9031         case DW_LNS_set_file:
9032         case DW_LNS_set_column:
9033         case DW_LNS_fixed_advance_pc:
9034           n_op_args = 1;
9035           break;
9036         default:
9037           n_op_args = 0;
9038           break;
9039         }
9040
9041       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9042                            opc, n_op_args);
9043     }
9044
9045   /* Write out the information about the files we use.  */
9046   output_file_names ();
9047   ASM_OUTPUT_LABEL (asm_out_file, p2);
9048
9049   /* We used to set the address register to the first location in the text
9050      section here, but that didn't accomplish anything since we already
9051      have a line note for the opening brace of the first function.  */
9052
9053   /* Generate the line number to PC correspondence table, encoded as
9054      a series of state machine operations.  */
9055   current_file = 1;
9056   current_line = 1;
9057
9058   if (cfun && in_cold_section_p)
9059     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9060   else
9061     strcpy (prev_line_label, text_section_label);
9062   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9063     {
9064       dw_line_info_ref line_info = &line_info_table[lt_index];
9065
9066 #if 0
9067       /* Disable this optimization for now; GDB wants to see two line notes
9068          at the beginning of a function so it can find the end of the
9069          prologue.  */
9070
9071       /* Don't emit anything for redundant notes.  Just updating the
9072          address doesn't accomplish anything, because we already assume
9073          that anything after the last address is this line.  */
9074       if (line_info->dw_line_num == current_line
9075           && line_info->dw_file_num == current_file)
9076         continue;
9077 #endif
9078
9079       /* Emit debug info for the address of the current line.
9080
9081          Unfortunately, we have little choice here currently, and must always
9082          use the most general form.  GCC does not know the address delta
9083          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9084          attributes which will give an upper bound on the address range.  We
9085          could perhaps use length attributes to determine when it is safe to
9086          use DW_LNS_fixed_advance_pc.  */
9087
9088       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9089       if (0)
9090         {
9091           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9092           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9093                                "DW_LNS_fixed_advance_pc");
9094           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9095         }
9096       else
9097         {
9098           /* This can handle any delta.  This takes
9099              4+DWARF2_ADDR_SIZE bytes.  */
9100           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9101           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9102           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9103           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9104         }
9105
9106       strcpy (prev_line_label, line_label);
9107
9108       /* Emit debug info for the source file of the current line, if
9109          different from the previous line.  */
9110       if (line_info->dw_file_num != current_file)
9111         {
9112           current_file = line_info->dw_file_num;
9113           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9114           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9115         }
9116
9117       /* Emit debug info for the current line number, choosing the encoding
9118          that uses the least amount of space.  */
9119       if (line_info->dw_line_num != current_line)
9120         {
9121           line_offset = line_info->dw_line_num - current_line;
9122           line_delta = line_offset - DWARF_LINE_BASE;
9123           current_line = line_info->dw_line_num;
9124           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9125             /* This can handle deltas from -10 to 234, using the current
9126                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9127                takes 1 byte.  */
9128             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9129                                  "line %lu", current_line);
9130           else
9131             {
9132               /* This can handle any delta.  This takes at least 4 bytes,
9133                  depending on the value being encoded.  */
9134               dw2_asm_output_data (1, DW_LNS_advance_line,
9135                                    "advance to line %lu", current_line);
9136               dw2_asm_output_data_sleb128 (line_offset, NULL);
9137               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9138             }
9139         }
9140       else
9141         /* We still need to start a new row, so output a copy insn.  */
9142         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9143     }
9144
9145   /* Emit debug info for the address of the end of the function.  */
9146   if (0)
9147     {
9148       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9149                            "DW_LNS_fixed_advance_pc");
9150       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9151     }
9152   else
9153     {
9154       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9155       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9156       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9157       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9158     }
9159
9160   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9161   dw2_asm_output_data_uleb128 (1, NULL);
9162   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9163
9164   function = 0;
9165   current_file = 1;
9166   current_line = 1;
9167   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9168     {
9169       dw_separate_line_info_ref line_info
9170         = &separate_line_info_table[lt_index];
9171
9172 #if 0
9173       /* Don't emit anything for redundant notes.  */
9174       if (line_info->dw_line_num == current_line
9175           && line_info->dw_file_num == current_file
9176           && line_info->function == function)
9177         goto cont;
9178 #endif
9179
9180       /* Emit debug info for the address of the current line.  If this is
9181          a new function, or the first line of a function, then we need
9182          to handle it differently.  */
9183       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9184                                    lt_index);
9185       if (function != line_info->function)
9186         {
9187           function = line_info->function;
9188
9189           /* Set the address register to the first line in the function.  */
9190           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9191           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9192           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9193           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9194         }
9195       else
9196         {
9197           /* ??? See the DW_LNS_advance_pc comment above.  */
9198           if (0)
9199             {
9200               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9201                                    "DW_LNS_fixed_advance_pc");
9202               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9203             }
9204           else
9205             {
9206               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9207               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9208               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9209               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9210             }
9211         }
9212
9213       strcpy (prev_line_label, line_label);
9214
9215       /* Emit debug info for the source file of the current line, if
9216          different from the previous line.  */
9217       if (line_info->dw_file_num != current_file)
9218         {
9219           current_file = line_info->dw_file_num;
9220           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9221           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9222         }
9223
9224       /* Emit debug info for the current line number, choosing the encoding
9225          that uses the least amount of space.  */
9226       if (line_info->dw_line_num != current_line)
9227         {
9228           line_offset = line_info->dw_line_num - current_line;
9229           line_delta = line_offset - DWARF_LINE_BASE;
9230           current_line = line_info->dw_line_num;
9231           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9232             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9233                                  "line %lu", current_line);
9234           else
9235             {
9236               dw2_asm_output_data (1, DW_LNS_advance_line,
9237                                    "advance to line %lu", current_line);
9238               dw2_asm_output_data_sleb128 (line_offset, NULL);
9239               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9240             }
9241         }
9242       else
9243         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9244
9245 #if 0
9246     cont:
9247 #endif
9248
9249       lt_index++;
9250
9251       /* If we're done with a function, end its sequence.  */
9252       if (lt_index == separate_line_info_table_in_use
9253           || separate_line_info_table[lt_index].function != function)
9254         {
9255           current_file = 1;
9256           current_line = 1;
9257
9258           /* Emit debug info for the address of the end of the function.  */
9259           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9260           if (0)
9261             {
9262               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9263                                    "DW_LNS_fixed_advance_pc");
9264               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9265             }
9266           else
9267             {
9268               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9269               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9270               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9271               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9272             }
9273
9274           /* Output the marker for the end of this sequence.  */
9275           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9276           dw2_asm_output_data_uleb128 (1, NULL);
9277           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9278         }
9279     }
9280
9281   /* Output the marker for the end of the line number info.  */
9282   ASM_OUTPUT_LABEL (asm_out_file, l2);
9283 }
9284 \f
9285 /* Given a pointer to a tree node for some base type, return a pointer to
9286    a DIE that describes the given type.
9287
9288    This routine must only be called for GCC type nodes that correspond to
9289    Dwarf base (fundamental) types.  */
9290
9291 static dw_die_ref
9292 base_type_die (tree type)
9293 {
9294   dw_die_ref base_type_result;
9295   enum dwarf_type encoding;
9296
9297   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9298     return 0;
9299
9300   switch (TREE_CODE (type))
9301     {
9302     case INTEGER_TYPE:
9303       if (TYPE_STRING_FLAG (type))
9304         {
9305           if (TYPE_UNSIGNED (type))
9306             encoding = DW_ATE_unsigned_char;
9307           else
9308             encoding = DW_ATE_signed_char;
9309         }
9310       else if (TYPE_UNSIGNED (type))
9311         encoding = DW_ATE_unsigned;
9312       else
9313         encoding = DW_ATE_signed;
9314       break;
9315
9316     case REAL_TYPE:
9317       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9318         encoding = DW_ATE_decimal_float;
9319       else
9320         encoding = DW_ATE_float;
9321       break;
9322
9323     case FIXED_POINT_TYPE:
9324       if (TYPE_UNSIGNED (type))
9325         encoding = DW_ATE_unsigned_fixed;
9326       else
9327         encoding = DW_ATE_signed_fixed;
9328       break;
9329
9330       /* Dwarf2 doesn't know anything about complex ints, so use
9331          a user defined type for it.  */
9332     case COMPLEX_TYPE:
9333       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9334         encoding = DW_ATE_complex_float;
9335       else
9336         encoding = DW_ATE_lo_user;
9337       break;
9338
9339     case BOOLEAN_TYPE:
9340       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9341       encoding = DW_ATE_boolean;
9342       break;
9343
9344     default:
9345       /* No other TREE_CODEs are Dwarf fundamental types.  */
9346       gcc_unreachable ();
9347     }
9348
9349   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9350
9351   /* This probably indicates a bug.  */
9352   if (! TYPE_NAME (type))
9353     add_name_attribute (base_type_result, "__unknown__");
9354
9355   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9356                    int_size_in_bytes (type));
9357   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9358
9359   return base_type_result;
9360 }
9361
9362 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9363    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9364
9365 static inline int
9366 is_base_type (tree type)
9367 {
9368   switch (TREE_CODE (type))
9369     {
9370     case ERROR_MARK:
9371     case VOID_TYPE:
9372     case INTEGER_TYPE:
9373     case REAL_TYPE:
9374     case FIXED_POINT_TYPE:
9375     case COMPLEX_TYPE:
9376     case BOOLEAN_TYPE:
9377       return 1;
9378
9379     case ARRAY_TYPE:
9380     case RECORD_TYPE:
9381     case UNION_TYPE:
9382     case QUAL_UNION_TYPE:
9383     case ENUMERAL_TYPE:
9384     case FUNCTION_TYPE:
9385     case METHOD_TYPE:
9386     case POINTER_TYPE:
9387     case REFERENCE_TYPE:
9388     case OFFSET_TYPE:
9389     case LANG_TYPE:
9390     case VECTOR_TYPE:
9391       return 0;
9392
9393     default:
9394       gcc_unreachable ();
9395     }
9396
9397   return 0;
9398 }
9399
9400 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9401    node, return the size in bits for the type if it is a constant, or else
9402    return the alignment for the type if the type's size is not constant, or
9403    else return BITS_PER_WORD if the type actually turns out to be an
9404    ERROR_MARK node.  */
9405
9406 static inline unsigned HOST_WIDE_INT
9407 simple_type_size_in_bits (const_tree type)
9408 {
9409   if (TREE_CODE (type) == ERROR_MARK)
9410     return BITS_PER_WORD;
9411   else if (TYPE_SIZE (type) == NULL_TREE)
9412     return 0;
9413   else if (host_integerp (TYPE_SIZE (type), 1))
9414     return tree_low_cst (TYPE_SIZE (type), 1);
9415   else
9416     return TYPE_ALIGN (type);
9417 }
9418
9419 /* Return true if the debug information for the given type should be
9420    emitted as a subrange type.  */
9421
9422 static inline bool
9423 is_subrange_type (const_tree type)
9424 {
9425   tree subtype = TREE_TYPE (type);
9426
9427   /* Subrange types are identified by the fact that they are integer
9428      types, and that they have a subtype which is either an integer type
9429      or an enumeral type.  */
9430
9431   if (TREE_CODE (type) != INTEGER_TYPE
9432       || subtype == NULL_TREE)
9433     return false;
9434
9435   if (TREE_CODE (subtype) != INTEGER_TYPE
9436       && TREE_CODE (subtype) != ENUMERAL_TYPE
9437       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9438     return false;
9439
9440   if (TREE_CODE (type) == TREE_CODE (subtype)
9441       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9442       && TYPE_MIN_VALUE (type) != NULL
9443       && TYPE_MIN_VALUE (subtype) != NULL
9444       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9445       && TYPE_MAX_VALUE (type) != NULL
9446       && TYPE_MAX_VALUE (subtype) != NULL
9447       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9448     {
9449       /* The type and its subtype have the same representation.  If in
9450          addition the two types also have the same name, then the given
9451          type is not a subrange type, but rather a plain base type.  */
9452       /* FIXME: brobecker/2004-03-22:
9453          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9454          therefore be sufficient to check the TYPE_SIZE node pointers
9455          rather than checking the actual size.  Unfortunately, we have
9456          found some cases, such as in the Ada "integer" type, where
9457          this is not the case.  Until this problem is solved, we need to
9458          keep checking the actual size.  */
9459       tree type_name = TYPE_NAME (type);
9460       tree subtype_name = TYPE_NAME (subtype);
9461
9462       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9463         type_name = DECL_NAME (type_name);
9464
9465       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9466         subtype_name = DECL_NAME (subtype_name);
9467
9468       if (type_name == subtype_name)
9469         return false;
9470     }
9471
9472   return true;
9473 }
9474
9475 /*  Given a pointer to a tree node for a subrange type, return a pointer
9476     to a DIE that describes the given type.  */
9477
9478 static dw_die_ref
9479 subrange_type_die (tree type, dw_die_ref context_die)
9480 {
9481   dw_die_ref subrange_die;
9482   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9483
9484   if (context_die == NULL)
9485     context_die = comp_unit_die;
9486
9487   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9488
9489   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9490     {
9491       /* The size of the subrange type and its base type do not match,
9492          so we need to generate a size attribute for the subrange type.  */
9493       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9494     }
9495
9496   if (TYPE_MIN_VALUE (type) != NULL)
9497     add_bound_info (subrange_die, DW_AT_lower_bound,
9498                     TYPE_MIN_VALUE (type));
9499   if (TYPE_MAX_VALUE (type) != NULL)
9500     add_bound_info (subrange_die, DW_AT_upper_bound,
9501                     TYPE_MAX_VALUE (type));
9502
9503   return subrange_die;
9504 }
9505
9506 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9507    entry that chains various modifiers in front of the given type.  */
9508
9509 static dw_die_ref
9510 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9511                    dw_die_ref context_die)
9512 {
9513   enum tree_code code = TREE_CODE (type);
9514   dw_die_ref mod_type_die;
9515   dw_die_ref sub_die = NULL;
9516   tree item_type = NULL;
9517   tree qualified_type;
9518   tree name;
9519
9520   if (code == ERROR_MARK)
9521     return NULL;
9522
9523   /* See if we already have the appropriately qualified variant of
9524      this type.  */
9525   qualified_type
9526     = get_qualified_type (type,
9527                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9528                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9529
9530   /* If we do, then we can just use its DIE, if it exists.  */
9531   if (qualified_type)
9532     {
9533       mod_type_die = lookup_type_die (qualified_type);
9534       if (mod_type_die)
9535         return mod_type_die;
9536     }
9537
9538   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9539
9540   /* Handle C typedef types.  */
9541   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9542     {
9543       tree dtype = TREE_TYPE (name);
9544
9545       if (qualified_type == dtype)
9546         {
9547           /* For a named type, use the typedef.  */
9548           gen_type_die (qualified_type, context_die);
9549           return lookup_type_die (qualified_type);
9550         }
9551       else if (is_const_type < TYPE_READONLY (dtype)
9552                || is_volatile_type < TYPE_VOLATILE (dtype)
9553                || (is_const_type <= TYPE_READONLY (dtype)
9554                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9555                    && DECL_ORIGINAL_TYPE (name) != type))
9556         /* cv-unqualified version of named type.  Just use the unnamed
9557            type to which it refers.  */
9558         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9559                                   is_const_type, is_volatile_type,
9560                                   context_die);
9561       /* Else cv-qualified version of named type; fall through.  */
9562     }
9563
9564   if (is_const_type)
9565     {
9566       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9567       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9568     }
9569   else if (is_volatile_type)
9570     {
9571       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9572       sub_die = modified_type_die (type, 0, 0, context_die);
9573     }
9574   else if (code == POINTER_TYPE)
9575     {
9576       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9577       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9578                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9579       item_type = TREE_TYPE (type);
9580     }
9581   else if (code == REFERENCE_TYPE)
9582     {
9583       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9584       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9585                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9586       item_type = TREE_TYPE (type);
9587     }
9588   else if (is_subrange_type (type))
9589     {
9590       mod_type_die = subrange_type_die (type, context_die);
9591       item_type = TREE_TYPE (type);
9592     }
9593   else if (is_base_type (type))
9594     mod_type_die = base_type_die (type);
9595   else
9596     {
9597       gen_type_die (type, context_die);
9598
9599       /* We have to get the type_main_variant here (and pass that to the
9600          `lookup_type_die' routine) because the ..._TYPE node we have
9601          might simply be a *copy* of some original type node (where the
9602          copy was created to help us keep track of typedef names) and
9603          that copy might have a different TYPE_UID from the original
9604          ..._TYPE node.  */
9605       if (TREE_CODE (type) != VECTOR_TYPE)
9606         return lookup_type_die (type_main_variant (type));
9607       else
9608         /* Vectors have the debugging information in the type,
9609            not the main variant.  */
9610         return lookup_type_die (type);
9611     }
9612
9613   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9614      don't output a DW_TAG_typedef, since there isn't one in the
9615      user's program; just attach a DW_AT_name to the type.  */
9616   if (name
9617       && (TREE_CODE (name) != TYPE_DECL
9618           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9619     {
9620       if (TREE_CODE (name) == TYPE_DECL)
9621         /* Could just call add_name_and_src_coords_attributes here,
9622            but since this is a builtin type it doesn't have any
9623            useful source coordinates anyway.  */
9624         name = DECL_NAME (name);
9625       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9626     }
9627
9628   if (qualified_type)
9629     equate_type_number_to_die (qualified_type, mod_type_die);
9630
9631   if (item_type)
9632     /* We must do this after the equate_type_number_to_die call, in case
9633        this is a recursive type.  This ensures that the modified_type_die
9634        recursion will terminate even if the type is recursive.  Recursive
9635        types are possible in Ada.  */
9636     sub_die = modified_type_die (item_type,
9637                                  TYPE_READONLY (item_type),
9638                                  TYPE_VOLATILE (item_type),
9639                                  context_die);
9640
9641   if (sub_die != NULL)
9642     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9643
9644   return mod_type_die;
9645 }
9646
9647 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9648    an enumerated type.  */
9649
9650 static inline int
9651 type_is_enum (const_tree type)
9652 {
9653   return TREE_CODE (type) == ENUMERAL_TYPE;
9654 }
9655
9656 /* Return the DBX register number described by a given RTL node.  */
9657
9658 static unsigned int
9659 dbx_reg_number (const_rtx rtl)
9660 {
9661   unsigned regno = REGNO (rtl);
9662
9663   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9664
9665 #ifdef LEAF_REG_REMAP
9666   if (current_function_uses_only_leaf_regs)
9667     {
9668       int leaf_reg = LEAF_REG_REMAP (regno);
9669       if (leaf_reg != -1)
9670         regno = (unsigned) leaf_reg;
9671     }
9672 #endif
9673
9674   return DBX_REGISTER_NUMBER (regno);
9675 }
9676
9677 /* Optionally add a DW_OP_piece term to a location description expression.
9678    DW_OP_piece is only added if the location description expression already
9679    doesn't end with DW_OP_piece.  */
9680
9681 static void
9682 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9683 {
9684   dw_loc_descr_ref loc;
9685
9686   if (*list_head != NULL)
9687     {
9688       /* Find the end of the chain.  */
9689       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9690         ;
9691
9692       if (loc->dw_loc_opc != DW_OP_piece)
9693         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9694     }
9695 }
9696
9697 /* Return a location descriptor that designates a machine register or
9698    zero if there is none.  */
9699
9700 static dw_loc_descr_ref
9701 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9702 {
9703   rtx regs;
9704
9705   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9706     return 0;
9707
9708   regs = targetm.dwarf_register_span (rtl);
9709
9710   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9711     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9712   else
9713     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9714 }
9715
9716 /* Return a location descriptor that designates a machine register for
9717    a given hard register number.  */
9718
9719 static dw_loc_descr_ref
9720 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9721 {
9722   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9723
9724   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9725     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9726
9727   return reg_loc_descr;
9728 }
9729
9730 /* Given an RTL of a register, return a location descriptor that
9731    designates a value that spans more than one register.  */
9732
9733 static dw_loc_descr_ref
9734 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9735                              enum var_init_status initialized)
9736 {
9737   int nregs, size, i;
9738   unsigned reg;
9739   dw_loc_descr_ref loc_result = NULL;
9740
9741   reg = REGNO (rtl);
9742 #ifdef LEAF_REG_REMAP
9743   if (current_function_uses_only_leaf_regs)
9744     {
9745       int leaf_reg = LEAF_REG_REMAP (reg);
9746       if (leaf_reg != -1)
9747         reg = (unsigned) leaf_reg;
9748     }
9749 #endif
9750   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9751   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9752
9753   /* Simple, contiguous registers.  */
9754   if (regs == NULL_RTX)
9755     {
9756       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9757
9758       loc_result = NULL;
9759       while (nregs--)
9760         {
9761           dw_loc_descr_ref t;
9762
9763           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9764                                       VAR_INIT_STATUS_INITIALIZED);
9765           add_loc_descr (&loc_result, t);
9766           add_loc_descr_op_piece (&loc_result, size);
9767           ++reg;
9768         }
9769       return loc_result;
9770     }
9771
9772   /* Now onto stupid register sets in non contiguous locations.  */
9773
9774   gcc_assert (GET_CODE (regs) == PARALLEL);
9775
9776   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9777   loc_result = NULL;
9778
9779   for (i = 0; i < XVECLEN (regs, 0); ++i)
9780     {
9781       dw_loc_descr_ref t;
9782
9783       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9784                                   VAR_INIT_STATUS_INITIALIZED);
9785       add_loc_descr (&loc_result, t);
9786       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9787       add_loc_descr_op_piece (&loc_result, size);
9788     }
9789
9790   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9791     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9792   return loc_result;
9793 }
9794
9795 #endif /* DWARF2_DEBUGGING_INFO */
9796
9797 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9798
9799 /* Return a location descriptor that designates a constant.  */
9800
9801 static dw_loc_descr_ref
9802 int_loc_descriptor (HOST_WIDE_INT i)
9803 {
9804   enum dwarf_location_atom op;
9805
9806   /* Pick the smallest representation of a constant, rather than just
9807      defaulting to the LEB encoding.  */
9808   if (i >= 0)
9809     {
9810       if (i <= 31)
9811         op = DW_OP_lit0 + i;
9812       else if (i <= 0xff)
9813         op = DW_OP_const1u;
9814       else if (i <= 0xffff)
9815         op = DW_OP_const2u;
9816       else if (HOST_BITS_PER_WIDE_INT == 32
9817                || i <= 0xffffffff)
9818         op = DW_OP_const4u;
9819       else
9820         op = DW_OP_constu;
9821     }
9822   else
9823     {
9824       if (i >= -0x80)
9825         op = DW_OP_const1s;
9826       else if (i >= -0x8000)
9827         op = DW_OP_const2s;
9828       else if (HOST_BITS_PER_WIDE_INT == 32
9829                || i >= -0x80000000)
9830         op = DW_OP_const4s;
9831       else
9832         op = DW_OP_consts;
9833     }
9834
9835   return new_loc_descr (op, i, 0);
9836 }
9837 #endif
9838
9839 #ifdef DWARF2_DEBUGGING_INFO
9840
9841 /* Return a location descriptor that designates a base+offset location.  */
9842
9843 static dw_loc_descr_ref
9844 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9845                  enum var_init_status initialized)
9846 {
9847   unsigned int regno;
9848   dw_loc_descr_ref result;
9849   dw_fde_ref fde = current_fde ();
9850
9851   /* We only use "frame base" when we're sure we're talking about the
9852      post-prologue local stack frame.  We do this by *not* running
9853      register elimination until this point, and recognizing the special
9854      argument pointer and soft frame pointer rtx's.  */
9855   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9856     {
9857       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9858
9859       if (elim != reg)
9860         {
9861           if (GET_CODE (elim) == PLUS)
9862             {
9863               offset += INTVAL (XEXP (elim, 1));
9864               elim = XEXP (elim, 0);
9865             }
9866           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9867                        && (elim == hard_frame_pointer_rtx
9868                            || elim == stack_pointer_rtx))
9869                       || elim == (frame_pointer_needed
9870                                   ? hard_frame_pointer_rtx
9871                                   : stack_pointer_rtx));
9872
9873           /* If drap register is used to align stack, use frame
9874              pointer + offset to access stack variables.  If stack
9875              is aligned without drap, use stack pointer + offset to
9876              access stack variables.  */
9877           if (crtl->stack_realign_tried
9878               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9879               && reg == frame_pointer_rtx)
9880             {
9881               int base_reg
9882                 = DWARF_FRAME_REGNUM (cfa.indirect
9883                                       ? HARD_FRAME_POINTER_REGNUM
9884                                       : STACK_POINTER_REGNUM);
9885               return new_reg_loc_descr (base_reg, offset);
9886             }
9887
9888           offset += frame_pointer_fb_offset;
9889           return new_loc_descr (DW_OP_fbreg, offset, 0);
9890         }
9891     }
9892   else if (fde
9893            && fde->drap_reg != INVALID_REGNUM
9894            && (fde->drap_reg == REGNO (reg)
9895                || fde->vdrap_reg == REGNO (reg)))
9896     {
9897       /* Use cfa+offset to represent the location of arguments passed
9898          on stack when drap is used to align stack.  */
9899       return new_loc_descr (DW_OP_fbreg, offset, 0);
9900     }
9901
9902   regno = dbx_reg_number (reg);
9903   if (regno <= 31)
9904     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9905   else
9906     result = new_loc_descr (DW_OP_bregx, regno, offset);
9907
9908   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9909     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9910
9911   return result;
9912 }
9913
9914 /* Return true if this RTL expression describes a base+offset calculation.  */
9915
9916 static inline int
9917 is_based_loc (const_rtx rtl)
9918 {
9919   return (GET_CODE (rtl) == PLUS
9920           && ((REG_P (XEXP (rtl, 0))
9921                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9922                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9923 }
9924
9925 /* Return a descriptor that describes the concatenation of N locations
9926    used to form the address of a memory location.  */
9927
9928 static dw_loc_descr_ref
9929 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9930                             enum var_init_status initialized)
9931 {
9932   unsigned int i;
9933   dw_loc_descr_ref cc_loc_result = NULL;
9934   unsigned int n = XVECLEN (concatn, 0);
9935
9936   for (i = 0; i < n; ++i)
9937     {
9938       dw_loc_descr_ref ref;
9939       rtx x = XVECEXP (concatn, 0, i);
9940
9941       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9942       if (ref == NULL)
9943         return NULL;
9944
9945       add_loc_descr (&cc_loc_result, ref);
9946       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9947     }
9948
9949   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9950     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9951
9952   return cc_loc_result;
9953 }
9954
9955 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9956    failed.  */
9957
9958 static dw_loc_descr_ref
9959 tls_mem_loc_descriptor (rtx mem)
9960 {
9961   tree base;
9962   dw_loc_descr_ref loc_result, loc_result2;
9963
9964   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9965     return NULL;
9966
9967   base = get_base_address (MEM_EXPR (mem));
9968   if (base == NULL
9969       || TREE_CODE (base) != VAR_DECL
9970       || !DECL_THREAD_LOCAL_P (base))
9971     return NULL;
9972
9973   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9974   if (loc_result == NULL)
9975     return NULL;
9976
9977   if (INTVAL (MEM_OFFSET (mem)))
9978     {
9979       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9980         add_loc_descr (&loc_result,
9981                        new_loc_descr (DW_OP_plus_uconst,
9982                                       INTVAL (MEM_OFFSET (mem)), 0));
9983       else
9984         {
9985           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9986                                             VAR_INIT_STATUS_INITIALIZED);
9987           if (loc_result2 == 0)
9988             return NULL;
9989           add_loc_descr (&loc_result, loc_result2);
9990           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9991         }
9992     }
9993
9994   return loc_result;
9995 }
9996
9997 /* The following routine converts the RTL for a variable or parameter
9998    (resident in memory) into an equivalent Dwarf representation of a
9999    mechanism for getting the address of that same variable onto the top of a
10000    hypothetical "address evaluation" stack.
10001
10002    When creating memory location descriptors, we are effectively transforming
10003    the RTL for a memory-resident object into its Dwarf postfix expression
10004    equivalent.  This routine recursively descends an RTL tree, turning
10005    it into Dwarf postfix code as it goes.
10006
10007    MODE is the mode of the memory reference, needed to handle some
10008    autoincrement addressing modes.
10009
10010    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10011    location list for RTL.
10012
10013    Return 0 if we can't represent the location.  */
10014
10015 static dw_loc_descr_ref
10016 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10017                     enum var_init_status initialized)
10018 {
10019   dw_loc_descr_ref mem_loc_result = NULL;
10020   enum dwarf_location_atom op;
10021
10022   /* Note that for a dynamically sized array, the location we will generate a
10023      description of here will be the lowest numbered location which is
10024      actually within the array.  That's *not* necessarily the same as the
10025      zeroth element of the array.  */
10026
10027   rtl = targetm.delegitimize_address (rtl);
10028
10029   switch (GET_CODE (rtl))
10030     {
10031     case POST_INC:
10032     case POST_DEC:
10033     case POST_MODIFY:
10034       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10035          just fall into the SUBREG code.  */
10036
10037       /* ... fall through ...  */
10038
10039     case SUBREG:
10040       /* The case of a subreg may arise when we have a local (register)
10041          variable or a formal (register) parameter which doesn't quite fill
10042          up an entire register.  For now, just assume that it is
10043          legitimate to make the Dwarf info refer to the whole register which
10044          contains the given subreg.  */
10045       rtl = XEXP (rtl, 0);
10046
10047       /* ... fall through ...  */
10048
10049     case REG:
10050       /* Whenever a register number forms a part of the description of the
10051          method for calculating the (dynamic) address of a memory resident
10052          object, DWARF rules require the register number be referred to as
10053          a "base register".  This distinction is not based in any way upon
10054          what category of register the hardware believes the given register
10055          belongs to.  This is strictly DWARF terminology we're dealing with
10056          here. Note that in cases where the location of a memory-resident
10057          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10058          OP_CONST (0)) the actual DWARF location descriptor that we generate
10059          may just be OP_BASEREG (basereg).  This may look deceptively like
10060          the object in question was allocated to a register (rather than in
10061          memory) so DWARF consumers need to be aware of the subtle
10062          distinction between OP_REG and OP_BASEREG.  */
10063       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10064         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10065       else if (stack_realign_drap
10066                && crtl->drap_reg
10067                && crtl->args.internal_arg_pointer == rtl
10068                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10069         {
10070           /* If RTL is internal_arg_pointer, which has been optimized
10071              out, use DRAP instead.  */
10072           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10073                                             VAR_INIT_STATUS_INITIALIZED);
10074         }
10075       break;
10076
10077     case MEM:
10078       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10079                                            VAR_INIT_STATUS_INITIALIZED);
10080       if (mem_loc_result == NULL)
10081         mem_loc_result = tls_mem_loc_descriptor (rtl);
10082       if (mem_loc_result != 0)
10083         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10084       break;
10085
10086     case LO_SUM:
10087          rtl = XEXP (rtl, 1);
10088
10089       /* ... fall through ...  */
10090
10091     case LABEL_REF:
10092       /* Some ports can transform a symbol ref into a label ref, because
10093          the symbol ref is too far away and has to be dumped into a constant
10094          pool.  */
10095     case CONST:
10096     case SYMBOL_REF:
10097       /* Alternatively, the symbol in the constant pool might be referenced
10098          by a different symbol.  */
10099       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10100         {
10101           bool marked;
10102           rtx tmp = get_pool_constant_mark (rtl, &marked);
10103
10104           if (GET_CODE (tmp) == SYMBOL_REF)
10105             {
10106               rtl = tmp;
10107               if (CONSTANT_POOL_ADDRESS_P (tmp))
10108                 get_pool_constant_mark (tmp, &marked);
10109               else
10110                 marked = true;
10111             }
10112
10113           /* If all references to this pool constant were optimized away,
10114              it was not output and thus we can't represent it.
10115              FIXME: might try to use DW_OP_const_value here, though
10116              DW_OP_piece complicates it.  */
10117           if (!marked)
10118             return 0;
10119         }
10120
10121       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10122       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10123       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10124       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10125       break;
10126
10127     case PRE_MODIFY:
10128       /* Extract the PLUS expression nested inside and fall into
10129          PLUS code below.  */
10130       rtl = XEXP (rtl, 1);
10131       goto plus;
10132
10133     case PRE_INC:
10134     case PRE_DEC:
10135       /* Turn these into a PLUS expression and fall into the PLUS code
10136          below.  */
10137       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10138                           GEN_INT (GET_CODE (rtl) == PRE_INC
10139                                    ? GET_MODE_UNIT_SIZE (mode)
10140                                    : -GET_MODE_UNIT_SIZE (mode)));
10141
10142       /* ... fall through ...  */
10143
10144     case PLUS:
10145     plus:
10146       if (is_based_loc (rtl))
10147         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10148                                           INTVAL (XEXP (rtl, 1)),
10149                                           VAR_INIT_STATUS_INITIALIZED);
10150       else
10151         {
10152           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10153                                                VAR_INIT_STATUS_INITIALIZED);
10154           if (mem_loc_result == 0)
10155             break;
10156
10157           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10158               && INTVAL (XEXP (rtl, 1)) >= 0)
10159             add_loc_descr (&mem_loc_result,
10160                            new_loc_descr (DW_OP_plus_uconst,
10161                                           INTVAL (XEXP (rtl, 1)), 0));
10162           else
10163             {
10164               dw_loc_descr_ref mem_loc_result2
10165                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10166                                       VAR_INIT_STATUS_INITIALIZED);
10167               if (mem_loc_result2 == 0)
10168                 break;
10169               add_loc_descr (&mem_loc_result, mem_loc_result2);
10170               add_loc_descr (&mem_loc_result,
10171                              new_loc_descr (DW_OP_plus, 0, 0));
10172             }
10173         }
10174       break;
10175
10176     /* If a pseudo-reg is optimized away, it is possible for it to
10177        be replaced with a MEM containing a multiply or shift.  */
10178     case MULT:
10179       op = DW_OP_mul;
10180       goto do_binop;
10181
10182     case ASHIFT:
10183       op = DW_OP_shl;
10184       goto do_binop;
10185
10186     case ASHIFTRT:
10187       op = DW_OP_shra;
10188       goto do_binop;
10189
10190     case LSHIFTRT:
10191       op = DW_OP_shr;
10192       goto do_binop;
10193
10194     do_binop:
10195       {
10196         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10197                                                    VAR_INIT_STATUS_INITIALIZED);
10198         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10199                                                    VAR_INIT_STATUS_INITIALIZED);
10200
10201         if (op0 == 0 || op1 == 0)
10202           break;
10203
10204         mem_loc_result = op0;
10205         add_loc_descr (&mem_loc_result, op1);
10206         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10207         break;
10208       }
10209
10210     case CONST_INT:
10211       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10212       break;
10213
10214     case CONCATN:
10215       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10216                                                    VAR_INIT_STATUS_INITIALIZED);
10217       break;
10218
10219     case UNSPEC:
10220       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10221          can't express it in the debug info.  This can happen e.g. with some
10222          TLS UNSPECs.  */
10223       break;
10224
10225     default:
10226       gcc_unreachable ();
10227     }
10228
10229   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10230     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10231
10232   return mem_loc_result;
10233 }
10234
10235 /* Return a descriptor that describes the concatenation of two locations.
10236    This is typically a complex variable.  */
10237
10238 static dw_loc_descr_ref
10239 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10240 {
10241   dw_loc_descr_ref cc_loc_result = NULL;
10242   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10243   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10244
10245   if (x0_ref == 0 || x1_ref == 0)
10246     return 0;
10247
10248   cc_loc_result = x0_ref;
10249   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10250
10251   add_loc_descr (&cc_loc_result, x1_ref);
10252   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10253
10254   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10255     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10256
10257   return cc_loc_result;
10258 }
10259
10260 /* Return a descriptor that describes the concatenation of N
10261    locations.  */
10262
10263 static dw_loc_descr_ref
10264 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10265 {
10266   unsigned int i;
10267   dw_loc_descr_ref cc_loc_result = NULL;
10268   unsigned int n = XVECLEN (concatn, 0);
10269
10270   for (i = 0; i < n; ++i)
10271     {
10272       dw_loc_descr_ref ref;
10273       rtx x = XVECEXP (concatn, 0, i);
10274
10275       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10276       if (ref == NULL)
10277         return NULL;
10278
10279       add_loc_descr (&cc_loc_result, ref);
10280       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10281     }
10282
10283   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10284     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10285
10286   return cc_loc_result;
10287 }
10288
10289 /* Output a proper Dwarf location descriptor for a variable or parameter
10290    which is either allocated in a register or in a memory location.  For a
10291    register, we just generate an OP_REG and the register number.  For a
10292    memory location we provide a Dwarf postfix expression describing how to
10293    generate the (dynamic) address of the object onto the address stack.
10294
10295    If we don't know how to describe it, return 0.  */
10296
10297 static dw_loc_descr_ref
10298 loc_descriptor (rtx rtl, enum var_init_status initialized)
10299 {
10300   dw_loc_descr_ref loc_result = NULL;
10301
10302   switch (GET_CODE (rtl))
10303     {
10304     case SUBREG:
10305       /* The case of a subreg may arise when we have a local (register)
10306          variable or a formal (register) parameter which doesn't quite fill
10307          up an entire register.  For now, just assume that it is
10308          legitimate to make the Dwarf info refer to the whole register which
10309          contains the given subreg.  */
10310       rtl = SUBREG_REG (rtl);
10311
10312       /* ... fall through ...  */
10313
10314     case REG:
10315       loc_result = reg_loc_descriptor (rtl, initialized);
10316       break;
10317
10318     case MEM:
10319       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10320                                        initialized);
10321       if (loc_result == NULL)
10322         loc_result = tls_mem_loc_descriptor (rtl);
10323       break;
10324
10325     case CONCAT:
10326       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10327                                           initialized);
10328       break;
10329
10330     case CONCATN:
10331       loc_result = concatn_loc_descriptor (rtl, initialized);
10332       break;
10333
10334     case VAR_LOCATION:
10335       /* Single part.  */
10336       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10337         {
10338           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10339           break;
10340         }
10341
10342       rtl = XEXP (rtl, 1);
10343       /* FALLTHRU */
10344
10345     case PARALLEL:
10346       {
10347         rtvec par_elems = XVEC (rtl, 0);
10348         int num_elem = GET_NUM_ELEM (par_elems);
10349         enum machine_mode mode;
10350         int i;
10351
10352         /* Create the first one, so we have something to add to.  */
10353         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10354                                      initialized);
10355         if (loc_result == NULL)
10356           return NULL;
10357         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10358         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10359         for (i = 1; i < num_elem; i++)
10360           {
10361             dw_loc_descr_ref temp;
10362
10363             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10364                                    initialized);
10365             if (temp == NULL)
10366               return NULL;
10367             add_loc_descr (&loc_result, temp);
10368             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10369             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10370           }
10371       }
10372       break;
10373
10374     default:
10375       gcc_unreachable ();
10376     }
10377
10378   return loc_result;
10379 }
10380
10381 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10382    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10383    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10384    top-level invocation, and we require the address of LOC; is 0 if we require
10385    the value of LOC.  */
10386
10387 static dw_loc_descr_ref
10388 loc_descriptor_from_tree_1 (tree loc, int want_address)
10389 {
10390   dw_loc_descr_ref ret, ret1;
10391   int have_address = 0;
10392   enum dwarf_location_atom op;
10393
10394   /* ??? Most of the time we do not take proper care for sign/zero
10395      extending the values properly.  Hopefully this won't be a real
10396      problem...  */
10397
10398   switch (TREE_CODE (loc))
10399     {
10400     case ERROR_MARK:
10401       return 0;
10402
10403     case PLACEHOLDER_EXPR:
10404       /* This case involves extracting fields from an object to determine the
10405          position of other fields.  We don't try to encode this here.  The
10406          only user of this is Ada, which encodes the needed information using
10407          the names of types.  */
10408       return 0;
10409
10410     case CALL_EXPR:
10411       return 0;
10412
10413     case PREINCREMENT_EXPR:
10414     case PREDECREMENT_EXPR:
10415     case POSTINCREMENT_EXPR:
10416     case POSTDECREMENT_EXPR:
10417       /* There are no opcodes for these operations.  */
10418       return 0;
10419
10420     case ADDR_EXPR:
10421       /* If we already want an address, there's nothing we can do.  */
10422       if (want_address)
10423         return 0;
10424
10425       /* Otherwise, process the argument and look for the address.  */
10426       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10427
10428     case VAR_DECL:
10429       if (DECL_THREAD_LOCAL_P (loc))
10430         {
10431           rtx rtl;
10432           unsigned first_op;
10433           unsigned second_op;
10434
10435           if (targetm.have_tls)
10436             {
10437               /* If this is not defined, we have no way to emit the
10438                  data.  */
10439               if (!targetm.asm_out.output_dwarf_dtprel)
10440                 return 0;
10441
10442                /* The way DW_OP_GNU_push_tls_address is specified, we
10443                   can only look up addresses of objects in the current
10444                   module.  */
10445               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10446                 return 0;
10447               first_op = INTERNAL_DW_OP_tls_addr;
10448               second_op = DW_OP_GNU_push_tls_address;
10449             }
10450           else
10451             {
10452               if (!targetm.emutls.debug_form_tls_address)
10453                 return 0;
10454               loc = emutls_decl (loc);
10455               first_op = DW_OP_addr;
10456               second_op = DW_OP_form_tls_address;
10457             }
10458
10459           rtl = rtl_for_decl_location (loc);
10460           if (rtl == NULL_RTX)
10461             return 0;
10462
10463           if (!MEM_P (rtl))
10464             return 0;
10465           rtl = XEXP (rtl, 0);
10466           if (! CONSTANT_P (rtl))
10467             return 0;
10468
10469           ret = new_loc_descr (first_op, 0, 0);
10470           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10471           ret->dw_loc_oprnd1.v.val_addr = rtl;
10472
10473           ret1 = new_loc_descr (second_op, 0, 0);
10474           add_loc_descr (&ret, ret1);
10475
10476           have_address = 1;
10477           break;
10478         }
10479       /* FALLTHRU */
10480
10481     case PARM_DECL:
10482       if (DECL_HAS_VALUE_EXPR_P (loc))
10483         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10484                                            want_address);
10485       /* FALLTHRU */
10486
10487     case RESULT_DECL:
10488     case FUNCTION_DECL:
10489       {
10490         rtx rtl = rtl_for_decl_location (loc);
10491
10492         if (rtl == NULL_RTX)
10493           return 0;
10494         else if (GET_CODE (rtl) == CONST_INT)
10495           {
10496             HOST_WIDE_INT val = INTVAL (rtl);
10497             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10498               val &= GET_MODE_MASK (DECL_MODE (loc));
10499             ret = int_loc_descriptor (val);
10500           }
10501         else if (GET_CODE (rtl) == CONST_STRING)
10502           return 0;
10503         else if (CONSTANT_P (rtl))
10504           {
10505             ret = new_loc_descr (DW_OP_addr, 0, 0);
10506             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10507             ret->dw_loc_oprnd1.v.val_addr = rtl;
10508           }
10509         else
10510           {
10511             enum machine_mode mode;
10512
10513             /* Certain constructs can only be represented at top-level.  */
10514             if (want_address == 2)
10515               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10516
10517             mode = GET_MODE (rtl);
10518             if (MEM_P (rtl))
10519               {
10520                 rtl = XEXP (rtl, 0);
10521                 have_address = 1;
10522               }
10523             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10524           }
10525       }
10526       break;
10527
10528     case INDIRECT_REF:
10529       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10530       have_address = 1;
10531       break;
10532
10533     case COMPOUND_EXPR:
10534       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10535
10536     CASE_CONVERT:
10537     case VIEW_CONVERT_EXPR:
10538     case SAVE_EXPR:
10539     case MODIFY_EXPR:
10540       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10541
10542     case COMPONENT_REF:
10543     case BIT_FIELD_REF:
10544     case ARRAY_REF:
10545     case ARRAY_RANGE_REF:
10546       {
10547         tree obj, offset;
10548         HOST_WIDE_INT bitsize, bitpos, bytepos;
10549         enum machine_mode mode;
10550         int volatilep;
10551         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10552
10553         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10554                                    &unsignedp, &volatilep, false);
10555
10556         if (obj == loc)
10557           return 0;
10558
10559         ret = loc_descriptor_from_tree_1 (obj, 1);
10560         if (ret == 0
10561             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10562           return 0;
10563
10564         if (offset != NULL_TREE)
10565           {
10566             /* Variable offset.  */
10567             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10568             if (ret1 == 0)
10569               return 0;
10570             add_loc_descr (&ret, ret1);
10571             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10572           }
10573
10574         bytepos = bitpos / BITS_PER_UNIT;
10575         if (bytepos > 0)
10576           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10577         else if (bytepos < 0)
10578           {
10579             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10580             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10581           }
10582
10583         have_address = 1;
10584         break;
10585       }
10586
10587     case INTEGER_CST:
10588       if (host_integerp (loc, 0))
10589         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10590       else
10591         return 0;
10592       break;
10593
10594     case CONSTRUCTOR:
10595       {
10596         /* Get an RTL for this, if something has been emitted.  */
10597         rtx rtl = lookup_constant_def (loc);
10598         enum machine_mode mode;
10599
10600         if (!rtl || !MEM_P (rtl))
10601           return 0;
10602         mode = GET_MODE (rtl);
10603         rtl = XEXP (rtl, 0);
10604         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10605         have_address = 1;
10606         break;
10607       }
10608
10609     case TRUTH_AND_EXPR:
10610     case TRUTH_ANDIF_EXPR:
10611     case BIT_AND_EXPR:
10612       op = DW_OP_and;
10613       goto do_binop;
10614
10615     case TRUTH_XOR_EXPR:
10616     case BIT_XOR_EXPR:
10617       op = DW_OP_xor;
10618       goto do_binop;
10619
10620     case TRUTH_OR_EXPR:
10621     case TRUTH_ORIF_EXPR:
10622     case BIT_IOR_EXPR:
10623       op = DW_OP_or;
10624       goto do_binop;
10625
10626     case FLOOR_DIV_EXPR:
10627     case CEIL_DIV_EXPR:
10628     case ROUND_DIV_EXPR:
10629     case TRUNC_DIV_EXPR:
10630       op = DW_OP_div;
10631       goto do_binop;
10632
10633     case MINUS_EXPR:
10634       op = DW_OP_minus;
10635       goto do_binop;
10636
10637     case FLOOR_MOD_EXPR:
10638     case CEIL_MOD_EXPR:
10639     case ROUND_MOD_EXPR:
10640     case TRUNC_MOD_EXPR:
10641       op = DW_OP_mod;
10642       goto do_binop;
10643
10644     case MULT_EXPR:
10645       op = DW_OP_mul;
10646       goto do_binop;
10647
10648     case LSHIFT_EXPR:
10649       op = DW_OP_shl;
10650       goto do_binop;
10651
10652     case RSHIFT_EXPR:
10653       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10654       goto do_binop;
10655
10656     case POINTER_PLUS_EXPR:
10657     case PLUS_EXPR:
10658       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10659           && host_integerp (TREE_OPERAND (loc, 1), 0))
10660         {
10661           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10662           if (ret == 0)
10663             return 0;
10664
10665           add_loc_descr (&ret,
10666                          new_loc_descr (DW_OP_plus_uconst,
10667                                         tree_low_cst (TREE_OPERAND (loc, 1),
10668                                                       0),
10669                                         0));
10670           break;
10671         }
10672
10673       op = DW_OP_plus;
10674       goto do_binop;
10675
10676     case LE_EXPR:
10677       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10678         return 0;
10679
10680       op = DW_OP_le;
10681       goto do_binop;
10682
10683     case GE_EXPR:
10684       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10685         return 0;
10686
10687       op = DW_OP_ge;
10688       goto do_binop;
10689
10690     case LT_EXPR:
10691       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10692         return 0;
10693
10694       op = DW_OP_lt;
10695       goto do_binop;
10696
10697     case GT_EXPR:
10698       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10699         return 0;
10700
10701       op = DW_OP_gt;
10702       goto do_binop;
10703
10704     case EQ_EXPR:
10705       op = DW_OP_eq;
10706       goto do_binop;
10707
10708     case NE_EXPR:
10709       op = DW_OP_ne;
10710       goto do_binop;
10711
10712     do_binop:
10713       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10714       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10715       if (ret == 0 || ret1 == 0)
10716         return 0;
10717
10718       add_loc_descr (&ret, ret1);
10719       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10720       break;
10721
10722     case TRUTH_NOT_EXPR:
10723     case BIT_NOT_EXPR:
10724       op = DW_OP_not;
10725       goto do_unop;
10726
10727     case ABS_EXPR:
10728       op = DW_OP_abs;
10729       goto do_unop;
10730
10731     case NEGATE_EXPR:
10732       op = DW_OP_neg;
10733       goto do_unop;
10734
10735     do_unop:
10736       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10737       if (ret == 0)
10738         return 0;
10739
10740       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10741       break;
10742
10743     case MIN_EXPR:
10744     case MAX_EXPR:
10745       {
10746         const enum tree_code code =
10747           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10748
10749         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10750                       build2 (code, integer_type_node,
10751                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10752                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10753       }
10754
10755       /* ... fall through ...  */
10756
10757     case COND_EXPR:
10758       {
10759         dw_loc_descr_ref lhs
10760           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10761         dw_loc_descr_ref rhs
10762           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10763         dw_loc_descr_ref bra_node, jump_node, tmp;
10764
10765         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10766         if (ret == 0 || lhs == 0 || rhs == 0)
10767           return 0;
10768
10769         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10770         add_loc_descr (&ret, bra_node);
10771
10772         add_loc_descr (&ret, rhs);
10773         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10774         add_loc_descr (&ret, jump_node);
10775
10776         add_loc_descr (&ret, lhs);
10777         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10778         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10779
10780         /* ??? Need a node to point the skip at.  Use a nop.  */
10781         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10782         add_loc_descr (&ret, tmp);
10783         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10784         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10785       }
10786       break;
10787
10788     case FIX_TRUNC_EXPR:
10789       return 0;
10790
10791     default:
10792       /* Leave front-end specific codes as simply unknown.  This comes
10793          up, for instance, with the C STMT_EXPR.  */
10794       if ((unsigned int) TREE_CODE (loc)
10795           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10796         return 0;
10797
10798 #ifdef ENABLE_CHECKING
10799       /* Otherwise this is a generic code; we should just lists all of
10800          these explicitly.  We forgot one.  */
10801       gcc_unreachable ();
10802 #else
10803       /* In a release build, we want to degrade gracefully: better to
10804          generate incomplete debugging information than to crash.  */
10805       return NULL;
10806 #endif
10807     }
10808
10809   /* Show if we can't fill the request for an address.  */
10810   if (want_address && !have_address)
10811     return 0;
10812
10813   /* If we've got an address and don't want one, dereference.  */
10814   if (!want_address && have_address && ret)
10815     {
10816       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10817
10818       if (size > DWARF2_ADDR_SIZE || size == -1)
10819         return 0;
10820       else if (size == DWARF2_ADDR_SIZE)
10821         op = DW_OP_deref;
10822       else
10823         op = DW_OP_deref_size;
10824
10825       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10826     }
10827
10828   return ret;
10829 }
10830
10831 static inline dw_loc_descr_ref
10832 loc_descriptor_from_tree (tree loc)
10833 {
10834   return loc_descriptor_from_tree_1 (loc, 2);
10835 }
10836
10837 /* Given a value, round it up to the lowest multiple of `boundary'
10838    which is not less than the value itself.  */
10839
10840 static inline HOST_WIDE_INT
10841 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10842 {
10843   return (((value + boundary - 1) / boundary) * boundary);
10844 }
10845
10846 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10847    pointer to the declared type for the relevant field variable, or return
10848    `integer_type_node' if the given node turns out to be an
10849    ERROR_MARK node.  */
10850
10851 static inline tree
10852 field_type (const_tree decl)
10853 {
10854   tree type;
10855
10856   if (TREE_CODE (decl) == ERROR_MARK)
10857     return integer_type_node;
10858
10859   type = DECL_BIT_FIELD_TYPE (decl);
10860   if (type == NULL_TREE)
10861     type = TREE_TYPE (decl);
10862
10863   return type;
10864 }
10865
10866 /* Given a pointer to a tree node, return the alignment in bits for
10867    it, or else return BITS_PER_WORD if the node actually turns out to
10868    be an ERROR_MARK node.  */
10869
10870 static inline unsigned
10871 simple_type_align_in_bits (const_tree type)
10872 {
10873   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10874 }
10875
10876 static inline unsigned
10877 simple_decl_align_in_bits (const_tree decl)
10878 {
10879   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10880 }
10881
10882 /* Return the result of rounding T up to ALIGN.  */
10883
10884 static inline HOST_WIDE_INT
10885 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10886 {
10887   /* We must be careful if T is negative because HOST_WIDE_INT can be
10888      either "above" or "below" unsigned int as per the C promotion
10889      rules, depending on the host, thus making the signedness of the
10890      direct multiplication and division unpredictable.  */
10891   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10892
10893   u += align - 1;
10894   u /= align;
10895   u *= align;
10896
10897   return (HOST_WIDE_INT) u;
10898 }
10899
10900 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10901    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10902    or return 0 if we are unable to determine what that offset is, either
10903    because the argument turns out to be a pointer to an ERROR_MARK node, or
10904    because the offset is actually variable.  (We can't handle the latter case
10905    just yet).  */
10906
10907 static HOST_WIDE_INT
10908 field_byte_offset (const_tree decl)
10909 {
10910   HOST_WIDE_INT object_offset_in_bits;
10911   HOST_WIDE_INT bitpos_int;
10912
10913   if (TREE_CODE (decl) == ERROR_MARK)
10914     return 0;
10915
10916   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10917
10918   /* We cannot yet cope with fields whose positions are variable, so
10919      for now, when we see such things, we simply return 0.  Someday, we may
10920      be able to handle such cases, but it will be damn difficult.  */
10921   if (! host_integerp (bit_position (decl), 0))
10922     return 0;
10923
10924   bitpos_int = int_bit_position (decl);
10925
10926 #ifdef PCC_BITFIELD_TYPE_MATTERS
10927   if (PCC_BITFIELD_TYPE_MATTERS)
10928     {
10929       tree type;
10930       tree field_size_tree;
10931       HOST_WIDE_INT deepest_bitpos;
10932       unsigned HOST_WIDE_INT field_size_in_bits;
10933       unsigned int type_align_in_bits;
10934       unsigned int decl_align_in_bits;
10935       unsigned HOST_WIDE_INT type_size_in_bits;
10936
10937       type = field_type (decl);
10938       field_size_tree = DECL_SIZE (decl);
10939
10940       /* The size could be unspecified if there was an error, or for
10941          a flexible array member.  */
10942       if (! field_size_tree)
10943         field_size_tree = bitsize_zero_node;
10944
10945       /* If we don't know the size of the field, pretend it's a full word.  */
10946       if (host_integerp (field_size_tree, 1))
10947         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10948       else
10949         field_size_in_bits = BITS_PER_WORD;
10950
10951       type_size_in_bits = simple_type_size_in_bits (type);
10952       type_align_in_bits = simple_type_align_in_bits (type);
10953       decl_align_in_bits = simple_decl_align_in_bits (decl);
10954
10955       /* The GCC front-end doesn't make any attempt to keep track of the
10956          starting bit offset (relative to the start of the containing
10957          structure type) of the hypothetical "containing object" for a
10958          bit-field.  Thus, when computing the byte offset value for the
10959          start of the "containing object" of a bit-field, we must deduce
10960          this information on our own. This can be rather tricky to do in
10961          some cases.  For example, handling the following structure type
10962          definition when compiling for an i386/i486 target (which only
10963          aligns long long's to 32-bit boundaries) can be very tricky:
10964
10965          struct S { int field1; long long field2:31; };
10966
10967          Fortunately, there is a simple rule-of-thumb which can be used
10968          in such cases.  When compiling for an i386/i486, GCC will
10969          allocate 8 bytes for the structure shown above.  It decides to
10970          do this based upon one simple rule for bit-field allocation.
10971          GCC allocates each "containing object" for each bit-field at
10972          the first (i.e. lowest addressed) legitimate alignment boundary
10973          (based upon the required minimum alignment for the declared
10974          type of the field) which it can possibly use, subject to the
10975          condition that there is still enough available space remaining
10976          in the containing object (when allocated at the selected point)
10977          to fully accommodate all of the bits of the bit-field itself.
10978
10979          This simple rule makes it obvious why GCC allocates 8 bytes for
10980          each object of the structure type shown above.  When looking
10981          for a place to allocate the "containing object" for `field2',
10982          the compiler simply tries to allocate a 64-bit "containing
10983          object" at each successive 32-bit boundary (starting at zero)
10984          until it finds a place to allocate that 64- bit field such that
10985          at least 31 contiguous (and previously unallocated) bits remain
10986          within that selected 64 bit field.  (As it turns out, for the
10987          example above, the compiler finds it is OK to allocate the
10988          "containing object" 64-bit field at bit-offset zero within the
10989          structure type.)
10990
10991          Here we attempt to work backwards from the limited set of facts
10992          we're given, and we try to deduce from those facts, where GCC
10993          must have believed that the containing object started (within
10994          the structure type). The value we deduce is then used (by the
10995          callers of this routine) to generate DW_AT_location and
10996          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10997          the case of DW_AT_location, regular fields as well).  */
10998
10999       /* Figure out the bit-distance from the start of the structure to
11000          the "deepest" bit of the bit-field.  */
11001       deepest_bitpos = bitpos_int + field_size_in_bits;
11002
11003       /* This is the tricky part.  Use some fancy footwork to deduce
11004          where the lowest addressed bit of the containing object must
11005          be.  */
11006       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11007
11008       /* Round up to type_align by default.  This works best for
11009          bitfields.  */
11010       object_offset_in_bits
11011         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11012
11013       if (object_offset_in_bits > bitpos_int)
11014         {
11015           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11016
11017           /* Round up to decl_align instead.  */
11018           object_offset_in_bits
11019             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11020         }
11021     }
11022   else
11023 #endif
11024     object_offset_in_bits = bitpos_int;
11025
11026   return object_offset_in_bits / BITS_PER_UNIT;
11027 }
11028 \f
11029 /* The following routines define various Dwarf attributes and any data
11030    associated with them.  */
11031
11032 /* Add a location description attribute value to a DIE.
11033
11034    This emits location attributes suitable for whole variables and
11035    whole parameters.  Note that the location attributes for struct fields are
11036    generated by the routine `data_member_location_attribute' below.  */
11037
11038 static inline void
11039 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11040                              dw_loc_descr_ref descr)
11041 {
11042   if (descr != 0)
11043     add_AT_loc (die, attr_kind, descr);
11044 }
11045
11046 /* Attach the specialized form of location attribute used for data members of
11047    struct and union types.  In the special case of a FIELD_DECL node which
11048    represents a bit-field, the "offset" part of this special location
11049    descriptor must indicate the distance in bytes from the lowest-addressed
11050    byte of the containing struct or union type to the lowest-addressed byte of
11051    the "containing object" for the bit-field.  (See the `field_byte_offset'
11052    function above).
11053
11054    For any given bit-field, the "containing object" is a hypothetical object
11055    (of some integral or enum type) within which the given bit-field lives.  The
11056    type of this hypothetical "containing object" is always the same as the
11057    declared type of the individual bit-field itself (for GCC anyway... the
11058    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11059    bytes) of the hypothetical "containing object" which will be given in the
11060    DW_AT_byte_size attribute for this bit-field.  (See the
11061    `byte_size_attribute' function below.)  It is also used when calculating the
11062    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11063    function below.)  */
11064
11065 static void
11066 add_data_member_location_attribute (dw_die_ref die, tree decl)
11067 {
11068   HOST_WIDE_INT offset;
11069   dw_loc_descr_ref loc_descr = 0;
11070
11071   if (TREE_CODE (decl) == TREE_BINFO)
11072     {
11073       /* We're working on the TAG_inheritance for a base class.  */
11074       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11075         {
11076           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11077              aren't at a fixed offset from all (sub)objects of the same
11078              type.  We need to extract the appropriate offset from our
11079              vtable.  The following dwarf expression means
11080
11081                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11082
11083              This is specific to the V3 ABI, of course.  */
11084
11085           dw_loc_descr_ref tmp;
11086
11087           /* Make a copy of the object address.  */
11088           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11089           add_loc_descr (&loc_descr, tmp);
11090
11091           /* Extract the vtable address.  */
11092           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11093           add_loc_descr (&loc_descr, tmp);
11094
11095           /* Calculate the address of the offset.  */
11096           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11097           gcc_assert (offset < 0);
11098
11099           tmp = int_loc_descriptor (-offset);
11100           add_loc_descr (&loc_descr, tmp);
11101           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11102           add_loc_descr (&loc_descr, tmp);
11103
11104           /* Extract the offset.  */
11105           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11106           add_loc_descr (&loc_descr, tmp);
11107
11108           /* Add it to the object address.  */
11109           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11110           add_loc_descr (&loc_descr, tmp);
11111         }
11112       else
11113         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11114     }
11115   else
11116     offset = field_byte_offset (decl);
11117
11118   if (! loc_descr)
11119     {
11120       enum dwarf_location_atom op;
11121
11122       /* The DWARF2 standard says that we should assume that the structure
11123          address is already on the stack, so we can specify a structure field
11124          address by using DW_OP_plus_uconst.  */
11125
11126 #ifdef MIPS_DEBUGGING_INFO
11127       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11128          operator correctly.  It works only if we leave the offset on the
11129          stack.  */
11130       op = DW_OP_constu;
11131 #else
11132       op = DW_OP_plus_uconst;
11133 #endif
11134
11135       loc_descr = new_loc_descr (op, offset, 0);
11136     }
11137
11138   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11139 }
11140
11141 /* Writes integer values to dw_vec_const array.  */
11142
11143 static void
11144 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11145 {
11146   while (size != 0)
11147     {
11148       *dest++ = val & 0xff;
11149       val >>= 8;
11150       --size;
11151     }
11152 }
11153
11154 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11155
11156 static HOST_WIDE_INT
11157 extract_int (const unsigned char *src, unsigned int size)
11158 {
11159   HOST_WIDE_INT val = 0;
11160
11161   src += size;
11162   while (size != 0)
11163     {
11164       val <<= 8;
11165       val |= *--src & 0xff;
11166       --size;
11167     }
11168   return val;
11169 }
11170
11171 /* Writes floating point values to dw_vec_const array.  */
11172
11173 static void
11174 insert_float (const_rtx rtl, unsigned char *array)
11175 {
11176   REAL_VALUE_TYPE rv;
11177   long val[4];
11178   int i;
11179
11180   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11181   real_to_target (val, &rv, GET_MODE (rtl));
11182
11183   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11184   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11185     {
11186       insert_int (val[i], 4, array);
11187       array += 4;
11188     }
11189 }
11190
11191 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11192    does not have a "location" either in memory or in a register.  These
11193    things can arise in GNU C when a constant is passed as an actual parameter
11194    to an inlined function.  They can also arise in C++ where declared
11195    constants do not necessarily get memory "homes".  */
11196
11197 static void
11198 add_const_value_attribute (dw_die_ref die, rtx rtl)
11199 {
11200   switch (GET_CODE (rtl))
11201     {
11202     case CONST_INT:
11203       {
11204         HOST_WIDE_INT val = INTVAL (rtl);
11205
11206         if (val < 0)
11207           add_AT_int (die, DW_AT_const_value, val);
11208         else
11209           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11210       }
11211       break;
11212
11213     case CONST_DOUBLE:
11214       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11215          floating-point constant.  A CONST_DOUBLE is used whenever the
11216          constant requires more than one word in order to be adequately
11217          represented.  We output CONST_DOUBLEs as blocks.  */
11218       {
11219         enum machine_mode mode = GET_MODE (rtl);
11220
11221         if (SCALAR_FLOAT_MODE_P (mode))
11222           {
11223             unsigned int length = GET_MODE_SIZE (mode);
11224             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11225
11226             insert_float (rtl, array);
11227             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11228           }
11229         else
11230           {
11231             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11232             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11233
11234             add_AT_long_long (die, DW_AT_const_value,
11235                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11236           }
11237       }
11238       break;
11239
11240     case CONST_VECTOR:
11241       {
11242         enum machine_mode mode = GET_MODE (rtl);
11243         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11244         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11245         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11246         unsigned int i;
11247         unsigned char *p;
11248
11249         switch (GET_MODE_CLASS (mode))
11250           {
11251           case MODE_VECTOR_INT:
11252             for (i = 0, p = array; i < length; i++, p += elt_size)
11253               {
11254                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11255                 HOST_WIDE_INT lo, hi;
11256
11257                 switch (GET_CODE (elt))
11258                   {
11259                   case CONST_INT:
11260                     lo = INTVAL (elt);
11261                     hi = -(lo < 0);
11262                     break;
11263
11264                   case CONST_DOUBLE:
11265                     lo = CONST_DOUBLE_LOW (elt);
11266                     hi = CONST_DOUBLE_HIGH (elt);
11267                     break;
11268
11269                   default:
11270                     gcc_unreachable ();
11271                   }
11272
11273                 if (elt_size <= sizeof (HOST_WIDE_INT))
11274                   insert_int (lo, elt_size, p);
11275                 else
11276                   {
11277                     unsigned char *p0 = p;
11278                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11279
11280                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11281                     if (WORDS_BIG_ENDIAN)
11282                       {
11283                         p0 = p1;
11284                         p1 = p;
11285                       }
11286                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11287                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11288                   }
11289               }
11290             break;
11291
11292           case MODE_VECTOR_FLOAT:
11293             for (i = 0, p = array; i < length; i++, p += elt_size)
11294               {
11295                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11296                 insert_float (elt, p);
11297               }
11298             break;
11299
11300           default:
11301             gcc_unreachable ();
11302           }
11303
11304         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11305       }
11306       break;
11307
11308     case CONST_STRING:
11309       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11310       break;
11311
11312     case SYMBOL_REF:
11313     case LABEL_REF:
11314     case CONST:
11315       add_AT_addr (die, DW_AT_const_value, rtl);
11316       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11317       break;
11318
11319     case PLUS:
11320       /* In cases where an inlined instance of an inline function is passed
11321          the address of an `auto' variable (which is local to the caller) we
11322          can get a situation where the DECL_RTL of the artificial local
11323          variable (for the inlining) which acts as a stand-in for the
11324          corresponding formal parameter (of the inline function) will look
11325          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11326          exactly a compile-time constant expression, but it isn't the address
11327          of the (artificial) local variable either.  Rather, it represents the
11328          *value* which the artificial local variable always has during its
11329          lifetime.  We currently have no way to represent such quasi-constant
11330          values in Dwarf, so for now we just punt and generate nothing.  */
11331       break;
11332
11333     default:
11334       /* No other kinds of rtx should be possible here.  */
11335       gcc_unreachable ();
11336     }
11337
11338 }
11339
11340 /* Determine whether the evaluation of EXPR references any variables
11341    or functions which aren't otherwise used (and therefore may not be
11342    output).  */
11343 static tree
11344 reference_to_unused (tree * tp, int * walk_subtrees,
11345                      void * data ATTRIBUTE_UNUSED)
11346 {
11347   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11348     *walk_subtrees = 0;
11349
11350   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11351       && ! TREE_ASM_WRITTEN (*tp))
11352     return *tp;
11353   /* ???  The C++ FE emits debug information for using decls, so
11354      putting gcc_unreachable here falls over.  See PR31899.  For now
11355      be conservative.  */
11356   else if (!cgraph_global_info_ready
11357            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11358     return *tp;
11359   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11360     {
11361       struct varpool_node *node = varpool_node (*tp);
11362       if (!node->needed)
11363         return *tp;
11364     }
11365   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11366            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11367     {
11368       struct cgraph_node *node = cgraph_node (*tp);
11369       if (!node->output)
11370         return *tp;
11371     }
11372   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11373     return *tp;
11374
11375   return NULL_TREE;
11376 }
11377
11378 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11379    for use in a later add_const_value_attribute call.  */
11380
11381 static rtx
11382 rtl_for_decl_init (tree init, tree type)
11383 {
11384   rtx rtl = NULL_RTX;
11385
11386   /* If a variable is initialized with a string constant without embedded
11387      zeros, build CONST_STRING.  */
11388   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11389     {
11390       tree enttype = TREE_TYPE (type);
11391       tree domain = TYPE_DOMAIN (type);
11392       enum machine_mode mode = TYPE_MODE (enttype);
11393
11394       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11395           && domain
11396           && integer_zerop (TYPE_MIN_VALUE (domain))
11397           && compare_tree_int (TYPE_MAX_VALUE (domain),
11398                                TREE_STRING_LENGTH (init) - 1) == 0
11399           && ((size_t) TREE_STRING_LENGTH (init)
11400               == strlen (TREE_STRING_POINTER (init)) + 1))
11401         rtl = gen_rtx_CONST_STRING (VOIDmode,
11402                                     ggc_strdup (TREE_STRING_POINTER (init)));
11403     }
11404   /* Other aggregates, and complex values, could be represented using
11405      CONCAT: FIXME!  */
11406   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11407     ;
11408   /* Vectors only work if their mode is supported by the target.
11409      FIXME: generic vectors ought to work too.  */
11410   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11411     ;
11412   /* If the initializer is something that we know will expand into an
11413      immediate RTL constant, expand it now.  We must be careful not to
11414      reference variables which won't be output.  */
11415   else if (initializer_constant_valid_p (init, type)
11416            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11417     {
11418       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11419          possible.  */
11420       if (TREE_CODE (type) == VECTOR_TYPE)
11421         switch (TREE_CODE (init))
11422           {
11423           case VECTOR_CST:
11424             break;
11425           case CONSTRUCTOR:
11426             if (TREE_CONSTANT (init))
11427               {
11428                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11429                 bool constant_p = true;
11430                 tree value;
11431                 unsigned HOST_WIDE_INT ix;
11432
11433                 /* Even when ctor is constant, it might contain non-*_CST
11434                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11435                    belong into VECTOR_CST nodes.  */
11436                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11437                   if (!CONSTANT_CLASS_P (value))
11438                     {
11439                       constant_p = false;
11440                       break;
11441                     }
11442
11443                 if (constant_p)
11444                   {
11445                     init = build_vector_from_ctor (type, elts);
11446                     break;
11447                   }
11448               }
11449             /* FALLTHRU */
11450
11451           default:
11452             return NULL;
11453           }
11454
11455       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11456
11457       /* If expand_expr returns a MEM, it wasn't immediate.  */
11458       gcc_assert (!rtl || !MEM_P (rtl));
11459     }
11460
11461   return rtl;
11462 }
11463
11464 /* Generate RTL for the variable DECL to represent its location.  */
11465
11466 static rtx
11467 rtl_for_decl_location (tree decl)
11468 {
11469   rtx rtl;
11470
11471   /* Here we have to decide where we are going to say the parameter "lives"
11472      (as far as the debugger is concerned).  We only have a couple of
11473      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11474
11475      DECL_RTL normally indicates where the parameter lives during most of the
11476      activation of the function.  If optimization is enabled however, this
11477      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11478      that the parameter doesn't really live anywhere (as far as the code
11479      generation parts of GCC are concerned) during most of the function's
11480      activation.  That will happen (for example) if the parameter is never
11481      referenced within the function.
11482
11483      We could just generate a location descriptor here for all non-NULL
11484      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11485      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11486      where DECL_RTL is NULL or is a pseudo-reg.
11487
11488      Note however that we can only get away with using DECL_INCOMING_RTL as
11489      a backup substitute for DECL_RTL in certain limited cases.  In cases
11490      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11491      we can be sure that the parameter was passed using the same type as it is
11492      declared to have within the function, and that its DECL_INCOMING_RTL
11493      points us to a place where a value of that type is passed.
11494
11495      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11496      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11497      because in these cases DECL_INCOMING_RTL points us to a value of some
11498      type which is *different* from the type of the parameter itself.  Thus,
11499      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11500      such cases, the debugger would end up (for example) trying to fetch a
11501      `float' from a place which actually contains the first part of a
11502      `double'.  That would lead to really incorrect and confusing
11503      output at debug-time.
11504
11505      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11506      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11507      are a couple of exceptions however.  On little-endian machines we can
11508      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11509      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11510      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11511      when (on a little-endian machine) a non-prototyped function has a
11512      parameter declared to be of type `short' or `char'.  In such cases,
11513      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11514      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11515      passed `int' value.  If the debugger then uses that address to fetch
11516      a `short' or a `char' (on a little-endian machine) the result will be
11517      the correct data, so we allow for such exceptional cases below.
11518
11519      Note that our goal here is to describe the place where the given formal
11520      parameter lives during most of the function's activation (i.e. between the
11521      end of the prologue and the start of the epilogue).  We'll do that as best
11522      as we can. Note however that if the given formal parameter is modified
11523      sometime during the execution of the function, then a stack backtrace (at
11524      debug-time) will show the function as having been called with the *new*
11525      value rather than the value which was originally passed in.  This happens
11526      rarely enough that it is not a major problem, but it *is* a problem, and
11527      I'd like to fix it.
11528
11529      A future version of dwarf2out.c may generate two additional attributes for
11530      any given DW_TAG_formal_parameter DIE which will describe the "passed
11531      type" and the "passed location" for the given formal parameter in addition
11532      to the attributes we now generate to indicate the "declared type" and the
11533      "active location" for each parameter.  This additional set of attributes
11534      could be used by debuggers for stack backtraces. Separately, note that
11535      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11536      This happens (for example) for inlined-instances of inline function formal
11537      parameters which are never referenced.  This really shouldn't be
11538      happening.  All PARM_DECL nodes should get valid non-NULL
11539      DECL_INCOMING_RTL values.  FIXME.  */
11540
11541   /* Use DECL_RTL as the "location" unless we find something better.  */
11542   rtl = DECL_RTL_IF_SET (decl);
11543
11544   /* When generating abstract instances, ignore everything except
11545      constants, symbols living in memory, and symbols living in
11546      fixed registers.  */
11547   if (! reload_completed)
11548     {
11549       if (rtl
11550           && (CONSTANT_P (rtl)
11551               || (MEM_P (rtl)
11552                   && CONSTANT_P (XEXP (rtl, 0)))
11553               || (REG_P (rtl)
11554                   && TREE_CODE (decl) == VAR_DECL
11555                   && TREE_STATIC (decl))))
11556         {
11557           rtl = targetm.delegitimize_address (rtl);
11558           return rtl;
11559         }
11560       rtl = NULL_RTX;
11561     }
11562   else if (TREE_CODE (decl) == PARM_DECL)
11563     {
11564       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11565         {
11566           tree declared_type = TREE_TYPE (decl);
11567           tree passed_type = DECL_ARG_TYPE (decl);
11568           enum machine_mode dmode = TYPE_MODE (declared_type);
11569           enum machine_mode pmode = TYPE_MODE (passed_type);
11570
11571           /* This decl represents a formal parameter which was optimized out.
11572              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11573              all cases where (rtl == NULL_RTX) just below.  */
11574           if (dmode == pmode)
11575             rtl = DECL_INCOMING_RTL (decl);
11576           else if (SCALAR_INT_MODE_P (dmode)
11577                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11578                    && DECL_INCOMING_RTL (decl))
11579             {
11580               rtx inc = DECL_INCOMING_RTL (decl);
11581               if (REG_P (inc))
11582                 rtl = inc;
11583               else if (MEM_P (inc))
11584                 {
11585                   if (BYTES_BIG_ENDIAN)
11586                     rtl = adjust_address_nv (inc, dmode,
11587                                              GET_MODE_SIZE (pmode)
11588                                              - GET_MODE_SIZE (dmode));
11589                   else
11590                     rtl = inc;
11591                 }
11592             }
11593         }
11594
11595       /* If the parm was passed in registers, but lives on the stack, then
11596          make a big endian correction if the mode of the type of the
11597          parameter is not the same as the mode of the rtl.  */
11598       /* ??? This is the same series of checks that are made in dbxout.c before
11599          we reach the big endian correction code there.  It isn't clear if all
11600          of these checks are necessary here, but keeping them all is the safe
11601          thing to do.  */
11602       else if (MEM_P (rtl)
11603                && XEXP (rtl, 0) != const0_rtx
11604                && ! CONSTANT_P (XEXP (rtl, 0))
11605                /* Not passed in memory.  */
11606                && !MEM_P (DECL_INCOMING_RTL (decl))
11607                /* Not passed by invisible reference.  */
11608                && (!REG_P (XEXP (rtl, 0))
11609                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11610                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11611 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11612                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11613 #endif
11614                      )
11615                /* Big endian correction check.  */
11616                && BYTES_BIG_ENDIAN
11617                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11618                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11619                    < UNITS_PER_WORD))
11620         {
11621           int offset = (UNITS_PER_WORD
11622                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11623
11624           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11625                              plus_constant (XEXP (rtl, 0), offset));
11626         }
11627     }
11628   else if (TREE_CODE (decl) == VAR_DECL
11629            && rtl
11630            && MEM_P (rtl)
11631            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11632            && BYTES_BIG_ENDIAN)
11633     {
11634       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11635       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11636
11637       /* If a variable is declared "register" yet is smaller than
11638          a register, then if we store the variable to memory, it
11639          looks like we're storing a register-sized value, when in
11640          fact we are not.  We need to adjust the offset of the
11641          storage location to reflect the actual value's bytes,
11642          else gdb will not be able to display it.  */
11643       if (rsize > dsize)
11644         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11645                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11646     }
11647
11648   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11649      and will have been substituted directly into all expressions that use it.
11650      C does not have such a concept, but C++ and other languages do.  */
11651   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11652     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11653
11654   if (rtl)
11655     rtl = targetm.delegitimize_address (rtl);
11656
11657   /* If we don't look past the constant pool, we risk emitting a
11658      reference to a constant pool entry that isn't referenced from
11659      code, and thus is not emitted.  */
11660   if (rtl)
11661     rtl = avoid_constant_pool_reference (rtl);
11662
11663   return rtl;
11664 }
11665
11666 /* We need to figure out what section we should use as the base for the
11667    address ranges where a given location is valid.
11668    1. If this particular DECL has a section associated with it, use that.
11669    2. If this function has a section associated with it, use that.
11670    3. Otherwise, use the text section.
11671    XXX: If you split a variable across multiple sections, we won't notice.  */
11672
11673 static const char *
11674 secname_for_decl (const_tree decl)
11675 {
11676   const char *secname;
11677
11678   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11679     {
11680       tree sectree = DECL_SECTION_NAME (decl);
11681       secname = TREE_STRING_POINTER (sectree);
11682     }
11683   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11684     {
11685       tree sectree = DECL_SECTION_NAME (current_function_decl);
11686       secname = TREE_STRING_POINTER (sectree);
11687     }
11688   else if (cfun && in_cold_section_p)
11689     secname = crtl->subsections.cold_section_label;
11690   else
11691     secname = text_section_label;
11692
11693   return secname;
11694 }
11695
11696 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11697    returned.  If so, the decl for the COMMON block is returned, and the
11698    value is the offset into the common block for the symbol.  */
11699
11700 static tree
11701 fortran_common (tree decl, HOST_WIDE_INT *value)
11702 {
11703   tree val_expr, cvar;
11704   enum machine_mode mode;
11705   HOST_WIDE_INT bitsize, bitpos;
11706   tree offset;
11707   int volatilep = 0, unsignedp = 0;
11708
11709   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11710      it does not have a value (the offset into the common area), or if it
11711      is thread local (as opposed to global) then it isn't common, and shouldn't
11712      be handled as such.  */
11713   if (TREE_CODE (decl) != VAR_DECL
11714       || !TREE_PUBLIC (decl)
11715       || !TREE_STATIC (decl)
11716       || !DECL_HAS_VALUE_EXPR_P (decl)
11717       || !is_fortran ())
11718     return NULL_TREE;
11719
11720   val_expr = DECL_VALUE_EXPR (decl);
11721   if (TREE_CODE (val_expr) != COMPONENT_REF)
11722     return NULL_TREE;
11723
11724   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11725                               &mode, &unsignedp, &volatilep, true);
11726
11727   if (cvar == NULL_TREE
11728       || TREE_CODE (cvar) != VAR_DECL
11729       || DECL_ARTIFICIAL (cvar)
11730       || !TREE_PUBLIC (cvar))
11731     return NULL_TREE;
11732
11733   *value = 0;
11734   if (offset != NULL)
11735     {
11736       if (!host_integerp (offset, 0))
11737         return NULL_TREE;
11738       *value = tree_low_cst (offset, 0);
11739     }
11740   if (bitpos != 0)
11741     *value += bitpos / BITS_PER_UNIT;
11742
11743   return cvar;
11744 }
11745
11746 /* Dereference a location expression LOC if DECL is passed by invisible
11747    reference.  */
11748
11749 static dw_loc_descr_ref
11750 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11751 {
11752   HOST_WIDE_INT size;
11753   enum dwarf_location_atom op;
11754
11755   if (loc == NULL)
11756     return NULL;
11757
11758   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11759       || !DECL_BY_REFERENCE (decl))
11760     return loc;
11761
11762   size = int_size_in_bytes (TREE_TYPE (decl));
11763   if (size > DWARF2_ADDR_SIZE || size == -1)
11764     return 0;
11765   else if (size == DWARF2_ADDR_SIZE)
11766     op = DW_OP_deref;
11767   else
11768     op = DW_OP_deref_size;
11769   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11770   return loc;
11771 }
11772
11773 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11774    data attribute for a variable or a parameter.  We generate the
11775    DW_AT_const_value attribute only in those cases where the given variable
11776    or parameter does not have a true "location" either in memory or in a
11777    register.  This can happen (for example) when a constant is passed as an
11778    actual argument in a call to an inline function.  (It's possible that
11779    these things can crop up in other ways also.)  Note that one type of
11780    constant value which can be passed into an inlined function is a constant
11781    pointer.  This can happen for example if an actual argument in an inlined
11782    function call evaluates to a compile-time constant address.  */
11783
11784 static void
11785 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11786                                        enum dwarf_attribute attr)
11787 {
11788   rtx rtl;
11789   dw_loc_descr_ref descr;
11790   var_loc_list *loc_list;
11791   struct var_loc_node *node;
11792   if (TREE_CODE (decl) == ERROR_MARK)
11793     return;
11794
11795   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11796               || TREE_CODE (decl) == RESULT_DECL);
11797
11798   /* See if we possibly have multiple locations for this variable.  */
11799   loc_list = lookup_decl_loc (decl);
11800
11801   /* If it truly has multiple locations, the first and last node will
11802      differ.  */
11803   if (loc_list && loc_list->first != loc_list->last)
11804     {
11805       const char *endname, *secname;
11806       dw_loc_list_ref list;
11807       rtx varloc;
11808       enum var_init_status initialized;
11809
11810       /* Now that we know what section we are using for a base,
11811          actually construct the list of locations.
11812          The first location information is what is passed to the
11813          function that creates the location list, and the remaining
11814          locations just get added on to that list.
11815          Note that we only know the start address for a location
11816          (IE location changes), so to build the range, we use
11817          the range [current location start, next location start].
11818          This means we have to special case the last node, and generate
11819          a range of [last location start, end of function label].  */
11820
11821       node = loc_list->first;
11822       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11823       secname = secname_for_decl (decl);
11824
11825       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11826         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11827       else
11828         initialized = VAR_INIT_STATUS_INITIALIZED;
11829
11830       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11831       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11832       node = node->next;
11833
11834       for (; node->next; node = node->next)
11835         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11836           {
11837             /* The variable has a location between NODE->LABEL and
11838                NODE->NEXT->LABEL.  */
11839             enum var_init_status initialized =
11840               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11841             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11842             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11843                                       decl);
11844             add_loc_descr_to_loc_list (&list, descr,
11845                                        node->label, node->next->label, secname);
11846           }
11847
11848       /* If the variable has a location at the last label
11849          it keeps its location until the end of function.  */
11850       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11851         {
11852           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11853           enum var_init_status initialized =
11854             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11855
11856           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11857           if (!current_function_decl)
11858             endname = text_end_label;
11859           else
11860             {
11861               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11862                                            current_function_funcdef_no);
11863               endname = ggc_strdup (label_id);
11864             }
11865           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11866                                     decl);
11867           add_loc_descr_to_loc_list (&list, descr,
11868                                      node->label, endname, secname);
11869         }
11870
11871       /* Finally, add the location list to the DIE, and we are done.  */
11872       add_AT_loc_list (die, attr, list);
11873       return;
11874     }
11875
11876   /* Try to get some constant RTL for this decl, and use that as the value of
11877      the location.  */
11878
11879   rtl = rtl_for_decl_location (decl);
11880   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11881     {
11882       add_const_value_attribute (die, rtl);
11883       return;
11884     }
11885
11886   /* If we have tried to generate the location otherwise, and it
11887      didn't work out (we wouldn't be here if we did), and we have a one entry
11888      location list, try generating a location from that.  */
11889   if (loc_list && loc_list->first)
11890     {
11891       enum var_init_status status;
11892       node = loc_list->first;
11893       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11894       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11895       if (descr)
11896         {
11897           descr = loc_by_reference (descr, decl);
11898           add_AT_location_description (die, attr, descr);
11899           return;
11900         }
11901     }
11902
11903   /* We couldn't get any rtl, so try directly generating the location
11904      description from the tree.  */
11905   descr = loc_descriptor_from_tree (decl);
11906   if (descr)
11907     {
11908       descr = loc_by_reference (descr, decl);
11909       add_AT_location_description (die, attr, descr);
11910       return;
11911     }
11912   /* None of that worked, so it must not really have a location;
11913      try adding a constant value attribute from the DECL_INITIAL.  */
11914   tree_add_const_value_attribute (die, decl);
11915 }
11916
11917 /* Add VARIABLE and DIE into deferred locations list.  */
11918
11919 static void
11920 defer_location (tree variable, dw_die_ref die)
11921 {
11922   deferred_locations entry;
11923   entry.variable = variable;
11924   entry.die = die;
11925   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11926 }
11927
11928 /* Helper function for tree_add_const_value_attribute.  Natively encode
11929    initializer INIT into an array.  Return true if successful.  */
11930
11931 static bool
11932 native_encode_initializer (tree init, unsigned char *array, int size)
11933 {
11934   tree type;
11935
11936   if (init == NULL_TREE)
11937     return false;
11938
11939   STRIP_NOPS (init);
11940   switch (TREE_CODE (init))
11941     {
11942     case STRING_CST:
11943       type = TREE_TYPE (init);
11944       if (TREE_CODE (type) == ARRAY_TYPE)
11945         {
11946           tree enttype = TREE_TYPE (type);
11947           enum machine_mode mode = TYPE_MODE (enttype);
11948
11949           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11950             return false;
11951           if (int_size_in_bytes (type) != size)
11952             return false;
11953           if (size > TREE_STRING_LENGTH (init))
11954             {
11955               memcpy (array, TREE_STRING_POINTER (init),
11956                       TREE_STRING_LENGTH (init));
11957               memset (array + TREE_STRING_LENGTH (init),
11958                       '\0', size - TREE_STRING_LENGTH (init));
11959             }
11960           else
11961             memcpy (array, TREE_STRING_POINTER (init), size);
11962           return true;
11963         }
11964       return false;
11965     case CONSTRUCTOR:
11966       type = TREE_TYPE (init);
11967       if (int_size_in_bytes (type) != size)
11968         return false;
11969       if (TREE_CODE (type) == ARRAY_TYPE)
11970         {
11971           HOST_WIDE_INT min_index;
11972           unsigned HOST_WIDE_INT cnt;
11973           int curpos = 0, fieldsize;
11974           constructor_elt *ce;
11975
11976           if (TYPE_DOMAIN (type) == NULL_TREE
11977               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11978             return false;
11979
11980           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11981           if (fieldsize <= 0)
11982             return false;
11983
11984           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11985           memset (array, '\0', size);
11986           for (cnt = 0;
11987                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11988                cnt++)
11989             {
11990               tree val = ce->value;
11991               tree index = ce->index;
11992               int pos = curpos;
11993               if (index && TREE_CODE (index) == RANGE_EXPR)
11994                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11995                       * fieldsize;
11996               else if (index)
11997                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11998
11999               if (val)
12000                 {
12001                   STRIP_NOPS (val);
12002                   if (!native_encode_initializer (val, array + pos, fieldsize))
12003                     return false;
12004                 }
12005               curpos = pos + fieldsize;
12006               if (index && TREE_CODE (index) == RANGE_EXPR)
12007                 {
12008                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12009                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12010                   while (count > 0)
12011                     {
12012                       if (val)
12013                         memcpy (array + curpos, array + pos, fieldsize);
12014                       curpos += fieldsize;
12015                     }
12016                 }
12017               gcc_assert (curpos <= size);
12018             }
12019           return true;
12020         }
12021       else if (TREE_CODE (type) == RECORD_TYPE
12022                || TREE_CODE (type) == UNION_TYPE)
12023         {
12024           tree field = NULL_TREE;
12025           unsigned HOST_WIDE_INT cnt;
12026           constructor_elt *ce;
12027
12028           if (int_size_in_bytes (type) != size)
12029             return false;
12030
12031           if (TREE_CODE (type) == RECORD_TYPE)
12032             field = TYPE_FIELDS (type);
12033
12034           for (cnt = 0;
12035                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12036                cnt++, field = field ? TREE_CHAIN (field) : 0)
12037             {
12038               tree val = ce->value;
12039               int pos, fieldsize;
12040
12041               if (ce->index != 0)
12042                 field = ce->index;
12043
12044               if (val)
12045                 STRIP_NOPS (val);
12046
12047               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12048                 return false;
12049
12050               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12051                   && TYPE_DOMAIN (TREE_TYPE (field))
12052                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12053                 return false;
12054               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12055                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12056                 return false;
12057               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12058               pos = int_byte_position (field);
12059               gcc_assert (pos + fieldsize <= size);
12060               if (val
12061                   && !native_encode_initializer (val, array + pos, fieldsize))
12062                 return false;
12063             }
12064           return true;
12065         }
12066       return false;
12067     case VIEW_CONVERT_EXPR:
12068     case NON_LVALUE_EXPR:
12069       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12070     default:
12071       return native_encode_expr (init, array, size) == size;
12072     }
12073 }
12074
12075 /* If we don't have a copy of this variable in memory for some reason (such
12076    as a C++ member constant that doesn't have an out-of-line definition),
12077    we should tell the debugger about the constant value.  */
12078
12079 static void
12080 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12081 {
12082   tree init;
12083   tree type = TREE_TYPE (decl);
12084   rtx rtl;
12085
12086   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12087     return;
12088
12089   init = DECL_INITIAL (decl);
12090   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12091     /* OK */;
12092   else
12093     return;
12094
12095   rtl = rtl_for_decl_init (init, type);
12096   if (rtl)
12097     add_const_value_attribute (var_die, rtl);
12098   /* If the host and target are sane, try harder.  */
12099   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12100            && initializer_constant_valid_p (init, type))
12101     {
12102       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12103       if (size > 0 && (int) size == size)
12104         {
12105           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12106
12107           if (native_encode_initializer (init, array, size))
12108             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12109         }
12110     }
12111 }
12112
12113 /* Convert the CFI instructions for the current function into a
12114    location list.  This is used for DW_AT_frame_base when we targeting
12115    a dwarf2 consumer that does not support the dwarf3
12116    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12117    expressions.  */
12118
12119 static dw_loc_list_ref
12120 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12121 {
12122   dw_fde_ref fde;
12123   dw_loc_list_ref list, *list_tail;
12124   dw_cfi_ref cfi;
12125   dw_cfa_location last_cfa, next_cfa;
12126   const char *start_label, *last_label, *section;
12127
12128   fde = current_fde ();
12129   gcc_assert (fde != NULL);
12130
12131   section = secname_for_decl (current_function_decl);
12132   list_tail = &list;
12133   list = NULL;
12134
12135   next_cfa.reg = INVALID_REGNUM;
12136   next_cfa.offset = 0;
12137   next_cfa.indirect = 0;
12138   next_cfa.base_offset = 0;
12139
12140   start_label = fde->dw_fde_begin;
12141
12142   /* ??? Bald assumption that the CIE opcode list does not contain
12143      advance opcodes.  */
12144   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12145     lookup_cfa_1 (cfi, &next_cfa);
12146
12147   last_cfa = next_cfa;
12148   last_label = start_label;
12149
12150   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12151     switch (cfi->dw_cfi_opc)
12152       {
12153       case DW_CFA_set_loc:
12154       case DW_CFA_advance_loc1:
12155       case DW_CFA_advance_loc2:
12156       case DW_CFA_advance_loc4:
12157         if (!cfa_equal_p (&last_cfa, &next_cfa))
12158           {
12159             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12160                                        start_label, last_label, section,
12161                                        list == NULL);
12162
12163             list_tail = &(*list_tail)->dw_loc_next;
12164             last_cfa = next_cfa;
12165             start_label = last_label;
12166           }
12167         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12168         break;
12169
12170       case DW_CFA_advance_loc:
12171         /* The encoding is complex enough that we should never emit this.  */
12172       case DW_CFA_remember_state:
12173       case DW_CFA_restore_state:
12174         /* We don't handle these two in this function.  It would be possible
12175            if it were to be required.  */
12176         gcc_unreachable ();
12177
12178       default:
12179         lookup_cfa_1 (cfi, &next_cfa);
12180         break;
12181       }
12182
12183   if (!cfa_equal_p (&last_cfa, &next_cfa))
12184     {
12185       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12186                                  start_label, last_label, section,
12187                                  list == NULL);
12188       list_tail = &(*list_tail)->dw_loc_next;
12189       start_label = last_label;
12190     }
12191   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12192                              start_label, fde->dw_fde_end, section,
12193                              list == NULL);
12194
12195   return list;
12196 }
12197
12198 /* Compute a displacement from the "steady-state frame pointer" to the
12199    frame base (often the same as the CFA), and store it in
12200    frame_pointer_fb_offset.  OFFSET is added to the displacement
12201    before the latter is negated.  */
12202
12203 static void
12204 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12205 {
12206   rtx reg, elim;
12207
12208 #ifdef FRAME_POINTER_CFA_OFFSET
12209   reg = frame_pointer_rtx;
12210   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12211 #else
12212   reg = arg_pointer_rtx;
12213   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12214 #endif
12215
12216   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12217   if (GET_CODE (elim) == PLUS)
12218     {
12219       offset += INTVAL (XEXP (elim, 1));
12220       elim = XEXP (elim, 0);
12221     }
12222
12223   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12224                && (elim == hard_frame_pointer_rtx
12225                    || elim == stack_pointer_rtx))
12226               || elim == (frame_pointer_needed
12227                           ? hard_frame_pointer_rtx
12228                           : stack_pointer_rtx));
12229
12230   frame_pointer_fb_offset = -offset;
12231 }
12232
12233 /* Generate a DW_AT_name attribute given some string value to be included as
12234    the value of the attribute.  */
12235
12236 static void
12237 add_name_attribute (dw_die_ref die, const char *name_string)
12238 {
12239   if (name_string != NULL && *name_string != 0)
12240     {
12241       if (demangle_name_func)
12242         name_string = (*demangle_name_func) (name_string);
12243
12244       add_AT_string (die, DW_AT_name, name_string);
12245     }
12246 }
12247
12248 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12249
12250 static void
12251 add_comp_dir_attribute (dw_die_ref die)
12252 {
12253   const char *wd = get_src_pwd ();
12254   if (wd != NULL)
12255     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12256 }
12257
12258 /* Given a tree node describing an array bound (either lower or upper) output
12259    a representation for that bound.  */
12260
12261 static void
12262 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12263 {
12264   switch (TREE_CODE (bound))
12265     {
12266     case ERROR_MARK:
12267       return;
12268
12269     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12270     case INTEGER_CST:
12271       if (! host_integerp (bound, 0)
12272           || (bound_attr == DW_AT_lower_bound
12273               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12274                   || (is_fortran () && integer_onep (bound)))))
12275         /* Use the default.  */
12276         ;
12277       else
12278         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12279       break;
12280
12281     CASE_CONVERT:
12282     case VIEW_CONVERT_EXPR:
12283       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12284       break;
12285
12286     case SAVE_EXPR:
12287       break;
12288
12289     case VAR_DECL:
12290     case PARM_DECL:
12291     case RESULT_DECL:
12292       {
12293         dw_die_ref decl_die = lookup_decl_die (bound);
12294         dw_loc_descr_ref loc;
12295
12296         /* ??? Can this happen, or should the variable have been bound
12297            first?  Probably it can, since I imagine that we try to create
12298            the types of parameters in the order in which they exist in
12299            the list, and won't have created a forward reference to a
12300            later parameter.  */
12301         if (decl_die != NULL)
12302           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12303         else
12304           {
12305             loc = loc_descriptor_from_tree_1 (bound, 0);
12306             add_AT_location_description (subrange_die, bound_attr, loc);
12307           }
12308         break;
12309       }
12310
12311     default:
12312       {
12313         /* Otherwise try to create a stack operation procedure to
12314            evaluate the value of the array bound.  */
12315
12316         dw_die_ref ctx, decl_die;
12317         dw_loc_descr_ref loc;
12318
12319         loc = loc_descriptor_from_tree (bound);
12320         if (loc == NULL)
12321           break;
12322
12323         if (current_function_decl == 0)
12324           ctx = comp_unit_die;
12325         else
12326           ctx = lookup_decl_die (current_function_decl);
12327
12328         decl_die = new_die (DW_TAG_variable, ctx, bound);
12329         add_AT_flag (decl_die, DW_AT_artificial, 1);
12330         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12331         add_AT_loc (decl_die, DW_AT_location, loc);
12332
12333         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12334         break;
12335       }
12336     }
12337 }
12338
12339 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12340    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12341    Note that the block of subscript information for an array type also
12342    includes information about the element type of the given array type.  */
12343
12344 static void
12345 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12346 {
12347   unsigned dimension_number;
12348   tree lower, upper;
12349   dw_die_ref subrange_die;
12350
12351   for (dimension_number = 0;
12352        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12353        type = TREE_TYPE (type), dimension_number++)
12354     {
12355       tree domain = TYPE_DOMAIN (type);
12356
12357       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12358         break;
12359
12360       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12361          and (in GNU C only) variable bounds.  Handle all three forms
12362          here.  */
12363       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12364       if (domain)
12365         {
12366           /* We have an array type with specified bounds.  */
12367           lower = TYPE_MIN_VALUE (domain);
12368           upper = TYPE_MAX_VALUE (domain);
12369
12370           /* Define the index type.  */
12371           if (TREE_TYPE (domain))
12372             {
12373               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12374                  TREE_TYPE field.  We can't emit debug info for this
12375                  because it is an unnamed integral type.  */
12376               if (TREE_CODE (domain) == INTEGER_TYPE
12377                   && TYPE_NAME (domain) == NULL_TREE
12378                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12379                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12380                 ;
12381               else
12382                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12383                                     type_die);
12384             }
12385
12386           /* ??? If upper is NULL, the array has unspecified length,
12387              but it does have a lower bound.  This happens with Fortran
12388                dimension arr(N:*)
12389              Since the debugger is definitely going to need to know N
12390              to produce useful results, go ahead and output the lower
12391              bound solo, and hope the debugger can cope.  */
12392
12393           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12394           if (upper)
12395             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12396         }
12397
12398       /* Otherwise we have an array type with an unspecified length.  The
12399          DWARF-2 spec does not say how to handle this; let's just leave out the
12400          bounds.  */
12401     }
12402 }
12403
12404 static void
12405 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12406 {
12407   unsigned size;
12408
12409   switch (TREE_CODE (tree_node))
12410     {
12411     case ERROR_MARK:
12412       size = 0;
12413       break;
12414     case ENUMERAL_TYPE:
12415     case RECORD_TYPE:
12416     case UNION_TYPE:
12417     case QUAL_UNION_TYPE:
12418       size = int_size_in_bytes (tree_node);
12419       break;
12420     case FIELD_DECL:
12421       /* For a data member of a struct or union, the DW_AT_byte_size is
12422          generally given as the number of bytes normally allocated for an
12423          object of the *declared* type of the member itself.  This is true
12424          even for bit-fields.  */
12425       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12426       break;
12427     default:
12428       gcc_unreachable ();
12429     }
12430
12431   /* Note that `size' might be -1 when we get to this point.  If it is, that
12432      indicates that the byte size of the entity in question is variable.  We
12433      have no good way of expressing this fact in Dwarf at the present time,
12434      so just let the -1 pass on through.  */
12435   add_AT_unsigned (die, DW_AT_byte_size, size);
12436 }
12437
12438 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12439    which specifies the distance in bits from the highest order bit of the
12440    "containing object" for the bit-field to the highest order bit of the
12441    bit-field itself.
12442
12443    For any given bit-field, the "containing object" is a hypothetical object
12444    (of some integral or enum type) within which the given bit-field lives.  The
12445    type of this hypothetical "containing object" is always the same as the
12446    declared type of the individual bit-field itself.  The determination of the
12447    exact location of the "containing object" for a bit-field is rather
12448    complicated.  It's handled by the `field_byte_offset' function (above).
12449
12450    Note that it is the size (in bytes) of the hypothetical "containing object"
12451    which will be given in the DW_AT_byte_size attribute for this bit-field.
12452    (See `byte_size_attribute' above).  */
12453
12454 static inline void
12455 add_bit_offset_attribute (dw_die_ref die, tree decl)
12456 {
12457   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12458   tree type = DECL_BIT_FIELD_TYPE (decl);
12459   HOST_WIDE_INT bitpos_int;
12460   HOST_WIDE_INT highest_order_object_bit_offset;
12461   HOST_WIDE_INT highest_order_field_bit_offset;
12462   HOST_WIDE_INT unsigned bit_offset;
12463
12464   /* Must be a field and a bit field.  */
12465   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12466
12467   /* We can't yet handle bit-fields whose offsets are variable, so if we
12468      encounter such things, just return without generating any attribute
12469      whatsoever.  Likewise for variable or too large size.  */
12470   if (! host_integerp (bit_position (decl), 0)
12471       || ! host_integerp (DECL_SIZE (decl), 1))
12472     return;
12473
12474   bitpos_int = int_bit_position (decl);
12475
12476   /* Note that the bit offset is always the distance (in bits) from the
12477      highest-order bit of the "containing object" to the highest-order bit of
12478      the bit-field itself.  Since the "high-order end" of any object or field
12479      is different on big-endian and little-endian machines, the computation
12480      below must take account of these differences.  */
12481   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12482   highest_order_field_bit_offset = bitpos_int;
12483
12484   if (! BYTES_BIG_ENDIAN)
12485     {
12486       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12487       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12488     }
12489
12490   bit_offset
12491     = (! BYTES_BIG_ENDIAN
12492        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12493        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12494
12495   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12496 }
12497
12498 /* For a FIELD_DECL node which represents a bit field, output an attribute
12499    which specifies the length in bits of the given field.  */
12500
12501 static inline void
12502 add_bit_size_attribute (dw_die_ref die, tree decl)
12503 {
12504   /* Must be a field and a bit field.  */
12505   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12506               && DECL_BIT_FIELD_TYPE (decl));
12507
12508   if (host_integerp (DECL_SIZE (decl), 1))
12509     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12510 }
12511
12512 /* If the compiled language is ANSI C, then add a 'prototyped'
12513    attribute, if arg types are given for the parameters of a function.  */
12514
12515 static inline void
12516 add_prototyped_attribute (dw_die_ref die, tree func_type)
12517 {
12518   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12519       && TYPE_ARG_TYPES (func_type) != NULL)
12520     add_AT_flag (die, DW_AT_prototyped, 1);
12521 }
12522
12523 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12524    by looking in either the type declaration or object declaration
12525    equate table.  */
12526
12527 static inline dw_die_ref
12528 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12529 {
12530   dw_die_ref origin_die = NULL;
12531
12532   if (TREE_CODE (origin) != FUNCTION_DECL)
12533     {
12534       /* We may have gotten separated from the block for the inlined
12535          function, if we're in an exception handler or some such; make
12536          sure that the abstract function has been written out.
12537
12538          Doing this for nested functions is wrong, however; functions are
12539          distinct units, and our context might not even be inline.  */
12540       tree fn = origin;
12541
12542       if (TYPE_P (fn))
12543         fn = TYPE_STUB_DECL (fn);
12544
12545       fn = decl_function_context (fn);
12546       if (fn)
12547         dwarf2out_abstract_function (fn);
12548     }
12549
12550   if (DECL_P (origin))
12551     origin_die = lookup_decl_die (origin);
12552   else if (TYPE_P (origin))
12553     origin_die = lookup_type_die (origin);
12554
12555   /* XXX: Functions that are never lowered don't always have correct block
12556      trees (in the case of java, they simply have no block tree, in some other
12557      languages).  For these functions, there is nothing we can really do to
12558      output correct debug info for inlined functions in all cases.  Rather
12559      than die, we'll just produce deficient debug info now, in that we will
12560      have variables without a proper abstract origin.  In the future, when all
12561      functions are lowered, we should re-add a gcc_assert (origin_die)
12562      here.  */
12563
12564   if (origin_die)
12565     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12566   return origin_die;
12567 }
12568
12569 /* We do not currently support the pure_virtual attribute.  */
12570
12571 static inline void
12572 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12573 {
12574   if (DECL_VINDEX (func_decl))
12575     {
12576       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12577
12578       if (host_integerp (DECL_VINDEX (func_decl), 0))
12579         add_AT_loc (die, DW_AT_vtable_elem_location,
12580                     new_loc_descr (DW_OP_constu,
12581                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12582                                    0));
12583
12584       /* GNU extension: Record what type this method came from originally.  */
12585       if (debug_info_level > DINFO_LEVEL_TERSE)
12586         add_AT_die_ref (die, DW_AT_containing_type,
12587                         lookup_type_die (DECL_CONTEXT (func_decl)));
12588     }
12589 }
12590 \f
12591 /* Add source coordinate attributes for the given decl.  */
12592
12593 static void
12594 add_src_coords_attributes (dw_die_ref die, tree decl)
12595 {
12596   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12597
12598   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12599   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12600 }
12601
12602 /* Add a DW_AT_name attribute and source coordinate attribute for the
12603    given decl, but only if it actually has a name.  */
12604
12605 static void
12606 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12607 {
12608   tree decl_name;
12609
12610   decl_name = DECL_NAME (decl);
12611   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12612     {
12613       add_name_attribute (die, dwarf2_name (decl, 0));
12614       if (! DECL_ARTIFICIAL (decl))
12615         add_src_coords_attributes (die, decl);
12616
12617       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12618           && TREE_PUBLIC (decl)
12619           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12620           && !DECL_ABSTRACT (decl)
12621           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12622           && !is_fortran ())
12623         add_AT_string (die, DW_AT_MIPS_linkage_name,
12624                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12625     }
12626
12627 #ifdef VMS_DEBUGGING_INFO
12628   /* Get the function's name, as described by its RTL.  This may be different
12629      from the DECL_NAME name used in the source file.  */
12630   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12631     {
12632       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12633                    XEXP (DECL_RTL (decl), 0));
12634       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12635     }
12636 #endif
12637 }
12638
12639 /* Push a new declaration scope.  */
12640
12641 static void
12642 push_decl_scope (tree scope)
12643 {
12644   VEC_safe_push (tree, gc, decl_scope_table, scope);
12645 }
12646
12647 /* Pop a declaration scope.  */
12648
12649 static inline void
12650 pop_decl_scope (void)
12651 {
12652   VEC_pop (tree, decl_scope_table);
12653 }
12654
12655 /* Return the DIE for the scope that immediately contains this type.
12656    Non-named types get global scope.  Named types nested in other
12657    types get their containing scope if it's open, or global scope
12658    otherwise.  All other types (i.e. function-local named types) get
12659    the current active scope.  */
12660
12661 static dw_die_ref
12662 scope_die_for (tree t, dw_die_ref context_die)
12663 {
12664   dw_die_ref scope_die = NULL;
12665   tree containing_scope;
12666   int i;
12667
12668   /* Non-types always go in the current scope.  */
12669   gcc_assert (TYPE_P (t));
12670
12671   containing_scope = TYPE_CONTEXT (t);
12672
12673   /* Use the containing namespace if it was passed in (for a declaration).  */
12674   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12675     {
12676       if (context_die == lookup_decl_die (containing_scope))
12677         /* OK */;
12678       else
12679         containing_scope = NULL_TREE;
12680     }
12681
12682   /* Ignore function type "scopes" from the C frontend.  They mean that
12683      a tagged type is local to a parmlist of a function declarator, but
12684      that isn't useful to DWARF.  */
12685   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12686     containing_scope = NULL_TREE;
12687
12688   if (containing_scope == NULL_TREE)
12689     scope_die = comp_unit_die;
12690   else if (TYPE_P (containing_scope))
12691     {
12692       /* For types, we can just look up the appropriate DIE.  But
12693          first we check to see if we're in the middle of emitting it
12694          so we know where the new DIE should go.  */
12695       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12696         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12697           break;
12698
12699       if (i < 0)
12700         {
12701           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12702                       || TREE_ASM_WRITTEN (containing_scope));
12703
12704           /* If none of the current dies are suitable, we get file scope.  */
12705           scope_die = comp_unit_die;
12706         }
12707       else
12708         scope_die = lookup_type_die (containing_scope);
12709     }
12710   else
12711     scope_die = context_die;
12712
12713   return scope_die;
12714 }
12715
12716 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12717
12718 static inline int
12719 local_scope_p (dw_die_ref context_die)
12720 {
12721   for (; context_die; context_die = context_die->die_parent)
12722     if (context_die->die_tag == DW_TAG_inlined_subroutine
12723         || context_die->die_tag == DW_TAG_subprogram)
12724       return 1;
12725
12726   return 0;
12727 }
12728
12729 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12730    whether or not to treat a DIE in this context as a declaration.  */
12731
12732 static inline int
12733 class_or_namespace_scope_p (dw_die_ref context_die)
12734 {
12735   return (context_die
12736           && (context_die->die_tag == DW_TAG_structure_type
12737               || context_die->die_tag == DW_TAG_class_type
12738               || context_die->die_tag == DW_TAG_interface_type
12739               || context_die->die_tag == DW_TAG_union_type
12740               || context_die->die_tag == DW_TAG_namespace));
12741 }
12742
12743 /* Many forms of DIEs require a "type description" attribute.  This
12744    routine locates the proper "type descriptor" die for the type given
12745    by 'type', and adds a DW_AT_type attribute below the given die.  */
12746
12747 static void
12748 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12749                     int decl_volatile, dw_die_ref context_die)
12750 {
12751   enum tree_code code  = TREE_CODE (type);
12752   dw_die_ref type_die  = NULL;
12753
12754   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12755      or fixed-point type, use the inner type.  This is because we have no
12756      support for unnamed types in base_type_die.  This can happen if this is
12757      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12758   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12759       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12760     type = TREE_TYPE (type), code = TREE_CODE (type);
12761
12762   if (code == ERROR_MARK
12763       /* Handle a special case.  For functions whose return type is void, we
12764          generate *no* type attribute.  (Note that no object may have type
12765          `void', so this only applies to function return types).  */
12766       || code == VOID_TYPE)
12767     return;
12768
12769   type_die = modified_type_die (type,
12770                                 decl_const || TYPE_READONLY (type),
12771                                 decl_volatile || TYPE_VOLATILE (type),
12772                                 context_die);
12773
12774   if (type_die != NULL)
12775     add_AT_die_ref (object_die, DW_AT_type, type_die);
12776 }
12777
12778 /* Given an object die, add the calling convention attribute for the
12779    function call type.  */
12780 static void
12781 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12782 {
12783   enum dwarf_calling_convention value = DW_CC_normal;
12784
12785   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12786
12787   /* DWARF doesn't provide a way to identify a program's source-level
12788      entry point.  DW_AT_calling_convention attributes are only meant
12789      to describe functions' calling conventions.  However, lacking a
12790      better way to signal the Fortran main program, we use this for the
12791      time being, following existing custom.  */
12792   if (is_fortran ()
12793       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12794     value = DW_CC_program;
12795
12796   /* Only add the attribute if the backend requests it, and
12797      is not DW_CC_normal.  */
12798   if (value && (value != DW_CC_normal))
12799     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12800 }
12801
12802 /* Given a tree pointer to a struct, class, union, or enum type node, return
12803    a pointer to the (string) tag name for the given type, or zero if the type
12804    was declared without a tag.  */
12805
12806 static const char *
12807 type_tag (const_tree type)
12808 {
12809   const char *name = 0;
12810
12811   if (TYPE_NAME (type) != 0)
12812     {
12813       tree t = 0;
12814
12815       /* Find the IDENTIFIER_NODE for the type name.  */
12816       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12817         t = TYPE_NAME (type);
12818
12819       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12820          a TYPE_DECL node, regardless of whether or not a `typedef' was
12821          involved.  */
12822       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12823                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12824         {
12825           /* We want to be extra verbose.  Don't call dwarf_name if
12826              DECL_NAME isn't set.  The default hook for decl_printable_name
12827              doesn't like that, and in this context it's correct to return
12828              0, instead of "<anonymous>" or the like.  */
12829           if (DECL_NAME (TYPE_NAME (type)))
12830             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12831         }
12832
12833       /* Now get the name as a string, or invent one.  */
12834       if (!name && t != 0)
12835         name = IDENTIFIER_POINTER (t);
12836     }
12837
12838   return (name == 0 || *name == '\0') ? 0 : name;
12839 }
12840
12841 /* Return the type associated with a data member, make a special check
12842    for bit field types.  */
12843
12844 static inline tree
12845 member_declared_type (const_tree member)
12846 {
12847   return (DECL_BIT_FIELD_TYPE (member)
12848           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12849 }
12850
12851 /* Get the decl's label, as described by its RTL. This may be different
12852    from the DECL_NAME name used in the source file.  */
12853
12854 #if 0
12855 static const char *
12856 decl_start_label (tree decl)
12857 {
12858   rtx x;
12859   const char *fnname;
12860
12861   x = DECL_RTL (decl);
12862   gcc_assert (MEM_P (x));
12863
12864   x = XEXP (x, 0);
12865   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12866
12867   fnname = XSTR (x, 0);
12868   return fnname;
12869 }
12870 #endif
12871 \f
12872 /* These routines generate the internal representation of the DIE's for
12873    the compilation unit.  Debugging information is collected by walking
12874    the declaration trees passed in from dwarf2out_decl().  */
12875
12876 static void
12877 gen_array_type_die (tree type, dw_die_ref context_die)
12878 {
12879   dw_die_ref scope_die = scope_die_for (type, context_die);
12880   dw_die_ref array_die;
12881
12882   /* GNU compilers represent multidimensional array types as sequences of one
12883      dimensional array types whose element types are themselves array types.
12884      We sometimes squish that down to a single array_type DIE with multiple
12885      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12886      say that we are allowed to do this kind of compression in C, because
12887      there is no difference between an array of arrays and a multidimensional
12888      array.  We don't do this for Ada to remain as close as possible to the
12889      actual representation, which is especially important against the language
12890      flexibilty wrt arrays of variable size.  */
12891
12892   bool collapse_nested_arrays = !is_ada ();
12893   tree element_type;
12894
12895   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12896      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12897   if (TYPE_STRING_FLAG (type)
12898       && TREE_CODE (type) == ARRAY_TYPE
12899       && is_fortran ()
12900       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12901     {
12902       HOST_WIDE_INT size;
12903
12904       array_die = new_die (DW_TAG_string_type, scope_die, type);
12905       add_name_attribute (array_die, type_tag (type));
12906       equate_type_number_to_die (type, array_die);
12907       size = int_size_in_bytes (type);
12908       if (size >= 0)
12909         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12910       else if (TYPE_DOMAIN (type) != NULL_TREE
12911                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12912                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12913         {
12914           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12915           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12916
12917           size = int_size_in_bytes (TREE_TYPE (szdecl));
12918           if (loc && size > 0)
12919             {
12920               add_AT_loc (array_die, DW_AT_string_length, loc);
12921               if (size != DWARF2_ADDR_SIZE)
12922                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12923             }
12924         }
12925       return;
12926     }
12927
12928   /* ??? The SGI dwarf reader fails for array of array of enum types
12929      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12930      array type comes before the outer array type.  We thus call gen_type_die
12931      before we new_die and must prevent nested array types collapsing for this
12932      target.  */
12933
12934 #ifdef MIPS_DEBUGGING_INFO
12935   gen_type_die (TREE_TYPE (type), context_die);
12936   collapse_nested_arrays = false;
12937 #endif
12938
12939   array_die = new_die (DW_TAG_array_type, scope_die, type);
12940   add_name_attribute (array_die, type_tag (type));
12941   equate_type_number_to_die (type, array_die);
12942
12943   if (TREE_CODE (type) == VECTOR_TYPE)
12944     {
12945       /* The frontend feeds us a representation for the vector as a struct
12946          containing an array.  Pull out the array type.  */
12947       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12948       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12949     }
12950
12951   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12952   if (is_fortran ()
12953       && TREE_CODE (type) == ARRAY_TYPE
12954       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12955       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12956     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12957
12958 #if 0
12959   /* We default the array ordering.  SDB will probably do
12960      the right things even if DW_AT_ordering is not present.  It's not even
12961      an issue until we start to get into multidimensional arrays anyway.  If
12962      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12963      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12964      and when we find out that we need to put these in, we will only do so
12965      for multidimensional arrays.  */
12966   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12967 #endif
12968
12969 #ifdef MIPS_DEBUGGING_INFO
12970   /* The SGI compilers handle arrays of unknown bound by setting
12971      AT_declaration and not emitting any subrange DIEs.  */
12972   if (! TYPE_DOMAIN (type))
12973     add_AT_flag (array_die, DW_AT_declaration, 1);
12974   else
12975 #endif
12976     add_subscript_info (array_die, type, collapse_nested_arrays);
12977
12978   /* Add representation of the type of the elements of this array type and
12979      emit the corresponding DIE if we haven't done it already.  */  
12980   element_type = TREE_TYPE (type);
12981   if (collapse_nested_arrays)
12982     while (TREE_CODE (element_type) == ARRAY_TYPE)
12983       {
12984         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12985           break;
12986         element_type = TREE_TYPE (element_type);
12987       }
12988
12989 #ifndef MIPS_DEBUGGING_INFO
12990   gen_type_die (element_type, context_die);
12991 #endif
12992
12993   add_type_attribute (array_die, element_type, 0, 0, context_die);
12994
12995   if (get_AT (array_die, DW_AT_name))
12996     add_pubtype (type, array_die);
12997 }
12998
12999 static dw_loc_descr_ref
13000 descr_info_loc (tree val, tree base_decl)
13001 {
13002   HOST_WIDE_INT size;
13003   dw_loc_descr_ref loc, loc2;
13004   enum dwarf_location_atom op;
13005
13006   if (val == base_decl)
13007     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13008
13009   switch (TREE_CODE (val))
13010     {
13011     CASE_CONVERT:
13012       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13013     case VAR_DECL:
13014       return loc_descriptor_from_tree_1 (val, 0);
13015     case INTEGER_CST:
13016       if (host_integerp (val, 0))
13017         return int_loc_descriptor (tree_low_cst (val, 0));
13018       break;
13019     case INDIRECT_REF:
13020       size = int_size_in_bytes (TREE_TYPE (val));
13021       if (size < 0)
13022         break;
13023       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13024       if (!loc)
13025         break;
13026       if (size == DWARF2_ADDR_SIZE)
13027         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13028       else
13029         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13030       return loc;
13031     case POINTER_PLUS_EXPR:
13032     case PLUS_EXPR:
13033       if (host_integerp (TREE_OPERAND (val, 1), 1)
13034           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13035              < 16384)
13036         {
13037           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13038           if (!loc)
13039             break;
13040           add_loc_descr (&loc,
13041                          new_loc_descr (DW_OP_plus_uconst,
13042                                         tree_low_cst (TREE_OPERAND (val, 1),
13043                                                       1), 0));
13044         }
13045       else
13046         {
13047           op = DW_OP_plus;
13048         do_binop:
13049           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13050           if (!loc)
13051             break;
13052           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13053           if (!loc2)
13054             break;
13055           add_loc_descr (&loc, loc2);
13056           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13057         }
13058       return loc;
13059     case MINUS_EXPR:
13060       op = DW_OP_minus;
13061       goto do_binop;
13062     case MULT_EXPR:
13063       op = DW_OP_mul;
13064       goto do_binop;
13065     case EQ_EXPR:
13066       op = DW_OP_eq;
13067       goto do_binop;
13068     case NE_EXPR:
13069       op = DW_OP_ne;
13070       goto do_binop;
13071     default:
13072       break;
13073     }
13074   return NULL;
13075 }
13076
13077 static void
13078 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13079                       tree val, tree base_decl)
13080 {
13081   dw_loc_descr_ref loc;
13082
13083   if (host_integerp (val, 0))
13084     {
13085       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13086       return;
13087     }
13088
13089   loc = descr_info_loc (val, base_decl);
13090   if (!loc)
13091     return;
13092
13093   add_AT_loc (die, attr, loc);
13094 }
13095
13096 /* This routine generates DIE for array with hidden descriptor, details
13097    are filled into *info by a langhook.  */
13098
13099 static void
13100 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13101                           dw_die_ref context_die)
13102 {
13103   dw_die_ref scope_die = scope_die_for (type, context_die);
13104   dw_die_ref array_die;
13105   int dim;
13106
13107   array_die = new_die (DW_TAG_array_type, scope_die, type);
13108   add_name_attribute (array_die, type_tag (type));
13109   equate_type_number_to_die (type, array_die);
13110
13111   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13112   if (is_fortran ()
13113       && info->ndimensions >= 2)
13114     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13115
13116   if (info->data_location)
13117     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13118                           info->base_decl);
13119   if (info->associated)
13120     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13121                           info->base_decl);
13122   if (info->allocated)
13123     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13124                           info->base_decl);
13125
13126   for (dim = 0; dim < info->ndimensions; dim++)
13127     {
13128       dw_die_ref subrange_die
13129         = new_die (DW_TAG_subrange_type, array_die, NULL);
13130
13131       if (info->dimen[dim].lower_bound)
13132         {
13133           /* If it is the default value, omit it.  */
13134           if ((is_c_family () || is_java ())
13135               && integer_zerop (info->dimen[dim].lower_bound))
13136             ;
13137           else if (is_fortran ()
13138                    && integer_onep (info->dimen[dim].lower_bound))
13139             ;
13140           else
13141             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13142                                   info->dimen[dim].lower_bound,
13143                                   info->base_decl);
13144         }
13145       if (info->dimen[dim].upper_bound)
13146         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13147                               info->dimen[dim].upper_bound,
13148                               info->base_decl);
13149       if (info->dimen[dim].stride)
13150         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13151                               info->dimen[dim].stride,
13152                               info->base_decl);
13153     }
13154
13155   gen_type_die (info->element_type, context_die);
13156   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13157
13158   if (get_AT (array_die, DW_AT_name))
13159     add_pubtype (type, array_die);
13160 }
13161
13162 #if 0
13163 static void
13164 gen_entry_point_die (tree decl, dw_die_ref context_die)
13165 {
13166   tree origin = decl_ultimate_origin (decl);
13167   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13168
13169   if (origin != NULL)
13170     add_abstract_origin_attribute (decl_die, origin);
13171   else
13172     {
13173       add_name_and_src_coords_attributes (decl_die, decl);
13174       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13175                           0, 0, context_die);
13176     }
13177
13178   if (DECL_ABSTRACT (decl))
13179     equate_decl_number_to_die (decl, decl_die);
13180   else
13181     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13182 }
13183 #endif
13184
13185 /* Walk through the list of incomplete types again, trying once more to
13186    emit full debugging info for them.  */
13187
13188 static void
13189 retry_incomplete_types (void)
13190 {
13191   int i;
13192
13193   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13194     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13195 }
13196
13197 /* Determine what tag to use for a record type.  */
13198
13199 static enum dwarf_tag
13200 record_type_tag (tree type)
13201 {
13202   if (! lang_hooks.types.classify_record)
13203     return DW_TAG_structure_type;
13204
13205   switch (lang_hooks.types.classify_record (type))
13206     {
13207     case RECORD_IS_STRUCT:
13208       return DW_TAG_structure_type;
13209
13210     case RECORD_IS_CLASS:
13211       return DW_TAG_class_type;
13212
13213     case RECORD_IS_INTERFACE:
13214       return DW_TAG_interface_type;
13215
13216     default:
13217       gcc_unreachable ();
13218     }
13219 }
13220
13221 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13222    include all of the information about the enumeration values also. Each
13223    enumerated type name/value is listed as a child of the enumerated type
13224    DIE.  */
13225
13226 static dw_die_ref
13227 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13228 {
13229   dw_die_ref type_die = lookup_type_die (type);
13230
13231   if (type_die == NULL)
13232     {
13233       type_die = new_die (DW_TAG_enumeration_type,
13234                           scope_die_for (type, context_die), type);
13235       equate_type_number_to_die (type, type_die);
13236       add_name_attribute (type_die, type_tag (type));
13237     }
13238   else if (! TYPE_SIZE (type))
13239     return type_die;
13240   else
13241     remove_AT (type_die, DW_AT_declaration);
13242
13243   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13244      given enum type is incomplete, do not generate the DW_AT_byte_size
13245      attribute or the DW_AT_element_list attribute.  */
13246   if (TYPE_SIZE (type))
13247     {
13248       tree link;
13249
13250       TREE_ASM_WRITTEN (type) = 1;
13251       add_byte_size_attribute (type_die, type);
13252       if (TYPE_STUB_DECL (type) != NULL_TREE)
13253         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13254
13255       /* If the first reference to this type was as the return type of an
13256          inline function, then it may not have a parent.  Fix this now.  */
13257       if (type_die->die_parent == NULL)
13258         add_child_die (scope_die_for (type, context_die), type_die);
13259
13260       for (link = TYPE_VALUES (type);
13261            link != NULL; link = TREE_CHAIN (link))
13262         {
13263           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13264           tree value = TREE_VALUE (link);
13265
13266           add_name_attribute (enum_die,
13267                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13268
13269           if (TREE_CODE (value) == CONST_DECL)
13270             value = DECL_INITIAL (value);
13271
13272           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13273             /* DWARF2 does not provide a way of indicating whether or
13274                not enumeration constants are signed or unsigned.  GDB
13275                always assumes the values are signed, so we output all
13276                values as if they were signed.  That means that
13277                enumeration constants with very large unsigned values
13278                will appear to have negative values in the debugger.  */
13279             add_AT_int (enum_die, DW_AT_const_value,
13280                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13281         }
13282     }
13283   else
13284     add_AT_flag (type_die, DW_AT_declaration, 1);
13285
13286   if (get_AT (type_die, DW_AT_name))
13287     add_pubtype (type, type_die);
13288
13289   return type_die;
13290 }
13291
13292 /* Generate a DIE to represent either a real live formal parameter decl or to
13293    represent just the type of some formal parameter position in some function
13294    type.
13295
13296    Note that this routine is a bit unusual because its argument may be a
13297    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13298    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13299    node.  If it's the former then this function is being called to output a
13300    DIE to represent a formal parameter object (or some inlining thereof).  If
13301    it's the latter, then this function is only being called to output a
13302    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13303    argument type of some subprogram type.  */
13304
13305 static dw_die_ref
13306 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13307 {
13308   tree node_or_origin = node ? node : origin;
13309   dw_die_ref parm_die
13310     = new_die (DW_TAG_formal_parameter, context_die, node);
13311
13312   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13313     {
13314     case tcc_declaration:
13315       if (!origin)
13316         origin = decl_ultimate_origin (node);
13317       if (origin != NULL)
13318         add_abstract_origin_attribute (parm_die, origin);
13319       else
13320         {
13321           tree type = TREE_TYPE (node);
13322           add_name_and_src_coords_attributes (parm_die, node);
13323           if (DECL_BY_REFERENCE (node))
13324             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13325                                 context_die);
13326           else
13327             add_type_attribute (parm_die, type,
13328                                 TREE_READONLY (node),
13329                                 TREE_THIS_VOLATILE (node),
13330                                 context_die);
13331           if (DECL_ARTIFICIAL (node))
13332             add_AT_flag (parm_die, DW_AT_artificial, 1);
13333         }
13334
13335       if (node)
13336         equate_decl_number_to_die (node, parm_die);
13337       if (! DECL_ABSTRACT (node_or_origin))
13338         add_location_or_const_value_attribute (parm_die, node_or_origin,
13339                                                DW_AT_location);
13340
13341       break;
13342
13343     case tcc_type:
13344       /* We were called with some kind of a ..._TYPE node.  */
13345       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13346       break;
13347
13348     default:
13349       gcc_unreachable ();
13350     }
13351
13352   return parm_die;
13353 }
13354
13355 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13356    at the end of an (ANSI prototyped) formal parameters list.  */
13357
13358 static void
13359 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13360 {
13361   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13362 }
13363
13364 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13365    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13366    parameters as specified in some function type specification (except for
13367    those which appear as part of a function *definition*).  */
13368
13369 static void
13370 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13371 {
13372   tree link;
13373   tree formal_type = NULL;
13374   tree first_parm_type;
13375   tree arg;
13376
13377   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13378     {
13379       arg = DECL_ARGUMENTS (function_or_method_type);
13380       function_or_method_type = TREE_TYPE (function_or_method_type);
13381     }
13382   else
13383     arg = NULL_TREE;
13384
13385   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13386
13387   /* Make our first pass over the list of formal parameter types and output a
13388      DW_TAG_formal_parameter DIE for each one.  */
13389   for (link = first_parm_type; link; )
13390     {
13391       dw_die_ref parm_die;
13392
13393       formal_type = TREE_VALUE (link);
13394       if (formal_type == void_type_node)
13395         break;
13396
13397       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13398       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13399       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13400            && link == first_parm_type)
13401           || (arg && DECL_ARTIFICIAL (arg)))
13402         add_AT_flag (parm_die, DW_AT_artificial, 1);
13403
13404       link = TREE_CHAIN (link);
13405       if (arg)
13406         arg = TREE_CHAIN (arg);
13407     }
13408
13409   /* If this function type has an ellipsis, add a
13410      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13411   if (formal_type != void_type_node)
13412     gen_unspecified_parameters_die (function_or_method_type, context_die);
13413
13414   /* Make our second (and final) pass over the list of formal parameter types
13415      and output DIEs to represent those types (as necessary).  */
13416   for (link = TYPE_ARG_TYPES (function_or_method_type);
13417        link && TREE_VALUE (link);
13418        link = TREE_CHAIN (link))
13419     gen_type_die (TREE_VALUE (link), context_die);
13420 }
13421
13422 /* We want to generate the DIE for TYPE so that we can generate the
13423    die for MEMBER, which has been defined; we will need to refer back
13424    to the member declaration nested within TYPE.  If we're trying to
13425    generate minimal debug info for TYPE, processing TYPE won't do the
13426    trick; we need to attach the member declaration by hand.  */
13427
13428 static void
13429 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13430 {
13431   gen_type_die (type, context_die);
13432
13433   /* If we're trying to avoid duplicate debug info, we may not have
13434      emitted the member decl for this function.  Emit it now.  */
13435   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13436       && ! lookup_decl_die (member))
13437     {
13438       dw_die_ref type_die;
13439       gcc_assert (!decl_ultimate_origin (member));
13440
13441       push_decl_scope (type);
13442       type_die = lookup_type_die (type);
13443       if (TREE_CODE (member) == FUNCTION_DECL)
13444         gen_subprogram_die (member, type_die);
13445       else if (TREE_CODE (member) == FIELD_DECL)
13446         {
13447           /* Ignore the nameless fields that are used to skip bits but handle
13448              C++ anonymous unions and structs.  */
13449           if (DECL_NAME (member) != NULL_TREE
13450               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13451               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13452             {
13453               gen_type_die (member_declared_type (member), type_die);
13454               gen_field_die (member, type_die);
13455             }
13456         }
13457       else
13458         gen_variable_die (member, NULL_TREE, type_die);
13459
13460       pop_decl_scope ();
13461     }
13462 }
13463
13464 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13465    may later generate inlined and/or out-of-line instances of.  */
13466
13467 static void
13468 dwarf2out_abstract_function (tree decl)
13469 {
13470   dw_die_ref old_die;
13471   tree save_fn;
13472   tree context;
13473   int was_abstract = DECL_ABSTRACT (decl);
13474
13475   /* Make sure we have the actual abstract inline, not a clone.  */
13476   decl = DECL_ORIGIN (decl);
13477
13478   old_die = lookup_decl_die (decl);
13479   if (old_die && get_AT (old_die, DW_AT_inline))
13480     /* We've already generated the abstract instance.  */
13481     return;
13482
13483   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13484      we don't get confused by DECL_ABSTRACT.  */
13485   if (debug_info_level > DINFO_LEVEL_TERSE)
13486     {
13487       context = decl_class_context (decl);
13488       if (context)
13489         gen_type_die_for_member
13490           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13491     }
13492
13493   /* Pretend we've just finished compiling this function.  */
13494   save_fn = current_function_decl;
13495   current_function_decl = decl;
13496   push_cfun (DECL_STRUCT_FUNCTION (decl));
13497
13498   set_decl_abstract_flags (decl, 1);
13499   dwarf2out_decl (decl);
13500   if (! was_abstract)
13501     set_decl_abstract_flags (decl, 0);
13502
13503   current_function_decl = save_fn;
13504   pop_cfun ();
13505 }
13506
13507 /* Helper function of premark_used_types() which gets called through
13508    htab_traverse.
13509
13510    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13511    marked as unused by prune_unused_types.  */
13512
13513 static int
13514 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13515 {
13516   tree type;
13517   dw_die_ref die;
13518
13519   type = (tree) *slot;
13520   die = lookup_type_die (type);
13521   if (die != NULL)
13522     die->die_perennial_p = 1;
13523   return 1;
13524 }
13525
13526 /* Helper function of premark_types_used_by_global_vars which gets called
13527    through htab_traverse.
13528
13529    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13530    marked as unused by prune_unused_types. The DIE of the type is marked
13531    only if the global variable using the type will actually be emitted.  */
13532
13533 static int
13534 premark_types_used_by_global_vars_helper (void **slot,
13535                                           void *data ATTRIBUTE_UNUSED)
13536 {
13537   struct types_used_by_vars_entry *entry;
13538   dw_die_ref die;
13539
13540   entry = (struct types_used_by_vars_entry *) *slot;
13541   gcc_assert (entry->type != NULL
13542               && entry->var_decl != NULL);
13543   die = lookup_type_die (entry->type);
13544   if (die)
13545     {
13546       /* Ask cgraph if the global variable really is to be emitted.
13547          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
13548       struct varpool_node *node = varpool_node (entry->var_decl);
13549       if (node->needed)
13550         {
13551           die->die_perennial_p = 1;
13552           /* Keep the parent DIEs as well.  */
13553           while ((die = die->die_parent) && die->die_perennial_p == 0)
13554             die->die_perennial_p = 1;
13555         }
13556     }
13557   return 1;
13558 }
13559
13560 /* Mark all members of used_types_hash as perennial.  */
13561
13562 static void
13563 premark_used_types (void)
13564 {
13565   if (cfun && cfun->used_types_hash)
13566     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13567 }
13568
13569 /* Mark all members of types_used_by_vars_entry as perennial.  */
13570
13571 static void
13572 premark_types_used_by_global_vars (void)
13573 {
13574   if (types_used_by_vars_hash)
13575     htab_traverse (types_used_by_vars_hash,
13576                    premark_types_used_by_global_vars_helper, NULL);
13577 }
13578
13579 /* Generate a DIE to represent a declared function (either file-scope or
13580    block-local).  */
13581
13582 static void
13583 gen_subprogram_die (tree decl, dw_die_ref context_die)
13584 {
13585   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13586   tree origin = decl_ultimate_origin (decl);
13587   dw_die_ref subr_die;
13588   tree fn_arg_types;
13589   tree outer_scope;
13590   dw_die_ref old_die = lookup_decl_die (decl);
13591   int declaration = (current_function_decl != decl
13592                      || class_or_namespace_scope_p (context_die));
13593
13594   premark_used_types ();
13595
13596   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13597      started to generate the abstract instance of an inline, decided to output
13598      its containing class, and proceeded to emit the declaration of the inline
13599      from the member list for the class.  If so, DECLARATION takes priority;
13600      we'll get back to the abstract instance when done with the class.  */
13601
13602   /* The class-scope declaration DIE must be the primary DIE.  */
13603   if (origin && declaration && class_or_namespace_scope_p (context_die))
13604     {
13605       origin = NULL;
13606       gcc_assert (!old_die);
13607     }
13608
13609   /* Now that the C++ front end lazily declares artificial member fns, we
13610      might need to retrofit the declaration into its class.  */
13611   if (!declaration && !origin && !old_die
13612       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13613       && !class_or_namespace_scope_p (context_die)
13614       && debug_info_level > DINFO_LEVEL_TERSE)
13615     old_die = force_decl_die (decl);
13616
13617   if (origin != NULL)
13618     {
13619       gcc_assert (!declaration || local_scope_p (context_die));
13620
13621       /* Fixup die_parent for the abstract instance of a nested
13622          inline function.  */
13623       if (old_die && old_die->die_parent == NULL)
13624         add_child_die (context_die, old_die);
13625
13626       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13627       add_abstract_origin_attribute (subr_die, origin);
13628     }
13629   else if (old_die)
13630     {
13631       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13632       struct dwarf_file_data * file_index = lookup_filename (s.file);
13633
13634       if (!get_AT_flag (old_die, DW_AT_declaration)
13635           /* We can have a normal definition following an inline one in the
13636              case of redefinition of GNU C extern inlines.
13637              It seems reasonable to use AT_specification in this case.  */
13638           && !get_AT (old_die, DW_AT_inline))
13639         {
13640           /* Detect and ignore this case, where we are trying to output
13641              something we have already output.  */
13642           return;
13643         }
13644
13645       /* If the definition comes from the same place as the declaration,
13646          maybe use the old DIE.  We always want the DIE for this function
13647          that has the *_pc attributes to be under comp_unit_die so the
13648          debugger can find it.  We also need to do this for abstract
13649          instances of inlines, since the spec requires the out-of-line copy
13650          to have the same parent.  For local class methods, this doesn't
13651          apply; we just use the old DIE.  */
13652       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13653           && (DECL_ARTIFICIAL (decl)
13654               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13655                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13656                       == (unsigned) s.line))))
13657         {
13658           subr_die = old_die;
13659
13660           /* Clear out the declaration attribute and the formal parameters.
13661              Do not remove all children, because it is possible that this
13662              declaration die was forced using force_decl_die(). In such
13663              cases die that forced declaration die (e.g. TAG_imported_module)
13664              is one of the children that we do not want to remove.  */
13665           remove_AT (subr_die, DW_AT_declaration);
13666           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13667         }
13668       else
13669         {
13670           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13671           add_AT_specification (subr_die, old_die);
13672           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13673             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13674           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13675             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13676         }
13677     }
13678   else
13679     {
13680       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13681
13682       if (TREE_PUBLIC (decl))
13683         add_AT_flag (subr_die, DW_AT_external, 1);
13684
13685       add_name_and_src_coords_attributes (subr_die, decl);
13686       if (debug_info_level > DINFO_LEVEL_TERSE)
13687         {
13688           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13689           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13690                               0, 0, context_die);
13691         }
13692
13693       add_pure_or_virtual_attribute (subr_die, decl);
13694       if (DECL_ARTIFICIAL (decl))
13695         add_AT_flag (subr_die, DW_AT_artificial, 1);
13696
13697       if (TREE_PROTECTED (decl))
13698         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13699       else if (TREE_PRIVATE (decl))
13700         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13701     }
13702
13703   if (declaration)
13704     {
13705       if (!old_die || !get_AT (old_die, DW_AT_inline))
13706         {
13707           add_AT_flag (subr_die, DW_AT_declaration, 1);
13708
13709           /* The first time we see a member function, it is in the context of
13710              the class to which it belongs.  We make sure of this by emitting
13711              the class first.  The next time is the definition, which is
13712              handled above.  The two may come from the same source text.
13713
13714              Note that force_decl_die() forces function declaration die. It is
13715              later reused to represent definition.  */
13716           equate_decl_number_to_die (decl, subr_die);
13717         }
13718     }
13719   else if (DECL_ABSTRACT (decl))
13720     {
13721       if (DECL_DECLARED_INLINE_P (decl))
13722         {
13723           if (cgraph_function_possibly_inlined_p (decl))
13724             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13725           else
13726             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13727         }
13728       else
13729         {
13730           if (cgraph_function_possibly_inlined_p (decl))
13731             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13732           else
13733             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13734         }
13735
13736       if (DECL_DECLARED_INLINE_P (decl)
13737           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13738         add_AT_flag (subr_die, DW_AT_artificial, 1);
13739
13740       equate_decl_number_to_die (decl, subr_die);
13741     }
13742   else if (!DECL_EXTERNAL (decl))
13743     {
13744       HOST_WIDE_INT cfa_fb_offset;
13745
13746       if (!old_die || !get_AT (old_die, DW_AT_inline))
13747         equate_decl_number_to_die (decl, subr_die);
13748
13749       if (!flag_reorder_blocks_and_partition)
13750         {
13751           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13752                                        current_function_funcdef_no);
13753           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13754           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13755                                        current_function_funcdef_no);
13756           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13757
13758           add_pubname (decl, subr_die);
13759           add_arange (decl, subr_die);
13760         }
13761       else
13762         {  /* Do nothing for now; maybe need to duplicate die, one for
13763               hot section and one for cold section, then use the hot/cold
13764               section begin/end labels to generate the aranges...  */
13765           /*
13766             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13767             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13768             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13769             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13770
13771             add_pubname (decl, subr_die);
13772             add_arange (decl, subr_die);
13773             add_arange (decl, subr_die);
13774            */
13775         }
13776
13777 #ifdef MIPS_DEBUGGING_INFO
13778       /* Add a reference to the FDE for this routine.  */
13779       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13780 #endif
13781
13782       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13783
13784       /* We define the "frame base" as the function's CFA.  This is more
13785          convenient for several reasons: (1) It's stable across the prologue
13786          and epilogue, which makes it better than just a frame pointer,
13787          (2) With dwarf3, there exists a one-byte encoding that allows us
13788          to reference the .debug_frame data by proxy, but failing that,
13789          (3) We can at least reuse the code inspection and interpretation
13790          code that determines the CFA position at various points in the
13791          function.  */
13792       /* ??? Use some command-line or configury switch to enable the use
13793          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13794          consumers that understand it; fall back to "pure" dwarf2 and
13795          convert the CFA data into a location list.  */
13796       {
13797         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13798         if (list->dw_loc_next)
13799           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13800         else
13801           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13802       }
13803
13804       /* Compute a displacement from the "steady-state frame pointer" to
13805          the CFA.  The former is what all stack slots and argument slots
13806          will reference in the rtl; the later is what we've told the
13807          debugger about.  We'll need to adjust all frame_base references
13808          by this displacement.  */
13809       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13810
13811       if (cfun->static_chain_decl)
13812         add_AT_location_description (subr_die, DW_AT_static_link,
13813                  loc_descriptor_from_tree (cfun->static_chain_decl));
13814     }
13815
13816   /* Now output descriptions of the arguments for this function. This gets
13817      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13818      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13819      `...' at the end of the formal parameter list.  In order to find out if
13820      there was a trailing ellipsis or not, we must instead look at the type
13821      associated with the FUNCTION_DECL.  This will be a node of type
13822      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13823      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13824      an ellipsis at the end.  */
13825
13826   /* In the case where we are describing a mere function declaration, all we
13827      need to do here (and all we *can* do here) is to describe the *types* of
13828      its formal parameters.  */
13829   if (debug_info_level <= DINFO_LEVEL_TERSE)
13830     ;
13831   else if (declaration)
13832     gen_formal_types_die (decl, subr_die);
13833   else
13834     {
13835       /* Generate DIEs to represent all known formal parameters.  */
13836       tree arg_decls = DECL_ARGUMENTS (decl);
13837       tree parm;
13838
13839       /* When generating DIEs, generate the unspecified_parameters DIE
13840          instead if we come across the arg "__builtin_va_alist" */
13841       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13842         if (TREE_CODE (parm) == PARM_DECL)
13843           {
13844             if (DECL_NAME (parm)
13845                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13846                             "__builtin_va_alist"))
13847               gen_unspecified_parameters_die (parm, subr_die);
13848             else
13849               gen_decl_die (parm, NULL, subr_die);
13850           }
13851
13852       /* Decide whether we need an unspecified_parameters DIE at the end.
13853          There are 2 more cases to do this for: 1) the ansi ... declaration -
13854          this is detectable when the end of the arg list is not a
13855          void_type_node 2) an unprototyped function declaration (not a
13856          definition).  This just means that we have no info about the
13857          parameters at all.  */
13858       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13859       if (fn_arg_types != NULL)
13860         {
13861           /* This is the prototyped case, check for....  */
13862           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13863             gen_unspecified_parameters_die (decl, subr_die);
13864         }
13865       else if (DECL_INITIAL (decl) == NULL_TREE)
13866         gen_unspecified_parameters_die (decl, subr_die);
13867     }
13868
13869   /* Output Dwarf info for all of the stuff within the body of the function
13870      (if it has one - it may be just a declaration).  */
13871   outer_scope = DECL_INITIAL (decl);
13872
13873   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13874      a function.  This BLOCK actually represents the outermost binding contour
13875      for the function, i.e. the contour in which the function's formal
13876      parameters and labels get declared. Curiously, it appears that the front
13877      end doesn't actually put the PARM_DECL nodes for the current function onto
13878      the BLOCK_VARS list for this outer scope, but are strung off of the
13879      DECL_ARGUMENTS list for the function instead.
13880
13881      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13882      the LABEL_DECL nodes for the function however, and we output DWARF info
13883      for those in decls_for_scope.  Just within the `outer_scope' there will be
13884      a BLOCK node representing the function's outermost pair of curly braces,
13885      and any blocks used for the base and member initializers of a C++
13886      constructor function.  */
13887   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13888     {
13889       /* Emit a DW_TAG_variable DIE for a named return value.  */
13890       if (DECL_NAME (DECL_RESULT (decl)))
13891         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13892
13893       current_function_has_inlines = 0;
13894       decls_for_scope (outer_scope, subr_die, 0);
13895
13896 #if 0 && defined (MIPS_DEBUGGING_INFO)
13897       if (current_function_has_inlines)
13898         {
13899           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13900           if (! comp_unit_has_inlines)
13901             {
13902               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13903               comp_unit_has_inlines = 1;
13904             }
13905         }
13906 #endif
13907     }
13908   /* Add the calling convention attribute if requested.  */
13909   add_calling_convention_attribute (subr_die, decl);
13910
13911 }
13912
13913 /* Returns a hash value for X (which really is a die_struct).  */
13914
13915 static hashval_t
13916 common_block_die_table_hash (const void *x)
13917 {
13918   const_dw_die_ref d = (const_dw_die_ref) x;
13919   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13920 }
13921
13922 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13923    as decl_id and die_parent of die_struct Y.  */
13924
13925 static int
13926 common_block_die_table_eq (const void *x, const void *y)
13927 {
13928   const_dw_die_ref d = (const_dw_die_ref) x;
13929   const_dw_die_ref e = (const_dw_die_ref) y;
13930   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13931 }
13932
13933 /* Generate a DIE to represent a declared data object.
13934    Either DECL or ORIGIN must be non-null.  */
13935
13936 static void
13937 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13938 {
13939   HOST_WIDE_INT off;
13940   tree com_decl;
13941   tree decl_or_origin = decl ? decl : origin;
13942   dw_die_ref var_die;
13943   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13944   dw_die_ref origin_die;
13945   int declaration = (DECL_EXTERNAL (decl_or_origin)
13946                      /* If DECL is COMDAT and has not actually been
13947                         emitted, we cannot take its address; there
13948                         might end up being no definition anywhere in
13949                         the program.  For example, consider the C++
13950                         test case:
13951
13952                           template <class T>
13953                           struct S { static const int i = 7; };
13954
13955                           template <class T>
13956                           const int S<T>::i;
13957
13958                           int f() { return S<int>::i; }
13959
13960                         Here, S<int>::i is not DECL_EXTERNAL, but no
13961                         definition is required, so the compiler will
13962                         not emit a definition.  */
13963                      || (TREE_CODE (decl_or_origin) == VAR_DECL
13964                          && DECL_COMDAT (decl_or_origin)
13965                          && !TREE_ASM_WRITTEN (decl_or_origin))
13966                      || class_or_namespace_scope_p (context_die));
13967
13968   if (!origin)
13969     origin = decl_ultimate_origin (decl);
13970
13971   com_decl = fortran_common (decl_or_origin, &off);
13972
13973   /* Symbol in common gets emitted as a child of the common block, in the form
13974      of a data member.  */
13975   if (com_decl)
13976     {
13977       tree field;
13978       dw_die_ref com_die;
13979       dw_loc_descr_ref loc;
13980       die_node com_die_arg;
13981
13982       var_die = lookup_decl_die (decl_or_origin);
13983       if (var_die)
13984         {
13985           if (get_AT (var_die, DW_AT_location) == NULL)
13986             {
13987               loc = loc_descriptor_from_tree (com_decl);
13988               if (loc)
13989                 {
13990                   if (off)
13991                     {
13992                       /* Optimize the common case.  */
13993                       if (loc->dw_loc_opc == DW_OP_addr
13994                           && loc->dw_loc_next == NULL
13995                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13996                              == SYMBOL_REF)
13997                         loc->dw_loc_oprnd1.v.val_addr
13998                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13999                         else
14000                           add_loc_descr (&loc,
14001                                          new_loc_descr (DW_OP_plus_uconst,
14002                                                         off, 0));
14003                     }
14004                   add_AT_loc (var_die, DW_AT_location, loc);
14005                   remove_AT (var_die, DW_AT_declaration);
14006                 }
14007             }
14008           return;
14009         }
14010
14011       if (common_block_die_table == NULL)
14012         common_block_die_table
14013           = htab_create_ggc (10, common_block_die_table_hash,
14014                              common_block_die_table_eq, NULL);
14015
14016       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14017       com_die_arg.decl_id = DECL_UID (com_decl);
14018       com_die_arg.die_parent = context_die;
14019       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14020       loc = loc_descriptor_from_tree (com_decl);
14021       if (com_die == NULL)
14022         {
14023           const char *cnam
14024             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14025           void **slot;
14026
14027           com_die = new_die (DW_TAG_common_block, context_die, decl);
14028           add_name_and_src_coords_attributes (com_die, com_decl);
14029           if (loc)
14030             {
14031               add_AT_loc (com_die, DW_AT_location, loc);
14032               /* Avoid sharing the same loc descriptor between
14033                  DW_TAG_common_block and DW_TAG_variable.  */
14034               loc = loc_descriptor_from_tree (com_decl);
14035             }
14036           else if (DECL_EXTERNAL (decl))
14037             add_AT_flag (com_die, DW_AT_declaration, 1);
14038           add_pubname_string (cnam, com_die); /* ??? needed? */
14039           com_die->decl_id = DECL_UID (com_decl);
14040           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14041           *slot = (void *) com_die;
14042         }
14043       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14044         {
14045           add_AT_loc (com_die, DW_AT_location, loc);
14046           loc = loc_descriptor_from_tree (com_decl);
14047           remove_AT (com_die, DW_AT_declaration);
14048         }
14049       var_die = new_die (DW_TAG_variable, com_die, decl);
14050       add_name_and_src_coords_attributes (var_die, decl);
14051       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14052                           TREE_THIS_VOLATILE (decl), context_die);
14053       add_AT_flag (var_die, DW_AT_external, 1);
14054       if (loc)
14055         {
14056           if (off)
14057             {
14058               /* Optimize the common case.  */
14059               if (loc->dw_loc_opc == DW_OP_addr
14060                   && loc->dw_loc_next == NULL
14061                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14062                 loc->dw_loc_oprnd1.v.val_addr
14063                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14064               else
14065                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14066                                                     off, 0));
14067             }
14068           add_AT_loc (var_die, DW_AT_location, loc);
14069         }
14070       else if (DECL_EXTERNAL (decl))
14071         add_AT_flag (var_die, DW_AT_declaration, 1);
14072       equate_decl_number_to_die (decl, var_die);
14073       return;
14074     }
14075
14076   /* If the compiler emitted a definition for the DECL declaration
14077      and if we already emitted a DIE for it, don't emit a second
14078      DIE for it again.  */
14079   if (old_die
14080       && declaration
14081       && old_die->die_parent == context_die)
14082     return;
14083
14084   var_die = new_die (DW_TAG_variable, context_die, decl);
14085
14086   origin_die = NULL;
14087   if (origin != NULL)
14088     origin_die = add_abstract_origin_attribute (var_die, origin);
14089
14090   /* Loop unrolling can create multiple blocks that refer to the same
14091      static variable, so we must test for the DW_AT_declaration flag.
14092
14093      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14094      copy decls and set the DECL_ABSTRACT flag on them instead of
14095      sharing them.
14096
14097      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14098
14099      ??? The declare_in_namespace support causes us to get two DIEs for one
14100      variable, both of which are declarations.  We want to avoid considering
14101      one to be a specification, so we must test that this DIE is not a
14102      declaration.  */
14103   else if (old_die && TREE_STATIC (decl) && ! declaration
14104            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14105     {
14106       /* This is a definition of a C++ class level static.  */
14107       add_AT_specification (var_die, old_die);
14108       if (DECL_NAME (decl))
14109         {
14110           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14111           struct dwarf_file_data * file_index = lookup_filename (s.file);
14112
14113           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14114             add_AT_file (var_die, DW_AT_decl_file, file_index);
14115
14116           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14117             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14118         }
14119     }
14120   else
14121     {
14122       tree type = TREE_TYPE (decl);
14123
14124       add_name_and_src_coords_attributes (var_die, decl);
14125       if ((TREE_CODE (decl) == PARM_DECL
14126            || TREE_CODE (decl) == RESULT_DECL)
14127           && DECL_BY_REFERENCE (decl))
14128         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14129       else
14130         add_type_attribute (var_die, type, TREE_READONLY (decl),
14131                             TREE_THIS_VOLATILE (decl), context_die);
14132
14133       if (TREE_PUBLIC (decl))
14134         add_AT_flag (var_die, DW_AT_external, 1);
14135
14136       if (DECL_ARTIFICIAL (decl))
14137         add_AT_flag (var_die, DW_AT_artificial, 1);
14138
14139       if (TREE_PROTECTED (decl))
14140         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14141       else if (TREE_PRIVATE (decl))
14142         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14143     }
14144
14145   if (declaration)
14146     add_AT_flag (var_die, DW_AT_declaration, 1);
14147
14148   if (decl && (DECL_ABSTRACT (decl) || declaration))
14149     equate_decl_number_to_die (decl, var_die);
14150
14151   if (! declaration
14152       && (! DECL_ABSTRACT (decl_or_origin)
14153           /* Local static vars are shared between all clones/inlines,
14154              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14155              already set.  */
14156           || (TREE_CODE (decl_or_origin) == VAR_DECL
14157               && TREE_STATIC (decl_or_origin)
14158               && DECL_RTL_SET_P (decl_or_origin)))
14159       /* When abstract origin already has DW_AT_location attribute, no need
14160          to add it again.  */
14161       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14162     {
14163       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14164           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14165         defer_location (decl_or_origin, var_die);
14166       else
14167         add_location_or_const_value_attribute (var_die,
14168                                                decl_or_origin,
14169                                                DW_AT_location);
14170       add_pubname (decl_or_origin, var_die);
14171     }
14172   else
14173     tree_add_const_value_attribute (var_die, decl_or_origin);
14174 }
14175
14176 /* Generate a DIE to represent a named constant.  */
14177
14178 static void
14179 gen_const_die (tree decl, dw_die_ref context_die)
14180 {
14181   dw_die_ref const_die;
14182   tree type = TREE_TYPE (decl);
14183
14184   const_die = new_die (DW_TAG_constant, context_die, decl);
14185   add_name_and_src_coords_attributes (const_die, decl);
14186   add_type_attribute (const_die, type, 1, 0, context_die);
14187   if (TREE_PUBLIC (decl))
14188     add_AT_flag (const_die, DW_AT_external, 1);
14189   if (DECL_ARTIFICIAL (decl))
14190     add_AT_flag (const_die, DW_AT_artificial, 1);
14191   tree_add_const_value_attribute (const_die, decl);
14192 }
14193
14194 /* Generate a DIE to represent a label identifier.  */
14195
14196 static void
14197 gen_label_die (tree decl, dw_die_ref context_die)
14198 {
14199   tree origin = decl_ultimate_origin (decl);
14200   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14201   rtx insn;
14202   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14203
14204   if (origin != NULL)
14205     add_abstract_origin_attribute (lbl_die, origin);
14206   else
14207     add_name_and_src_coords_attributes (lbl_die, decl);
14208
14209   if (DECL_ABSTRACT (decl))
14210     equate_decl_number_to_die (decl, lbl_die);
14211   else
14212     {
14213       insn = DECL_RTL_IF_SET (decl);
14214
14215       /* Deleted labels are programmer specified labels which have been
14216          eliminated because of various optimizations.  We still emit them
14217          here so that it is possible to put breakpoints on them.  */
14218       if (insn
14219           && (LABEL_P (insn)
14220               || ((NOTE_P (insn)
14221                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14222         {
14223           /* When optimization is enabled (via -O) some parts of the compiler
14224              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14225              represent source-level labels which were explicitly declared by
14226              the user.  This really shouldn't be happening though, so catch
14227              it if it ever does happen.  */
14228           gcc_assert (!INSN_DELETED_P (insn));
14229
14230           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14231           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14232         }
14233     }
14234 }
14235
14236 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14237    attributes to the DIE for a block STMT, to describe where the inlined
14238    function was called from.  This is similar to add_src_coords_attributes.  */
14239
14240 static inline void
14241 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14242 {
14243   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14244
14245   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14246   add_AT_unsigned (die, DW_AT_call_line, s.line);
14247 }
14248
14249
14250 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14251    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14252
14253 static inline void
14254 add_high_low_attributes (tree stmt, dw_die_ref die)
14255 {
14256   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14257
14258   if (BLOCK_FRAGMENT_CHAIN (stmt))
14259     {
14260       tree chain;
14261
14262       if (inlined_function_outer_scope_p (stmt))
14263         {
14264           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14265                                        BLOCK_NUMBER (stmt));
14266           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14267         }
14268
14269       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14270
14271       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14272       do
14273         {
14274           add_ranges (chain);
14275           chain = BLOCK_FRAGMENT_CHAIN (chain);
14276         }
14277       while (chain);
14278       add_ranges (NULL);
14279     }
14280   else
14281     {
14282       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14283                                    BLOCK_NUMBER (stmt));
14284       add_AT_lbl_id (die, DW_AT_low_pc, label);
14285       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14286                                    BLOCK_NUMBER (stmt));
14287       add_AT_lbl_id (die, DW_AT_high_pc, label);
14288     }
14289 }
14290
14291 /* Generate a DIE for a lexical block.  */
14292
14293 static void
14294 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14295 {
14296   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14297
14298   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14299     add_high_low_attributes (stmt, stmt_die);
14300
14301   decls_for_scope (stmt, stmt_die, depth);
14302 }
14303
14304 /* Generate a DIE for an inlined subprogram.  */
14305
14306 static void
14307 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14308 {
14309   tree decl;
14310
14311   /* The instance of function that is effectively being inlined shall not
14312      be abstract.  */
14313   gcc_assert (! BLOCK_ABSTRACT (stmt));
14314
14315   decl = block_ultimate_origin (stmt);
14316
14317   /* Emit info for the abstract instance first, if we haven't yet.  We
14318      must emit this even if the block is abstract, otherwise when we
14319      emit the block below (or elsewhere), we may end up trying to emit
14320      a die whose origin die hasn't been emitted, and crashing.  */
14321   dwarf2out_abstract_function (decl);
14322
14323   if (! BLOCK_ABSTRACT (stmt))
14324     {
14325       dw_die_ref subr_die
14326         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14327
14328       add_abstract_origin_attribute (subr_die, decl);
14329       if (TREE_ASM_WRITTEN (stmt))
14330         add_high_low_attributes (stmt, subr_die);
14331       add_call_src_coords_attributes (stmt, subr_die);
14332
14333       decls_for_scope (stmt, subr_die, depth);
14334       current_function_has_inlines = 1;
14335     }
14336 }
14337
14338 /* Generate a DIE for a field in a record, or structure.  */
14339
14340 static void
14341 gen_field_die (tree decl, dw_die_ref context_die)
14342 {
14343   dw_die_ref decl_die;
14344
14345   if (TREE_TYPE (decl) == error_mark_node)
14346     return;
14347
14348   decl_die = new_die (DW_TAG_member, context_die, decl);
14349   add_name_and_src_coords_attributes (decl_die, decl);
14350   add_type_attribute (decl_die, member_declared_type (decl),
14351                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14352                       context_die);
14353
14354   if (DECL_BIT_FIELD_TYPE (decl))
14355     {
14356       add_byte_size_attribute (decl_die, decl);
14357       add_bit_size_attribute (decl_die, decl);
14358       add_bit_offset_attribute (decl_die, decl);
14359     }
14360
14361   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14362     add_data_member_location_attribute (decl_die, decl);
14363
14364   if (DECL_ARTIFICIAL (decl))
14365     add_AT_flag (decl_die, DW_AT_artificial, 1);
14366
14367   if (TREE_PROTECTED (decl))
14368     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14369   else if (TREE_PRIVATE (decl))
14370     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14371
14372   /* Equate decl number to die, so that we can look up this decl later on.  */
14373   equate_decl_number_to_die (decl, decl_die);
14374 }
14375
14376 #if 0
14377 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14378    Use modified_type_die instead.
14379    We keep this code here just in case these types of DIEs may be needed to
14380    represent certain things in other languages (e.g. Pascal) someday.  */
14381
14382 static void
14383 gen_pointer_type_die (tree type, dw_die_ref context_die)
14384 {
14385   dw_die_ref ptr_die
14386     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14387
14388   equate_type_number_to_die (type, ptr_die);
14389   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14390   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14391 }
14392
14393 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14394    Use modified_type_die instead.
14395    We keep this code here just in case these types of DIEs may be needed to
14396    represent certain things in other languages (e.g. Pascal) someday.  */
14397
14398 static void
14399 gen_reference_type_die (tree type, dw_die_ref context_die)
14400 {
14401   dw_die_ref ref_die
14402     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14403
14404   equate_type_number_to_die (type, ref_die);
14405   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14406   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14407 }
14408 #endif
14409
14410 /* Generate a DIE for a pointer to a member type.  */
14411
14412 static void
14413 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14414 {
14415   dw_die_ref ptr_die
14416     = new_die (DW_TAG_ptr_to_member_type,
14417                scope_die_for (type, context_die), type);
14418
14419   equate_type_number_to_die (type, ptr_die);
14420   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14421                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14422   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14423 }
14424
14425 /* Generate the DIE for the compilation unit.  */
14426
14427 static dw_die_ref
14428 gen_compile_unit_die (const char *filename)
14429 {
14430   dw_die_ref die;
14431   char producer[250];
14432   const char *language_string = lang_hooks.name;
14433   int language;
14434
14435   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14436
14437   if (filename)
14438     {
14439       add_name_attribute (die, filename);
14440       /* Don't add cwd for <built-in>.  */
14441       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14442         add_comp_dir_attribute (die);
14443     }
14444
14445   sprintf (producer, "%s %s", language_string, version_string);
14446
14447 #ifdef MIPS_DEBUGGING_INFO
14448   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14449      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14450      not appear in the producer string, the debugger reaches the conclusion
14451      that the object file is stripped and has no debugging information.
14452      To get the MIPS/SGI debugger to believe that there is debugging
14453      information in the object file, we add a -g to the producer string.  */
14454   if (debug_info_level > DINFO_LEVEL_TERSE)
14455     strcat (producer, " -g");
14456 #endif
14457
14458   add_AT_string (die, DW_AT_producer, producer);
14459
14460   if (strcmp (language_string, "GNU C++") == 0)
14461     language = DW_LANG_C_plus_plus;
14462   else if (strcmp (language_string, "GNU Ada") == 0)
14463     language = DW_LANG_Ada95;
14464   else if (strcmp (language_string, "GNU F77") == 0)
14465     language = DW_LANG_Fortran77;
14466   else if (strcmp (language_string, "GNU Fortran") == 0)
14467     language = DW_LANG_Fortran95;
14468   else if (strcmp (language_string, "GNU Pascal") == 0)
14469     language = DW_LANG_Pascal83;
14470   else if (strcmp (language_string, "GNU Java") == 0)
14471     language = DW_LANG_Java;
14472   else if (strcmp (language_string, "GNU Objective-C") == 0)
14473     language = DW_LANG_ObjC;
14474   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14475     language = DW_LANG_ObjC_plus_plus;
14476   else
14477     language = DW_LANG_C89;
14478
14479   add_AT_unsigned (die, DW_AT_language, language);
14480   return die;
14481 }
14482
14483 /* Generate the DIE for a base class.  */
14484
14485 static void
14486 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14487 {
14488   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14489
14490   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14491   add_data_member_location_attribute (die, binfo);
14492
14493   if (BINFO_VIRTUAL_P (binfo))
14494     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14495
14496   if (access == access_public_node)
14497     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14498   else if (access == access_protected_node)
14499     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14500 }
14501
14502 /* Generate a DIE for a class member.  */
14503
14504 static void
14505 gen_member_die (tree type, dw_die_ref context_die)
14506 {
14507   tree member;
14508   tree binfo = TYPE_BINFO (type);
14509   dw_die_ref child;
14510
14511   /* If this is not an incomplete type, output descriptions of each of its
14512      members. Note that as we output the DIEs necessary to represent the
14513      members of this record or union type, we will also be trying to output
14514      DIEs to represent the *types* of those members. However the `type'
14515      function (above) will specifically avoid generating type DIEs for member
14516      types *within* the list of member DIEs for this (containing) type except
14517      for those types (of members) which are explicitly marked as also being
14518      members of this (containing) type themselves.  The g++ front- end can
14519      force any given type to be treated as a member of some other (containing)
14520      type by setting the TYPE_CONTEXT of the given (member) type to point to
14521      the TREE node representing the appropriate (containing) type.  */
14522
14523   /* First output info about the base classes.  */
14524   if (binfo)
14525     {
14526       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14527       int i;
14528       tree base;
14529
14530       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14531         gen_inheritance_die (base,
14532                              (accesses ? VEC_index (tree, accesses, i)
14533                               : access_public_node), context_die);
14534     }
14535
14536   /* Now output info about the data members and type members.  */
14537   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14538     {
14539       /* If we thought we were generating minimal debug info for TYPE
14540          and then changed our minds, some of the member declarations
14541          may have already been defined.  Don't define them again, but
14542          do put them in the right order.  */
14543
14544       child = lookup_decl_die (member);
14545       if (child)
14546         splice_child_die (context_die, child);
14547       else
14548         gen_decl_die (member, NULL, context_die);
14549     }
14550
14551   /* Now output info about the function members (if any).  */
14552   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14553     {
14554       /* Don't include clones in the member list.  */
14555       if (DECL_ABSTRACT_ORIGIN (member))
14556         continue;
14557
14558       child = lookup_decl_die (member);
14559       if (child)
14560         splice_child_die (context_die, child);
14561       else
14562         gen_decl_die (member, NULL, context_die);
14563     }
14564 }
14565
14566 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14567    is set, we pretend that the type was never defined, so we only get the
14568    member DIEs needed by later specification DIEs.  */
14569
14570 static void
14571 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14572                                 enum debug_info_usage usage)
14573 {
14574   dw_die_ref type_die = lookup_type_die (type);
14575   dw_die_ref scope_die = 0;
14576   int nested = 0;
14577   int complete = (TYPE_SIZE (type)
14578                   && (! TYPE_STUB_DECL (type)
14579                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14580   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14581   complete = complete && should_emit_struct_debug (type, usage);
14582
14583   if (type_die && ! complete)
14584     return;
14585
14586   if (TYPE_CONTEXT (type) != NULL_TREE
14587       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14588           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14589     nested = 1;
14590
14591   scope_die = scope_die_for (type, context_die);
14592
14593   if (! type_die || (nested && scope_die == comp_unit_die))
14594     /* First occurrence of type or toplevel definition of nested class.  */
14595     {
14596       dw_die_ref old_die = type_die;
14597
14598       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14599                           ? record_type_tag (type) : DW_TAG_union_type,
14600                           scope_die, type);
14601       equate_type_number_to_die (type, type_die);
14602       if (old_die)
14603         add_AT_specification (type_die, old_die);
14604       else
14605         add_name_attribute (type_die, type_tag (type));
14606     }
14607   else
14608     remove_AT (type_die, DW_AT_declaration);
14609
14610   /* If this type has been completed, then give it a byte_size attribute and
14611      then give a list of members.  */
14612   if (complete && !ns_decl)
14613     {
14614       /* Prevent infinite recursion in cases where the type of some member of
14615          this type is expressed in terms of this type itself.  */
14616       TREE_ASM_WRITTEN (type) = 1;
14617       add_byte_size_attribute (type_die, type);
14618       if (TYPE_STUB_DECL (type) != NULL_TREE)
14619         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14620
14621       /* If the first reference to this type was as the return type of an
14622          inline function, then it may not have a parent.  Fix this now.  */
14623       if (type_die->die_parent == NULL)
14624         add_child_die (scope_die, type_die);
14625
14626       push_decl_scope (type);
14627       gen_member_die (type, type_die);
14628       pop_decl_scope ();
14629
14630       /* GNU extension: Record what type our vtable lives in.  */
14631       if (TYPE_VFIELD (type))
14632         {
14633           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14634
14635           gen_type_die (vtype, context_die);
14636           add_AT_die_ref (type_die, DW_AT_containing_type,
14637                           lookup_type_die (vtype));
14638         }
14639     }
14640   else
14641     {
14642       add_AT_flag (type_die, DW_AT_declaration, 1);
14643
14644       /* We don't need to do this for function-local types.  */
14645       if (TYPE_STUB_DECL (type)
14646           && ! decl_function_context (TYPE_STUB_DECL (type)))
14647         VEC_safe_push (tree, gc, incomplete_types, type);
14648     }
14649
14650   if (get_AT (type_die, DW_AT_name))
14651     add_pubtype (type, type_die);
14652 }
14653
14654 /* Generate a DIE for a subroutine _type_.  */
14655
14656 static void
14657 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14658 {
14659   tree return_type = TREE_TYPE (type);
14660   dw_die_ref subr_die
14661     = new_die (DW_TAG_subroutine_type,
14662                scope_die_for (type, context_die), type);
14663
14664   equate_type_number_to_die (type, subr_die);
14665   add_prototyped_attribute (subr_die, type);
14666   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14667   gen_formal_types_die (type, subr_die);
14668
14669   if (get_AT (subr_die, DW_AT_name))
14670     add_pubtype (type, subr_die);
14671 }
14672
14673 /* Generate a DIE for a type definition.  */
14674
14675 static void
14676 gen_typedef_die (tree decl, dw_die_ref context_die)
14677 {
14678   dw_die_ref type_die;
14679   tree origin;
14680
14681   if (TREE_ASM_WRITTEN (decl))
14682     return;
14683
14684   TREE_ASM_WRITTEN (decl) = 1;
14685   type_die = new_die (DW_TAG_typedef, context_die, decl);
14686   origin = decl_ultimate_origin (decl);
14687   if (origin != NULL)
14688     add_abstract_origin_attribute (type_die, origin);
14689   else
14690     {
14691       tree type;
14692
14693       add_name_and_src_coords_attributes (type_die, decl);
14694       if (DECL_ORIGINAL_TYPE (decl))
14695         {
14696           type = DECL_ORIGINAL_TYPE (decl);
14697
14698           gcc_assert (type != TREE_TYPE (decl));
14699           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14700         }
14701       else
14702         type = TREE_TYPE (decl);
14703
14704       add_type_attribute (type_die, type, TREE_READONLY (decl),
14705                           TREE_THIS_VOLATILE (decl), context_die);
14706     }
14707
14708   if (DECL_ABSTRACT (decl))
14709     equate_decl_number_to_die (decl, type_die);
14710
14711   if (get_AT (type_die, DW_AT_name))
14712     add_pubtype (decl, type_die);
14713 }
14714
14715 /* Generate a type description DIE.  */
14716
14717 static void
14718 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14719                                 enum debug_info_usage usage)
14720 {
14721   int need_pop;
14722   struct array_descr_info info;
14723
14724   if (type == NULL_TREE || type == error_mark_node)
14725     return;
14726
14727   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14728       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14729     {
14730       if (TREE_ASM_WRITTEN (type))
14731         return;
14732
14733       /* Prevent broken recursion; we can't hand off to the same type.  */
14734       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14735
14736       /* Use the DIE of the containing namespace as the parent DIE of
14737          the type description DIE we want to generate.  */
14738       if (DECL_CONTEXT (TYPE_NAME (type))
14739           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
14740         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
14741
14742       TREE_ASM_WRITTEN (type) = 1;
14743       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14744       return;
14745     }
14746
14747   /* If this is an array type with hidden descriptor, handle it first.  */
14748   if (!TREE_ASM_WRITTEN (type)
14749       && lang_hooks.types.get_array_descr_info
14750       && lang_hooks.types.get_array_descr_info (type, &info))
14751     {
14752       gen_descr_array_type_die (type, &info, context_die);
14753       TREE_ASM_WRITTEN (type) = 1;
14754       return;
14755     }
14756
14757   /* We are going to output a DIE to represent the unqualified version
14758      of this type (i.e. without any const or volatile qualifiers) so
14759      get the main variant (i.e. the unqualified version) of this type
14760      now.  (Vectors are special because the debugging info is in the
14761      cloned type itself).  */
14762   if (TREE_CODE (type) != VECTOR_TYPE)
14763     type = type_main_variant (type);
14764
14765   if (TREE_ASM_WRITTEN (type))
14766     return;
14767
14768   switch (TREE_CODE (type))
14769     {
14770     case ERROR_MARK:
14771       break;
14772
14773     case POINTER_TYPE:
14774     case REFERENCE_TYPE:
14775       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14776          ensures that the gen_type_die recursion will terminate even if the
14777          type is recursive.  Recursive types are possible in Ada.  */
14778       /* ??? We could perhaps do this for all types before the switch
14779          statement.  */
14780       TREE_ASM_WRITTEN (type) = 1;
14781
14782       /* For these types, all that is required is that we output a DIE (or a
14783          set of DIEs) to represent the "basis" type.  */
14784       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14785                                 DINFO_USAGE_IND_USE);
14786       break;
14787
14788     case OFFSET_TYPE:
14789       /* This code is used for C++ pointer-to-data-member types.
14790          Output a description of the relevant class type.  */
14791       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14792                                         DINFO_USAGE_IND_USE);
14793
14794       /* Output a description of the type of the object pointed to.  */
14795       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14796                                         DINFO_USAGE_IND_USE);
14797
14798       /* Now output a DIE to represent this pointer-to-data-member type
14799          itself.  */
14800       gen_ptr_to_mbr_type_die (type, context_die);
14801       break;
14802
14803     case FUNCTION_TYPE:
14804       /* Force out return type (in case it wasn't forced out already).  */
14805       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14806                                         DINFO_USAGE_DIR_USE);
14807       gen_subroutine_type_die (type, context_die);
14808       break;
14809
14810     case METHOD_TYPE:
14811       /* Force out return type (in case it wasn't forced out already).  */
14812       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14813                                         DINFO_USAGE_DIR_USE);
14814       gen_subroutine_type_die (type, context_die);
14815       break;
14816
14817     case ARRAY_TYPE:
14818       gen_array_type_die (type, context_die);
14819       break;
14820
14821     case VECTOR_TYPE:
14822       gen_array_type_die (type, context_die);
14823       break;
14824
14825     case ENUMERAL_TYPE:
14826     case RECORD_TYPE:
14827     case UNION_TYPE:
14828     case QUAL_UNION_TYPE:
14829       /* If this is a nested type whose containing class hasn't been written
14830          out yet, writing it out will cover this one, too.  This does not apply
14831          to instantiations of member class templates; they need to be added to
14832          the containing class as they are generated.  FIXME: This hurts the
14833          idea of combining type decls from multiple TUs, since we can't predict
14834          what set of template instantiations we'll get.  */
14835       if (TYPE_CONTEXT (type)
14836           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14837           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14838         {
14839           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14840
14841           if (TREE_ASM_WRITTEN (type))
14842             return;
14843
14844           /* If that failed, attach ourselves to the stub.  */
14845           push_decl_scope (TYPE_CONTEXT (type));
14846           context_die = lookup_type_die (TYPE_CONTEXT (type));
14847           need_pop = 1;
14848         }
14849       else
14850         {
14851           context_die = declare_in_namespace (type, context_die);
14852           need_pop = 0;
14853         }
14854
14855       if (TREE_CODE (type) == ENUMERAL_TYPE)
14856         {
14857           /* This might have been written out by the call to
14858              declare_in_namespace.  */
14859           if (!TREE_ASM_WRITTEN (type))
14860             gen_enumeration_type_die (type, context_die);
14861         }
14862       else
14863         gen_struct_or_union_type_die (type, context_die, usage);
14864
14865       if (need_pop)
14866         pop_decl_scope ();
14867
14868       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14869          it up if it is ever completed.  gen_*_type_die will set it for us
14870          when appropriate.  */
14871       return;
14872
14873     case VOID_TYPE:
14874     case INTEGER_TYPE:
14875     case REAL_TYPE:
14876     case FIXED_POINT_TYPE:
14877     case COMPLEX_TYPE:
14878     case BOOLEAN_TYPE:
14879       /* No DIEs needed for fundamental types.  */
14880       break;
14881
14882     case LANG_TYPE:
14883       /* No Dwarf representation currently defined.  */
14884       break;
14885
14886     default:
14887       gcc_unreachable ();
14888     }
14889
14890   TREE_ASM_WRITTEN (type) = 1;
14891 }
14892
14893 static void
14894 gen_type_die (tree type, dw_die_ref context_die)
14895 {
14896   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14897 }
14898
14899 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14900    things which are local to the given block.  */
14901
14902 static void
14903 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14904 {
14905   int must_output_die = 0;
14906   bool inlined_func;
14907
14908   /* Ignore blocks that are NULL.  */
14909   if (stmt == NULL_TREE)
14910     return;
14911
14912   inlined_func = inlined_function_outer_scope_p (stmt);
14913
14914   /* If the block is one fragment of a non-contiguous block, do not
14915      process the variables, since they will have been done by the
14916      origin block.  Do process subblocks.  */
14917   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14918     {
14919       tree sub;
14920
14921       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14922         gen_block_die (sub, context_die, depth + 1);
14923
14924       return;
14925     }
14926
14927   /* Determine if we need to output any Dwarf DIEs at all to represent this
14928      block.  */
14929   if (inlined_func)
14930     /* The outer scopes for inlinings *must* always be represented.  We
14931        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14932     must_output_die = 1;
14933   else
14934     {
14935       /* Determine if this block directly contains any "significant"
14936          local declarations which we will need to output DIEs for.  */
14937       if (debug_info_level > DINFO_LEVEL_TERSE)
14938         /* We are not in terse mode so *any* local declaration counts
14939            as being a "significant" one.  */
14940         must_output_die = ((BLOCK_VARS (stmt) != NULL
14941                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14942                            && (TREE_USED (stmt)
14943                                || TREE_ASM_WRITTEN (stmt)
14944                                || BLOCK_ABSTRACT (stmt)));
14945       else if ((TREE_USED (stmt)
14946                 || TREE_ASM_WRITTEN (stmt)
14947                 || BLOCK_ABSTRACT (stmt))
14948                && !dwarf2out_ignore_block (stmt))
14949         must_output_die = 1;
14950     }
14951
14952   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14953      DIE for any block which contains no significant local declarations at
14954      all.  Rather, in such cases we just call `decls_for_scope' so that any
14955      needed Dwarf info for any sub-blocks will get properly generated. Note
14956      that in terse mode, our definition of what constitutes a "significant"
14957      local declaration gets restricted to include only inlined function
14958      instances and local (nested) function definitions.  */
14959   if (must_output_die)
14960     {
14961       if (inlined_func)
14962         {
14963           /* If STMT block is abstract, that means we have been called
14964              indirectly from dwarf2out_abstract_function.
14965              That function rightfully marks the descendent blocks (of
14966              the abstract function it is dealing with) as being abstract,
14967              precisely to prevent us from emitting any
14968              DW_TAG_inlined_subroutine DIE as a descendent
14969              of an abstract function instance. So in that case, we should
14970              not call gen_inlined_subroutine_die.
14971
14972              Later though, when cgraph asks dwarf2out to emit info
14973              for the concrete instance of the function decl into which
14974              the concrete instance of STMT got inlined, the later will lead
14975              to the generation of a DW_TAG_inlined_subroutine DIE.  */
14976           if (! BLOCK_ABSTRACT (stmt))
14977             gen_inlined_subroutine_die (stmt, context_die, depth);
14978         }
14979       else
14980         gen_lexical_block_die (stmt, context_die, depth);
14981     }
14982   else
14983     decls_for_scope (stmt, context_die, depth);
14984 }
14985
14986 /* Process variable DECL (or variable with origin ORIGIN) within
14987    block STMT and add it to CONTEXT_DIE.  */
14988 static void
14989 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14990 {
14991   dw_die_ref die;
14992   tree decl_or_origin = decl ? decl : origin;
14993   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14994
14995   if (ultimate_origin)
14996     origin = ultimate_origin;
14997
14998   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14999     die = lookup_decl_die (decl_or_origin);
15000   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15001            && TYPE_DECL_IS_STUB (decl_or_origin))
15002     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15003   else
15004     die = NULL;
15005
15006   if (die != NULL && die->die_parent == NULL)
15007     add_child_die (context_die, die);
15008   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15009     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15010                                          stmt, context_die);
15011   else
15012     gen_decl_die (decl, origin, context_die);
15013 }
15014
15015 /* Generate all of the decls declared within a given scope and (recursively)
15016    all of its sub-blocks.  */
15017
15018 static void
15019 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15020 {
15021   tree decl;
15022   unsigned int i;
15023   tree subblocks;
15024
15025   /* Ignore NULL blocks.  */
15026   if (stmt == NULL_TREE)
15027     return;
15028
15029   /* Output the DIEs to represent all of the data objects and typedefs
15030      declared directly within this block but not within any nested
15031      sub-blocks.  Also, nested function and tag DIEs have been
15032      generated with a parent of NULL; fix that up now.  */
15033   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15034     process_scope_var (stmt, decl, NULL_TREE, context_die);
15035   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15036     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15037                        context_die);
15038
15039   /* If we're at -g1, we're not interested in subblocks.  */
15040   if (debug_info_level <= DINFO_LEVEL_TERSE)
15041     return;
15042
15043   /* Output the DIEs to represent all sub-blocks (and the items declared
15044      therein) of this block.  */
15045   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15046        subblocks != NULL;
15047        subblocks = BLOCK_CHAIN (subblocks))
15048     gen_block_die (subblocks, context_die, depth + 1);
15049 }
15050
15051 /* Is this a typedef we can avoid emitting?  */
15052
15053 static inline int
15054 is_redundant_typedef (const_tree decl)
15055 {
15056   if (TYPE_DECL_IS_STUB (decl))
15057     return 1;
15058
15059   if (DECL_ARTIFICIAL (decl)
15060       && DECL_CONTEXT (decl)
15061       && is_tagged_type (DECL_CONTEXT (decl))
15062       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15063       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15064     /* Also ignore the artificial member typedef for the class name.  */
15065     return 1;
15066
15067   return 0;
15068 }
15069
15070 /* Returns the DIE for a context.  */
15071
15072 static inline dw_die_ref
15073 get_context_die (tree context)
15074 {
15075   if (context)
15076     {
15077       /* Find die that represents this context.  */
15078       if (TYPE_P (context))
15079         return force_type_die (context);
15080       else
15081         return force_decl_die (context);
15082     }
15083   return comp_unit_die;
15084 }
15085
15086 /* Returns the DIE for decl.  A DIE will always be returned.  */
15087
15088 static dw_die_ref
15089 force_decl_die (tree decl)
15090 {
15091   dw_die_ref decl_die;
15092   unsigned saved_external_flag;
15093   tree save_fn = NULL_TREE;
15094   decl_die = lookup_decl_die (decl);
15095   if (!decl_die)
15096     {
15097       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15098
15099       decl_die = lookup_decl_die (decl);
15100       if (decl_die)
15101         return decl_die;
15102
15103       switch (TREE_CODE (decl))
15104         {
15105         case FUNCTION_DECL:
15106           /* Clear current_function_decl, so that gen_subprogram_die thinks
15107              that this is a declaration. At this point, we just want to force
15108              declaration die.  */
15109           save_fn = current_function_decl;
15110           current_function_decl = NULL_TREE;
15111           gen_subprogram_die (decl, context_die);
15112           current_function_decl = save_fn;
15113           break;
15114
15115         case VAR_DECL:
15116           /* Set external flag to force declaration die. Restore it after
15117            gen_decl_die() call.  */
15118           saved_external_flag = DECL_EXTERNAL (decl);
15119           DECL_EXTERNAL (decl) = 1;
15120           gen_decl_die (decl, NULL, context_die);
15121           DECL_EXTERNAL (decl) = saved_external_flag;
15122           break;
15123
15124         case NAMESPACE_DECL:
15125           dwarf2out_decl (decl);
15126           break;
15127
15128         default:
15129           gcc_unreachable ();
15130         }
15131
15132       /* We should be able to find the DIE now.  */
15133       if (!decl_die)
15134         decl_die = lookup_decl_die (decl);
15135       gcc_assert (decl_die);
15136     }
15137
15138   return decl_die;
15139 }
15140
15141 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15142    always returned.  */
15143
15144 static dw_die_ref
15145 force_type_die (tree type)
15146 {
15147   dw_die_ref type_die;
15148
15149   type_die = lookup_type_die (type);
15150   if (!type_die)
15151     {
15152       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15153
15154       type_die = modified_type_die (type, TYPE_READONLY (type),
15155                                     TYPE_VOLATILE (type), context_die);
15156       gcc_assert (type_die);
15157     }
15158   return type_die;
15159 }
15160
15161 /* Force out any required namespaces to be able to output DECL,
15162    and return the new context_die for it, if it's changed.  */
15163
15164 static dw_die_ref
15165 setup_namespace_context (tree thing, dw_die_ref context_die)
15166 {
15167   tree context = (DECL_P (thing)
15168                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15169   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15170     /* Force out the namespace.  */
15171     context_die = force_decl_die (context);
15172
15173   return context_die;
15174 }
15175
15176 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15177    type) within its namespace, if appropriate.
15178
15179    For compatibility with older debuggers, namespace DIEs only contain
15180    declarations; all definitions are emitted at CU scope.  */
15181
15182 static dw_die_ref
15183 declare_in_namespace (tree thing, dw_die_ref context_die)
15184 {
15185   dw_die_ref ns_context;
15186
15187   if (debug_info_level <= DINFO_LEVEL_TERSE)
15188     return context_die;
15189
15190   /* If this decl is from an inlined function, then don't try to emit it in its
15191      namespace, as we will get confused.  It would have already been emitted
15192      when the abstract instance of the inline function was emitted anyways.  */
15193   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15194     return context_die;
15195
15196   ns_context = setup_namespace_context (thing, context_die);
15197
15198   if (ns_context != context_die)
15199     {
15200       if (is_fortran ())
15201         return ns_context;
15202       if (DECL_P (thing))
15203         gen_decl_die (thing, NULL, ns_context);
15204       else
15205         gen_type_die (thing, ns_context);
15206     }
15207   return context_die;
15208 }
15209
15210 /* Generate a DIE for a namespace or namespace alias.  */
15211
15212 static void
15213 gen_namespace_die (tree decl, dw_die_ref context_die)
15214 {
15215   dw_die_ref namespace_die;
15216
15217   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15218      they are an alias of.  */
15219   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15220     {
15221       /* Output a real namespace or module.  */
15222       context_die = setup_namespace_context (decl, comp_unit_die);
15223       namespace_die = new_die (is_fortran ()
15224                                ? DW_TAG_module : DW_TAG_namespace,
15225                                context_die, decl);
15226       /* For Fortran modules defined in different CU don't add src coords.  */
15227       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15228         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15229       else
15230         add_name_and_src_coords_attributes (namespace_die, decl);
15231       if (DECL_EXTERNAL (decl))
15232         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15233       equate_decl_number_to_die (decl, namespace_die);
15234     }
15235   else
15236     {
15237       /* Output a namespace alias.  */
15238
15239       /* Force out the namespace we are an alias of, if necessary.  */
15240       dw_die_ref origin_die
15241         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15242
15243       if (DECL_CONTEXT (decl) == NULL_TREE
15244           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15245         context_die = setup_namespace_context (decl, comp_unit_die);
15246       /* Now create the namespace alias DIE.  */
15247       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15248       add_name_and_src_coords_attributes (namespace_die, decl);
15249       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15250       equate_decl_number_to_die (decl, namespace_die);
15251     }
15252 }
15253
15254 /* Generate Dwarf debug information for a decl described by DECL.  */
15255
15256 static void
15257 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15258 {
15259   tree decl_or_origin = decl ? decl : origin;
15260   tree class_origin = NULL;
15261
15262   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15263     return;
15264
15265   switch (TREE_CODE (decl_or_origin))
15266     {
15267     case ERROR_MARK:
15268       break;
15269
15270     case CONST_DECL:
15271       if (!is_fortran ())
15272         {
15273           /* The individual enumerators of an enum type get output when we output
15274              the Dwarf representation of the relevant enum type itself.  */
15275           break;
15276         }
15277
15278       /* Emit its type.  */
15279       gen_type_die (TREE_TYPE (decl), context_die);
15280
15281       /* And its containing namespace.  */
15282       context_die = declare_in_namespace (decl, context_die);
15283
15284       gen_const_die (decl, context_die);
15285       break;
15286
15287     case FUNCTION_DECL:
15288       /* Don't output any DIEs to represent mere function declarations,
15289          unless they are class members or explicit block externs.  */
15290       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15291           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15292           && (current_function_decl == NULL_TREE
15293               || DECL_ARTIFICIAL (decl_or_origin)))
15294         break;
15295
15296 #if 0
15297       /* FIXME */
15298       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15299          on local redeclarations of global functions.  That seems broken.  */
15300       if (current_function_decl != decl)
15301         /* This is only a declaration.  */;
15302 #endif
15303
15304       /* If we're emitting a clone, emit info for the abstract instance.  */
15305       if (origin || DECL_ORIGIN (decl) != decl)
15306         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15307
15308       /* If we're emitting an out-of-line copy of an inline function,
15309          emit info for the abstract instance and set up to refer to it.  */
15310       else if (cgraph_function_possibly_inlined_p (decl)
15311                && ! DECL_ABSTRACT (decl)
15312                && ! class_or_namespace_scope_p (context_die)
15313                /* dwarf2out_abstract_function won't emit a die if this is just
15314                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15315                   that case, because that works only if we have a die.  */
15316                && DECL_INITIAL (decl) != NULL_TREE)
15317         {
15318           dwarf2out_abstract_function (decl);
15319           set_decl_origin_self (decl);
15320         }
15321
15322       /* Otherwise we're emitting the primary DIE for this decl.  */
15323       else if (debug_info_level > DINFO_LEVEL_TERSE)
15324         {
15325           /* Before we describe the FUNCTION_DECL itself, make sure that we
15326              have described its return type.  */
15327           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15328
15329           /* And its virtual context.  */
15330           if (DECL_VINDEX (decl) != NULL_TREE)
15331             gen_type_die (DECL_CONTEXT (decl), context_die);
15332
15333           /* And its containing type.  */
15334           if (!origin)
15335             origin = decl_class_context (decl);
15336           if (origin != NULL_TREE)
15337             gen_type_die_for_member (origin, decl, context_die);
15338
15339           /* And its containing namespace.  */
15340           context_die = declare_in_namespace (decl, context_die);
15341         }
15342
15343       /* Now output a DIE to represent the function itself.  */
15344       if (decl)
15345         gen_subprogram_die (decl, context_die);
15346       break;
15347
15348     case TYPE_DECL:
15349       /* If we are in terse mode, don't generate any DIEs to represent any
15350          actual typedefs.  */
15351       if (debug_info_level <= DINFO_LEVEL_TERSE)
15352         break;
15353
15354       /* In the special case of a TYPE_DECL node representing the declaration
15355          of some type tag, if the given TYPE_DECL is marked as having been
15356          instantiated from some other (original) TYPE_DECL node (e.g. one which
15357          was generated within the original definition of an inline function) we
15358          used to generate a special (abbreviated) DW_TAG_structure_type,
15359          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15360          should be actually referencing those DIEs, as variable DIEs with that
15361          type would be emitted already in the abstract origin, so it was always
15362          removed during unused type prunning.  Don't add anything in this
15363          case.  */
15364       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15365         break;
15366
15367       if (is_redundant_typedef (decl))
15368         gen_type_die (TREE_TYPE (decl), context_die);
15369       else
15370         /* Output a DIE to represent the typedef itself.  */
15371         gen_typedef_die (decl, context_die);
15372       break;
15373
15374     case LABEL_DECL:
15375       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15376         gen_label_die (decl, context_die);
15377       break;
15378
15379     case VAR_DECL:
15380     case RESULT_DECL:
15381       /* If we are in terse mode, don't generate any DIEs to represent any
15382          variable declarations or definitions.  */
15383       if (debug_info_level <= DINFO_LEVEL_TERSE)
15384         break;
15385
15386       /* Output any DIEs that are needed to specify the type of this data
15387          object.  */
15388       if (TREE_CODE (decl_or_origin) == RESULT_DECL
15389           && DECL_BY_REFERENCE (decl_or_origin))
15390         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15391       else
15392         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15393
15394       /* And its containing type.  */
15395       class_origin = decl_class_context (decl_or_origin);
15396       if (class_origin != NULL_TREE)
15397         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15398
15399       /* And its containing namespace.  */
15400       context_die = declare_in_namespace (decl_or_origin, context_die);
15401
15402       /* Now output the DIE to represent the data object itself.  This gets
15403          complicated because of the possibility that the VAR_DECL really
15404          represents an inlined instance of a formal parameter for an inline
15405          function.  */
15406       if (!origin)
15407         origin = decl_ultimate_origin (decl);
15408       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15409         gen_formal_parameter_die (decl, origin, context_die);
15410       else
15411         gen_variable_die (decl, origin, context_die);
15412       break;
15413
15414     case FIELD_DECL:
15415       /* Ignore the nameless fields that are used to skip bits but handle C++
15416          anonymous unions and structs.  */
15417       if (DECL_NAME (decl) != NULL_TREE
15418           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15419           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15420         {
15421           gen_type_die (member_declared_type (decl), context_die);
15422           gen_field_die (decl, context_die);
15423         }
15424       break;
15425
15426     case PARM_DECL:
15427       if (DECL_BY_REFERENCE (decl_or_origin))
15428         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15429       else
15430         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15431       gen_formal_parameter_die (decl, origin, context_die);
15432       break;
15433
15434     case NAMESPACE_DECL:
15435     case IMPORTED_DECL:
15436       gen_namespace_die (decl, context_die);
15437       break;
15438
15439     default:
15440       /* Probably some frontend-internal decl.  Assume we don't care.  */
15441       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15442       break;
15443     }
15444 }
15445 \f
15446 /* Output debug information for global decl DECL.  Called from toplev.c after
15447    compilation proper has finished.  */
15448
15449 static void
15450 dwarf2out_global_decl (tree decl)
15451 {
15452   /* Output DWARF2 information for file-scope tentative data object
15453      declarations, file-scope (extern) function declarations (which
15454      had no corresponding body) and file-scope tagged type declarations
15455      and definitions which have not yet been forced out.  */
15456   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15457     dwarf2out_decl (decl);
15458 }
15459
15460 /* Output debug information for type decl DECL.  Called from toplev.c
15461    and from language front ends (to record built-in types).  */
15462 static void
15463 dwarf2out_type_decl (tree decl, int local)
15464 {
15465   if (!local)
15466     dwarf2out_decl (decl);
15467 }
15468
15469 /* Output debug information for imported module or decl DECL.
15470    NAME is non-NULL name in the lexical block if the decl has been renamed.
15471    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15472    that DECL belongs to.
15473    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15474 static void
15475 dwarf2out_imported_module_or_decl_1 (tree decl,
15476                                      tree name,
15477                                      tree lexical_block,
15478                                      dw_die_ref lexical_block_die)
15479 {
15480   expanded_location xloc;
15481   dw_die_ref imported_die = NULL;
15482   dw_die_ref at_import_die;
15483
15484   if (TREE_CODE (decl) == IMPORTED_DECL)
15485     {
15486       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15487       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15488       gcc_assert (decl);
15489     }
15490   else
15491     xloc = expand_location (input_location);
15492
15493   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15494     {
15495       if (is_base_type (TREE_TYPE (decl)))
15496         at_import_die = base_type_die (TREE_TYPE (decl));
15497       else
15498         at_import_die = force_type_die (TREE_TYPE (decl));
15499       /* For namespace N { typedef void T; } using N::T; base_type_die
15500          returns NULL, but DW_TAG_imported_declaration requires
15501          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15502       if (!at_import_die)
15503         {
15504           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15505           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15506           at_import_die = lookup_type_die (TREE_TYPE (decl));
15507           gcc_assert (at_import_die);
15508         }
15509     }
15510   else
15511     {
15512       at_import_die = lookup_decl_die (decl);
15513       if (!at_import_die)
15514         {
15515           /* If we're trying to avoid duplicate debug info, we may not have
15516              emitted the member decl for this field.  Emit it now.  */
15517           if (TREE_CODE (decl) == FIELD_DECL)
15518             {
15519               tree type = DECL_CONTEXT (decl);
15520
15521               if (TYPE_CONTEXT (type)
15522                   && TYPE_P (TYPE_CONTEXT (type))
15523                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15524                                                 DINFO_USAGE_DIR_USE))
15525                 return;
15526               gen_type_die_for_member (type, decl,
15527                                        get_context_die (TYPE_CONTEXT (type)));
15528             }
15529           at_import_die = force_decl_die (decl);
15530         }
15531     }
15532
15533   if (TREE_CODE (decl) == NAMESPACE_DECL)
15534     imported_die = new_die (DW_TAG_imported_module,
15535                             lexical_block_die,
15536                             lexical_block);
15537   else
15538     imported_die = new_die (DW_TAG_imported_declaration,
15539                             lexical_block_die,
15540                             lexical_block);
15541
15542   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15543   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15544   if (name)
15545     add_AT_string (imported_die, DW_AT_name,
15546                    IDENTIFIER_POINTER (name));
15547   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15548 }
15549
15550 /* Output debug information for imported module or decl DECL.
15551    NAME is non-NULL name in context if the decl has been renamed.
15552    CHILD is true if decl is one of the renamed decls as part of
15553    importing whole module.  */
15554
15555 static void
15556 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15557                                    bool child)
15558 {
15559   /* dw_die_ref at_import_die;  */
15560   dw_die_ref scope_die;
15561
15562   if (debug_info_level <= DINFO_LEVEL_TERSE)
15563     return;
15564
15565   gcc_assert (decl);
15566
15567   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15568      We need decl DIE for reference and scope die. First, get DIE for the decl
15569      itself.  */
15570
15571   /* Get the scope die for decl context. Use comp_unit_die for global module
15572      or decl. If die is not found for non globals, force new die.  */
15573   if (context
15574       && TYPE_P (context)
15575       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15576     return;
15577   scope_die = get_context_die (context);
15578
15579   if (child)
15580     {
15581       gcc_assert (scope_die->die_child);
15582       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15583       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15584       scope_die = scope_die->die_child;
15585     }
15586
15587   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15588   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15589
15590 }
15591
15592 /* Write the debugging output for DECL.  */
15593
15594 void
15595 dwarf2out_decl (tree decl)
15596 {
15597   dw_die_ref context_die = comp_unit_die;
15598
15599   switch (TREE_CODE (decl))
15600     {
15601     case ERROR_MARK:
15602       return;
15603
15604     case FUNCTION_DECL:
15605       /* What we would really like to do here is to filter out all mere
15606          file-scope declarations of file-scope functions which are never
15607          referenced later within this translation unit (and keep all of ones
15608          that *are* referenced later on) but we aren't clairvoyant, so we have
15609          no idea which functions will be referenced in the future (i.e. later
15610          on within the current translation unit). So here we just ignore all
15611          file-scope function declarations which are not also definitions.  If
15612          and when the debugger needs to know something about these functions,
15613          it will have to hunt around and find the DWARF information associated
15614          with the definition of the function.
15615
15616          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15617          nodes represent definitions and which ones represent mere
15618          declarations.  We have to check DECL_INITIAL instead. That's because
15619          the C front-end supports some weird semantics for "extern inline"
15620          function definitions.  These can get inlined within the current
15621          translation unit (and thus, we need to generate Dwarf info for their
15622          abstract instances so that the Dwarf info for the concrete inlined
15623          instances can have something to refer to) but the compiler never
15624          generates any out-of-lines instances of such things (despite the fact
15625          that they *are* definitions).
15626
15627          The important point is that the C front-end marks these "extern
15628          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15629          them anyway. Note that the C++ front-end also plays some similar games
15630          for inline function definitions appearing within include files which
15631          also contain `#pragma interface' pragmas.  */
15632       if (DECL_INITIAL (decl) == NULL_TREE)
15633         return;
15634
15635       /* If we're a nested function, initially use a parent of NULL; if we're
15636          a plain function, this will be fixed up in decls_for_scope.  If
15637          we're a method, it will be ignored, since we already have a DIE.  */
15638       if (decl_function_context (decl)
15639           /* But if we're in terse mode, we don't care about scope.  */
15640           && debug_info_level > DINFO_LEVEL_TERSE)
15641         context_die = NULL;
15642       break;
15643
15644     case VAR_DECL:
15645       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15646          declaration and if the declaration was never even referenced from
15647          within this entire compilation unit.  We suppress these DIEs in
15648          order to save space in the .debug section (by eliminating entries
15649          which are probably useless).  Note that we must not suppress
15650          block-local extern declarations (whether used or not) because that
15651          would screw-up the debugger's name lookup mechanism and cause it to
15652          miss things which really ought to be in scope at a given point.  */
15653       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15654         return;
15655
15656       /* For local statics lookup proper context die.  */
15657       if (TREE_STATIC (decl) && decl_function_context (decl))
15658         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15659
15660       /* If we are in terse mode, don't generate any DIEs to represent any
15661          variable declarations or definitions.  */
15662       if (debug_info_level <= DINFO_LEVEL_TERSE)
15663         return;
15664       break;
15665
15666     case CONST_DECL:
15667       if (debug_info_level <= DINFO_LEVEL_TERSE)
15668         return;
15669       if (!is_fortran ())
15670         return;
15671       if (TREE_STATIC (decl) && decl_function_context (decl))
15672         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15673       break;
15674
15675     case NAMESPACE_DECL:
15676     case IMPORTED_DECL:
15677       if (debug_info_level <= DINFO_LEVEL_TERSE)
15678         return;
15679       if (lookup_decl_die (decl) != NULL)
15680         return;
15681       break;
15682
15683     case TYPE_DECL:
15684       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15685       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15686         return;
15687
15688       /* Don't bother trying to generate any DIEs to represent any of the
15689          normal built-in types for the language we are compiling.  */
15690       if (DECL_IS_BUILTIN (decl))
15691         {
15692           /* OK, we need to generate one for `bool' so GDB knows what type
15693              comparisons have.  */
15694           if (is_cxx ()
15695               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15696               && ! DECL_IGNORED_P (decl))
15697             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15698
15699           return;
15700         }
15701
15702       /* If we are in terse mode, don't generate any DIEs for types.  */
15703       if (debug_info_level <= DINFO_LEVEL_TERSE)
15704         return;
15705
15706       /* If we're a function-scope tag, initially use a parent of NULL;
15707          this will be fixed up in decls_for_scope.  */
15708       if (decl_function_context (decl))
15709         context_die = NULL;
15710
15711       break;
15712
15713     default:
15714       return;
15715     }
15716
15717   gen_decl_die (decl, NULL, context_die);
15718 }
15719
15720 /* Output a marker (i.e. a label) for the beginning of the generated code for
15721    a lexical block.  */
15722
15723 static void
15724 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15725                        unsigned int blocknum)
15726 {
15727   switch_to_section (current_function_section ());
15728   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15729 }
15730
15731 /* Output a marker (i.e. a label) for the end of the generated code for a
15732    lexical block.  */
15733
15734 static void
15735 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15736 {
15737   switch_to_section (current_function_section ());
15738   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15739 }
15740
15741 /* Returns nonzero if it is appropriate not to emit any debugging
15742    information for BLOCK, because it doesn't contain any instructions.
15743
15744    Don't allow this for blocks with nested functions or local classes
15745    as we would end up with orphans, and in the presence of scheduling
15746    we may end up calling them anyway.  */
15747
15748 static bool
15749 dwarf2out_ignore_block (const_tree block)
15750 {
15751   tree decl;
15752   unsigned int i;
15753
15754   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15755     if (TREE_CODE (decl) == FUNCTION_DECL
15756         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15757       return 0;
15758   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15759     {
15760       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15761       if (TREE_CODE (decl) == FUNCTION_DECL
15762           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15763       return 0;
15764     }
15765
15766   return 1;
15767 }
15768
15769 /* Hash table routines for file_hash.  */
15770
15771 static int
15772 file_table_eq (const void *p1_p, const void *p2_p)
15773 {
15774   const struct dwarf_file_data *const p1 =
15775     (const struct dwarf_file_data *) p1_p;
15776   const char *const p2 = (const char *) p2_p;
15777   return strcmp (p1->filename, p2) == 0;
15778 }
15779
15780 static hashval_t
15781 file_table_hash (const void *p_p)
15782 {
15783   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15784   return htab_hash_string (p->filename);
15785 }
15786
15787 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15788    dwarf2out.c) and return its "index".  The index of each (known) filename is
15789    just a unique number which is associated with only that one filename.  We
15790    need such numbers for the sake of generating labels (in the .debug_sfnames
15791    section) and references to those files numbers (in the .debug_srcinfo
15792    and.debug_macinfo sections).  If the filename given as an argument is not
15793    found in our current list, add it to the list and assign it the next
15794    available unique index number.  In order to speed up searches, we remember
15795    the index of the filename was looked up last.  This handles the majority of
15796    all searches.  */
15797
15798 static struct dwarf_file_data *
15799 lookup_filename (const char *file_name)
15800 {
15801   void ** slot;
15802   struct dwarf_file_data * created;
15803
15804   /* Check to see if the file name that was searched on the previous
15805      call matches this file name.  If so, return the index.  */
15806   if (file_table_last_lookup
15807       && (file_name == file_table_last_lookup->filename
15808           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15809     return file_table_last_lookup;
15810
15811   /* Didn't match the previous lookup, search the table.  */
15812   slot = htab_find_slot_with_hash (file_table, file_name,
15813                                    htab_hash_string (file_name), INSERT);
15814   if (*slot)
15815     return (struct dwarf_file_data *) *slot;
15816
15817   created = GGC_NEW (struct dwarf_file_data);
15818   created->filename = file_name;
15819   created->emitted_number = 0;
15820   *slot = created;
15821   return created;
15822 }
15823
15824 /* If the assembler will construct the file table, then translate the compiler
15825    internal file table number into the assembler file table number, and emit
15826    a .file directive if we haven't already emitted one yet.  The file table
15827    numbers are different because we prune debug info for unused variables and
15828    types, which may include filenames.  */
15829
15830 static int
15831 maybe_emit_file (struct dwarf_file_data * fd)
15832 {
15833   if (! fd->emitted_number)
15834     {
15835       if (last_emitted_file)
15836         fd->emitted_number = last_emitted_file->emitted_number + 1;
15837       else
15838         fd->emitted_number = 1;
15839       last_emitted_file = fd;
15840
15841       if (DWARF2_ASM_LINE_DEBUG_INFO)
15842         {
15843           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15844           output_quoted_string (asm_out_file,
15845                                 remap_debug_filename (fd->filename));
15846           fputc ('\n', asm_out_file);
15847         }
15848     }
15849
15850   return fd->emitted_number;
15851 }
15852
15853 /* Called by the final INSN scan whenever we see a var location.  We
15854    use it to drop labels in the right places, and throw the location in
15855    our lookup table.  */
15856
15857 static void
15858 dwarf2out_var_location (rtx loc_note)
15859 {
15860   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15861   struct var_loc_node *newloc;
15862   rtx prev_insn;
15863   static rtx last_insn;
15864   static const char *last_label;
15865   tree decl;
15866
15867   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15868     return;
15869   prev_insn = PREV_INSN (loc_note);
15870
15871   newloc = GGC_CNEW (struct var_loc_node);
15872   /* If the insn we processed last time is the previous insn
15873      and it is also a var location note, use the label we emitted
15874      last time.  */
15875   if (last_insn != NULL_RTX
15876       && last_insn == prev_insn
15877       && NOTE_P (prev_insn)
15878       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15879     {
15880       newloc->label = last_label;
15881     }
15882   else
15883     {
15884       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15885       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15886       loclabel_num++;
15887       newloc->label = ggc_strdup (loclabel);
15888     }
15889   newloc->var_loc_note = loc_note;
15890   newloc->next = NULL;
15891
15892   if (cfun && in_cold_section_p)
15893     newloc->section_label = crtl->subsections.cold_section_label;
15894   else
15895     newloc->section_label = text_section_label;
15896
15897   last_insn = loc_note;
15898   last_label = newloc->label;
15899   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15900   add_var_loc_to_decl (decl, newloc);
15901 }
15902
15903 /* We need to reset the locations at the beginning of each
15904    function. We can't do this in the end_function hook, because the
15905    declarations that use the locations won't have been output when
15906    that hook is called.  Also compute have_multiple_function_sections here.  */
15907
15908 static void
15909 dwarf2out_begin_function (tree fun)
15910 {
15911   htab_empty (decl_loc_table);
15912
15913   if (function_section (fun) != text_section)
15914     have_multiple_function_sections = true;
15915
15916   dwarf2out_note_section_used ();
15917 }
15918
15919 /* Output a label to mark the beginning of a source code line entry
15920    and record information relating to this source line, in
15921    'line_info_table' for later output of the .debug_line section.  */
15922
15923 static void
15924 dwarf2out_source_line (unsigned int line, const char *filename)
15925 {
15926   if (debug_info_level >= DINFO_LEVEL_NORMAL
15927       && line != 0)
15928     {
15929       int file_num = maybe_emit_file (lookup_filename (filename));
15930
15931       switch_to_section (current_function_section ());
15932
15933       /* If requested, emit something human-readable.  */
15934       if (flag_debug_asm)
15935         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15936                  filename, line);
15937
15938       if (DWARF2_ASM_LINE_DEBUG_INFO)
15939         {
15940           /* Emit the .loc directive understood by GNU as.  */
15941           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15942
15943           /* Indicate that line number info exists.  */
15944           line_info_table_in_use++;
15945         }
15946       else if (function_section (current_function_decl) != text_section)
15947         {
15948           dw_separate_line_info_ref line_info;
15949           targetm.asm_out.internal_label (asm_out_file,
15950                                           SEPARATE_LINE_CODE_LABEL,
15951                                           separate_line_info_table_in_use);
15952
15953           /* Expand the line info table if necessary.  */
15954           if (separate_line_info_table_in_use
15955               == separate_line_info_table_allocated)
15956             {
15957               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15958               separate_line_info_table
15959                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15960                                  separate_line_info_table,
15961                                  separate_line_info_table_allocated);
15962               memset (separate_line_info_table
15963                        + separate_line_info_table_in_use,
15964                       0,
15965                       (LINE_INFO_TABLE_INCREMENT
15966                        * sizeof (dw_separate_line_info_entry)));
15967             }
15968
15969           /* Add the new entry at the end of the line_info_table.  */
15970           line_info
15971             = &separate_line_info_table[separate_line_info_table_in_use++];
15972           line_info->dw_file_num = file_num;
15973           line_info->dw_line_num = line;
15974           line_info->function = current_function_funcdef_no;
15975         }
15976       else
15977         {
15978           dw_line_info_ref line_info;
15979
15980           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15981                                      line_info_table_in_use);
15982
15983           /* Expand the line info table if necessary.  */
15984           if (line_info_table_in_use == line_info_table_allocated)
15985             {
15986               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15987               line_info_table
15988                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15989                                  line_info_table_allocated);
15990               memset (line_info_table + line_info_table_in_use, 0,
15991                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15992             }
15993
15994           /* Add the new entry at the end of the line_info_table.  */
15995           line_info = &line_info_table[line_info_table_in_use++];
15996           line_info->dw_file_num = file_num;
15997           line_info->dw_line_num = line;
15998         }
15999     }
16000 }
16001
16002 /* Record the beginning of a new source file.  */
16003
16004 static void
16005 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16006 {
16007   if (flag_eliminate_dwarf2_dups)
16008     {
16009       /* Record the beginning of the file for break_out_includes.  */
16010       dw_die_ref bincl_die;
16011
16012       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16013       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16014     }
16015
16016   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16017     {
16018       int file_num = maybe_emit_file (lookup_filename (filename));
16019
16020       switch_to_section (debug_macinfo_section);
16021       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16022       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16023                                    lineno);
16024
16025       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16026     }
16027 }
16028
16029 /* Record the end of a source file.  */
16030
16031 static void
16032 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16033 {
16034   if (flag_eliminate_dwarf2_dups)
16035     /* Record the end of the file for break_out_includes.  */
16036     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16037
16038   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16039     {
16040       switch_to_section (debug_macinfo_section);
16041       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16042     }
16043 }
16044
16045 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16046    the tail part of the directive line, i.e. the part which is past the
16047    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16048
16049 static void
16050 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16051                   const char *buffer ATTRIBUTE_UNUSED)
16052 {
16053   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16054     {
16055       switch_to_section (debug_macinfo_section);
16056       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16057       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16058       dw2_asm_output_nstring (buffer, -1, "The macro");
16059     }
16060 }
16061
16062 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16063    the tail part of the directive line, i.e. the part which is past the
16064    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16065
16066 static void
16067 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16068                  const char *buffer ATTRIBUTE_UNUSED)
16069 {
16070   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16071     {
16072       switch_to_section (debug_macinfo_section);
16073       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16074       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16075       dw2_asm_output_nstring (buffer, -1, "The macro");
16076     }
16077 }
16078
16079 /* Set up for Dwarf output at the start of compilation.  */
16080
16081 static void
16082 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16083 {
16084   /* Allocate the file_table.  */
16085   file_table = htab_create_ggc (50, file_table_hash,
16086                                 file_table_eq, NULL);
16087
16088   /* Allocate the decl_die_table.  */
16089   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16090                                     decl_die_table_eq, NULL);
16091
16092   /* Allocate the decl_loc_table.  */
16093   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16094                                     decl_loc_table_eq, NULL);
16095
16096   /* Allocate the initial hunk of the decl_scope_table.  */
16097   decl_scope_table = VEC_alloc (tree, gc, 256);
16098
16099   /* Allocate the initial hunk of the abbrev_die_table.  */
16100   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16101   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16102   /* Zero-th entry is allocated, but unused.  */
16103   abbrev_die_table_in_use = 1;
16104
16105   /* Allocate the initial hunk of the line_info_table.  */
16106   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16107   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16108
16109   /* Zero-th entry is allocated, but unused.  */
16110   line_info_table_in_use = 1;
16111
16112   /* Allocate the pubtypes and pubnames vectors.  */
16113   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16114   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16115
16116   /* Generate the initial DIE for the .debug section.  Note that the (string)
16117      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16118      will (typically) be a relative pathname and that this pathname should be
16119      taken as being relative to the directory from which the compiler was
16120      invoked when the given (base) source file was compiled.  We will fill
16121      in this value in dwarf2out_finish.  */
16122   comp_unit_die = gen_compile_unit_die (NULL);
16123
16124   incomplete_types = VEC_alloc (tree, gc, 64);
16125
16126   used_rtx_array = VEC_alloc (rtx, gc, 32);
16127
16128   debug_info_section = get_section (DEBUG_INFO_SECTION,
16129                                     SECTION_DEBUG, NULL);
16130   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16131                                       SECTION_DEBUG, NULL);
16132   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16133                                        SECTION_DEBUG, NULL);
16134   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16135                                        SECTION_DEBUG, NULL);
16136   debug_line_section = get_section (DEBUG_LINE_SECTION,
16137                                     SECTION_DEBUG, NULL);
16138   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16139                                    SECTION_DEBUG, NULL);
16140   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16141                                         SECTION_DEBUG, NULL);
16142 #ifdef DEBUG_PUBTYPES_SECTION
16143   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16144                                         SECTION_DEBUG, NULL);
16145 #endif
16146   debug_str_section = get_section (DEBUG_STR_SECTION,
16147                                    DEBUG_STR_SECTION_FLAGS, NULL);
16148   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16149                                       SECTION_DEBUG, NULL);
16150   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16151                                      SECTION_DEBUG, NULL);
16152
16153   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16154   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16155                                DEBUG_ABBREV_SECTION_LABEL, 0);
16156   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16157   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16158                                COLD_TEXT_SECTION_LABEL, 0);
16159   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16160
16161   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16162                                DEBUG_INFO_SECTION_LABEL, 0);
16163   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16164                                DEBUG_LINE_SECTION_LABEL, 0);
16165   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16166                                DEBUG_RANGES_SECTION_LABEL, 0);
16167   switch_to_section (debug_abbrev_section);
16168   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16169   switch_to_section (debug_info_section);
16170   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16171   switch_to_section (debug_line_section);
16172   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16173
16174   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16175     {
16176       switch_to_section (debug_macinfo_section);
16177       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16178                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16179       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16180     }
16181
16182   switch_to_section (text_section);
16183   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16184   if (flag_reorder_blocks_and_partition)
16185     {
16186       cold_text_section = unlikely_text_section ();
16187       switch_to_section (cold_text_section);
16188       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16189     }
16190 }
16191
16192 /* A helper function for dwarf2out_finish called through
16193    ht_forall.  Emit one queued .debug_str string.  */
16194
16195 static int
16196 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16197 {
16198   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16199
16200   if (node->form == DW_FORM_strp)
16201     {
16202       switch_to_section (debug_str_section);
16203       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16204       assemble_string (node->str, strlen (node->str) + 1);
16205     }
16206
16207   return 1;
16208 }
16209
16210 #if ENABLE_ASSERT_CHECKING
16211 /* Verify that all marks are clear.  */
16212
16213 static void
16214 verify_marks_clear (dw_die_ref die)
16215 {
16216   dw_die_ref c;
16217
16218   gcc_assert (! die->die_mark);
16219   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16220 }
16221 #endif /* ENABLE_ASSERT_CHECKING */
16222
16223 /* Clear the marks for a die and its children.
16224    Be cool if the mark isn't set.  */
16225
16226 static void
16227 prune_unmark_dies (dw_die_ref die)
16228 {
16229   dw_die_ref c;
16230
16231   if (die->die_mark)
16232     die->die_mark = 0;
16233   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16234 }
16235
16236 /* Given DIE that we're marking as used, find any other dies
16237    it references as attributes and mark them as used.  */
16238
16239 static void
16240 prune_unused_types_walk_attribs (dw_die_ref die)
16241 {
16242   dw_attr_ref a;
16243   unsigned ix;
16244
16245   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16246     {
16247       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16248         {
16249           /* A reference to another DIE.
16250              Make sure that it will get emitted.  */
16251           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16252         }
16253       /* Set the string's refcount to 0 so that prune_unused_types_mark
16254          accounts properly for it.  */
16255       if (AT_class (a) == dw_val_class_str)
16256         a->dw_attr_val.v.val_str->refcount = 0;
16257     }
16258 }
16259
16260
16261 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16262    to DIE's children.  */
16263
16264 static void
16265 prune_unused_types_mark (dw_die_ref die, int dokids)
16266 {
16267   dw_die_ref c;
16268
16269   if (die->die_mark == 0)
16270     {
16271       /* We haven't done this node yet.  Mark it as used.  */
16272       die->die_mark = 1;
16273
16274       /* We also have to mark its parents as used.
16275          (But we don't want to mark our parents' kids due to this.)  */
16276       if (die->die_parent)
16277         prune_unused_types_mark (die->die_parent, 0);
16278
16279       /* Mark any referenced nodes.  */
16280       prune_unused_types_walk_attribs (die);
16281
16282       /* If this node is a specification,
16283          also mark the definition, if it exists.  */
16284       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16285         prune_unused_types_mark (die->die_definition, 1);
16286     }
16287
16288   if (dokids && die->die_mark != 2)
16289     {
16290       /* We need to walk the children, but haven't done so yet.
16291          Remember that we've walked the kids.  */
16292       die->die_mark = 2;
16293
16294       /* If this is an array type, we need to make sure our
16295          kids get marked, even if they're types.  */
16296       if (die->die_tag == DW_TAG_array_type)
16297         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16298       else
16299         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16300     }
16301 }
16302
16303 /* For local classes, look if any static member functions were emitted
16304    and if so, mark them.  */
16305
16306 static void
16307 prune_unused_types_walk_local_classes (dw_die_ref die)
16308 {
16309   dw_die_ref c;
16310
16311   if (die->die_mark == 2)
16312     return;
16313
16314   switch (die->die_tag)
16315     {
16316     case DW_TAG_structure_type:
16317     case DW_TAG_union_type:
16318     case DW_TAG_class_type:
16319       break;
16320
16321     case DW_TAG_subprogram:
16322       if (!get_AT_flag (die, DW_AT_declaration)
16323           || die->die_definition != NULL)
16324         prune_unused_types_mark (die, 1);
16325       return;
16326
16327     default:
16328       return;
16329     }
16330
16331   /* Mark children.  */
16332   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16333 }
16334
16335 /* Walk the tree DIE and mark types that we actually use.  */
16336
16337 static void
16338 prune_unused_types_walk (dw_die_ref die)
16339 {
16340   dw_die_ref c;
16341
16342   /* Don't do anything if this node is already marked and
16343      children have been marked as well.  */
16344   if (die->die_mark == 2)
16345     return;
16346
16347   switch (die->die_tag)
16348     {
16349     case DW_TAG_structure_type:
16350     case DW_TAG_union_type:
16351     case DW_TAG_class_type:
16352       if (die->die_perennial_p)
16353         break;
16354
16355       for (c = die->die_parent; c; c = c->die_parent)
16356         if (c->die_tag == DW_TAG_subprogram)
16357           break;
16358
16359       /* Finding used static member functions inside of classes
16360          is needed just for local classes, because for other classes
16361          static member function DIEs with DW_AT_specification
16362          are emitted outside of the DW_TAG_*_type.  If we ever change
16363          it, we'd need to call this even for non-local classes.  */
16364       if (c)
16365         prune_unused_types_walk_local_classes (die);
16366
16367       /* It's a type node --- don't mark it.  */
16368       return;
16369
16370     case DW_TAG_const_type:
16371     case DW_TAG_packed_type:
16372     case DW_TAG_pointer_type:
16373     case DW_TAG_reference_type:
16374     case DW_TAG_volatile_type:
16375     case DW_TAG_typedef:
16376     case DW_TAG_array_type:
16377     case DW_TAG_interface_type:
16378     case DW_TAG_friend:
16379     case DW_TAG_variant_part:
16380     case DW_TAG_enumeration_type:
16381     case DW_TAG_subroutine_type:
16382     case DW_TAG_string_type:
16383     case DW_TAG_set_type:
16384     case DW_TAG_subrange_type:
16385     case DW_TAG_ptr_to_member_type:
16386     case DW_TAG_file_type:
16387       if (die->die_perennial_p)
16388         break;
16389
16390       /* It's a type node --- don't mark it.  */
16391       return;
16392
16393     default:
16394       /* Mark everything else.  */
16395       break;
16396   }
16397
16398   if (die->die_mark == 0)
16399     {
16400       die->die_mark = 1;
16401
16402       /* Now, mark any dies referenced from here.  */
16403       prune_unused_types_walk_attribs (die);
16404     }
16405
16406   die->die_mark = 2;
16407
16408   /* Mark children.  */
16409   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16410 }
16411
16412 /* Increment the string counts on strings referred to from DIE's
16413    attributes.  */
16414
16415 static void
16416 prune_unused_types_update_strings (dw_die_ref die)
16417 {
16418   dw_attr_ref a;
16419   unsigned ix;
16420
16421   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16422     if (AT_class (a) == dw_val_class_str)
16423       {
16424         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16425         s->refcount++;
16426         /* Avoid unnecessarily putting strings that are used less than
16427            twice in the hash table.  */
16428         if (s->refcount
16429             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16430           {
16431             void ** slot;
16432             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16433                                              htab_hash_string (s->str),
16434                                              INSERT);
16435             gcc_assert (*slot == NULL);
16436             *slot = s;
16437           }
16438       }
16439 }
16440
16441 /* Remove from the tree DIE any dies that aren't marked.  */
16442
16443 static void
16444 prune_unused_types_prune (dw_die_ref die)
16445 {
16446   dw_die_ref c;
16447
16448   gcc_assert (die->die_mark);
16449   prune_unused_types_update_strings (die);
16450
16451   if (! die->die_child)
16452     return;
16453
16454   c = die->die_child;
16455   do {
16456     dw_die_ref prev = c;
16457     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16458       if (c == die->die_child)
16459         {
16460           /* No marked children between 'prev' and the end of the list.  */
16461           if (prev == c)
16462             /* No marked children at all.  */
16463             die->die_child = NULL;
16464           else
16465             {
16466               prev->die_sib = c->die_sib;
16467               die->die_child = prev;
16468             }
16469           return;
16470         }
16471
16472     if (c != prev->die_sib)
16473       prev->die_sib = c;
16474     prune_unused_types_prune (c);
16475   } while (c != die->die_child);
16476 }
16477
16478
16479 /* Remove dies representing declarations that we never use.  */
16480
16481 static void
16482 prune_unused_types (void)
16483 {
16484   unsigned int i;
16485   limbo_die_node *node;
16486   pubname_ref pub;
16487
16488 #if ENABLE_ASSERT_CHECKING
16489   /* All the marks should already be clear.  */
16490   verify_marks_clear (comp_unit_die);
16491   for (node = limbo_die_list; node; node = node->next)
16492     verify_marks_clear (node->die);
16493 #endif /* ENABLE_ASSERT_CHECKING */
16494
16495   /* Mark types that are used in global variables.  */
16496   premark_types_used_by_global_vars ();
16497
16498   /* Set the mark on nodes that are actually used.  */
16499   prune_unused_types_walk (comp_unit_die);
16500   for (node = limbo_die_list; node; node = node->next)
16501     prune_unused_types_walk (node->die);
16502
16503   /* Also set the mark on nodes referenced from the
16504      pubname_table or arange_table.  */
16505   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16506     prune_unused_types_mark (pub->die, 1);
16507   for (i = 0; i < arange_table_in_use; i++)
16508     prune_unused_types_mark (arange_table[i], 1);
16509
16510   /* Get rid of nodes that aren't marked; and update the string counts.  */
16511   if (debug_str_hash)
16512     htab_empty (debug_str_hash);
16513   prune_unused_types_prune (comp_unit_die);
16514   for (node = limbo_die_list; node; node = node->next)
16515     prune_unused_types_prune (node->die);
16516
16517   /* Leave the marks clear.  */
16518   prune_unmark_dies (comp_unit_die);
16519   for (node = limbo_die_list; node; node = node->next)
16520     prune_unmark_dies (node->die);
16521 }
16522
16523 /* Set the parameter to true if there are any relative pathnames in
16524    the file table.  */
16525 static int
16526 file_table_relative_p (void ** slot, void *param)
16527 {
16528   bool *p = (bool *) param;
16529   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16530   if (!IS_ABSOLUTE_PATH (d->filename))
16531     {
16532       *p = true;
16533       return 0;
16534     }
16535   return 1;
16536 }
16537
16538 /* Output stuff that dwarf requires at the end of every file,
16539    and generate the DWARF-2 debugging info.  */
16540
16541 static void
16542 dwarf2out_finish (const char *filename)
16543 {
16544   limbo_die_node *node, *next_node;
16545   dw_die_ref die = 0;
16546   unsigned int i;
16547
16548   /* Add the name for the main input file now.  We delayed this from
16549      dwarf2out_init to avoid complications with PCH.  */
16550   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16551   if (!IS_ABSOLUTE_PATH (filename))
16552     add_comp_dir_attribute (comp_unit_die);
16553   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16554     {
16555       bool p = false;
16556       htab_traverse (file_table, file_table_relative_p, &p);
16557       if (p)
16558         add_comp_dir_attribute (comp_unit_die);
16559     }
16560
16561   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16562     {
16563       add_location_or_const_value_attribute (
16564         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16565         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16566         DW_AT_location);
16567     }
16568
16569   /* Traverse the limbo die list, and add parent/child links.  The only
16570      dies without parents that should be here are concrete instances of
16571      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16572      For concrete instances, we can get the parent die from the abstract
16573      instance.  */
16574   for (node = limbo_die_list; node; node = next_node)
16575     {
16576       next_node = node->next;
16577       die = node->die;
16578
16579       if (die->die_parent == NULL)
16580         {
16581           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16582
16583           if (origin)
16584             add_child_die (origin->die_parent, die);
16585           else if (die == comp_unit_die)
16586             ;
16587           else if (errorcount > 0 || sorrycount > 0)
16588             /* It's OK to be confused by errors in the input.  */
16589             add_child_die (comp_unit_die, die);
16590           else
16591             {
16592               /* In certain situations, the lexical block containing a
16593                  nested function can be optimized away, which results
16594                  in the nested function die being orphaned.  Likewise
16595                  with the return type of that nested function.  Force
16596                  this to be a child of the containing function.
16597
16598                  It may happen that even the containing function got fully
16599                  inlined and optimized out.  In that case we are lost and
16600                  assign the empty child.  This should not be big issue as
16601                  the function is likely unreachable too.  */
16602               tree context = NULL_TREE;
16603
16604               gcc_assert (node->created_for);
16605
16606               if (DECL_P (node->created_for))
16607                 context = DECL_CONTEXT (node->created_for);
16608               else if (TYPE_P (node->created_for))
16609                 context = TYPE_CONTEXT (node->created_for);
16610
16611               gcc_assert (context
16612                           && (TREE_CODE (context) == FUNCTION_DECL
16613                               || TREE_CODE (context) == NAMESPACE_DECL));
16614
16615               origin = lookup_decl_die (context);
16616               if (origin)
16617                 add_child_die (origin, die);
16618               else
16619                 add_child_die (comp_unit_die, die);
16620             }
16621         }
16622     }
16623
16624   limbo_die_list = NULL;
16625
16626   /* Walk through the list of incomplete types again, trying once more to
16627      emit full debugging info for them.  */
16628   retry_incomplete_types ();
16629
16630   if (flag_eliminate_unused_debug_types)
16631     prune_unused_types ();
16632
16633   /* Generate separate CUs for each of the include files we've seen.
16634      They will go into limbo_die_list.  */
16635   if (flag_eliminate_dwarf2_dups)
16636     break_out_includes (comp_unit_die);
16637
16638   /* Traverse the DIE's and add add sibling attributes to those DIE's
16639      that have children.  */
16640   add_sibling_attributes (comp_unit_die);
16641   for (node = limbo_die_list; node; node = node->next)
16642     add_sibling_attributes (node->die);
16643
16644   /* Output a terminator label for the .text section.  */
16645   switch_to_section (text_section);
16646   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16647   if (flag_reorder_blocks_and_partition)
16648     {
16649       switch_to_section (unlikely_text_section ());
16650       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16651     }
16652
16653   /* We can only use the low/high_pc attributes if all of the code was
16654      in .text.  */
16655   if (!have_multiple_function_sections)
16656     {
16657       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16658       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16659     }
16660
16661   else
16662     {
16663       unsigned fde_idx = 0;
16664
16665       /* We need to give .debug_loc and .debug_ranges an appropriate
16666          "base address".  Use zero so that these addresses become
16667          absolute.  Historically, we've emitted the unexpected
16668          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16669          Emit both to give time for other tools to adapt.  */
16670       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16671       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16672
16673       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16674                          add_ranges_by_labels (text_section_label,
16675                                                text_end_label));
16676       if (flag_reorder_blocks_and_partition)
16677         add_ranges_by_labels (cold_text_section_label,
16678                               cold_end_label);
16679
16680       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16681         {
16682           dw_fde_ref fde = &fde_table[fde_idx];
16683
16684           if (fde->dw_fde_switched_sections)
16685             {
16686               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16687                                     fde->dw_fde_hot_section_end_label);
16688               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16689                                     fde->dw_fde_unlikely_section_end_label);
16690             }
16691           else
16692             add_ranges_by_labels (fde->dw_fde_begin,
16693                                   fde->dw_fde_end);
16694         }
16695
16696       add_ranges (NULL);
16697     }
16698
16699   /* Output location list section if necessary.  */
16700   if (have_location_lists)
16701     {
16702       /* Output the location lists info.  */
16703       switch_to_section (debug_loc_section);
16704       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16705                                    DEBUG_LOC_SECTION_LABEL, 0);
16706       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16707       output_location_lists (die);
16708     }
16709
16710   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16711     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16712                     debug_line_section_label);
16713
16714   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16715     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16716
16717   /* Output all of the compilation units.  We put the main one last so that
16718      the offsets are available to output_pubnames.  */
16719   for (node = limbo_die_list; node; node = node->next)
16720     output_comp_unit (node->die, 0);
16721
16722   /* Output the main compilation unit if non-empty or if .debug_macinfo
16723      has been emitted.  */
16724   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16725
16726   /* Output the abbreviation table.  */
16727   switch_to_section (debug_abbrev_section);
16728   output_abbrev_section ();
16729
16730   /* Output public names table if necessary.  */
16731   if (!VEC_empty (pubname_entry, pubname_table))
16732     {
16733       switch_to_section (debug_pubnames_section);
16734       output_pubnames (pubname_table);
16735     }
16736
16737 #ifdef DEBUG_PUBTYPES_SECTION
16738   /* Output public types table if necessary.  */
16739   if (!VEC_empty (pubname_entry, pubtype_table))
16740     {
16741       switch_to_section (debug_pubtypes_section);
16742       output_pubnames (pubtype_table);
16743     }
16744 #endif
16745
16746   /* Output the address range information.  We only put functions in the arange
16747      table, so don't write it out if we don't have any.  */
16748   if (fde_table_in_use)
16749     {
16750       switch_to_section (debug_aranges_section);
16751       output_aranges ();
16752     }
16753
16754   /* Output ranges section if necessary.  */
16755   if (ranges_table_in_use)
16756     {
16757       switch_to_section (debug_ranges_section);
16758       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16759       output_ranges ();
16760     }
16761
16762   /* Output the source line correspondence table.  We must do this
16763      even if there is no line information.  Otherwise, on an empty
16764      translation unit, we will generate a present, but empty,
16765      .debug_info section.  IRIX 6.5 `nm' will then complain when
16766      examining the file.  This is done late so that any filenames
16767      used by the debug_info section are marked as 'used'.  */
16768   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16769     {
16770       switch_to_section (debug_line_section);
16771       output_line_info ();
16772     }
16773
16774   /* Have to end the macro section.  */
16775   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16776     {
16777       switch_to_section (debug_macinfo_section);
16778       dw2_asm_output_data (1, 0, "End compilation unit");
16779     }
16780
16781   /* If we emitted any DW_FORM_strp form attribute, output the string
16782      table too.  */
16783   if (debug_str_hash)
16784     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16785 }
16786 #else
16787
16788 /* This should never be used, but its address is needed for comparisons.  */
16789 const struct gcc_debug_hooks dwarf2_debug_hooks;
16790
16791 #endif /* DWARF2_DEBUGGING_INFO */
16792
16793 #include "gt-dwarf2out.h"