Merge branch 'vendor/GCC44'
[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, 2010
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "real.h"
68 #include "rtl.h"
69 #include "hard-reg-set.h"
70 #include "regs.h"
71 #include "insn-config.h"
72 #include "reload.h"
73 #include "function.h"
74 #include "output.h"
75 #include "expr.h"
76 #include "libfuncs.h"
77 #include "except.h"
78 #include "dwarf2.h"
79 #include "dwarf2out.h"
80 #include "dwarf2asm.h"
81 #include "toplev.h"
82 #include "varray.h"
83 #include "ggc.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "debug.h"
88 #include "target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93
94 #ifdef DWARF2_DEBUGGING_INFO
95 static void dwarf2out_source_line (unsigned int, const char *);
96 #endif
97
98 #ifndef DWARF2_FRAME_INFO
99 # ifdef DWARF2_DEBUGGING_INFO
100 #  define DWARF2_FRAME_INFO \
101   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
102 # else
103 #  define DWARF2_FRAME_INFO 0
104 # endif
105 #endif
106
107 /* Map register numbers held in the call frame info that gcc has
108    collected using DWARF_FRAME_REGNUM to those that should be output in
109    .debug_frame and .eh_frame.  */
110 #ifndef DWARF2_FRAME_REG_OUT
111 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
112 #endif
113
114 /* Save the result of dwarf2out_do_frame across PCH.  */
115 static GTY(()) bool saved_do_cfi_asm = 0;
116
117 /* Decide whether we want to emit frame unwind information for the current
118    translation unit.  */
119
120 int
121 dwarf2out_do_frame (void)
122 {
123   /* We want to emit correct CFA location expressions or lists, so we
124      have to return true if we're going to output debug info, even if
125      we're not going to output frame or unwind info.  */
126   return (write_symbols == DWARF2_DEBUG
127           || write_symbols == VMS_AND_DWARF2_DEBUG
128           || DWARF2_FRAME_INFO || saved_do_cfi_asm
129 #ifdef DWARF2_UNWIND_INFO
130           || (DWARF2_UNWIND_INFO
131               && (flag_unwind_tables
132                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
133 #endif
134           );
135 }
136
137 /* Decide whether to emit frame unwind via assembler directives.  */
138
139 int
140 dwarf2out_do_cfi_asm (void)
141 {
142   int enc;
143
144 #ifdef MIPS_DEBUGGING_INFO
145   return false;
146 #endif
147   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
148     return false;
149   if (saved_do_cfi_asm)
150     return true;
151   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
152     {
153 #ifdef TARGET_UNWIND_INFO
154       return false;
155 #else
156       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
157         return false;
158 #endif
159     }
160   if (!eh_personality_libfunc)
161     return true;
162   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
163     return false;
164
165   /* Make sure the personality encoding is one the assembler can support.
166      In particular, aligned addresses can't be handled.  */
167   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
168   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
169     return false;
170   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
171   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
172     return false;
173
174   saved_do_cfi_asm = true;
175   return true;
176 }
177
178 /* The size of the target's pointer type.  */
179 #ifndef PTR_SIZE
180 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
181 #endif
182
183 /* Array of RTXes referenced by the debugging information, which therefore
184    must be kept around forever.  */
185 static GTY(()) VEC(rtx,gc) *used_rtx_array;
186
187 /* A pointer to the base of a list of incomplete types which might be
188    completed at some later time.  incomplete_types_list needs to be a
189    VEC(tree,gc) because we want to tell the garbage collector about
190    it.  */
191 static GTY(()) VEC(tree,gc) *incomplete_types;
192
193 /* A pointer to the base of a table of references to declaration
194    scopes.  This table is a display which tracks the nesting
195    of declaration scopes at the current scope and containing
196    scopes.  This table is used to find the proper place to
197    define type declaration DIE's.  */
198 static GTY(()) VEC(tree,gc) *decl_scope_table;
199
200 /* Pointers to various DWARF2 sections.  */
201 static GTY(()) section *debug_info_section;
202 static GTY(()) section *debug_abbrev_section;
203 static GTY(()) section *debug_aranges_section;
204 static GTY(()) section *debug_macinfo_section;
205 static GTY(()) section *debug_line_section;
206 static GTY(()) section *debug_loc_section;
207 static GTY(()) section *debug_pubnames_section;
208 static GTY(()) section *debug_pubtypes_section;
209 static GTY(()) section *debug_str_section;
210 static GTY(()) section *debug_ranges_section;
211 static GTY(()) section *debug_frame_section;
212
213 /* How to start an assembler comment.  */
214 #ifndef ASM_COMMENT_START
215 #define ASM_COMMENT_START ";#"
216 #endif
217
218 typedef struct dw_cfi_struct *dw_cfi_ref;
219 typedef struct dw_fde_struct *dw_fde_ref;
220 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
221
222 /* Call frames are described using a sequence of Call Frame
223    Information instructions.  The register number, offset
224    and address fields are provided as possible operands;
225    their use is selected by the opcode field.  */
226
227 enum dw_cfi_oprnd_type {
228   dw_cfi_oprnd_unused,
229   dw_cfi_oprnd_reg_num,
230   dw_cfi_oprnd_offset,
231   dw_cfi_oprnd_addr,
232   dw_cfi_oprnd_loc
233 };
234
235 typedef union dw_cfi_oprnd_struct GTY(())
236 {
237   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
238   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
239   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
240   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
241 }
242 dw_cfi_oprnd;
243
244 typedef struct dw_cfi_struct GTY(())
245 {
246   dw_cfi_ref dw_cfi_next;
247   enum dwarf_call_frame_info dw_cfi_opc;
248   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
249     dw_cfi_oprnd1;
250   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
251     dw_cfi_oprnd2;
252 }
253 dw_cfi_node;
254
255 /* This is how we define the location of the CFA. We use to handle it
256    as REG + OFFSET all the time,  but now it can be more complex.
257    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
258    Instead of passing around REG and OFFSET, we pass a copy
259    of this structure.  */
260 typedef struct cfa_loc GTY(())
261 {
262   HOST_WIDE_INT offset;
263   HOST_WIDE_INT base_offset;
264   unsigned int reg;
265   int indirect;            /* 1 if CFA is accessed via a dereference.  */
266 } dw_cfa_location;
267
268 /* All call frame descriptions (FDE's) in the GCC generated DWARF
269    refer to a single Common Information Entry (CIE), defined at
270    the beginning of the .debug_frame section.  This use of a single
271    CIE obviates the need to keep track of multiple CIE's
272    in the DWARF generation routines below.  */
273
274 typedef struct dw_fde_struct GTY(())
275 {
276   tree decl;
277   const char *dw_fde_begin;
278   const char *dw_fde_current_label;
279   const char *dw_fde_end;
280   const char *dw_fde_hot_section_label;
281   const char *dw_fde_hot_section_end_label;
282   const char *dw_fde_unlikely_section_label;
283   const char *dw_fde_unlikely_section_end_label;
284   bool dw_fde_switched_sections;
285   dw_cfi_ref dw_fde_cfi;
286   unsigned funcdef_number;
287   HOST_WIDE_INT stack_realignment;
288   /* Dynamic realign argument pointer register.  */
289   unsigned int drap_reg;
290   /* Virtual dynamic realign argument pointer register.  */
291   unsigned int vdrap_reg;
292   unsigned all_throwers_are_sibcalls : 1;
293   unsigned nothrow : 1;
294   unsigned uses_eh_lsda : 1;
295   /* Whether we did stack realign in this call frame.  */
296   unsigned stack_realign : 1;
297   /* Whether dynamic realign argument pointer register has been saved.  */
298   unsigned drap_reg_saved: 1;
299 }
300 dw_fde_node;
301
302 /* Maximum size (in bytes) of an artificially generated label.  */
303 #define MAX_ARTIFICIAL_LABEL_BYTES      30
304
305 /* The size of addresses as they appear in the Dwarf 2 data.
306    Some architectures use word addresses to refer to code locations,
307    but Dwarf 2 info always uses byte addresses.  On such machines,
308    Dwarf 2 addresses need to be larger than the architecture's
309    pointers.  */
310 #ifndef DWARF2_ADDR_SIZE
311 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
312 #endif
313
314 /* The size in bytes of a DWARF field indicating an offset or length
315    relative to a debug info section, specified to be 4 bytes in the
316    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
317    as PTR_SIZE.  */
318
319 #ifndef DWARF_OFFSET_SIZE
320 #define DWARF_OFFSET_SIZE 4
321 #endif
322
323 /* According to the (draft) DWARF 3 specification, the initial length
324    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
325    bytes are 0xffffffff, followed by the length stored in the next 8
326    bytes.
327
328    However, the SGI/MIPS ABI uses an initial length which is equal to
329    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
330
331 #ifndef DWARF_INITIAL_LENGTH_SIZE
332 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
333 #endif
334
335 #define DWARF_VERSION 2
336
337 /* Round SIZE up to the nearest BOUNDARY.  */
338 #define DWARF_ROUND(SIZE,BOUNDARY) \
339   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
340
341 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
342 #ifndef DWARF_CIE_DATA_ALIGNMENT
343 #ifdef STACK_GROWS_DOWNWARD
344 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
345 #else
346 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
347 #endif
348 #endif
349
350 /* CIE identifier.  */
351 #if HOST_BITS_PER_WIDE_INT >= 64
352 #define DWARF_CIE_ID \
353   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
354 #else
355 #define DWARF_CIE_ID DW_CIE_ID
356 #endif
357
358 /* A pointer to the base of a table that contains frame description
359    information for each routine.  */
360 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
361
362 /* Number of elements currently allocated for fde_table.  */
363 static GTY(()) unsigned fde_table_allocated;
364
365 /* Number of elements in fde_table currently in use.  */
366 static GTY(()) unsigned fde_table_in_use;
367
368 /* Size (in elements) of increments by which we may expand the
369    fde_table.  */
370 #define FDE_TABLE_INCREMENT 256
371
372 /* Get the current fde_table entry we should use.  */
373
374 static inline dw_fde_ref
375 current_fde (void)
376 {
377   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
378 }
379
380 /* A list of call frame insns for the CIE.  */
381 static GTY(()) dw_cfi_ref cie_cfi_head;
382
383 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
384 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
385    attribute that accelerates the lookup of the FDE associated
386    with the subprogram.  This variable holds the table index of the FDE
387    associated with the current function (body) definition.  */
388 static unsigned current_funcdef_fde;
389 #endif
390
391 struct indirect_string_node GTY(())
392 {
393   const char *str;
394   unsigned int refcount;
395   unsigned int form;
396   char *label;
397 };
398
399 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
400
401 static GTY(()) int dw2_string_counter;
402 static GTY(()) unsigned long dwarf2out_cfi_label_num;
403
404 /* True if the compilation unit places functions in more than one section.  */
405 static GTY(()) bool have_multiple_function_sections = false;
406
407 /* Whether the default text and cold text sections have been used at all.  */
408
409 static GTY(()) bool text_section_used = false;
410 static GTY(()) bool cold_text_section_used = false;
411
412 /* The default cold text section.  */
413 static GTY(()) section *cold_text_section;
414
415 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
416
417 /* Forward declarations for functions defined in this file.  */
418
419 static char *stripattributes (const char *);
420 static const char *dwarf_cfi_name (unsigned);
421 static dw_cfi_ref new_cfi (void);
422 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
423 static void add_fde_cfi (const char *, dw_cfi_ref);
424 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
425 static void lookup_cfa (dw_cfa_location *);
426 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
427 #ifdef DWARF2_UNWIND_INFO
428 static void initial_return_save (rtx);
429 #endif
430 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
431                                           HOST_WIDE_INT);
432 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
433 static void output_cfi_directive (dw_cfi_ref);
434 static void output_call_frame_info (int);
435 static void dwarf2out_note_section_used (void);
436 static void dwarf2out_stack_adjust (rtx, bool);
437 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
438 static void flush_queued_reg_saves (void);
439 static bool clobbers_queued_reg_save (const_rtx);
440 static void dwarf2out_frame_debug_expr (rtx, const char *);
441
442 /* Support for complex CFA locations.  */
443 static void output_cfa_loc (dw_cfi_ref);
444 static void output_cfa_loc_raw (dw_cfi_ref);
445 static void get_cfa_from_loc_descr (dw_cfa_location *,
446                                     struct dw_loc_descr_struct *);
447 static struct dw_loc_descr_struct *build_cfa_loc
448   (dw_cfa_location *, HOST_WIDE_INT);
449 static struct dw_loc_descr_struct *build_cfa_aligned_loc
450   (HOST_WIDE_INT, HOST_WIDE_INT);
451 static void def_cfa_1 (const char *, dw_cfa_location *);
452
453 /* How to start an assembler comment.  */
454 #ifndef ASM_COMMENT_START
455 #define ASM_COMMENT_START ";#"
456 #endif
457
458 /* Data and reference forms for relocatable data.  */
459 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
460 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
461
462 #ifndef DEBUG_FRAME_SECTION
463 #define DEBUG_FRAME_SECTION     ".debug_frame"
464 #endif
465
466 #ifndef FUNC_BEGIN_LABEL
467 #define FUNC_BEGIN_LABEL        "LFB"
468 #endif
469
470 #ifndef FUNC_END_LABEL
471 #define FUNC_END_LABEL          "LFE"
472 #endif
473
474 #ifndef FRAME_BEGIN_LABEL
475 #define FRAME_BEGIN_LABEL       "Lframe"
476 #endif
477 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
478 #define CIE_END_LABEL           "LECIE"
479 #define FDE_LABEL               "LSFDE"
480 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
481 #define FDE_END_LABEL           "LEFDE"
482 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
483 #define LINE_NUMBER_END_LABEL   "LELT"
484 #define LN_PROLOG_AS_LABEL      "LASLTP"
485 #define LN_PROLOG_END_LABEL     "LELTP"
486 #define DIE_LABEL_PREFIX        "DW"
487
488 /* The DWARF 2 CFA column which tracks the return address.  Normally this
489    is the column for PC, or the first column after all of the hard
490    registers.  */
491 #ifndef DWARF_FRAME_RETURN_COLUMN
492 #ifdef PC_REGNUM
493 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
494 #else
495 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
496 #endif
497 #endif
498
499 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
500    default, we just provide columns for all registers.  */
501 #ifndef DWARF_FRAME_REGNUM
502 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
503 #endif
504 \f
505 /* Hook used by __throw.  */
506
507 rtx
508 expand_builtin_dwarf_sp_column (void)
509 {
510   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
511   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
512 }
513
514 /* Return a pointer to a copy of the section string name S with all
515    attributes stripped off, and an asterisk prepended (for assemble_name).  */
516
517 static inline char *
518 stripattributes (const char *s)
519 {
520   char *stripped = XNEWVEC (char, strlen (s) + 2);
521   char *p = stripped;
522
523   *p++ = '*';
524
525   while (*s && *s != ',')
526     *p++ = *s++;
527
528   *p = '\0';
529   return stripped;
530 }
531
532 /* MEM is a memory reference for the register size table, each element of
533    which has mode MODE.  Initialize column C as a return address column.  */
534
535 static void
536 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
537 {
538   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
539   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
540   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
541 }
542
543 /* Generate code to initialize the register size table.  */
544
545 void
546 expand_builtin_init_dwarf_reg_sizes (tree address)
547 {
548   unsigned int i;
549   enum machine_mode mode = TYPE_MODE (char_type_node);
550   rtx addr = expand_normal (address);
551   rtx mem = gen_rtx_MEM (BLKmode, addr);
552   bool wrote_return_column = false;
553
554   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
555     {
556       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
557
558       if (rnum < DWARF_FRAME_REGISTERS)
559         {
560           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
561           enum machine_mode save_mode = reg_raw_mode[i];
562           HOST_WIDE_INT size;
563
564           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
565             save_mode = choose_hard_reg_mode (i, 1, true);
566           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
567             {
568               if (save_mode == VOIDmode)
569                 continue;
570               wrote_return_column = true;
571             }
572           size = GET_MODE_SIZE (save_mode);
573           if (offset < 0)
574             continue;
575
576           emit_move_insn (adjust_address (mem, mode, offset),
577                           gen_int_mode (size, mode));
578         }
579     }
580
581   if (!wrote_return_column)
582     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
583
584 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
585   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
586 #endif
587
588   targetm.init_dwarf_reg_sizes_extra (address);
589 }
590
591 /* Convert a DWARF call frame info. operation to its string name */
592
593 static const char *
594 dwarf_cfi_name (unsigned int cfi_opc)
595 {
596   switch (cfi_opc)
597     {
598     case DW_CFA_advance_loc:
599       return "DW_CFA_advance_loc";
600     case DW_CFA_offset:
601       return "DW_CFA_offset";
602     case DW_CFA_restore:
603       return "DW_CFA_restore";
604     case DW_CFA_nop:
605       return "DW_CFA_nop";
606     case DW_CFA_set_loc:
607       return "DW_CFA_set_loc";
608     case DW_CFA_advance_loc1:
609       return "DW_CFA_advance_loc1";
610     case DW_CFA_advance_loc2:
611       return "DW_CFA_advance_loc2";
612     case DW_CFA_advance_loc4:
613       return "DW_CFA_advance_loc4";
614     case DW_CFA_offset_extended:
615       return "DW_CFA_offset_extended";
616     case DW_CFA_restore_extended:
617       return "DW_CFA_restore_extended";
618     case DW_CFA_undefined:
619       return "DW_CFA_undefined";
620     case DW_CFA_same_value:
621       return "DW_CFA_same_value";
622     case DW_CFA_register:
623       return "DW_CFA_register";
624     case DW_CFA_remember_state:
625       return "DW_CFA_remember_state";
626     case DW_CFA_restore_state:
627       return "DW_CFA_restore_state";
628     case DW_CFA_def_cfa:
629       return "DW_CFA_def_cfa";
630     case DW_CFA_def_cfa_register:
631       return "DW_CFA_def_cfa_register";
632     case DW_CFA_def_cfa_offset:
633       return "DW_CFA_def_cfa_offset";
634
635     /* DWARF 3 */
636     case DW_CFA_def_cfa_expression:
637       return "DW_CFA_def_cfa_expression";
638     case DW_CFA_expression:
639       return "DW_CFA_expression";
640     case DW_CFA_offset_extended_sf:
641       return "DW_CFA_offset_extended_sf";
642     case DW_CFA_def_cfa_sf:
643       return "DW_CFA_def_cfa_sf";
644     case DW_CFA_def_cfa_offset_sf:
645       return "DW_CFA_def_cfa_offset_sf";
646
647     /* SGI/MIPS specific */
648     case DW_CFA_MIPS_advance_loc8:
649       return "DW_CFA_MIPS_advance_loc8";
650
651     /* GNU extensions */
652     case DW_CFA_GNU_window_save:
653       return "DW_CFA_GNU_window_save";
654     case DW_CFA_GNU_args_size:
655       return "DW_CFA_GNU_args_size";
656     case DW_CFA_GNU_negative_offset_extended:
657       return "DW_CFA_GNU_negative_offset_extended";
658
659     default:
660       return "DW_CFA_<unknown>";
661     }
662 }
663
664 /* Return a pointer to a newly allocated Call Frame Instruction.  */
665
666 static inline dw_cfi_ref
667 new_cfi (void)
668 {
669   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
670
671   cfi->dw_cfi_next = NULL;
672   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
673   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
674
675   return cfi;
676 }
677
678 /* Add a Call Frame Instruction to list of instructions.  */
679
680 static inline void
681 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
682 {
683   dw_cfi_ref *p;
684   dw_fde_ref fde = current_fde ();
685
686   /* When DRAP is used, CFA is defined with an expression.  Redefine
687      CFA may lead to a different CFA value.   */
688   if (fde && fde->drap_reg != INVALID_REGNUM)
689     switch (cfi->dw_cfi_opc)
690       {
691         case DW_CFA_def_cfa_register:
692         case DW_CFA_def_cfa_offset:
693         case DW_CFA_def_cfa_offset_sf:
694         case DW_CFA_def_cfa:
695         case DW_CFA_def_cfa_sf:
696           gcc_unreachable ();
697
698         default:
699           break;
700       }
701
702   /* Find the end of the chain.  */
703   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
704     ;
705
706   *p = cfi;
707 }
708
709 /* Generate a new label for the CFI info to refer to.  FORCE is true
710    if a label needs to be output even when using .cfi_* directives.  */
711
712 char *
713 dwarf2out_cfi_label (bool force)
714 {
715   static char label[20];
716
717   if (!force && dwarf2out_do_cfi_asm ())
718     {
719       /* In this case, we will be emitting the asm directive instead of
720          the label, so just return a placeholder to keep the rest of the
721          interfaces happy.  */
722       strcpy (label, "<do not output>");
723     }
724   else
725     {
726       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
727       ASM_OUTPUT_LABEL (asm_out_file, label);
728     }
729
730   return label;
731 }
732
733 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
734    or to the CIE if LABEL is NULL.  */
735
736 static void
737 add_fde_cfi (const char *label, dw_cfi_ref cfi)
738 {
739   dw_cfi_ref *list_head = &cie_cfi_head;
740
741   if (dwarf2out_do_cfi_asm ())
742     {
743       if (label)
744         {
745           dw_fde_ref fde = current_fde ();
746
747           gcc_assert (fde != NULL);
748
749           /* We still have to add the cfi to the list so that
750              lookup_cfa works later on.  When -g2 and above we
751              even need to force emitting of CFI labels and
752              add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
753              purposes.  */
754           switch (cfi->dw_cfi_opc)
755             {
756             case DW_CFA_def_cfa_offset:
757             case DW_CFA_def_cfa_offset_sf:
758             case DW_CFA_def_cfa_register:
759             case DW_CFA_def_cfa:
760             case DW_CFA_def_cfa_sf:
761             case DW_CFA_def_cfa_expression:
762             case DW_CFA_restore_state:
763               if (write_symbols != DWARF2_DEBUG
764                   && write_symbols != VMS_AND_DWARF2_DEBUG)
765                 break;
766               if (debug_info_level <= DINFO_LEVEL_TERSE)
767                 break;
768
769               if (*label == 0 || strcmp (label, "<do not output>") == 0)
770                 label = dwarf2out_cfi_label (true);
771
772               if (fde->dw_fde_current_label == NULL
773                   || strcmp (label, fde->dw_fde_current_label) != 0)
774                 {
775                   dw_cfi_ref xcfi;
776
777                   label = xstrdup (label);
778
779                   /* Set the location counter to the new label.  */
780                   xcfi = new_cfi ();
781                   /* It doesn't metter whether DW_CFA_set_loc
782                      or DW_CFA_advance_loc4 is added here, those aren't
783                      emitted into assembly, only looked up by
784                      convert_cfa_to_fb_loc_list.  */
785                   xcfi->dw_cfi_opc = DW_CFA_set_loc;
786                   xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
787                   add_cfi (&fde->dw_fde_cfi, xcfi);
788                   fde->dw_fde_current_label = label;
789                 }
790               break;
791             default:
792               break;
793             }
794
795           output_cfi_directive (cfi);
796
797           list_head = &fde->dw_fde_cfi;
798         }
799       /* ??? If this is a CFI for the CIE, we don't emit.  This
800          assumes that the standard CIE contents that the assembler
801          uses matches the standard CIE contents that the compiler
802          uses.  This is probably a bad assumption.  I'm not quite
803          sure how to address this for now.  */
804     }
805   else if (label)
806     {
807       dw_fde_ref fde = current_fde ();
808
809       gcc_assert (fde != NULL);
810
811       if (*label == 0)
812         label = dwarf2out_cfi_label (false);
813
814       if (fde->dw_fde_current_label == NULL
815           || strcmp (label, fde->dw_fde_current_label) != 0)
816         {
817           dw_cfi_ref xcfi;
818
819           label = xstrdup (label);
820
821           /* Set the location counter to the new label.  */
822           xcfi = new_cfi ();
823           /* If we have a current label, advance from there, otherwise
824              set the location directly using set_loc.  */
825           xcfi->dw_cfi_opc = fde->dw_fde_current_label
826                              ? DW_CFA_advance_loc4
827                              : DW_CFA_set_loc;
828           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
829           add_cfi (&fde->dw_fde_cfi, xcfi);
830
831           fde->dw_fde_current_label = label;
832         }
833
834       list_head = &fde->dw_fde_cfi;
835     }
836
837   add_cfi (list_head, cfi);
838 }
839
840 /* Subroutine of lookup_cfa.  */
841
842 static void
843 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
844 {
845   switch (cfi->dw_cfi_opc)
846     {
847     case DW_CFA_def_cfa_offset:
848     case DW_CFA_def_cfa_offset_sf:
849       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
850       break;
851     case DW_CFA_def_cfa_register:
852       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
853       break;
854     case DW_CFA_def_cfa:
855     case DW_CFA_def_cfa_sf:
856       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
857       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
858       break;
859     case DW_CFA_def_cfa_expression:
860       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
861       break;
862     default:
863       break;
864     }
865 }
866
867 /* Find the previous value for the CFA.  */
868
869 static void
870 lookup_cfa (dw_cfa_location *loc)
871 {
872   dw_cfi_ref cfi;
873   dw_fde_ref fde;
874
875   loc->reg = INVALID_REGNUM;
876   loc->offset = 0;
877   loc->indirect = 0;
878   loc->base_offset = 0;
879
880   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
881     lookup_cfa_1 (cfi, loc);
882
883   fde = current_fde ();
884   if (fde)
885     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
886       lookup_cfa_1 (cfi, loc);
887 }
888
889 /* The current rule for calculating the DWARF2 canonical frame address.  */
890 static dw_cfa_location cfa;
891
892 /* The register used for saving registers to the stack, and its offset
893    from the CFA.  */
894 static dw_cfa_location cfa_store;
895
896 /* The running total of the size of arguments pushed onto the stack.  */
897 static HOST_WIDE_INT args_size;
898
899 /* The last args_size we actually output.  */
900 static HOST_WIDE_INT old_args_size;
901
902 /* Entry point to update the canonical frame address (CFA).
903    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
904    calculated from REG+OFFSET.  */
905
906 void
907 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
908 {
909   dw_cfa_location loc;
910   loc.indirect = 0;
911   loc.base_offset = 0;
912   loc.reg = reg;
913   loc.offset = offset;
914   def_cfa_1 (label, &loc);
915 }
916
917 /* Determine if two dw_cfa_location structures define the same data.  */
918
919 static bool
920 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
921 {
922   return (loc1->reg == loc2->reg
923           && loc1->offset == loc2->offset
924           && loc1->indirect == loc2->indirect
925           && (loc1->indirect == 0
926               || loc1->base_offset == loc2->base_offset));
927 }
928
929 /* This routine does the actual work.  The CFA is now calculated from
930    the dw_cfa_location structure.  */
931
932 static void
933 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
934 {
935   dw_cfi_ref cfi;
936   dw_cfa_location old_cfa, loc;
937
938   cfa = *loc_p;
939   loc = *loc_p;
940
941   if (cfa_store.reg == loc.reg && loc.indirect == 0)
942     cfa_store.offset = loc.offset;
943
944   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
945   lookup_cfa (&old_cfa);
946
947   /* If nothing changed, no need to issue any call frame instructions.  */
948   if (cfa_equal_p (&loc, &old_cfa))
949     return;
950
951   cfi = new_cfi ();
952
953   if (loc.reg == old_cfa.reg && !loc.indirect)
954     {
955       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
956          the CFA register did not change but the offset did.  The data 
957          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
958          in the assembler via the .cfi_def_cfa_offset directive.  */
959       if (loc.offset < 0)
960         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
961       else
962         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
963       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
964     }
965
966 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
967   else if (loc.offset == old_cfa.offset
968            && old_cfa.reg != INVALID_REGNUM
969            && !loc.indirect)
970     {
971       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
972          indicating the CFA register has changed to <register> but the
973          offset has not changed.  */
974       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
975       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
976     }
977 #endif
978
979   else if (loc.indirect == 0)
980     {
981       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
982          indicating the CFA register has changed to <register> with
983          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
984          happens in output_cfi, or in the assembler via the .cfi_def_cfa
985          directive.  */
986       if (loc.offset < 0)
987         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
988       else
989         cfi->dw_cfi_opc = DW_CFA_def_cfa;
990       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
991       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
992     }
993   else
994     {
995       /* Construct a DW_CFA_def_cfa_expression instruction to
996          calculate the CFA using a full location expression since no
997          register-offset pair is available.  */
998       struct dw_loc_descr_struct *loc_list;
999
1000       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1001       loc_list = build_cfa_loc (&loc, 0);
1002       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1003     }
1004
1005   add_fde_cfi (label, cfi);
1006 }
1007
1008 /* Add the CFI for saving a register.  REG is the CFA column number.
1009    LABEL is passed to add_fde_cfi.
1010    If SREG is -1, the register is saved at OFFSET from the CFA;
1011    otherwise it is saved in SREG.  */
1012
1013 static void
1014 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1015 {
1016   dw_cfi_ref cfi = new_cfi ();
1017   dw_fde_ref fde = current_fde ();
1018
1019   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1020
1021   /* When stack is aligned, store REG using DW_CFA_expression with
1022      FP.  */
1023   if (fde
1024       && fde->stack_realign
1025       && sreg == INVALID_REGNUM)
1026     {
1027       cfi->dw_cfi_opc = DW_CFA_expression;
1028       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1029       cfi->dw_cfi_oprnd1.dw_cfi_loc
1030         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1031     }
1032   else if (sreg == INVALID_REGNUM)
1033     {
1034       if (offset < 0)
1035         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1036       else if (reg & ~0x3f)
1037         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1038       else
1039         cfi->dw_cfi_opc = DW_CFA_offset;
1040       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1041     }
1042   else if (sreg == reg)
1043     cfi->dw_cfi_opc = DW_CFA_same_value;
1044   else
1045     {
1046       cfi->dw_cfi_opc = DW_CFA_register;
1047       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1048     }
1049
1050   add_fde_cfi (label, cfi);
1051 }
1052
1053 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1054    This CFI tells the unwinder that it needs to restore the window registers
1055    from the previous frame's window save area.
1056
1057    ??? Perhaps we should note in the CIE where windows are saved (instead of
1058    assuming 0(cfa)) and what registers are in the window.  */
1059
1060 void
1061 dwarf2out_window_save (const char *label)
1062 {
1063   dw_cfi_ref cfi = new_cfi ();
1064
1065   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1066   add_fde_cfi (label, cfi);
1067 }
1068
1069 /* Add a CFI to update the running total of the size of arguments
1070    pushed onto the stack.  */
1071
1072 void
1073 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1074 {
1075   dw_cfi_ref cfi;
1076
1077   if (size == old_args_size)
1078     return;
1079
1080   old_args_size = size;
1081
1082   cfi = new_cfi ();
1083   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1084   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1085   add_fde_cfi (label, cfi);
1086 }
1087
1088 /* Entry point for saving a register to the stack.  REG is the GCC register
1089    number.  LABEL and OFFSET are passed to reg_save.  */
1090
1091 void
1092 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1093 {
1094   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1095 }
1096
1097 /* Entry point for saving the return address in the stack.
1098    LABEL and OFFSET are passed to reg_save.  */
1099
1100 void
1101 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1102 {
1103   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1104 }
1105
1106 /* Entry point for saving the return address in a register.
1107    LABEL and SREG are passed to reg_save.  */
1108
1109 void
1110 dwarf2out_return_reg (const char *label, unsigned int sreg)
1111 {
1112   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1113 }
1114
1115 #ifdef DWARF2_UNWIND_INFO
1116 /* Record the initial position of the return address.  RTL is
1117    INCOMING_RETURN_ADDR_RTX.  */
1118
1119 static void
1120 initial_return_save (rtx rtl)
1121 {
1122   unsigned int reg = INVALID_REGNUM;
1123   HOST_WIDE_INT offset = 0;
1124
1125   switch (GET_CODE (rtl))
1126     {
1127     case REG:
1128       /* RA is in a register.  */
1129       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1130       break;
1131
1132     case MEM:
1133       /* RA is on the stack.  */
1134       rtl = XEXP (rtl, 0);
1135       switch (GET_CODE (rtl))
1136         {
1137         case REG:
1138           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1139           offset = 0;
1140           break;
1141
1142         case PLUS:
1143           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1144           offset = INTVAL (XEXP (rtl, 1));
1145           break;
1146
1147         case MINUS:
1148           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1149           offset = -INTVAL (XEXP (rtl, 1));
1150           break;
1151
1152         default:
1153           gcc_unreachable ();
1154         }
1155
1156       break;
1157
1158     case PLUS:
1159       /* The return address is at some offset from any value we can
1160          actually load.  For instance, on the SPARC it is in %i7+8. Just
1161          ignore the offset for now; it doesn't matter for unwinding frames.  */
1162       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1163       initial_return_save (XEXP (rtl, 0));
1164       return;
1165
1166     default:
1167       gcc_unreachable ();
1168     }
1169
1170   if (reg != DWARF_FRAME_RETURN_COLUMN)
1171     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1172 }
1173 #endif
1174
1175 /* Given a SET, calculate the amount of stack adjustment it
1176    contains.  */
1177
1178 static HOST_WIDE_INT
1179 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1180                      HOST_WIDE_INT cur_offset)
1181 {
1182   const_rtx src = SET_SRC (pattern);
1183   const_rtx dest = SET_DEST (pattern);
1184   HOST_WIDE_INT offset = 0;
1185   enum rtx_code code;
1186
1187   if (dest == stack_pointer_rtx)
1188     {
1189       code = GET_CODE (src);
1190
1191       /* Assume (set (reg sp) (reg whatever)) sets args_size
1192          level to 0.  */
1193       if (code == REG && src != stack_pointer_rtx)
1194         {
1195           offset = -cur_args_size;
1196 #ifndef STACK_GROWS_DOWNWARD
1197           offset = -offset;
1198 #endif
1199           return offset - cur_offset;
1200         }
1201
1202       if (! (code == PLUS || code == MINUS)
1203           || XEXP (src, 0) != stack_pointer_rtx
1204           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1205         return 0;
1206
1207       /* (set (reg sp) (plus (reg sp) (const_int))) */
1208       offset = INTVAL (XEXP (src, 1));
1209       if (code == PLUS)
1210         offset = -offset;
1211       return offset;
1212     }
1213
1214   if (MEM_P (src) && !MEM_P (dest))
1215     dest = src;
1216   if (MEM_P (dest))
1217     {
1218       /* (set (mem (pre_dec (reg sp))) (foo)) */
1219       src = XEXP (dest, 0);
1220       code = GET_CODE (src);
1221
1222       switch (code)
1223         {
1224         case PRE_MODIFY:
1225         case POST_MODIFY:
1226           if (XEXP (src, 0) == stack_pointer_rtx)
1227             {
1228               rtx val = XEXP (XEXP (src, 1), 1);
1229               /* We handle only adjustments by constant amount.  */
1230               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1231                           && GET_CODE (val) == CONST_INT);
1232               offset = -INTVAL (val);
1233               break;
1234             }
1235           return 0;
1236
1237         case PRE_DEC:
1238         case POST_DEC:
1239           if (XEXP (src, 0) == stack_pointer_rtx)
1240             {
1241               offset = GET_MODE_SIZE (GET_MODE (dest));
1242               break;
1243             }
1244           return 0;
1245
1246         case PRE_INC:
1247         case POST_INC:
1248           if (XEXP (src, 0) == stack_pointer_rtx)
1249             {
1250               offset = -GET_MODE_SIZE (GET_MODE (dest));
1251               break;
1252             }
1253           return 0;
1254
1255         default:
1256           return 0;
1257         }
1258     }
1259   else
1260     return 0;
1261
1262   return offset;
1263 }
1264
1265 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1266    indexed by INSN_UID.  */
1267
1268 static HOST_WIDE_INT *barrier_args_size;
1269
1270 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1271
1272 static HOST_WIDE_INT
1273 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1274                              VEC (rtx, heap) **next)
1275 {
1276   HOST_WIDE_INT offset = 0;
1277   int i;
1278
1279   if (! RTX_FRAME_RELATED_P (insn))
1280     {
1281       if (prologue_epilogue_contains (insn)
1282           || sibcall_epilogue_contains (insn))
1283         /* Nothing */;
1284       else if (GET_CODE (PATTERN (insn)) == SET)
1285         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1286       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1287                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1288         {
1289           /* There may be stack adjustments inside compound insns.  Search
1290              for them.  */
1291           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1292             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1293               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1294                                              cur_args_size, offset);
1295         }
1296     }
1297   else
1298     {
1299       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1300
1301       if (expr)
1302         {
1303           expr = XEXP (expr, 0);
1304           if (GET_CODE (expr) == PARALLEL
1305               || GET_CODE (expr) == SEQUENCE)
1306             for (i = 1; i < XVECLEN (expr, 0); i++)
1307               {
1308                 rtx elem = XVECEXP (expr, 0, i);
1309
1310                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1311                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1312               }
1313         }
1314     }
1315
1316 #ifndef STACK_GROWS_DOWNWARD
1317   offset = -offset;
1318 #endif
1319
1320   cur_args_size += offset;
1321   if (cur_args_size < 0)
1322     cur_args_size = 0;
1323
1324   if (JUMP_P (insn))
1325     {
1326       rtx dest = JUMP_LABEL (insn);
1327
1328       if (dest)
1329         {
1330           if (barrier_args_size [INSN_UID (dest)] < 0)
1331             {
1332               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1333               VEC_safe_push (rtx, heap, *next, dest);
1334             }
1335         }
1336     }
1337
1338   return cur_args_size;
1339 }
1340
1341 /* Walk the whole function and compute args_size on BARRIERs.  */
1342
1343 static void
1344 compute_barrier_args_size (void)
1345 {
1346   int max_uid = get_max_uid (), i;
1347   rtx insn;
1348   VEC (rtx, heap) *worklist, *next, *tmp;
1349
1350   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1351   for (i = 0; i < max_uid; i++)
1352     barrier_args_size[i] = -1;
1353
1354   worklist = VEC_alloc (rtx, heap, 20);
1355   next = VEC_alloc (rtx, heap, 20);
1356   insn = get_insns ();
1357   barrier_args_size[INSN_UID (insn)] = 0;
1358   VEC_quick_push (rtx, worklist, insn);
1359   for (;;)
1360     {
1361       while (!VEC_empty (rtx, worklist))
1362         {
1363           rtx prev, body, first_insn;
1364           HOST_WIDE_INT cur_args_size;
1365
1366           first_insn = insn = VEC_pop (rtx, worklist);
1367           cur_args_size = barrier_args_size[INSN_UID (insn)];
1368           prev = prev_nonnote_insn (insn);
1369           if (prev && BARRIER_P (prev))
1370             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1371
1372           for (; insn; insn = NEXT_INSN (insn))
1373             {
1374               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1375                 continue;
1376               if (BARRIER_P (insn))
1377                 break;
1378
1379               if (LABEL_P (insn))
1380                 {
1381                   if (insn == first_insn)
1382                     continue;
1383                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1384                     {
1385                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1386                       continue;
1387                     }
1388                   else
1389                     {
1390                       /* The insns starting with this label have been
1391                          already scanned or are in the worklist.  */
1392                       break;
1393                     }
1394                 }
1395
1396               body = PATTERN (insn);
1397               if (GET_CODE (body) == SEQUENCE)
1398                 {
1399                   HOST_WIDE_INT dest_args_size = cur_args_size;
1400                   for (i = 1; i < XVECLEN (body, 0); i++)
1401                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1402                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1403                       dest_args_size
1404                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1405                                                        dest_args_size, &next);
1406                     else
1407                       cur_args_size
1408                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1409                                                        cur_args_size, &next);
1410
1411                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1412                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1413                                                  dest_args_size, &next);
1414                   else
1415                     cur_args_size
1416                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1417                                                      cur_args_size, &next);
1418                 }
1419               else
1420                 cur_args_size
1421                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1422             }
1423         }
1424
1425       if (VEC_empty (rtx, next))
1426         break;
1427
1428       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1429       tmp = next;
1430       next = worklist;
1431       worklist = tmp;
1432       VEC_truncate (rtx, next, 0);
1433     }
1434
1435   VEC_free (rtx, heap, worklist);
1436   VEC_free (rtx, heap, next);
1437 }
1438
1439
1440 /* Check INSN to see if it looks like a push or a stack adjustment, and
1441    make a note of it if it does.  EH uses this information to find out how
1442    much extra space it needs to pop off the stack.  */
1443
1444 static void
1445 dwarf2out_stack_adjust (rtx insn, bool after_p)
1446 {
1447   HOST_WIDE_INT offset;
1448   const char *label;
1449   int i;
1450
1451   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1452      with this function.  Proper support would require all frame-related
1453      insns to be marked, and to be able to handle saving state around
1454      epilogues textually in the middle of the function.  */
1455   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1456     return;
1457
1458   /* If INSN is an instruction from target of an annulled branch, the
1459      effects are for the target only and so current argument size
1460      shouldn't change at all.  */
1461   if (final_sequence
1462       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1463       && INSN_FROM_TARGET_P (insn))
1464     return;
1465
1466   /* If only calls can throw, and we have a frame pointer,
1467      save up adjustments until we see the CALL_INSN.  */
1468   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1469     {
1470       if (CALL_P (insn) && !after_p)
1471         {
1472           /* Extract the size of the args from the CALL rtx itself.  */
1473           insn = PATTERN (insn);
1474           if (GET_CODE (insn) == PARALLEL)
1475             insn = XVECEXP (insn, 0, 0);
1476           if (GET_CODE (insn) == SET)
1477             insn = SET_SRC (insn);
1478           gcc_assert (GET_CODE (insn) == CALL);
1479           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1480         }
1481       return;
1482     }
1483
1484   if (CALL_P (insn) && !after_p)
1485     {
1486       if (!flag_asynchronous_unwind_tables)
1487         dwarf2out_args_size ("", args_size);
1488       return;
1489     }
1490   else if (BARRIER_P (insn))
1491     {
1492       /* Don't call compute_barrier_args_size () if the only
1493          BARRIER is at the end of function.  */
1494       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1495         compute_barrier_args_size ();
1496       if (barrier_args_size == NULL)
1497         offset = 0;
1498       else
1499         {
1500           offset = barrier_args_size[INSN_UID (insn)];
1501           if (offset < 0)
1502             offset = 0;
1503         }
1504
1505       offset -= args_size;
1506 #ifndef STACK_GROWS_DOWNWARD
1507       offset = -offset;
1508 #endif
1509     }
1510   else if (GET_CODE (PATTERN (insn)) == SET)
1511     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1512   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1513            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1514     {
1515       /* There may be stack adjustments inside compound insns.  Search
1516          for them.  */
1517       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1518         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1519           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1520                                          args_size, offset);
1521     }
1522   else
1523     return;
1524
1525   if (offset == 0)
1526     return;
1527
1528   label = dwarf2out_cfi_label (false);
1529   dwarf2out_args_size_adjust (offset, label);
1530 }
1531
1532 /* Adjust args_size based on stack adjustment OFFSET.  */
1533
1534 static void
1535 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1536 {
1537   if (cfa.reg == STACK_POINTER_REGNUM)
1538     cfa.offset += offset;
1539
1540   if (cfa_store.reg == STACK_POINTER_REGNUM)
1541     cfa_store.offset += offset;
1542
1543 #ifndef STACK_GROWS_DOWNWARD
1544   offset = -offset;
1545 #endif
1546
1547   args_size += offset;
1548   if (args_size < 0)
1549     args_size = 0;
1550
1551   def_cfa_1 (label, &cfa);
1552   if (flag_asynchronous_unwind_tables)
1553     dwarf2out_args_size (label, args_size);
1554 }
1555
1556 #endif
1557
1558 /* We delay emitting a register save until either (a) we reach the end
1559    of the prologue or (b) the register is clobbered.  This clusters
1560    register saves so that there are fewer pc advances.  */
1561
1562 struct queued_reg_save GTY(())
1563 {
1564   struct queued_reg_save *next;
1565   rtx reg;
1566   HOST_WIDE_INT cfa_offset;
1567   rtx saved_reg;
1568 };
1569
1570 static GTY(()) struct queued_reg_save *queued_reg_saves;
1571
1572 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1573 struct reg_saved_in_data GTY(()) {
1574   rtx orig_reg;
1575   rtx saved_in_reg;
1576 };
1577
1578 /* A list of registers saved in other registers.
1579    The list intentionally has a small maximum capacity of 4; if your
1580    port needs more than that, you might consider implementing a
1581    more efficient data structure.  */
1582 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1583 static GTY(()) size_t num_regs_saved_in_regs;
1584
1585 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1586 static const char *last_reg_save_label;
1587
1588 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1589    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1590
1591 static void
1592 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1593 {
1594   struct queued_reg_save *q;
1595
1596   /* Duplicates waste space, but it's also necessary to remove them
1597      for correctness, since the queue gets output in reverse
1598      order.  */
1599   for (q = queued_reg_saves; q != NULL; q = q->next)
1600     if (REGNO (q->reg) == REGNO (reg))
1601       break;
1602
1603   if (q == NULL)
1604     {
1605       q = GGC_NEW (struct queued_reg_save);
1606       q->next = queued_reg_saves;
1607       queued_reg_saves = q;
1608     }
1609
1610   q->reg = reg;
1611   q->cfa_offset = offset;
1612   q->saved_reg = sreg;
1613
1614   last_reg_save_label = label;
1615 }
1616
1617 /* Output all the entries in QUEUED_REG_SAVES.  */
1618
1619 static void
1620 flush_queued_reg_saves (void)
1621 {
1622   struct queued_reg_save *q;
1623
1624   for (q = queued_reg_saves; q; q = q->next)
1625     {
1626       size_t i;
1627       unsigned int reg, sreg;
1628
1629       for (i = 0; i < num_regs_saved_in_regs; i++)
1630         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1631           break;
1632       if (q->saved_reg && i == num_regs_saved_in_regs)
1633         {
1634           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1635           num_regs_saved_in_regs++;
1636         }
1637       if (i != num_regs_saved_in_regs)
1638         {
1639           regs_saved_in_regs[i].orig_reg = q->reg;
1640           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1641         }
1642
1643       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1644       if (q->saved_reg)
1645         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1646       else
1647         sreg = INVALID_REGNUM;
1648       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1649     }
1650
1651   queued_reg_saves = NULL;
1652   last_reg_save_label = NULL;
1653 }
1654
1655 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1656    location for?  Or, does it clobber a register which we've previously
1657    said that some other register is saved in, and for which we now
1658    have a new location for?  */
1659
1660 static bool
1661 clobbers_queued_reg_save (const_rtx insn)
1662 {
1663   struct queued_reg_save *q;
1664
1665   for (q = queued_reg_saves; q; q = q->next)
1666     {
1667       size_t i;
1668       if (modified_in_p (q->reg, insn))
1669         return true;
1670       for (i = 0; i < num_regs_saved_in_regs; i++)
1671         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1672             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1673           return true;
1674     }
1675
1676   return false;
1677 }
1678
1679 /* Entry point for saving the first register into the second.  */
1680
1681 void
1682 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1683 {
1684   size_t i;
1685   unsigned int regno, sregno;
1686
1687   for (i = 0; i < num_regs_saved_in_regs; i++)
1688     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1689       break;
1690   if (i == num_regs_saved_in_regs)
1691     {
1692       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1693       num_regs_saved_in_regs++;
1694     }
1695   regs_saved_in_regs[i].orig_reg = reg;
1696   regs_saved_in_regs[i].saved_in_reg = sreg;
1697
1698   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1699   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1700   reg_save (label, regno, sregno, 0);
1701 }
1702
1703 /* What register, if any, is currently saved in REG?  */
1704
1705 static rtx
1706 reg_saved_in (rtx reg)
1707 {
1708   unsigned int regn = REGNO (reg);
1709   size_t i;
1710   struct queued_reg_save *q;
1711
1712   for (q = queued_reg_saves; q; q = q->next)
1713     if (q->saved_reg && regn == REGNO (q->saved_reg))
1714       return q->reg;
1715
1716   for (i = 0; i < num_regs_saved_in_regs; i++)
1717     if (regs_saved_in_regs[i].saved_in_reg
1718         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1719       return regs_saved_in_regs[i].orig_reg;
1720
1721   return NULL_RTX;
1722 }
1723
1724
1725 /* A temporary register holding an integral value used in adjusting SP
1726    or setting up the store_reg.  The "offset" field holds the integer
1727    value, not an offset.  */
1728 static dw_cfa_location cfa_temp;
1729
1730 /* Record call frame debugging information for an expression EXPR,
1731    which either sets SP or FP (adjusting how we calculate the frame
1732    address) or saves a register to the stack or another register.
1733    LABEL indicates the address of EXPR.
1734
1735    This function encodes a state machine mapping rtxes to actions on
1736    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1737    users need not read the source code.
1738
1739   The High-Level Picture
1740
1741   Changes in the register we use to calculate the CFA: Currently we
1742   assume that if you copy the CFA register into another register, we
1743   should take the other one as the new CFA register; this seems to
1744   work pretty well.  If it's wrong for some target, it's simple
1745   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1746
1747   Changes in the register we use for saving registers to the stack:
1748   This is usually SP, but not always.  Again, we deduce that if you
1749   copy SP into another register (and SP is not the CFA register),
1750   then the new register is the one we will be using for register
1751   saves.  This also seems to work.
1752
1753   Register saves: There's not much guesswork about this one; if
1754   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1755   register save, and the register used to calculate the destination
1756   had better be the one we think we're using for this purpose.
1757   It's also assumed that a copy from a call-saved register to another
1758   register is saving that register if RTX_FRAME_RELATED_P is set on
1759   that instruction.  If the copy is from a call-saved register to
1760   the *same* register, that means that the register is now the same
1761   value as in the caller.
1762
1763   Except: If the register being saved is the CFA register, and the
1764   offset is nonzero, we are saving the CFA, so we assume we have to
1765   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1766   the intent is to save the value of SP from the previous frame.
1767
1768   In addition, if a register has previously been saved to a different
1769   register,
1770
1771   Invariants / Summaries of Rules
1772
1773   cfa          current rule for calculating the CFA.  It usually
1774                consists of a register and an offset.
1775   cfa_store    register used by prologue code to save things to the stack
1776                cfa_store.offset is the offset from the value of
1777                cfa_store.reg to the actual CFA
1778   cfa_temp     register holding an integral value.  cfa_temp.offset
1779                stores the value, which will be used to adjust the
1780                stack pointer.  cfa_temp is also used like cfa_store,
1781                to track stores to the stack via fp or a temp reg.
1782
1783   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1784                with cfa.reg as the first operand changes the cfa.reg and its
1785                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1786                cfa_temp.offset.
1787
1788   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1789                expression yielding a constant.  This sets cfa_temp.reg
1790                and cfa_temp.offset.
1791
1792   Rule 5:      Create a new register cfa_store used to save items to the
1793                stack.
1794
1795   Rules 10-14: Save a register to the stack.  Define offset as the
1796                difference of the original location and cfa_store's
1797                location (or cfa_temp's location if cfa_temp is used).
1798
1799   Rules 16-20: If AND operation happens on sp in prologue, we assume
1800                stack is realigned.  We will use a group of DW_OP_XXX
1801                expressions to represent the location of the stored
1802                register instead of CFA+offset.
1803
1804   The Rules
1805
1806   "{a,b}" indicates a choice of a xor b.
1807   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1808
1809   Rule 1:
1810   (set <reg1> <reg2>:cfa.reg)
1811   effects: cfa.reg = <reg1>
1812            cfa.offset unchanged
1813            cfa_temp.reg = <reg1>
1814            cfa_temp.offset = cfa.offset
1815
1816   Rule 2:
1817   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1818                               {<const_int>,<reg>:cfa_temp.reg}))
1819   effects: cfa.reg = sp if fp used
1820            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1821            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1822              if cfa_store.reg==sp
1823
1824   Rule 3:
1825   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1826   effects: cfa.reg = fp
1827            cfa_offset += +/- <const_int>
1828
1829   Rule 4:
1830   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1831   constraints: <reg1> != fp
1832                <reg1> != sp
1833   effects: cfa.reg = <reg1>
1834            cfa_temp.reg = <reg1>
1835            cfa_temp.offset = cfa.offset
1836
1837   Rule 5:
1838   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1839   constraints: <reg1> != fp
1840                <reg1> != sp
1841   effects: cfa_store.reg = <reg1>
1842            cfa_store.offset = cfa.offset - cfa_temp.offset
1843
1844   Rule 6:
1845   (set <reg> <const_int>)
1846   effects: cfa_temp.reg = <reg>
1847            cfa_temp.offset = <const_int>
1848
1849   Rule 7:
1850   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1851   effects: cfa_temp.reg = <reg1>
1852            cfa_temp.offset |= <const_int>
1853
1854   Rule 8:
1855   (set <reg> (high <exp>))
1856   effects: none
1857
1858   Rule 9:
1859   (set <reg> (lo_sum <exp> <const_int>))
1860   effects: cfa_temp.reg = <reg>
1861            cfa_temp.offset = <const_int>
1862
1863   Rule 10:
1864   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1865   effects: cfa_store.offset -= <const_int>
1866            cfa.offset = cfa_store.offset if cfa.reg == sp
1867            cfa.reg = sp
1868            cfa.base_offset = -cfa_store.offset
1869
1870   Rule 11:
1871   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1872   effects: cfa_store.offset += -/+ mode_size(mem)
1873            cfa.offset = cfa_store.offset if cfa.reg == sp
1874            cfa.reg = sp
1875            cfa.base_offset = -cfa_store.offset
1876
1877   Rule 12:
1878   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1879
1880        <reg2>)
1881   effects: cfa.reg = <reg1>
1882            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1883
1884   Rule 13:
1885   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1886   effects: cfa.reg = <reg1>
1887            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1888
1889   Rule 14:
1890   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1891   effects: cfa.reg = <reg1>
1892            cfa.base_offset = -cfa_temp.offset
1893            cfa_temp.offset -= mode_size(mem)
1894
1895   Rule 15:
1896   (set <reg> {unspec, unspec_volatile})
1897   effects: target-dependent
1898
1899   Rule 16:
1900   (set sp (and: sp <const_int>))
1901   constraints: cfa_store.reg == sp
1902   effects: current_fde.stack_realign = 1
1903            cfa_store.offset = 0
1904            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1905
1906   Rule 17:
1907   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1908   effects: cfa_store.offset += -/+ mode_size(mem)
1909
1910   Rule 18:
1911   (set (mem ({pre_inc, pre_dec} sp)) fp)
1912   constraints: fde->stack_realign == 1
1913   effects: cfa_store.offset = 0
1914            cfa.reg != HARD_FRAME_POINTER_REGNUM
1915
1916   Rule 19:
1917   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1918   constraints: fde->stack_realign == 1
1919                && cfa.offset == 0
1920                && cfa.indirect == 0
1921                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1922   effects: Use DW_CFA_def_cfa_expression to define cfa
1923            cfa.reg == fde->drap_reg
1924
1925   Rule 20:
1926   (set reg fde->drap_reg)
1927   constraints: fde->vdrap_reg == INVALID_REGNUM
1928   effects: fde->vdrap_reg = reg.
1929   (set mem fde->drap_reg)
1930   constraints: fde->drap_reg_saved == 1
1931   effects: none.  */
1932
1933 static void
1934 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1935 {
1936   rtx src, dest, span;
1937   HOST_WIDE_INT offset;
1938   dw_fde_ref fde;
1939
1940   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1941      the PARALLEL independently. The first element is always processed if
1942      it is a SET. This is for backward compatibility.   Other elements
1943      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1944      flag is set in them.  */
1945   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1946     {
1947       int par_index;
1948       int limit = XVECLEN (expr, 0);
1949       rtx elem;
1950
1951       /* PARALLELs have strict read-modify-write semantics, so we
1952          ought to evaluate every rvalue before changing any lvalue.
1953          It's cumbersome to do that in general, but there's an
1954          easy approximation that is enough for all current users:
1955          handle register saves before register assignments.  */
1956       if (GET_CODE (expr) == PARALLEL)
1957         for (par_index = 0; par_index < limit; par_index++)
1958           {
1959             elem = XVECEXP (expr, 0, par_index);
1960             if (GET_CODE (elem) == SET
1961                 && MEM_P (SET_DEST (elem))
1962                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1963               dwarf2out_frame_debug_expr (elem, label);
1964           }
1965
1966       for (par_index = 0; par_index < limit; par_index++)
1967         {
1968           elem = XVECEXP (expr, 0, par_index);
1969           if (GET_CODE (elem) == SET
1970               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1971               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1972             dwarf2out_frame_debug_expr (elem, label);
1973           else if (GET_CODE (elem) == SET
1974                    && par_index != 0
1975                    && !RTX_FRAME_RELATED_P (elem))
1976             {
1977               /* Stack adjustment combining might combine some post-prologue
1978                  stack adjustment into a prologue stack adjustment.  */
1979               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1980
1981               if (offset != 0)
1982                 dwarf2out_args_size_adjust (offset, label);
1983             }
1984         }
1985       return;
1986     }
1987
1988   gcc_assert (GET_CODE (expr) == SET);
1989
1990   src = SET_SRC (expr);
1991   dest = SET_DEST (expr);
1992
1993   if (REG_P (src))
1994     {
1995       rtx rsi = reg_saved_in (src);
1996       if (rsi)
1997         src = rsi;
1998     }
1999
2000   fde = current_fde ();
2001
2002   if (GET_CODE (src) == REG
2003       && fde
2004       && fde->drap_reg == REGNO (src)
2005       && (fde->drap_reg_saved
2006           || GET_CODE (dest) == REG))
2007     {
2008       /* Rule 20 */
2009       /* If we are saving dynamic realign argument pointer to a
2010          register, the destination is virtual dynamic realign
2011          argument pointer.  It may be used to access argument.  */
2012       if (GET_CODE (dest) == REG)
2013         {
2014           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2015           fde->vdrap_reg = REGNO (dest);
2016         }
2017       return;
2018     }
2019
2020   switch (GET_CODE (dest))
2021     {
2022     case REG:
2023       switch (GET_CODE (src))
2024         {
2025           /* Setting FP from SP.  */
2026         case REG:
2027           if (cfa.reg == (unsigned) REGNO (src))
2028             {
2029               /* Rule 1 */
2030               /* Update the CFA rule wrt SP or FP.  Make sure src is
2031                  relative to the current CFA register.
2032
2033                  We used to require that dest be either SP or FP, but the
2034                  ARM copies SP to a temporary register, and from there to
2035                  FP.  So we just rely on the backends to only set
2036                  RTX_FRAME_RELATED_P on appropriate insns.  */
2037               cfa.reg = REGNO (dest);
2038               cfa_temp.reg = cfa.reg;
2039               cfa_temp.offset = cfa.offset;
2040             }
2041           else
2042             {
2043               /* Saving a register in a register.  */
2044               gcc_assert (!fixed_regs [REGNO (dest)]
2045                           /* For the SPARC and its register window.  */
2046                           || (DWARF_FRAME_REGNUM (REGNO (src))
2047                               == DWARF_FRAME_RETURN_COLUMN));
2048
2049               /* After stack is aligned, we can only save SP in FP
2050                  if drap register is used.  In this case, we have
2051                  to restore stack pointer with the CFA value and we
2052                  don't generate this DWARF information.  */
2053               if (fde
2054                   && fde->stack_realign
2055                   && REGNO (src) == STACK_POINTER_REGNUM)
2056                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2057                             && fde->drap_reg != INVALID_REGNUM
2058                             && cfa.reg != REGNO (src));
2059               else
2060                 queue_reg_save (label, src, dest, 0);
2061             }
2062           break;
2063
2064         case PLUS:
2065         case MINUS:
2066         case LO_SUM:
2067           if (dest == stack_pointer_rtx)
2068             {
2069               /* Rule 2 */
2070               /* Adjusting SP.  */
2071               switch (GET_CODE (XEXP (src, 1)))
2072                 {
2073                 case CONST_INT:
2074                   offset = INTVAL (XEXP (src, 1));
2075                   break;
2076                 case REG:
2077                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2078                               == cfa_temp.reg);
2079                   offset = cfa_temp.offset;
2080                   break;
2081                 default:
2082                   gcc_unreachable ();
2083                 }
2084
2085               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2086                 {
2087                   /* Restoring SP from FP in the epilogue.  */
2088                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2089                   cfa.reg = STACK_POINTER_REGNUM;
2090                 }
2091               else if (GET_CODE (src) == LO_SUM)
2092                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2093                 ;
2094               else
2095                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2096
2097               if (GET_CODE (src) != MINUS)
2098                 offset = -offset;
2099               if (cfa.reg == STACK_POINTER_REGNUM)
2100                 cfa.offset += offset;
2101               if (cfa_store.reg == STACK_POINTER_REGNUM)
2102                 cfa_store.offset += offset;
2103             }
2104           else if (dest == hard_frame_pointer_rtx)
2105             {
2106               /* Rule 3 */
2107               /* Either setting the FP from an offset of the SP,
2108                  or adjusting the FP */
2109               gcc_assert (frame_pointer_needed);
2110
2111               gcc_assert (REG_P (XEXP (src, 0))
2112                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2113                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2114               offset = INTVAL (XEXP (src, 1));
2115               if (GET_CODE (src) != MINUS)
2116                 offset = -offset;
2117               cfa.offset += offset;
2118               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2119             }
2120           else
2121             {
2122               gcc_assert (GET_CODE (src) != MINUS);
2123
2124               /* Rule 4 */
2125               if (REG_P (XEXP (src, 0))
2126                   && REGNO (XEXP (src, 0)) == cfa.reg
2127                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2128                 {
2129                   /* Setting a temporary CFA register that will be copied
2130                      into the FP later on.  */
2131                   offset = - INTVAL (XEXP (src, 1));
2132                   cfa.offset += offset;
2133                   cfa.reg = REGNO (dest);
2134                   /* Or used to save regs to the stack.  */
2135                   cfa_temp.reg = cfa.reg;
2136                   cfa_temp.offset = cfa.offset;
2137                 }
2138
2139               /* Rule 5 */
2140               else if (REG_P (XEXP (src, 0))
2141                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2142                        && XEXP (src, 1) == stack_pointer_rtx)
2143                 {
2144                   /* Setting a scratch register that we will use instead
2145                      of SP for saving registers to the stack.  */
2146                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2147                   cfa_store.reg = REGNO (dest);
2148                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2149                 }
2150
2151               /* Rule 9 */
2152               else if (GET_CODE (src) == LO_SUM
2153                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2154                 {
2155                   cfa_temp.reg = REGNO (dest);
2156                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2157                 }
2158               else
2159                 gcc_unreachable ();
2160             }
2161           break;
2162
2163           /* Rule 6 */
2164         case CONST_INT:
2165           cfa_temp.reg = REGNO (dest);
2166           cfa_temp.offset = INTVAL (src);
2167           break;
2168
2169           /* Rule 7 */
2170         case IOR:
2171           gcc_assert (REG_P (XEXP (src, 0))
2172                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2173                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2174
2175           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2176             cfa_temp.reg = REGNO (dest);
2177           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2178           break;
2179
2180           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2181              which will fill in all of the bits.  */
2182           /* Rule 8 */
2183         case HIGH:
2184           break;
2185
2186           /* Rule 15 */
2187         case UNSPEC:
2188         case UNSPEC_VOLATILE:
2189           gcc_assert (targetm.dwarf_handle_frame_unspec);
2190           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2191           return;
2192
2193           /* Rule 16 */
2194         case AND:
2195           /* If this AND operation happens on stack pointer in prologue,
2196              we assume the stack is realigned and we extract the
2197              alignment.  */
2198           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2199             {
2200               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2201               fde->stack_realign = 1;
2202               fde->stack_realignment = INTVAL (XEXP (src, 1));
2203               cfa_store.offset = 0;
2204
2205               if (cfa.reg != STACK_POINTER_REGNUM
2206                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2207                 fde->drap_reg = cfa.reg;
2208             }
2209           return;
2210
2211         default:
2212           gcc_unreachable ();
2213         }
2214
2215       def_cfa_1 (label, &cfa);
2216       break;
2217
2218     case MEM:
2219
2220       /* Saving a register to the stack.  Make sure dest is relative to the
2221          CFA register.  */
2222       switch (GET_CODE (XEXP (dest, 0)))
2223         {
2224           /* Rule 10 */
2225           /* With a push.  */
2226         case PRE_MODIFY:
2227           /* We can't handle variable size modifications.  */
2228           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2229                       == CONST_INT);
2230           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2231
2232           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2233                       && cfa_store.reg == STACK_POINTER_REGNUM);
2234
2235           cfa_store.offset += offset;
2236           if (cfa.reg == STACK_POINTER_REGNUM)
2237             cfa.offset = cfa_store.offset;
2238
2239           offset = -cfa_store.offset;
2240           break;
2241
2242           /* Rule 11 */
2243         case PRE_INC:
2244         case PRE_DEC:
2245           offset = GET_MODE_SIZE (GET_MODE (dest));
2246           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2247             offset = -offset;
2248
2249           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2250                        == STACK_POINTER_REGNUM)
2251                       && cfa_store.reg == STACK_POINTER_REGNUM);
2252
2253           cfa_store.offset += offset;
2254
2255           /* Rule 18: If stack is aligned, we will use FP as a
2256              reference to represent the address of the stored
2257              regiser.  */
2258           if (fde
2259               && fde->stack_realign
2260               && src == hard_frame_pointer_rtx)
2261             {
2262               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2263               cfa_store.offset = 0;
2264             }
2265
2266           if (cfa.reg == STACK_POINTER_REGNUM)
2267             cfa.offset = cfa_store.offset;
2268
2269           offset = -cfa_store.offset;
2270           break;
2271
2272           /* Rule 12 */
2273           /* With an offset.  */
2274         case PLUS:
2275         case MINUS:
2276         case LO_SUM:
2277           {
2278             int regno;
2279
2280             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2281                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2282             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2283             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2284               offset = -offset;
2285
2286             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2287
2288             if (cfa_store.reg == (unsigned) regno)
2289               offset -= cfa_store.offset;
2290             else
2291               {
2292                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2293                 offset -= cfa_temp.offset;
2294               }
2295           }
2296           break;
2297
2298           /* Rule 13 */
2299           /* Without an offset.  */
2300         case REG:
2301           {
2302             int regno = REGNO (XEXP (dest, 0));
2303
2304             if (cfa_store.reg == (unsigned) regno)
2305               offset = -cfa_store.offset;
2306             else
2307               {
2308                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2309                 offset = -cfa_temp.offset;
2310               }
2311           }
2312           break;
2313
2314           /* Rule 14 */
2315         case POST_INC:
2316           gcc_assert (cfa_temp.reg
2317                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2318           offset = -cfa_temp.offset;
2319           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2320           break;
2321
2322         default:
2323           gcc_unreachable ();
2324         }
2325
2326         /* Rule 17 */
2327         /* If the source operand of this MEM operation is not a
2328            register, basically the source is return address.  Here
2329            we only care how much stack grew and we don't save it.  */
2330       if (!REG_P (src))
2331         break;
2332
2333       if (REGNO (src) != STACK_POINTER_REGNUM
2334           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2335           && (unsigned) REGNO (src) == cfa.reg)
2336         {
2337           /* We're storing the current CFA reg into the stack.  */
2338
2339           if (cfa.offset == 0)
2340             {
2341               /* Rule 19 */
2342               /* If stack is aligned, putting CFA reg into stack means
2343                  we can no longer use reg + offset to represent CFA.
2344                  Here we use DW_CFA_def_cfa_expression instead.  The
2345                  result of this expression equals to the original CFA
2346                  value.  */
2347               if (fde
2348                   && fde->stack_realign
2349                   && cfa.indirect == 0
2350                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2351                 {
2352                   dw_cfa_location cfa_exp;
2353
2354                   gcc_assert (fde->drap_reg == cfa.reg);
2355
2356                   cfa_exp.indirect = 1;
2357                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2358                   cfa_exp.base_offset = offset;
2359                   cfa_exp.offset = 0;
2360
2361                   fde->drap_reg_saved = 1;
2362
2363                   def_cfa_1 (label, &cfa_exp);
2364                   break;
2365                 }
2366
2367               /* If the source register is exactly the CFA, assume
2368                  we're saving SP like any other register; this happens
2369                  on the ARM.  */
2370               def_cfa_1 (label, &cfa);
2371               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2372               break;
2373             }
2374           else
2375             {
2376               /* Otherwise, we'll need to look in the stack to
2377                  calculate the CFA.  */
2378               rtx x = XEXP (dest, 0);
2379
2380               if (!REG_P (x))
2381                 x = XEXP (x, 0);
2382               gcc_assert (REG_P (x));
2383
2384               cfa.reg = REGNO (x);
2385               cfa.base_offset = offset;
2386               cfa.indirect = 1;
2387               def_cfa_1 (label, &cfa);
2388               break;
2389             }
2390         }
2391
2392       def_cfa_1 (label, &cfa);
2393       {
2394         span = targetm.dwarf_register_span (src);
2395
2396         if (!span)
2397           queue_reg_save (label, src, NULL_RTX, offset);
2398         else
2399           {
2400             /* We have a PARALLEL describing where the contents of SRC
2401                live.  Queue register saves for each piece of the
2402                PARALLEL.  */
2403             int par_index;
2404             int limit;
2405             HOST_WIDE_INT span_offset = offset;
2406
2407             gcc_assert (GET_CODE (span) == PARALLEL);
2408
2409             limit = XVECLEN (span, 0);
2410             for (par_index = 0; par_index < limit; par_index++)
2411               {
2412                 rtx elem = XVECEXP (span, 0, par_index);
2413
2414                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2415                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2416               }
2417           }
2418       }
2419       break;
2420
2421     default:
2422       gcc_unreachable ();
2423     }
2424 }
2425
2426 /* Record call frame debugging information for INSN, which either
2427    sets SP or FP (adjusting how we calculate the frame address) or saves a
2428    register to the stack.  If INSN is NULL_RTX, initialize our state.
2429
2430    If AFTER_P is false, we're being called before the insn is emitted,
2431    otherwise after.  Call instructions get invoked twice.  */
2432
2433 void
2434 dwarf2out_frame_debug (rtx insn, bool after_p)
2435 {
2436   const char *label;
2437   rtx src;
2438
2439   if (insn == NULL_RTX)
2440     {
2441       size_t i;
2442
2443       /* Flush any queued register saves.  */
2444       flush_queued_reg_saves ();
2445
2446       /* Set up state for generating call frame debug info.  */
2447       lookup_cfa (&cfa);
2448       gcc_assert (cfa.reg
2449                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2450
2451       cfa.reg = STACK_POINTER_REGNUM;
2452       cfa_store = cfa;
2453       cfa_temp.reg = -1;
2454       cfa_temp.offset = 0;
2455
2456       for (i = 0; i < num_regs_saved_in_regs; i++)
2457         {
2458           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2459           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2460         }
2461       num_regs_saved_in_regs = 0;
2462
2463       if (barrier_args_size)
2464         {
2465           XDELETEVEC (barrier_args_size);
2466           barrier_args_size = NULL;
2467         }
2468       return;
2469     }
2470
2471   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2472     flush_queued_reg_saves ();
2473
2474   if (! RTX_FRAME_RELATED_P (insn))
2475     {
2476       if (!ACCUMULATE_OUTGOING_ARGS)
2477         dwarf2out_stack_adjust (insn, after_p);
2478       return;
2479     }
2480
2481   label = dwarf2out_cfi_label (false);
2482   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2483   if (src)
2484     insn = XEXP (src, 0);
2485   else
2486     insn = PATTERN (insn);
2487
2488   dwarf2out_frame_debug_expr (insn, label);
2489 }
2490
2491 #endif
2492
2493 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2494 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2495  (enum dwarf_call_frame_info cfi);
2496
2497 static enum dw_cfi_oprnd_type
2498 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2499 {
2500   switch (cfi)
2501     {
2502     case DW_CFA_nop:
2503     case DW_CFA_GNU_window_save:
2504       return dw_cfi_oprnd_unused;
2505
2506     case DW_CFA_set_loc:
2507     case DW_CFA_advance_loc1:
2508     case DW_CFA_advance_loc2:
2509     case DW_CFA_advance_loc4:
2510     case DW_CFA_MIPS_advance_loc8:
2511       return dw_cfi_oprnd_addr;
2512
2513     case DW_CFA_offset:
2514     case DW_CFA_offset_extended:
2515     case DW_CFA_def_cfa:
2516     case DW_CFA_offset_extended_sf:
2517     case DW_CFA_def_cfa_sf:
2518     case DW_CFA_restore_extended:
2519     case DW_CFA_undefined:
2520     case DW_CFA_same_value:
2521     case DW_CFA_def_cfa_register:
2522     case DW_CFA_register:
2523       return dw_cfi_oprnd_reg_num;
2524
2525     case DW_CFA_def_cfa_offset:
2526     case DW_CFA_GNU_args_size:
2527     case DW_CFA_def_cfa_offset_sf:
2528       return dw_cfi_oprnd_offset;
2529
2530     case DW_CFA_def_cfa_expression:
2531     case DW_CFA_expression:
2532       return dw_cfi_oprnd_loc;
2533
2534     default:
2535       gcc_unreachable ();
2536     }
2537 }
2538
2539 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2540 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2541  (enum dwarf_call_frame_info cfi);
2542
2543 static enum dw_cfi_oprnd_type
2544 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2545 {
2546   switch (cfi)
2547     {
2548     case DW_CFA_def_cfa:
2549     case DW_CFA_def_cfa_sf:
2550     case DW_CFA_offset:
2551     case DW_CFA_offset_extended_sf:
2552     case DW_CFA_offset_extended:
2553       return dw_cfi_oprnd_offset;
2554
2555     case DW_CFA_register:
2556       return dw_cfi_oprnd_reg_num;
2557
2558     default:
2559       return dw_cfi_oprnd_unused;
2560     }
2561 }
2562
2563 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2564
2565 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2566    switch to the data section instead, and write out a synthetic label
2567    for collect2.  */
2568
2569 static void
2570 switch_to_eh_frame_section (void)
2571 {
2572   tree label;
2573
2574 #ifdef EH_FRAME_SECTION_NAME
2575   if (eh_frame_section == 0)
2576     {
2577       int flags;
2578
2579       if (EH_TABLES_CAN_BE_READ_ONLY)
2580         {
2581           int fde_encoding;
2582           int per_encoding;
2583           int lsda_encoding;
2584
2585           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2586                                                        /*global=*/0);
2587           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2588                                                        /*global=*/1);
2589           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2590                                                         /*global=*/0);
2591           flags = ((! flag_pic
2592                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2593                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2594                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2595                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2596                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2597                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2598                    ? 0 : SECTION_WRITE);
2599         }
2600       else
2601         flags = SECTION_WRITE;
2602       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2603     }
2604 #endif
2605
2606   if (eh_frame_section)
2607     switch_to_section (eh_frame_section);
2608   else
2609     {
2610       /* We have no special eh_frame section.  Put the information in
2611          the data section and emit special labels to guide collect2.  */
2612       switch_to_section (data_section);
2613       label = get_file_function_name ("F");
2614       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2615       targetm.asm_out.globalize_label (asm_out_file,
2616                                        IDENTIFIER_POINTER (label));
2617       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2618     }
2619 }
2620
2621 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2622
2623 static HOST_WIDE_INT
2624 div_data_align (HOST_WIDE_INT off)
2625 {
2626   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2627   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2628   return r;
2629 }
2630
2631 /* Output a Call Frame Information opcode and its operand(s).  */
2632
2633 static void
2634 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2635 {
2636   unsigned long r;
2637   HOST_WIDE_INT off;
2638
2639   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2640     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2641                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2642                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2643                          ((unsigned HOST_WIDE_INT)
2644                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2645   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2646     {
2647       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2648       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2649                            "DW_CFA_offset, column 0x%lx", r);
2650       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2651       dw2_asm_output_data_uleb128 (off, NULL);
2652     }
2653   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2654     {
2655       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2656       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2657                            "DW_CFA_restore, column 0x%lx", r);
2658     }
2659   else
2660     {
2661       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2662                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2663
2664       switch (cfi->dw_cfi_opc)
2665         {
2666         case DW_CFA_set_loc:
2667           if (for_eh)
2668             dw2_asm_output_encoded_addr_rtx (
2669                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2670                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2671                 false, NULL);
2672           else
2673             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2674                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2675           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2676           break;
2677
2678         case DW_CFA_advance_loc1:
2679           dw2_asm_output_delta (1, 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_advance_loc2:
2685           dw2_asm_output_delta (2, 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_advance_loc4:
2691           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2692                                 fde->dw_fde_current_label, NULL);
2693           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2694           break;
2695
2696         case DW_CFA_MIPS_advance_loc8:
2697           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2698                                 fde->dw_fde_current_label, NULL);
2699           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2700           break;
2701
2702         case DW_CFA_offset_extended:
2703           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2704           dw2_asm_output_data_uleb128 (r, NULL);
2705           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2706           dw2_asm_output_data_uleb128 (off, NULL);
2707           break;
2708
2709         case DW_CFA_def_cfa:
2710           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2711           dw2_asm_output_data_uleb128 (r, NULL);
2712           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2713           break;
2714
2715         case DW_CFA_offset_extended_sf:
2716           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2717           dw2_asm_output_data_uleb128 (r, NULL);
2718           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2719           dw2_asm_output_data_sleb128 (off, NULL);
2720           break;
2721
2722         case DW_CFA_def_cfa_sf:
2723           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2724           dw2_asm_output_data_uleb128 (r, NULL);
2725           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2726           dw2_asm_output_data_sleb128 (off, NULL);
2727           break;
2728
2729         case DW_CFA_restore_extended:
2730         case DW_CFA_undefined:
2731         case DW_CFA_same_value:
2732         case DW_CFA_def_cfa_register:
2733           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2734           dw2_asm_output_data_uleb128 (r, NULL);
2735           break;
2736
2737         case DW_CFA_register:
2738           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2739           dw2_asm_output_data_uleb128 (r, NULL);
2740           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2741           dw2_asm_output_data_uleb128 (r, NULL);
2742           break;
2743
2744         case DW_CFA_def_cfa_offset:
2745         case DW_CFA_GNU_args_size:
2746           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2747           break;
2748
2749         case DW_CFA_def_cfa_offset_sf:
2750           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2751           dw2_asm_output_data_sleb128 (off, NULL);
2752           break;
2753
2754         case DW_CFA_GNU_window_save:
2755           break;
2756
2757         case DW_CFA_def_cfa_expression:
2758         case DW_CFA_expression:
2759           output_cfa_loc (cfi);
2760           break;
2761
2762         case DW_CFA_GNU_negative_offset_extended:
2763           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2764           gcc_unreachable ();
2765
2766         default:
2767           break;
2768         }
2769     }
2770 }
2771
2772 /* Similar, but do it via assembler directives instead.  */
2773
2774 static void
2775 output_cfi_directive (dw_cfi_ref cfi)
2776 {
2777   unsigned long r, r2;
2778
2779   switch (cfi->dw_cfi_opc)
2780     {
2781     case DW_CFA_advance_loc:
2782     case DW_CFA_advance_loc1:
2783     case DW_CFA_advance_loc2:
2784     case DW_CFA_advance_loc4:
2785     case DW_CFA_MIPS_advance_loc8:
2786     case DW_CFA_set_loc:
2787       /* Should only be created by add_fde_cfi in a code path not
2788          followed when emitting via directives.  The assembler is
2789          going to take care of this for us.  */
2790       gcc_unreachable ();
2791
2792     case DW_CFA_offset:
2793     case DW_CFA_offset_extended:
2794     case DW_CFA_offset_extended_sf:
2795       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2796       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2797                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2798       break;
2799
2800     case DW_CFA_restore:
2801     case DW_CFA_restore_extended:
2802       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2803       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2804       break;
2805
2806     case DW_CFA_undefined:
2807       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2808       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2809       break;
2810
2811     case DW_CFA_same_value:
2812       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2813       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2814       break;
2815
2816     case DW_CFA_def_cfa:
2817     case DW_CFA_def_cfa_sf:
2818       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2819       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2820                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2821       break;
2822
2823     case DW_CFA_def_cfa_register:
2824       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2825       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2826       break;
2827
2828     case DW_CFA_register:
2829       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
2830       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
2831       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2832       break;
2833
2834     case DW_CFA_def_cfa_offset:
2835     case DW_CFA_def_cfa_offset_sf:
2836       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2837                HOST_WIDE_INT_PRINT_DEC"\n",
2838                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2839       break;
2840
2841     case DW_CFA_GNU_args_size:
2842       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2843       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2844       if (flag_debug_asm)
2845         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2846                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2847       fputc ('\n', asm_out_file);
2848       break;
2849
2850     case DW_CFA_GNU_window_save:
2851       fprintf (asm_out_file, "\t.cfi_window_save\n");
2852       break;
2853
2854     case DW_CFA_def_cfa_expression:
2855     case DW_CFA_expression:
2856       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2857       output_cfa_loc_raw (cfi);
2858       fputc ('\n', asm_out_file);
2859       break;
2860
2861     default:
2862       gcc_unreachable ();
2863     }
2864 }
2865
2866 /* Output the call frame information used to record information
2867    that relates to calculating the frame pointer, and records the
2868    location of saved registers.  */
2869
2870 static void
2871 output_call_frame_info (int for_eh)
2872 {
2873   unsigned int i;
2874   dw_fde_ref fde;
2875   dw_cfi_ref cfi;
2876   char l1[20], l2[20], section_start_label[20];
2877   bool any_lsda_needed = false;
2878   char augmentation[6];
2879   int augmentation_size;
2880   int fde_encoding = DW_EH_PE_absptr;
2881   int per_encoding = DW_EH_PE_absptr;
2882   int lsda_encoding = DW_EH_PE_absptr;
2883   int return_reg;
2884
2885   /* Don't emit a CIE if there won't be any FDEs.  */
2886   if (fde_table_in_use == 0)
2887     return;
2888
2889   /* Nothing to do if the assembler's doing it all.  */
2890   if (dwarf2out_do_cfi_asm ())
2891     return;
2892
2893   /* If we make FDEs linkonce, we may have to emit an empty label for
2894      an FDE that wouldn't otherwise be emitted.  We want to avoid
2895      having an FDE kept around when the function it refers to is
2896      discarded.  Example where this matters: a primary function
2897      template in C++ requires EH information, but an explicit
2898      specialization doesn't.  */
2899   if (TARGET_USES_WEAK_UNWIND_INFO
2900       && ! flag_asynchronous_unwind_tables
2901       && flag_exceptions
2902       && for_eh)
2903     for (i = 0; i < fde_table_in_use; i++)
2904       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2905           && !fde_table[i].uses_eh_lsda
2906           && ! DECL_WEAK (fde_table[i].decl))
2907         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2908                                       for_eh, /* empty */ 1);
2909
2910   /* If we don't have any functions we'll want to unwind out of, don't
2911      emit any EH unwind information.  Note that if exceptions aren't
2912      enabled, we won't have collected nothrow information, and if we
2913      asked for asynchronous tables, we always want this info.  */
2914   if (for_eh)
2915     {
2916       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2917
2918       for (i = 0; i < fde_table_in_use; i++)
2919         if (fde_table[i].uses_eh_lsda)
2920           any_eh_needed = any_lsda_needed = true;
2921         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2922           any_eh_needed = true;
2923         else if (! fde_table[i].nothrow
2924                  && ! fde_table[i].all_throwers_are_sibcalls)
2925           any_eh_needed = true;
2926
2927       if (! any_eh_needed)
2928         return;
2929     }
2930
2931   /* We're going to be generating comments, so turn on app.  */
2932   if (flag_debug_asm)
2933     app_enable ();
2934
2935   if (for_eh)
2936     switch_to_eh_frame_section ();
2937   else
2938     {
2939       if (!debug_frame_section)
2940         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2941                                            SECTION_DEBUG, NULL);
2942       switch_to_section (debug_frame_section);
2943     }
2944
2945   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2946   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2947
2948   /* Output the CIE.  */
2949   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2950   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2951   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2952     dw2_asm_output_data (4, 0xffffffff,
2953       "Initial length escape value indicating 64-bit DWARF extension");
2954   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2955                         "Length of Common Information Entry");
2956   ASM_OUTPUT_LABEL (asm_out_file, l1);
2957
2958   /* Now that the CIE pointer is PC-relative for EH,
2959      use 0 to identify the CIE.  */
2960   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2961                        (for_eh ? 0 : DWARF_CIE_ID),
2962                        "CIE Identifier Tag");
2963
2964   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2965
2966   augmentation[0] = 0;
2967   augmentation_size = 0;
2968   if (for_eh)
2969     {
2970       char *p;
2971
2972       /* Augmentation:
2973          z      Indicates that a uleb128 is present to size the
2974                 augmentation section.
2975          L      Indicates the encoding (and thus presence) of
2976                 an LSDA pointer in the FDE augmentation.
2977          R      Indicates a non-default pointer encoding for
2978                 FDE code pointers.
2979          P      Indicates the presence of an encoding + language
2980                 personality routine in the CIE augmentation.  */
2981
2982       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2983       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2984       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2985
2986       p = augmentation + 1;
2987       if (eh_personality_libfunc)
2988         {
2989           *p++ = 'P';
2990           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2991           assemble_external_libcall (eh_personality_libfunc);
2992         }
2993       if (any_lsda_needed)
2994         {
2995           *p++ = 'L';
2996           augmentation_size += 1;
2997         }
2998       if (fde_encoding != DW_EH_PE_absptr)
2999         {
3000           *p++ = 'R';
3001           augmentation_size += 1;
3002         }
3003       if (p > augmentation + 1)
3004         {
3005           augmentation[0] = 'z';
3006           *p = '\0';
3007         }
3008
3009       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3010       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3011         {
3012           int offset = (  4             /* Length */
3013                         + 4             /* CIE Id */
3014                         + 1             /* CIE version */
3015                         + strlen (augmentation) + 1     /* Augmentation */
3016                         + size_of_uleb128 (1)           /* Code alignment */
3017                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3018                         + 1             /* RA column */
3019                         + 1             /* Augmentation size */
3020                         + 1             /* Personality encoding */ );
3021           int pad = -offset & (PTR_SIZE - 1);
3022
3023           augmentation_size += pad;
3024
3025           /* Augmentations should be small, so there's scarce need to
3026              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3027           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3028         }
3029     }
3030
3031   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3032   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3033   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3034                                "CIE Data Alignment Factor");
3035
3036   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3037   if (DW_CIE_VERSION == 1)
3038     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3039   else
3040     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3041
3042   if (augmentation[0])
3043     {
3044       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3045       if (eh_personality_libfunc)
3046         {
3047           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3048                                eh_data_format_name (per_encoding));
3049           dw2_asm_output_encoded_addr_rtx (per_encoding,
3050                                            eh_personality_libfunc,
3051                                            true, NULL);
3052         }
3053
3054       if (any_lsda_needed)
3055         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3056                              eh_data_format_name (lsda_encoding));
3057
3058       if (fde_encoding != DW_EH_PE_absptr)
3059         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3060                              eh_data_format_name (fde_encoding));
3061     }
3062
3063   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3064     output_cfi (cfi, NULL, for_eh);
3065
3066   /* Pad the CIE out to an address sized boundary.  */
3067   ASM_OUTPUT_ALIGN (asm_out_file,
3068                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3069   ASM_OUTPUT_LABEL (asm_out_file, l2);
3070
3071   /* Loop through all of the FDE's.  */
3072   for (i = 0; i < fde_table_in_use; i++)
3073     {
3074       fde = &fde_table[i];
3075
3076       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3077       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3078           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3079           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3080           && !fde->uses_eh_lsda)
3081         continue;
3082
3083       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3084       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3085       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3086       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3087       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3088         dw2_asm_output_data (4, 0xffffffff,
3089                              "Initial length escape value indicating 64-bit DWARF extension");
3090       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3091                             "FDE Length");
3092       ASM_OUTPUT_LABEL (asm_out_file, l1);
3093
3094       if (for_eh)
3095         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3096       else
3097         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3098                                debug_frame_section, "FDE CIE offset");
3099
3100       if (for_eh)
3101         {
3102           if (fde->dw_fde_switched_sections)
3103             {
3104               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3105                                       fde->dw_fde_unlikely_section_label);
3106               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3107                                       fde->dw_fde_hot_section_label);
3108               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3109               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3110               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3111                                                "FDE initial location");
3112               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3113                                     fde->dw_fde_hot_section_end_label,
3114                                     fde->dw_fde_hot_section_label,
3115                                     "FDE address range");
3116               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3117                                                "FDE initial location");
3118               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3119                                     fde->dw_fde_unlikely_section_end_label,
3120                                     fde->dw_fde_unlikely_section_label,
3121                                     "FDE address range");
3122             }
3123           else
3124             {
3125               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3126               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3127               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3128                                                sym_ref,
3129                                                false,
3130                                                "FDE initial location");
3131               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3132                                     fde->dw_fde_end, fde->dw_fde_begin,
3133                                     "FDE address range");
3134             }
3135         }
3136       else
3137         {
3138           if (fde->dw_fde_switched_sections)
3139             {
3140               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3141                                    fde->dw_fde_hot_section_label,
3142                                    "FDE initial location");
3143               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3144                                     fde->dw_fde_hot_section_end_label,
3145                                     fde->dw_fde_hot_section_label,
3146                                     "FDE address range");
3147               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3148                                    fde->dw_fde_unlikely_section_label,
3149                                    "FDE initial location");
3150               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3151                                     fde->dw_fde_unlikely_section_end_label,
3152                                     fde->dw_fde_unlikely_section_label,
3153                                     "FDE address range");
3154             }
3155           else
3156             {
3157               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3158                                    "FDE initial location");
3159               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3160                                     fde->dw_fde_end, fde->dw_fde_begin,
3161                                     "FDE address range");
3162             }
3163         }
3164
3165       if (augmentation[0])
3166         {
3167           if (any_lsda_needed)
3168             {
3169               int size = size_of_encoded_value (lsda_encoding);
3170
3171               if (lsda_encoding == DW_EH_PE_aligned)
3172                 {
3173                   int offset = (  4             /* Length */
3174                                 + 4             /* CIE offset */
3175                                 + 2 * size_of_encoded_value (fde_encoding)
3176                                 + 1             /* Augmentation size */ );
3177                   int pad = -offset & (PTR_SIZE - 1);
3178
3179                   size += pad;
3180                   gcc_assert (size_of_uleb128 (size) == 1);
3181                 }
3182
3183               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3184
3185               if (fde->uses_eh_lsda)
3186                 {
3187                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3188                                                fde->funcdef_number);
3189                   dw2_asm_output_encoded_addr_rtx (
3190                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3191                         false, "Language Specific Data Area");
3192                 }
3193               else
3194                 {
3195                   if (lsda_encoding == DW_EH_PE_aligned)
3196                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3197                   dw2_asm_output_data
3198                     (size_of_encoded_value (lsda_encoding), 0,
3199                      "Language Specific Data Area (none)");
3200                 }
3201             }
3202           else
3203             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3204         }
3205
3206       /* Loop through the Call Frame Instructions associated with
3207          this FDE.  */
3208       fde->dw_fde_current_label = fde->dw_fde_begin;
3209       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3210         output_cfi (cfi, fde, for_eh);
3211
3212       /* Pad the FDE out to an address sized boundary.  */
3213       ASM_OUTPUT_ALIGN (asm_out_file,
3214                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3215       ASM_OUTPUT_LABEL (asm_out_file, l2);
3216     }
3217
3218   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3219     dw2_asm_output_data (4, 0, "End of Table");
3220 #ifdef MIPS_DEBUGGING_INFO
3221   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3222      get a value of 0.  Putting .align 0 after the label fixes it.  */
3223   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3224 #endif
3225
3226   /* Turn off app to make assembly quicker.  */
3227   if (flag_debug_asm)
3228     app_disable ();
3229 }
3230
3231 /* Output a marker (i.e. a label) for the beginning of a function, before
3232    the prologue.  */
3233
3234 void
3235 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3236                           const char *file ATTRIBUTE_UNUSED)
3237 {
3238   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3239   char * dup_label;
3240   dw_fde_ref fde;
3241
3242   current_function_func_begin_label = NULL;
3243
3244 #ifdef TARGET_UNWIND_INFO
3245   /* ??? current_function_func_begin_label is also used by except.c
3246      for call-site information.  We must emit this label if it might
3247      be used.  */
3248   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3249       && ! dwarf2out_do_frame ())
3250     return;
3251 #else
3252   if (! dwarf2out_do_frame ())
3253     return;
3254 #endif
3255
3256   switch_to_section (function_section (current_function_decl));
3257   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3258                                current_function_funcdef_no);
3259   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3260                           current_function_funcdef_no);
3261   dup_label = xstrdup (label);
3262   current_function_func_begin_label = dup_label;
3263
3264 #ifdef TARGET_UNWIND_INFO
3265   /* We can elide the fde allocation if we're not emitting debug info.  */
3266   if (! dwarf2out_do_frame ())
3267     return;
3268 #endif
3269
3270   /* Expand the fde table if necessary.  */
3271   if (fde_table_in_use == fde_table_allocated)
3272     {
3273       fde_table_allocated += FDE_TABLE_INCREMENT;
3274       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3275       memset (fde_table + fde_table_in_use, 0,
3276               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3277     }
3278
3279   /* Record the FDE associated with this function.  */
3280   current_funcdef_fde = fde_table_in_use;
3281
3282   /* Add the new FDE at the end of the fde_table.  */
3283   fde = &fde_table[fde_table_in_use++];
3284   fde->decl = current_function_decl;
3285   fde->dw_fde_begin = dup_label;
3286   fde->dw_fde_current_label = dup_label;
3287   fde->dw_fde_hot_section_label = NULL;
3288   fde->dw_fde_hot_section_end_label = NULL;
3289   fde->dw_fde_unlikely_section_label = NULL;
3290   fde->dw_fde_unlikely_section_end_label = NULL;
3291   fde->dw_fde_switched_sections = false;
3292   fde->dw_fde_end = NULL;
3293   fde->dw_fde_cfi = NULL;
3294   fde->funcdef_number = current_function_funcdef_no;
3295   fde->nothrow = TREE_NOTHROW (current_function_decl);
3296   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3297   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3298   fde->drap_reg = INVALID_REGNUM;
3299   fde->vdrap_reg = INVALID_REGNUM;
3300
3301   args_size = old_args_size = 0;
3302
3303   /* We only want to output line number information for the genuine dwarf2
3304      prologue case, not the eh frame case.  */
3305 #ifdef DWARF2_DEBUGGING_INFO
3306   if (file)
3307     dwarf2out_source_line (line, file);
3308 #endif
3309
3310   if (dwarf2out_do_cfi_asm ())
3311     {
3312       int enc;
3313       rtx ref;
3314
3315       fprintf (asm_out_file, "\t.cfi_startproc\n");
3316
3317       if (eh_personality_libfunc)
3318         {
3319           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3320           ref = eh_personality_libfunc;
3321
3322           /* ??? The GAS support isn't entirely consistent.  We have to
3323              handle indirect support ourselves, but PC-relative is done
3324              in the assembler.  Further, the assembler can't handle any
3325              of the weirder relocation types.  */
3326           if (enc & DW_EH_PE_indirect)
3327             ref = dw2_force_const_mem (ref, true);
3328
3329           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3330           output_addr_const (asm_out_file, ref);
3331           fputc ('\n', asm_out_file);
3332         }
3333
3334       if (crtl->uses_eh_lsda)
3335         {
3336           char lab[20];
3337
3338           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3339           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3340                                        current_function_funcdef_no);
3341           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3342           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3343
3344           if (enc & DW_EH_PE_indirect)
3345             ref = dw2_force_const_mem (ref, true);
3346
3347           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3348           output_addr_const (asm_out_file, ref);
3349           fputc ('\n', asm_out_file);
3350         }
3351     }
3352 }
3353
3354 /* Output a marker (i.e. a label) for the absolute end of the generated code
3355    for a function definition.  This gets called *after* the epilogue code has
3356    been generated.  */
3357
3358 void
3359 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3360                         const char *file ATTRIBUTE_UNUSED)
3361 {
3362   dw_fde_ref fde;
3363   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3364
3365   if (dwarf2out_do_cfi_asm ())
3366     fprintf (asm_out_file, "\t.cfi_endproc\n");
3367
3368   /* Output a label to mark the endpoint of the code generated for this
3369      function.  */
3370   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3371                                current_function_funcdef_no);
3372   ASM_OUTPUT_LABEL (asm_out_file, label);
3373   fde = current_fde ();
3374   gcc_assert (fde != NULL);
3375   fde->dw_fde_end = xstrdup (label);
3376 }
3377
3378 void
3379 dwarf2out_frame_init (void)
3380 {
3381   /* Allocate the initial hunk of the fde_table.  */
3382   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3383   fde_table_allocated = FDE_TABLE_INCREMENT;
3384   fde_table_in_use = 0;
3385
3386   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3387      sake of lookup_cfa.  */
3388
3389   /* On entry, the Canonical Frame Address is at SP.  */
3390   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3391
3392 #ifdef DWARF2_UNWIND_INFO
3393   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3394     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3395 #endif
3396 }
3397
3398 void
3399 dwarf2out_frame_finish (void)
3400 {
3401   /* Output call frame information.  */
3402   if (DWARF2_FRAME_INFO)
3403     output_call_frame_info (0);
3404
3405 #ifndef TARGET_UNWIND_INFO
3406   /* Output another copy for the unwinder.  */
3407   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3408     output_call_frame_info (1);
3409 #endif
3410 }
3411
3412 /* Note that the current function section is being used for code.  */
3413
3414 static void
3415 dwarf2out_note_section_used (void)
3416 {
3417   section *sec = current_function_section ();
3418   if (sec == text_section)
3419     text_section_used = true;
3420   else if (sec == cold_text_section)
3421     cold_text_section_used = true;
3422 }
3423
3424 void
3425 dwarf2out_switch_text_section (void)
3426 {
3427   dw_fde_ref fde = current_fde ();
3428
3429   gcc_assert (cfun && fde);
3430
3431   fde->dw_fde_switched_sections = true;
3432   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3433   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3434   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3435   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3436   have_multiple_function_sections = true;
3437
3438   /* Reset the current label on switching text sections, so that we
3439      don't attempt to advance_loc4 between labels in different sections.  */
3440   fde->dw_fde_current_label = NULL;
3441
3442   /* There is no need to mark used sections when not debugging.  */
3443   if (cold_text_section != NULL)
3444     dwarf2out_note_section_used ();
3445 }
3446 #endif
3447 \f
3448 /* And now, the subset of the debugging information support code necessary
3449    for emitting location expressions.  */
3450
3451 /* Data about a single source file.  */
3452 struct dwarf_file_data GTY(())
3453 {
3454   const char * filename;
3455   int emitted_number;
3456 };
3457
3458 /* We need some way to distinguish DW_OP_addr with a direct symbol
3459    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3460 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3461
3462
3463 typedef struct dw_val_struct *dw_val_ref;
3464 typedef struct die_struct *dw_die_ref;
3465 typedef const struct die_struct *const_dw_die_ref;
3466 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3467 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3468
3469 typedef struct deferred_locations_struct GTY(()) 
3470 {
3471   tree variable;
3472   dw_die_ref die;
3473 } deferred_locations;
3474
3475 DEF_VEC_O(deferred_locations);
3476 DEF_VEC_ALLOC_O(deferred_locations,gc);
3477
3478 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3479
3480 /* Each DIE may have a series of attribute/value pairs.  Values
3481    can take on several forms.  The forms that are used in this
3482    implementation are listed below.  */
3483
3484 enum dw_val_class
3485 {
3486   dw_val_class_addr,
3487   dw_val_class_offset,
3488   dw_val_class_loc,
3489   dw_val_class_loc_list,
3490   dw_val_class_range_list,
3491   dw_val_class_const,
3492   dw_val_class_unsigned_const,
3493   dw_val_class_long_long,
3494   dw_val_class_vec,
3495   dw_val_class_flag,
3496   dw_val_class_die_ref,
3497   dw_val_class_fde_ref,
3498   dw_val_class_lbl_id,
3499   dw_val_class_lineptr,
3500   dw_val_class_str,
3501   dw_val_class_macptr,
3502   dw_val_class_file
3503 };
3504
3505 /* Describe a double word constant value.  */
3506 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3507
3508 typedef struct dw_long_long_struct GTY(())
3509 {
3510   unsigned long hi;
3511   unsigned long low;
3512 }
3513 dw_long_long_const;
3514
3515 /* Describe a floating point constant value, or a vector constant value.  */
3516
3517 typedef struct dw_vec_struct GTY(())
3518 {
3519   unsigned char * GTY((length ("%h.length"))) array;
3520   unsigned length;
3521   unsigned elt_size;
3522 }
3523 dw_vec_const;
3524
3525 /* The dw_val_node describes an attribute's value, as it is
3526    represented internally.  */
3527
3528 typedef struct dw_val_struct GTY(())
3529 {
3530   enum dw_val_class val_class;
3531   union dw_val_struct_union
3532     {
3533       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3534       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3535       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3536       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3537       HOST_WIDE_INT GTY ((default)) val_int;
3538       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3539       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3540       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3541       struct dw_val_die_union
3542         {
3543           dw_die_ref die;
3544           int external;
3545         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3546       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3547       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3548       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3549       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3550       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3551     }
3552   GTY ((desc ("%1.val_class"))) v;
3553 }
3554 dw_val_node;
3555
3556 /* Locations in memory are described using a sequence of stack machine
3557    operations.  */
3558
3559 typedef struct dw_loc_descr_struct GTY(())
3560 {
3561   dw_loc_descr_ref dw_loc_next;
3562   enum dwarf_location_atom dw_loc_opc;
3563   int dw_loc_addr;
3564   dw_val_node dw_loc_oprnd1;
3565   dw_val_node dw_loc_oprnd2;
3566 }
3567 dw_loc_descr_node;
3568
3569 /* Location lists are ranges + location descriptions for that range,
3570    so you can track variables that are in different places over
3571    their entire life.  */
3572 typedef struct dw_loc_list_struct GTY(())
3573 {
3574   dw_loc_list_ref dw_loc_next;
3575   const char *begin; /* Label for begin address of range */
3576   const char *end;  /* Label for end address of range */
3577   char *ll_symbol; /* Label for beginning of location list.
3578                       Only on head of list */
3579   const char *section; /* Section this loclist is relative to */
3580   dw_loc_descr_ref expr;
3581 } dw_loc_list_node;
3582
3583 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3584
3585 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3586
3587 /* Convert a DWARF stack opcode into its string name.  */
3588
3589 static const char *
3590 dwarf_stack_op_name (unsigned int op)
3591 {
3592   switch (op)
3593     {
3594     case DW_OP_addr:
3595     case INTERNAL_DW_OP_tls_addr:
3596       return "DW_OP_addr";
3597     case DW_OP_deref:
3598       return "DW_OP_deref";
3599     case DW_OP_const1u:
3600       return "DW_OP_const1u";
3601     case DW_OP_const1s:
3602       return "DW_OP_const1s";
3603     case DW_OP_const2u:
3604       return "DW_OP_const2u";
3605     case DW_OP_const2s:
3606       return "DW_OP_const2s";
3607     case DW_OP_const4u:
3608       return "DW_OP_const4u";
3609     case DW_OP_const4s:
3610       return "DW_OP_const4s";
3611     case DW_OP_const8u:
3612       return "DW_OP_const8u";
3613     case DW_OP_const8s:
3614       return "DW_OP_const8s";
3615     case DW_OP_constu:
3616       return "DW_OP_constu";
3617     case DW_OP_consts:
3618       return "DW_OP_consts";
3619     case DW_OP_dup:
3620       return "DW_OP_dup";
3621     case DW_OP_drop:
3622       return "DW_OP_drop";
3623     case DW_OP_over:
3624       return "DW_OP_over";
3625     case DW_OP_pick:
3626       return "DW_OP_pick";
3627     case DW_OP_swap:
3628       return "DW_OP_swap";
3629     case DW_OP_rot:
3630       return "DW_OP_rot";
3631     case DW_OP_xderef:
3632       return "DW_OP_xderef";
3633     case DW_OP_abs:
3634       return "DW_OP_abs";
3635     case DW_OP_and:
3636       return "DW_OP_and";
3637     case DW_OP_div:
3638       return "DW_OP_div";
3639     case DW_OP_minus:
3640       return "DW_OP_minus";
3641     case DW_OP_mod:
3642       return "DW_OP_mod";
3643     case DW_OP_mul:
3644       return "DW_OP_mul";
3645     case DW_OP_neg:
3646       return "DW_OP_neg";
3647     case DW_OP_not:
3648       return "DW_OP_not";
3649     case DW_OP_or:
3650       return "DW_OP_or";
3651     case DW_OP_plus:
3652       return "DW_OP_plus";
3653     case DW_OP_plus_uconst:
3654       return "DW_OP_plus_uconst";
3655     case DW_OP_shl:
3656       return "DW_OP_shl";
3657     case DW_OP_shr:
3658       return "DW_OP_shr";
3659     case DW_OP_shra:
3660       return "DW_OP_shra";
3661     case DW_OP_xor:
3662       return "DW_OP_xor";
3663     case DW_OP_bra:
3664       return "DW_OP_bra";
3665     case DW_OP_eq:
3666       return "DW_OP_eq";
3667     case DW_OP_ge:
3668       return "DW_OP_ge";
3669     case DW_OP_gt:
3670       return "DW_OP_gt";
3671     case DW_OP_le:
3672       return "DW_OP_le";
3673     case DW_OP_lt:
3674       return "DW_OP_lt";
3675     case DW_OP_ne:
3676       return "DW_OP_ne";
3677     case DW_OP_skip:
3678       return "DW_OP_skip";
3679     case DW_OP_lit0:
3680       return "DW_OP_lit0";
3681     case DW_OP_lit1:
3682       return "DW_OP_lit1";
3683     case DW_OP_lit2:
3684       return "DW_OP_lit2";
3685     case DW_OP_lit3:
3686       return "DW_OP_lit3";
3687     case DW_OP_lit4:
3688       return "DW_OP_lit4";
3689     case DW_OP_lit5:
3690       return "DW_OP_lit5";
3691     case DW_OP_lit6:
3692       return "DW_OP_lit6";
3693     case DW_OP_lit7:
3694       return "DW_OP_lit7";
3695     case DW_OP_lit8:
3696       return "DW_OP_lit8";
3697     case DW_OP_lit9:
3698       return "DW_OP_lit9";
3699     case DW_OP_lit10:
3700       return "DW_OP_lit10";
3701     case DW_OP_lit11:
3702       return "DW_OP_lit11";
3703     case DW_OP_lit12:
3704       return "DW_OP_lit12";
3705     case DW_OP_lit13:
3706       return "DW_OP_lit13";
3707     case DW_OP_lit14:
3708       return "DW_OP_lit14";
3709     case DW_OP_lit15:
3710       return "DW_OP_lit15";
3711     case DW_OP_lit16:
3712       return "DW_OP_lit16";
3713     case DW_OP_lit17:
3714       return "DW_OP_lit17";
3715     case DW_OP_lit18:
3716       return "DW_OP_lit18";
3717     case DW_OP_lit19:
3718       return "DW_OP_lit19";
3719     case DW_OP_lit20:
3720       return "DW_OP_lit20";
3721     case DW_OP_lit21:
3722       return "DW_OP_lit21";
3723     case DW_OP_lit22:
3724       return "DW_OP_lit22";
3725     case DW_OP_lit23:
3726       return "DW_OP_lit23";
3727     case DW_OP_lit24:
3728       return "DW_OP_lit24";
3729     case DW_OP_lit25:
3730       return "DW_OP_lit25";
3731     case DW_OP_lit26:
3732       return "DW_OP_lit26";
3733     case DW_OP_lit27:
3734       return "DW_OP_lit27";
3735     case DW_OP_lit28:
3736       return "DW_OP_lit28";
3737     case DW_OP_lit29:
3738       return "DW_OP_lit29";
3739     case DW_OP_lit30:
3740       return "DW_OP_lit30";
3741     case DW_OP_lit31:
3742       return "DW_OP_lit31";
3743     case DW_OP_reg0:
3744       return "DW_OP_reg0";
3745     case DW_OP_reg1:
3746       return "DW_OP_reg1";
3747     case DW_OP_reg2:
3748       return "DW_OP_reg2";
3749     case DW_OP_reg3:
3750       return "DW_OP_reg3";
3751     case DW_OP_reg4:
3752       return "DW_OP_reg4";
3753     case DW_OP_reg5:
3754       return "DW_OP_reg5";
3755     case DW_OP_reg6:
3756       return "DW_OP_reg6";
3757     case DW_OP_reg7:
3758       return "DW_OP_reg7";
3759     case DW_OP_reg8:
3760       return "DW_OP_reg8";
3761     case DW_OP_reg9:
3762       return "DW_OP_reg9";
3763     case DW_OP_reg10:
3764       return "DW_OP_reg10";
3765     case DW_OP_reg11:
3766       return "DW_OP_reg11";
3767     case DW_OP_reg12:
3768       return "DW_OP_reg12";
3769     case DW_OP_reg13:
3770       return "DW_OP_reg13";
3771     case DW_OP_reg14:
3772       return "DW_OP_reg14";
3773     case DW_OP_reg15:
3774       return "DW_OP_reg15";
3775     case DW_OP_reg16:
3776       return "DW_OP_reg16";
3777     case DW_OP_reg17:
3778       return "DW_OP_reg17";
3779     case DW_OP_reg18:
3780       return "DW_OP_reg18";
3781     case DW_OP_reg19:
3782       return "DW_OP_reg19";
3783     case DW_OP_reg20:
3784       return "DW_OP_reg20";
3785     case DW_OP_reg21:
3786       return "DW_OP_reg21";
3787     case DW_OP_reg22:
3788       return "DW_OP_reg22";
3789     case DW_OP_reg23:
3790       return "DW_OP_reg23";
3791     case DW_OP_reg24:
3792       return "DW_OP_reg24";
3793     case DW_OP_reg25:
3794       return "DW_OP_reg25";
3795     case DW_OP_reg26:
3796       return "DW_OP_reg26";
3797     case DW_OP_reg27:
3798       return "DW_OP_reg27";
3799     case DW_OP_reg28:
3800       return "DW_OP_reg28";
3801     case DW_OP_reg29:
3802       return "DW_OP_reg29";
3803     case DW_OP_reg30:
3804       return "DW_OP_reg30";
3805     case DW_OP_reg31:
3806       return "DW_OP_reg31";
3807     case DW_OP_breg0:
3808       return "DW_OP_breg0";
3809     case DW_OP_breg1:
3810       return "DW_OP_breg1";
3811     case DW_OP_breg2:
3812       return "DW_OP_breg2";
3813     case DW_OP_breg3:
3814       return "DW_OP_breg3";
3815     case DW_OP_breg4:
3816       return "DW_OP_breg4";
3817     case DW_OP_breg5:
3818       return "DW_OP_breg5";
3819     case DW_OP_breg6:
3820       return "DW_OP_breg6";
3821     case DW_OP_breg7:
3822       return "DW_OP_breg7";
3823     case DW_OP_breg8:
3824       return "DW_OP_breg8";
3825     case DW_OP_breg9:
3826       return "DW_OP_breg9";
3827     case DW_OP_breg10:
3828       return "DW_OP_breg10";
3829     case DW_OP_breg11:
3830       return "DW_OP_breg11";
3831     case DW_OP_breg12:
3832       return "DW_OP_breg12";
3833     case DW_OP_breg13:
3834       return "DW_OP_breg13";
3835     case DW_OP_breg14:
3836       return "DW_OP_breg14";
3837     case DW_OP_breg15:
3838       return "DW_OP_breg15";
3839     case DW_OP_breg16:
3840       return "DW_OP_breg16";
3841     case DW_OP_breg17:
3842       return "DW_OP_breg17";
3843     case DW_OP_breg18:
3844       return "DW_OP_breg18";
3845     case DW_OP_breg19:
3846       return "DW_OP_breg19";
3847     case DW_OP_breg20:
3848       return "DW_OP_breg20";
3849     case DW_OP_breg21:
3850       return "DW_OP_breg21";
3851     case DW_OP_breg22:
3852       return "DW_OP_breg22";
3853     case DW_OP_breg23:
3854       return "DW_OP_breg23";
3855     case DW_OP_breg24:
3856       return "DW_OP_breg24";
3857     case DW_OP_breg25:
3858       return "DW_OP_breg25";
3859     case DW_OP_breg26:
3860       return "DW_OP_breg26";
3861     case DW_OP_breg27:
3862       return "DW_OP_breg27";
3863     case DW_OP_breg28:
3864       return "DW_OP_breg28";
3865     case DW_OP_breg29:
3866       return "DW_OP_breg29";
3867     case DW_OP_breg30:
3868       return "DW_OP_breg30";
3869     case DW_OP_breg31:
3870       return "DW_OP_breg31";
3871     case DW_OP_regx:
3872       return "DW_OP_regx";
3873     case DW_OP_fbreg:
3874       return "DW_OP_fbreg";
3875     case DW_OP_bregx:
3876       return "DW_OP_bregx";
3877     case DW_OP_piece:
3878       return "DW_OP_piece";
3879     case DW_OP_deref_size:
3880       return "DW_OP_deref_size";
3881     case DW_OP_xderef_size:
3882       return "DW_OP_xderef_size";
3883     case DW_OP_nop:
3884       return "DW_OP_nop";
3885     case DW_OP_push_object_address:
3886       return "DW_OP_push_object_address";
3887     case DW_OP_call2:
3888       return "DW_OP_call2";
3889     case DW_OP_call4:
3890       return "DW_OP_call4";
3891     case DW_OP_call_ref:
3892       return "DW_OP_call_ref";
3893     case DW_OP_GNU_push_tls_address:
3894       return "DW_OP_GNU_push_tls_address";
3895     case DW_OP_GNU_uninit:
3896       return "DW_OP_GNU_uninit";
3897     default:
3898       return "OP_<unknown>";
3899     }
3900 }
3901
3902 /* Return a pointer to a newly allocated location description.  Location
3903    descriptions are simple expression terms that can be strung
3904    together to form more complicated location (address) descriptions.  */
3905
3906 static inline dw_loc_descr_ref
3907 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3908                unsigned HOST_WIDE_INT oprnd2)
3909 {
3910   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3911
3912   descr->dw_loc_opc = op;
3913   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3914   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3915   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3916   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3917
3918   return descr;
3919 }
3920
3921 /* Return a pointer to a newly allocated location description for
3922    REG and OFFSET.  */
3923
3924 static inline dw_loc_descr_ref
3925 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3926 {
3927   if (offset)
3928     {
3929       if (reg <= 31)
3930         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3931       else
3932         return new_loc_descr (DW_OP_bregx, reg, offset);
3933     }
3934   else if (reg <= 31)
3935     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3936   else
3937    return new_loc_descr (DW_OP_regx, reg, 0);
3938 }
3939
3940 /* Add a location description term to a location description expression.  */
3941
3942 static inline void
3943 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3944 {
3945   dw_loc_descr_ref *d;
3946
3947   /* Find the end of the chain.  */
3948   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3949     ;
3950
3951   *d = descr;
3952 }
3953
3954 /* Return the size of a location descriptor.  */
3955
3956 static unsigned long
3957 size_of_loc_descr (dw_loc_descr_ref loc)
3958 {
3959   unsigned long size = 1;
3960
3961   switch (loc->dw_loc_opc)
3962     {
3963     case DW_OP_addr:
3964     case INTERNAL_DW_OP_tls_addr:
3965       size += DWARF2_ADDR_SIZE;
3966       break;
3967     case DW_OP_const1u:
3968     case DW_OP_const1s:
3969       size += 1;
3970       break;
3971     case DW_OP_const2u:
3972     case DW_OP_const2s:
3973       size += 2;
3974       break;
3975     case DW_OP_const4u:
3976     case DW_OP_const4s:
3977       size += 4;
3978       break;
3979     case DW_OP_const8u:
3980     case DW_OP_const8s:
3981       size += 8;
3982       break;
3983     case DW_OP_constu:
3984       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3985       break;
3986     case DW_OP_consts:
3987       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3988       break;
3989     case DW_OP_pick:
3990       size += 1;
3991       break;
3992     case DW_OP_plus_uconst:
3993       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3994       break;
3995     case DW_OP_skip:
3996     case DW_OP_bra:
3997       size += 2;
3998       break;
3999     case DW_OP_breg0:
4000     case DW_OP_breg1:
4001     case DW_OP_breg2:
4002     case DW_OP_breg3:
4003     case DW_OP_breg4:
4004     case DW_OP_breg5:
4005     case DW_OP_breg6:
4006     case DW_OP_breg7:
4007     case DW_OP_breg8:
4008     case DW_OP_breg9:
4009     case DW_OP_breg10:
4010     case DW_OP_breg11:
4011     case DW_OP_breg12:
4012     case DW_OP_breg13:
4013     case DW_OP_breg14:
4014     case DW_OP_breg15:
4015     case DW_OP_breg16:
4016     case DW_OP_breg17:
4017     case DW_OP_breg18:
4018     case DW_OP_breg19:
4019     case DW_OP_breg20:
4020     case DW_OP_breg21:
4021     case DW_OP_breg22:
4022     case DW_OP_breg23:
4023     case DW_OP_breg24:
4024     case DW_OP_breg25:
4025     case DW_OP_breg26:
4026     case DW_OP_breg27:
4027     case DW_OP_breg28:
4028     case DW_OP_breg29:
4029     case DW_OP_breg30:
4030     case DW_OP_breg31:
4031       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4032       break;
4033     case DW_OP_regx:
4034       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4035       break;
4036     case DW_OP_fbreg:
4037       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4038       break;
4039     case DW_OP_bregx:
4040       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4041       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4042       break;
4043     case DW_OP_piece:
4044       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4045       break;
4046     case DW_OP_deref_size:
4047     case DW_OP_xderef_size:
4048       size += 1;
4049       break;
4050     case DW_OP_call2:
4051       size += 2;
4052       break;
4053     case DW_OP_call4:
4054       size += 4;
4055       break;
4056     case DW_OP_call_ref:
4057       size += DWARF2_ADDR_SIZE;
4058       break;
4059     default:
4060       break;
4061     }
4062
4063   return size;
4064 }
4065
4066 /* Return the size of a series of location descriptors.  */
4067
4068 static unsigned long
4069 size_of_locs (dw_loc_descr_ref loc)
4070 {
4071   dw_loc_descr_ref l;
4072   unsigned long size;
4073
4074   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4075      field, to avoid writing to a PCH file.  */
4076   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4077     {
4078       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4079         break;
4080       size += size_of_loc_descr (l);
4081     }
4082   if (! l)
4083     return size;
4084
4085   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4086     {
4087       l->dw_loc_addr = size;
4088       size += size_of_loc_descr (l);
4089     }
4090
4091   return size;
4092 }
4093
4094 /* Output location description stack opcode's operands (if any).  */
4095
4096 static void
4097 output_loc_operands (dw_loc_descr_ref loc)
4098 {
4099   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4100   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4101
4102   switch (loc->dw_loc_opc)
4103     {
4104 #ifdef DWARF2_DEBUGGING_INFO
4105     case DW_OP_addr:
4106       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4107       break;
4108     case DW_OP_const2u:
4109     case DW_OP_const2s:
4110       dw2_asm_output_data (2, val1->v.val_int, NULL);
4111       break;
4112     case DW_OP_const4u:
4113     case DW_OP_const4s:
4114       dw2_asm_output_data (4, val1->v.val_int, NULL);
4115       break;
4116     case DW_OP_const8u:
4117     case DW_OP_const8s:
4118       gcc_assert (HOST_BITS_PER_LONG >= 64);
4119       dw2_asm_output_data (8, val1->v.val_int, NULL);
4120       break;
4121     case DW_OP_skip:
4122     case DW_OP_bra:
4123       {
4124         int offset;
4125
4126         gcc_assert (val1->val_class == dw_val_class_loc);
4127         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4128
4129         dw2_asm_output_data (2, offset, NULL);
4130       }
4131       break;
4132 #else
4133     case DW_OP_addr:
4134     case DW_OP_const2u:
4135     case DW_OP_const2s:
4136     case DW_OP_const4u:
4137     case DW_OP_const4s:
4138     case DW_OP_const8u:
4139     case DW_OP_const8s:
4140     case DW_OP_skip:
4141     case DW_OP_bra:
4142       /* We currently don't make any attempt to make sure these are
4143          aligned properly like we do for the main unwind info, so
4144          don't support emitting things larger than a byte if we're
4145          only doing unwinding.  */
4146       gcc_unreachable ();
4147 #endif
4148     case DW_OP_const1u:
4149     case DW_OP_const1s:
4150       dw2_asm_output_data (1, val1->v.val_int, NULL);
4151       break;
4152     case DW_OP_constu:
4153       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4154       break;
4155     case DW_OP_consts:
4156       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4157       break;
4158     case DW_OP_pick:
4159       dw2_asm_output_data (1, val1->v.val_int, NULL);
4160       break;
4161     case DW_OP_plus_uconst:
4162       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4163       break;
4164     case DW_OP_breg0:
4165     case DW_OP_breg1:
4166     case DW_OP_breg2:
4167     case DW_OP_breg3:
4168     case DW_OP_breg4:
4169     case DW_OP_breg5:
4170     case DW_OP_breg6:
4171     case DW_OP_breg7:
4172     case DW_OP_breg8:
4173     case DW_OP_breg9:
4174     case DW_OP_breg10:
4175     case DW_OP_breg11:
4176     case DW_OP_breg12:
4177     case DW_OP_breg13:
4178     case DW_OP_breg14:
4179     case DW_OP_breg15:
4180     case DW_OP_breg16:
4181     case DW_OP_breg17:
4182     case DW_OP_breg18:
4183     case DW_OP_breg19:
4184     case DW_OP_breg20:
4185     case DW_OP_breg21:
4186     case DW_OP_breg22:
4187     case DW_OP_breg23:
4188     case DW_OP_breg24:
4189     case DW_OP_breg25:
4190     case DW_OP_breg26:
4191     case DW_OP_breg27:
4192     case DW_OP_breg28:
4193     case DW_OP_breg29:
4194     case DW_OP_breg30:
4195     case DW_OP_breg31:
4196       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4197       break;
4198     case DW_OP_regx:
4199       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4200       break;
4201     case DW_OP_fbreg:
4202       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4203       break;
4204     case DW_OP_bregx:
4205       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4206       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4207       break;
4208     case DW_OP_piece:
4209       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4210       break;
4211     case DW_OP_deref_size:
4212     case DW_OP_xderef_size:
4213       dw2_asm_output_data (1, val1->v.val_int, NULL);
4214       break;
4215
4216     case INTERNAL_DW_OP_tls_addr:
4217       if (targetm.asm_out.output_dwarf_dtprel)
4218         {
4219           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4220                                                DWARF2_ADDR_SIZE,
4221                                                val1->v.val_addr);
4222           fputc ('\n', asm_out_file);
4223         }
4224       else
4225         gcc_unreachable ();
4226       break;
4227
4228     default:
4229       /* Other codes have no operands.  */
4230       break;
4231     }
4232 }
4233
4234 /* Output a sequence of location operations.  */
4235
4236 static void
4237 output_loc_sequence (dw_loc_descr_ref loc)
4238 {
4239   for (; loc != NULL; loc = loc->dw_loc_next)
4240     {
4241       /* Output the opcode.  */
4242       dw2_asm_output_data (1, loc->dw_loc_opc,
4243                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4244
4245       /* Output the operand(s) (if any).  */
4246       output_loc_operands (loc);
4247     }
4248 }
4249
4250 /* Output location description stack opcode's operands (if any).
4251    The output is single bytes on a line, suitable for .cfi_escape.  */
4252
4253 static void
4254 output_loc_operands_raw (dw_loc_descr_ref loc)
4255 {
4256   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4257   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4258
4259   switch (loc->dw_loc_opc)
4260     {
4261     case DW_OP_addr:
4262       /* We cannot output addresses in .cfi_escape, only bytes.  */
4263       gcc_unreachable ();
4264
4265     case DW_OP_const1u:
4266     case DW_OP_const1s:
4267     case DW_OP_pick:
4268     case DW_OP_deref_size:
4269     case DW_OP_xderef_size:
4270       fputc (',', asm_out_file);
4271       dw2_asm_output_data_raw (1, val1->v.val_int);
4272       break;
4273
4274     case DW_OP_const2u:
4275     case DW_OP_const2s:
4276       fputc (',', asm_out_file);
4277       dw2_asm_output_data_raw (2, val1->v.val_int);
4278       break;
4279
4280     case DW_OP_const4u:
4281     case DW_OP_const4s:
4282       fputc (',', asm_out_file);
4283       dw2_asm_output_data_raw (4, val1->v.val_int);
4284       break;
4285
4286     case DW_OP_const8u:
4287     case DW_OP_const8s:
4288       gcc_assert (HOST_BITS_PER_LONG >= 64);
4289       fputc (',', asm_out_file);
4290       dw2_asm_output_data_raw (8, val1->v.val_int);
4291       break;
4292
4293     case DW_OP_skip:
4294     case DW_OP_bra:
4295       {
4296         int offset;
4297
4298         gcc_assert (val1->val_class == dw_val_class_loc);
4299         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4300
4301         fputc (',', asm_out_file);
4302         dw2_asm_output_data_raw (2, offset);
4303       }
4304       break;
4305
4306     case DW_OP_constu:
4307     case DW_OP_plus_uconst:
4308     case DW_OP_regx:
4309     case DW_OP_piece:
4310       fputc (',', asm_out_file);
4311       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4312       break;
4313
4314     case DW_OP_consts:
4315     case DW_OP_breg0:
4316     case DW_OP_breg1:
4317     case DW_OP_breg2:
4318     case DW_OP_breg3:
4319     case DW_OP_breg4:
4320     case DW_OP_breg5:
4321     case DW_OP_breg6:
4322     case DW_OP_breg7:
4323     case DW_OP_breg8:
4324     case DW_OP_breg9:
4325     case DW_OP_breg10:
4326     case DW_OP_breg11:
4327     case DW_OP_breg12:
4328     case DW_OP_breg13:
4329     case DW_OP_breg14:
4330     case DW_OP_breg15:
4331     case DW_OP_breg16:
4332     case DW_OP_breg17:
4333     case DW_OP_breg18:
4334     case DW_OP_breg19:
4335     case DW_OP_breg20:
4336     case DW_OP_breg21:
4337     case DW_OP_breg22:
4338     case DW_OP_breg23:
4339     case DW_OP_breg24:
4340     case DW_OP_breg25:
4341     case DW_OP_breg26:
4342     case DW_OP_breg27:
4343     case DW_OP_breg28:
4344     case DW_OP_breg29:
4345     case DW_OP_breg30:
4346     case DW_OP_breg31:
4347     case DW_OP_fbreg:
4348       fputc (',', asm_out_file);
4349       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4350       break;
4351
4352     case DW_OP_bregx:
4353       fputc (',', asm_out_file);
4354       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4355       fputc (',', asm_out_file);
4356       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4357       break;
4358
4359     case INTERNAL_DW_OP_tls_addr:
4360       gcc_unreachable ();
4361
4362     default:
4363       /* Other codes have no operands.  */
4364       break;
4365     }
4366 }
4367
4368 static void
4369 output_loc_sequence_raw (dw_loc_descr_ref loc)
4370 {
4371   while (1)
4372     {
4373       /* Output the opcode.  */
4374       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4375       output_loc_operands_raw (loc);
4376
4377       if (!loc->dw_loc_next)
4378         break;
4379       loc = loc->dw_loc_next;
4380
4381       fputc (',', asm_out_file);
4382     }
4383 }
4384
4385 /* This routine will generate the correct assembly data for a location
4386    description based on a cfi entry with a complex address.  */
4387
4388 static void
4389 output_cfa_loc (dw_cfi_ref cfi)
4390 {
4391   dw_loc_descr_ref loc;
4392   unsigned long size;
4393
4394   if (cfi->dw_cfi_opc == DW_CFA_expression)
4395     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4396
4397   /* Output the size of the block.  */
4398   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4399   size = size_of_locs (loc);
4400   dw2_asm_output_data_uleb128 (size, NULL);
4401
4402   /* Now output the operations themselves.  */
4403   output_loc_sequence (loc);
4404 }
4405
4406 /* Similar, but used for .cfi_escape.  */
4407
4408 static void
4409 output_cfa_loc_raw (dw_cfi_ref cfi)
4410 {
4411   dw_loc_descr_ref loc;
4412   unsigned long size;
4413
4414   if (cfi->dw_cfi_opc == DW_CFA_expression)
4415     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4416
4417   /* Output the size of the block.  */
4418   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4419   size = size_of_locs (loc);
4420   dw2_asm_output_data_uleb128_raw (size);
4421   fputc (',', asm_out_file);
4422
4423   /* Now output the operations themselves.  */
4424   output_loc_sequence_raw (loc);
4425 }
4426
4427 /* This function builds a dwarf location descriptor sequence from a
4428    dw_cfa_location, adding the given OFFSET to the result of the
4429    expression.  */
4430
4431 static struct dw_loc_descr_struct *
4432 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4433 {
4434   struct dw_loc_descr_struct *head, *tmp;
4435
4436   offset += cfa->offset;
4437
4438   if (cfa->indirect)
4439     {
4440       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4441       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4442       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4443       add_loc_descr (&head, tmp);
4444       if (offset != 0)
4445         {
4446           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4447           add_loc_descr (&head, tmp);
4448         }
4449     }
4450   else
4451     head = new_reg_loc_descr (cfa->reg, offset);
4452
4453   return head;
4454 }
4455
4456 /* This function builds a dwarf location descriptor sequence for
4457    the address at OFFSET from the CFA when stack is aligned to
4458    ALIGNMENT byte.  */
4459
4460 static struct dw_loc_descr_struct *
4461 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4462 {
4463   struct dw_loc_descr_struct *head;
4464   unsigned int dwarf_fp
4465     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4466
4467  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4468   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4469     {
4470       head = new_reg_loc_descr (dwarf_fp, 0);
4471       add_loc_descr (&head, int_loc_descriptor (alignment));
4472       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4473
4474       add_loc_descr (&head, int_loc_descriptor (offset));
4475       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4476     }
4477   else
4478     head = new_reg_loc_descr (dwarf_fp, offset);
4479   return head;
4480 }
4481
4482 /* This function fills in aa dw_cfa_location structure from a dwarf location
4483    descriptor sequence.  */
4484
4485 static void
4486 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4487 {
4488   struct dw_loc_descr_struct *ptr;
4489   cfa->offset = 0;
4490   cfa->base_offset = 0;
4491   cfa->indirect = 0;
4492   cfa->reg = -1;
4493
4494   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4495     {
4496       enum dwarf_location_atom op = ptr->dw_loc_opc;
4497
4498       switch (op)
4499         {
4500         case DW_OP_reg0:
4501         case DW_OP_reg1:
4502         case DW_OP_reg2:
4503         case DW_OP_reg3:
4504         case DW_OP_reg4:
4505         case DW_OP_reg5:
4506         case DW_OP_reg6:
4507         case DW_OP_reg7:
4508         case DW_OP_reg8:
4509         case DW_OP_reg9:
4510         case DW_OP_reg10:
4511         case DW_OP_reg11:
4512         case DW_OP_reg12:
4513         case DW_OP_reg13:
4514         case DW_OP_reg14:
4515         case DW_OP_reg15:
4516         case DW_OP_reg16:
4517         case DW_OP_reg17:
4518         case DW_OP_reg18:
4519         case DW_OP_reg19:
4520         case DW_OP_reg20:
4521         case DW_OP_reg21:
4522         case DW_OP_reg22:
4523         case DW_OP_reg23:
4524         case DW_OP_reg24:
4525         case DW_OP_reg25:
4526         case DW_OP_reg26:
4527         case DW_OP_reg27:
4528         case DW_OP_reg28:
4529         case DW_OP_reg29:
4530         case DW_OP_reg30:
4531         case DW_OP_reg31:
4532           cfa->reg = op - DW_OP_reg0;
4533           break;
4534         case DW_OP_regx:
4535           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4536           break;
4537         case DW_OP_breg0:
4538         case DW_OP_breg1:
4539         case DW_OP_breg2:
4540         case DW_OP_breg3:
4541         case DW_OP_breg4:
4542         case DW_OP_breg5:
4543         case DW_OP_breg6:
4544         case DW_OP_breg7:
4545         case DW_OP_breg8:
4546         case DW_OP_breg9:
4547         case DW_OP_breg10:
4548         case DW_OP_breg11:
4549         case DW_OP_breg12:
4550         case DW_OP_breg13:
4551         case DW_OP_breg14:
4552         case DW_OP_breg15:
4553         case DW_OP_breg16:
4554         case DW_OP_breg17:
4555         case DW_OP_breg18:
4556         case DW_OP_breg19:
4557         case DW_OP_breg20:
4558         case DW_OP_breg21:
4559         case DW_OP_breg22:
4560         case DW_OP_breg23:
4561         case DW_OP_breg24:
4562         case DW_OP_breg25:
4563         case DW_OP_breg26:
4564         case DW_OP_breg27:
4565         case DW_OP_breg28:
4566         case DW_OP_breg29:
4567         case DW_OP_breg30:
4568         case DW_OP_breg31:
4569           cfa->reg = op - DW_OP_breg0;
4570           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4571           break;
4572         case DW_OP_bregx:
4573           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4574           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4575           break;
4576         case DW_OP_deref:
4577           cfa->indirect = 1;
4578           break;
4579         case DW_OP_plus_uconst:
4580           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4581           break;
4582         default:
4583           internal_error ("DW_LOC_OP %s not implemented",
4584                           dwarf_stack_op_name (ptr->dw_loc_opc));
4585         }
4586     }
4587 }
4588 #endif /* .debug_frame support */
4589 \f
4590 /* And now, the support for symbolic debugging information.  */
4591 #ifdef DWARF2_DEBUGGING_INFO
4592
4593 /* .debug_str support.  */
4594 static int output_indirect_string (void **, void *);
4595
4596 static void dwarf2out_init (const char *);
4597 static void dwarf2out_finish (const char *);
4598 static void dwarf2out_assembly_start (void);
4599 static void dwarf2out_define (unsigned int, const char *);
4600 static void dwarf2out_undef (unsigned int, const char *);
4601 static void dwarf2out_start_source_file (unsigned, const char *);
4602 static void dwarf2out_end_source_file (unsigned);
4603 static void dwarf2out_begin_block (unsigned, unsigned);
4604 static void dwarf2out_end_block (unsigned, unsigned);
4605 static bool dwarf2out_ignore_block (const_tree);
4606 static void dwarf2out_global_decl (tree);
4607 static void dwarf2out_type_decl (tree, int);
4608 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4609 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4610                                                  dw_die_ref);
4611 static void dwarf2out_abstract_function (tree);
4612 static void dwarf2out_var_location (rtx);
4613 static void dwarf2out_begin_function (tree);
4614
4615 /* The debug hooks structure.  */
4616
4617 const struct gcc_debug_hooks dwarf2_debug_hooks =
4618 {
4619   dwarf2out_init,
4620   dwarf2out_finish,
4621   dwarf2out_assembly_start,
4622   dwarf2out_define,
4623   dwarf2out_undef,
4624   dwarf2out_start_source_file,
4625   dwarf2out_end_source_file,
4626   dwarf2out_begin_block,
4627   dwarf2out_end_block,
4628   dwarf2out_ignore_block,
4629   dwarf2out_source_line,
4630   dwarf2out_begin_prologue,
4631   debug_nothing_int_charstar,   /* end_prologue */
4632   dwarf2out_end_epilogue,
4633   dwarf2out_begin_function,
4634   debug_nothing_int,            /* end_function */
4635   dwarf2out_decl,               /* function_decl */
4636   dwarf2out_global_decl,
4637   dwarf2out_type_decl,          /* type_decl */
4638   dwarf2out_imported_module_or_decl,
4639   debug_nothing_tree,           /* deferred_inline_function */
4640   /* The DWARF 2 backend tries to reduce debugging bloat by not
4641      emitting the abstract description of inline functions until
4642      something tries to reference them.  */
4643   dwarf2out_abstract_function,  /* outlining_inline_function */
4644   debug_nothing_rtx,            /* label */
4645   debug_nothing_int,            /* handle_pch */
4646   dwarf2out_var_location,
4647   dwarf2out_switch_text_section,
4648   1                             /* start_end_main_source_file */
4649 };
4650 #endif
4651 \f
4652 /* NOTE: In the comments in this file, many references are made to
4653    "Debugging Information Entries".  This term is abbreviated as `DIE'
4654    throughout the remainder of this file.  */
4655
4656 /* An internal representation of the DWARF output is built, and then
4657    walked to generate the DWARF debugging info.  The walk of the internal
4658    representation is done after the entire program has been compiled.
4659    The types below are used to describe the internal representation.  */
4660
4661 /* Various DIE's use offsets relative to the beginning of the
4662    .debug_info section to refer to each other.  */
4663
4664 typedef long int dw_offset;
4665
4666 /* Define typedefs here to avoid circular dependencies.  */
4667
4668 typedef struct dw_attr_struct *dw_attr_ref;
4669 typedef struct dw_line_info_struct *dw_line_info_ref;
4670 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4671 typedef struct pubname_struct *pubname_ref;
4672 typedef struct dw_ranges_struct *dw_ranges_ref;
4673 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4674
4675 /* Each entry in the line_info_table maintains the file and
4676    line number associated with the label generated for that
4677    entry.  The label gives the PC value associated with
4678    the line number entry.  */
4679
4680 typedef struct dw_line_info_struct GTY(())
4681 {
4682   unsigned long dw_file_num;
4683   unsigned long dw_line_num;
4684 }
4685 dw_line_info_entry;
4686
4687 /* Line information for functions in separate sections; each one gets its
4688    own sequence.  */
4689 typedef struct dw_separate_line_info_struct GTY(())
4690 {
4691   unsigned long dw_file_num;
4692   unsigned long dw_line_num;
4693   unsigned long function;
4694 }
4695 dw_separate_line_info_entry;
4696
4697 /* Each DIE attribute has a field specifying the attribute kind,
4698    a link to the next attribute in the chain, and an attribute value.
4699    Attributes are typically linked below the DIE they modify.  */
4700
4701 typedef struct dw_attr_struct GTY(())
4702 {
4703   enum dwarf_attribute dw_attr;
4704   dw_val_node dw_attr_val;
4705 }
4706 dw_attr_node;
4707
4708 DEF_VEC_O(dw_attr_node);
4709 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4710
4711 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4712    The children of each node form a circular list linked by
4713    die_sib.  die_child points to the node *before* the "first" child node.  */
4714
4715 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4716 {
4717   enum dwarf_tag die_tag;
4718   char *die_symbol;
4719   VEC(dw_attr_node,gc) * die_attr;
4720   dw_die_ref die_parent;
4721   dw_die_ref die_child;
4722   dw_die_ref die_sib;
4723   dw_die_ref die_definition; /* ref from a specification to its definition */
4724   dw_offset die_offset;
4725   unsigned long die_abbrev;
4726   int die_mark;
4727   /* Die is used and must not be pruned as unused.  */
4728   int die_perennial_p;
4729   unsigned int decl_id;
4730 }
4731 die_node;
4732
4733 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4734 #define FOR_EACH_CHILD(die, c, expr) do {       \
4735   c = die->die_child;                           \
4736   if (c) do {                                   \
4737     c = c->die_sib;                             \
4738     expr;                                       \
4739   } while (c != die->die_child);                \
4740 } while (0)
4741
4742 /* The pubname structure */
4743
4744 typedef struct pubname_struct GTY(())
4745 {
4746   dw_die_ref die;
4747   const char *name;
4748 }
4749 pubname_entry;
4750
4751 DEF_VEC_O(pubname_entry);
4752 DEF_VEC_ALLOC_O(pubname_entry, gc);
4753
4754 struct dw_ranges_struct GTY(())
4755 {
4756   /* If this is positive, it's a block number, otherwise it's a
4757      bitwise-negated index into dw_ranges_by_label.  */
4758   int num;
4759 };
4760
4761 struct dw_ranges_by_label_struct GTY(())
4762 {
4763   const char *begin;
4764   const char *end;
4765 };
4766
4767 /* The limbo die list structure.  */
4768 typedef struct limbo_die_struct GTY(())
4769 {
4770   dw_die_ref die;
4771   tree created_for;
4772   struct limbo_die_struct *next;
4773 }
4774 limbo_die_node;
4775
4776 /* How to start an assembler comment.  */
4777 #ifndef ASM_COMMENT_START
4778 #define ASM_COMMENT_START ";#"
4779 #endif
4780
4781 /* Define a macro which returns nonzero for a TYPE_DECL which was
4782    implicitly generated for a tagged type.
4783
4784    Note that unlike the gcc front end (which generates a NULL named
4785    TYPE_DECL node for each complete tagged type, each array type, and
4786    each function type node created) the g++ front end generates a
4787    _named_ TYPE_DECL node for each tagged type node created.
4788    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4789    generate a DW_TAG_typedef DIE for them.  */
4790
4791 #define TYPE_DECL_IS_STUB(decl)                         \
4792   (DECL_NAME (decl) == NULL_TREE                        \
4793    || (DECL_ARTIFICIAL (decl)                           \
4794        && is_tagged_type (TREE_TYPE (decl))             \
4795        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4796            /* This is necessary for stub decls that     \
4797               appear in nested inline functions.  */    \
4798            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4799                && (decl_ultimate_origin (decl)          \
4800                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4801
4802 /* Information concerning the compilation unit's programming
4803    language, and compiler version.  */
4804
4805 /* Fixed size portion of the DWARF compilation unit header.  */
4806 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4807   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4808
4809 /* Fixed size portion of public names info.  */
4810 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4811
4812 /* Fixed size portion of the address range info.  */
4813 #define DWARF_ARANGES_HEADER_SIZE                                       \
4814   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4815                 DWARF2_ADDR_SIZE * 2)                                   \
4816    - DWARF_INITIAL_LENGTH_SIZE)
4817
4818 /* Size of padding portion in the address range info.  It must be
4819    aligned to twice the pointer size.  */
4820 #define DWARF_ARANGES_PAD_SIZE \
4821   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4822                 DWARF2_ADDR_SIZE * 2)                              \
4823    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4824
4825 /* Use assembler line directives if available.  */
4826 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4827 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4828 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4829 #else
4830 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4831 #endif
4832 #endif
4833
4834 /* Minimum line offset in a special line info. opcode.
4835    This value was chosen to give a reasonable range of values.  */
4836 #define DWARF_LINE_BASE  -10
4837
4838 /* First special line opcode - leave room for the standard opcodes.  */
4839 #define DWARF_LINE_OPCODE_BASE  10
4840
4841 /* Range of line offsets in a special line info. opcode.  */
4842 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4843
4844 /* Flag that indicates the initial value of the is_stmt_start flag.
4845    In the present implementation, we do not mark any lines as
4846    the beginning of a source statement, because that information
4847    is not made available by the GCC front-end.  */
4848 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4849
4850 #ifdef DWARF2_DEBUGGING_INFO
4851 /* This location is used by calc_die_sizes() to keep track
4852    the offset of each DIE within the .debug_info section.  */
4853 static unsigned long next_die_offset;
4854 #endif
4855
4856 /* Record the root of the DIE's built for the current compilation unit.  */
4857 static GTY(()) dw_die_ref comp_unit_die;
4858
4859 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4860 static GTY(()) limbo_die_node *limbo_die_list;
4861
4862 /* Filenames referenced by this compilation unit.  */
4863 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4864
4865 /* A hash table of references to DIE's that describe declarations.
4866    The key is a DECL_UID() which is a unique number identifying each decl.  */
4867 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4868
4869 /* A hash table of references to DIE's that describe COMMON blocks.
4870    The key is DECL_UID() ^ die_parent.  */
4871 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4872
4873 /* Node of the variable location list.  */
4874 struct var_loc_node GTY ((chain_next ("%h.next")))
4875 {
4876   rtx GTY (()) var_loc_note;
4877   const char * GTY (()) label;
4878   const char * GTY (()) section_label;
4879   struct var_loc_node * GTY (()) next;
4880 };
4881
4882 /* Variable location list.  */
4883 struct var_loc_list_def GTY (())
4884 {
4885   struct var_loc_node * GTY (()) first;
4886
4887   /* Do not mark the last element of the chained list because
4888      it is marked through the chain.  */
4889   struct var_loc_node * GTY ((skip ("%h"))) last;
4890
4891   /* DECL_UID of the variable decl.  */
4892   unsigned int decl_id;
4893 };
4894 typedef struct var_loc_list_def var_loc_list;
4895
4896
4897 /* Table of decl location linked lists.  */
4898 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4899
4900 /* A pointer to the base of a list of references to DIE's that
4901    are uniquely identified by their tag, presence/absence of
4902    children DIE's, and list of attribute/value pairs.  */
4903 static GTY((length ("abbrev_die_table_allocated")))
4904   dw_die_ref *abbrev_die_table;
4905
4906 /* Number of elements currently allocated for abbrev_die_table.  */
4907 static GTY(()) unsigned abbrev_die_table_allocated;
4908
4909 /* Number of elements in type_die_table currently in use.  */
4910 static GTY(()) unsigned abbrev_die_table_in_use;
4911
4912 /* Size (in elements) of increments by which we may expand the
4913    abbrev_die_table.  */
4914 #define ABBREV_DIE_TABLE_INCREMENT 256
4915
4916 /* A pointer to the base of a table that contains line information
4917    for each source code line in .text in the compilation unit.  */
4918 static GTY((length ("line_info_table_allocated")))
4919      dw_line_info_ref line_info_table;
4920
4921 /* Number of elements currently allocated for line_info_table.  */
4922 static GTY(()) unsigned line_info_table_allocated;
4923
4924 /* Number of elements in line_info_table currently in use.  */
4925 static GTY(()) unsigned line_info_table_in_use;
4926
4927 /* A pointer to the base of a table that contains line information
4928    for each source code line outside of .text in the compilation unit.  */
4929 static GTY ((length ("separate_line_info_table_allocated")))
4930      dw_separate_line_info_ref separate_line_info_table;
4931
4932 /* Number of elements currently allocated for separate_line_info_table.  */
4933 static GTY(()) unsigned separate_line_info_table_allocated;
4934
4935 /* Number of elements in separate_line_info_table currently in use.  */
4936 static GTY(()) unsigned separate_line_info_table_in_use;
4937
4938 /* Size (in elements) of increments by which we may expand the
4939    line_info_table.  */
4940 #define LINE_INFO_TABLE_INCREMENT 1024
4941
4942 /* A pointer to the base of a table that contains a list of publicly
4943    accessible names.  */
4944 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4945
4946 /* A pointer to the base of a table that contains a list of publicly
4947    accessible types.  */
4948 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4949
4950 /* Array of dies for which we should generate .debug_arange info.  */
4951 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4952
4953 /* Number of elements currently allocated for arange_table.  */
4954 static GTY(()) unsigned arange_table_allocated;
4955
4956 /* Number of elements in arange_table currently in use.  */
4957 static GTY(()) unsigned arange_table_in_use;
4958
4959 /* Size (in elements) of increments by which we may expand the
4960    arange_table.  */
4961 #define ARANGE_TABLE_INCREMENT 64
4962
4963 /* Array of dies for which we should generate .debug_ranges info.  */
4964 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4965
4966 /* Number of elements currently allocated for ranges_table.  */
4967 static GTY(()) unsigned ranges_table_allocated;
4968
4969 /* Number of elements in ranges_table currently in use.  */
4970 static GTY(()) unsigned ranges_table_in_use;
4971
4972 /* Array of pairs of labels referenced in ranges_table.  */
4973 static GTY ((length ("ranges_by_label_allocated")))
4974      dw_ranges_by_label_ref ranges_by_label;
4975
4976 /* Number of elements currently allocated for ranges_by_label.  */
4977 static GTY(()) unsigned ranges_by_label_allocated;
4978
4979 /* Number of elements in ranges_by_label currently in use.  */
4980 static GTY(()) unsigned ranges_by_label_in_use;
4981
4982 /* Size (in elements) of increments by which we may expand the
4983    ranges_table.  */
4984 #define RANGES_TABLE_INCREMENT 64
4985
4986 /* Whether we have location lists that need outputting */
4987 static GTY(()) bool have_location_lists;
4988
4989 /* Unique label counter.  */
4990 static GTY(()) unsigned int loclabel_num;
4991
4992 #ifdef DWARF2_DEBUGGING_INFO
4993 /* Record whether the function being analyzed contains inlined functions.  */
4994 static int current_function_has_inlines;
4995 #endif
4996 #if 0 && defined (MIPS_DEBUGGING_INFO)
4997 static int comp_unit_has_inlines;
4998 #endif
4999
5000 /* The last file entry emitted by maybe_emit_file().  */
5001 static GTY(()) struct dwarf_file_data * last_emitted_file;
5002
5003 /* Number of internal labels generated by gen_internal_sym().  */
5004 static GTY(()) int label_num;
5005
5006 /* Cached result of previous call to lookup_filename.  */
5007 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5008
5009 #ifdef DWARF2_DEBUGGING_INFO
5010
5011 /* Offset from the "steady-state frame pointer" to the frame base,
5012    within the current function.  */
5013 static HOST_WIDE_INT frame_pointer_fb_offset;
5014
5015 /* Forward declarations for functions defined in this file.  */
5016
5017 static int is_pseudo_reg (const_rtx);
5018 static tree type_main_variant (tree);
5019 static int is_tagged_type (const_tree);
5020 static const char *dwarf_tag_name (unsigned);
5021 static const char *dwarf_attr_name (unsigned);
5022 static const char *dwarf_form_name (unsigned);
5023 static tree decl_ultimate_origin (const_tree);
5024 static tree decl_class_context (tree);
5025 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5026 static inline enum dw_val_class AT_class (dw_attr_ref);
5027 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5028 static inline unsigned AT_flag (dw_attr_ref);
5029 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5030 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5031 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5032 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5033 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5034                               unsigned long);
5035 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5036                                unsigned int, unsigned char *);
5037 static hashval_t debug_str_do_hash (const void *);
5038 static int debug_str_eq (const void *, const void *);
5039 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5040 static inline const char *AT_string (dw_attr_ref);
5041 static int AT_string_form (dw_attr_ref);
5042 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5043 static void add_AT_specification (dw_die_ref, dw_die_ref);
5044 static inline dw_die_ref AT_ref (dw_attr_ref);
5045 static inline int AT_ref_external (dw_attr_ref);
5046 static inline void set_AT_ref_external (dw_attr_ref, int);
5047 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5048 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5049 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5050 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5051                              dw_loc_list_ref);
5052 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5053 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5054 static inline rtx AT_addr (dw_attr_ref);
5055 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5056 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5057 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5058 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5059                            unsigned HOST_WIDE_INT);
5060 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5061                                unsigned long);
5062 static inline const char *AT_lbl (dw_attr_ref);
5063 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5064 static const char *get_AT_low_pc (dw_die_ref);
5065 static const char *get_AT_hi_pc (dw_die_ref);
5066 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5067 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5068 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5069 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5070 static bool is_c_family (void);
5071 static bool is_cxx (void);
5072 static bool is_java (void);
5073 static bool is_fortran (void);
5074 static bool is_ada (void);
5075 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5076 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5077 static void add_child_die (dw_die_ref, dw_die_ref);
5078 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5079 static dw_die_ref lookup_type_die (tree);
5080 static void equate_type_number_to_die (tree, dw_die_ref);
5081 static hashval_t decl_die_table_hash (const void *);
5082 static int decl_die_table_eq (const void *, const void *);
5083 static dw_die_ref lookup_decl_die (tree);
5084 static hashval_t common_block_die_table_hash (const void *);
5085 static int common_block_die_table_eq (const void *, const void *);
5086 static hashval_t decl_loc_table_hash (const void *);
5087 static int decl_loc_table_eq (const void *, const void *);
5088 static var_loc_list *lookup_decl_loc (const_tree);
5089 static void equate_decl_number_to_die (tree, dw_die_ref);
5090 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5091 static void print_spaces (FILE *);
5092 static void print_die (dw_die_ref, FILE *);
5093 static void print_dwarf_line_table (FILE *);
5094 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5095 static dw_die_ref pop_compile_unit (dw_die_ref);
5096 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5097 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5098 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5099 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5100 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5101 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5102 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5103 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5104 static void compute_section_prefix (dw_die_ref);
5105 static int is_type_die (dw_die_ref);
5106 static int is_comdat_die (dw_die_ref);
5107 static int is_symbol_die (dw_die_ref);
5108 static void assign_symbol_names (dw_die_ref);
5109 static void break_out_includes (dw_die_ref);
5110 static hashval_t htab_cu_hash (const void *);
5111 static int htab_cu_eq (const void *, const void *);
5112 static void htab_cu_del (void *);
5113 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5114 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5115 static void add_sibling_attributes (dw_die_ref);
5116 static void build_abbrev_table (dw_die_ref);
5117 static void output_location_lists (dw_die_ref);
5118 static int constant_size (unsigned HOST_WIDE_INT);
5119 static unsigned long size_of_die (dw_die_ref);
5120 static void calc_die_sizes (dw_die_ref);
5121 static void mark_dies (dw_die_ref);
5122 static void unmark_dies (dw_die_ref);
5123 static void unmark_all_dies (dw_die_ref);
5124 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5125 static unsigned long size_of_aranges (void);
5126 static enum dwarf_form value_format (dw_attr_ref);
5127 static void output_value_format (dw_attr_ref);
5128 static void output_abbrev_section (void);
5129 static void output_die_symbol (dw_die_ref);
5130 static void output_die (dw_die_ref);
5131 static void output_compilation_unit_header (void);
5132 static void output_comp_unit (dw_die_ref, int);
5133 static const char *dwarf2_name (tree, int);
5134 static void add_pubname (tree, dw_die_ref);
5135 static void add_pubname_string (const char *, dw_die_ref);
5136 static void add_pubtype (tree, dw_die_ref);
5137 static void output_pubnames (VEC (pubname_entry,gc) *);
5138 static void add_arange (tree, dw_die_ref);
5139 static void output_aranges (void);
5140 static unsigned int add_ranges_num (int);
5141 static unsigned int add_ranges (const_tree);
5142 static unsigned int add_ranges_by_labels (const char *, const char *);
5143 static void output_ranges (void);
5144 static void output_line_info (void);
5145 static void output_file_names (void);
5146 static dw_die_ref base_type_die (tree);
5147 static int is_base_type (tree);
5148 static bool is_subrange_type (const_tree);
5149 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5150 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5151 static int type_is_enum (const_tree);
5152 static unsigned int dbx_reg_number (const_rtx);
5153 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5154 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5155 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5156                                                 enum var_init_status);
5157 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5158                                                      enum var_init_status);
5159 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5160                                          enum var_init_status);
5161 static int is_based_loc (const_rtx);
5162 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5163                                             enum var_init_status);
5164 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5165                                                enum var_init_status);
5166 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5167 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5168 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5169 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5170 static tree field_type (const_tree);
5171 static unsigned int simple_type_align_in_bits (const_tree);
5172 static unsigned int simple_decl_align_in_bits (const_tree);
5173 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5174 static HOST_WIDE_INT field_byte_offset (const_tree);
5175 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5176                                          dw_loc_descr_ref);
5177 static void add_data_member_location_attribute (dw_die_ref, tree);
5178 static void add_const_value_attribute (dw_die_ref, rtx);
5179 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5180 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5181 static void insert_float (const_rtx, unsigned char *);
5182 static rtx rtl_for_decl_location (tree);
5183 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5184                                                    enum dwarf_attribute);
5185 static void tree_add_const_value_attribute (dw_die_ref, tree);
5186 static void add_name_attribute (dw_die_ref, const char *);
5187 static void add_comp_dir_attribute (dw_die_ref);
5188 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5189 static void add_subscript_info (dw_die_ref, tree, bool);
5190 static void add_byte_size_attribute (dw_die_ref, tree);
5191 static void add_bit_offset_attribute (dw_die_ref, tree);
5192 static void add_bit_size_attribute (dw_die_ref, tree);
5193 static void add_prototyped_attribute (dw_die_ref, tree);
5194 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5195 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5196 static void add_src_coords_attributes (dw_die_ref, tree);
5197 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5198 static void push_decl_scope (tree);
5199 static void pop_decl_scope (void);
5200 static dw_die_ref scope_die_for (tree, dw_die_ref);
5201 static inline int local_scope_p (dw_die_ref);
5202 static inline int class_or_namespace_scope_p (dw_die_ref);
5203 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5204 static void add_calling_convention_attribute (dw_die_ref, tree);
5205 static const char *type_tag (const_tree);
5206 static tree member_declared_type (const_tree);
5207 #if 0
5208 static const char *decl_start_label (tree);
5209 #endif
5210 static void gen_array_type_die (tree, dw_die_ref);
5211 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5212 #if 0
5213 static void gen_entry_point_die (tree, dw_die_ref);
5214 #endif
5215 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5216 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5217 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5218 static void gen_formal_types_die (tree, dw_die_ref);
5219 static void gen_subprogram_die (tree, dw_die_ref);
5220 static void gen_variable_die (tree, tree, dw_die_ref);
5221 static void gen_const_die (tree, dw_die_ref);
5222 static void gen_label_die (tree, dw_die_ref);
5223 static void gen_lexical_block_die (tree, dw_die_ref, int);
5224 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5225 static void gen_field_die (tree, dw_die_ref);
5226 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5227 static dw_die_ref gen_compile_unit_die (const char *);
5228 static void gen_inheritance_die (tree, tree, dw_die_ref);
5229 static void gen_member_die (tree, dw_die_ref);
5230 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5231                                                 enum debug_info_usage);
5232 static void gen_subroutine_type_die (tree, dw_die_ref);
5233 static void gen_typedef_die (tree, dw_die_ref);
5234 static void gen_type_die (tree, dw_die_ref);
5235 static void gen_block_die (tree, dw_die_ref, int);
5236 static void decls_for_scope (tree, dw_die_ref, int);
5237 static int is_redundant_typedef (const_tree);
5238 static void gen_namespace_die (tree, dw_die_ref);
5239 static void gen_decl_die (tree, tree, dw_die_ref);
5240 static dw_die_ref force_decl_die (tree);
5241 static dw_die_ref force_type_die (tree);
5242 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5243 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5244 static struct dwarf_file_data * lookup_filename (const char *);
5245 static void retry_incomplete_types (void);
5246 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5247 static void splice_child_die (dw_die_ref, dw_die_ref);
5248 static int file_info_cmp (const void *, const void *);
5249 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5250                                      const char *, const char *, unsigned);
5251 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5252                                        const char *, const char *,
5253                                        const char *);
5254 static void output_loc_list (dw_loc_list_ref);
5255 static char *gen_internal_sym (const char *);
5256
5257 static void prune_unmark_dies (dw_die_ref);
5258 static void prune_unused_types_mark (dw_die_ref, int);
5259 static void prune_unused_types_walk (dw_die_ref);
5260 static void prune_unused_types_walk_attribs (dw_die_ref);
5261 static void prune_unused_types_prune (dw_die_ref);
5262 static void prune_unused_types (void);
5263 static int maybe_emit_file (struct dwarf_file_data *fd);
5264
5265 /* Section names used to hold DWARF debugging information.  */
5266 #ifndef DEBUG_INFO_SECTION
5267 #define DEBUG_INFO_SECTION      ".debug_info"
5268 #endif
5269 #ifndef DEBUG_ABBREV_SECTION
5270 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5271 #endif
5272 #ifndef DEBUG_ARANGES_SECTION
5273 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5274 #endif
5275 #ifndef DEBUG_MACINFO_SECTION
5276 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5277 #endif
5278 #ifndef DEBUG_LINE_SECTION
5279 #define DEBUG_LINE_SECTION      ".debug_line"
5280 #endif
5281 #ifndef DEBUG_LOC_SECTION
5282 #define DEBUG_LOC_SECTION       ".debug_loc"
5283 #endif
5284 #ifndef DEBUG_PUBNAMES_SECTION
5285 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5286 #endif
5287 #ifndef DEBUG_STR_SECTION
5288 #define DEBUG_STR_SECTION       ".debug_str"
5289 #endif
5290 #ifndef DEBUG_RANGES_SECTION
5291 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5292 #endif
5293
5294 /* Standard ELF section names for compiled code and data.  */
5295 #ifndef TEXT_SECTION_NAME
5296 #define TEXT_SECTION_NAME       ".text"
5297 #endif
5298
5299 /* Section flags for .debug_str section.  */
5300 #define DEBUG_STR_SECTION_FLAGS \
5301   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5302    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5303    : SECTION_DEBUG)
5304
5305 /* Labels we insert at beginning sections we can reference instead of
5306    the section names themselves.  */
5307
5308 #ifndef TEXT_SECTION_LABEL
5309 #define TEXT_SECTION_LABEL              "Ltext"
5310 #endif
5311 #ifndef COLD_TEXT_SECTION_LABEL
5312 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5313 #endif
5314 #ifndef DEBUG_LINE_SECTION_LABEL
5315 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5316 #endif
5317 #ifndef DEBUG_INFO_SECTION_LABEL
5318 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5319 #endif
5320 #ifndef DEBUG_ABBREV_SECTION_LABEL
5321 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5322 #endif
5323 #ifndef DEBUG_LOC_SECTION_LABEL
5324 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5325 #endif
5326 #ifndef DEBUG_RANGES_SECTION_LABEL
5327 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5328 #endif
5329 #ifndef DEBUG_MACINFO_SECTION_LABEL
5330 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5331 #endif
5332
5333 /* Definitions of defaults for formats and names of various special
5334    (artificial) labels which may be generated within this file (when the -g
5335    options is used and DWARF2_DEBUGGING_INFO is in effect.
5336    If necessary, these may be overridden from within the tm.h file, but
5337    typically, overriding these defaults is unnecessary.  */
5338
5339 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5340 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5341 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5342 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5343 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5344 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5345 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5346 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5347 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5348 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5349
5350 #ifndef TEXT_END_LABEL
5351 #define TEXT_END_LABEL          "Letext"
5352 #endif
5353 #ifndef COLD_END_LABEL
5354 #define COLD_END_LABEL          "Letext_cold"
5355 #endif
5356 #ifndef BLOCK_BEGIN_LABEL
5357 #define BLOCK_BEGIN_LABEL       "LBB"
5358 #endif
5359 #ifndef BLOCK_END_LABEL
5360 #define BLOCK_END_LABEL         "LBE"
5361 #endif
5362 #ifndef LINE_CODE_LABEL
5363 #define LINE_CODE_LABEL         "LM"
5364 #endif
5365 #ifndef SEPARATE_LINE_CODE_LABEL
5366 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5367 #endif
5368
5369 \f
5370 /* We allow a language front-end to designate a function that is to be
5371    called to "demangle" any name before it is put into a DIE.  */
5372
5373 static const char *(*demangle_name_func) (const char *);
5374
5375 void
5376 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5377 {
5378   demangle_name_func = func;
5379 }
5380
5381 /* Test if rtl node points to a pseudo register.  */
5382
5383 static inline int
5384 is_pseudo_reg (const_rtx rtl)
5385 {
5386   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5387           || (GET_CODE (rtl) == SUBREG
5388               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5389 }
5390
5391 /* Return a reference to a type, with its const and volatile qualifiers
5392    removed.  */
5393
5394 static inline tree
5395 type_main_variant (tree type)
5396 {
5397   type = TYPE_MAIN_VARIANT (type);
5398
5399   /* ??? There really should be only one main variant among any group of
5400      variants of a given type (and all of the MAIN_VARIANT values for all
5401      members of the group should point to that one type) but sometimes the C
5402      front-end messes this up for array types, so we work around that bug
5403      here.  */
5404   if (TREE_CODE (type) == ARRAY_TYPE)
5405     while (type != TYPE_MAIN_VARIANT (type))
5406       type = TYPE_MAIN_VARIANT (type);
5407
5408   return type;
5409 }
5410
5411 /* Return nonzero if the given type node represents a tagged type.  */
5412
5413 static inline int
5414 is_tagged_type (const_tree type)
5415 {
5416   enum tree_code code = TREE_CODE (type);
5417
5418   return (code == RECORD_TYPE || code == UNION_TYPE
5419           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5420 }
5421
5422 /* Convert a DIE tag into its string name.  */
5423
5424 static const char *
5425 dwarf_tag_name (unsigned int tag)
5426 {
5427   switch (tag)
5428     {
5429     case DW_TAG_padding:
5430       return "DW_TAG_padding";
5431     case DW_TAG_array_type:
5432       return "DW_TAG_array_type";
5433     case DW_TAG_class_type:
5434       return "DW_TAG_class_type";
5435     case DW_TAG_entry_point:
5436       return "DW_TAG_entry_point";
5437     case DW_TAG_enumeration_type:
5438       return "DW_TAG_enumeration_type";
5439     case DW_TAG_formal_parameter:
5440       return "DW_TAG_formal_parameter";
5441     case DW_TAG_imported_declaration:
5442       return "DW_TAG_imported_declaration";
5443     case DW_TAG_label:
5444       return "DW_TAG_label";
5445     case DW_TAG_lexical_block:
5446       return "DW_TAG_lexical_block";
5447     case DW_TAG_member:
5448       return "DW_TAG_member";
5449     case DW_TAG_pointer_type:
5450       return "DW_TAG_pointer_type";
5451     case DW_TAG_reference_type:
5452       return "DW_TAG_reference_type";
5453     case DW_TAG_compile_unit:
5454       return "DW_TAG_compile_unit";
5455     case DW_TAG_string_type:
5456       return "DW_TAG_string_type";
5457     case DW_TAG_structure_type:
5458       return "DW_TAG_structure_type";
5459     case DW_TAG_subroutine_type:
5460       return "DW_TAG_subroutine_type";
5461     case DW_TAG_typedef:
5462       return "DW_TAG_typedef";
5463     case DW_TAG_union_type:
5464       return "DW_TAG_union_type";
5465     case DW_TAG_unspecified_parameters:
5466       return "DW_TAG_unspecified_parameters";
5467     case DW_TAG_variant:
5468       return "DW_TAG_variant";
5469     case DW_TAG_common_block:
5470       return "DW_TAG_common_block";
5471     case DW_TAG_common_inclusion:
5472       return "DW_TAG_common_inclusion";
5473     case DW_TAG_inheritance:
5474       return "DW_TAG_inheritance";
5475     case DW_TAG_inlined_subroutine:
5476       return "DW_TAG_inlined_subroutine";
5477     case DW_TAG_module:
5478       return "DW_TAG_module";
5479     case DW_TAG_ptr_to_member_type:
5480       return "DW_TAG_ptr_to_member_type";
5481     case DW_TAG_set_type:
5482       return "DW_TAG_set_type";
5483     case DW_TAG_subrange_type:
5484       return "DW_TAG_subrange_type";
5485     case DW_TAG_with_stmt:
5486       return "DW_TAG_with_stmt";
5487     case DW_TAG_access_declaration:
5488       return "DW_TAG_access_declaration";
5489     case DW_TAG_base_type:
5490       return "DW_TAG_base_type";
5491     case DW_TAG_catch_block:
5492       return "DW_TAG_catch_block";
5493     case DW_TAG_const_type:
5494       return "DW_TAG_const_type";
5495     case DW_TAG_constant:
5496       return "DW_TAG_constant";
5497     case DW_TAG_enumerator:
5498       return "DW_TAG_enumerator";
5499     case DW_TAG_file_type:
5500       return "DW_TAG_file_type";
5501     case DW_TAG_friend:
5502       return "DW_TAG_friend";
5503     case DW_TAG_namelist:
5504       return "DW_TAG_namelist";
5505     case DW_TAG_namelist_item:
5506       return "DW_TAG_namelist_item";
5507     case DW_TAG_packed_type:
5508       return "DW_TAG_packed_type";
5509     case DW_TAG_subprogram:
5510       return "DW_TAG_subprogram";
5511     case DW_TAG_template_type_param:
5512       return "DW_TAG_template_type_param";
5513     case DW_TAG_template_value_param:
5514       return "DW_TAG_template_value_param";
5515     case DW_TAG_thrown_type:
5516       return "DW_TAG_thrown_type";
5517     case DW_TAG_try_block:
5518       return "DW_TAG_try_block";
5519     case DW_TAG_variant_part:
5520       return "DW_TAG_variant_part";
5521     case DW_TAG_variable:
5522       return "DW_TAG_variable";
5523     case DW_TAG_volatile_type:
5524       return "DW_TAG_volatile_type";
5525     case DW_TAG_dwarf_procedure:
5526       return "DW_TAG_dwarf_procedure";
5527     case DW_TAG_restrict_type:
5528       return "DW_TAG_restrict_type";
5529     case DW_TAG_interface_type:
5530       return "DW_TAG_interface_type";
5531     case DW_TAG_namespace:
5532       return "DW_TAG_namespace";
5533     case DW_TAG_imported_module:
5534       return "DW_TAG_imported_module";
5535     case DW_TAG_unspecified_type:
5536       return "DW_TAG_unspecified_type";
5537     case DW_TAG_partial_unit:
5538       return "DW_TAG_partial_unit";
5539     case DW_TAG_imported_unit:
5540       return "DW_TAG_imported_unit";
5541     case DW_TAG_condition:
5542       return "DW_TAG_condition";
5543     case DW_TAG_shared_type:
5544       return "DW_TAG_shared_type";
5545     case DW_TAG_MIPS_loop:
5546       return "DW_TAG_MIPS_loop";
5547     case DW_TAG_format_label:
5548       return "DW_TAG_format_label";
5549     case DW_TAG_function_template:
5550       return "DW_TAG_function_template";
5551     case DW_TAG_class_template:
5552       return "DW_TAG_class_template";
5553     case DW_TAG_GNU_BINCL:
5554       return "DW_TAG_GNU_BINCL";
5555     case DW_TAG_GNU_EINCL:
5556       return "DW_TAG_GNU_EINCL";
5557     default:
5558       return "DW_TAG_<unknown>";
5559     }
5560 }
5561
5562 /* Convert a DWARF attribute code into its string name.  */
5563
5564 static const char *
5565 dwarf_attr_name (unsigned int attr)
5566 {
5567   switch (attr)
5568     {
5569     case DW_AT_sibling:
5570       return "DW_AT_sibling";
5571     case DW_AT_location:
5572       return "DW_AT_location";
5573     case DW_AT_name:
5574       return "DW_AT_name";
5575     case DW_AT_ordering:
5576       return "DW_AT_ordering";
5577     case DW_AT_subscr_data:
5578       return "DW_AT_subscr_data";
5579     case DW_AT_byte_size:
5580       return "DW_AT_byte_size";
5581     case DW_AT_bit_offset:
5582       return "DW_AT_bit_offset";
5583     case DW_AT_bit_size:
5584       return "DW_AT_bit_size";
5585     case DW_AT_element_list:
5586       return "DW_AT_element_list";
5587     case DW_AT_stmt_list:
5588       return "DW_AT_stmt_list";
5589     case DW_AT_low_pc:
5590       return "DW_AT_low_pc";
5591     case DW_AT_high_pc:
5592       return "DW_AT_high_pc";
5593     case DW_AT_language:
5594       return "DW_AT_language";
5595     case DW_AT_member:
5596       return "DW_AT_member";
5597     case DW_AT_discr:
5598       return "DW_AT_discr";
5599     case DW_AT_discr_value:
5600       return "DW_AT_discr_value";
5601     case DW_AT_visibility:
5602       return "DW_AT_visibility";
5603     case DW_AT_import:
5604       return "DW_AT_import";
5605     case DW_AT_string_length:
5606       return "DW_AT_string_length";
5607     case DW_AT_common_reference:
5608       return "DW_AT_common_reference";
5609     case DW_AT_comp_dir:
5610       return "DW_AT_comp_dir";
5611     case DW_AT_const_value:
5612       return "DW_AT_const_value";
5613     case DW_AT_containing_type:
5614       return "DW_AT_containing_type";
5615     case DW_AT_default_value:
5616       return "DW_AT_default_value";
5617     case DW_AT_inline:
5618       return "DW_AT_inline";
5619     case DW_AT_is_optional:
5620       return "DW_AT_is_optional";
5621     case DW_AT_lower_bound:
5622       return "DW_AT_lower_bound";
5623     case DW_AT_producer:
5624       return "DW_AT_producer";
5625     case DW_AT_prototyped:
5626       return "DW_AT_prototyped";
5627     case DW_AT_return_addr:
5628       return "DW_AT_return_addr";
5629     case DW_AT_start_scope:
5630       return "DW_AT_start_scope";
5631     case DW_AT_bit_stride:
5632       return "DW_AT_bit_stride";
5633     case DW_AT_upper_bound:
5634       return "DW_AT_upper_bound";
5635     case DW_AT_abstract_origin:
5636       return "DW_AT_abstract_origin";
5637     case DW_AT_accessibility:
5638       return "DW_AT_accessibility";
5639     case DW_AT_address_class:
5640       return "DW_AT_address_class";
5641     case DW_AT_artificial:
5642       return "DW_AT_artificial";
5643     case DW_AT_base_types:
5644       return "DW_AT_base_types";
5645     case DW_AT_calling_convention:
5646       return "DW_AT_calling_convention";
5647     case DW_AT_count:
5648       return "DW_AT_count";
5649     case DW_AT_data_member_location:
5650       return "DW_AT_data_member_location";
5651     case DW_AT_decl_column:
5652       return "DW_AT_decl_column";
5653     case DW_AT_decl_file:
5654       return "DW_AT_decl_file";
5655     case DW_AT_decl_line:
5656       return "DW_AT_decl_line";
5657     case DW_AT_declaration:
5658       return "DW_AT_declaration";
5659     case DW_AT_discr_list:
5660       return "DW_AT_discr_list";
5661     case DW_AT_encoding:
5662       return "DW_AT_encoding";
5663     case DW_AT_external:
5664       return "DW_AT_external";
5665     case DW_AT_frame_base:
5666       return "DW_AT_frame_base";
5667     case DW_AT_friend:
5668       return "DW_AT_friend";
5669     case DW_AT_identifier_case:
5670       return "DW_AT_identifier_case";
5671     case DW_AT_macro_info:
5672       return "DW_AT_macro_info";
5673     case DW_AT_namelist_items:
5674       return "DW_AT_namelist_items";
5675     case DW_AT_priority:
5676       return "DW_AT_priority";
5677     case DW_AT_segment:
5678       return "DW_AT_segment";
5679     case DW_AT_specification:
5680       return "DW_AT_specification";
5681     case DW_AT_static_link:
5682       return "DW_AT_static_link";
5683     case DW_AT_type:
5684       return "DW_AT_type";
5685     case DW_AT_use_location:
5686       return "DW_AT_use_location";
5687     case DW_AT_variable_parameter:
5688       return "DW_AT_variable_parameter";
5689     case DW_AT_virtuality:
5690       return "DW_AT_virtuality";
5691     case DW_AT_vtable_elem_location:
5692       return "DW_AT_vtable_elem_location";
5693
5694     case DW_AT_allocated:
5695       return "DW_AT_allocated";
5696     case DW_AT_associated:
5697       return "DW_AT_associated";
5698     case DW_AT_data_location:
5699       return "DW_AT_data_location";
5700     case DW_AT_byte_stride:
5701       return "DW_AT_byte_stride";
5702     case DW_AT_entry_pc:
5703       return "DW_AT_entry_pc";
5704     case DW_AT_use_UTF8:
5705       return "DW_AT_use_UTF8";
5706     case DW_AT_extension:
5707       return "DW_AT_extension";
5708     case DW_AT_ranges:
5709       return "DW_AT_ranges";
5710     case DW_AT_trampoline:
5711       return "DW_AT_trampoline";
5712     case DW_AT_call_column:
5713       return "DW_AT_call_column";
5714     case DW_AT_call_file:
5715       return "DW_AT_call_file";
5716     case DW_AT_call_line:
5717       return "DW_AT_call_line";
5718
5719     case DW_AT_MIPS_fde:
5720       return "DW_AT_MIPS_fde";
5721     case DW_AT_MIPS_loop_begin:
5722       return "DW_AT_MIPS_loop_begin";
5723     case DW_AT_MIPS_tail_loop_begin:
5724       return "DW_AT_MIPS_tail_loop_begin";
5725     case DW_AT_MIPS_epilog_begin:
5726       return "DW_AT_MIPS_epilog_begin";
5727     case DW_AT_MIPS_loop_unroll_factor:
5728       return "DW_AT_MIPS_loop_unroll_factor";
5729     case DW_AT_MIPS_software_pipeline_depth:
5730       return "DW_AT_MIPS_software_pipeline_depth";
5731     case DW_AT_MIPS_linkage_name:
5732       return "DW_AT_MIPS_linkage_name";
5733     case DW_AT_MIPS_stride:
5734       return "DW_AT_MIPS_stride";
5735     case DW_AT_MIPS_abstract_name:
5736       return "DW_AT_MIPS_abstract_name";
5737     case DW_AT_MIPS_clone_origin:
5738       return "DW_AT_MIPS_clone_origin";
5739     case DW_AT_MIPS_has_inlines:
5740       return "DW_AT_MIPS_has_inlines";
5741
5742     case DW_AT_sf_names:
5743       return "DW_AT_sf_names";
5744     case DW_AT_src_info:
5745       return "DW_AT_src_info";
5746     case DW_AT_mac_info:
5747       return "DW_AT_mac_info";
5748     case DW_AT_src_coords:
5749       return "DW_AT_src_coords";
5750     case DW_AT_body_begin:
5751       return "DW_AT_body_begin";
5752     case DW_AT_body_end:
5753       return "DW_AT_body_end";
5754     case DW_AT_GNU_vector:
5755       return "DW_AT_GNU_vector";
5756
5757     case DW_AT_VMS_rtnbeg_pd_address:
5758       return "DW_AT_VMS_rtnbeg_pd_address";
5759
5760     default:
5761       return "DW_AT_<unknown>";
5762     }
5763 }
5764
5765 /* Convert a DWARF value form code into its string name.  */
5766
5767 static const char *
5768 dwarf_form_name (unsigned int form)
5769 {
5770   switch (form)
5771     {
5772     case DW_FORM_addr:
5773       return "DW_FORM_addr";
5774     case DW_FORM_block2:
5775       return "DW_FORM_block2";
5776     case DW_FORM_block4:
5777       return "DW_FORM_block4";
5778     case DW_FORM_data2:
5779       return "DW_FORM_data2";
5780     case DW_FORM_data4:
5781       return "DW_FORM_data4";
5782     case DW_FORM_data8:
5783       return "DW_FORM_data8";
5784     case DW_FORM_string:
5785       return "DW_FORM_string";
5786     case DW_FORM_block:
5787       return "DW_FORM_block";
5788     case DW_FORM_block1:
5789       return "DW_FORM_block1";
5790     case DW_FORM_data1:
5791       return "DW_FORM_data1";
5792     case DW_FORM_flag:
5793       return "DW_FORM_flag";
5794     case DW_FORM_sdata:
5795       return "DW_FORM_sdata";
5796     case DW_FORM_strp:
5797       return "DW_FORM_strp";
5798     case DW_FORM_udata:
5799       return "DW_FORM_udata";
5800     case DW_FORM_ref_addr:
5801       return "DW_FORM_ref_addr";
5802     case DW_FORM_ref1:
5803       return "DW_FORM_ref1";
5804     case DW_FORM_ref2:
5805       return "DW_FORM_ref2";
5806     case DW_FORM_ref4:
5807       return "DW_FORM_ref4";
5808     case DW_FORM_ref8:
5809       return "DW_FORM_ref8";
5810     case DW_FORM_ref_udata:
5811       return "DW_FORM_ref_udata";
5812     case DW_FORM_indirect:
5813       return "DW_FORM_indirect";
5814     default:
5815       return "DW_FORM_<unknown>";
5816     }
5817 }
5818 \f
5819 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5820    instance of an inlined instance of a decl which is local to an inline
5821    function, so we have to trace all of the way back through the origin chain
5822    to find out what sort of node actually served as the original seed for the
5823    given block.  */
5824
5825 static tree
5826 decl_ultimate_origin (const_tree decl)
5827 {
5828   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5829     return NULL_TREE;
5830
5831   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5832      nodes in the function to point to themselves; ignore that if
5833      we're trying to output the abstract instance of this function.  */
5834   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5835     return NULL_TREE;
5836
5837   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5838      most distant ancestor, this should never happen.  */
5839   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5840
5841   return DECL_ABSTRACT_ORIGIN (decl);
5842 }
5843
5844 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5845    of a virtual function may refer to a base class, so we check the 'this'
5846    parameter.  */
5847
5848 static tree
5849 decl_class_context (tree decl)
5850 {
5851   tree context = NULL_TREE;
5852
5853   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5854     context = DECL_CONTEXT (decl);
5855   else
5856     context = TYPE_MAIN_VARIANT
5857       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5858
5859   if (context && !TYPE_P (context))
5860     context = NULL_TREE;
5861
5862   return context;
5863 }
5864 \f
5865 /* Add an attribute/value pair to a DIE.  */
5866
5867 static inline void
5868 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5869 {
5870   /* Maybe this should be an assert?  */
5871   if (die == NULL)
5872     return;
5873
5874   if (die->die_attr == NULL)
5875     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5876   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5877 }
5878
5879 static inline enum dw_val_class
5880 AT_class (dw_attr_ref a)
5881 {
5882   return a->dw_attr_val.val_class;
5883 }
5884
5885 /* Add a flag value attribute to a DIE.  */
5886
5887 static inline void
5888 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5889 {
5890   dw_attr_node attr;
5891
5892   attr.dw_attr = attr_kind;
5893   attr.dw_attr_val.val_class = dw_val_class_flag;
5894   attr.dw_attr_val.v.val_flag = flag;
5895   add_dwarf_attr (die, &attr);
5896 }
5897
5898 static inline unsigned
5899 AT_flag (dw_attr_ref a)
5900 {
5901   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5902   return a->dw_attr_val.v.val_flag;
5903 }
5904
5905 /* Add a signed integer attribute value to a DIE.  */
5906
5907 static inline void
5908 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5909 {
5910   dw_attr_node attr;
5911
5912   attr.dw_attr = attr_kind;
5913   attr.dw_attr_val.val_class = dw_val_class_const;
5914   attr.dw_attr_val.v.val_int = int_val;
5915   add_dwarf_attr (die, &attr);
5916 }
5917
5918 static inline HOST_WIDE_INT
5919 AT_int (dw_attr_ref a)
5920 {
5921   gcc_assert (a && AT_class (a) == dw_val_class_const);
5922   return a->dw_attr_val.v.val_int;
5923 }
5924
5925 /* Add an unsigned integer attribute value to a DIE.  */
5926
5927 static inline void
5928 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5929                  unsigned HOST_WIDE_INT unsigned_val)
5930 {
5931   dw_attr_node attr;
5932
5933   attr.dw_attr = attr_kind;
5934   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5935   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5936   add_dwarf_attr (die, &attr);
5937 }
5938
5939 static inline unsigned HOST_WIDE_INT
5940 AT_unsigned (dw_attr_ref a)
5941 {
5942   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5943   return a->dw_attr_val.v.val_unsigned;
5944 }
5945
5946 /* Add an unsigned double integer attribute value to a DIE.  */
5947
5948 static inline void
5949 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5950                   long unsigned int val_hi, long unsigned int val_low)
5951 {
5952   dw_attr_node attr;
5953
5954   attr.dw_attr = attr_kind;
5955   attr.dw_attr_val.val_class = dw_val_class_long_long;
5956   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5957   attr.dw_attr_val.v.val_long_long.low = val_low;
5958   add_dwarf_attr (die, &attr);
5959 }
5960
5961 /* Add a floating point attribute value to a DIE and return it.  */
5962
5963 static inline void
5964 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5965             unsigned int length, unsigned int elt_size, unsigned char *array)
5966 {
5967   dw_attr_node attr;
5968
5969   attr.dw_attr = attr_kind;
5970   attr.dw_attr_val.val_class = dw_val_class_vec;
5971   attr.dw_attr_val.v.val_vec.length = length;
5972   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5973   attr.dw_attr_val.v.val_vec.array = array;
5974   add_dwarf_attr (die, &attr);
5975 }
5976
5977 /* Hash and equality functions for debug_str_hash.  */
5978
5979 static hashval_t
5980 debug_str_do_hash (const void *x)
5981 {
5982   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5983 }
5984
5985 static int
5986 debug_str_eq (const void *x1, const void *x2)
5987 {
5988   return strcmp ((((const struct indirect_string_node *)x1)->str),
5989                  (const char *)x2) == 0;
5990 }
5991
5992 /* Add a string attribute value to a DIE.  */
5993
5994 static inline void
5995 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5996 {
5997   dw_attr_node attr;
5998   struct indirect_string_node *node;
5999   void **slot;
6000
6001   if (! debug_str_hash)
6002     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6003                                       debug_str_eq, NULL);
6004
6005   slot = htab_find_slot_with_hash (debug_str_hash, str,
6006                                    htab_hash_string (str), INSERT);
6007   if (*slot == NULL)
6008     {
6009       node = (struct indirect_string_node *)
6010                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6011       node->str = ggc_strdup (str);
6012       *slot = node;
6013     }
6014   else
6015     node = (struct indirect_string_node *) *slot;
6016
6017   node->refcount++;
6018
6019   attr.dw_attr = attr_kind;
6020   attr.dw_attr_val.val_class = dw_val_class_str;
6021   attr.dw_attr_val.v.val_str = node;
6022   add_dwarf_attr (die, &attr);
6023 }
6024
6025 static inline const char *
6026 AT_string (dw_attr_ref a)
6027 {
6028   gcc_assert (a && AT_class (a) == dw_val_class_str);
6029   return a->dw_attr_val.v.val_str->str;
6030 }
6031
6032 /* Find out whether a string should be output inline in DIE
6033    or out-of-line in .debug_str section.  */
6034
6035 static int
6036 AT_string_form (dw_attr_ref a)
6037 {
6038   struct indirect_string_node *node;
6039   unsigned int len;
6040   char label[32];
6041
6042   gcc_assert (a && AT_class (a) == dw_val_class_str);
6043
6044   node = a->dw_attr_val.v.val_str;
6045   if (node->form)
6046     return node->form;
6047
6048   len = strlen (node->str) + 1;
6049
6050   /* If the string is shorter or equal to the size of the reference, it is
6051      always better to put it inline.  */
6052   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6053     return node->form = DW_FORM_string;
6054
6055   /* If we cannot expect the linker to merge strings in .debug_str
6056      section, only put it into .debug_str if it is worth even in this
6057      single module.  */
6058   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6059       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6060     return node->form = DW_FORM_string;
6061
6062   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6063   ++dw2_string_counter;
6064   node->label = xstrdup (label);
6065
6066   return node->form = DW_FORM_strp;
6067 }
6068
6069 /* Add a DIE reference attribute value to a DIE.  */
6070
6071 static inline void
6072 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6073 {
6074   dw_attr_node attr;
6075
6076   attr.dw_attr = attr_kind;
6077   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6078   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6079   attr.dw_attr_val.v.val_die_ref.external = 0;
6080   add_dwarf_attr (die, &attr);
6081 }
6082
6083 /* Add an AT_specification attribute to a DIE, and also make the back
6084    pointer from the specification to the definition.  */
6085
6086 static inline void
6087 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6088 {
6089   add_AT_die_ref (die, DW_AT_specification, targ_die);
6090   gcc_assert (!targ_die->die_definition);
6091   targ_die->die_definition = die;
6092 }
6093
6094 static inline dw_die_ref
6095 AT_ref (dw_attr_ref a)
6096 {
6097   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6098   return a->dw_attr_val.v.val_die_ref.die;
6099 }
6100
6101 static inline int
6102 AT_ref_external (dw_attr_ref a)
6103 {
6104   if (a && AT_class (a) == dw_val_class_die_ref)
6105     return a->dw_attr_val.v.val_die_ref.external;
6106
6107   return 0;
6108 }
6109
6110 static inline void
6111 set_AT_ref_external (dw_attr_ref a, int i)
6112 {
6113   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6114   a->dw_attr_val.v.val_die_ref.external = i;
6115 }
6116
6117 /* Add an FDE reference attribute value to a DIE.  */
6118
6119 static inline void
6120 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6121 {
6122   dw_attr_node attr;
6123
6124   attr.dw_attr = attr_kind;
6125   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6126   attr.dw_attr_val.v.val_fde_index = targ_fde;
6127   add_dwarf_attr (die, &attr);
6128 }
6129
6130 /* Add a location description attribute value to a DIE.  */
6131
6132 static inline void
6133 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6134 {
6135   dw_attr_node attr;
6136
6137   attr.dw_attr = attr_kind;
6138   attr.dw_attr_val.val_class = dw_val_class_loc;
6139   attr.dw_attr_val.v.val_loc = loc;
6140   add_dwarf_attr (die, &attr);
6141 }
6142
6143 static inline dw_loc_descr_ref
6144 AT_loc (dw_attr_ref a)
6145 {
6146   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6147   return a->dw_attr_val.v.val_loc;
6148 }
6149
6150 static inline void
6151 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6152 {
6153   dw_attr_node attr;
6154
6155   attr.dw_attr = attr_kind;
6156   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6157   attr.dw_attr_val.v.val_loc_list = loc_list;
6158   add_dwarf_attr (die, &attr);
6159   have_location_lists = true;
6160 }
6161
6162 static inline dw_loc_list_ref
6163 AT_loc_list (dw_attr_ref a)
6164 {
6165   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6166   return a->dw_attr_val.v.val_loc_list;
6167 }
6168
6169 /* Add an address constant attribute value to a DIE.  */
6170
6171 static inline void
6172 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6173 {
6174   dw_attr_node attr;
6175
6176   attr.dw_attr = attr_kind;
6177   attr.dw_attr_val.val_class = dw_val_class_addr;
6178   attr.dw_attr_val.v.val_addr = addr;
6179   add_dwarf_attr (die, &attr);
6180 }
6181
6182 /* Get the RTX from to an address DIE attribute.  */
6183
6184 static inline rtx
6185 AT_addr (dw_attr_ref a)
6186 {
6187   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6188   return a->dw_attr_val.v.val_addr;
6189 }
6190
6191 /* Add a file attribute value to a DIE.  */
6192
6193 static inline void
6194 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6195              struct dwarf_file_data *fd)
6196 {
6197   dw_attr_node attr;
6198
6199   attr.dw_attr = attr_kind;
6200   attr.dw_attr_val.val_class = dw_val_class_file;
6201   attr.dw_attr_val.v.val_file = fd;
6202   add_dwarf_attr (die, &attr);
6203 }
6204
6205 /* Get the dwarf_file_data from a file DIE attribute.  */
6206
6207 static inline struct dwarf_file_data *
6208 AT_file (dw_attr_ref a)
6209 {
6210   gcc_assert (a && AT_class (a) == dw_val_class_file);
6211   return a->dw_attr_val.v.val_file;
6212 }
6213
6214 /* Add a label identifier attribute value to a DIE.  */
6215
6216 static inline void
6217 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6218 {
6219   dw_attr_node attr;
6220
6221   attr.dw_attr = attr_kind;
6222   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6223   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6224   add_dwarf_attr (die, &attr);
6225 }
6226
6227 /* Add a section offset attribute value to a DIE, an offset into the
6228    debug_line section.  */
6229
6230 static inline void
6231 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6232                 const char *label)
6233 {
6234   dw_attr_node attr;
6235
6236   attr.dw_attr = attr_kind;
6237   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6238   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6239   add_dwarf_attr (die, &attr);
6240 }
6241
6242 /* Add a section offset attribute value to a DIE, an offset into the
6243    debug_macinfo section.  */
6244
6245 static inline void
6246 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6247                const char *label)
6248 {
6249   dw_attr_node attr;
6250
6251   attr.dw_attr = attr_kind;
6252   attr.dw_attr_val.val_class = dw_val_class_macptr;
6253   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6254   add_dwarf_attr (die, &attr);
6255 }
6256
6257 /* Add an offset attribute value to a DIE.  */
6258
6259 static inline void
6260 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6261                unsigned HOST_WIDE_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_offset;
6267   attr.dw_attr_val.v.val_offset = offset;
6268   add_dwarf_attr (die, &attr);
6269 }
6270
6271 /* Add an range_list attribute value to a DIE.  */
6272
6273 static void
6274 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6275                    long unsigned int offset)
6276 {
6277   dw_attr_node attr;
6278
6279   attr.dw_attr = attr_kind;
6280   attr.dw_attr_val.val_class = dw_val_class_range_list;
6281   attr.dw_attr_val.v.val_offset = offset;
6282   add_dwarf_attr (die, &attr);
6283 }
6284
6285 static inline const char *
6286 AT_lbl (dw_attr_ref a)
6287 {
6288   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6289                     || AT_class (a) == dw_val_class_lineptr
6290                     || AT_class (a) == dw_val_class_macptr));
6291   return a->dw_attr_val.v.val_lbl_id;
6292 }
6293
6294 /* Get the attribute of type attr_kind.  */
6295
6296 static dw_attr_ref
6297 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6298 {
6299   dw_attr_ref a;
6300   unsigned ix;
6301   dw_die_ref spec = NULL;
6302
6303   if (! die)
6304     return NULL;
6305
6306   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6307     if (a->dw_attr == attr_kind)
6308       return a;
6309     else if (a->dw_attr == DW_AT_specification
6310              || a->dw_attr == DW_AT_abstract_origin)
6311       spec = AT_ref (a);
6312
6313   if (spec)
6314     return get_AT (spec, attr_kind);
6315
6316   return NULL;
6317 }
6318
6319 /* Return the "low pc" attribute value, typically associated with a subprogram
6320    DIE.  Return null if the "low pc" attribute is either not present, or if it
6321    cannot be represented as an assembler label identifier.  */
6322
6323 static inline const char *
6324 get_AT_low_pc (dw_die_ref die)
6325 {
6326   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6327
6328   return a ? AT_lbl (a) : NULL;
6329 }
6330
6331 /* Return the "high pc" attribute value, typically associated with a subprogram
6332    DIE.  Return null if the "high pc" attribute is either not present, or if it
6333    cannot be represented as an assembler label identifier.  */
6334
6335 static inline const char *
6336 get_AT_hi_pc (dw_die_ref die)
6337 {
6338   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6339
6340   return a ? AT_lbl (a) : NULL;
6341 }
6342
6343 /* Return the value of the string attribute designated by ATTR_KIND, or
6344    NULL if it is not present.  */
6345
6346 static inline const char *
6347 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6348 {
6349   dw_attr_ref a = get_AT (die, attr_kind);
6350
6351   return a ? AT_string (a) : NULL;
6352 }
6353
6354 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6355    if it is not present.  */
6356
6357 static inline int
6358 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6359 {
6360   dw_attr_ref a = get_AT (die, attr_kind);
6361
6362   return a ? AT_flag (a) : 0;
6363 }
6364
6365 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6366    if it is not present.  */
6367
6368 static inline unsigned
6369 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6370 {
6371   dw_attr_ref a = get_AT (die, attr_kind);
6372
6373   return a ? AT_unsigned (a) : 0;
6374 }
6375
6376 static inline dw_die_ref
6377 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6378 {
6379   dw_attr_ref a = get_AT (die, attr_kind);
6380
6381   return a ? AT_ref (a) : NULL;
6382 }
6383
6384 static inline struct dwarf_file_data *
6385 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6386 {
6387   dw_attr_ref a = get_AT (die, attr_kind);
6388
6389   return a ? AT_file (a) : NULL;
6390 }
6391
6392 /* Return TRUE if the language is C or C++.  */
6393
6394 static inline bool
6395 is_c_family (void)
6396 {
6397   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6398
6399   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6400           || lang == DW_LANG_C99
6401           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6402 }
6403
6404 /* Return TRUE if the language is C++.  */
6405
6406 static inline bool
6407 is_cxx (void)
6408 {
6409   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6410
6411   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6412 }
6413
6414 /* Return TRUE if the language is Fortran.  */
6415
6416 static inline bool
6417 is_fortran (void)
6418 {
6419   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6420
6421   return (lang == DW_LANG_Fortran77
6422           || lang == DW_LANG_Fortran90
6423           || lang == DW_LANG_Fortran95);
6424 }
6425
6426 /* Return TRUE if the language is Java.  */
6427
6428 static inline bool
6429 is_java (void)
6430 {
6431   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6432
6433   return lang == DW_LANG_Java;
6434 }
6435
6436 /* Return TRUE if the language is Ada.  */
6437
6438 static inline bool
6439 is_ada (void)
6440 {
6441   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6442
6443   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6444 }
6445
6446 /* Remove the specified attribute if present.  */
6447
6448 static void
6449 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6450 {
6451   dw_attr_ref a;
6452   unsigned ix;
6453
6454   if (! die)
6455     return;
6456
6457   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6458     if (a->dw_attr == attr_kind)
6459       {
6460         if (AT_class (a) == dw_val_class_str)
6461           if (a->dw_attr_val.v.val_str->refcount)
6462             a->dw_attr_val.v.val_str->refcount--;
6463
6464         /* VEC_ordered_remove should help reduce the number of abbrevs
6465            that are needed.  */
6466         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6467         return;
6468       }
6469 }
6470
6471 /* Remove CHILD from its parent.  PREV must have the property that
6472    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6473
6474 static void
6475 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6476 {
6477   gcc_assert (child->die_parent == prev->die_parent);
6478   gcc_assert (prev->die_sib == child);
6479   if (prev == child)
6480     {
6481       gcc_assert (child->die_parent->die_child == child);
6482       prev = NULL;
6483     }
6484   else
6485     prev->die_sib = child->die_sib;
6486   if (child->die_parent->die_child == child)
6487     child->die_parent->die_child = prev;
6488 }
6489
6490 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6491    matches TAG.  */
6492
6493 static void
6494 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6495 {
6496   dw_die_ref c;
6497
6498   c = die->die_child;
6499   if (c) do {
6500     dw_die_ref prev = c;
6501     c = c->die_sib;
6502     while (c->die_tag == tag)
6503       {
6504         remove_child_with_prev (c, prev);
6505         /* Might have removed every child.  */
6506         if (c == c->die_sib)
6507           return;
6508         c = c->die_sib;
6509       }
6510   } while (c != die->die_child);
6511 }
6512
6513 /* Add a CHILD_DIE as the last child of DIE.  */
6514
6515 static void
6516 add_child_die (dw_die_ref die, dw_die_ref child_die)
6517 {
6518   /* FIXME this should probably be an assert.  */
6519   if (! die || ! child_die)
6520     return;
6521   gcc_assert (die != child_die);
6522
6523   child_die->die_parent = die;
6524   if (die->die_child)
6525     {
6526       child_die->die_sib = die->die_child->die_sib;
6527       die->die_child->die_sib = child_die;
6528     }
6529   else
6530     child_die->die_sib = child_die;
6531   die->die_child = child_die;
6532 }
6533
6534 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6535    is the specification, to the end of PARENT's list of children.
6536    This is done by removing and re-adding it.  */
6537
6538 static void
6539 splice_child_die (dw_die_ref parent, dw_die_ref child)
6540 {
6541   dw_die_ref p;
6542
6543   /* We want the declaration DIE from inside the class, not the
6544      specification DIE at toplevel.  */
6545   if (child->die_parent != parent)
6546     {
6547       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6548
6549       if (tmp)
6550         child = tmp;
6551     }
6552
6553   gcc_assert (child->die_parent == parent
6554               || (child->die_parent
6555                   == get_AT_ref (parent, DW_AT_specification)));
6556
6557   for (p = child->die_parent->die_child; ; p = p->die_sib)
6558     if (p->die_sib == child)
6559       {
6560         remove_child_with_prev (child, p);
6561         break;
6562       }
6563
6564   add_child_die (parent, child);
6565 }
6566
6567 /* Return a pointer to a newly created DIE node.  */
6568
6569 static inline dw_die_ref
6570 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6571 {
6572   dw_die_ref die = GGC_CNEW (die_node);
6573
6574   die->die_tag = tag_value;
6575
6576   if (parent_die != NULL)
6577     add_child_die (parent_die, die);
6578   else
6579     {
6580       limbo_die_node *limbo_node;
6581
6582       limbo_node = GGC_CNEW (limbo_die_node);
6583       limbo_node->die = die;
6584       limbo_node->created_for = t;
6585       limbo_node->next = limbo_die_list;
6586       limbo_die_list = limbo_node;
6587     }
6588
6589   return die;
6590 }
6591
6592 /* Return the DIE associated with the given type specifier.  */
6593
6594 static inline dw_die_ref
6595 lookup_type_die (tree type)
6596 {
6597   return TYPE_SYMTAB_DIE (type);
6598 }
6599
6600 /* Equate a DIE to a given type specifier.  */
6601
6602 static inline void
6603 equate_type_number_to_die (tree type, dw_die_ref type_die)
6604 {
6605   TYPE_SYMTAB_DIE (type) = type_die;
6606 }
6607
6608 /* Returns a hash value for X (which really is a die_struct).  */
6609
6610 static hashval_t
6611 decl_die_table_hash (const void *x)
6612 {
6613   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6614 }
6615
6616 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6617
6618 static int
6619 decl_die_table_eq (const void *x, const void *y)
6620 {
6621   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6622 }
6623
6624 /* Return the DIE associated with a given declaration.  */
6625
6626 static inline dw_die_ref
6627 lookup_decl_die (tree decl)
6628 {
6629   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6630 }
6631
6632 /* Returns a hash value for X (which really is a var_loc_list).  */
6633
6634 static hashval_t
6635 decl_loc_table_hash (const void *x)
6636 {
6637   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6638 }
6639
6640 /* Return nonzero if decl_id of var_loc_list X is the same as
6641    UID of decl *Y.  */
6642
6643 static int
6644 decl_loc_table_eq (const void *x, const void *y)
6645 {
6646   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6647 }
6648
6649 /* Return the var_loc list associated with a given declaration.  */
6650
6651 static inline var_loc_list *
6652 lookup_decl_loc (const_tree decl)
6653 {
6654   return (var_loc_list *)
6655     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6656 }
6657
6658 /* Equate a DIE to a particular declaration.  */
6659
6660 static void
6661 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6662 {
6663   unsigned int decl_id = DECL_UID (decl);
6664   void **slot;
6665
6666   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6667   *slot = decl_die;
6668   decl_die->decl_id = decl_id;
6669 }
6670
6671 /* Add a variable location node to the linked list for DECL.  */
6672
6673 static void
6674 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6675 {
6676   unsigned int decl_id = DECL_UID (decl);
6677   var_loc_list *temp;
6678   void **slot;
6679
6680   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6681   if (*slot == NULL)
6682     {
6683       temp = GGC_CNEW (var_loc_list);
6684       temp->decl_id = decl_id;
6685       *slot = temp;
6686     }
6687   else
6688     temp = (var_loc_list *) *slot;
6689
6690   if (temp->last)
6691     {
6692       /* If the current location is the same as the end of the list,
6693          and either both or neither of the locations is uninitialized,
6694          we have nothing to do.  */
6695       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6696                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6697           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6698                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6699               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6700                    == VAR_INIT_STATUS_UNINITIALIZED)
6701                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6702                       == VAR_INIT_STATUS_UNINITIALIZED))))
6703         {
6704           /* Add LOC to the end of list and update LAST.  */
6705           temp->last->next = loc;
6706           temp->last = loc;
6707         }
6708     }
6709   /* Do not add empty location to the beginning of the list.  */
6710   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6711     {
6712       temp->first = loc;
6713       temp->last = loc;
6714     }
6715 }
6716 \f
6717 /* Keep track of the number of spaces used to indent the
6718    output of the debugging routines that print the structure of
6719    the DIE internal representation.  */
6720 static int print_indent;
6721
6722 /* Indent the line the number of spaces given by print_indent.  */
6723
6724 static inline void
6725 print_spaces (FILE *outfile)
6726 {
6727   fprintf (outfile, "%*s", print_indent, "");
6728 }
6729
6730 /* Print the information associated with a given DIE, and its children.
6731    This routine is a debugging aid only.  */
6732
6733 static void
6734 print_die (dw_die_ref die, FILE *outfile)
6735 {
6736   dw_attr_ref a;
6737   dw_die_ref c;
6738   unsigned ix;
6739
6740   print_spaces (outfile);
6741   fprintf (outfile, "DIE %4ld: %s\n",
6742            die->die_offset, dwarf_tag_name (die->die_tag));
6743   print_spaces (outfile);
6744   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6745   fprintf (outfile, " offset: %ld\n", die->die_offset);
6746
6747   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6748     {
6749       print_spaces (outfile);
6750       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6751
6752       switch (AT_class (a))
6753         {
6754         case dw_val_class_addr:
6755           fprintf (outfile, "address");
6756           break;
6757         case dw_val_class_offset:
6758           fprintf (outfile, "offset");
6759           break;
6760         case dw_val_class_loc:
6761           fprintf (outfile, "location descriptor");
6762           break;
6763         case dw_val_class_loc_list:
6764           fprintf (outfile, "location list -> label:%s",
6765                    AT_loc_list (a)->ll_symbol);
6766           break;
6767         case dw_val_class_range_list:
6768           fprintf (outfile, "range list");
6769           break;
6770         case dw_val_class_const:
6771           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6772           break;
6773         case dw_val_class_unsigned_const:
6774           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6775           break;
6776         case dw_val_class_long_long:
6777           fprintf (outfile, "constant (%lu,%lu)",
6778                    a->dw_attr_val.v.val_long_long.hi,
6779                    a->dw_attr_val.v.val_long_long.low);
6780           break;
6781         case dw_val_class_vec:
6782           fprintf (outfile, "floating-point or vector constant");
6783           break;
6784         case dw_val_class_flag:
6785           fprintf (outfile, "%u", AT_flag (a));
6786           break;
6787         case dw_val_class_die_ref:
6788           if (AT_ref (a) != NULL)
6789             {
6790               if (AT_ref (a)->die_symbol)
6791                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6792               else
6793                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6794             }
6795           else
6796             fprintf (outfile, "die -> <null>");
6797           break;
6798         case dw_val_class_lbl_id:
6799         case dw_val_class_lineptr:
6800         case dw_val_class_macptr:
6801           fprintf (outfile, "label: %s", AT_lbl (a));
6802           break;
6803         case dw_val_class_str:
6804           if (AT_string (a) != NULL)
6805             fprintf (outfile, "\"%s\"", AT_string (a));
6806           else
6807             fprintf (outfile, "<null>");
6808           break;
6809         case dw_val_class_file:
6810           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6811                    AT_file (a)->emitted_number);
6812           break;
6813         default:
6814           break;
6815         }
6816
6817       fprintf (outfile, "\n");
6818     }
6819
6820   if (die->die_child != NULL)
6821     {
6822       print_indent += 4;
6823       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6824       print_indent -= 4;
6825     }
6826   if (print_indent == 0)
6827     fprintf (outfile, "\n");
6828 }
6829
6830 /* Print the contents of the source code line number correspondence table.
6831    This routine is a debugging aid only.  */
6832
6833 static void
6834 print_dwarf_line_table (FILE *outfile)
6835 {
6836   unsigned i;
6837   dw_line_info_ref line_info;
6838
6839   fprintf (outfile, "\n\nDWARF source line information\n");
6840   for (i = 1; i < line_info_table_in_use; i++)
6841     {
6842       line_info = &line_info_table[i];
6843       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6844                line_info->dw_file_num,
6845                line_info->dw_line_num);
6846     }
6847
6848   fprintf (outfile, "\n\n");
6849 }
6850
6851 /* Print the information collected for a given DIE.  */
6852
6853 void
6854 debug_dwarf_die (dw_die_ref die)
6855 {
6856   print_die (die, stderr);
6857 }
6858
6859 /* Print all DWARF information collected for the compilation unit.
6860    This routine is a debugging aid only.  */
6861
6862 void
6863 debug_dwarf (void)
6864 {
6865   print_indent = 0;
6866   print_die (comp_unit_die, stderr);
6867   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6868     print_dwarf_line_table (stderr);
6869 }
6870 \f
6871 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6872    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6873    DIE that marks the start of the DIEs for this include file.  */
6874
6875 static dw_die_ref
6876 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6877 {
6878   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6879   dw_die_ref new_unit = gen_compile_unit_die (filename);
6880
6881   new_unit->die_sib = old_unit;
6882   return new_unit;
6883 }
6884
6885 /* Close an include-file CU and reopen the enclosing one.  */
6886
6887 static dw_die_ref
6888 pop_compile_unit (dw_die_ref old_unit)
6889 {
6890   dw_die_ref new_unit = old_unit->die_sib;
6891
6892   old_unit->die_sib = NULL;
6893   return new_unit;
6894 }
6895
6896 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6897 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6898
6899 /* Calculate the checksum of a location expression.  */
6900
6901 static inline void
6902 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6903 {
6904   CHECKSUM (loc->dw_loc_opc);
6905   CHECKSUM (loc->dw_loc_oprnd1);
6906   CHECKSUM (loc->dw_loc_oprnd2);
6907 }
6908
6909 /* Calculate the checksum of an attribute.  */
6910
6911 static void
6912 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6913 {
6914   dw_loc_descr_ref loc;
6915   rtx r;
6916
6917   CHECKSUM (at->dw_attr);
6918
6919   /* We don't care that this was compiled with a different compiler
6920      snapshot; if the output is the same, that's what matters.  */
6921   if (at->dw_attr == DW_AT_producer)
6922     return;
6923
6924   switch (AT_class (at))
6925     {
6926     case dw_val_class_const:
6927       CHECKSUM (at->dw_attr_val.v.val_int);
6928       break;
6929     case dw_val_class_unsigned_const:
6930       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6931       break;
6932     case dw_val_class_long_long:
6933       CHECKSUM (at->dw_attr_val.v.val_long_long);
6934       break;
6935     case dw_val_class_vec:
6936       CHECKSUM (at->dw_attr_val.v.val_vec);
6937       break;
6938     case dw_val_class_flag:
6939       CHECKSUM (at->dw_attr_val.v.val_flag);
6940       break;
6941     case dw_val_class_str:
6942       CHECKSUM_STRING (AT_string (at));
6943       break;
6944
6945     case dw_val_class_addr:
6946       r = AT_addr (at);
6947       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6948       CHECKSUM_STRING (XSTR (r, 0));
6949       break;
6950
6951     case dw_val_class_offset:
6952       CHECKSUM (at->dw_attr_val.v.val_offset);
6953       break;
6954
6955     case dw_val_class_loc:
6956       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6957         loc_checksum (loc, ctx);
6958       break;
6959
6960     case dw_val_class_die_ref:
6961       die_checksum (AT_ref (at), ctx, mark);
6962       break;
6963
6964     case dw_val_class_fde_ref:
6965     case dw_val_class_lbl_id:
6966     case dw_val_class_lineptr:
6967     case dw_val_class_macptr:
6968       break;
6969
6970     case dw_val_class_file:
6971       CHECKSUM_STRING (AT_file (at)->filename);
6972       break;
6973
6974     default:
6975       break;
6976     }
6977 }
6978
6979 /* Calculate the checksum of a DIE.  */
6980
6981 static void
6982 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6983 {
6984   dw_die_ref c;
6985   dw_attr_ref a;
6986   unsigned ix;
6987
6988   /* To avoid infinite recursion.  */
6989   if (die->die_mark)
6990     {
6991       CHECKSUM (die->die_mark);
6992       return;
6993     }
6994   die->die_mark = ++(*mark);
6995
6996   CHECKSUM (die->die_tag);
6997
6998   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6999     attr_checksum (a, ctx, mark);
7000
7001   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7002 }
7003
7004 #undef CHECKSUM
7005 #undef CHECKSUM_STRING
7006
7007 /* Do the location expressions look same?  */
7008 static inline int
7009 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7010 {
7011   return loc1->dw_loc_opc == loc2->dw_loc_opc
7012          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7013          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7014 }
7015
7016 /* Do the values look the same?  */
7017 static int
7018 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7019 {
7020   dw_loc_descr_ref loc1, loc2;
7021   rtx r1, r2;
7022
7023   if (v1->val_class != v2->val_class)
7024     return 0;
7025
7026   switch (v1->val_class)
7027     {
7028     case dw_val_class_const:
7029       return v1->v.val_int == v2->v.val_int;
7030     case dw_val_class_unsigned_const:
7031       return v1->v.val_unsigned == v2->v.val_unsigned;
7032     case dw_val_class_long_long:
7033       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7034              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7035     case dw_val_class_vec:
7036       if (v1->v.val_vec.length != v2->v.val_vec.length
7037           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7038         return 0;
7039       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7040                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7041         return 0;
7042       return 1;
7043     case dw_val_class_flag:
7044       return v1->v.val_flag == v2->v.val_flag;
7045     case dw_val_class_str:
7046       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7047
7048     case dw_val_class_addr:
7049       r1 = v1->v.val_addr;
7050       r2 = v2->v.val_addr;
7051       if (GET_CODE (r1) != GET_CODE (r2))
7052         return 0;
7053       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7054       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7055
7056     case dw_val_class_offset:
7057       return v1->v.val_offset == v2->v.val_offset;
7058
7059     case dw_val_class_loc:
7060       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7061            loc1 && loc2;
7062            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7063         if (!same_loc_p (loc1, loc2, mark))
7064           return 0;
7065       return !loc1 && !loc2;
7066
7067     case dw_val_class_die_ref:
7068       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7069
7070     case dw_val_class_fde_ref:
7071     case dw_val_class_lbl_id:
7072     case dw_val_class_lineptr:
7073     case dw_val_class_macptr:
7074       return 1;
7075
7076     case dw_val_class_file:
7077       return v1->v.val_file == v2->v.val_file;
7078
7079     default:
7080       return 1;
7081     }
7082 }
7083
7084 /* Do the attributes look the same?  */
7085
7086 static int
7087 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7088 {
7089   if (at1->dw_attr != at2->dw_attr)
7090     return 0;
7091
7092   /* We don't care that this was compiled with a different compiler
7093      snapshot; if the output is the same, that's what matters. */
7094   if (at1->dw_attr == DW_AT_producer)
7095     return 1;
7096
7097   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7098 }
7099
7100 /* Do the dies look the same?  */
7101
7102 static int
7103 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7104 {
7105   dw_die_ref c1, c2;
7106   dw_attr_ref a1;
7107   unsigned ix;
7108
7109   /* To avoid infinite recursion.  */
7110   if (die1->die_mark)
7111     return die1->die_mark == die2->die_mark;
7112   die1->die_mark = die2->die_mark = ++(*mark);
7113
7114   if (die1->die_tag != die2->die_tag)
7115     return 0;
7116
7117   if (VEC_length (dw_attr_node, die1->die_attr)
7118       != VEC_length (dw_attr_node, die2->die_attr))
7119     return 0;
7120
7121   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7122     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7123       return 0;
7124
7125   c1 = die1->die_child;
7126   c2 = die2->die_child;
7127   if (! c1)
7128     {
7129       if (c2)
7130         return 0;
7131     }
7132   else
7133     for (;;)
7134       {
7135         if (!same_die_p (c1, c2, mark))
7136           return 0;
7137         c1 = c1->die_sib;
7138         c2 = c2->die_sib;
7139         if (c1 == die1->die_child)
7140           {
7141             if (c2 == die2->die_child)
7142               break;
7143             else
7144               return 0;
7145           }
7146     }
7147
7148   return 1;
7149 }
7150
7151 /* Do the dies look the same?  Wrapper around same_die_p.  */
7152
7153 static int
7154 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7155 {
7156   int mark = 0;
7157   int ret = same_die_p (die1, die2, &mark);
7158
7159   unmark_all_dies (die1);
7160   unmark_all_dies (die2);
7161
7162   return ret;
7163 }
7164
7165 /* The prefix to attach to symbols on DIEs in the current comdat debug
7166    info section.  */
7167 static char *comdat_symbol_id;
7168
7169 /* The index of the current symbol within the current comdat CU.  */
7170 static unsigned int comdat_symbol_number;
7171
7172 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7173    children, and set comdat_symbol_id accordingly.  */
7174
7175 static void
7176 compute_section_prefix (dw_die_ref unit_die)
7177 {
7178   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7179   const char *base = die_name ? lbasename (die_name) : "anonymous";
7180   char *name = XALLOCAVEC (char, strlen (base) + 64);
7181   char *p;
7182   int i, mark;
7183   unsigned char checksum[16];
7184   struct md5_ctx ctx;
7185
7186   /* Compute the checksum of the DIE, then append part of it as hex digits to
7187      the name filename of the unit.  */
7188
7189   md5_init_ctx (&ctx);
7190   mark = 0;
7191   die_checksum (unit_die, &ctx, &mark);
7192   unmark_all_dies (unit_die);
7193   md5_finish_ctx (&ctx, checksum);
7194
7195   sprintf (name, "%s.", base);
7196   clean_symbol_name (name);
7197
7198   p = name + strlen (name);
7199   for (i = 0; i < 4; i++)
7200     {
7201       sprintf (p, "%.2x", checksum[i]);
7202       p += 2;
7203     }
7204
7205   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7206   comdat_symbol_number = 0;
7207 }
7208
7209 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7210
7211 static int
7212 is_type_die (dw_die_ref die)
7213 {
7214   switch (die->die_tag)
7215     {
7216     case DW_TAG_array_type:
7217     case DW_TAG_class_type:
7218     case DW_TAG_interface_type:
7219     case DW_TAG_enumeration_type:
7220     case DW_TAG_pointer_type:
7221     case DW_TAG_reference_type:
7222     case DW_TAG_string_type:
7223     case DW_TAG_structure_type:
7224     case DW_TAG_subroutine_type:
7225     case DW_TAG_union_type:
7226     case DW_TAG_ptr_to_member_type:
7227     case DW_TAG_set_type:
7228     case DW_TAG_subrange_type:
7229     case DW_TAG_base_type:
7230     case DW_TAG_const_type:
7231     case DW_TAG_file_type:
7232     case DW_TAG_packed_type:
7233     case DW_TAG_volatile_type:
7234     case DW_TAG_typedef:
7235       return 1;
7236     default:
7237       return 0;
7238     }
7239 }
7240
7241 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7242    Basically, we want to choose the bits that are likely to be shared between
7243    compilations (types) and leave out the bits that are specific to individual
7244    compilations (functions).  */
7245
7246 static int
7247 is_comdat_die (dw_die_ref c)
7248 {
7249   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7250      we do for stabs.  The advantage is a greater likelihood of sharing between
7251      objects that don't include headers in the same order (and therefore would
7252      put the base types in a different comdat).  jason 8/28/00 */
7253
7254   if (c->die_tag == DW_TAG_base_type)
7255     return 0;
7256
7257   if (c->die_tag == DW_TAG_pointer_type
7258       || c->die_tag == DW_TAG_reference_type
7259       || c->die_tag == DW_TAG_const_type
7260       || c->die_tag == DW_TAG_volatile_type)
7261     {
7262       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7263
7264       return t ? is_comdat_die (t) : 0;
7265     }
7266
7267   return is_type_die (c);
7268 }
7269
7270 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7271    compilation unit.  */
7272
7273 static int
7274 is_symbol_die (dw_die_ref c)
7275 {
7276   return (is_type_die (c)
7277           || (get_AT (c, DW_AT_declaration)
7278               && !get_AT (c, DW_AT_specification))
7279           || c->die_tag == DW_TAG_namespace
7280           || c->die_tag == DW_TAG_module);
7281 }
7282
7283 static char *
7284 gen_internal_sym (const char *prefix)
7285 {
7286   char buf[256];
7287
7288   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7289   return xstrdup (buf);
7290 }
7291
7292 /* Assign symbols to all worthy DIEs under DIE.  */
7293
7294 static void
7295 assign_symbol_names (dw_die_ref die)
7296 {
7297   dw_die_ref c;
7298
7299   if (is_symbol_die (die))
7300     {
7301       if (comdat_symbol_id)
7302         {
7303           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7304
7305           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7306                    comdat_symbol_id, comdat_symbol_number++);
7307           die->die_symbol = xstrdup (p);
7308         }
7309       else
7310         die->die_symbol = gen_internal_sym ("LDIE");
7311     }
7312
7313   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7314 }
7315
7316 struct cu_hash_table_entry
7317 {
7318   dw_die_ref cu;
7319   unsigned min_comdat_num, max_comdat_num;
7320   struct cu_hash_table_entry *next;
7321 };
7322
7323 /* Routines to manipulate hash table of CUs.  */
7324 static hashval_t
7325 htab_cu_hash (const void *of)
7326 {
7327   const struct cu_hash_table_entry *const entry =
7328     (const struct cu_hash_table_entry *) of;
7329
7330   return htab_hash_string (entry->cu->die_symbol);
7331 }
7332
7333 static int
7334 htab_cu_eq (const void *of1, const void *of2)
7335 {
7336   const struct cu_hash_table_entry *const entry1 =
7337     (const struct cu_hash_table_entry *) of1;
7338   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7339
7340   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7341 }
7342
7343 static void
7344 htab_cu_del (void *what)
7345 {
7346   struct cu_hash_table_entry *next,
7347     *entry = (struct cu_hash_table_entry *) what;
7348
7349   while (entry)
7350     {
7351       next = entry->next;
7352       free (entry);
7353       entry = next;
7354     }
7355 }
7356
7357 /* Check whether we have already seen this CU and set up SYM_NUM
7358    accordingly.  */
7359 static int
7360 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7361 {
7362   struct cu_hash_table_entry dummy;
7363   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7364
7365   dummy.max_comdat_num = 0;
7366
7367   slot = (struct cu_hash_table_entry **)
7368     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7369         INSERT);
7370   entry = *slot;
7371
7372   for (; entry; last = entry, entry = entry->next)
7373     {
7374       if (same_die_p_wrap (cu, entry->cu))
7375         break;
7376     }
7377
7378   if (entry)
7379     {
7380       *sym_num = entry->min_comdat_num;
7381       return 1;
7382     }
7383
7384   entry = XCNEW (struct cu_hash_table_entry);
7385   entry->cu = cu;
7386   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7387   entry->next = *slot;
7388   *slot = entry;
7389
7390   return 0;
7391 }
7392
7393 /* Record SYM_NUM to record of CU in HTABLE.  */
7394 static void
7395 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7396 {
7397   struct cu_hash_table_entry **slot, *entry;
7398
7399   slot = (struct cu_hash_table_entry **)
7400     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7401         NO_INSERT);
7402   entry = *slot;
7403
7404   entry->max_comdat_num = sym_num;
7405 }
7406
7407 /* Traverse the DIE (which is always comp_unit_die), and set up
7408    additional compilation units for each of the include files we see
7409    bracketed by BINCL/EINCL.  */
7410
7411 static void
7412 break_out_includes (dw_die_ref die)
7413 {
7414   dw_die_ref c;
7415   dw_die_ref unit = NULL;
7416   limbo_die_node *node, **pnode;
7417   htab_t cu_hash_table;
7418
7419   c = die->die_child;
7420   if (c) do {
7421     dw_die_ref prev = c;
7422     c = c->die_sib;
7423     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7424            || (unit && is_comdat_die (c)))
7425       {
7426         dw_die_ref next = c->die_sib;
7427
7428         /* This DIE is for a secondary CU; remove it from the main one.  */
7429         remove_child_with_prev (c, prev);
7430
7431         if (c->die_tag == DW_TAG_GNU_BINCL)
7432           unit = push_new_compile_unit (unit, c);
7433         else if (c->die_tag == DW_TAG_GNU_EINCL)
7434           unit = pop_compile_unit (unit);
7435         else
7436           add_child_die (unit, c);
7437         c = next;
7438         if (c == die->die_child)
7439           break;
7440       }
7441   } while (c != die->die_child);
7442
7443 #if 0
7444   /* We can only use this in debugging, since the frontend doesn't check
7445      to make sure that we leave every include file we enter.  */
7446   gcc_assert (!unit);
7447 #endif
7448
7449   assign_symbol_names (die);
7450   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7451   for (node = limbo_die_list, pnode = &limbo_die_list;
7452        node;
7453        node = node->next)
7454     {
7455       int is_dupl;
7456
7457       compute_section_prefix (node->die);
7458       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7459                         &comdat_symbol_number);
7460       assign_symbol_names (node->die);
7461       if (is_dupl)
7462         *pnode = node->next;
7463       else
7464         {
7465           pnode = &node->next;
7466           record_comdat_symbol_number (node->die, cu_hash_table,
7467                 comdat_symbol_number);
7468         }
7469     }
7470   htab_delete (cu_hash_table);
7471 }
7472
7473 /* Traverse the DIE and add a sibling attribute if it may have the
7474    effect of speeding up access to siblings.  To save some space,
7475    avoid generating sibling attributes for DIE's without children.  */
7476
7477 static void
7478 add_sibling_attributes (dw_die_ref die)
7479 {
7480   dw_die_ref c;
7481
7482   if (! die->die_child)
7483     return;
7484
7485   if (die->die_parent && die != die->die_parent->die_child)
7486     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7487
7488   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7489 }
7490
7491 /* Output all location lists for the DIE and its children.  */
7492
7493 static void
7494 output_location_lists (dw_die_ref die)
7495 {
7496   dw_die_ref c;
7497   dw_attr_ref a;
7498   unsigned ix;
7499
7500   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7501     if (AT_class (a) == dw_val_class_loc_list)
7502       output_loc_list (AT_loc_list (a));
7503
7504   FOR_EACH_CHILD (die, c, output_location_lists (c));
7505 }
7506
7507 /* The format of each DIE (and its attribute value pairs) is encoded in an
7508    abbreviation table.  This routine builds the abbreviation table and assigns
7509    a unique abbreviation id for each abbreviation entry.  The children of each
7510    die are visited recursively.  */
7511
7512 static void
7513 build_abbrev_table (dw_die_ref die)
7514 {
7515   unsigned long abbrev_id;
7516   unsigned int n_alloc;
7517   dw_die_ref c;
7518   dw_attr_ref a;
7519   unsigned ix;
7520
7521   /* Scan the DIE references, and mark as external any that refer to
7522      DIEs from other CUs (i.e. those which are not marked).  */
7523   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7524     if (AT_class (a) == dw_val_class_die_ref
7525         && AT_ref (a)->die_mark == 0)
7526       {
7527         gcc_assert (AT_ref (a)->die_symbol);
7528
7529         set_AT_ref_external (a, 1);
7530       }
7531
7532   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7533     {
7534       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7535       dw_attr_ref die_a, abbrev_a;
7536       unsigned ix;
7537       bool ok = true;
7538
7539       if (abbrev->die_tag != die->die_tag)
7540         continue;
7541       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7542         continue;
7543
7544       if (VEC_length (dw_attr_node, abbrev->die_attr)
7545           != VEC_length (dw_attr_node, die->die_attr))
7546         continue;
7547
7548       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7549         {
7550           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7551           if ((abbrev_a->dw_attr != die_a->dw_attr)
7552               || (value_format (abbrev_a) != value_format (die_a)))
7553             {
7554               ok = false;
7555               break;
7556             }
7557         }
7558       if (ok)
7559         break;
7560     }
7561
7562   if (abbrev_id >= abbrev_die_table_in_use)
7563     {
7564       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7565         {
7566           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7567           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7568                                             n_alloc);
7569
7570           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7571                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7572           abbrev_die_table_allocated = n_alloc;
7573         }
7574
7575       ++abbrev_die_table_in_use;
7576       abbrev_die_table[abbrev_id] = die;
7577     }
7578
7579   die->die_abbrev = abbrev_id;
7580   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7581 }
7582 \f
7583 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7584
7585 static int
7586 constant_size (unsigned HOST_WIDE_INT value)
7587 {
7588   int log;
7589
7590   if (value == 0)
7591     log = 0;
7592   else
7593     log = floor_log2 (value);
7594
7595   log = log / 8;
7596   log = 1 << (floor_log2 (log) + 1);
7597
7598   return log;
7599 }
7600
7601 /* Return the size of a DIE as it is represented in the
7602    .debug_info section.  */
7603
7604 static unsigned long
7605 size_of_die (dw_die_ref die)
7606 {
7607   unsigned long size = 0;
7608   dw_attr_ref a;
7609   unsigned ix;
7610
7611   size += size_of_uleb128 (die->die_abbrev);
7612   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7613     {
7614       switch (AT_class (a))
7615         {
7616         case dw_val_class_addr:
7617           size += DWARF2_ADDR_SIZE;
7618           break;
7619         case dw_val_class_offset:
7620           size += DWARF_OFFSET_SIZE;
7621           break;
7622         case dw_val_class_loc:
7623           {
7624             unsigned long lsize = size_of_locs (AT_loc (a));
7625
7626             /* Block length.  */
7627             size += constant_size (lsize);
7628             size += lsize;
7629           }
7630           break;
7631         case dw_val_class_loc_list:
7632           size += DWARF_OFFSET_SIZE;
7633           break;
7634         case dw_val_class_range_list:
7635           size += DWARF_OFFSET_SIZE;
7636           break;
7637         case dw_val_class_const:
7638           size += size_of_sleb128 (AT_int (a));
7639           break;
7640         case dw_val_class_unsigned_const:
7641           size += constant_size (AT_unsigned (a));
7642           break;
7643         case dw_val_class_long_long:
7644           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7645           break;
7646         case dw_val_class_vec:
7647           size += constant_size (a->dw_attr_val.v.val_vec.length
7648                                  * a->dw_attr_val.v.val_vec.elt_size)
7649                   + a->dw_attr_val.v.val_vec.length
7650                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7651           break;
7652         case dw_val_class_flag:
7653           size += 1;
7654           break;
7655         case dw_val_class_die_ref:
7656           if (AT_ref_external (a))
7657             size += DWARF2_ADDR_SIZE;
7658           else
7659             size += DWARF_OFFSET_SIZE;
7660           break;
7661         case dw_val_class_fde_ref:
7662           size += DWARF_OFFSET_SIZE;
7663           break;
7664         case dw_val_class_lbl_id:
7665           size += DWARF2_ADDR_SIZE;
7666           break;
7667         case dw_val_class_lineptr:
7668         case dw_val_class_macptr:
7669           size += DWARF_OFFSET_SIZE;
7670           break;
7671         case dw_val_class_str:
7672           if (AT_string_form (a) == DW_FORM_strp)
7673             size += DWARF_OFFSET_SIZE;
7674           else
7675             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7676           break;
7677         case dw_val_class_file:
7678           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7679           break;
7680         default:
7681           gcc_unreachable ();
7682         }
7683     }
7684
7685   return size;
7686 }
7687
7688 /* Size the debugging information associated with a given DIE.  Visits the
7689    DIE's children recursively.  Updates the global variable next_die_offset, on
7690    each time through.  Uses the current value of next_die_offset to update the
7691    die_offset field in each DIE.  */
7692
7693 static void
7694 calc_die_sizes (dw_die_ref die)
7695 {
7696   dw_die_ref c;
7697
7698   die->die_offset = next_die_offset;
7699   next_die_offset += size_of_die (die);
7700
7701   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7702
7703   if (die->die_child != NULL)
7704     /* Count the null byte used to terminate sibling lists.  */
7705     next_die_offset += 1;
7706 }
7707
7708 /* Set the marks for a die and its children.  We do this so
7709    that we know whether or not a reference needs to use FORM_ref_addr; only
7710    DIEs in the same CU will be marked.  We used to clear out the offset
7711    and use that as the flag, but ran into ordering problems.  */
7712
7713 static void
7714 mark_dies (dw_die_ref die)
7715 {
7716   dw_die_ref c;
7717
7718   gcc_assert (!die->die_mark);
7719
7720   die->die_mark = 1;
7721   FOR_EACH_CHILD (die, c, mark_dies (c));
7722 }
7723
7724 /* Clear the marks for a die and its children.  */
7725
7726 static void
7727 unmark_dies (dw_die_ref die)
7728 {
7729   dw_die_ref c;
7730
7731   gcc_assert (die->die_mark);
7732
7733   die->die_mark = 0;
7734   FOR_EACH_CHILD (die, c, unmark_dies (c));
7735 }
7736
7737 /* Clear the marks for a die, its children and referred dies.  */
7738
7739 static void
7740 unmark_all_dies (dw_die_ref die)
7741 {
7742   dw_die_ref c;
7743   dw_attr_ref a;
7744   unsigned ix;
7745
7746   if (!die->die_mark)
7747     return;
7748   die->die_mark = 0;
7749
7750   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7751
7752   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7753     if (AT_class (a) == dw_val_class_die_ref)
7754       unmark_all_dies (AT_ref (a));
7755 }
7756
7757 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7758    generated for the compilation unit.  */
7759
7760 static unsigned long
7761 size_of_pubnames (VEC (pubname_entry, gc) * names)
7762 {
7763   unsigned long size;
7764   unsigned i;
7765   pubname_ref p;
7766
7767   size = DWARF_PUBNAMES_HEADER_SIZE;
7768   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7769     if (names != pubtype_table
7770         || p->die->die_offset != 0
7771         || !flag_eliminate_unused_debug_types)
7772       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7773
7774   size += DWARF_OFFSET_SIZE;
7775   return size;
7776 }
7777
7778 /* Return the size of the information in the .debug_aranges section.  */
7779
7780 static unsigned long
7781 size_of_aranges (void)
7782 {
7783   unsigned long size;
7784
7785   size = DWARF_ARANGES_HEADER_SIZE;
7786
7787   /* Count the address/length pair for this compilation unit.  */
7788   if (text_section_used)
7789     size += 2 * DWARF2_ADDR_SIZE;
7790   if (cold_text_section_used)
7791     size += 2 * DWARF2_ADDR_SIZE;
7792   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7793
7794   /* Count the two zero words used to terminated the address range table.  */
7795   size += 2 * DWARF2_ADDR_SIZE;
7796   return size;
7797 }
7798 \f
7799 /* Select the encoding of an attribute value.  */
7800
7801 static enum dwarf_form
7802 value_format (dw_attr_ref a)
7803 {
7804   switch (a->dw_attr_val.val_class)
7805     {
7806     case dw_val_class_addr:
7807       return DW_FORM_addr;
7808     case dw_val_class_range_list:
7809     case dw_val_class_offset:
7810     case dw_val_class_loc_list:
7811       switch (DWARF_OFFSET_SIZE)
7812         {
7813         case 4:
7814           return DW_FORM_data4;
7815         case 8:
7816           return DW_FORM_data8;
7817         default:
7818           gcc_unreachable ();
7819         }
7820     case dw_val_class_loc:
7821       switch (constant_size (size_of_locs (AT_loc (a))))
7822         {
7823         case 1:
7824           return DW_FORM_block1;
7825         case 2:
7826           return DW_FORM_block2;
7827         default:
7828           gcc_unreachable ();
7829         }
7830     case dw_val_class_const:
7831       return DW_FORM_sdata;
7832     case dw_val_class_unsigned_const:
7833       switch (constant_size (AT_unsigned (a)))
7834         {
7835         case 1:
7836           return DW_FORM_data1;
7837         case 2:
7838           return DW_FORM_data2;
7839         case 4:
7840           return DW_FORM_data4;
7841         case 8:
7842           return DW_FORM_data8;
7843         default:
7844           gcc_unreachable ();
7845         }
7846     case dw_val_class_long_long:
7847       return DW_FORM_block1;
7848     case dw_val_class_vec:
7849       switch (constant_size (a->dw_attr_val.v.val_vec.length
7850                              * a->dw_attr_val.v.val_vec.elt_size))
7851         {
7852         case 1:
7853           return DW_FORM_block1;
7854         case 2:
7855           return DW_FORM_block2;
7856         case 4:
7857           return DW_FORM_block4;
7858         default:
7859           gcc_unreachable ();
7860         }
7861     case dw_val_class_flag:
7862       return DW_FORM_flag;
7863     case dw_val_class_die_ref:
7864       if (AT_ref_external (a))
7865         return DW_FORM_ref_addr;
7866       else
7867         return DW_FORM_ref;
7868     case dw_val_class_fde_ref:
7869       return DW_FORM_data;
7870     case dw_val_class_lbl_id:
7871       return DW_FORM_addr;
7872     case dw_val_class_lineptr:
7873     case dw_val_class_macptr:
7874       return DW_FORM_data;
7875     case dw_val_class_str:
7876       return AT_string_form (a);
7877     case dw_val_class_file:
7878       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7879         {
7880         case 1:
7881           return DW_FORM_data1;
7882         case 2:
7883           return DW_FORM_data2;
7884         case 4:
7885           return DW_FORM_data4;
7886         default:
7887           gcc_unreachable ();
7888         }
7889
7890     default:
7891       gcc_unreachable ();
7892     }
7893 }
7894
7895 /* Output the encoding of an attribute value.  */
7896
7897 static void
7898 output_value_format (dw_attr_ref a)
7899 {
7900   enum dwarf_form form = value_format (a);
7901
7902   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7903 }
7904
7905 /* Output the .debug_abbrev section which defines the DIE abbreviation
7906    table.  */
7907
7908 static void
7909 output_abbrev_section (void)
7910 {
7911   unsigned long abbrev_id;
7912
7913   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7914     {
7915       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7916       unsigned ix;
7917       dw_attr_ref a_attr;
7918
7919       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7920       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7921                                    dwarf_tag_name (abbrev->die_tag));
7922
7923       if (abbrev->die_child != NULL)
7924         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7925       else
7926         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7927
7928       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7929            ix++)
7930         {
7931           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7932                                        dwarf_attr_name (a_attr->dw_attr));
7933           output_value_format (a_attr);
7934         }
7935
7936       dw2_asm_output_data (1, 0, NULL);
7937       dw2_asm_output_data (1, 0, NULL);
7938     }
7939
7940   /* Terminate the table.  */
7941   dw2_asm_output_data (1, 0, NULL);
7942 }
7943
7944 /* Output a symbol we can use to refer to this DIE from another CU.  */
7945
7946 static inline void
7947 output_die_symbol (dw_die_ref die)
7948 {
7949   char *sym = die->die_symbol;
7950
7951   if (sym == 0)
7952     return;
7953
7954   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7955     /* We make these global, not weak; if the target doesn't support
7956        .linkonce, it doesn't support combining the sections, so debugging
7957        will break.  */
7958     targetm.asm_out.globalize_label (asm_out_file, sym);
7959
7960   ASM_OUTPUT_LABEL (asm_out_file, sym);
7961 }
7962
7963 /* Return a new location list, given the begin and end range, and the
7964    expression. gensym tells us whether to generate a new internal symbol for
7965    this location list node, which is done for the head of the list only.  */
7966
7967 static inline dw_loc_list_ref
7968 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7969               const char *section, unsigned int gensym)
7970 {
7971   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7972
7973   retlist->begin = begin;
7974   retlist->end = end;
7975   retlist->expr = expr;
7976   retlist->section = section;
7977   if (gensym)
7978     retlist->ll_symbol = gen_internal_sym ("LLST");
7979
7980   return retlist;
7981 }
7982
7983 /* Add a location description expression to a location list.  */
7984
7985 static inline void
7986 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7987                            const char *begin, const char *end,
7988                            const char *section)
7989 {
7990   dw_loc_list_ref *d;
7991
7992   /* Find the end of the chain.  */
7993   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7994     ;
7995
7996   /* Add a new location list node to the list.  */
7997   *d = new_loc_list (descr, begin, end, section, 0);
7998 }
7999
8000 /* Output the location list given to us.  */
8001
8002 static void
8003 output_loc_list (dw_loc_list_ref list_head)
8004 {
8005   dw_loc_list_ref curr = list_head;
8006
8007   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8008
8009   /* Walk the location list, and output each range + expression.  */
8010   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8011     {
8012       unsigned long size;
8013       /* Don't output an entry that starts and ends at the same address.  */
8014       if (strcmp (curr->begin, curr->end) == 0)
8015         continue;
8016       if (!have_multiple_function_sections)
8017         {
8018           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8019                                 "Location list begin address (%s)",
8020                                 list_head->ll_symbol);
8021           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8022                                 "Location list end address (%s)",
8023                                 list_head->ll_symbol);
8024         }
8025       else
8026         {
8027           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8028                                "Location list begin address (%s)",
8029                                list_head->ll_symbol);
8030           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8031                                "Location list end address (%s)",
8032                                list_head->ll_symbol);
8033         }
8034       size = size_of_locs (curr->expr);
8035
8036       /* Output the block length for this list of location operations.  */
8037       gcc_assert (size <= 0xffff);
8038       dw2_asm_output_data (2, size, "%s", "Location expression size");
8039
8040       output_loc_sequence (curr->expr);
8041     }
8042
8043   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8044                        "Location list terminator begin (%s)",
8045                        list_head->ll_symbol);
8046   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8047                        "Location list terminator end (%s)",
8048                        list_head->ll_symbol);
8049 }
8050
8051 /* Output the DIE and its attributes.  Called recursively to generate
8052    the definitions of each child DIE.  */
8053
8054 static void
8055 output_die (dw_die_ref die)
8056 {
8057   dw_attr_ref a;
8058   dw_die_ref c;
8059   unsigned long size;
8060   unsigned ix;
8061
8062   /* If someone in another CU might refer to us, set up a symbol for
8063      them to point to.  */
8064   if (die->die_symbol)
8065     output_die_symbol (die);
8066
8067   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8068                                (unsigned long)die->die_offset,
8069                                dwarf_tag_name (die->die_tag));
8070
8071   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8072     {
8073       const char *name = dwarf_attr_name (a->dw_attr);
8074
8075       switch (AT_class (a))
8076         {
8077         case dw_val_class_addr:
8078           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8079           break;
8080
8081         case dw_val_class_offset:
8082           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8083                                "%s", name);
8084           break;
8085
8086         case dw_val_class_range_list:
8087           {
8088             char *p = strchr (ranges_section_label, '\0');
8089
8090             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8091                      a->dw_attr_val.v.val_offset);
8092             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8093                                    debug_ranges_section, "%s", name);
8094             *p = '\0';
8095           }
8096           break;
8097
8098         case dw_val_class_loc:
8099           size = size_of_locs (AT_loc (a));
8100
8101           /* Output the block length for this list of location operations.  */
8102           dw2_asm_output_data (constant_size (size), size, "%s", name);
8103
8104           output_loc_sequence (AT_loc (a));
8105           break;
8106
8107         case dw_val_class_const:
8108           /* ??? It would be slightly more efficient to use a scheme like is
8109              used for unsigned constants below, but gdb 4.x does not sign
8110              extend.  Gdb 5.x does sign extend.  */
8111           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8112           break;
8113
8114         case dw_val_class_unsigned_const:
8115           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8116                                AT_unsigned (a), "%s", name);
8117           break;
8118
8119         case dw_val_class_long_long:
8120           {
8121             unsigned HOST_WIDE_INT first, second;
8122
8123             dw2_asm_output_data (1,
8124                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8125                                  "%s", name);
8126
8127             if (WORDS_BIG_ENDIAN)
8128               {
8129                 first = a->dw_attr_val.v.val_long_long.hi;
8130                 second = a->dw_attr_val.v.val_long_long.low;
8131               }
8132             else
8133               {
8134                 first = a->dw_attr_val.v.val_long_long.low;
8135                 second = a->dw_attr_val.v.val_long_long.hi;
8136               }
8137
8138             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8139                                  first, "long long constant");
8140             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8141                                  second, NULL);
8142           }
8143           break;
8144
8145         case dw_val_class_vec:
8146           {
8147             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8148             unsigned int len = a->dw_attr_val.v.val_vec.length;
8149             unsigned int i;
8150             unsigned char *p;
8151
8152             dw2_asm_output_data (constant_size (len * elt_size),
8153                                  len * elt_size, "%s", name);
8154             if (elt_size > sizeof (HOST_WIDE_INT))
8155               {
8156                 elt_size /= 2;
8157                 len *= 2;
8158               }
8159             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8160                  i < len;
8161                  i++, p += elt_size)
8162               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8163                                    "fp or vector constant word %u", i);
8164             break;
8165           }
8166
8167         case dw_val_class_flag:
8168           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8169           break;
8170
8171         case dw_val_class_loc_list:
8172           {
8173             char *sym = AT_loc_list (a)->ll_symbol;
8174
8175             gcc_assert (sym);
8176             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8177                                    "%s", name);
8178           }
8179           break;
8180
8181         case dw_val_class_die_ref:
8182           if (AT_ref_external (a))
8183             {
8184               char *sym = AT_ref (a)->die_symbol;
8185
8186               gcc_assert (sym);
8187               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8188                                      "%s", name);
8189             }
8190           else
8191             {
8192               gcc_assert (AT_ref (a)->die_offset);
8193               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8194                                    "%s", name);
8195             }
8196           break;
8197
8198         case dw_val_class_fde_ref:
8199           {
8200             char l1[20];
8201
8202             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8203                                          a->dw_attr_val.v.val_fde_index * 2);
8204             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8205                                    "%s", name);
8206           }
8207           break;
8208
8209         case dw_val_class_lbl_id:
8210           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8211           break;
8212
8213         case dw_val_class_lineptr:
8214           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8215                                  debug_line_section, "%s", name);
8216           break;
8217
8218         case dw_val_class_macptr:
8219           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8220                                  debug_macinfo_section, "%s", name);
8221           break;
8222
8223         case dw_val_class_str:
8224           if (AT_string_form (a) == DW_FORM_strp)
8225             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8226                                    a->dw_attr_val.v.val_str->label,
8227                                    debug_str_section,
8228                                    "%s: \"%s\"", name, AT_string (a));
8229           else
8230             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8231           break;
8232
8233         case dw_val_class_file:
8234           {
8235             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8236
8237             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8238                                  a->dw_attr_val.v.val_file->filename);
8239             break;
8240           }
8241
8242         default:
8243           gcc_unreachable ();
8244         }
8245     }
8246
8247   FOR_EACH_CHILD (die, c, output_die (c));
8248
8249   /* Add null byte to terminate sibling list.  */
8250   if (die->die_child != NULL)
8251     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8252                          (unsigned long) die->die_offset);
8253 }
8254
8255 /* Output the compilation unit that appears at the beginning of the
8256    .debug_info section, and precedes the DIE descriptions.  */
8257
8258 static void
8259 output_compilation_unit_header (void)
8260 {
8261   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8262     dw2_asm_output_data (4, 0xffffffff,
8263       "Initial length escape value indicating 64-bit DWARF extension");
8264   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8265                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8266                        "Length of Compilation Unit Info");
8267   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8268   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8269                          debug_abbrev_section,
8270                          "Offset Into Abbrev. Section");
8271   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8272 }
8273
8274 /* Output the compilation unit DIE and its children.  */
8275
8276 static void
8277 output_comp_unit (dw_die_ref die, int output_if_empty)
8278 {
8279   const char *secname;
8280   char *oldsym, *tmp;
8281
8282   /* Unless we are outputting main CU, we may throw away empty ones.  */
8283   if (!output_if_empty && die->die_child == NULL)
8284     return;
8285
8286   /* Even if there are no children of this DIE, we must output the information
8287      about the compilation unit.  Otherwise, on an empty translation unit, we
8288      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8289      will then complain when examining the file.  First mark all the DIEs in
8290      this CU so we know which get local refs.  */
8291   mark_dies (die);
8292
8293   build_abbrev_table (die);
8294
8295   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8296   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8297   calc_die_sizes (die);
8298
8299   oldsym = die->die_symbol;
8300   if (oldsym)
8301     {
8302       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8303
8304       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8305       secname = tmp;
8306       die->die_symbol = NULL;
8307       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8308     }
8309   else
8310     switch_to_section (debug_info_section);
8311
8312   /* Output debugging information.  */
8313   output_compilation_unit_header ();
8314   output_die (die);
8315
8316   /* Leave the marks on the main CU, so we can check them in
8317      output_pubnames.  */
8318   if (oldsym)
8319     {
8320       unmark_dies (die);
8321       die->die_symbol = oldsym;
8322     }
8323 }
8324
8325 /* Return the DWARF2/3 pubname associated with a decl.  */
8326
8327 static const char *
8328 dwarf2_name (tree decl, int scope)
8329 {
8330   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8331 }
8332
8333 /* Add a new entry to .debug_pubnames if appropriate.  */
8334
8335 static void
8336 add_pubname_string (const char *str, dw_die_ref die)
8337 {
8338   pubname_entry e;
8339
8340   e.die = die;
8341   e.name = xstrdup (str);
8342   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8343 }
8344
8345 static void
8346 add_pubname (tree decl, dw_die_ref die)
8347 {
8348
8349   if (TREE_PUBLIC (decl))
8350     add_pubname_string (dwarf2_name (decl, 1), die);
8351 }
8352
8353 /* Add a new entry to .debug_pubtypes if appropriate.  */
8354
8355 static void
8356 add_pubtype (tree decl, dw_die_ref die)
8357 {
8358   pubname_entry e;
8359
8360   e.name = NULL;
8361   if ((TREE_PUBLIC (decl)
8362        || die->die_parent == comp_unit_die)
8363       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8364     {
8365       e.die = die;
8366       if (TYPE_P (decl))
8367         {
8368           if (TYPE_NAME (decl))
8369             {
8370               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8371                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8372               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8373                        && DECL_NAME (TYPE_NAME (decl)))
8374                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8375               else
8376                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8377             }
8378         }
8379       else
8380         e.name = xstrdup (dwarf2_name (decl, 1));
8381
8382       /* If we don't have a name for the type, there's no point in adding
8383          it to the table.  */
8384       if (e.name && e.name[0] != '\0')
8385         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8386     }
8387 }
8388
8389 /* Output the public names table used to speed up access to externally
8390    visible names; or the public types table used to find type definitions.  */
8391
8392 static void
8393 output_pubnames (VEC (pubname_entry, gc) * names)
8394 {
8395   unsigned i;
8396   unsigned long pubnames_length = size_of_pubnames (names);
8397   pubname_ref pub;
8398
8399   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8400     dw2_asm_output_data (4, 0xffffffff,
8401       "Initial length escape value indicating 64-bit DWARF extension");
8402   if (names == pubname_table)
8403     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8404                          "Length of Public Names Info");
8405   else
8406     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8407                          "Length of Public Type Names Info");
8408   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8409   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8410                          debug_info_section,
8411                          "Offset of Compilation Unit Info");
8412   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8413                        "Compilation Unit Length");
8414
8415   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8416     {
8417       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8418       if (names == pubname_table)
8419         gcc_assert (pub->die->die_mark);
8420
8421       if (names != pubtype_table
8422           || pub->die->die_offset != 0
8423           || !flag_eliminate_unused_debug_types)
8424         {
8425           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8426                                "DIE offset");
8427
8428           dw2_asm_output_nstring (pub->name, -1, "external name");
8429         }
8430     }
8431
8432   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8433 }
8434
8435 /* Add a new entry to .debug_aranges if appropriate.  */
8436
8437 static void
8438 add_arange (tree decl, dw_die_ref die)
8439 {
8440   if (! DECL_SECTION_NAME (decl))
8441     return;
8442
8443   if (arange_table_in_use == arange_table_allocated)
8444     {
8445       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8446       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8447                                     arange_table_allocated);
8448       memset (arange_table + arange_table_in_use, 0,
8449               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8450     }
8451
8452   arange_table[arange_table_in_use++] = die;
8453 }
8454
8455 /* Output the information that goes into the .debug_aranges table.
8456    Namely, define the beginning and ending address range of the
8457    text section generated for this compilation unit.  */
8458
8459 static void
8460 output_aranges (void)
8461 {
8462   unsigned i;
8463   unsigned long aranges_length = size_of_aranges ();
8464
8465   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8466     dw2_asm_output_data (4, 0xffffffff,
8467       "Initial length escape value indicating 64-bit DWARF extension");
8468   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8469                        "Length of Address Ranges Info");
8470   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8471   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8472                          debug_info_section,
8473                          "Offset of Compilation Unit Info");
8474   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8475   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8476
8477   /* We need to align to twice the pointer size here.  */
8478   if (DWARF_ARANGES_PAD_SIZE)
8479     {
8480       /* Pad using a 2 byte words so that padding is correct for any
8481          pointer size.  */
8482       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8483                            2 * DWARF2_ADDR_SIZE);
8484       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8485         dw2_asm_output_data (2, 0, NULL);
8486     }
8487
8488   /* It is necessary not to output these entries if the sections were
8489      not used; if the sections were not used, the length will be 0 and
8490      the address may end up as 0 if the section is discarded by ld
8491      --gc-sections, leaving an invalid (0, 0) entry that can be
8492      confused with the terminator.  */
8493   if (text_section_used)
8494     {
8495       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8496       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8497                             text_section_label, "Length");
8498     }
8499   if (cold_text_section_used)
8500     {
8501       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8502                            "Address");
8503       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8504                             cold_text_section_label, "Length");
8505     }
8506
8507   for (i = 0; i < arange_table_in_use; i++)
8508     {
8509       dw_die_ref die = arange_table[i];
8510
8511       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8512       gcc_assert (die->die_mark);
8513
8514       if (die->die_tag == DW_TAG_subprogram)
8515         {
8516           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8517                                "Address");
8518           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8519                                 get_AT_low_pc (die), "Length");
8520         }
8521       else
8522         {
8523           /* A static variable; extract the symbol from DW_AT_location.
8524              Note that this code isn't currently hit, as we only emit
8525              aranges for functions (jason 9/23/99).  */
8526           dw_attr_ref a = get_AT (die, DW_AT_location);
8527           dw_loc_descr_ref loc;
8528
8529           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8530
8531           loc = AT_loc (a);
8532           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8533
8534           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8535                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8536           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8537                                get_AT_unsigned (die, DW_AT_byte_size),
8538                                "Length");
8539         }
8540     }
8541
8542   /* Output the terminator words.  */
8543   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8544   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8545 }
8546
8547 /* Add a new entry to .debug_ranges.  Return the offset at which it
8548    was placed.  */
8549
8550 static unsigned int
8551 add_ranges_num (int num)
8552 {
8553   unsigned int in_use = ranges_table_in_use;
8554
8555   if (in_use == ranges_table_allocated)
8556     {
8557       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8558       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8559                                     ranges_table_allocated);
8560       memset (ranges_table + ranges_table_in_use, 0,
8561               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8562     }
8563
8564   ranges_table[in_use].num = num;
8565   ranges_table_in_use = in_use + 1;
8566
8567   return in_use * 2 * DWARF2_ADDR_SIZE;
8568 }
8569
8570 /* Add a new entry to .debug_ranges corresponding to a block, or a
8571    range terminator if BLOCK is NULL.  */
8572
8573 static unsigned int
8574 add_ranges (const_tree block)
8575 {
8576   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8577 }
8578
8579 /* Add a new entry to .debug_ranges corresponding to a pair of
8580    labels.  */
8581
8582 static unsigned int
8583 add_ranges_by_labels (const char *begin, const char *end)
8584 {
8585   unsigned int in_use = ranges_by_label_in_use;
8586
8587   if (in_use == ranges_by_label_allocated)
8588     {
8589       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8590       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8591                                        ranges_by_label,
8592                                        ranges_by_label_allocated);
8593       memset (ranges_by_label + ranges_by_label_in_use, 0,
8594               RANGES_TABLE_INCREMENT
8595               * sizeof (struct dw_ranges_by_label_struct));
8596     }
8597
8598   ranges_by_label[in_use].begin = begin;
8599   ranges_by_label[in_use].end = end;
8600   ranges_by_label_in_use = in_use + 1;
8601
8602   return add_ranges_num (-(int)in_use - 1);
8603 }
8604
8605 static void
8606 output_ranges (void)
8607 {
8608   unsigned i;
8609   static const char *const start_fmt = "Offset 0x%x";
8610   const char *fmt = start_fmt;
8611
8612   for (i = 0; i < ranges_table_in_use; i++)
8613     {
8614       int block_num = ranges_table[i].num;
8615
8616       if (block_num > 0)
8617         {
8618           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8619           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8620
8621           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8622           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8623
8624           /* If all code is in the text section, then the compilation
8625              unit base address defaults to DW_AT_low_pc, which is the
8626              base of the text section.  */
8627           if (!have_multiple_function_sections)
8628             {
8629               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8630                                     text_section_label,
8631                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8632               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8633                                     text_section_label, NULL);
8634             }
8635
8636           /* Otherwise, the compilation unit base address is zero,
8637              which allows us to use absolute addresses, and not worry
8638              about whether the target supports cross-section
8639              arithmetic.  */
8640           else
8641             {
8642               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8643                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8644               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8645             }
8646
8647           fmt = NULL;
8648         }
8649
8650       /* Negative block_num stands for an index into ranges_by_label.  */
8651       else if (block_num < 0)
8652         {
8653           int lab_idx = - block_num - 1;
8654
8655           if (!have_multiple_function_sections)
8656             {
8657               gcc_unreachable ();
8658 #if 0
8659               /* If we ever use add_ranges_by_labels () for a single
8660                  function section, all we have to do is to take out
8661                  the #if 0 above.  */
8662               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8663                                     ranges_by_label[lab_idx].begin,
8664                                     text_section_label,
8665                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8666               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8667                                     ranges_by_label[lab_idx].end,
8668                                     text_section_label, NULL);
8669 #endif
8670             }
8671           else
8672             {
8673               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8674                                    ranges_by_label[lab_idx].begin,
8675                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8676               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8677                                    ranges_by_label[lab_idx].end,
8678                                    NULL);
8679             }
8680         }
8681       else
8682         {
8683           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8684           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8685           fmt = start_fmt;
8686         }
8687     }
8688 }
8689
8690 /* Data structure containing information about input files.  */
8691 struct file_info
8692 {
8693   const char *path;     /* Complete file name.  */
8694   const char *fname;    /* File name part.  */
8695   int length;           /* Length of entire string.  */
8696   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8697   int dir_idx;          /* Index in directory table.  */
8698 };
8699
8700 /* Data structure containing information about directories with source
8701    files.  */
8702 struct dir_info
8703 {
8704   const char *path;     /* Path including directory name.  */
8705   int length;           /* Path length.  */
8706   int prefix;           /* Index of directory entry which is a prefix.  */
8707   int count;            /* Number of files in this directory.  */
8708   int dir_idx;          /* Index of directory used as base.  */
8709 };
8710
8711 /* Callback function for file_info comparison.  We sort by looking at
8712    the directories in the path.  */
8713
8714 static int
8715 file_info_cmp (const void *p1, const void *p2)
8716 {
8717   const struct file_info *const s1 = (const struct file_info *) p1;
8718   const struct file_info *const s2 = (const struct file_info *) p2;
8719   const unsigned char *cp1;
8720   const unsigned char *cp2;
8721
8722   /* Take care of file names without directories.  We need to make sure that
8723      we return consistent values to qsort since some will get confused if
8724      we return the same value when identical operands are passed in opposite
8725      orders.  So if neither has a directory, return 0 and otherwise return
8726      1 or -1 depending on which one has the directory.  */
8727   if ((s1->path == s1->fname || s2->path == s2->fname))
8728     return (s2->path == s2->fname) - (s1->path == s1->fname);
8729
8730   cp1 = (const unsigned char *) s1->path;
8731   cp2 = (const unsigned char *) s2->path;
8732
8733   while (1)
8734     {
8735       ++cp1;
8736       ++cp2;
8737       /* Reached the end of the first path?  If so, handle like above.  */
8738       if ((cp1 == (const unsigned char *) s1->fname)
8739           || (cp2 == (const unsigned char *) s2->fname))
8740         return ((cp2 == (const unsigned char *) s2->fname)
8741                 - (cp1 == (const unsigned char *) s1->fname));
8742
8743       /* Character of current path component the same?  */
8744       else if (*cp1 != *cp2)
8745         return *cp1 - *cp2;
8746     }
8747 }
8748
8749 struct file_name_acquire_data
8750 {
8751   struct file_info *files;
8752   int used_files;
8753   int max_files;
8754 };
8755
8756 /* Traversal function for the hash table.  */
8757
8758 static int
8759 file_name_acquire (void ** slot, void *data)
8760 {
8761   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8762   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8763   struct file_info *fi;
8764   const char *f;
8765
8766   gcc_assert (fnad->max_files >= d->emitted_number);
8767
8768   if (! d->emitted_number)
8769     return 1;
8770
8771   gcc_assert (fnad->max_files != fnad->used_files);
8772
8773   fi = fnad->files + fnad->used_files++;
8774
8775   /* Skip all leading "./".  */
8776   f = d->filename;
8777   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8778     f += 2;
8779
8780   /* Create a new array entry.  */
8781   fi->path = f;
8782   fi->length = strlen (f);
8783   fi->file_idx = d;
8784
8785   /* Search for the file name part.  */
8786   f = strrchr (f, DIR_SEPARATOR);
8787 #if defined (DIR_SEPARATOR_2)
8788   {
8789     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8790
8791     if (g != NULL)
8792       {
8793         if (f == NULL || f < g)
8794           f = g;
8795       }
8796   }
8797 #endif
8798
8799   fi->fname = f == NULL ? fi->path : f + 1;
8800   return 1;
8801 }
8802
8803 /* Output the directory table and the file name table.  We try to minimize
8804    the total amount of memory needed.  A heuristic is used to avoid large
8805    slowdowns with many input files.  */
8806
8807 static void
8808 output_file_names (void)
8809 {
8810   struct file_name_acquire_data fnad;
8811   int numfiles;
8812   struct file_info *files;
8813   struct dir_info *dirs;
8814   int *saved;
8815   int *savehere;
8816   int *backmap;
8817   int ndirs;
8818   int idx_offset;
8819   int i;
8820   int idx;
8821
8822   if (!last_emitted_file)
8823     {
8824       dw2_asm_output_data (1, 0, "End directory table");
8825       dw2_asm_output_data (1, 0, "End file name table");
8826       return;
8827     }
8828
8829   numfiles = last_emitted_file->emitted_number;
8830
8831   /* Allocate the various arrays we need.  */
8832   files = XALLOCAVEC (struct file_info, numfiles);
8833   dirs = XALLOCAVEC (struct dir_info, numfiles);
8834
8835   fnad.files = files;
8836   fnad.used_files = 0;
8837   fnad.max_files = numfiles;
8838   htab_traverse (file_table, file_name_acquire, &fnad);
8839   gcc_assert (fnad.used_files == fnad.max_files);
8840
8841   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8842
8843   /* Find all the different directories used.  */
8844   dirs[0].path = files[0].path;
8845   dirs[0].length = files[0].fname - files[0].path;
8846   dirs[0].prefix = -1;
8847   dirs[0].count = 1;
8848   dirs[0].dir_idx = 0;
8849   files[0].dir_idx = 0;
8850   ndirs = 1;
8851
8852   for (i = 1; i < numfiles; i++)
8853     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8854         && memcmp (dirs[ndirs - 1].path, files[i].path,
8855                    dirs[ndirs - 1].length) == 0)
8856       {
8857         /* Same directory as last entry.  */
8858         files[i].dir_idx = ndirs - 1;
8859         ++dirs[ndirs - 1].count;
8860       }
8861     else
8862       {
8863         int j;
8864
8865         /* This is a new directory.  */
8866         dirs[ndirs].path = files[i].path;
8867         dirs[ndirs].length = files[i].fname - files[i].path;
8868         dirs[ndirs].count = 1;
8869         dirs[ndirs].dir_idx = ndirs;
8870         files[i].dir_idx = ndirs;
8871
8872         /* Search for a prefix.  */
8873         dirs[ndirs].prefix = -1;
8874         for (j = 0; j < ndirs; j++)
8875           if (dirs[j].length < dirs[ndirs].length
8876               && dirs[j].length > 1
8877               && (dirs[ndirs].prefix == -1
8878                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8879               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8880             dirs[ndirs].prefix = j;
8881
8882         ++ndirs;
8883       }
8884
8885   /* Now to the actual work.  We have to find a subset of the directories which
8886      allow expressing the file name using references to the directory table
8887      with the least amount of characters.  We do not do an exhaustive search
8888      where we would have to check out every combination of every single
8889      possible prefix.  Instead we use a heuristic which provides nearly optimal
8890      results in most cases and never is much off.  */
8891   saved = XALLOCAVEC (int, ndirs);
8892   savehere = XALLOCAVEC (int, ndirs);
8893
8894   memset (saved, '\0', ndirs * sizeof (saved[0]));
8895   for (i = 0; i < ndirs; i++)
8896     {
8897       int j;
8898       int total;
8899
8900       /* We can always save some space for the current directory.  But this
8901          does not mean it will be enough to justify adding the directory.  */
8902       savehere[i] = dirs[i].length;
8903       total = (savehere[i] - saved[i]) * dirs[i].count;
8904
8905       for (j = i + 1; j < ndirs; j++)
8906         {
8907           savehere[j] = 0;
8908           if (saved[j] < dirs[i].length)
8909             {
8910               /* Determine whether the dirs[i] path is a prefix of the
8911                  dirs[j] path.  */
8912               int k;
8913
8914               k = dirs[j].prefix;
8915               while (k != -1 && k != (int) i)
8916                 k = dirs[k].prefix;
8917
8918               if (k == (int) i)
8919                 {
8920                   /* Yes it is.  We can possibly save some memory by
8921                      writing the filenames in dirs[j] relative to
8922                      dirs[i].  */
8923                   savehere[j] = dirs[i].length;
8924                   total += (savehere[j] - saved[j]) * dirs[j].count;
8925                 }
8926             }
8927         }
8928
8929       /* Check whether we can save enough to justify adding the dirs[i]
8930          directory.  */
8931       if (total > dirs[i].length + 1)
8932         {
8933           /* It's worthwhile adding.  */
8934           for (j = i; j < ndirs; j++)
8935             if (savehere[j] > 0)
8936               {
8937                 /* Remember how much we saved for this directory so far.  */
8938                 saved[j] = savehere[j];
8939
8940                 /* Remember the prefix directory.  */
8941                 dirs[j].dir_idx = i;
8942               }
8943         }
8944     }
8945
8946   /* Emit the directory name table.  */
8947   idx = 1;
8948   idx_offset = dirs[0].length > 0 ? 1 : 0;
8949   for (i = 1 - idx_offset; i < ndirs; i++)
8950     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8951                             "Directory Entry: 0x%x", i + idx_offset);
8952
8953   dw2_asm_output_data (1, 0, "End directory table");
8954
8955   /* We have to emit them in the order of emitted_number since that's
8956      used in the debug info generation.  To do this efficiently we
8957      generate a back-mapping of the indices first.  */
8958   backmap = XALLOCAVEC (int, numfiles);
8959   for (i = 0; i < numfiles; i++)
8960     backmap[files[i].file_idx->emitted_number - 1] = i;
8961
8962   /* Now write all the file names.  */
8963   for (i = 0; i < numfiles; i++)
8964     {
8965       int file_idx = backmap[i];
8966       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8967
8968       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8969                               "File Entry: 0x%x", (unsigned) i + 1);
8970
8971       /* Include directory index.  */
8972       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8973
8974       /* Modification time.  */
8975       dw2_asm_output_data_uleb128 (0, NULL);
8976
8977       /* File length in bytes.  */
8978       dw2_asm_output_data_uleb128 (0, NULL);
8979     }
8980
8981   dw2_asm_output_data (1, 0, "End file name table");
8982 }
8983
8984
8985 /* Output the source line number correspondence information.  This
8986    information goes into the .debug_line section.  */
8987
8988 static void
8989 output_line_info (void)
8990 {
8991   char l1[20], l2[20], p1[20], p2[20];
8992   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8993   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8994   unsigned opc;
8995   unsigned n_op_args;
8996   unsigned long lt_index;
8997   unsigned long current_line;
8998   long line_offset;
8999   long line_delta;
9000   unsigned long current_file;
9001   unsigned long function;
9002
9003   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9004   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9005   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9006   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9007
9008   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9009     dw2_asm_output_data (4, 0xffffffff,
9010       "Initial length escape value indicating 64-bit DWARF extension");
9011   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9012                         "Length of Source Line Info");
9013   ASM_OUTPUT_LABEL (asm_out_file, l1);
9014
9015   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9016   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9017   ASM_OUTPUT_LABEL (asm_out_file, p1);
9018
9019   /* Define the architecture-dependent minimum instruction length (in
9020    bytes).  In this implementation of DWARF, this field is used for
9021    information purposes only.  Since GCC generates assembly language,
9022    we have no a priori knowledge of how many instruction bytes are
9023    generated for each source line, and therefore can use only the
9024    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9025    commands.  Accordingly, we fix this as `1', which is "correct
9026    enough" for all architectures, and don't let the target override.  */
9027   dw2_asm_output_data (1, 1,
9028                        "Minimum Instruction Length");
9029
9030   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9031                        "Default is_stmt_start flag");
9032   dw2_asm_output_data (1, DWARF_LINE_BASE,
9033                        "Line Base Value (Special Opcodes)");
9034   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9035                        "Line Range Value (Special Opcodes)");
9036   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9037                        "Special Opcode Base");
9038
9039   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9040     {
9041       switch (opc)
9042         {
9043         case DW_LNS_advance_pc:
9044         case DW_LNS_advance_line:
9045         case DW_LNS_set_file:
9046         case DW_LNS_set_column:
9047         case DW_LNS_fixed_advance_pc:
9048           n_op_args = 1;
9049           break;
9050         default:
9051           n_op_args = 0;
9052           break;
9053         }
9054
9055       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9056                            opc, n_op_args);
9057     }
9058
9059   /* Write out the information about the files we use.  */
9060   output_file_names ();
9061   ASM_OUTPUT_LABEL (asm_out_file, p2);
9062
9063   /* We used to set the address register to the first location in the text
9064      section here, but that didn't accomplish anything since we already
9065      have a line note for the opening brace of the first function.  */
9066
9067   /* Generate the line number to PC correspondence table, encoded as
9068      a series of state machine operations.  */
9069   current_file = 1;
9070   current_line = 1;
9071
9072   if (cfun && in_cold_section_p)
9073     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9074   else
9075     strcpy (prev_line_label, text_section_label);
9076   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9077     {
9078       dw_line_info_ref line_info = &line_info_table[lt_index];
9079
9080 #if 0
9081       /* Disable this optimization for now; GDB wants to see two line notes
9082          at the beginning of a function so it can find the end of the
9083          prologue.  */
9084
9085       /* Don't emit anything for redundant notes.  Just updating the
9086          address doesn't accomplish anything, because we already assume
9087          that anything after the last address is this line.  */
9088       if (line_info->dw_line_num == current_line
9089           && line_info->dw_file_num == current_file)
9090         continue;
9091 #endif
9092
9093       /* Emit debug info for the address of the current line.
9094
9095          Unfortunately, we have little choice here currently, and must always
9096          use the most general form.  GCC does not know the address delta
9097          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9098          attributes which will give an upper bound on the address range.  We
9099          could perhaps use length attributes to determine when it is safe to
9100          use DW_LNS_fixed_advance_pc.  */
9101
9102       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9103       if (0)
9104         {
9105           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9106           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9107                                "DW_LNS_fixed_advance_pc");
9108           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9109         }
9110       else
9111         {
9112           /* This can handle any delta.  This takes
9113              4+DWARF2_ADDR_SIZE bytes.  */
9114           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9115           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9116           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9117           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9118         }
9119
9120       strcpy (prev_line_label, line_label);
9121
9122       /* Emit debug info for the source file of the current line, if
9123          different from the previous line.  */
9124       if (line_info->dw_file_num != current_file)
9125         {
9126           current_file = line_info->dw_file_num;
9127           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9128           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9129         }
9130
9131       /* Emit debug info for the current line number, choosing the encoding
9132          that uses the least amount of space.  */
9133       if (line_info->dw_line_num != current_line)
9134         {
9135           line_offset = line_info->dw_line_num - current_line;
9136           line_delta = line_offset - DWARF_LINE_BASE;
9137           current_line = line_info->dw_line_num;
9138           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9139             /* This can handle deltas from -10 to 234, using the current
9140                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9141                takes 1 byte.  */
9142             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9143                                  "line %lu", current_line);
9144           else
9145             {
9146               /* This can handle any delta.  This takes at least 4 bytes,
9147                  depending on the value being encoded.  */
9148               dw2_asm_output_data (1, DW_LNS_advance_line,
9149                                    "advance to line %lu", current_line);
9150               dw2_asm_output_data_sleb128 (line_offset, NULL);
9151               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9152             }
9153         }
9154       else
9155         /* We still need to start a new row, so output a copy insn.  */
9156         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9157     }
9158
9159   /* Emit debug info for the address of the end of the function.  */
9160   if (0)
9161     {
9162       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9163                            "DW_LNS_fixed_advance_pc");
9164       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9165     }
9166   else
9167     {
9168       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9169       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9170       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9171       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9172     }
9173
9174   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9175   dw2_asm_output_data_uleb128 (1, NULL);
9176   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9177
9178   function = 0;
9179   current_file = 1;
9180   current_line = 1;
9181   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9182     {
9183       dw_separate_line_info_ref line_info
9184         = &separate_line_info_table[lt_index];
9185
9186 #if 0
9187       /* Don't emit anything for redundant notes.  */
9188       if (line_info->dw_line_num == current_line
9189           && line_info->dw_file_num == current_file
9190           && line_info->function == function)
9191         goto cont;
9192 #endif
9193
9194       /* Emit debug info for the address of the current line.  If this is
9195          a new function, or the first line of a function, then we need
9196          to handle it differently.  */
9197       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9198                                    lt_index);
9199       if (function != line_info->function)
9200         {
9201           function = line_info->function;
9202
9203           /* Set the address register to the first line in the function.  */
9204           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9205           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9206           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9207           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9208         }
9209       else
9210         {
9211           /* ??? See the DW_LNS_advance_pc comment above.  */
9212           if (0)
9213             {
9214               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9215                                    "DW_LNS_fixed_advance_pc");
9216               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9217             }
9218           else
9219             {
9220               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9221               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9222               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9223               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9224             }
9225         }
9226
9227       strcpy (prev_line_label, line_label);
9228
9229       /* Emit debug info for the source file of the current line, if
9230          different from the previous line.  */
9231       if (line_info->dw_file_num != current_file)
9232         {
9233           current_file = line_info->dw_file_num;
9234           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9235           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9236         }
9237
9238       /* Emit debug info for the current line number, choosing the encoding
9239          that uses the least amount of space.  */
9240       if (line_info->dw_line_num != current_line)
9241         {
9242           line_offset = line_info->dw_line_num - current_line;
9243           line_delta = line_offset - DWARF_LINE_BASE;
9244           current_line = line_info->dw_line_num;
9245           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9246             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9247                                  "line %lu", current_line);
9248           else
9249             {
9250               dw2_asm_output_data (1, DW_LNS_advance_line,
9251                                    "advance to line %lu", current_line);
9252               dw2_asm_output_data_sleb128 (line_offset, NULL);
9253               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9254             }
9255         }
9256       else
9257         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9258
9259 #if 0
9260     cont:
9261 #endif
9262
9263       lt_index++;
9264
9265       /* If we're done with a function, end its sequence.  */
9266       if (lt_index == separate_line_info_table_in_use
9267           || separate_line_info_table[lt_index].function != function)
9268         {
9269           current_file = 1;
9270           current_line = 1;
9271
9272           /* Emit debug info for the address of the end of the function.  */
9273           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9274           if (0)
9275             {
9276               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9277                                    "DW_LNS_fixed_advance_pc");
9278               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9279             }
9280           else
9281             {
9282               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9283               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9284               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9285               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9286             }
9287
9288           /* Output the marker for the end of this sequence.  */
9289           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9290           dw2_asm_output_data_uleb128 (1, NULL);
9291           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9292         }
9293     }
9294
9295   /* Output the marker for the end of the line number info.  */
9296   ASM_OUTPUT_LABEL (asm_out_file, l2);
9297 }
9298 \f
9299 /* Given a pointer to a tree node for some base type, return a pointer to
9300    a DIE that describes the given type.
9301
9302    This routine must only be called for GCC type nodes that correspond to
9303    Dwarf base (fundamental) types.  */
9304
9305 static dw_die_ref
9306 base_type_die (tree type)
9307 {
9308   dw_die_ref base_type_result;
9309   enum dwarf_type encoding;
9310
9311   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9312     return 0;
9313
9314   switch (TREE_CODE (type))
9315     {
9316     case INTEGER_TYPE:
9317       if (TYPE_STRING_FLAG (type))
9318         {
9319           if (TYPE_UNSIGNED (type))
9320             encoding = DW_ATE_unsigned_char;
9321           else
9322             encoding = DW_ATE_signed_char;
9323         }
9324       else if (TYPE_UNSIGNED (type))
9325         encoding = DW_ATE_unsigned;
9326       else
9327         encoding = DW_ATE_signed;
9328       break;
9329
9330     case REAL_TYPE:
9331       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9332         encoding = DW_ATE_decimal_float;
9333       else
9334         encoding = DW_ATE_float;
9335       break;
9336
9337     case FIXED_POINT_TYPE:
9338       if (TYPE_UNSIGNED (type))
9339         encoding = DW_ATE_unsigned_fixed;
9340       else
9341         encoding = DW_ATE_signed_fixed;
9342       break;
9343
9344       /* Dwarf2 doesn't know anything about complex ints, so use
9345          a user defined type for it.  */
9346     case COMPLEX_TYPE:
9347       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9348         encoding = DW_ATE_complex_float;
9349       else
9350         encoding = DW_ATE_lo_user;
9351       break;
9352
9353     case BOOLEAN_TYPE:
9354       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9355       encoding = DW_ATE_boolean;
9356       break;
9357
9358     default:
9359       /* No other TREE_CODEs are Dwarf fundamental types.  */
9360       gcc_unreachable ();
9361     }
9362
9363   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9364
9365   /* This probably indicates a bug.  */
9366   if (! TYPE_NAME (type))
9367     add_name_attribute (base_type_result, "__unknown__");
9368
9369   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9370                    int_size_in_bytes (type));
9371   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9372
9373   return base_type_result;
9374 }
9375
9376 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9377    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9378
9379 static inline int
9380 is_base_type (tree type)
9381 {
9382   switch (TREE_CODE (type))
9383     {
9384     case ERROR_MARK:
9385     case VOID_TYPE:
9386     case INTEGER_TYPE:
9387     case REAL_TYPE:
9388     case FIXED_POINT_TYPE:
9389     case COMPLEX_TYPE:
9390     case BOOLEAN_TYPE:
9391       return 1;
9392
9393     case ARRAY_TYPE:
9394     case RECORD_TYPE:
9395     case UNION_TYPE:
9396     case QUAL_UNION_TYPE:
9397     case ENUMERAL_TYPE:
9398     case FUNCTION_TYPE:
9399     case METHOD_TYPE:
9400     case POINTER_TYPE:
9401     case REFERENCE_TYPE:
9402     case OFFSET_TYPE:
9403     case LANG_TYPE:
9404     case VECTOR_TYPE:
9405       return 0;
9406
9407     default:
9408       gcc_unreachable ();
9409     }
9410
9411   return 0;
9412 }
9413
9414 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9415    node, return the size in bits for the type if it is a constant, or else
9416    return the alignment for the type if the type's size is not constant, or
9417    else return BITS_PER_WORD if the type actually turns out to be an
9418    ERROR_MARK node.  */
9419
9420 static inline unsigned HOST_WIDE_INT
9421 simple_type_size_in_bits (const_tree type)
9422 {
9423   if (TREE_CODE (type) == ERROR_MARK)
9424     return BITS_PER_WORD;
9425   else if (TYPE_SIZE (type) == NULL_TREE)
9426     return 0;
9427   else if (host_integerp (TYPE_SIZE (type), 1))
9428     return tree_low_cst (TYPE_SIZE (type), 1);
9429   else
9430     return TYPE_ALIGN (type);
9431 }
9432
9433 /* Return true if the debug information for the given type should be
9434    emitted as a subrange type.  */
9435
9436 static inline bool
9437 is_subrange_type (const_tree type)
9438 {
9439   tree subtype = TREE_TYPE (type);
9440
9441   /* Subrange types are identified by the fact that they are integer
9442      types, and that they have a subtype which is either an integer type
9443      or an enumeral type.  */
9444
9445   if (TREE_CODE (type) != INTEGER_TYPE
9446       || subtype == NULL_TREE)
9447     return false;
9448
9449   if (TREE_CODE (subtype) != INTEGER_TYPE
9450       && TREE_CODE (subtype) != ENUMERAL_TYPE
9451       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9452     return false;
9453
9454   if (TREE_CODE (type) == TREE_CODE (subtype)
9455       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9456       && TYPE_MIN_VALUE (type) != NULL
9457       && TYPE_MIN_VALUE (subtype) != NULL
9458       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9459       && TYPE_MAX_VALUE (type) != NULL
9460       && TYPE_MAX_VALUE (subtype) != NULL
9461       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9462     {
9463       /* The type and its subtype have the same representation.  If in
9464          addition the two types also have the same name, then the given
9465          type is not a subrange type, but rather a plain base type.  */
9466       /* FIXME: brobecker/2004-03-22:
9467          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9468          therefore be sufficient to check the TYPE_SIZE node pointers
9469          rather than checking the actual size.  Unfortunately, we have
9470          found some cases, such as in the Ada "integer" type, where
9471          this is not the case.  Until this problem is solved, we need to
9472          keep checking the actual size.  */
9473       tree type_name = TYPE_NAME (type);
9474       tree subtype_name = TYPE_NAME (subtype);
9475
9476       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9477         type_name = DECL_NAME (type_name);
9478
9479       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9480         subtype_name = DECL_NAME (subtype_name);
9481
9482       if (type_name == subtype_name)
9483         return false;
9484     }
9485
9486   return true;
9487 }
9488
9489 /*  Given a pointer to a tree node for a subrange type, return a pointer
9490     to a DIE that describes the given type.  */
9491
9492 static dw_die_ref
9493 subrange_type_die (tree type, dw_die_ref context_die)
9494 {
9495   dw_die_ref subrange_die;
9496   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9497
9498   if (context_die == NULL)
9499     context_die = comp_unit_die;
9500
9501   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9502
9503   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9504     {
9505       /* The size of the subrange type and its base type do not match,
9506          so we need to generate a size attribute for the subrange type.  */
9507       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9508     }
9509
9510   if (TYPE_MIN_VALUE (type) != NULL)
9511     add_bound_info (subrange_die, DW_AT_lower_bound,
9512                     TYPE_MIN_VALUE (type));
9513   if (TYPE_MAX_VALUE (type) != NULL)
9514     add_bound_info (subrange_die, DW_AT_upper_bound,
9515                     TYPE_MAX_VALUE (type));
9516
9517   return subrange_die;
9518 }
9519
9520 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9521    entry that chains various modifiers in front of the given type.  */
9522
9523 static dw_die_ref
9524 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9525                    dw_die_ref context_die)
9526 {
9527   enum tree_code code = TREE_CODE (type);
9528   dw_die_ref mod_type_die;
9529   dw_die_ref sub_die = NULL;
9530   tree item_type = NULL;
9531   tree qualified_type;
9532   tree name;
9533
9534   if (code == ERROR_MARK)
9535     return NULL;
9536
9537   /* See if we already have the appropriately qualified variant of
9538      this type.  */
9539   qualified_type
9540     = get_qualified_type (type,
9541                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9542                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9543
9544   /* If we do, then we can just use its DIE, if it exists.  */
9545   if (qualified_type)
9546     {
9547       mod_type_die = lookup_type_die (qualified_type);
9548       if (mod_type_die)
9549         return mod_type_die;
9550     }
9551
9552   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9553
9554   /* Handle C typedef types.  */
9555   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9556     {
9557       tree dtype = TREE_TYPE (name);
9558
9559       if (qualified_type == dtype)
9560         {
9561           /* For a named type, use the typedef.  */
9562           gen_type_die (qualified_type, context_die);
9563           return lookup_type_die (qualified_type);
9564         }
9565       else if (is_const_type < TYPE_READONLY (dtype)
9566                || is_volatile_type < TYPE_VOLATILE (dtype)
9567                || (is_const_type <= TYPE_READONLY (dtype)
9568                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9569                    && DECL_ORIGINAL_TYPE (name) != type))
9570         /* cv-unqualified version of named type.  Just use the unnamed
9571            type to which it refers.  */
9572         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9573                                   is_const_type, is_volatile_type,
9574                                   context_die);
9575       /* Else cv-qualified version of named type; fall through.  */
9576     }
9577
9578   if (is_const_type)
9579     {
9580       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9581       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9582     }
9583   else if (is_volatile_type)
9584     {
9585       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9586       sub_die = modified_type_die (type, 0, 0, context_die);
9587     }
9588   else if (code == POINTER_TYPE)
9589     {
9590       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9591       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9592                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9593       item_type = TREE_TYPE (type);
9594     }
9595   else if (code == REFERENCE_TYPE)
9596     {
9597       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9598       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9599                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9600       item_type = TREE_TYPE (type);
9601     }
9602   else if (is_subrange_type (type))
9603     {
9604       mod_type_die = subrange_type_die (type, context_die);
9605       item_type = TREE_TYPE (type);
9606     }
9607   else if (is_base_type (type))
9608     mod_type_die = base_type_die (type);
9609   else
9610     {
9611       gen_type_die (type, context_die);
9612
9613       /* We have to get the type_main_variant here (and pass that to the
9614          `lookup_type_die' routine) because the ..._TYPE node we have
9615          might simply be a *copy* of some original type node (where the
9616          copy was created to help us keep track of typedef names) and
9617          that copy might have a different TYPE_UID from the original
9618          ..._TYPE node.  */
9619       if (TREE_CODE (type) != VECTOR_TYPE)
9620         return lookup_type_die (type_main_variant (type));
9621       else
9622         /* Vectors have the debugging information in the type,
9623            not the main variant.  */
9624         return lookup_type_die (type);
9625     }
9626
9627   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9628      don't output a DW_TAG_typedef, since there isn't one in the
9629      user's program; just attach a DW_AT_name to the type.  */
9630   if (name
9631       && (TREE_CODE (name) != TYPE_DECL
9632           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9633     {
9634       if (TREE_CODE (name) == TYPE_DECL)
9635         /* Could just call add_name_and_src_coords_attributes here,
9636            but since this is a builtin type it doesn't have any
9637            useful source coordinates anyway.  */
9638         name = DECL_NAME (name);
9639       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9640     }
9641
9642   if (qualified_type)
9643     equate_type_number_to_die (qualified_type, mod_type_die);
9644
9645   if (item_type)
9646     /* We must do this after the equate_type_number_to_die call, in case
9647        this is a recursive type.  This ensures that the modified_type_die
9648        recursion will terminate even if the type is recursive.  Recursive
9649        types are possible in Ada.  */
9650     sub_die = modified_type_die (item_type,
9651                                  TYPE_READONLY (item_type),
9652                                  TYPE_VOLATILE (item_type),
9653                                  context_die);
9654
9655   if (sub_die != NULL)
9656     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9657
9658   return mod_type_die;
9659 }
9660
9661 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9662    an enumerated type.  */
9663
9664 static inline int
9665 type_is_enum (const_tree type)
9666 {
9667   return TREE_CODE (type) == ENUMERAL_TYPE;
9668 }
9669
9670 /* Return the DBX register number described by a given RTL node.  */
9671
9672 static unsigned int
9673 dbx_reg_number (const_rtx rtl)
9674 {
9675   unsigned regno = REGNO (rtl);
9676
9677   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9678
9679 #ifdef LEAF_REG_REMAP
9680   if (current_function_uses_only_leaf_regs)
9681     {
9682       int leaf_reg = LEAF_REG_REMAP (regno);
9683       if (leaf_reg != -1)
9684         regno = (unsigned) leaf_reg;
9685     }
9686 #endif
9687
9688   return DBX_REGISTER_NUMBER (regno);
9689 }
9690
9691 /* Optionally add a DW_OP_piece term to a location description expression.
9692    DW_OP_piece is only added if the location description expression already
9693    doesn't end with DW_OP_piece.  */
9694
9695 static void
9696 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9697 {
9698   dw_loc_descr_ref loc;
9699
9700   if (*list_head != NULL)
9701     {
9702       /* Find the end of the chain.  */
9703       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9704         ;
9705
9706       if (loc->dw_loc_opc != DW_OP_piece)
9707         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9708     }
9709 }
9710
9711 /* Return a location descriptor that designates a machine register or
9712    zero if there is none.  */
9713
9714 static dw_loc_descr_ref
9715 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9716 {
9717   rtx regs;
9718
9719   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9720     return 0;
9721
9722   regs = targetm.dwarf_register_span (rtl);
9723
9724   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9725     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9726   else
9727     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9728 }
9729
9730 /* Return a location descriptor that designates a machine register for
9731    a given hard register number.  */
9732
9733 static dw_loc_descr_ref
9734 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9735 {
9736   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9737
9738   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9739     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9740
9741   return reg_loc_descr;
9742 }
9743
9744 /* Given an RTL of a register, return a location descriptor that
9745    designates a value that spans more than one register.  */
9746
9747 static dw_loc_descr_ref
9748 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9749                              enum var_init_status initialized)
9750 {
9751   int nregs, size, i;
9752   unsigned reg;
9753   dw_loc_descr_ref loc_result = NULL;
9754
9755   reg = REGNO (rtl);
9756 #ifdef LEAF_REG_REMAP
9757   if (current_function_uses_only_leaf_regs)
9758     {
9759       int leaf_reg = LEAF_REG_REMAP (reg);
9760       if (leaf_reg != -1)
9761         reg = (unsigned) leaf_reg;
9762     }
9763 #endif
9764   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9765   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9766
9767   /* Simple, contiguous registers.  */
9768   if (regs == NULL_RTX)
9769     {
9770       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9771
9772       loc_result = NULL;
9773       while (nregs--)
9774         {
9775           dw_loc_descr_ref t;
9776
9777           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9778                                       VAR_INIT_STATUS_INITIALIZED);
9779           add_loc_descr (&loc_result, t);
9780           add_loc_descr_op_piece (&loc_result, size);
9781           ++reg;
9782         }
9783       return loc_result;
9784     }
9785
9786   /* Now onto stupid register sets in non contiguous locations.  */
9787
9788   gcc_assert (GET_CODE (regs) == PARALLEL);
9789
9790   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9791   loc_result = NULL;
9792
9793   for (i = 0; i < XVECLEN (regs, 0); ++i)
9794     {
9795       dw_loc_descr_ref t;
9796
9797       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9798                                   VAR_INIT_STATUS_INITIALIZED);
9799       add_loc_descr (&loc_result, t);
9800       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9801       add_loc_descr_op_piece (&loc_result, size);
9802     }
9803
9804   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9805     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9806   return loc_result;
9807 }
9808
9809 #endif /* DWARF2_DEBUGGING_INFO */
9810
9811 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9812
9813 /* Return a location descriptor that designates a constant.  */
9814
9815 static dw_loc_descr_ref
9816 int_loc_descriptor (HOST_WIDE_INT i)
9817 {
9818   enum dwarf_location_atom op;
9819
9820   /* Pick the smallest representation of a constant, rather than just
9821      defaulting to the LEB encoding.  */
9822   if (i >= 0)
9823     {
9824       if (i <= 31)
9825         op = DW_OP_lit0 + i;
9826       else if (i <= 0xff)
9827         op = DW_OP_const1u;
9828       else if (i <= 0xffff)
9829         op = DW_OP_const2u;
9830       else if (HOST_BITS_PER_WIDE_INT == 32
9831                || i <= 0xffffffff)
9832         op = DW_OP_const4u;
9833       else
9834         op = DW_OP_constu;
9835     }
9836   else
9837     {
9838       if (i >= -0x80)
9839         op = DW_OP_const1s;
9840       else if (i >= -0x8000)
9841         op = DW_OP_const2s;
9842       else if (HOST_BITS_PER_WIDE_INT == 32
9843                || i >= -0x80000000)
9844         op = DW_OP_const4s;
9845       else
9846         op = DW_OP_consts;
9847     }
9848
9849   return new_loc_descr (op, i, 0);
9850 }
9851 #endif
9852
9853 #ifdef DWARF2_DEBUGGING_INFO
9854
9855 /* Return a location descriptor that designates a base+offset location.  */
9856
9857 static dw_loc_descr_ref
9858 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9859                  enum var_init_status initialized)
9860 {
9861   unsigned int regno;
9862   dw_loc_descr_ref result;
9863   dw_fde_ref fde = current_fde ();
9864
9865   /* We only use "frame base" when we're sure we're talking about the
9866      post-prologue local stack frame.  We do this by *not* running
9867      register elimination until this point, and recognizing the special
9868      argument pointer and soft frame pointer rtx's.  */
9869   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9870     {
9871       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9872
9873       if (elim != reg)
9874         {
9875           if (GET_CODE (elim) == PLUS)
9876             {
9877               offset += INTVAL (XEXP (elim, 1));
9878               elim = XEXP (elim, 0);
9879             }
9880           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9881                        && (elim == hard_frame_pointer_rtx
9882                            || elim == stack_pointer_rtx))
9883                       || elim == (frame_pointer_needed
9884                                   ? hard_frame_pointer_rtx
9885                                   : stack_pointer_rtx));
9886
9887           /* If drap register is used to align stack, use frame
9888              pointer + offset to access stack variables.  If stack
9889              is aligned without drap, use stack pointer + offset to
9890              access stack variables.  */
9891           if (crtl->stack_realign_tried
9892               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9893               && reg == frame_pointer_rtx)
9894             {
9895               int base_reg
9896                 = DWARF_FRAME_REGNUM (cfa.indirect
9897                                       ? HARD_FRAME_POINTER_REGNUM
9898                                       : STACK_POINTER_REGNUM);
9899               return new_reg_loc_descr (base_reg, offset);
9900             }
9901
9902           offset += frame_pointer_fb_offset;
9903           return new_loc_descr (DW_OP_fbreg, offset, 0);
9904         }
9905     }
9906   else if (fde
9907            && fde->drap_reg != INVALID_REGNUM
9908            && (fde->drap_reg == REGNO (reg)
9909                || fde->vdrap_reg == REGNO (reg)))
9910     {
9911       /* Use cfa+offset to represent the location of arguments passed
9912          on stack when drap is used to align stack.  */
9913       return new_loc_descr (DW_OP_fbreg, offset, 0);
9914     }
9915
9916   regno = dbx_reg_number (reg);
9917   if (regno <= 31)
9918     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9919   else
9920     result = new_loc_descr (DW_OP_bregx, regno, offset);
9921
9922   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9923     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9924
9925   return result;
9926 }
9927
9928 /* Return true if this RTL expression describes a base+offset calculation.  */
9929
9930 static inline int
9931 is_based_loc (const_rtx rtl)
9932 {
9933   return (GET_CODE (rtl) == PLUS
9934           && ((REG_P (XEXP (rtl, 0))
9935                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9936                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9937 }
9938
9939 /* Return a descriptor that describes the concatenation of N locations
9940    used to form the address of a memory location.  */
9941
9942 static dw_loc_descr_ref
9943 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9944                             enum var_init_status initialized)
9945 {
9946   unsigned int i;
9947   dw_loc_descr_ref cc_loc_result = NULL;
9948   unsigned int n = XVECLEN (concatn, 0);
9949
9950   for (i = 0; i < n; ++i)
9951     {
9952       dw_loc_descr_ref ref;
9953       rtx x = XVECEXP (concatn, 0, i);
9954
9955       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9956       if (ref == NULL)
9957         return NULL;
9958
9959       add_loc_descr (&cc_loc_result, ref);
9960       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9961     }
9962
9963   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9964     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9965
9966   return cc_loc_result;
9967 }
9968
9969 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9970    failed.  */
9971
9972 static dw_loc_descr_ref
9973 tls_mem_loc_descriptor (rtx mem)
9974 {
9975   tree base;
9976   dw_loc_descr_ref loc_result, loc_result2;
9977
9978   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9979     return NULL;
9980
9981   base = get_base_address (MEM_EXPR (mem));
9982   if (base == NULL
9983       || TREE_CODE (base) != VAR_DECL
9984       || !DECL_THREAD_LOCAL_P (base))
9985     return NULL;
9986
9987   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9988   if (loc_result == NULL)
9989     return NULL;
9990
9991   if (INTVAL (MEM_OFFSET (mem)))
9992     {
9993       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9994         add_loc_descr (&loc_result,
9995                        new_loc_descr (DW_OP_plus_uconst,
9996                                       INTVAL (MEM_OFFSET (mem)), 0));
9997       else
9998         {
9999           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
10000                                             VAR_INIT_STATUS_INITIALIZED);
10001           if (loc_result2 == 0)
10002             return NULL;
10003           add_loc_descr (&loc_result, loc_result2);
10004           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
10005         }
10006     }
10007
10008   return loc_result;
10009 }
10010
10011 /* The following routine converts the RTL for a variable or parameter
10012    (resident in memory) into an equivalent Dwarf representation of a
10013    mechanism for getting the address of that same variable onto the top of a
10014    hypothetical "address evaluation" stack.
10015
10016    When creating memory location descriptors, we are effectively transforming
10017    the RTL for a memory-resident object into its Dwarf postfix expression
10018    equivalent.  This routine recursively descends an RTL tree, turning
10019    it into Dwarf postfix code as it goes.
10020
10021    MODE is the mode of the memory reference, needed to handle some
10022    autoincrement addressing modes.
10023
10024    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10025    location list for RTL.
10026
10027    Return 0 if we can't represent the location.  */
10028
10029 static dw_loc_descr_ref
10030 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10031                     enum var_init_status initialized)
10032 {
10033   dw_loc_descr_ref mem_loc_result = NULL;
10034   enum dwarf_location_atom op;
10035
10036   /* Note that for a dynamically sized array, the location we will generate a
10037      description of here will be the lowest numbered location which is
10038      actually within the array.  That's *not* necessarily the same as the
10039      zeroth element of the array.  */
10040
10041   rtl = targetm.delegitimize_address (rtl);
10042
10043   switch (GET_CODE (rtl))
10044     {
10045     case POST_INC:
10046     case POST_DEC:
10047     case POST_MODIFY:
10048       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10049          just fall into the SUBREG code.  */
10050
10051       /* ... fall through ...  */
10052
10053     case SUBREG:
10054       /* The case of a subreg may arise when we have a local (register)
10055          variable or a formal (register) parameter which doesn't quite fill
10056          up an entire register.  For now, just assume that it is
10057          legitimate to make the Dwarf info refer to the whole register which
10058          contains the given subreg.  */
10059       rtl = XEXP (rtl, 0);
10060
10061       /* ... fall through ...  */
10062
10063     case REG:
10064       /* Whenever a register number forms a part of the description of the
10065          method for calculating the (dynamic) address of a memory resident
10066          object, DWARF rules require the register number be referred to as
10067          a "base register".  This distinction is not based in any way upon
10068          what category of register the hardware believes the given register
10069          belongs to.  This is strictly DWARF terminology we're dealing with
10070          here. Note that in cases where the location of a memory-resident
10071          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10072          OP_CONST (0)) the actual DWARF location descriptor that we generate
10073          may just be OP_BASEREG (basereg).  This may look deceptively like
10074          the object in question was allocated to a register (rather than in
10075          memory) so DWARF consumers need to be aware of the subtle
10076          distinction between OP_REG and OP_BASEREG.  */
10077       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10078         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10079       else if (stack_realign_drap
10080                && crtl->drap_reg
10081                && crtl->args.internal_arg_pointer == rtl
10082                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10083         {
10084           /* If RTL is internal_arg_pointer, which has been optimized
10085              out, use DRAP instead.  */
10086           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10087                                             VAR_INIT_STATUS_INITIALIZED);
10088         }
10089       break;
10090
10091     case MEM:
10092       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10093                                            VAR_INIT_STATUS_INITIALIZED);
10094       if (mem_loc_result == NULL)
10095         mem_loc_result = tls_mem_loc_descriptor (rtl);
10096       if (mem_loc_result != 0)
10097         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10098       break;
10099
10100     case LO_SUM:
10101          rtl = XEXP (rtl, 1);
10102
10103       /* ... fall through ...  */
10104
10105     case LABEL_REF:
10106       /* Some ports can transform a symbol ref into a label ref, because
10107          the symbol ref is too far away and has to be dumped into a constant
10108          pool.  */
10109     case CONST:
10110     case SYMBOL_REF:
10111       /* Alternatively, the symbol in the constant pool might be referenced
10112          by a different symbol.  */
10113       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10114         {
10115           bool marked;
10116           rtx tmp = get_pool_constant_mark (rtl, &marked);
10117
10118           if (GET_CODE (tmp) == SYMBOL_REF)
10119             {
10120               rtl = tmp;
10121               if (CONSTANT_POOL_ADDRESS_P (tmp))
10122                 get_pool_constant_mark (tmp, &marked);
10123               else
10124                 marked = true;
10125             }
10126
10127           /* If all references to this pool constant were optimized away,
10128              it was not output and thus we can't represent it.
10129              FIXME: might try to use DW_OP_const_value here, though
10130              DW_OP_piece complicates it.  */
10131           if (!marked)
10132             return 0;
10133         }
10134
10135       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10136       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10137       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10138       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10139       break;
10140
10141     case PRE_MODIFY:
10142       /* Extract the PLUS expression nested inside and fall into
10143          PLUS code below.  */
10144       rtl = XEXP (rtl, 1);
10145       goto plus;
10146
10147     case PRE_INC:
10148     case PRE_DEC:
10149       /* Turn these into a PLUS expression and fall into the PLUS code
10150          below.  */
10151       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10152                           GEN_INT (GET_CODE (rtl) == PRE_INC
10153                                    ? GET_MODE_UNIT_SIZE (mode)
10154                                    : -GET_MODE_UNIT_SIZE (mode)));
10155
10156       /* ... fall through ...  */
10157
10158     case PLUS:
10159     plus:
10160       if (is_based_loc (rtl))
10161         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10162                                           INTVAL (XEXP (rtl, 1)),
10163                                           VAR_INIT_STATUS_INITIALIZED);
10164       else
10165         {
10166           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10167                                                VAR_INIT_STATUS_INITIALIZED);
10168           if (mem_loc_result == 0)
10169             break;
10170
10171           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10172               && INTVAL (XEXP (rtl, 1)) >= 0)
10173             add_loc_descr (&mem_loc_result,
10174                            new_loc_descr (DW_OP_plus_uconst,
10175                                           INTVAL (XEXP (rtl, 1)), 0));
10176           else
10177             {
10178               dw_loc_descr_ref mem_loc_result2
10179                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10180                                       VAR_INIT_STATUS_INITIALIZED);
10181               if (mem_loc_result2 == 0)
10182                 break;
10183               add_loc_descr (&mem_loc_result, mem_loc_result2);
10184               add_loc_descr (&mem_loc_result,
10185                              new_loc_descr (DW_OP_plus, 0, 0));
10186             }
10187         }
10188       break;
10189
10190     /* If a pseudo-reg is optimized away, it is possible for it to
10191        be replaced with a MEM containing a multiply or shift.  */
10192     case MULT:
10193       op = DW_OP_mul;
10194       goto do_binop;
10195
10196     case ASHIFT:
10197       op = DW_OP_shl;
10198       goto do_binop;
10199
10200     case ASHIFTRT:
10201       op = DW_OP_shra;
10202       goto do_binop;
10203
10204     case LSHIFTRT:
10205       op = DW_OP_shr;
10206       goto do_binop;
10207
10208     do_binop:
10209       {
10210         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10211                                                    VAR_INIT_STATUS_INITIALIZED);
10212         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10213                                                    VAR_INIT_STATUS_INITIALIZED);
10214
10215         if (op0 == 0 || op1 == 0)
10216           break;
10217
10218         mem_loc_result = op0;
10219         add_loc_descr (&mem_loc_result, op1);
10220         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10221         break;
10222       }
10223
10224     case CONST_INT:
10225       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10226       break;
10227
10228     case CONCATN:
10229       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10230                                                    VAR_INIT_STATUS_INITIALIZED);
10231       break;
10232
10233     case UNSPEC:
10234       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10235          can't express it in the debug info.  This can happen e.g. with some
10236          TLS UNSPECs.  */
10237       break;
10238
10239     default:
10240       gcc_unreachable ();
10241     }
10242
10243   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10244     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10245
10246   return mem_loc_result;
10247 }
10248
10249 /* Return a descriptor that describes the concatenation of two locations.
10250    This is typically a complex variable.  */
10251
10252 static dw_loc_descr_ref
10253 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10254 {
10255   dw_loc_descr_ref cc_loc_result = NULL;
10256   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10257   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10258
10259   if (x0_ref == 0 || x1_ref == 0)
10260     return 0;
10261
10262   cc_loc_result = x0_ref;
10263   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10264
10265   add_loc_descr (&cc_loc_result, x1_ref);
10266   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10267
10268   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10269     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10270
10271   return cc_loc_result;
10272 }
10273
10274 /* Return a descriptor that describes the concatenation of N
10275    locations.  */
10276
10277 static dw_loc_descr_ref
10278 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10279 {
10280   unsigned int i;
10281   dw_loc_descr_ref cc_loc_result = NULL;
10282   unsigned int n = XVECLEN (concatn, 0);
10283
10284   for (i = 0; i < n; ++i)
10285     {
10286       dw_loc_descr_ref ref;
10287       rtx x = XVECEXP (concatn, 0, i);
10288
10289       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10290       if (ref == NULL)
10291         return NULL;
10292
10293       add_loc_descr (&cc_loc_result, ref);
10294       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10295     }
10296
10297   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10298     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10299
10300   return cc_loc_result;
10301 }
10302
10303 /* Output a proper Dwarf location descriptor for a variable or parameter
10304    which is either allocated in a register or in a memory location.  For a
10305    register, we just generate an OP_REG and the register number.  For a
10306    memory location we provide a Dwarf postfix expression describing how to
10307    generate the (dynamic) address of the object onto the address stack.
10308
10309    If we don't know how to describe it, return 0.  */
10310
10311 static dw_loc_descr_ref
10312 loc_descriptor (rtx rtl, enum var_init_status initialized)
10313 {
10314   dw_loc_descr_ref loc_result = NULL;
10315
10316   switch (GET_CODE (rtl))
10317     {
10318     case SUBREG:
10319       /* The case of a subreg may arise when we have a local (register)
10320          variable or a formal (register) parameter which doesn't quite fill
10321          up an entire register.  For now, just assume that it is
10322          legitimate to make the Dwarf info refer to the whole register which
10323          contains the given subreg.  */
10324       rtl = SUBREG_REG (rtl);
10325
10326       /* ... fall through ...  */
10327
10328     case REG:
10329       loc_result = reg_loc_descriptor (rtl, initialized);
10330       break;
10331
10332     case MEM:
10333       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10334                                        initialized);
10335       if (loc_result == NULL)
10336         loc_result = tls_mem_loc_descriptor (rtl);
10337       break;
10338
10339     case CONCAT:
10340       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10341                                           initialized);
10342       break;
10343
10344     case CONCATN:
10345       loc_result = concatn_loc_descriptor (rtl, initialized);
10346       break;
10347
10348     case VAR_LOCATION:
10349       /* Single part.  */
10350       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10351         {
10352           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10353           break;
10354         }
10355
10356       rtl = XEXP (rtl, 1);
10357       /* FALLTHRU */
10358
10359     case PARALLEL:
10360       {
10361         rtvec par_elems = XVEC (rtl, 0);
10362         int num_elem = GET_NUM_ELEM (par_elems);
10363         enum machine_mode mode;
10364         int i;
10365
10366         /* Create the first one, so we have something to add to.  */
10367         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10368                                      initialized);
10369         if (loc_result == NULL)
10370           return NULL;
10371         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10372         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10373         for (i = 1; i < num_elem; i++)
10374           {
10375             dw_loc_descr_ref temp;
10376
10377             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10378                                    initialized);
10379             if (temp == NULL)
10380               return NULL;
10381             add_loc_descr (&loc_result, temp);
10382             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10383             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10384           }
10385       }
10386       break;
10387
10388     default:
10389       gcc_unreachable ();
10390     }
10391
10392   return loc_result;
10393 }
10394
10395 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10396    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10397    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10398    top-level invocation, and we require the address of LOC; is 0 if we require
10399    the value of LOC.  */
10400
10401 static dw_loc_descr_ref
10402 loc_descriptor_from_tree_1 (tree loc, int want_address)
10403 {
10404   dw_loc_descr_ref ret, ret1;
10405   int have_address = 0;
10406   enum dwarf_location_atom op;
10407
10408   /* ??? Most of the time we do not take proper care for sign/zero
10409      extending the values properly.  Hopefully this won't be a real
10410      problem...  */
10411
10412   switch (TREE_CODE (loc))
10413     {
10414     case ERROR_MARK:
10415       return 0;
10416
10417     case PLACEHOLDER_EXPR:
10418       /* This case involves extracting fields from an object to determine the
10419          position of other fields.  We don't try to encode this here.  The
10420          only user of this is Ada, which encodes the needed information using
10421          the names of types.  */
10422       return 0;
10423
10424     case CALL_EXPR:
10425       return 0;
10426
10427     case PREINCREMENT_EXPR:
10428     case PREDECREMENT_EXPR:
10429     case POSTINCREMENT_EXPR:
10430     case POSTDECREMENT_EXPR:
10431       /* There are no opcodes for these operations.  */
10432       return 0;
10433
10434     case ADDR_EXPR:
10435       /* If we already want an address, there's nothing we can do.  */
10436       if (want_address)
10437         return 0;
10438
10439       /* Otherwise, process the argument and look for the address.  */
10440       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10441
10442     case VAR_DECL:
10443       if (DECL_THREAD_LOCAL_P (loc))
10444         {
10445           rtx rtl;
10446           unsigned first_op;
10447           unsigned second_op;
10448
10449           if (targetm.have_tls)
10450             {
10451               /* If this is not defined, we have no way to emit the
10452                  data.  */
10453               if (!targetm.asm_out.output_dwarf_dtprel)
10454                 return 0;
10455
10456                /* The way DW_OP_GNU_push_tls_address is specified, we
10457                   can only look up addresses of objects in the current
10458                   module.  */
10459               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10460                 return 0;
10461               first_op = INTERNAL_DW_OP_tls_addr;
10462               second_op = DW_OP_GNU_push_tls_address;
10463             }
10464           else
10465             {
10466               if (!targetm.emutls.debug_form_tls_address)
10467                 return 0;
10468               loc = emutls_decl (loc);
10469               first_op = DW_OP_addr;
10470               second_op = DW_OP_form_tls_address;
10471             }
10472
10473           rtl = rtl_for_decl_location (loc);
10474           if (rtl == NULL_RTX)
10475             return 0;
10476
10477           if (!MEM_P (rtl))
10478             return 0;
10479           rtl = XEXP (rtl, 0);
10480           if (! CONSTANT_P (rtl))
10481             return 0;
10482
10483           ret = new_loc_descr (first_op, 0, 0);
10484           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10485           ret->dw_loc_oprnd1.v.val_addr = rtl;
10486
10487           ret1 = new_loc_descr (second_op, 0, 0);
10488           add_loc_descr (&ret, ret1);
10489
10490           have_address = 1;
10491           break;
10492         }
10493       /* FALLTHRU */
10494
10495     case PARM_DECL:
10496       if (DECL_HAS_VALUE_EXPR_P (loc))
10497         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10498                                            want_address);
10499       /* FALLTHRU */
10500
10501     case RESULT_DECL:
10502     case FUNCTION_DECL:
10503       {
10504         rtx rtl = rtl_for_decl_location (loc);
10505
10506         if (rtl == NULL_RTX)
10507           return 0;
10508         else if (GET_CODE (rtl) == CONST_INT)
10509           {
10510             HOST_WIDE_INT val = INTVAL (rtl);
10511             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10512               val &= GET_MODE_MASK (DECL_MODE (loc));
10513             ret = int_loc_descriptor (val);
10514           }
10515         else if (GET_CODE (rtl) == CONST_STRING)
10516           return 0;
10517         else if (CONSTANT_P (rtl))
10518           {
10519             ret = new_loc_descr (DW_OP_addr, 0, 0);
10520             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10521             ret->dw_loc_oprnd1.v.val_addr = rtl;
10522           }
10523         else
10524           {
10525             enum machine_mode mode;
10526
10527             /* Certain constructs can only be represented at top-level.  */
10528             if (want_address == 2)
10529               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10530
10531             mode = GET_MODE (rtl);
10532             if (MEM_P (rtl))
10533               {
10534                 rtl = XEXP (rtl, 0);
10535                 have_address = 1;
10536               }
10537             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10538           }
10539       }
10540       break;
10541
10542     case INDIRECT_REF:
10543       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10544       have_address = 1;
10545       break;
10546
10547     case COMPOUND_EXPR:
10548       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10549
10550     CASE_CONVERT:
10551     case VIEW_CONVERT_EXPR:
10552     case SAVE_EXPR:
10553     case MODIFY_EXPR:
10554       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10555
10556     case COMPONENT_REF:
10557     case BIT_FIELD_REF:
10558     case ARRAY_REF:
10559     case ARRAY_RANGE_REF:
10560       {
10561         tree obj, offset;
10562         HOST_WIDE_INT bitsize, bitpos, bytepos;
10563         enum machine_mode mode;
10564         int volatilep;
10565         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10566
10567         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10568                                    &unsignedp, &volatilep, false);
10569
10570         if (obj == loc)
10571           return 0;
10572
10573         ret = loc_descriptor_from_tree_1 (obj, 1);
10574         if (ret == 0
10575             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10576           return 0;
10577
10578         if (offset != NULL_TREE)
10579           {
10580             /* Variable offset.  */
10581             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10582             if (ret1 == 0)
10583               return 0;
10584             add_loc_descr (&ret, ret1);
10585             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10586           }
10587
10588         bytepos = bitpos / BITS_PER_UNIT;
10589         if (bytepos > 0)
10590           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10591         else if (bytepos < 0)
10592           {
10593             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10594             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10595           }
10596
10597         have_address = 1;
10598         break;
10599       }
10600
10601     case INTEGER_CST:
10602       if (host_integerp (loc, 0))
10603         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10604       else
10605         return 0;
10606       break;
10607
10608     case CONSTRUCTOR:
10609       {
10610         /* Get an RTL for this, if something has been emitted.  */
10611         rtx rtl = lookup_constant_def (loc);
10612         enum machine_mode mode;
10613
10614         if (!rtl || !MEM_P (rtl))
10615           return 0;
10616         mode = GET_MODE (rtl);
10617         rtl = XEXP (rtl, 0);
10618         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10619         have_address = 1;
10620         break;
10621       }
10622
10623     case TRUTH_AND_EXPR:
10624     case TRUTH_ANDIF_EXPR:
10625     case BIT_AND_EXPR:
10626       op = DW_OP_and;
10627       goto do_binop;
10628
10629     case TRUTH_XOR_EXPR:
10630     case BIT_XOR_EXPR:
10631       op = DW_OP_xor;
10632       goto do_binop;
10633
10634     case TRUTH_OR_EXPR:
10635     case TRUTH_ORIF_EXPR:
10636     case BIT_IOR_EXPR:
10637       op = DW_OP_or;
10638       goto do_binop;
10639
10640     case FLOOR_DIV_EXPR:
10641     case CEIL_DIV_EXPR:
10642     case ROUND_DIV_EXPR:
10643     case TRUNC_DIV_EXPR:
10644       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10645         return 0;
10646       op = DW_OP_div;
10647       goto do_binop;
10648
10649     case MINUS_EXPR:
10650       op = DW_OP_minus;
10651       goto do_binop;
10652
10653     case FLOOR_MOD_EXPR:
10654     case CEIL_MOD_EXPR:
10655     case ROUND_MOD_EXPR:
10656     case TRUNC_MOD_EXPR:
10657       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10658         {
10659           op = DW_OP_mod;
10660           goto do_binop;
10661         }
10662       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10663       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10664       if (ret == 0 || ret1 == 0)
10665         return 0;
10666
10667       add_loc_descr (&ret, ret1);
10668       add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
10669       add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
10670       add_loc_descr (&ret, new_loc_descr (DW_OP_div, 0, 0));
10671       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10672       add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
10673       break;
10674
10675     case MULT_EXPR:
10676       op = DW_OP_mul;
10677       goto do_binop;
10678
10679     case LSHIFT_EXPR:
10680       op = DW_OP_shl;
10681       goto do_binop;
10682
10683     case RSHIFT_EXPR:
10684       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10685       goto do_binop;
10686
10687     case POINTER_PLUS_EXPR:
10688     case PLUS_EXPR:
10689       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10690           && host_integerp (TREE_OPERAND (loc, 1), 0))
10691         {
10692           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10693           if (ret == 0)
10694             return 0;
10695
10696           add_loc_descr (&ret,
10697                          new_loc_descr (DW_OP_plus_uconst,
10698                                         tree_low_cst (TREE_OPERAND (loc, 1),
10699                                                       0),
10700                                         0));
10701           break;
10702         }
10703
10704       op = DW_OP_plus;
10705       goto do_binop;
10706
10707     case LE_EXPR:
10708       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10709         return 0;
10710
10711       op = DW_OP_le;
10712       goto do_binop;
10713
10714     case GE_EXPR:
10715       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10716         return 0;
10717
10718       op = DW_OP_ge;
10719       goto do_binop;
10720
10721     case LT_EXPR:
10722       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10723         return 0;
10724
10725       op = DW_OP_lt;
10726       goto do_binop;
10727
10728     case GT_EXPR:
10729       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10730         return 0;
10731
10732       op = DW_OP_gt;
10733       goto do_binop;
10734
10735     case EQ_EXPR:
10736       op = DW_OP_eq;
10737       goto do_binop;
10738
10739     case NE_EXPR:
10740       op = DW_OP_ne;
10741       goto do_binop;
10742
10743     do_binop:
10744       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10745       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10746       if (ret == 0 || ret1 == 0)
10747         return 0;
10748
10749       add_loc_descr (&ret, ret1);
10750       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10751       break;
10752
10753     case TRUTH_NOT_EXPR:
10754     case BIT_NOT_EXPR:
10755       op = DW_OP_not;
10756       goto do_unop;
10757
10758     case ABS_EXPR:
10759       op = DW_OP_abs;
10760       goto do_unop;
10761
10762     case NEGATE_EXPR:
10763       op = DW_OP_neg;
10764       goto do_unop;
10765
10766     do_unop:
10767       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10768       if (ret == 0)
10769         return 0;
10770
10771       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10772       break;
10773
10774     case MIN_EXPR:
10775     case MAX_EXPR:
10776       {
10777         const enum tree_code code =
10778           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10779
10780         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10781                       build2 (code, integer_type_node,
10782                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10783                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10784       }
10785
10786       /* ... fall through ...  */
10787
10788     case COND_EXPR:
10789       {
10790         dw_loc_descr_ref lhs
10791           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10792         dw_loc_descr_ref rhs
10793           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10794         dw_loc_descr_ref bra_node, jump_node, tmp;
10795
10796         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10797         if (ret == 0 || lhs == 0 || rhs == 0)
10798           return 0;
10799
10800         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10801         add_loc_descr (&ret, bra_node);
10802
10803         add_loc_descr (&ret, rhs);
10804         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10805         add_loc_descr (&ret, jump_node);
10806
10807         add_loc_descr (&ret, lhs);
10808         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10809         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10810
10811         /* ??? Need a node to point the skip at.  Use a nop.  */
10812         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10813         add_loc_descr (&ret, tmp);
10814         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10815         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10816       }
10817       break;
10818
10819     case FIX_TRUNC_EXPR:
10820       return 0;
10821
10822     default:
10823       /* Leave front-end specific codes as simply unknown.  This comes
10824          up, for instance, with the C STMT_EXPR.  */
10825       if ((unsigned int) TREE_CODE (loc)
10826           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10827         return 0;
10828
10829 #ifdef ENABLE_CHECKING
10830       /* Otherwise this is a generic code; we should just lists all of
10831          these explicitly.  We forgot one.  */
10832       gcc_unreachable ();
10833 #else
10834       /* In a release build, we want to degrade gracefully: better to
10835          generate incomplete debugging information than to crash.  */
10836       return NULL;
10837 #endif
10838     }
10839
10840   /* Show if we can't fill the request for an address.  */
10841   if (want_address && !have_address)
10842     return 0;
10843
10844   /* If we've got an address and don't want one, dereference.  */
10845   if (!want_address && have_address && ret)
10846     {
10847       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10848
10849       if (size > DWARF2_ADDR_SIZE || size == -1)
10850         return 0;
10851       else if (size == DWARF2_ADDR_SIZE)
10852         op = DW_OP_deref;
10853       else
10854         op = DW_OP_deref_size;
10855
10856       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10857     }
10858
10859   return ret;
10860 }
10861
10862 static inline dw_loc_descr_ref
10863 loc_descriptor_from_tree (tree loc)
10864 {
10865   return loc_descriptor_from_tree_1 (loc, 2);
10866 }
10867
10868 /* Given a value, round it up to the lowest multiple of `boundary'
10869    which is not less than the value itself.  */
10870
10871 static inline HOST_WIDE_INT
10872 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10873 {
10874   return (((value + boundary - 1) / boundary) * boundary);
10875 }
10876
10877 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10878    pointer to the declared type for the relevant field variable, or return
10879    `integer_type_node' if the given node turns out to be an
10880    ERROR_MARK node.  */
10881
10882 static inline tree
10883 field_type (const_tree decl)
10884 {
10885   tree type;
10886
10887   if (TREE_CODE (decl) == ERROR_MARK)
10888     return integer_type_node;
10889
10890   type = DECL_BIT_FIELD_TYPE (decl);
10891   if (type == NULL_TREE)
10892     type = TREE_TYPE (decl);
10893
10894   return type;
10895 }
10896
10897 /* Given a pointer to a tree node, return the alignment in bits for
10898    it, or else return BITS_PER_WORD if the node actually turns out to
10899    be an ERROR_MARK node.  */
10900
10901 static inline unsigned
10902 simple_type_align_in_bits (const_tree type)
10903 {
10904   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10905 }
10906
10907 static inline unsigned
10908 simple_decl_align_in_bits (const_tree decl)
10909 {
10910   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10911 }
10912
10913 /* Return the result of rounding T up to ALIGN.  */
10914
10915 static inline HOST_WIDE_INT
10916 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10917 {
10918   /* We must be careful if T is negative because HOST_WIDE_INT can be
10919      either "above" or "below" unsigned int as per the C promotion
10920      rules, depending on the host, thus making the signedness of the
10921      direct multiplication and division unpredictable.  */
10922   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10923
10924   u += align - 1;
10925   u /= align;
10926   u *= align;
10927
10928   return (HOST_WIDE_INT) u;
10929 }
10930
10931 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10932    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10933    or return 0 if we are unable to determine what that offset is, either
10934    because the argument turns out to be a pointer to an ERROR_MARK node, or
10935    because the offset is actually variable.  (We can't handle the latter case
10936    just yet).  */
10937
10938 static HOST_WIDE_INT
10939 field_byte_offset (const_tree decl)
10940 {
10941   HOST_WIDE_INT object_offset_in_bits;
10942   HOST_WIDE_INT bitpos_int;
10943
10944   if (TREE_CODE (decl) == ERROR_MARK)
10945     return 0;
10946
10947   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10948
10949   /* We cannot yet cope with fields whose positions are variable, so
10950      for now, when we see such things, we simply return 0.  Someday, we may
10951      be able to handle such cases, but it will be damn difficult.  */
10952   if (! host_integerp (bit_position (decl), 0))
10953     return 0;
10954
10955   bitpos_int = int_bit_position (decl);
10956
10957 #ifdef PCC_BITFIELD_TYPE_MATTERS
10958   if (PCC_BITFIELD_TYPE_MATTERS)
10959     {
10960       tree type;
10961       tree field_size_tree;
10962       HOST_WIDE_INT deepest_bitpos;
10963       unsigned HOST_WIDE_INT field_size_in_bits;
10964       unsigned int type_align_in_bits;
10965       unsigned int decl_align_in_bits;
10966       unsigned HOST_WIDE_INT type_size_in_bits;
10967
10968       type = field_type (decl);
10969       field_size_tree = DECL_SIZE (decl);
10970
10971       /* The size could be unspecified if there was an error, or for
10972          a flexible array member.  */
10973       if (! field_size_tree)
10974         field_size_tree = bitsize_zero_node;
10975
10976       /* If we don't know the size of the field, pretend it's a full word.  */
10977       if (host_integerp (field_size_tree, 1))
10978         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10979       else
10980         field_size_in_bits = BITS_PER_WORD;
10981
10982       type_size_in_bits = simple_type_size_in_bits (type);
10983       type_align_in_bits = simple_type_align_in_bits (type);
10984       decl_align_in_bits = simple_decl_align_in_bits (decl);
10985
10986       /* The GCC front-end doesn't make any attempt to keep track of the
10987          starting bit offset (relative to the start of the containing
10988          structure type) of the hypothetical "containing object" for a
10989          bit-field.  Thus, when computing the byte offset value for the
10990          start of the "containing object" of a bit-field, we must deduce
10991          this information on our own. This can be rather tricky to do in
10992          some cases.  For example, handling the following structure type
10993          definition when compiling for an i386/i486 target (which only
10994          aligns long long's to 32-bit boundaries) can be very tricky:
10995
10996          struct S { int field1; long long field2:31; };
10997
10998          Fortunately, there is a simple rule-of-thumb which can be used
10999          in such cases.  When compiling for an i386/i486, GCC will
11000          allocate 8 bytes for the structure shown above.  It decides to
11001          do this based upon one simple rule for bit-field allocation.
11002          GCC allocates each "containing object" for each bit-field at
11003          the first (i.e. lowest addressed) legitimate alignment boundary
11004          (based upon the required minimum alignment for the declared
11005          type of the field) which it can possibly use, subject to the
11006          condition that there is still enough available space remaining
11007          in the containing object (when allocated at the selected point)
11008          to fully accommodate all of the bits of the bit-field itself.
11009
11010          This simple rule makes it obvious why GCC allocates 8 bytes for
11011          each object of the structure type shown above.  When looking
11012          for a place to allocate the "containing object" for `field2',
11013          the compiler simply tries to allocate a 64-bit "containing
11014          object" at each successive 32-bit boundary (starting at zero)
11015          until it finds a place to allocate that 64- bit field such that
11016          at least 31 contiguous (and previously unallocated) bits remain
11017          within that selected 64 bit field.  (As it turns out, for the
11018          example above, the compiler finds it is OK to allocate the
11019          "containing object" 64-bit field at bit-offset zero within the
11020          structure type.)
11021
11022          Here we attempt to work backwards from the limited set of facts
11023          we're given, and we try to deduce from those facts, where GCC
11024          must have believed that the containing object started (within
11025          the structure type). The value we deduce is then used (by the
11026          callers of this routine) to generate DW_AT_location and
11027          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11028          the case of DW_AT_location, regular fields as well).  */
11029
11030       /* Figure out the bit-distance from the start of the structure to
11031          the "deepest" bit of the bit-field.  */
11032       deepest_bitpos = bitpos_int + field_size_in_bits;
11033
11034       /* This is the tricky part.  Use some fancy footwork to deduce
11035          where the lowest addressed bit of the containing object must
11036          be.  */
11037       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11038
11039       /* Round up to type_align by default.  This works best for
11040          bitfields.  */
11041       object_offset_in_bits
11042         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11043
11044       if (object_offset_in_bits > bitpos_int)
11045         {
11046           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11047
11048           /* Round up to decl_align instead.  */
11049           object_offset_in_bits
11050             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11051         }
11052     }
11053   else
11054 #endif
11055     object_offset_in_bits = bitpos_int;
11056
11057   return object_offset_in_bits / BITS_PER_UNIT;
11058 }
11059 \f
11060 /* The following routines define various Dwarf attributes and any data
11061    associated with them.  */
11062
11063 /* Add a location description attribute value to a DIE.
11064
11065    This emits location attributes suitable for whole variables and
11066    whole parameters.  Note that the location attributes for struct fields are
11067    generated by the routine `data_member_location_attribute' below.  */
11068
11069 static inline void
11070 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11071                              dw_loc_descr_ref descr)
11072 {
11073   if (descr != 0)
11074     add_AT_loc (die, attr_kind, descr);
11075 }
11076
11077 /* Attach the specialized form of location attribute used for data members of
11078    struct and union types.  In the special case of a FIELD_DECL node which
11079    represents a bit-field, the "offset" part of this special location
11080    descriptor must indicate the distance in bytes from the lowest-addressed
11081    byte of the containing struct or union type to the lowest-addressed byte of
11082    the "containing object" for the bit-field.  (See the `field_byte_offset'
11083    function above).
11084
11085    For any given bit-field, the "containing object" is a hypothetical object
11086    (of some integral or enum type) within which the given bit-field lives.  The
11087    type of this hypothetical "containing object" is always the same as the
11088    declared type of the individual bit-field itself (for GCC anyway... the
11089    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11090    bytes) of the hypothetical "containing object" which will be given in the
11091    DW_AT_byte_size attribute for this bit-field.  (See the
11092    `byte_size_attribute' function below.)  It is also used when calculating the
11093    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11094    function below.)  */
11095
11096 static void
11097 add_data_member_location_attribute (dw_die_ref die, tree decl)
11098 {
11099   HOST_WIDE_INT offset;
11100   dw_loc_descr_ref loc_descr = 0;
11101
11102   if (TREE_CODE (decl) == TREE_BINFO)
11103     {
11104       /* We're working on the TAG_inheritance for a base class.  */
11105       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11106         {
11107           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11108              aren't at a fixed offset from all (sub)objects of the same
11109              type.  We need to extract the appropriate offset from our
11110              vtable.  The following dwarf expression means
11111
11112                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11113
11114              This is specific to the V3 ABI, of course.  */
11115
11116           dw_loc_descr_ref tmp;
11117
11118           /* Make a copy of the object address.  */
11119           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11120           add_loc_descr (&loc_descr, tmp);
11121
11122           /* Extract the vtable address.  */
11123           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11124           add_loc_descr (&loc_descr, tmp);
11125
11126           /* Calculate the address of the offset.  */
11127           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11128           gcc_assert (offset < 0);
11129
11130           tmp = int_loc_descriptor (-offset);
11131           add_loc_descr (&loc_descr, tmp);
11132           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11133           add_loc_descr (&loc_descr, tmp);
11134
11135           /* Extract the offset.  */
11136           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11137           add_loc_descr (&loc_descr, tmp);
11138
11139           /* Add it to the object address.  */
11140           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11141           add_loc_descr (&loc_descr, tmp);
11142         }
11143       else
11144         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11145     }
11146   else
11147     offset = field_byte_offset (decl);
11148
11149   if (! loc_descr)
11150     {
11151       enum dwarf_location_atom op;
11152
11153       /* The DWARF2 standard says that we should assume that the structure
11154          address is already on the stack, so we can specify a structure field
11155          address by using DW_OP_plus_uconst.  */
11156
11157 #ifdef MIPS_DEBUGGING_INFO
11158       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11159          operator correctly.  It works only if we leave the offset on the
11160          stack.  */
11161       op = DW_OP_constu;
11162 #else
11163       op = DW_OP_plus_uconst;
11164 #endif
11165
11166       loc_descr = new_loc_descr (op, offset, 0);
11167     }
11168
11169   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11170 }
11171
11172 /* Writes integer values to dw_vec_const array.  */
11173
11174 static void
11175 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11176 {
11177   while (size != 0)
11178     {
11179       *dest++ = val & 0xff;
11180       val >>= 8;
11181       --size;
11182     }
11183 }
11184
11185 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11186
11187 static HOST_WIDE_INT
11188 extract_int (const unsigned char *src, unsigned int size)
11189 {
11190   HOST_WIDE_INT val = 0;
11191
11192   src += size;
11193   while (size != 0)
11194     {
11195       val <<= 8;
11196       val |= *--src & 0xff;
11197       --size;
11198     }
11199   return val;
11200 }
11201
11202 /* Writes floating point values to dw_vec_const array.  */
11203
11204 static void
11205 insert_float (const_rtx rtl, unsigned char *array)
11206 {
11207   REAL_VALUE_TYPE rv;
11208   long val[4];
11209   int i;
11210
11211   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11212   real_to_target (val, &rv, GET_MODE (rtl));
11213
11214   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11215   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11216     {
11217       insert_int (val[i], 4, array);
11218       array += 4;
11219     }
11220 }
11221
11222 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11223    does not have a "location" either in memory or in a register.  These
11224    things can arise in GNU C when a constant is passed as an actual parameter
11225    to an inlined function.  They can also arise in C++ where declared
11226    constants do not necessarily get memory "homes".  */
11227
11228 static void
11229 add_const_value_attribute (dw_die_ref die, rtx rtl)
11230 {
11231   switch (GET_CODE (rtl))
11232     {
11233     case CONST_INT:
11234       {
11235         HOST_WIDE_INT val = INTVAL (rtl);
11236
11237         if (val < 0)
11238           add_AT_int (die, DW_AT_const_value, val);
11239         else
11240           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11241       }
11242       break;
11243
11244     case CONST_DOUBLE:
11245       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11246          floating-point constant.  A CONST_DOUBLE is used whenever the
11247          constant requires more than one word in order to be adequately
11248          represented.  We output CONST_DOUBLEs as blocks.  */
11249       {
11250         enum machine_mode mode = GET_MODE (rtl);
11251
11252         if (SCALAR_FLOAT_MODE_P (mode))
11253           {
11254             unsigned int length = GET_MODE_SIZE (mode);
11255             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11256
11257             insert_float (rtl, array);
11258             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11259           }
11260         else
11261           {
11262             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11263             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11264
11265             add_AT_long_long (die, DW_AT_const_value,
11266                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11267           }
11268       }
11269       break;
11270
11271     case CONST_VECTOR:
11272       {
11273         enum machine_mode mode = GET_MODE (rtl);
11274         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11275         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11276         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11277         unsigned int i;
11278         unsigned char *p;
11279
11280         switch (GET_MODE_CLASS (mode))
11281           {
11282           case MODE_VECTOR_INT:
11283             for (i = 0, p = array; i < length; i++, p += elt_size)
11284               {
11285                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11286                 HOST_WIDE_INT lo, hi;
11287
11288                 switch (GET_CODE (elt))
11289                   {
11290                   case CONST_INT:
11291                     lo = INTVAL (elt);
11292                     hi = -(lo < 0);
11293                     break;
11294
11295                   case CONST_DOUBLE:
11296                     lo = CONST_DOUBLE_LOW (elt);
11297                     hi = CONST_DOUBLE_HIGH (elt);
11298                     break;
11299
11300                   default:
11301                     gcc_unreachable ();
11302                   }
11303
11304                 if (elt_size <= sizeof (HOST_WIDE_INT))
11305                   insert_int (lo, elt_size, p);
11306                 else
11307                   {
11308                     unsigned char *p0 = p;
11309                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11310
11311                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11312                     if (WORDS_BIG_ENDIAN)
11313                       {
11314                         p0 = p1;
11315                         p1 = p;
11316                       }
11317                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11318                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11319                   }
11320               }
11321             break;
11322
11323           case MODE_VECTOR_FLOAT:
11324             for (i = 0, p = array; i < length; i++, p += elt_size)
11325               {
11326                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11327                 insert_float (elt, p);
11328               }
11329             break;
11330
11331           default:
11332             gcc_unreachable ();
11333           }
11334
11335         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11336       }
11337       break;
11338
11339     case CONST_STRING:
11340       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11341       break;
11342
11343     case SYMBOL_REF:
11344     case LABEL_REF:
11345     case CONST:
11346       add_AT_addr (die, DW_AT_const_value, rtl);
11347       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11348       break;
11349
11350     case PLUS:
11351       /* In cases where an inlined instance of an inline function is passed
11352          the address of an `auto' variable (which is local to the caller) we
11353          can get a situation where the DECL_RTL of the artificial local
11354          variable (for the inlining) which acts as a stand-in for the
11355          corresponding formal parameter (of the inline function) will look
11356          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11357          exactly a compile-time constant expression, but it isn't the address
11358          of the (artificial) local variable either.  Rather, it represents the
11359          *value* which the artificial local variable always has during its
11360          lifetime.  We currently have no way to represent such quasi-constant
11361          values in Dwarf, so for now we just punt and generate nothing.  */
11362       break;
11363
11364     default:
11365       /* No other kinds of rtx should be possible here.  */
11366       gcc_unreachable ();
11367     }
11368
11369 }
11370
11371 /* Determine whether the evaluation of EXPR references any variables
11372    or functions which aren't otherwise used (and therefore may not be
11373    output).  */
11374 static tree
11375 reference_to_unused (tree * tp, int * walk_subtrees,
11376                      void * data ATTRIBUTE_UNUSED)
11377 {
11378   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11379     *walk_subtrees = 0;
11380
11381   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11382       && ! TREE_ASM_WRITTEN (*tp))
11383     return *tp;
11384   /* ???  The C++ FE emits debug information for using decls, so
11385      putting gcc_unreachable here falls over.  See PR31899.  For now
11386      be conservative.  */
11387   else if (!cgraph_global_info_ready
11388            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11389     return *tp;
11390   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11391     {
11392       struct varpool_node *node = varpool_node (*tp);
11393       if (!node->needed)
11394         return *tp;
11395     }
11396   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11397            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11398     {
11399       struct cgraph_node *node = cgraph_node (*tp);
11400       if (!node->output)
11401         return *tp;
11402     }
11403   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11404     return *tp;
11405
11406   return NULL_TREE;
11407 }
11408
11409 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11410    for use in a later add_const_value_attribute call.  */
11411
11412 static rtx
11413 rtl_for_decl_init (tree init, tree type)
11414 {
11415   rtx rtl = NULL_RTX;
11416
11417   /* If a variable is initialized with a string constant without embedded
11418      zeros, build CONST_STRING.  */
11419   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11420     {
11421       tree enttype = TREE_TYPE (type);
11422       tree domain = TYPE_DOMAIN (type);
11423       enum machine_mode mode = TYPE_MODE (enttype);
11424
11425       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11426           && domain
11427           && integer_zerop (TYPE_MIN_VALUE (domain))
11428           && compare_tree_int (TYPE_MAX_VALUE (domain),
11429                                TREE_STRING_LENGTH (init) - 1) == 0
11430           && ((size_t) TREE_STRING_LENGTH (init)
11431               == strlen (TREE_STRING_POINTER (init)) + 1))
11432         rtl = gen_rtx_CONST_STRING (VOIDmode,
11433                                     ggc_strdup (TREE_STRING_POINTER (init)));
11434     }
11435   /* Other aggregates, and complex values, could be represented using
11436      CONCAT: FIXME!  */
11437   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11438     ;
11439   /* Vectors only work if their mode is supported by the target.
11440      FIXME: generic vectors ought to work too.  */
11441   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11442     ;
11443   /* If the initializer is something that we know will expand into an
11444      immediate RTL constant, expand it now.  We must be careful not to
11445      reference variables which won't be output.  */
11446   else if (initializer_constant_valid_p (init, type)
11447            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11448     {
11449       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11450          possible.  */
11451       if (TREE_CODE (type) == VECTOR_TYPE)
11452         switch (TREE_CODE (init))
11453           {
11454           case VECTOR_CST:
11455             break;
11456           case CONSTRUCTOR:
11457             if (TREE_CONSTANT (init))
11458               {
11459                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11460                 bool constant_p = true;
11461                 tree value;
11462                 unsigned HOST_WIDE_INT ix;
11463
11464                 /* Even when ctor is constant, it might contain non-*_CST
11465                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11466                    belong into VECTOR_CST nodes.  */
11467                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11468                   if (!CONSTANT_CLASS_P (value))
11469                     {
11470                       constant_p = false;
11471                       break;
11472                     }
11473
11474                 if (constant_p)
11475                   {
11476                     init = build_vector_from_ctor (type, elts);
11477                     break;
11478                   }
11479               }
11480             /* FALLTHRU */
11481
11482           default:
11483             return NULL;
11484           }
11485
11486       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11487
11488       /* If expand_expr returns a MEM, it wasn't immediate.  */
11489       gcc_assert (!rtl || !MEM_P (rtl));
11490     }
11491
11492   return rtl;
11493 }
11494
11495 /* Generate RTL for the variable DECL to represent its location.  */
11496
11497 static rtx
11498 rtl_for_decl_location (tree decl)
11499 {
11500   rtx rtl;
11501
11502   /* Here we have to decide where we are going to say the parameter "lives"
11503      (as far as the debugger is concerned).  We only have a couple of
11504      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11505
11506      DECL_RTL normally indicates where the parameter lives during most of the
11507      activation of the function.  If optimization is enabled however, this
11508      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11509      that the parameter doesn't really live anywhere (as far as the code
11510      generation parts of GCC are concerned) during most of the function's
11511      activation.  That will happen (for example) if the parameter is never
11512      referenced within the function.
11513
11514      We could just generate a location descriptor here for all non-NULL
11515      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11516      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11517      where DECL_RTL is NULL or is a pseudo-reg.
11518
11519      Note however that we can only get away with using DECL_INCOMING_RTL as
11520      a backup substitute for DECL_RTL in certain limited cases.  In cases
11521      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11522      we can be sure that the parameter was passed using the same type as it is
11523      declared to have within the function, and that its DECL_INCOMING_RTL
11524      points us to a place where a value of that type is passed.
11525
11526      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11527      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11528      because in these cases DECL_INCOMING_RTL points us to a value of some
11529      type which is *different* from the type of the parameter itself.  Thus,
11530      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11531      such cases, the debugger would end up (for example) trying to fetch a
11532      `float' from a place which actually contains the first part of a
11533      `double'.  That would lead to really incorrect and confusing
11534      output at debug-time.
11535
11536      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11537      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11538      are a couple of exceptions however.  On little-endian machines we can
11539      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11540      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11541      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11542      when (on a little-endian machine) a non-prototyped function has a
11543      parameter declared to be of type `short' or `char'.  In such cases,
11544      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11545      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11546      passed `int' value.  If the debugger then uses that address to fetch
11547      a `short' or a `char' (on a little-endian machine) the result will be
11548      the correct data, so we allow for such exceptional cases below.
11549
11550      Note that our goal here is to describe the place where the given formal
11551      parameter lives during most of the function's activation (i.e. between the
11552      end of the prologue and the start of the epilogue).  We'll do that as best
11553      as we can. Note however that if the given formal parameter is modified
11554      sometime during the execution of the function, then a stack backtrace (at
11555      debug-time) will show the function as having been called with the *new*
11556      value rather than the value which was originally passed in.  This happens
11557      rarely enough that it is not a major problem, but it *is* a problem, and
11558      I'd like to fix it.
11559
11560      A future version of dwarf2out.c may generate two additional attributes for
11561      any given DW_TAG_formal_parameter DIE which will describe the "passed
11562      type" and the "passed location" for the given formal parameter in addition
11563      to the attributes we now generate to indicate the "declared type" and the
11564      "active location" for each parameter.  This additional set of attributes
11565      could be used by debuggers for stack backtraces. Separately, note that
11566      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11567      This happens (for example) for inlined-instances of inline function formal
11568      parameters which are never referenced.  This really shouldn't be
11569      happening.  All PARM_DECL nodes should get valid non-NULL
11570      DECL_INCOMING_RTL values.  FIXME.  */
11571
11572   /* Use DECL_RTL as the "location" unless we find something better.  */
11573   rtl = DECL_RTL_IF_SET (decl);
11574
11575   /* When generating abstract instances, ignore everything except
11576      constants, symbols living in memory, and symbols living in
11577      fixed registers.  */
11578   if (! reload_completed)
11579     {
11580       if (rtl
11581           && (CONSTANT_P (rtl)
11582               || (MEM_P (rtl)
11583                   && CONSTANT_P (XEXP (rtl, 0)))
11584               || (REG_P (rtl)
11585                   && TREE_CODE (decl) == VAR_DECL
11586                   && TREE_STATIC (decl))))
11587         {
11588           rtl = targetm.delegitimize_address (rtl);
11589           return rtl;
11590         }
11591       rtl = NULL_RTX;
11592     }
11593   else if (TREE_CODE (decl) == PARM_DECL)
11594     {
11595       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11596         {
11597           tree declared_type = TREE_TYPE (decl);
11598           tree passed_type = DECL_ARG_TYPE (decl);
11599           enum machine_mode dmode = TYPE_MODE (declared_type);
11600           enum machine_mode pmode = TYPE_MODE (passed_type);
11601
11602           /* This decl represents a formal parameter which was optimized out.
11603              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11604              all cases where (rtl == NULL_RTX) just below.  */
11605           if (dmode == pmode)
11606             rtl = DECL_INCOMING_RTL (decl);
11607           else if (SCALAR_INT_MODE_P (dmode)
11608                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11609                    && DECL_INCOMING_RTL (decl))
11610             {
11611               rtx inc = DECL_INCOMING_RTL (decl);
11612               if (REG_P (inc))
11613                 rtl = inc;
11614               else if (MEM_P (inc))
11615                 {
11616                   if (BYTES_BIG_ENDIAN)
11617                     rtl = adjust_address_nv (inc, dmode,
11618                                              GET_MODE_SIZE (pmode)
11619                                              - GET_MODE_SIZE (dmode));
11620                   else
11621                     rtl = inc;
11622                 }
11623             }
11624         }
11625
11626       /* If the parm was passed in registers, but lives on the stack, then
11627          make a big endian correction if the mode of the type of the
11628          parameter is not the same as the mode of the rtl.  */
11629       /* ??? This is the same series of checks that are made in dbxout.c before
11630          we reach the big endian correction code there.  It isn't clear if all
11631          of these checks are necessary here, but keeping them all is the safe
11632          thing to do.  */
11633       else if (MEM_P (rtl)
11634                && XEXP (rtl, 0) != const0_rtx
11635                && ! CONSTANT_P (XEXP (rtl, 0))
11636                /* Not passed in memory.  */
11637                && !MEM_P (DECL_INCOMING_RTL (decl))
11638                /* Not passed by invisible reference.  */
11639                && (!REG_P (XEXP (rtl, 0))
11640                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11641                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11642 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11643                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11644 #endif
11645                      )
11646                /* Big endian correction check.  */
11647                && BYTES_BIG_ENDIAN
11648                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11649                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11650                    < UNITS_PER_WORD))
11651         {
11652           int offset = (UNITS_PER_WORD
11653                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11654
11655           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11656                              plus_constant (XEXP (rtl, 0), offset));
11657         }
11658     }
11659   else if (TREE_CODE (decl) == VAR_DECL
11660            && rtl
11661            && MEM_P (rtl)
11662            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11663            && BYTES_BIG_ENDIAN)
11664     {
11665       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11666       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11667
11668       /* If a variable is declared "register" yet is smaller than
11669          a register, then if we store the variable to memory, it
11670          looks like we're storing a register-sized value, when in
11671          fact we are not.  We need to adjust the offset of the
11672          storage location to reflect the actual value's bytes,
11673          else gdb will not be able to display it.  */
11674       if (rsize > dsize)
11675         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11676                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11677     }
11678
11679   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11680      and will have been substituted directly into all expressions that use it.
11681      C does not have such a concept, but C++ and other languages do.  */
11682   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11683     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11684
11685   if (rtl)
11686     rtl = targetm.delegitimize_address (rtl);
11687
11688   /* If we don't look past the constant pool, we risk emitting a
11689      reference to a constant pool entry that isn't referenced from
11690      code, and thus is not emitted.  */
11691   if (rtl)
11692     rtl = avoid_constant_pool_reference (rtl);
11693
11694   return rtl;
11695 }
11696
11697 /* We need to figure out what section we should use as the base for the
11698    address ranges where a given location is valid.
11699    1. If this particular DECL has a section associated with it, use that.
11700    2. If this function has a section associated with it, use that.
11701    3. Otherwise, use the text section.
11702    XXX: If you split a variable across multiple sections, we won't notice.  */
11703
11704 static const char *
11705 secname_for_decl (const_tree decl)
11706 {
11707   const char *secname;
11708
11709   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11710     {
11711       tree sectree = DECL_SECTION_NAME (decl);
11712       secname = TREE_STRING_POINTER (sectree);
11713     }
11714   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11715     {
11716       tree sectree = DECL_SECTION_NAME (current_function_decl);
11717       secname = TREE_STRING_POINTER (sectree);
11718     }
11719   else if (cfun && in_cold_section_p)
11720     secname = crtl->subsections.cold_section_label;
11721   else
11722     secname = text_section_label;
11723
11724   return secname;
11725 }
11726
11727 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11728    returned.  If so, the decl for the COMMON block is returned, and the
11729    value is the offset into the common block for the symbol.  */
11730
11731 static tree
11732 fortran_common (tree decl, HOST_WIDE_INT *value)
11733 {
11734   tree val_expr, cvar;
11735   enum machine_mode mode;
11736   HOST_WIDE_INT bitsize, bitpos;
11737   tree offset;
11738   int volatilep = 0, unsignedp = 0;
11739
11740   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11741      it does not have a value (the offset into the common area), or if it
11742      is thread local (as opposed to global) then it isn't common, and shouldn't
11743      be handled as such.  */
11744   if (TREE_CODE (decl) != VAR_DECL
11745       || !TREE_PUBLIC (decl)
11746       || !TREE_STATIC (decl)
11747       || !DECL_HAS_VALUE_EXPR_P (decl)
11748       || !is_fortran ())
11749     return NULL_TREE;
11750
11751   val_expr = DECL_VALUE_EXPR (decl);
11752   if (TREE_CODE (val_expr) != COMPONENT_REF)
11753     return NULL_TREE;
11754
11755   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11756                               &mode, &unsignedp, &volatilep, true);
11757
11758   if (cvar == NULL_TREE
11759       || TREE_CODE (cvar) != VAR_DECL
11760       || DECL_ARTIFICIAL (cvar)
11761       || !TREE_PUBLIC (cvar))
11762     return NULL_TREE;
11763
11764   *value = 0;
11765   if (offset != NULL)
11766     {
11767       if (!host_integerp (offset, 0))
11768         return NULL_TREE;
11769       *value = tree_low_cst (offset, 0);
11770     }
11771   if (bitpos != 0)
11772     *value += bitpos / BITS_PER_UNIT;
11773
11774   return cvar;
11775 }
11776
11777 /* Dereference a location expression LOC if DECL is passed by invisible
11778    reference.  */
11779
11780 static dw_loc_descr_ref
11781 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11782 {
11783   HOST_WIDE_INT size;
11784   enum dwarf_location_atom op;
11785
11786   if (loc == NULL)
11787     return NULL;
11788
11789   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11790       || !DECL_BY_REFERENCE (decl))
11791     return loc;
11792
11793   size = int_size_in_bytes (TREE_TYPE (decl));
11794   if (size > DWARF2_ADDR_SIZE || size == -1)
11795     return 0;
11796   else if (size == DWARF2_ADDR_SIZE)
11797     op = DW_OP_deref;
11798   else
11799     op = DW_OP_deref_size;
11800   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11801   return loc;
11802 }
11803
11804 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11805    data attribute for a variable or a parameter.  We generate the
11806    DW_AT_const_value attribute only in those cases where the given variable
11807    or parameter does not have a true "location" either in memory or in a
11808    register.  This can happen (for example) when a constant is passed as an
11809    actual argument in a call to an inline function.  (It's possible that
11810    these things can crop up in other ways also.)  Note that one type of
11811    constant value which can be passed into an inlined function is a constant
11812    pointer.  This can happen for example if an actual argument in an inlined
11813    function call evaluates to a compile-time constant address.  */
11814
11815 static void
11816 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11817                                        enum dwarf_attribute attr)
11818 {
11819   rtx rtl;
11820   dw_loc_descr_ref descr;
11821   var_loc_list *loc_list;
11822   struct var_loc_node *node;
11823   if (TREE_CODE (decl) == ERROR_MARK)
11824     return;
11825
11826   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11827               || TREE_CODE (decl) == RESULT_DECL);
11828
11829   /* See if we possibly have multiple locations for this variable.  */
11830   loc_list = lookup_decl_loc (decl);
11831
11832   /* If it truly has multiple locations, the first and last node will
11833      differ.  */
11834   if (loc_list && loc_list->first != loc_list->last)
11835     {
11836       const char *endname, *secname;
11837       dw_loc_list_ref list;
11838       rtx varloc;
11839       enum var_init_status initialized;
11840
11841       /* Now that we know what section we are using for a base,
11842          actually construct the list of locations.
11843          The first location information is what is passed to the
11844          function that creates the location list, and the remaining
11845          locations just get added on to that list.
11846          Note that we only know the start address for a location
11847          (IE location changes), so to build the range, we use
11848          the range [current location start, next location start].
11849          This means we have to special case the last node, and generate
11850          a range of [last location start, end of function label].  */
11851
11852       node = loc_list->first;
11853       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11854       secname = secname_for_decl (decl);
11855
11856       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11857         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11858       else
11859         initialized = VAR_INIT_STATUS_INITIALIZED;
11860
11861       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11862       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11863       node = node->next;
11864
11865       for (; node->next; node = node->next)
11866         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11867           {
11868             /* The variable has a location between NODE->LABEL and
11869                NODE->NEXT->LABEL.  */
11870             enum var_init_status initialized =
11871               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11872             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11873             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11874                                       decl);
11875             add_loc_descr_to_loc_list (&list, descr,
11876                                        node->label, node->next->label, secname);
11877           }
11878
11879       /* If the variable has a location at the last label
11880          it keeps its location until the end of function.  */
11881       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11882         {
11883           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11884           enum var_init_status initialized =
11885             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11886
11887           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11888           if (!current_function_decl)
11889             endname = text_end_label;
11890           else
11891             {
11892               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11893                                            current_function_funcdef_no);
11894               endname = ggc_strdup (label_id);
11895             }
11896           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11897                                     decl);
11898           add_loc_descr_to_loc_list (&list, descr,
11899                                      node->label, endname, secname);
11900         }
11901
11902       /* Finally, add the location list to the DIE, and we are done.  */
11903       add_AT_loc_list (die, attr, list);
11904       return;
11905     }
11906
11907   /* Try to get some constant RTL for this decl, and use that as the value of
11908      the location.  */
11909
11910   rtl = rtl_for_decl_location (decl);
11911   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11912     {
11913       add_const_value_attribute (die, rtl);
11914       return;
11915     }
11916
11917   /* If we have tried to generate the location otherwise, and it
11918      didn't work out (we wouldn't be here if we did), and we have a one entry
11919      location list, try generating a location from that.  */
11920   if (loc_list && loc_list->first)
11921     {
11922       enum var_init_status status;
11923       node = loc_list->first;
11924       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11925       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11926       if (descr)
11927         {
11928           descr = loc_by_reference (descr, decl);
11929           add_AT_location_description (die, attr, descr);
11930           return;
11931         }
11932     }
11933
11934   /* We couldn't get any rtl, so try directly generating the location
11935      description from the tree.  */
11936   descr = loc_descriptor_from_tree (decl);
11937   if (descr)
11938     {
11939       descr = loc_by_reference (descr, decl);
11940       add_AT_location_description (die, attr, descr);
11941       return;
11942     }
11943   /* None of that worked, so it must not really have a location;
11944      try adding a constant value attribute from the DECL_INITIAL.  */
11945   tree_add_const_value_attribute (die, decl);
11946 }
11947
11948 /* Add VARIABLE and DIE into deferred locations list.  */
11949
11950 static void
11951 defer_location (tree variable, dw_die_ref die)
11952 {
11953   deferred_locations entry;
11954   entry.variable = variable;
11955   entry.die = die;
11956   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11957 }
11958
11959 /* Helper function for tree_add_const_value_attribute.  Natively encode
11960    initializer INIT into an array.  Return true if successful.  */
11961
11962 static bool
11963 native_encode_initializer (tree init, unsigned char *array, int size)
11964 {
11965   tree type;
11966
11967   if (init == NULL_TREE)
11968     return false;
11969
11970   STRIP_NOPS (init);
11971   switch (TREE_CODE (init))
11972     {
11973     case STRING_CST:
11974       type = TREE_TYPE (init);
11975       if (TREE_CODE (type) == ARRAY_TYPE)
11976         {
11977           tree enttype = TREE_TYPE (type);
11978           enum machine_mode mode = TYPE_MODE (enttype);
11979
11980           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11981             return false;
11982           if (int_size_in_bytes (type) != size)
11983             return false;
11984           if (size > TREE_STRING_LENGTH (init))
11985             {
11986               memcpy (array, TREE_STRING_POINTER (init),
11987                       TREE_STRING_LENGTH (init));
11988               memset (array + TREE_STRING_LENGTH (init),
11989                       '\0', size - TREE_STRING_LENGTH (init));
11990             }
11991           else
11992             memcpy (array, TREE_STRING_POINTER (init), size);
11993           return true;
11994         }
11995       return false;
11996     case CONSTRUCTOR:
11997       type = TREE_TYPE (init);
11998       if (int_size_in_bytes (type) != size)
11999         return false;
12000       if (TREE_CODE (type) == ARRAY_TYPE)
12001         {
12002           HOST_WIDE_INT min_index;
12003           unsigned HOST_WIDE_INT cnt;
12004           int curpos = 0, fieldsize;
12005           constructor_elt *ce;
12006
12007           if (TYPE_DOMAIN (type) == NULL_TREE
12008               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12009             return false;
12010
12011           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12012           if (fieldsize <= 0)
12013             return false;
12014
12015           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12016           memset (array, '\0', size);
12017           for (cnt = 0;
12018                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12019                cnt++)
12020             {
12021               tree val = ce->value;
12022               tree index = ce->index;
12023               int pos = curpos;
12024               if (index && TREE_CODE (index) == RANGE_EXPR)
12025                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12026                       * fieldsize;
12027               else if (index)
12028                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12029
12030               if (val)
12031                 {
12032                   STRIP_NOPS (val);
12033                   if (!native_encode_initializer (val, array + pos, fieldsize))
12034                     return false;
12035                 }
12036               curpos = pos + fieldsize;
12037               if (index && TREE_CODE (index) == RANGE_EXPR)
12038                 {
12039                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12040                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12041                   while (count > 0)
12042                     {
12043                       if (val)
12044                         memcpy (array + curpos, array + pos, fieldsize);
12045                       curpos += fieldsize;
12046                     }
12047                 }
12048               gcc_assert (curpos <= size);
12049             }
12050           return true;
12051         }
12052       else if (TREE_CODE (type) == RECORD_TYPE
12053                || TREE_CODE (type) == UNION_TYPE)
12054         {
12055           tree field = NULL_TREE;
12056           unsigned HOST_WIDE_INT cnt;
12057           constructor_elt *ce;
12058
12059           if (int_size_in_bytes (type) != size)
12060             return false;
12061
12062           if (TREE_CODE (type) == RECORD_TYPE)
12063             field = TYPE_FIELDS (type);
12064
12065           for (cnt = 0;
12066                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12067                cnt++, field = field ? TREE_CHAIN (field) : 0)
12068             {
12069               tree val = ce->value;
12070               int pos, fieldsize;
12071
12072               if (ce->index != 0)
12073                 field = ce->index;
12074
12075               if (val)
12076                 STRIP_NOPS (val);
12077
12078               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12079                 return false;
12080
12081               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12082                   && TYPE_DOMAIN (TREE_TYPE (field))
12083                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12084                 return false;
12085               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12086                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12087                 return false;
12088               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12089               pos = int_byte_position (field);
12090               gcc_assert (pos + fieldsize <= size);
12091               if (val
12092                   && !native_encode_initializer (val, array + pos, fieldsize))
12093                 return false;
12094             }
12095           return true;
12096         }
12097       return false;
12098     case VIEW_CONVERT_EXPR:
12099     case NON_LVALUE_EXPR:
12100       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12101     default:
12102       return native_encode_expr (init, array, size) == size;
12103     }
12104 }
12105
12106 /* If we don't have a copy of this variable in memory for some reason (such
12107    as a C++ member constant that doesn't have an out-of-line definition),
12108    we should tell the debugger about the constant value.  */
12109
12110 static void
12111 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12112 {
12113   tree init;
12114   tree type = TREE_TYPE (decl);
12115   rtx rtl;
12116
12117   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12118     return;
12119
12120   init = DECL_INITIAL (decl);
12121   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12122     /* OK */;
12123   else
12124     return;
12125
12126   rtl = rtl_for_decl_init (init, type);
12127   if (rtl)
12128     add_const_value_attribute (var_die, rtl);
12129   /* If the host and target are sane, try harder.  */
12130   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12131            && initializer_constant_valid_p (init, type))
12132     {
12133       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12134       if (size > 0 && (int) size == size)
12135         {
12136           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12137
12138           if (native_encode_initializer (init, array, size))
12139             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12140         }
12141     }
12142 }
12143
12144 /* Convert the CFI instructions for the current function into a
12145    location list.  This is used for DW_AT_frame_base when we targeting
12146    a dwarf2 consumer that does not support the dwarf3
12147    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12148    expressions.  */
12149
12150 static dw_loc_list_ref
12151 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12152 {
12153   dw_fde_ref fde;
12154   dw_loc_list_ref list, *list_tail;
12155   dw_cfi_ref cfi;
12156   dw_cfa_location last_cfa, next_cfa;
12157   const char *start_label, *last_label, *section;
12158
12159   fde = current_fde ();
12160   gcc_assert (fde != NULL);
12161
12162   section = secname_for_decl (current_function_decl);
12163   list_tail = &list;
12164   list = NULL;
12165
12166   next_cfa.reg = INVALID_REGNUM;
12167   next_cfa.offset = 0;
12168   next_cfa.indirect = 0;
12169   next_cfa.base_offset = 0;
12170
12171   start_label = fde->dw_fde_begin;
12172
12173   /* ??? Bald assumption that the CIE opcode list does not contain
12174      advance opcodes.  */
12175   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12176     lookup_cfa_1 (cfi, &next_cfa);
12177
12178   last_cfa = next_cfa;
12179   last_label = start_label;
12180
12181   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12182     switch (cfi->dw_cfi_opc)
12183       {
12184       case DW_CFA_set_loc:
12185       case DW_CFA_advance_loc1:
12186       case DW_CFA_advance_loc2:
12187       case DW_CFA_advance_loc4:
12188         if (!cfa_equal_p (&last_cfa, &next_cfa))
12189           {
12190             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12191                                        start_label, last_label, section,
12192                                        list == NULL);
12193
12194             list_tail = &(*list_tail)->dw_loc_next;
12195             last_cfa = next_cfa;
12196             start_label = last_label;
12197           }
12198         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12199         break;
12200
12201       case DW_CFA_advance_loc:
12202         /* The encoding is complex enough that we should never emit this.  */
12203       case DW_CFA_remember_state:
12204       case DW_CFA_restore_state:
12205         /* We don't handle these two in this function.  It would be possible
12206            if it were to be required.  */
12207         gcc_unreachable ();
12208
12209       default:
12210         lookup_cfa_1 (cfi, &next_cfa);
12211         break;
12212       }
12213
12214   if (!cfa_equal_p (&last_cfa, &next_cfa))
12215     {
12216       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12217                                  start_label, last_label, section,
12218                                  list == NULL);
12219       list_tail = &(*list_tail)->dw_loc_next;
12220       start_label = last_label;
12221     }
12222   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12223                              start_label, fde->dw_fde_end, section,
12224                              list == NULL);
12225
12226   return list;
12227 }
12228
12229 /* Compute a displacement from the "steady-state frame pointer" to the
12230    frame base (often the same as the CFA), and store it in
12231    frame_pointer_fb_offset.  OFFSET is added to the displacement
12232    before the latter is negated.  */
12233
12234 static void
12235 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12236 {
12237   rtx reg, elim;
12238
12239 #ifdef FRAME_POINTER_CFA_OFFSET
12240   reg = frame_pointer_rtx;
12241   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12242 #else
12243   reg = arg_pointer_rtx;
12244   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12245 #endif
12246
12247   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12248   if (GET_CODE (elim) == PLUS)
12249     {
12250       offset += INTVAL (XEXP (elim, 1));
12251       elim = XEXP (elim, 0);
12252     }
12253
12254   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12255                && (elim == hard_frame_pointer_rtx
12256                    || elim == stack_pointer_rtx))
12257               || elim == (frame_pointer_needed
12258                           ? hard_frame_pointer_rtx
12259                           : stack_pointer_rtx));
12260
12261   frame_pointer_fb_offset = -offset;
12262 }
12263
12264 /* Generate a DW_AT_name attribute given some string value to be included as
12265    the value of the attribute.  */
12266
12267 static void
12268 add_name_attribute (dw_die_ref die, const char *name_string)
12269 {
12270   if (name_string != NULL && *name_string != 0)
12271     {
12272       if (demangle_name_func)
12273         name_string = (*demangle_name_func) (name_string);
12274
12275       add_AT_string (die, DW_AT_name, name_string);
12276     }
12277 }
12278
12279 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12280
12281 static void
12282 add_comp_dir_attribute (dw_die_ref die)
12283 {
12284   const char *wd = get_src_pwd ();
12285   if (wd != NULL)
12286     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12287 }
12288
12289 /* Given a tree node describing an array bound (either lower or upper) output
12290    a representation for that bound.  */
12291
12292 static void
12293 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12294 {
12295   switch (TREE_CODE (bound))
12296     {
12297     case ERROR_MARK:
12298       return;
12299
12300     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12301     case INTEGER_CST:
12302       if (! host_integerp (bound, 0)
12303           || (bound_attr == DW_AT_lower_bound
12304               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12305                   || (is_fortran () && integer_onep (bound)))))
12306         /* Use the default.  */
12307         ;
12308       else
12309         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12310       break;
12311
12312     CASE_CONVERT:
12313     case VIEW_CONVERT_EXPR:
12314       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12315       break;
12316
12317     case SAVE_EXPR:
12318       break;
12319
12320     case VAR_DECL:
12321     case PARM_DECL:
12322     case RESULT_DECL:
12323       {
12324         dw_die_ref decl_die = lookup_decl_die (bound);
12325         dw_loc_descr_ref loc;
12326
12327         /* ??? Can this happen, or should the variable have been bound
12328            first?  Probably it can, since I imagine that we try to create
12329            the types of parameters in the order in which they exist in
12330            the list, and won't have created a forward reference to a
12331            later parameter.  */
12332         if (decl_die != NULL)
12333           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12334         else
12335           {
12336             loc = loc_descriptor_from_tree_1 (bound, 0);
12337             add_AT_location_description (subrange_die, bound_attr, loc);
12338           }
12339         break;
12340       }
12341
12342     default:
12343       {
12344         /* Otherwise try to create a stack operation procedure to
12345            evaluate the value of the array bound.  */
12346
12347         dw_die_ref ctx, decl_die;
12348         dw_loc_descr_ref loc;
12349
12350         loc = loc_descriptor_from_tree (bound);
12351         if (loc == NULL)
12352           break;
12353
12354         if (current_function_decl == 0)
12355           ctx = comp_unit_die;
12356         else
12357           ctx = lookup_decl_die (current_function_decl);
12358
12359         decl_die = new_die (DW_TAG_variable, ctx, bound);
12360         add_AT_flag (decl_die, DW_AT_artificial, 1);
12361         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12362         add_AT_loc (decl_die, DW_AT_location, loc);
12363
12364         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12365         break;
12366       }
12367     }
12368 }
12369
12370 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12371    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12372    Note that the block of subscript information for an array type also
12373    includes information about the element type of the given array type.  */
12374
12375 static void
12376 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12377 {
12378   unsigned dimension_number;
12379   tree lower, upper;
12380   dw_die_ref subrange_die;
12381
12382   for (dimension_number = 0;
12383        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12384        type = TREE_TYPE (type), dimension_number++)
12385     {
12386       tree domain = TYPE_DOMAIN (type);
12387
12388       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12389         break;
12390
12391       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12392          and (in GNU C only) variable bounds.  Handle all three forms
12393          here.  */
12394       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12395       if (domain)
12396         {
12397           /* We have an array type with specified bounds.  */
12398           lower = TYPE_MIN_VALUE (domain);
12399           upper = TYPE_MAX_VALUE (domain);
12400
12401           /* Define the index type.  */
12402           if (TREE_TYPE (domain))
12403             {
12404               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12405                  TREE_TYPE field.  We can't emit debug info for this
12406                  because it is an unnamed integral type.  */
12407               if (TREE_CODE (domain) == INTEGER_TYPE
12408                   && TYPE_NAME (domain) == NULL_TREE
12409                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12410                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12411                 ;
12412               else
12413                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12414                                     type_die);
12415             }
12416
12417           /* ??? If upper is NULL, the array has unspecified length,
12418              but it does have a lower bound.  This happens with Fortran
12419                dimension arr(N:*)
12420              Since the debugger is definitely going to need to know N
12421              to produce useful results, go ahead and output the lower
12422              bound solo, and hope the debugger can cope.  */
12423
12424           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12425           if (upper)
12426             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12427         }
12428
12429       /* Otherwise we have an array type with an unspecified length.  The
12430          DWARF-2 spec does not say how to handle this; let's just leave out the
12431          bounds.  */
12432     }
12433 }
12434
12435 static void
12436 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12437 {
12438   unsigned size;
12439
12440   switch (TREE_CODE (tree_node))
12441     {
12442     case ERROR_MARK:
12443       size = 0;
12444       break;
12445     case ENUMERAL_TYPE:
12446     case RECORD_TYPE:
12447     case UNION_TYPE:
12448     case QUAL_UNION_TYPE:
12449       size = int_size_in_bytes (tree_node);
12450       break;
12451     case FIELD_DECL:
12452       /* For a data member of a struct or union, the DW_AT_byte_size is
12453          generally given as the number of bytes normally allocated for an
12454          object of the *declared* type of the member itself.  This is true
12455          even for bit-fields.  */
12456       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12457       break;
12458     default:
12459       gcc_unreachable ();
12460     }
12461
12462   /* Note that `size' might be -1 when we get to this point.  If it is, that
12463      indicates that the byte size of the entity in question is variable.  We
12464      have no good way of expressing this fact in Dwarf at the present time,
12465      so just let the -1 pass on through.  */
12466   add_AT_unsigned (die, DW_AT_byte_size, size);
12467 }
12468
12469 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12470    which specifies the distance in bits from the highest order bit of the
12471    "containing object" for the bit-field to the highest order bit of the
12472    bit-field itself.
12473
12474    For any given bit-field, the "containing object" is a hypothetical object
12475    (of some integral or enum type) within which the given bit-field lives.  The
12476    type of this hypothetical "containing object" is always the same as the
12477    declared type of the individual bit-field itself.  The determination of the
12478    exact location of the "containing object" for a bit-field is rather
12479    complicated.  It's handled by the `field_byte_offset' function (above).
12480
12481    Note that it is the size (in bytes) of the hypothetical "containing object"
12482    which will be given in the DW_AT_byte_size attribute for this bit-field.
12483    (See `byte_size_attribute' above).  */
12484
12485 static inline void
12486 add_bit_offset_attribute (dw_die_ref die, tree decl)
12487 {
12488   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12489   tree type = DECL_BIT_FIELD_TYPE (decl);
12490   HOST_WIDE_INT bitpos_int;
12491   HOST_WIDE_INT highest_order_object_bit_offset;
12492   HOST_WIDE_INT highest_order_field_bit_offset;
12493   HOST_WIDE_INT unsigned bit_offset;
12494
12495   /* Must be a field and a bit field.  */
12496   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12497
12498   /* We can't yet handle bit-fields whose offsets are variable, so if we
12499      encounter such things, just return without generating any attribute
12500      whatsoever.  Likewise for variable or too large size.  */
12501   if (! host_integerp (bit_position (decl), 0)
12502       || ! host_integerp (DECL_SIZE (decl), 1))
12503     return;
12504
12505   bitpos_int = int_bit_position (decl);
12506
12507   /* Note that the bit offset is always the distance (in bits) from the
12508      highest-order bit of the "containing object" to the highest-order bit of
12509      the bit-field itself.  Since the "high-order end" of any object or field
12510      is different on big-endian and little-endian machines, the computation
12511      below must take account of these differences.  */
12512   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12513   highest_order_field_bit_offset = bitpos_int;
12514
12515   if (! BYTES_BIG_ENDIAN)
12516     {
12517       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12518       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12519     }
12520
12521   bit_offset
12522     = (! BYTES_BIG_ENDIAN
12523        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12524        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12525
12526   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12527 }
12528
12529 /* For a FIELD_DECL node which represents a bit field, output an attribute
12530    which specifies the length in bits of the given field.  */
12531
12532 static inline void
12533 add_bit_size_attribute (dw_die_ref die, tree decl)
12534 {
12535   /* Must be a field and a bit field.  */
12536   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12537               && DECL_BIT_FIELD_TYPE (decl));
12538
12539   if (host_integerp (DECL_SIZE (decl), 1))
12540     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12541 }
12542
12543 /* If the compiled language is ANSI C, then add a 'prototyped'
12544    attribute, if arg types are given for the parameters of a function.  */
12545
12546 static inline void
12547 add_prototyped_attribute (dw_die_ref die, tree func_type)
12548 {
12549   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12550       && TYPE_ARG_TYPES (func_type) != NULL)
12551     add_AT_flag (die, DW_AT_prototyped, 1);
12552 }
12553
12554 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12555    by looking in either the type declaration or object declaration
12556    equate table.  */
12557
12558 static inline dw_die_ref
12559 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12560 {
12561   dw_die_ref origin_die = NULL;
12562
12563   if (TREE_CODE (origin) != FUNCTION_DECL)
12564     {
12565       /* We may have gotten separated from the block for the inlined
12566          function, if we're in an exception handler or some such; make
12567          sure that the abstract function has been written out.
12568
12569          Doing this for nested functions is wrong, however; functions are
12570          distinct units, and our context might not even be inline.  */
12571       tree fn = origin;
12572
12573       if (TYPE_P (fn))
12574         fn = TYPE_STUB_DECL (fn);
12575
12576       fn = decl_function_context (fn);
12577       if (fn)
12578         dwarf2out_abstract_function (fn);
12579     }
12580
12581   if (DECL_P (origin))
12582     origin_die = lookup_decl_die (origin);
12583   else if (TYPE_P (origin))
12584     origin_die = lookup_type_die (origin);
12585
12586   /* XXX: Functions that are never lowered don't always have correct block
12587      trees (in the case of java, they simply have no block tree, in some other
12588      languages).  For these functions, there is nothing we can really do to
12589      output correct debug info for inlined functions in all cases.  Rather
12590      than die, we'll just produce deficient debug info now, in that we will
12591      have variables without a proper abstract origin.  In the future, when all
12592      functions are lowered, we should re-add a gcc_assert (origin_die)
12593      here.  */
12594
12595   if (origin_die)
12596     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12597   return origin_die;
12598 }
12599
12600 /* We do not currently support the pure_virtual attribute.  */
12601
12602 static inline void
12603 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12604 {
12605   if (DECL_VINDEX (func_decl))
12606     {
12607       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12608
12609       if (host_integerp (DECL_VINDEX (func_decl), 0))
12610         add_AT_loc (die, DW_AT_vtable_elem_location,
12611                     new_loc_descr (DW_OP_constu,
12612                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12613                                    0));
12614
12615       /* GNU extension: Record what type this method came from originally.  */
12616       if (debug_info_level > DINFO_LEVEL_TERSE)
12617         add_AT_die_ref (die, DW_AT_containing_type,
12618                         lookup_type_die (DECL_CONTEXT (func_decl)));
12619     }
12620 }
12621 \f
12622 /* Add source coordinate attributes for the given decl.  */
12623
12624 static void
12625 add_src_coords_attributes (dw_die_ref die, tree decl)
12626 {
12627   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12628
12629   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12630   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12631 }
12632
12633 /* Add a DW_AT_name attribute and source coordinate attribute for the
12634    given decl, but only if it actually has a name.  */
12635
12636 static void
12637 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12638 {
12639   tree decl_name;
12640
12641   decl_name = DECL_NAME (decl);
12642   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12643     {
12644       add_name_attribute (die, dwarf2_name (decl, 0));
12645       if (! DECL_ARTIFICIAL (decl))
12646         add_src_coords_attributes (die, decl);
12647
12648       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12649           && TREE_PUBLIC (decl)
12650           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12651           && !DECL_ABSTRACT (decl)
12652           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12653           && !is_fortran ())
12654         add_AT_string (die, DW_AT_MIPS_linkage_name,
12655                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12656     }
12657
12658 #ifdef VMS_DEBUGGING_INFO
12659   /* Get the function's name, as described by its RTL.  This may be different
12660      from the DECL_NAME name used in the source file.  */
12661   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12662     {
12663       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12664                    XEXP (DECL_RTL (decl), 0));
12665       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12666     }
12667 #endif
12668 }
12669
12670 /* Push a new declaration scope.  */
12671
12672 static void
12673 push_decl_scope (tree scope)
12674 {
12675   VEC_safe_push (tree, gc, decl_scope_table, scope);
12676 }
12677
12678 /* Pop a declaration scope.  */
12679
12680 static inline void
12681 pop_decl_scope (void)
12682 {
12683   VEC_pop (tree, decl_scope_table);
12684 }
12685
12686 /* Return the DIE for the scope that immediately contains this type.
12687    Non-named types get global scope.  Named types nested in other
12688    types get their containing scope if it's open, or global scope
12689    otherwise.  All other types (i.e. function-local named types) get
12690    the current active scope.  */
12691
12692 static dw_die_ref
12693 scope_die_for (tree t, dw_die_ref context_die)
12694 {
12695   dw_die_ref scope_die = NULL;
12696   tree containing_scope;
12697   int i;
12698
12699   /* Non-types always go in the current scope.  */
12700   gcc_assert (TYPE_P (t));
12701
12702   containing_scope = TYPE_CONTEXT (t);
12703
12704   /* Use the containing namespace if it was passed in (for a declaration).  */
12705   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12706     {
12707       if (context_die == lookup_decl_die (containing_scope))
12708         /* OK */;
12709       else
12710         containing_scope = NULL_TREE;
12711     }
12712
12713   /* Ignore function type "scopes" from the C frontend.  They mean that
12714      a tagged type is local to a parmlist of a function declarator, but
12715      that isn't useful to DWARF.  */
12716   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12717     containing_scope = NULL_TREE;
12718
12719   if (containing_scope == NULL_TREE)
12720     scope_die = comp_unit_die;
12721   else if (TYPE_P (containing_scope))
12722     {
12723       /* For types, we can just look up the appropriate DIE.  But
12724          first we check to see if we're in the middle of emitting it
12725          so we know where the new DIE should go.  */
12726       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12727         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12728           break;
12729
12730       if (i < 0)
12731         {
12732           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12733                       || TREE_ASM_WRITTEN (containing_scope));
12734
12735           /* If none of the current dies are suitable, we get file scope.  */
12736           scope_die = comp_unit_die;
12737         }
12738       else
12739         scope_die = lookup_type_die (containing_scope);
12740     }
12741   else
12742     scope_die = context_die;
12743
12744   return scope_die;
12745 }
12746
12747 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12748
12749 static inline int
12750 local_scope_p (dw_die_ref context_die)
12751 {
12752   for (; context_die; context_die = context_die->die_parent)
12753     if (context_die->die_tag == DW_TAG_inlined_subroutine
12754         || context_die->die_tag == DW_TAG_subprogram)
12755       return 1;
12756
12757   return 0;
12758 }
12759
12760 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12761    whether or not to treat a DIE in this context as a declaration.  */
12762
12763 static inline int
12764 class_or_namespace_scope_p (dw_die_ref context_die)
12765 {
12766   return (context_die
12767           && (context_die->die_tag == DW_TAG_structure_type
12768               || context_die->die_tag == DW_TAG_class_type
12769               || context_die->die_tag == DW_TAG_interface_type
12770               || context_die->die_tag == DW_TAG_union_type
12771               || context_die->die_tag == DW_TAG_namespace));
12772 }
12773
12774 /* Many forms of DIEs require a "type description" attribute.  This
12775    routine locates the proper "type descriptor" die for the type given
12776    by 'type', and adds a DW_AT_type attribute below the given die.  */
12777
12778 static void
12779 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12780                     int decl_volatile, dw_die_ref context_die)
12781 {
12782   enum tree_code code  = TREE_CODE (type);
12783   dw_die_ref type_die  = NULL;
12784
12785   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12786      or fixed-point type, use the inner type.  This is because we have no
12787      support for unnamed types in base_type_die.  This can happen if this is
12788      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12789   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12790       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12791     type = TREE_TYPE (type), code = TREE_CODE (type);
12792
12793   if (code == ERROR_MARK
12794       /* Handle a special case.  For functions whose return type is void, we
12795          generate *no* type attribute.  (Note that no object may have type
12796          `void', so this only applies to function return types).  */
12797       || code == VOID_TYPE)
12798     return;
12799
12800   type_die = modified_type_die (type,
12801                                 decl_const || TYPE_READONLY (type),
12802                                 decl_volatile || TYPE_VOLATILE (type),
12803                                 context_die);
12804
12805   if (type_die != NULL)
12806     add_AT_die_ref (object_die, DW_AT_type, type_die);
12807 }
12808
12809 /* Given an object die, add the calling convention attribute for the
12810    function call type.  */
12811 static void
12812 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12813 {
12814   enum dwarf_calling_convention value = DW_CC_normal;
12815
12816   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12817
12818   /* DWARF doesn't provide a way to identify a program's source-level
12819      entry point.  DW_AT_calling_convention attributes are only meant
12820      to describe functions' calling conventions.  However, lacking a
12821      better way to signal the Fortran main program, we use this for the
12822      time being, following existing custom.  */
12823   if (is_fortran ()
12824       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12825     value = DW_CC_program;
12826
12827   /* Only add the attribute if the backend requests it, and
12828      is not DW_CC_normal.  */
12829   if (value && (value != DW_CC_normal))
12830     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12831 }
12832
12833 /* Given a tree pointer to a struct, class, union, or enum type node, return
12834    a pointer to the (string) tag name for the given type, or zero if the type
12835    was declared without a tag.  */
12836
12837 static const char *
12838 type_tag (const_tree type)
12839 {
12840   const char *name = 0;
12841
12842   if (TYPE_NAME (type) != 0)
12843     {
12844       tree t = 0;
12845
12846       /* Find the IDENTIFIER_NODE for the type name.  */
12847       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12848         t = TYPE_NAME (type);
12849
12850       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12851          a TYPE_DECL node, regardless of whether or not a `typedef' was
12852          involved.  */
12853       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12854                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12855         {
12856           /* We want to be extra verbose.  Don't call dwarf_name if
12857              DECL_NAME isn't set.  The default hook for decl_printable_name
12858              doesn't like that, and in this context it's correct to return
12859              0, instead of "<anonymous>" or the like.  */
12860           if (DECL_NAME (TYPE_NAME (type)))
12861             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12862         }
12863
12864       /* Now get the name as a string, or invent one.  */
12865       if (!name && t != 0)
12866         name = IDENTIFIER_POINTER (t);
12867     }
12868
12869   return (name == 0 || *name == '\0') ? 0 : name;
12870 }
12871
12872 /* Return the type associated with a data member, make a special check
12873    for bit field types.  */
12874
12875 static inline tree
12876 member_declared_type (const_tree member)
12877 {
12878   return (DECL_BIT_FIELD_TYPE (member)
12879           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12880 }
12881
12882 /* Get the decl's label, as described by its RTL. This may be different
12883    from the DECL_NAME name used in the source file.  */
12884
12885 #if 0
12886 static const char *
12887 decl_start_label (tree decl)
12888 {
12889   rtx x;
12890   const char *fnname;
12891
12892   x = DECL_RTL (decl);
12893   gcc_assert (MEM_P (x));
12894
12895   x = XEXP (x, 0);
12896   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12897
12898   fnname = XSTR (x, 0);
12899   return fnname;
12900 }
12901 #endif
12902 \f
12903 /* These routines generate the internal representation of the DIE's for
12904    the compilation unit.  Debugging information is collected by walking
12905    the declaration trees passed in from dwarf2out_decl().  */
12906
12907 static void
12908 gen_array_type_die (tree type, dw_die_ref context_die)
12909 {
12910   dw_die_ref scope_die = scope_die_for (type, context_die);
12911   dw_die_ref array_die;
12912
12913   /* GNU compilers represent multidimensional array types as sequences of one
12914      dimensional array types whose element types are themselves array types.
12915      We sometimes squish that down to a single array_type DIE with multiple
12916      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12917      say that we are allowed to do this kind of compression in C, because
12918      there is no difference between an array of arrays and a multidimensional
12919      array.  We don't do this for Ada to remain as close as possible to the
12920      actual representation, which is especially important against the language
12921      flexibilty wrt arrays of variable size.  */
12922
12923   bool collapse_nested_arrays = !is_ada ();
12924   tree element_type;
12925
12926   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12927      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12928   if (TYPE_STRING_FLAG (type)
12929       && TREE_CODE (type) == ARRAY_TYPE
12930       && is_fortran ()
12931       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12932     {
12933       HOST_WIDE_INT size;
12934
12935       array_die = new_die (DW_TAG_string_type, scope_die, type);
12936       add_name_attribute (array_die, type_tag (type));
12937       equate_type_number_to_die (type, array_die);
12938       size = int_size_in_bytes (type);
12939       if (size >= 0)
12940         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12941       else if (TYPE_DOMAIN (type) != NULL_TREE
12942                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12943                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12944         {
12945           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12946           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12947
12948           size = int_size_in_bytes (TREE_TYPE (szdecl));
12949           if (loc && size > 0)
12950             {
12951               add_AT_loc (array_die, DW_AT_string_length, loc);
12952               if (size != DWARF2_ADDR_SIZE)
12953                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12954             }
12955         }
12956       return;
12957     }
12958
12959   /* ??? The SGI dwarf reader fails for array of array of enum types
12960      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12961      array type comes before the outer array type.  We thus call gen_type_die
12962      before we new_die and must prevent nested array types collapsing for this
12963      target.  */
12964
12965 #ifdef MIPS_DEBUGGING_INFO
12966   gen_type_die (TREE_TYPE (type), context_die);
12967   collapse_nested_arrays = false;
12968 #endif
12969
12970   array_die = new_die (DW_TAG_array_type, scope_die, type);
12971   add_name_attribute (array_die, type_tag (type));
12972   equate_type_number_to_die (type, array_die);
12973
12974   if (TREE_CODE (type) == VECTOR_TYPE)
12975     {
12976       /* The frontend feeds us a representation for the vector as a struct
12977          containing an array.  Pull out the array type.  */
12978       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12979       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12980     }
12981
12982   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12983   if (is_fortran ()
12984       && TREE_CODE (type) == ARRAY_TYPE
12985       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12986       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12987     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12988
12989 #if 0
12990   /* We default the array ordering.  SDB will probably do
12991      the right things even if DW_AT_ordering is not present.  It's not even
12992      an issue until we start to get into multidimensional arrays anyway.  If
12993      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12994      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12995      and when we find out that we need to put these in, we will only do so
12996      for multidimensional arrays.  */
12997   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12998 #endif
12999
13000 #ifdef MIPS_DEBUGGING_INFO
13001   /* The SGI compilers handle arrays of unknown bound by setting
13002      AT_declaration and not emitting any subrange DIEs.  */
13003   if (! TYPE_DOMAIN (type))
13004     add_AT_flag (array_die, DW_AT_declaration, 1);
13005   else
13006 #endif
13007     add_subscript_info (array_die, type, collapse_nested_arrays);
13008
13009   /* Add representation of the type of the elements of this array type and
13010      emit the corresponding DIE if we haven't done it already.  */  
13011   element_type = TREE_TYPE (type);
13012   if (collapse_nested_arrays)
13013     while (TREE_CODE (element_type) == ARRAY_TYPE)
13014       {
13015         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13016           break;
13017         element_type = TREE_TYPE (element_type);
13018       }
13019
13020 #ifndef MIPS_DEBUGGING_INFO
13021   gen_type_die (element_type, context_die);
13022 #endif
13023
13024   add_type_attribute (array_die, element_type, 0, 0, context_die);
13025
13026   if (get_AT (array_die, DW_AT_name))
13027     add_pubtype (type, array_die);
13028 }
13029
13030 static dw_loc_descr_ref
13031 descr_info_loc (tree val, tree base_decl)
13032 {
13033   HOST_WIDE_INT size;
13034   dw_loc_descr_ref loc, loc2;
13035   enum dwarf_location_atom op;
13036
13037   if (val == base_decl)
13038     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13039
13040   switch (TREE_CODE (val))
13041     {
13042     CASE_CONVERT:
13043       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13044     case VAR_DECL:
13045       return loc_descriptor_from_tree_1 (val, 0);
13046     case INTEGER_CST:
13047       if (host_integerp (val, 0))
13048         return int_loc_descriptor (tree_low_cst (val, 0));
13049       break;
13050     case INDIRECT_REF:
13051       size = int_size_in_bytes (TREE_TYPE (val));
13052       if (size < 0)
13053         break;
13054       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13055       if (!loc)
13056         break;
13057       if (size == DWARF2_ADDR_SIZE)
13058         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13059       else
13060         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13061       return loc;
13062     case POINTER_PLUS_EXPR:
13063     case PLUS_EXPR:
13064       if (host_integerp (TREE_OPERAND (val, 1), 1)
13065           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13066              < 16384)
13067         {
13068           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13069           if (!loc)
13070             break;
13071           add_loc_descr (&loc,
13072                          new_loc_descr (DW_OP_plus_uconst,
13073                                         tree_low_cst (TREE_OPERAND (val, 1),
13074                                                       1), 0));
13075         }
13076       else
13077         {
13078           op = DW_OP_plus;
13079         do_binop:
13080           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13081           if (!loc)
13082             break;
13083           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13084           if (!loc2)
13085             break;
13086           add_loc_descr (&loc, loc2);
13087           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13088         }
13089       return loc;
13090     case MINUS_EXPR:
13091       op = DW_OP_minus;
13092       goto do_binop;
13093     case MULT_EXPR:
13094       op = DW_OP_mul;
13095       goto do_binop;
13096     case EQ_EXPR:
13097       op = DW_OP_eq;
13098       goto do_binop;
13099     case NE_EXPR:
13100       op = DW_OP_ne;
13101       goto do_binop;
13102     default:
13103       break;
13104     }
13105   return NULL;
13106 }
13107
13108 static void
13109 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13110                       tree val, tree base_decl)
13111 {
13112   dw_loc_descr_ref loc;
13113
13114   if (host_integerp (val, 0))
13115     {
13116       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13117       return;
13118     }
13119
13120   loc = descr_info_loc (val, base_decl);
13121   if (!loc)
13122     return;
13123
13124   add_AT_loc (die, attr, loc);
13125 }
13126
13127 /* This routine generates DIE for array with hidden descriptor, details
13128    are filled into *info by a langhook.  */
13129
13130 static void
13131 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13132                           dw_die_ref context_die)
13133 {
13134   dw_die_ref scope_die = scope_die_for (type, context_die);
13135   dw_die_ref array_die;
13136   int dim;
13137
13138   array_die = new_die (DW_TAG_array_type, scope_die, type);
13139   add_name_attribute (array_die, type_tag (type));
13140   equate_type_number_to_die (type, array_die);
13141
13142   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13143   if (is_fortran ()
13144       && info->ndimensions >= 2)
13145     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13146
13147   if (info->data_location)
13148     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13149                           info->base_decl);
13150   if (info->associated)
13151     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13152                           info->base_decl);
13153   if (info->allocated)
13154     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13155                           info->base_decl);
13156
13157   for (dim = 0; dim < info->ndimensions; dim++)
13158     {
13159       dw_die_ref subrange_die
13160         = new_die (DW_TAG_subrange_type, array_die, NULL);
13161
13162       if (info->dimen[dim].lower_bound)
13163         {
13164           /* If it is the default value, omit it.  */
13165           if ((is_c_family () || is_java ())
13166               && integer_zerop (info->dimen[dim].lower_bound))
13167             ;
13168           else if (is_fortran ()
13169                    && integer_onep (info->dimen[dim].lower_bound))
13170             ;
13171           else
13172             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13173                                   info->dimen[dim].lower_bound,
13174                                   info->base_decl);
13175         }
13176       if (info->dimen[dim].upper_bound)
13177         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13178                               info->dimen[dim].upper_bound,
13179                               info->base_decl);
13180       if (info->dimen[dim].stride)
13181         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13182                               info->dimen[dim].stride,
13183                               info->base_decl);
13184     }
13185
13186   gen_type_die (info->element_type, context_die);
13187   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13188
13189   if (get_AT (array_die, DW_AT_name))
13190     add_pubtype (type, array_die);
13191 }
13192
13193 #if 0
13194 static void
13195 gen_entry_point_die (tree decl, dw_die_ref context_die)
13196 {
13197   tree origin = decl_ultimate_origin (decl);
13198   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13199
13200   if (origin != NULL)
13201     add_abstract_origin_attribute (decl_die, origin);
13202   else
13203     {
13204       add_name_and_src_coords_attributes (decl_die, decl);
13205       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13206                           0, 0, context_die);
13207     }
13208
13209   if (DECL_ABSTRACT (decl))
13210     equate_decl_number_to_die (decl, decl_die);
13211   else
13212     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13213 }
13214 #endif
13215
13216 /* Walk through the list of incomplete types again, trying once more to
13217    emit full debugging info for them.  */
13218
13219 static void
13220 retry_incomplete_types (void)
13221 {
13222   int i;
13223
13224   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13225     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
13226                                   DINFO_USAGE_DIR_USE))
13227       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13228 }
13229
13230 /* Determine what tag to use for a record type.  */
13231
13232 static enum dwarf_tag
13233 record_type_tag (tree type)
13234 {
13235   if (! lang_hooks.types.classify_record)
13236     return DW_TAG_structure_type;
13237
13238   switch (lang_hooks.types.classify_record (type))
13239     {
13240     case RECORD_IS_STRUCT:
13241       return DW_TAG_structure_type;
13242
13243     case RECORD_IS_CLASS:
13244       return DW_TAG_class_type;
13245
13246     case RECORD_IS_INTERFACE:
13247       return DW_TAG_interface_type;
13248
13249     default:
13250       gcc_unreachable ();
13251     }
13252 }
13253
13254 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13255    include all of the information about the enumeration values also. Each
13256    enumerated type name/value is listed as a child of the enumerated type
13257    DIE.  */
13258
13259 static dw_die_ref
13260 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13261 {
13262   dw_die_ref type_die = lookup_type_die (type);
13263
13264   if (type_die == NULL)
13265     {
13266       type_die = new_die (DW_TAG_enumeration_type,
13267                           scope_die_for (type, context_die), type);
13268       equate_type_number_to_die (type, type_die);
13269       add_name_attribute (type_die, type_tag (type));
13270     }
13271   else if (! TYPE_SIZE (type))
13272     return type_die;
13273   else
13274     remove_AT (type_die, DW_AT_declaration);
13275
13276   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13277      given enum type is incomplete, do not generate the DW_AT_byte_size
13278      attribute or the DW_AT_element_list attribute.  */
13279   if (TYPE_SIZE (type))
13280     {
13281       tree link;
13282
13283       TREE_ASM_WRITTEN (type) = 1;
13284       add_byte_size_attribute (type_die, type);
13285       if (TYPE_STUB_DECL (type) != NULL_TREE)
13286         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13287
13288       /* If the first reference to this type was as the return type of an
13289          inline function, then it may not have a parent.  Fix this now.  */
13290       if (type_die->die_parent == NULL)
13291         add_child_die (scope_die_for (type, context_die), type_die);
13292
13293       for (link = TYPE_VALUES (type);
13294            link != NULL; link = TREE_CHAIN (link))
13295         {
13296           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13297           tree value = TREE_VALUE (link);
13298
13299           add_name_attribute (enum_die,
13300                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13301
13302           if (TREE_CODE (value) == CONST_DECL)
13303             value = DECL_INITIAL (value);
13304
13305           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13306             /* DWARF2 does not provide a way of indicating whether or
13307                not enumeration constants are signed or unsigned.  GDB
13308                always assumes the values are signed, so we output all
13309                values as if they were signed.  That means that
13310                enumeration constants with very large unsigned values
13311                will appear to have negative values in the debugger.  */
13312             add_AT_int (enum_die, DW_AT_const_value,
13313                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13314         }
13315     }
13316   else
13317     add_AT_flag (type_die, DW_AT_declaration, 1);
13318
13319   if (get_AT (type_die, DW_AT_name))
13320     add_pubtype (type, type_die);
13321
13322   return type_die;
13323 }
13324
13325 /* Generate a DIE to represent either a real live formal parameter decl or to
13326    represent just the type of some formal parameter position in some function
13327    type.
13328
13329    Note that this routine is a bit unusual because its argument may be a
13330    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13331    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13332    node.  If it's the former then this function is being called to output a
13333    DIE to represent a formal parameter object (or some inlining thereof).  If
13334    it's the latter, then this function is only being called to output a
13335    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13336    argument type of some subprogram type.  */
13337
13338 static dw_die_ref
13339 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13340 {
13341   tree node_or_origin = node ? node : origin;
13342   dw_die_ref parm_die
13343     = new_die (DW_TAG_formal_parameter, context_die, node);
13344
13345   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13346     {
13347     case tcc_declaration:
13348       if (!origin)
13349         origin = decl_ultimate_origin (node);
13350       if (origin != NULL)
13351         add_abstract_origin_attribute (parm_die, origin);
13352       else
13353         {
13354           tree type = TREE_TYPE (node);
13355           add_name_and_src_coords_attributes (parm_die, node);
13356           if (DECL_BY_REFERENCE (node))
13357             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13358                                 context_die);
13359           else
13360             add_type_attribute (parm_die, type,
13361                                 TREE_READONLY (node),
13362                                 TREE_THIS_VOLATILE (node),
13363                                 context_die);
13364           if (DECL_ARTIFICIAL (node))
13365             add_AT_flag (parm_die, DW_AT_artificial, 1);
13366         }
13367
13368       if (node)
13369         equate_decl_number_to_die (node, parm_die);
13370       if (! DECL_ABSTRACT (node_or_origin))
13371         add_location_or_const_value_attribute (parm_die, node_or_origin,
13372                                                DW_AT_location);
13373
13374       break;
13375
13376     case tcc_type:
13377       /* We were called with some kind of a ..._TYPE node.  */
13378       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13379       break;
13380
13381     default:
13382       gcc_unreachable ();
13383     }
13384
13385   return parm_die;
13386 }
13387
13388 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13389    at the end of an (ANSI prototyped) formal parameters list.  */
13390
13391 static void
13392 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13393 {
13394   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13395 }
13396
13397 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13398    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13399    parameters as specified in some function type specification (except for
13400    those which appear as part of a function *definition*).  */
13401
13402 static void
13403 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13404 {
13405   tree link;
13406   tree formal_type = NULL;
13407   tree first_parm_type;
13408   tree arg;
13409
13410   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13411     {
13412       arg = DECL_ARGUMENTS (function_or_method_type);
13413       function_or_method_type = TREE_TYPE (function_or_method_type);
13414     }
13415   else
13416     arg = NULL_TREE;
13417
13418   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13419
13420   /* Make our first pass over the list of formal parameter types and output a
13421      DW_TAG_formal_parameter DIE for each one.  */
13422   for (link = first_parm_type; link; )
13423     {
13424       dw_die_ref parm_die;
13425
13426       formal_type = TREE_VALUE (link);
13427       if (formal_type == void_type_node)
13428         break;
13429
13430       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13431       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13432       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13433            && link == first_parm_type)
13434           || (arg && DECL_ARTIFICIAL (arg)))
13435         add_AT_flag (parm_die, DW_AT_artificial, 1);
13436
13437       link = TREE_CHAIN (link);
13438       if (arg)
13439         arg = TREE_CHAIN (arg);
13440     }
13441
13442   /* If this function type has an ellipsis, add a
13443      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13444   if (formal_type != void_type_node)
13445     gen_unspecified_parameters_die (function_or_method_type, context_die);
13446
13447   /* Make our second (and final) pass over the list of formal parameter types
13448      and output DIEs to represent those types (as necessary).  */
13449   for (link = TYPE_ARG_TYPES (function_or_method_type);
13450        link && TREE_VALUE (link);
13451        link = TREE_CHAIN (link))
13452     gen_type_die (TREE_VALUE (link), context_die);
13453 }
13454
13455 /* We want to generate the DIE for TYPE so that we can generate the
13456    die for MEMBER, which has been defined; we will need to refer back
13457    to the member declaration nested within TYPE.  If we're trying to
13458    generate minimal debug info for TYPE, processing TYPE won't do the
13459    trick; we need to attach the member declaration by hand.  */
13460
13461 static void
13462 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13463 {
13464   gen_type_die (type, context_die);
13465
13466   /* If we're trying to avoid duplicate debug info, we may not have
13467      emitted the member decl for this function.  Emit it now.  */
13468   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13469       && ! lookup_decl_die (member))
13470     {
13471       dw_die_ref type_die;
13472       gcc_assert (!decl_ultimate_origin (member));
13473
13474       push_decl_scope (type);
13475       type_die = lookup_type_die (type);
13476       if (TREE_CODE (member) == FUNCTION_DECL)
13477         gen_subprogram_die (member, type_die);
13478       else if (TREE_CODE (member) == FIELD_DECL)
13479         {
13480           /* Ignore the nameless fields that are used to skip bits but handle
13481              C++ anonymous unions and structs.  */
13482           if (DECL_NAME (member) != NULL_TREE
13483               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13484               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13485             {
13486               gen_type_die (member_declared_type (member), type_die);
13487               gen_field_die (member, type_die);
13488             }
13489         }
13490       else
13491         gen_variable_die (member, NULL_TREE, type_die);
13492
13493       pop_decl_scope ();
13494     }
13495 }
13496
13497 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13498    may later generate inlined and/or out-of-line instances of.  */
13499
13500 static void
13501 dwarf2out_abstract_function (tree decl)
13502 {
13503   dw_die_ref old_die;
13504   tree save_fn;
13505   tree context;
13506   int was_abstract = DECL_ABSTRACT (decl);
13507
13508   /* Make sure we have the actual abstract inline, not a clone.  */
13509   decl = DECL_ORIGIN (decl);
13510
13511   old_die = lookup_decl_die (decl);
13512   if (old_die && get_AT (old_die, DW_AT_inline))
13513     /* We've already generated the abstract instance.  */
13514     return;
13515
13516   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13517      we don't get confused by DECL_ABSTRACT.  */
13518   if (debug_info_level > DINFO_LEVEL_TERSE)
13519     {
13520       context = decl_class_context (decl);
13521       if (context)
13522         gen_type_die_for_member
13523           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13524     }
13525
13526   /* Pretend we've just finished compiling this function.  */
13527   save_fn = current_function_decl;
13528   current_function_decl = decl;
13529   push_cfun (DECL_STRUCT_FUNCTION (decl));
13530
13531   set_decl_abstract_flags (decl, 1);
13532   dwarf2out_decl (decl);
13533   if (! was_abstract)
13534     set_decl_abstract_flags (decl, 0);
13535
13536   current_function_decl = save_fn;
13537   pop_cfun ();
13538 }
13539
13540 /* Helper function of premark_used_types() which gets called through
13541    htab_traverse.
13542
13543    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13544    marked as unused by prune_unused_types.  */
13545
13546 static int
13547 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13548 {
13549   tree type;
13550   dw_die_ref die;
13551
13552   type = (tree) *slot;
13553   die = lookup_type_die (type);
13554   if (die != NULL)
13555     die->die_perennial_p = 1;
13556   return 1;
13557 }
13558
13559 /* Helper function of premark_types_used_by_global_vars which gets called
13560    through htab_traverse.
13561
13562    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13563    marked as unused by prune_unused_types. The DIE of the type is marked
13564    only if the global variable using the type will actually be emitted.  */
13565
13566 static int
13567 premark_types_used_by_global_vars_helper (void **slot,
13568                                           void *data ATTRIBUTE_UNUSED)
13569 {
13570   struct types_used_by_vars_entry *entry;
13571   dw_die_ref die;
13572
13573   entry = (struct types_used_by_vars_entry *) *slot;
13574   gcc_assert (entry->type != NULL
13575               && entry->var_decl != NULL);
13576   die = lookup_type_die (entry->type);
13577   if (die)
13578     {
13579       /* Ask cgraph if the global variable really is to be emitted.
13580          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
13581       struct varpool_node *node = varpool_node (entry->var_decl);
13582       if (node->needed)
13583         {
13584           die->die_perennial_p = 1;
13585           /* Keep the parent DIEs as well.  */
13586           while ((die = die->die_parent) && die->die_perennial_p == 0)
13587             die->die_perennial_p = 1;
13588         }
13589     }
13590   return 1;
13591 }
13592
13593 /* Mark all members of used_types_hash as perennial.  */
13594
13595 static void
13596 premark_used_types (void)
13597 {
13598   if (cfun && cfun->used_types_hash)
13599     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13600 }
13601
13602 /* Mark all members of types_used_by_vars_entry as perennial.  */
13603
13604 static void
13605 premark_types_used_by_global_vars (void)
13606 {
13607   if (types_used_by_vars_hash)
13608     htab_traverse (types_used_by_vars_hash,
13609                    premark_types_used_by_global_vars_helper, NULL);
13610 }
13611
13612 /* Generate a DIE to represent a declared function (either file-scope or
13613    block-local).  */
13614
13615 static void
13616 gen_subprogram_die (tree decl, dw_die_ref context_die)
13617 {
13618   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13619   tree origin = decl_ultimate_origin (decl);
13620   dw_die_ref subr_die;
13621   tree fn_arg_types;
13622   tree outer_scope;
13623   dw_die_ref old_die = lookup_decl_die (decl);
13624   int declaration = (current_function_decl != decl
13625                      || class_or_namespace_scope_p (context_die));
13626
13627   premark_used_types ();
13628
13629   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13630      started to generate the abstract instance of an inline, decided to output
13631      its containing class, and proceeded to emit the declaration of the inline
13632      from the member list for the class.  If so, DECLARATION takes priority;
13633      we'll get back to the abstract instance when done with the class.  */
13634
13635   /* The class-scope declaration DIE must be the primary DIE.  */
13636   if (origin && declaration && class_or_namespace_scope_p (context_die))
13637     {
13638       origin = NULL;
13639       gcc_assert (!old_die);
13640     }
13641
13642   /* Now that the C++ front end lazily declares artificial member fns, we
13643      might need to retrofit the declaration into its class.  */
13644   if (!declaration && !origin && !old_die
13645       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13646       && !class_or_namespace_scope_p (context_die)
13647       && debug_info_level > DINFO_LEVEL_TERSE)
13648     old_die = force_decl_die (decl);
13649
13650   if (origin != NULL)
13651     {
13652       gcc_assert (!declaration || local_scope_p (context_die));
13653
13654       /* Fixup die_parent for the abstract instance of a nested
13655          inline function.  */
13656       if (old_die && old_die->die_parent == NULL)
13657         add_child_die (context_die, old_die);
13658
13659       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13660       add_abstract_origin_attribute (subr_die, origin);
13661     }
13662   else if (old_die)
13663     {
13664       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13665       struct dwarf_file_data * file_index = lookup_filename (s.file);
13666
13667       if (!get_AT_flag (old_die, DW_AT_declaration)
13668           /* We can have a normal definition following an inline one in the
13669              case of redefinition of GNU C extern inlines.
13670              It seems reasonable to use AT_specification in this case.  */
13671           && !get_AT (old_die, DW_AT_inline))
13672         {
13673           /* Detect and ignore this case, where we are trying to output
13674              something we have already output.  */
13675           return;
13676         }
13677
13678       /* If the definition comes from the same place as the declaration,
13679          maybe use the old DIE.  We always want the DIE for this function
13680          that has the *_pc attributes to be under comp_unit_die so the
13681          debugger can find it.  We also need to do this for abstract
13682          instances of inlines, since the spec requires the out-of-line copy
13683          to have the same parent.  For local class methods, this doesn't
13684          apply; we just use the old DIE.  */
13685       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13686           && (DECL_ARTIFICIAL (decl)
13687               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13688                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13689                       == (unsigned) s.line))))
13690         {
13691           subr_die = old_die;
13692
13693           /* Clear out the declaration attribute and the formal parameters.
13694              Do not remove all children, because it is possible that this
13695              declaration die was forced using force_decl_die(). In such
13696              cases die that forced declaration die (e.g. TAG_imported_module)
13697              is one of the children that we do not want to remove.  */
13698           remove_AT (subr_die, DW_AT_declaration);
13699           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13700         }
13701       else
13702         {
13703           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13704           add_AT_specification (subr_die, old_die);
13705           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13706             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13707           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13708             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13709         }
13710     }
13711   else
13712     {
13713       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13714
13715       if (TREE_PUBLIC (decl))
13716         add_AT_flag (subr_die, DW_AT_external, 1);
13717
13718       add_name_and_src_coords_attributes (subr_die, decl);
13719       if (debug_info_level > DINFO_LEVEL_TERSE)
13720         {
13721           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13722           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13723                               0, 0, context_die);
13724         }
13725
13726       add_pure_or_virtual_attribute (subr_die, decl);
13727       if (DECL_ARTIFICIAL (decl))
13728         add_AT_flag (subr_die, DW_AT_artificial, 1);
13729
13730       if (TREE_PROTECTED (decl))
13731         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13732       else if (TREE_PRIVATE (decl))
13733         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13734     }
13735
13736   if (declaration)
13737     {
13738       if (!old_die || !get_AT (old_die, DW_AT_inline))
13739         {
13740           add_AT_flag (subr_die, DW_AT_declaration, 1);
13741
13742           /* The first time we see a member function, it is in the context of
13743              the class to which it belongs.  We make sure of this by emitting
13744              the class first.  The next time is the definition, which is
13745              handled above.  The two may come from the same source text.
13746
13747              Note that force_decl_die() forces function declaration die. It is
13748              later reused to represent definition.  */
13749           equate_decl_number_to_die (decl, subr_die);
13750         }
13751     }
13752   else if (DECL_ABSTRACT (decl))
13753     {
13754       if (DECL_DECLARED_INLINE_P (decl))
13755         {
13756           if (cgraph_function_possibly_inlined_p (decl))
13757             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13758           else
13759             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13760         }
13761       else
13762         {
13763           if (cgraph_function_possibly_inlined_p (decl))
13764             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13765           else
13766             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13767         }
13768
13769       if (DECL_DECLARED_INLINE_P (decl)
13770           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13771         add_AT_flag (subr_die, DW_AT_artificial, 1);
13772
13773       equate_decl_number_to_die (decl, subr_die);
13774     }
13775   else if (!DECL_EXTERNAL (decl))
13776     {
13777       HOST_WIDE_INT cfa_fb_offset;
13778
13779       if (!old_die || !get_AT (old_die, DW_AT_inline))
13780         equate_decl_number_to_die (decl, subr_die);
13781
13782       if (!flag_reorder_blocks_and_partition)
13783         {
13784           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13785                                        current_function_funcdef_no);
13786           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13787           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13788                                        current_function_funcdef_no);
13789           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13790
13791           add_pubname (decl, subr_die);
13792           add_arange (decl, subr_die);
13793         }
13794       else
13795         {  /* Do nothing for now; maybe need to duplicate die, one for
13796               hot section and one for cold section, then use the hot/cold
13797               section begin/end labels to generate the aranges...  */
13798           /*
13799             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13800             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13801             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13802             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13803
13804             add_pubname (decl, subr_die);
13805             add_arange (decl, subr_die);
13806             add_arange (decl, subr_die);
13807            */
13808         }
13809
13810 #ifdef MIPS_DEBUGGING_INFO
13811       /* Add a reference to the FDE for this routine.  */
13812       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13813 #endif
13814
13815       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13816
13817       /* We define the "frame base" as the function's CFA.  This is more
13818          convenient for several reasons: (1) It's stable across the prologue
13819          and epilogue, which makes it better than just a frame pointer,
13820          (2) With dwarf3, there exists a one-byte encoding that allows us
13821          to reference the .debug_frame data by proxy, but failing that,
13822          (3) We can at least reuse the code inspection and interpretation
13823          code that determines the CFA position at various points in the
13824          function.  */
13825       /* ??? Use some command-line or configury switch to enable the use
13826          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13827          consumers that understand it; fall back to "pure" dwarf2 and
13828          convert the CFA data into a location list.  */
13829       {
13830         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13831         if (list->dw_loc_next)
13832           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13833         else
13834           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13835       }
13836
13837       /* Compute a displacement from the "steady-state frame pointer" to
13838          the CFA.  The former is what all stack slots and argument slots
13839          will reference in the rtl; the later is what we've told the
13840          debugger about.  We'll need to adjust all frame_base references
13841          by this displacement.  */
13842       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13843
13844       if (cfun->static_chain_decl)
13845         add_AT_location_description (subr_die, DW_AT_static_link,
13846                  loc_descriptor_from_tree (cfun->static_chain_decl));
13847     }
13848
13849   /* Now output descriptions of the arguments for this function. This gets
13850      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13851      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13852      `...' at the end of the formal parameter list.  In order to find out if
13853      there was a trailing ellipsis or not, we must instead look at the type
13854      associated with the FUNCTION_DECL.  This will be a node of type
13855      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13856      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13857      an ellipsis at the end.  */
13858
13859   /* In the case where we are describing a mere function declaration, all we
13860      need to do here (and all we *can* do here) is to describe the *types* of
13861      its formal parameters.  */
13862   if (debug_info_level <= DINFO_LEVEL_TERSE)
13863     ;
13864   else if (declaration)
13865     gen_formal_types_die (decl, subr_die);
13866   else
13867     {
13868       /* Generate DIEs to represent all known formal parameters.  */
13869       tree arg_decls = DECL_ARGUMENTS (decl);
13870       tree parm;
13871
13872       /* When generating DIEs, generate the unspecified_parameters DIE
13873          instead if we come across the arg "__builtin_va_alist" */
13874       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13875         if (TREE_CODE (parm) == PARM_DECL)
13876           {
13877             if (DECL_NAME (parm)
13878                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13879                             "__builtin_va_alist"))
13880               gen_unspecified_parameters_die (parm, subr_die);
13881             else
13882               gen_decl_die (parm, NULL, subr_die);
13883           }
13884
13885       /* Decide whether we need an unspecified_parameters DIE at the end.
13886          There are 2 more cases to do this for: 1) the ansi ... declaration -
13887          this is detectable when the end of the arg list is not a
13888          void_type_node 2) an unprototyped function declaration (not a
13889          definition).  This just means that we have no info about the
13890          parameters at all.  */
13891       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13892       if (fn_arg_types != NULL)
13893         {
13894           /* This is the prototyped case, check for....  */
13895           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13896             gen_unspecified_parameters_die (decl, subr_die);
13897         }
13898       else if (DECL_INITIAL (decl) == NULL_TREE)
13899         gen_unspecified_parameters_die (decl, subr_die);
13900     }
13901
13902   /* Output Dwarf info for all of the stuff within the body of the function
13903      (if it has one - it may be just a declaration).  */
13904   outer_scope = DECL_INITIAL (decl);
13905
13906   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13907      a function.  This BLOCK actually represents the outermost binding contour
13908      for the function, i.e. the contour in which the function's formal
13909      parameters and labels get declared. Curiously, it appears that the front
13910      end doesn't actually put the PARM_DECL nodes for the current function onto
13911      the BLOCK_VARS list for this outer scope, but are strung off of the
13912      DECL_ARGUMENTS list for the function instead.
13913
13914      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13915      the LABEL_DECL nodes for the function however, and we output DWARF info
13916      for those in decls_for_scope.  Just within the `outer_scope' there will be
13917      a BLOCK node representing the function's outermost pair of curly braces,
13918      and any blocks used for the base and member initializers of a C++
13919      constructor function.  */
13920   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13921     {
13922       /* Emit a DW_TAG_variable DIE for a named return value.  */
13923       if (DECL_NAME (DECL_RESULT (decl)))
13924         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13925
13926       current_function_has_inlines = 0;
13927       decls_for_scope (outer_scope, subr_die, 0);
13928
13929 #if 0 && defined (MIPS_DEBUGGING_INFO)
13930       if (current_function_has_inlines)
13931         {
13932           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13933           if (! comp_unit_has_inlines)
13934             {
13935               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13936               comp_unit_has_inlines = 1;
13937             }
13938         }
13939 #endif
13940     }
13941   /* Add the calling convention attribute if requested.  */
13942   add_calling_convention_attribute (subr_die, decl);
13943
13944 }
13945
13946 /* Returns a hash value for X (which really is a die_struct).  */
13947
13948 static hashval_t
13949 common_block_die_table_hash (const void *x)
13950 {
13951   const_dw_die_ref d = (const_dw_die_ref) x;
13952   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13953 }
13954
13955 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13956    as decl_id and die_parent of die_struct Y.  */
13957
13958 static int
13959 common_block_die_table_eq (const void *x, const void *y)
13960 {
13961   const_dw_die_ref d = (const_dw_die_ref) x;
13962   const_dw_die_ref e = (const_dw_die_ref) y;
13963   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13964 }
13965
13966 /* Generate a DIE to represent a declared data object.
13967    Either DECL or ORIGIN must be non-null.  */
13968
13969 static void
13970 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13971 {
13972   HOST_WIDE_INT off;
13973   tree com_decl;
13974   tree decl_or_origin = decl ? decl : origin;
13975   dw_die_ref var_die;
13976   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13977   dw_die_ref origin_die;
13978   int declaration = (DECL_EXTERNAL (decl_or_origin)
13979                      /* If DECL is COMDAT and has not actually been
13980                         emitted, we cannot take its address; there
13981                         might end up being no definition anywhere in
13982                         the program.  For example, consider the C++
13983                         test case:
13984
13985                           template <class T>
13986                           struct S { static const int i = 7; };
13987
13988                           template <class T>
13989                           const int S<T>::i;
13990
13991                           int f() { return S<int>::i; }
13992
13993                         Here, S<int>::i is not DECL_EXTERNAL, but no
13994                         definition is required, so the compiler will
13995                         not emit a definition.  */
13996                      || (TREE_CODE (decl_or_origin) == VAR_DECL
13997                          && DECL_COMDAT (decl_or_origin)
13998                          && !TREE_ASM_WRITTEN (decl_or_origin))
13999                      || class_or_namespace_scope_p (context_die));
14000
14001   if (!origin)
14002     origin = decl_ultimate_origin (decl);
14003
14004   com_decl = fortran_common (decl_or_origin, &off);
14005
14006   /* Symbol in common gets emitted as a child of the common block, in the form
14007      of a data member.  */
14008   if (com_decl)
14009     {
14010       tree field;
14011       dw_die_ref com_die;
14012       dw_loc_descr_ref loc;
14013       die_node com_die_arg;
14014
14015       var_die = lookup_decl_die (decl_or_origin);
14016       if (var_die)
14017         {
14018           if (get_AT (var_die, DW_AT_location) == NULL)
14019             {
14020               loc = loc_descriptor_from_tree (com_decl);
14021               if (loc)
14022                 {
14023                   if (off)
14024                     {
14025                       /* Optimize the common case.  */
14026                       if (loc->dw_loc_opc == DW_OP_addr
14027                           && loc->dw_loc_next == NULL
14028                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14029                              == SYMBOL_REF)
14030                         loc->dw_loc_oprnd1.v.val_addr
14031                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14032                         else
14033                           add_loc_descr (&loc,
14034                                          new_loc_descr (DW_OP_plus_uconst,
14035                                                         off, 0));
14036                     }
14037                   add_AT_loc (var_die, DW_AT_location, loc);
14038                   remove_AT (var_die, DW_AT_declaration);
14039                 }
14040             }
14041           return;
14042         }
14043
14044       if (common_block_die_table == NULL)
14045         common_block_die_table
14046           = htab_create_ggc (10, common_block_die_table_hash,
14047                              common_block_die_table_eq, NULL);
14048
14049       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14050       com_die_arg.decl_id = DECL_UID (com_decl);
14051       com_die_arg.die_parent = context_die;
14052       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14053       loc = loc_descriptor_from_tree (com_decl);
14054       if (com_die == NULL)
14055         {
14056           const char *cnam
14057             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14058           void **slot;
14059
14060           com_die = new_die (DW_TAG_common_block, context_die, decl);
14061           add_name_and_src_coords_attributes (com_die, com_decl);
14062           if (loc)
14063             {
14064               add_AT_loc (com_die, DW_AT_location, loc);
14065               /* Avoid sharing the same loc descriptor between
14066                  DW_TAG_common_block and DW_TAG_variable.  */
14067               loc = loc_descriptor_from_tree (com_decl);
14068             }
14069           else if (DECL_EXTERNAL (decl))
14070             add_AT_flag (com_die, DW_AT_declaration, 1);
14071           add_pubname_string (cnam, com_die); /* ??? needed? */
14072           com_die->decl_id = DECL_UID (com_decl);
14073           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14074           *slot = (void *) com_die;
14075         }
14076       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14077         {
14078           add_AT_loc (com_die, DW_AT_location, loc);
14079           loc = loc_descriptor_from_tree (com_decl);
14080           remove_AT (com_die, DW_AT_declaration);
14081         }
14082       var_die = new_die (DW_TAG_variable, com_die, decl);
14083       add_name_and_src_coords_attributes (var_die, decl);
14084       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14085                           TREE_THIS_VOLATILE (decl), context_die);
14086       add_AT_flag (var_die, DW_AT_external, 1);
14087       if (loc)
14088         {
14089           if (off)
14090             {
14091               /* Optimize the common case.  */
14092               if (loc->dw_loc_opc == DW_OP_addr
14093                   && loc->dw_loc_next == NULL
14094                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14095                 loc->dw_loc_oprnd1.v.val_addr
14096                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14097               else
14098                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14099                                                     off, 0));
14100             }
14101           add_AT_loc (var_die, DW_AT_location, loc);
14102         }
14103       else if (DECL_EXTERNAL (decl))
14104         add_AT_flag (var_die, DW_AT_declaration, 1);
14105       equate_decl_number_to_die (decl, var_die);
14106       return;
14107     }
14108
14109   /* If the compiler emitted a definition for the DECL declaration
14110      and if we already emitted a DIE for it, don't emit a second
14111      DIE for it again.  */
14112   if (old_die
14113       && declaration
14114       && old_die->die_parent == context_die)
14115     return;
14116
14117   var_die = new_die (DW_TAG_variable, context_die, decl);
14118
14119   origin_die = NULL;
14120   if (origin != NULL)
14121     origin_die = add_abstract_origin_attribute (var_die, origin);
14122
14123   /* Loop unrolling can create multiple blocks that refer to the same
14124      static variable, so we must test for the DW_AT_declaration flag.
14125
14126      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14127      copy decls and set the DECL_ABSTRACT flag on them instead of
14128      sharing them.
14129
14130      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14131
14132      ??? The declare_in_namespace support causes us to get two DIEs for one
14133      variable, both of which are declarations.  We want to avoid considering
14134      one to be a specification, so we must test that this DIE is not a
14135      declaration.  */
14136   else if (old_die && TREE_STATIC (decl) && ! declaration
14137            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14138     {
14139       /* This is a definition of a C++ class level static.  */
14140       add_AT_specification (var_die, old_die);
14141       if (DECL_NAME (decl))
14142         {
14143           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14144           struct dwarf_file_data * file_index = lookup_filename (s.file);
14145
14146           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14147             add_AT_file (var_die, DW_AT_decl_file, file_index);
14148
14149           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14150             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14151         }
14152     }
14153   else
14154     {
14155       tree type = TREE_TYPE (decl);
14156
14157       add_name_and_src_coords_attributes (var_die, decl);
14158       if ((TREE_CODE (decl) == PARM_DECL
14159            || TREE_CODE (decl) == RESULT_DECL)
14160           && DECL_BY_REFERENCE (decl))
14161         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14162       else
14163         add_type_attribute (var_die, type, TREE_READONLY (decl),
14164                             TREE_THIS_VOLATILE (decl), context_die);
14165
14166       if (TREE_PUBLIC (decl))
14167         add_AT_flag (var_die, DW_AT_external, 1);
14168
14169       if (DECL_ARTIFICIAL (decl))
14170         add_AT_flag (var_die, DW_AT_artificial, 1);
14171
14172       if (TREE_PROTECTED (decl))
14173         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14174       else if (TREE_PRIVATE (decl))
14175         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14176     }
14177
14178   if (declaration)
14179     add_AT_flag (var_die, DW_AT_declaration, 1);
14180
14181   if (decl && (DECL_ABSTRACT (decl) || declaration))
14182     equate_decl_number_to_die (decl, var_die);
14183
14184   if (! declaration
14185       && (! DECL_ABSTRACT (decl_or_origin)
14186           /* Local static vars are shared between all clones/inlines,
14187              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14188              already set.  */
14189           || (TREE_CODE (decl_or_origin) == VAR_DECL
14190               && TREE_STATIC (decl_or_origin)
14191               && DECL_RTL_SET_P (decl_or_origin)))
14192       /* When abstract origin already has DW_AT_location attribute, no need
14193          to add it again.  */
14194       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14195     {
14196       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14197           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14198         defer_location (decl_or_origin, var_die);
14199       else
14200         add_location_or_const_value_attribute (var_die,
14201                                                decl_or_origin,
14202                                                DW_AT_location);
14203       add_pubname (decl_or_origin, var_die);
14204     }
14205   else
14206     tree_add_const_value_attribute (var_die, decl_or_origin);
14207 }
14208
14209 /* Generate a DIE to represent a named constant.  */
14210
14211 static void
14212 gen_const_die (tree decl, dw_die_ref context_die)
14213 {
14214   dw_die_ref const_die;
14215   tree type = TREE_TYPE (decl);
14216
14217   const_die = new_die (DW_TAG_constant, context_die, decl);
14218   add_name_and_src_coords_attributes (const_die, decl);
14219   add_type_attribute (const_die, type, 1, 0, context_die);
14220   if (TREE_PUBLIC (decl))
14221     add_AT_flag (const_die, DW_AT_external, 1);
14222   if (DECL_ARTIFICIAL (decl))
14223     add_AT_flag (const_die, DW_AT_artificial, 1);
14224   tree_add_const_value_attribute (const_die, decl);
14225 }
14226
14227 /* Generate a DIE to represent a label identifier.  */
14228
14229 static void
14230 gen_label_die (tree decl, dw_die_ref context_die)
14231 {
14232   tree origin = decl_ultimate_origin (decl);
14233   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14234   rtx insn;
14235   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14236
14237   if (origin != NULL)
14238     add_abstract_origin_attribute (lbl_die, origin);
14239   else
14240     add_name_and_src_coords_attributes (lbl_die, decl);
14241
14242   if (DECL_ABSTRACT (decl))
14243     equate_decl_number_to_die (decl, lbl_die);
14244   else
14245     {
14246       insn = DECL_RTL_IF_SET (decl);
14247
14248       /* Deleted labels are programmer specified labels which have been
14249          eliminated because of various optimizations.  We still emit them
14250          here so that it is possible to put breakpoints on them.  */
14251       if (insn
14252           && (LABEL_P (insn)
14253               || ((NOTE_P (insn)
14254                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14255         {
14256           /* When optimization is enabled (via -O) some parts of the compiler
14257              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14258              represent source-level labels which were explicitly declared by
14259              the user.  This really shouldn't be happening though, so catch
14260              it if it ever does happen.  */
14261           gcc_assert (!INSN_DELETED_P (insn));
14262
14263           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14264           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14265         }
14266     }
14267 }
14268
14269 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14270    attributes to the DIE for a block STMT, to describe where the inlined
14271    function was called from.  This is similar to add_src_coords_attributes.  */
14272
14273 static inline void
14274 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14275 {
14276   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14277
14278   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14279   add_AT_unsigned (die, DW_AT_call_line, s.line);
14280 }
14281
14282
14283 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14284    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14285
14286 static inline void
14287 add_high_low_attributes (tree stmt, dw_die_ref die)
14288 {
14289   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14290
14291   if (BLOCK_FRAGMENT_CHAIN (stmt))
14292     {
14293       tree chain;
14294
14295       if (inlined_function_outer_scope_p (stmt))
14296         {
14297           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14298                                        BLOCK_NUMBER (stmt));
14299           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14300         }
14301
14302       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14303
14304       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14305       do
14306         {
14307           add_ranges (chain);
14308           chain = BLOCK_FRAGMENT_CHAIN (chain);
14309         }
14310       while (chain);
14311       add_ranges (NULL);
14312     }
14313   else
14314     {
14315       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14316                                    BLOCK_NUMBER (stmt));
14317       add_AT_lbl_id (die, DW_AT_low_pc, label);
14318       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14319                                    BLOCK_NUMBER (stmt));
14320       add_AT_lbl_id (die, DW_AT_high_pc, label);
14321     }
14322 }
14323
14324 /* Generate a DIE for a lexical block.  */
14325
14326 static void
14327 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14328 {
14329   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14330
14331   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14332     add_high_low_attributes (stmt, stmt_die);
14333
14334   decls_for_scope (stmt, stmt_die, depth);
14335 }
14336
14337 /* Generate a DIE for an inlined subprogram.  */
14338
14339 static void
14340 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14341 {
14342   tree decl;
14343
14344   /* The instance of function that is effectively being inlined shall not
14345      be abstract.  */
14346   gcc_assert (! BLOCK_ABSTRACT (stmt));
14347
14348   decl = block_ultimate_origin (stmt);
14349
14350   /* Emit info for the abstract instance first, if we haven't yet.  We
14351      must emit this even if the block is abstract, otherwise when we
14352      emit the block below (or elsewhere), we may end up trying to emit
14353      a die whose origin die hasn't been emitted, and crashing.  */
14354   dwarf2out_abstract_function (decl);
14355
14356   if (! BLOCK_ABSTRACT (stmt))
14357     {
14358       dw_die_ref subr_die
14359         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14360
14361       add_abstract_origin_attribute (subr_die, decl);
14362       if (TREE_ASM_WRITTEN (stmt))
14363         add_high_low_attributes (stmt, subr_die);
14364       add_call_src_coords_attributes (stmt, subr_die);
14365
14366       decls_for_scope (stmt, subr_die, depth);
14367       current_function_has_inlines = 1;
14368     }
14369 }
14370
14371 /* Generate a DIE for a field in a record, or structure.  */
14372
14373 static void
14374 gen_field_die (tree decl, dw_die_ref context_die)
14375 {
14376   dw_die_ref decl_die;
14377
14378   if (TREE_TYPE (decl) == error_mark_node)
14379     return;
14380
14381   decl_die = new_die (DW_TAG_member, context_die, decl);
14382   add_name_and_src_coords_attributes (decl_die, decl);
14383   add_type_attribute (decl_die, member_declared_type (decl),
14384                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14385                       context_die);
14386
14387   if (DECL_BIT_FIELD_TYPE (decl))
14388     {
14389       add_byte_size_attribute (decl_die, decl);
14390       add_bit_size_attribute (decl_die, decl);
14391       add_bit_offset_attribute (decl_die, decl);
14392     }
14393
14394   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14395     add_data_member_location_attribute (decl_die, decl);
14396
14397   if (DECL_ARTIFICIAL (decl))
14398     add_AT_flag (decl_die, DW_AT_artificial, 1);
14399
14400   if (TREE_PROTECTED (decl))
14401     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14402   else if (TREE_PRIVATE (decl))
14403     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14404
14405   /* Equate decl number to die, so that we can look up this decl later on.  */
14406   equate_decl_number_to_die (decl, decl_die);
14407 }
14408
14409 #if 0
14410 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14411    Use modified_type_die instead.
14412    We keep this code here just in case these types of DIEs may be needed to
14413    represent certain things in other languages (e.g. Pascal) someday.  */
14414
14415 static void
14416 gen_pointer_type_die (tree type, dw_die_ref context_die)
14417 {
14418   dw_die_ref ptr_die
14419     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14420
14421   equate_type_number_to_die (type, ptr_die);
14422   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14423   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14424 }
14425
14426 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14427    Use modified_type_die instead.
14428    We keep this code here just in case these types of DIEs may be needed to
14429    represent certain things in other languages (e.g. Pascal) someday.  */
14430
14431 static void
14432 gen_reference_type_die (tree type, dw_die_ref context_die)
14433 {
14434   dw_die_ref ref_die
14435     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14436
14437   equate_type_number_to_die (type, ref_die);
14438   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14439   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14440 }
14441 #endif
14442
14443 /* Generate a DIE for a pointer to a member type.  */
14444
14445 static void
14446 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14447 {
14448   dw_die_ref ptr_die
14449     = new_die (DW_TAG_ptr_to_member_type,
14450                scope_die_for (type, context_die), type);
14451
14452   equate_type_number_to_die (type, ptr_die);
14453   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14454                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14455   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14456 }
14457
14458 /* Generate the DIE for the compilation unit.  */
14459
14460 static dw_die_ref
14461 gen_compile_unit_die (const char *filename)
14462 {
14463   dw_die_ref die;
14464   char producer[250];
14465   const char *language_string = lang_hooks.name;
14466   int language;
14467
14468   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14469
14470   if (filename)
14471     {
14472       add_name_attribute (die, filename);
14473       /* Don't add cwd for <built-in>.  */
14474       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14475         add_comp_dir_attribute (die);
14476     }
14477
14478   sprintf (producer, "%s %s", language_string, version_string);
14479
14480 #ifdef MIPS_DEBUGGING_INFO
14481   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14482      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14483      not appear in the producer string, the debugger reaches the conclusion
14484      that the object file is stripped and has no debugging information.
14485      To get the MIPS/SGI debugger to believe that there is debugging
14486      information in the object file, we add a -g to the producer string.  */
14487   if (debug_info_level > DINFO_LEVEL_TERSE)
14488     strcat (producer, " -g");
14489 #endif
14490
14491   add_AT_string (die, DW_AT_producer, producer);
14492
14493   if (strcmp (language_string, "GNU C++") == 0)
14494     language = DW_LANG_C_plus_plus;
14495   else if (strcmp (language_string, "GNU Ada") == 0)
14496     language = DW_LANG_Ada95;
14497   else if (strcmp (language_string, "GNU F77") == 0)
14498     language = DW_LANG_Fortran77;
14499   else if (strcmp (language_string, "GNU Fortran") == 0)
14500     language = DW_LANG_Fortran95;
14501   else if (strcmp (language_string, "GNU Pascal") == 0)
14502     language = DW_LANG_Pascal83;
14503   else if (strcmp (language_string, "GNU Java") == 0)
14504     language = DW_LANG_Java;
14505   else if (strcmp (language_string, "GNU Objective-C") == 0)
14506     language = DW_LANG_ObjC;
14507   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14508     language = DW_LANG_ObjC_plus_plus;
14509   else
14510     language = DW_LANG_C89;
14511
14512   add_AT_unsigned (die, DW_AT_language, language);
14513   return die;
14514 }
14515
14516 /* Generate the DIE for a base class.  */
14517
14518 static void
14519 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14520 {
14521   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14522
14523   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14524   add_data_member_location_attribute (die, binfo);
14525
14526   if (BINFO_VIRTUAL_P (binfo))
14527     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14528
14529   if (access == access_public_node)
14530     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14531   else if (access == access_protected_node)
14532     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14533 }
14534
14535 /* Generate a DIE for a class member.  */
14536
14537 static void
14538 gen_member_die (tree type, dw_die_ref context_die)
14539 {
14540   tree member;
14541   tree binfo = TYPE_BINFO (type);
14542   dw_die_ref child;
14543
14544   /* If this is not an incomplete type, output descriptions of each of its
14545      members. Note that as we output the DIEs necessary to represent the
14546      members of this record or union type, we will also be trying to output
14547      DIEs to represent the *types* of those members. However the `type'
14548      function (above) will specifically avoid generating type DIEs for member
14549      types *within* the list of member DIEs for this (containing) type except
14550      for those types (of members) which are explicitly marked as also being
14551      members of this (containing) type themselves.  The g++ front- end can
14552      force any given type to be treated as a member of some other (containing)
14553      type by setting the TYPE_CONTEXT of the given (member) type to point to
14554      the TREE node representing the appropriate (containing) type.  */
14555
14556   /* First output info about the base classes.  */
14557   if (binfo)
14558     {
14559       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14560       int i;
14561       tree base;
14562
14563       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14564         gen_inheritance_die (base,
14565                              (accesses ? VEC_index (tree, accesses, i)
14566                               : access_public_node), context_die);
14567     }
14568
14569   /* Now output info about the data members and type members.  */
14570   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14571     {
14572       /* If we thought we were generating minimal debug info for TYPE
14573          and then changed our minds, some of the member declarations
14574          may have already been defined.  Don't define them again, but
14575          do put them in the right order.  */
14576
14577       child = lookup_decl_die (member);
14578       if (child)
14579         splice_child_die (context_die, child);
14580       else
14581         gen_decl_die (member, NULL, context_die);
14582     }
14583
14584   /* Now output info about the function members (if any).  */
14585   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14586     {
14587       /* Don't include clones in the member list.  */
14588       if (DECL_ABSTRACT_ORIGIN (member))
14589         continue;
14590
14591       child = lookup_decl_die (member);
14592       if (child)
14593         splice_child_die (context_die, child);
14594       else
14595         gen_decl_die (member, NULL, context_die);
14596     }
14597 }
14598
14599 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14600    is set, we pretend that the type was never defined, so we only get the
14601    member DIEs needed by later specification DIEs.  */
14602
14603 static void
14604 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14605                                 enum debug_info_usage usage)
14606 {
14607   dw_die_ref type_die = lookup_type_die (type);
14608   dw_die_ref scope_die = 0;
14609   int nested = 0;
14610   int complete = (TYPE_SIZE (type)
14611                   && (! TYPE_STUB_DECL (type)
14612                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14613   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14614   complete = complete && should_emit_struct_debug (type, usage);
14615
14616   if (type_die && ! complete)
14617     return;
14618
14619   if (TYPE_CONTEXT (type) != NULL_TREE
14620       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14621           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14622     nested = 1;
14623
14624   scope_die = scope_die_for (type, context_die);
14625
14626   if (! type_die || (nested && scope_die == comp_unit_die))
14627     /* First occurrence of type or toplevel definition of nested class.  */
14628     {
14629       dw_die_ref old_die = type_die;
14630
14631       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14632                           ? record_type_tag (type) : DW_TAG_union_type,
14633                           scope_die, type);
14634       equate_type_number_to_die (type, type_die);
14635       if (old_die)
14636         add_AT_specification (type_die, old_die);
14637       else
14638         add_name_attribute (type_die, type_tag (type));
14639     }
14640   else
14641     remove_AT (type_die, DW_AT_declaration);
14642
14643   /* If this type has been completed, then give it a byte_size attribute and
14644      then give a list of members.  */
14645   if (complete && !ns_decl)
14646     {
14647       /* Prevent infinite recursion in cases where the type of some member of
14648          this type is expressed in terms of this type itself.  */
14649       TREE_ASM_WRITTEN (type) = 1;
14650       add_byte_size_attribute (type_die, type);
14651       if (TYPE_STUB_DECL (type) != NULL_TREE)
14652         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14653
14654       /* If the first reference to this type was as the return type of an
14655          inline function, then it may not have a parent.  Fix this now.  */
14656       if (type_die->die_parent == NULL)
14657         add_child_die (scope_die, type_die);
14658
14659       push_decl_scope (type);
14660       gen_member_die (type, type_die);
14661       pop_decl_scope ();
14662
14663       /* GNU extension: Record what type our vtable lives in.  */
14664       if (TYPE_VFIELD (type))
14665         {
14666           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14667
14668           gen_type_die (vtype, context_die);
14669           add_AT_die_ref (type_die, DW_AT_containing_type,
14670                           lookup_type_die (vtype));
14671         }
14672     }
14673   else
14674     {
14675       add_AT_flag (type_die, DW_AT_declaration, 1);
14676
14677       /* We don't need to do this for function-local types.  */
14678       if (TYPE_STUB_DECL (type)
14679           && ! decl_function_context (TYPE_STUB_DECL (type)))
14680         VEC_safe_push (tree, gc, incomplete_types, type);
14681     }
14682
14683   if (get_AT (type_die, DW_AT_name))
14684     add_pubtype (type, type_die);
14685 }
14686
14687 /* Generate a DIE for a subroutine _type_.  */
14688
14689 static void
14690 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14691 {
14692   tree return_type = TREE_TYPE (type);
14693   dw_die_ref subr_die
14694     = new_die (DW_TAG_subroutine_type,
14695                scope_die_for (type, context_die), type);
14696
14697   equate_type_number_to_die (type, subr_die);
14698   add_prototyped_attribute (subr_die, type);
14699   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14700   gen_formal_types_die (type, subr_die);
14701
14702   if (get_AT (subr_die, DW_AT_name))
14703     add_pubtype (type, subr_die);
14704 }
14705
14706 /* Generate a DIE for a type definition.  */
14707
14708 static void
14709 gen_typedef_die (tree decl, dw_die_ref context_die)
14710 {
14711   dw_die_ref type_die;
14712   tree origin;
14713
14714   if (TREE_ASM_WRITTEN (decl))
14715     return;
14716
14717   TREE_ASM_WRITTEN (decl) = 1;
14718   type_die = new_die (DW_TAG_typedef, context_die, decl);
14719   origin = decl_ultimate_origin (decl);
14720   if (origin != NULL)
14721     add_abstract_origin_attribute (type_die, origin);
14722   else
14723     {
14724       tree type;
14725
14726       add_name_and_src_coords_attributes (type_die, decl);
14727       if (DECL_ORIGINAL_TYPE (decl))
14728         {
14729           type = DECL_ORIGINAL_TYPE (decl);
14730
14731           gcc_assert (type != TREE_TYPE (decl));
14732           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14733         }
14734       else
14735         type = TREE_TYPE (decl);
14736
14737       add_type_attribute (type_die, type, TREE_READONLY (decl),
14738                           TREE_THIS_VOLATILE (decl), context_die);
14739     }
14740
14741   if (DECL_ABSTRACT (decl))
14742     equate_decl_number_to_die (decl, type_die);
14743
14744   if (get_AT (type_die, DW_AT_name))
14745     add_pubtype (decl, type_die);
14746 }
14747
14748 /* Generate a type description DIE.  */
14749
14750 static void
14751 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14752                                 enum debug_info_usage usage)
14753 {
14754   int need_pop;
14755   struct array_descr_info info;
14756
14757   if (type == NULL_TREE || type == error_mark_node)
14758     return;
14759
14760   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14761       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14762     {
14763       if (TREE_ASM_WRITTEN (type))
14764         return;
14765
14766       /* Prevent broken recursion; we can't hand off to the same type.  */
14767       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14768
14769       /* Use the DIE of the containing namespace as the parent DIE of
14770          the type description DIE we want to generate.  */
14771       if (DECL_CONTEXT (TYPE_NAME (type))
14772           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
14773         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
14774
14775       TREE_ASM_WRITTEN (type) = 1;
14776       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14777       return;
14778     }
14779
14780   /* If this is an array type with hidden descriptor, handle it first.  */
14781   if (!TREE_ASM_WRITTEN (type)
14782       && lang_hooks.types.get_array_descr_info
14783       && lang_hooks.types.get_array_descr_info (type, &info))
14784     {
14785       gen_descr_array_type_die (type, &info, context_die);
14786       TREE_ASM_WRITTEN (type) = 1;
14787       return;
14788     }
14789
14790   /* We are going to output a DIE to represent the unqualified version
14791      of this type (i.e. without any const or volatile qualifiers) so
14792      get the main variant (i.e. the unqualified version) of this type
14793      now.  (Vectors are special because the debugging info is in the
14794      cloned type itself).  */
14795   if (TREE_CODE (type) != VECTOR_TYPE)
14796     type = type_main_variant (type);
14797
14798   if (TREE_ASM_WRITTEN (type))
14799     return;
14800
14801   switch (TREE_CODE (type))
14802     {
14803     case ERROR_MARK:
14804       break;
14805
14806     case POINTER_TYPE:
14807     case REFERENCE_TYPE:
14808       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14809          ensures that the gen_type_die recursion will terminate even if the
14810          type is recursive.  Recursive types are possible in Ada.  */
14811       /* ??? We could perhaps do this for all types before the switch
14812          statement.  */
14813       TREE_ASM_WRITTEN (type) = 1;
14814
14815       /* For these types, all that is required is that we output a DIE (or a
14816          set of DIEs) to represent the "basis" type.  */
14817       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14818                                 DINFO_USAGE_IND_USE);
14819       break;
14820
14821     case OFFSET_TYPE:
14822       /* This code is used for C++ pointer-to-data-member types.
14823          Output a description of the relevant class type.  */
14824       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14825                                         DINFO_USAGE_IND_USE);
14826
14827       /* Output a description of the type of the object pointed to.  */
14828       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14829                                         DINFO_USAGE_IND_USE);
14830
14831       /* Now output a DIE to represent this pointer-to-data-member type
14832          itself.  */
14833       gen_ptr_to_mbr_type_die (type, context_die);
14834       break;
14835
14836     case FUNCTION_TYPE:
14837       /* Force out return type (in case it wasn't forced out already).  */
14838       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14839                                         DINFO_USAGE_DIR_USE);
14840       gen_subroutine_type_die (type, context_die);
14841       break;
14842
14843     case METHOD_TYPE:
14844       /* Force out return type (in case it wasn't forced out already).  */
14845       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14846                                         DINFO_USAGE_DIR_USE);
14847       gen_subroutine_type_die (type, context_die);
14848       break;
14849
14850     case ARRAY_TYPE:
14851       gen_array_type_die (type, context_die);
14852       break;
14853
14854     case VECTOR_TYPE:
14855       gen_array_type_die (type, context_die);
14856       break;
14857
14858     case ENUMERAL_TYPE:
14859     case RECORD_TYPE:
14860     case UNION_TYPE:
14861     case QUAL_UNION_TYPE:
14862       /* If this is a nested type whose containing class hasn't been written
14863          out yet, writing it out will cover this one, too.  This does not apply
14864          to instantiations of member class templates; they need to be added to
14865          the containing class as they are generated.  FIXME: This hurts the
14866          idea of combining type decls from multiple TUs, since we can't predict
14867          what set of template instantiations we'll get.  */
14868       if (TYPE_CONTEXT (type)
14869           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14870           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14871         {
14872           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14873
14874           if (TREE_ASM_WRITTEN (type))
14875             return;
14876
14877           /* If that failed, attach ourselves to the stub.  */
14878           push_decl_scope (TYPE_CONTEXT (type));
14879           context_die = lookup_type_die (TYPE_CONTEXT (type));
14880           need_pop = 1;
14881         }
14882       else
14883         {
14884           context_die = declare_in_namespace (type, context_die);
14885           need_pop = 0;
14886         }
14887
14888       if (TREE_CODE (type) == ENUMERAL_TYPE)
14889         {
14890           /* This might have been written out by the call to
14891              declare_in_namespace.  */
14892           if (!TREE_ASM_WRITTEN (type))
14893             gen_enumeration_type_die (type, context_die);
14894         }
14895       else
14896         gen_struct_or_union_type_die (type, context_die, usage);
14897
14898       if (need_pop)
14899         pop_decl_scope ();
14900
14901       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14902          it up if it is ever completed.  gen_*_type_die will set it for us
14903          when appropriate.  */
14904       return;
14905
14906     case VOID_TYPE:
14907     case INTEGER_TYPE:
14908     case REAL_TYPE:
14909     case FIXED_POINT_TYPE:
14910     case COMPLEX_TYPE:
14911     case BOOLEAN_TYPE:
14912       /* No DIEs needed for fundamental types.  */
14913       break;
14914
14915     case LANG_TYPE:
14916       /* No Dwarf representation currently defined.  */
14917       break;
14918
14919     default:
14920       gcc_unreachable ();
14921     }
14922
14923   TREE_ASM_WRITTEN (type) = 1;
14924 }
14925
14926 static void
14927 gen_type_die (tree type, dw_die_ref context_die)
14928 {
14929   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14930 }
14931
14932 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14933    things which are local to the given block.  */
14934
14935 static void
14936 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14937 {
14938   int must_output_die = 0;
14939   bool inlined_func;
14940
14941   /* Ignore blocks that are NULL.  */
14942   if (stmt == NULL_TREE)
14943     return;
14944
14945   inlined_func = inlined_function_outer_scope_p (stmt);
14946
14947   /* If the block is one fragment of a non-contiguous block, do not
14948      process the variables, since they will have been done by the
14949      origin block.  Do process subblocks.  */
14950   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14951     {
14952       tree sub;
14953
14954       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14955         gen_block_die (sub, context_die, depth + 1);
14956
14957       return;
14958     }
14959
14960   /* Determine if we need to output any Dwarf DIEs at all to represent this
14961      block.  */
14962   if (inlined_func)
14963     /* The outer scopes for inlinings *must* always be represented.  We
14964        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14965     must_output_die = 1;
14966   else
14967     {
14968       /* Determine if this block directly contains any "significant"
14969          local declarations which we will need to output DIEs for.  */
14970       if (debug_info_level > DINFO_LEVEL_TERSE)
14971         /* We are not in terse mode so *any* local declaration counts
14972            as being a "significant" one.  */
14973         must_output_die = ((BLOCK_VARS (stmt) != NULL
14974                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14975                            && (TREE_USED (stmt)
14976                                || TREE_ASM_WRITTEN (stmt)
14977                                || BLOCK_ABSTRACT (stmt)));
14978       else if ((TREE_USED (stmt)
14979                 || TREE_ASM_WRITTEN (stmt)
14980                 || BLOCK_ABSTRACT (stmt))
14981                && !dwarf2out_ignore_block (stmt))
14982         must_output_die = 1;
14983     }
14984
14985   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14986      DIE for any block which contains no significant local declarations at
14987      all.  Rather, in such cases we just call `decls_for_scope' so that any
14988      needed Dwarf info for any sub-blocks will get properly generated. Note
14989      that in terse mode, our definition of what constitutes a "significant"
14990      local declaration gets restricted to include only inlined function
14991      instances and local (nested) function definitions.  */
14992   if (must_output_die)
14993     {
14994       if (inlined_func)
14995         {
14996           /* If STMT block is abstract, that means we have been called
14997              indirectly from dwarf2out_abstract_function.
14998              That function rightfully marks the descendent blocks (of
14999              the abstract function it is dealing with) as being abstract,
15000              precisely to prevent us from emitting any
15001              DW_TAG_inlined_subroutine DIE as a descendent
15002              of an abstract function instance. So in that case, we should
15003              not call gen_inlined_subroutine_die.
15004
15005              Later though, when cgraph asks dwarf2out to emit info
15006              for the concrete instance of the function decl into which
15007              the concrete instance of STMT got inlined, the later will lead
15008              to the generation of a DW_TAG_inlined_subroutine DIE.  */
15009           if (! BLOCK_ABSTRACT (stmt))
15010             gen_inlined_subroutine_die (stmt, context_die, depth);
15011         }
15012       else
15013         gen_lexical_block_die (stmt, context_die, depth);
15014     }
15015   else
15016     decls_for_scope (stmt, context_die, depth);
15017 }
15018
15019 /* Process variable DECL (or variable with origin ORIGIN) within
15020    block STMT and add it to CONTEXT_DIE.  */
15021 static void
15022 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15023 {
15024   dw_die_ref die;
15025   tree decl_or_origin = decl ? decl : origin;
15026   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15027
15028   if (ultimate_origin)
15029     origin = ultimate_origin;
15030
15031   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15032     die = lookup_decl_die (decl_or_origin);
15033   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15034            && TYPE_DECL_IS_STUB (decl_or_origin))
15035     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15036   else
15037     die = NULL;
15038
15039   if (die != NULL && die->die_parent == NULL)
15040     add_child_die (context_die, die);
15041   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15042     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15043                                          stmt, context_die);
15044   else
15045     gen_decl_die (decl, origin, context_die);
15046 }
15047
15048 /* Generate all of the decls declared within a given scope and (recursively)
15049    all of its sub-blocks.  */
15050
15051 static void
15052 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15053 {
15054   tree decl;
15055   unsigned int i;
15056   tree subblocks;
15057
15058   /* Ignore NULL blocks.  */
15059   if (stmt == NULL_TREE)
15060     return;
15061
15062   /* Output the DIEs to represent all of the data objects and typedefs
15063      declared directly within this block but not within any nested
15064      sub-blocks.  Also, nested function and tag DIEs have been
15065      generated with a parent of NULL; fix that up now.  */
15066   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15067     process_scope_var (stmt, decl, NULL_TREE, context_die);
15068   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15069     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15070                        context_die);
15071
15072   /* If we're at -g1, we're not interested in subblocks.  */
15073   if (debug_info_level <= DINFO_LEVEL_TERSE)
15074     return;
15075
15076   /* Output the DIEs to represent all sub-blocks (and the items declared
15077      therein) of this block.  */
15078   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15079        subblocks != NULL;
15080        subblocks = BLOCK_CHAIN (subblocks))
15081     gen_block_die (subblocks, context_die, depth + 1);
15082 }
15083
15084 /* Is this a typedef we can avoid emitting?  */
15085
15086 static inline int
15087 is_redundant_typedef (const_tree decl)
15088 {
15089   if (TYPE_DECL_IS_STUB (decl))
15090     return 1;
15091
15092   if (DECL_ARTIFICIAL (decl)
15093       && DECL_CONTEXT (decl)
15094       && is_tagged_type (DECL_CONTEXT (decl))
15095       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15096       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15097     /* Also ignore the artificial member typedef for the class name.  */
15098     return 1;
15099
15100   return 0;
15101 }
15102
15103 /* Returns the DIE for a context.  */
15104
15105 static inline dw_die_ref
15106 get_context_die (tree context)
15107 {
15108   if (context)
15109     {
15110       /* Find die that represents this context.  */
15111       if (TYPE_P (context))
15112         return force_type_die (context);
15113       else
15114         return force_decl_die (context);
15115     }
15116   return comp_unit_die;
15117 }
15118
15119 /* Returns the DIE for decl.  A DIE will always be returned.  */
15120
15121 static dw_die_ref
15122 force_decl_die (tree decl)
15123 {
15124   dw_die_ref decl_die;
15125   unsigned saved_external_flag;
15126   tree save_fn = NULL_TREE;
15127   decl_die = lookup_decl_die (decl);
15128   if (!decl_die)
15129     {
15130       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15131
15132       decl_die = lookup_decl_die (decl);
15133       if (decl_die)
15134         return decl_die;
15135
15136       switch (TREE_CODE (decl))
15137         {
15138         case FUNCTION_DECL:
15139           /* Clear current_function_decl, so that gen_subprogram_die thinks
15140              that this is a declaration. At this point, we just want to force
15141              declaration die.  */
15142           save_fn = current_function_decl;
15143           current_function_decl = NULL_TREE;
15144           gen_subprogram_die (decl, context_die);
15145           current_function_decl = save_fn;
15146           break;
15147
15148         case VAR_DECL:
15149           /* Set external flag to force declaration die. Restore it after
15150            gen_decl_die() call.  */
15151           saved_external_flag = DECL_EXTERNAL (decl);
15152           DECL_EXTERNAL (decl) = 1;
15153           gen_decl_die (decl, NULL, context_die);
15154           DECL_EXTERNAL (decl) = saved_external_flag;
15155           break;
15156
15157         case NAMESPACE_DECL:
15158           dwarf2out_decl (decl);
15159           break;
15160
15161         default:
15162           gcc_unreachable ();
15163         }
15164
15165       /* We should be able to find the DIE now.  */
15166       if (!decl_die)
15167         decl_die = lookup_decl_die (decl);
15168       gcc_assert (decl_die);
15169     }
15170
15171   return decl_die;
15172 }
15173
15174 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15175    always returned.  */
15176
15177 static dw_die_ref
15178 force_type_die (tree type)
15179 {
15180   dw_die_ref type_die;
15181
15182   type_die = lookup_type_die (type);
15183   if (!type_die)
15184     {
15185       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15186
15187       type_die = modified_type_die (type, TYPE_READONLY (type),
15188                                     TYPE_VOLATILE (type), context_die);
15189       gcc_assert (type_die);
15190     }
15191   return type_die;
15192 }
15193
15194 /* Force out any required namespaces to be able to output DECL,
15195    and return the new context_die for it, if it's changed.  */
15196
15197 static dw_die_ref
15198 setup_namespace_context (tree thing, dw_die_ref context_die)
15199 {
15200   tree context = (DECL_P (thing)
15201                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15202   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15203     /* Force out the namespace.  */
15204     context_die = force_decl_die (context);
15205
15206   return context_die;
15207 }
15208
15209 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15210    type) within its namespace, if appropriate.
15211
15212    For compatibility with older debuggers, namespace DIEs only contain
15213    declarations; all definitions are emitted at CU scope.  */
15214
15215 static dw_die_ref
15216 declare_in_namespace (tree thing, dw_die_ref context_die)
15217 {
15218   dw_die_ref ns_context;
15219
15220   if (debug_info_level <= DINFO_LEVEL_TERSE)
15221     return context_die;
15222
15223   /* If this decl is from an inlined function, then don't try to emit it in its
15224      namespace, as we will get confused.  It would have already been emitted
15225      when the abstract instance of the inline function was emitted anyways.  */
15226   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15227     return context_die;
15228
15229   ns_context = setup_namespace_context (thing, context_die);
15230
15231   if (ns_context != context_die)
15232     {
15233       if (is_fortran ())
15234         return ns_context;
15235       if (DECL_P (thing))
15236         gen_decl_die (thing, NULL, ns_context);
15237       else
15238         gen_type_die (thing, ns_context);
15239     }
15240   return context_die;
15241 }
15242
15243 /* Generate a DIE for a namespace or namespace alias.  */
15244
15245 static void
15246 gen_namespace_die (tree decl, dw_die_ref context_die)
15247 {
15248   dw_die_ref namespace_die;
15249
15250   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15251      they are an alias of.  */
15252   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15253     {
15254       /* Output a real namespace or module.  */
15255       context_die = setup_namespace_context (decl, comp_unit_die);
15256       namespace_die = new_die (is_fortran ()
15257                                ? DW_TAG_module : DW_TAG_namespace,
15258                                context_die, decl);
15259       /* For Fortran modules defined in different CU don't add src coords.  */
15260       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15261         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15262       else
15263         add_name_and_src_coords_attributes (namespace_die, decl);
15264       if (DECL_EXTERNAL (decl))
15265         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15266       equate_decl_number_to_die (decl, namespace_die);
15267     }
15268   else
15269     {
15270       /* Output a namespace alias.  */
15271
15272       /* Force out the namespace we are an alias of, if necessary.  */
15273       dw_die_ref origin_die
15274         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15275
15276       if (DECL_CONTEXT (decl) == NULL_TREE
15277           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15278         context_die = setup_namespace_context (decl, comp_unit_die);
15279       /* Now create the namespace alias DIE.  */
15280       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15281       add_name_and_src_coords_attributes (namespace_die, decl);
15282       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15283       equate_decl_number_to_die (decl, namespace_die);
15284     }
15285 }
15286
15287 /* Generate Dwarf debug information for a decl described by DECL.  */
15288
15289 static void
15290 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15291 {
15292   tree decl_or_origin = decl ? decl : origin;
15293   tree class_origin = NULL;
15294
15295   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15296     return;
15297
15298   switch (TREE_CODE (decl_or_origin))
15299     {
15300     case ERROR_MARK:
15301       break;
15302
15303     case CONST_DECL:
15304       if (!is_fortran ())
15305         {
15306           /* The individual enumerators of an enum type get output when we output
15307              the Dwarf representation of the relevant enum type itself.  */
15308           break;
15309         }
15310
15311       /* Emit its type.  */
15312       gen_type_die (TREE_TYPE (decl), context_die);
15313
15314       /* And its containing namespace.  */
15315       context_die = declare_in_namespace (decl, context_die);
15316
15317       gen_const_die (decl, context_die);
15318       break;
15319
15320     case FUNCTION_DECL:
15321       /* Don't output any DIEs to represent mere function declarations,
15322          unless they are class members or explicit block externs.  */
15323       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15324           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15325           && (current_function_decl == NULL_TREE
15326               || DECL_ARTIFICIAL (decl_or_origin)))
15327         break;
15328
15329 #if 0
15330       /* FIXME */
15331       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15332          on local redeclarations of global functions.  That seems broken.  */
15333       if (current_function_decl != decl)
15334         /* This is only a declaration.  */;
15335 #endif
15336
15337       /* If we're emitting a clone, emit info for the abstract instance.  */
15338       if (origin || DECL_ORIGIN (decl) != decl)
15339         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15340
15341       /* If we're emitting an out-of-line copy of an inline function,
15342          emit info for the abstract instance and set up to refer to it.  */
15343       else if (cgraph_function_possibly_inlined_p (decl)
15344                && ! DECL_ABSTRACT (decl)
15345                && ! class_or_namespace_scope_p (context_die)
15346                /* dwarf2out_abstract_function won't emit a die if this is just
15347                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15348                   that case, because that works only if we have a die.  */
15349                && DECL_INITIAL (decl) != NULL_TREE)
15350         {
15351           dwarf2out_abstract_function (decl);
15352           set_decl_origin_self (decl);
15353         }
15354
15355       /* Otherwise we're emitting the primary DIE for this decl.  */
15356       else if (debug_info_level > DINFO_LEVEL_TERSE)
15357         {
15358           /* Before we describe the FUNCTION_DECL itself, make sure that we
15359              have described its return type.  */
15360           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15361
15362           /* And its virtual context.  */
15363           if (DECL_VINDEX (decl) != NULL_TREE)
15364             gen_type_die (DECL_CONTEXT (decl), context_die);
15365
15366           /* And its containing type.  */
15367           if (!origin)
15368             origin = decl_class_context (decl);
15369           if (origin != NULL_TREE)
15370             gen_type_die_for_member (origin, decl, context_die);
15371
15372           /* And its containing namespace.  */
15373           context_die = declare_in_namespace (decl, context_die);
15374         }
15375
15376       /* Now output a DIE to represent the function itself.  */
15377       if (decl)
15378         gen_subprogram_die (decl, context_die);
15379       break;
15380
15381     case TYPE_DECL:
15382       /* If we are in terse mode, don't generate any DIEs to represent any
15383          actual typedefs.  */
15384       if (debug_info_level <= DINFO_LEVEL_TERSE)
15385         break;
15386
15387       /* In the special case of a TYPE_DECL node representing the declaration
15388          of some type tag, if the given TYPE_DECL is marked as having been
15389          instantiated from some other (original) TYPE_DECL node (e.g. one which
15390          was generated within the original definition of an inline function) we
15391          used to generate a special (abbreviated) DW_TAG_structure_type,
15392          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15393          should be actually referencing those DIEs, as variable DIEs with that
15394          type would be emitted already in the abstract origin, so it was always
15395          removed during unused type prunning.  Don't add anything in this
15396          case.  */
15397       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15398         break;
15399
15400       if (is_redundant_typedef (decl))
15401         gen_type_die (TREE_TYPE (decl), context_die);
15402       else
15403         /* Output a DIE to represent the typedef itself.  */
15404         gen_typedef_die (decl, context_die);
15405       break;
15406
15407     case LABEL_DECL:
15408       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15409         gen_label_die (decl, context_die);
15410       break;
15411
15412     case VAR_DECL:
15413     case RESULT_DECL:
15414       /* If we are in terse mode, don't generate any DIEs to represent any
15415          variable declarations or definitions.  */
15416       if (debug_info_level <= DINFO_LEVEL_TERSE)
15417         break;
15418
15419       /* Output any DIEs that are needed to specify the type of this data
15420          object.  */
15421       if (TREE_CODE (decl_or_origin) == RESULT_DECL
15422           && DECL_BY_REFERENCE (decl_or_origin))
15423         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15424       else
15425         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15426
15427       /* And its containing type.  */
15428       class_origin = decl_class_context (decl_or_origin);
15429       if (class_origin != NULL_TREE)
15430         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15431
15432       /* And its containing namespace.  */
15433       context_die = declare_in_namespace (decl_or_origin, context_die);
15434
15435       /* Now output the DIE to represent the data object itself.  This gets
15436          complicated because of the possibility that the VAR_DECL really
15437          represents an inlined instance of a formal parameter for an inline
15438          function.  */
15439       if (!origin)
15440         origin = decl_ultimate_origin (decl);
15441       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15442         gen_formal_parameter_die (decl, origin, context_die);
15443       else
15444         gen_variable_die (decl, origin, context_die);
15445       break;
15446
15447     case FIELD_DECL:
15448       /* Ignore the nameless fields that are used to skip bits but handle C++
15449          anonymous unions and structs.  */
15450       if (DECL_NAME (decl) != NULL_TREE
15451           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15452           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15453         {
15454           gen_type_die (member_declared_type (decl), context_die);
15455           gen_field_die (decl, context_die);
15456         }
15457       break;
15458
15459     case PARM_DECL:
15460       if (DECL_BY_REFERENCE (decl_or_origin))
15461         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15462       else
15463         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15464       gen_formal_parameter_die (decl, origin, context_die);
15465       break;
15466
15467     case NAMESPACE_DECL:
15468     case IMPORTED_DECL:
15469       gen_namespace_die (decl, context_die);
15470       break;
15471
15472     default:
15473       /* Probably some frontend-internal decl.  Assume we don't care.  */
15474       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15475       break;
15476     }
15477 }
15478 \f
15479 /* Output debug information for global decl DECL.  Called from toplev.c after
15480    compilation proper has finished.  */
15481
15482 static void
15483 dwarf2out_global_decl (tree decl)
15484 {
15485   /* Output DWARF2 information for file-scope tentative data object
15486      declarations, file-scope (extern) function declarations (which
15487      had no corresponding body) and file-scope tagged type declarations
15488      and definitions which have not yet been forced out.  */
15489   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15490     dwarf2out_decl (decl);
15491 }
15492
15493 /* Output debug information for type decl DECL.  Called from toplev.c
15494    and from language front ends (to record built-in types).  */
15495 static void
15496 dwarf2out_type_decl (tree decl, int local)
15497 {
15498   if (!local)
15499     dwarf2out_decl (decl);
15500 }
15501
15502 /* Output debug information for imported module or decl DECL.
15503    NAME is non-NULL name in the lexical block if the decl has been renamed.
15504    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15505    that DECL belongs to.
15506    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15507 static void
15508 dwarf2out_imported_module_or_decl_1 (tree decl,
15509                                      tree name,
15510                                      tree lexical_block,
15511                                      dw_die_ref lexical_block_die)
15512 {
15513   expanded_location xloc;
15514   dw_die_ref imported_die = NULL;
15515   dw_die_ref at_import_die;
15516
15517   if (TREE_CODE (decl) == IMPORTED_DECL)
15518     {
15519       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15520       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15521       gcc_assert (decl);
15522     }
15523   else
15524     xloc = expand_location (input_location);
15525
15526   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15527     {
15528       if (is_base_type (TREE_TYPE (decl)))
15529         at_import_die = base_type_die (TREE_TYPE (decl));
15530       else
15531         at_import_die = force_type_die (TREE_TYPE (decl));
15532       /* For namespace N { typedef void T; } using N::T; base_type_die
15533          returns NULL, but DW_TAG_imported_declaration requires
15534          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15535       if (!at_import_die)
15536         {
15537           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15538           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15539           at_import_die = lookup_type_die (TREE_TYPE (decl));
15540           gcc_assert (at_import_die);
15541         }
15542     }
15543   else
15544     {
15545       at_import_die = lookup_decl_die (decl);
15546       if (!at_import_die)
15547         {
15548           /* If we're trying to avoid duplicate debug info, we may not have
15549              emitted the member decl for this field.  Emit it now.  */
15550           if (TREE_CODE (decl) == FIELD_DECL)
15551             {
15552               tree type = DECL_CONTEXT (decl);
15553
15554               if (TYPE_CONTEXT (type)
15555                   && TYPE_P (TYPE_CONTEXT (type))
15556                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15557                                                 DINFO_USAGE_DIR_USE))
15558                 return;
15559               gen_type_die_for_member (type, decl,
15560                                        get_context_die (TYPE_CONTEXT (type)));
15561             }
15562           at_import_die = force_decl_die (decl);
15563         }
15564     }
15565
15566   if (TREE_CODE (decl) == NAMESPACE_DECL)
15567     imported_die = new_die (DW_TAG_imported_module,
15568                             lexical_block_die,
15569                             lexical_block);
15570   else
15571     imported_die = new_die (DW_TAG_imported_declaration,
15572                             lexical_block_die,
15573                             lexical_block);
15574
15575   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15576   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15577   if (name)
15578     add_AT_string (imported_die, DW_AT_name,
15579                    IDENTIFIER_POINTER (name));
15580   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15581 }
15582
15583 /* Output debug information for imported module or decl DECL.
15584    NAME is non-NULL name in context if the decl has been renamed.
15585    CHILD is true if decl is one of the renamed decls as part of
15586    importing whole module.  */
15587
15588 static void
15589 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15590                                    bool child)
15591 {
15592   /* dw_die_ref at_import_die;  */
15593   dw_die_ref scope_die;
15594
15595   if (debug_info_level <= DINFO_LEVEL_TERSE)
15596     return;
15597
15598   gcc_assert (decl);
15599
15600   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15601      We need decl DIE for reference and scope die. First, get DIE for the decl
15602      itself.  */
15603
15604   /* Get the scope die for decl context. Use comp_unit_die for global module
15605      or decl. If die is not found for non globals, force new die.  */
15606   if (context
15607       && TYPE_P (context)
15608       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15609     return;
15610   scope_die = get_context_die (context);
15611
15612   if (child)
15613     {
15614       gcc_assert (scope_die->die_child);
15615       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15616       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15617       scope_die = scope_die->die_child;
15618     }
15619
15620   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15621   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15622
15623 }
15624
15625 /* Write the debugging output for DECL.  */
15626
15627 void
15628 dwarf2out_decl (tree decl)
15629 {
15630   dw_die_ref context_die = comp_unit_die;
15631
15632   switch (TREE_CODE (decl))
15633     {
15634     case ERROR_MARK:
15635       return;
15636
15637     case FUNCTION_DECL:
15638       /* What we would really like to do here is to filter out all mere
15639          file-scope declarations of file-scope functions which are never
15640          referenced later within this translation unit (and keep all of ones
15641          that *are* referenced later on) but we aren't clairvoyant, so we have
15642          no idea which functions will be referenced in the future (i.e. later
15643          on within the current translation unit). So here we just ignore all
15644          file-scope function declarations which are not also definitions.  If
15645          and when the debugger needs to know something about these functions,
15646          it will have to hunt around and find the DWARF information associated
15647          with the definition of the function.
15648
15649          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15650          nodes represent definitions and which ones represent mere
15651          declarations.  We have to check DECL_INITIAL instead. That's because
15652          the C front-end supports some weird semantics for "extern inline"
15653          function definitions.  These can get inlined within the current
15654          translation unit (and thus, we need to generate Dwarf info for their
15655          abstract instances so that the Dwarf info for the concrete inlined
15656          instances can have something to refer to) but the compiler never
15657          generates any out-of-lines instances of such things (despite the fact
15658          that they *are* definitions).
15659
15660          The important point is that the C front-end marks these "extern
15661          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15662          them anyway. Note that the C++ front-end also plays some similar games
15663          for inline function definitions appearing within include files which
15664          also contain `#pragma interface' pragmas.  */
15665       if (DECL_INITIAL (decl) == NULL_TREE)
15666         return;
15667
15668       /* If we're a nested function, initially use a parent of NULL; if we're
15669          a plain function, this will be fixed up in decls_for_scope.  If
15670          we're a method, it will be ignored, since we already have a DIE.  */
15671       if (decl_function_context (decl)
15672           /* But if we're in terse mode, we don't care about scope.  */
15673           && debug_info_level > DINFO_LEVEL_TERSE)
15674         context_die = NULL;
15675       break;
15676
15677     case VAR_DECL:
15678       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15679          declaration and if the declaration was never even referenced from
15680          within this entire compilation unit.  We suppress these DIEs in
15681          order to save space in the .debug section (by eliminating entries
15682          which are probably useless).  Note that we must not suppress
15683          block-local extern declarations (whether used or not) because that
15684          would screw-up the debugger's name lookup mechanism and cause it to
15685          miss things which really ought to be in scope at a given point.  */
15686       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15687         return;
15688
15689       /* For local statics lookup proper context die.  */
15690       if (TREE_STATIC (decl) && decl_function_context (decl))
15691         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15692
15693       /* If we are in terse mode, don't generate any DIEs to represent any
15694          variable declarations or definitions.  */
15695       if (debug_info_level <= DINFO_LEVEL_TERSE)
15696         return;
15697       break;
15698
15699     case CONST_DECL:
15700       if (debug_info_level <= DINFO_LEVEL_TERSE)
15701         return;
15702       if (!is_fortran ())
15703         return;
15704       if (TREE_STATIC (decl) && decl_function_context (decl))
15705         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15706       break;
15707
15708     case NAMESPACE_DECL:
15709     case IMPORTED_DECL:
15710       if (debug_info_level <= DINFO_LEVEL_TERSE)
15711         return;
15712       if (lookup_decl_die (decl) != NULL)
15713         return;
15714       break;
15715
15716     case TYPE_DECL:
15717       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15718       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15719         return;
15720
15721       /* Don't bother trying to generate any DIEs to represent any of the
15722          normal built-in types for the language we are compiling.  */
15723       if (DECL_IS_BUILTIN (decl))
15724         {
15725           /* OK, we need to generate one for `bool' so GDB knows what type
15726              comparisons have.  */
15727           if (is_cxx ()
15728               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15729               && ! DECL_IGNORED_P (decl))
15730             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15731
15732           return;
15733         }
15734
15735       /* If we are in terse mode, don't generate any DIEs for types.  */
15736       if (debug_info_level <= DINFO_LEVEL_TERSE)
15737         return;
15738
15739       /* If we're a function-scope tag, initially use a parent of NULL;
15740          this will be fixed up in decls_for_scope.  */
15741       if (decl_function_context (decl))
15742         context_die = NULL;
15743
15744       break;
15745
15746     default:
15747       return;
15748     }
15749
15750   gen_decl_die (decl, NULL, context_die);
15751 }
15752
15753 /* Output a marker (i.e. a label) for the beginning of the generated code for
15754    a lexical block.  */
15755
15756 static void
15757 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15758                        unsigned int blocknum)
15759 {
15760   switch_to_section (current_function_section ());
15761   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15762 }
15763
15764 /* Output a marker (i.e. a label) for the end of the generated code for a
15765    lexical block.  */
15766
15767 static void
15768 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15769 {
15770   switch_to_section (current_function_section ());
15771   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15772 }
15773
15774 /* Returns nonzero if it is appropriate not to emit any debugging
15775    information for BLOCK, because it doesn't contain any instructions.
15776
15777    Don't allow this for blocks with nested functions or local classes
15778    as we would end up with orphans, and in the presence of scheduling
15779    we may end up calling them anyway.  */
15780
15781 static bool
15782 dwarf2out_ignore_block (const_tree block)
15783 {
15784   tree decl;
15785   unsigned int i;
15786
15787   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15788     if (TREE_CODE (decl) == FUNCTION_DECL
15789         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15790       return 0;
15791   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15792     {
15793       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15794       if (TREE_CODE (decl) == FUNCTION_DECL
15795           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15796       return 0;
15797     }
15798
15799   return 1;
15800 }
15801
15802 /* Hash table routines for file_hash.  */
15803
15804 static int
15805 file_table_eq (const void *p1_p, const void *p2_p)
15806 {
15807   const struct dwarf_file_data *const p1 =
15808     (const struct dwarf_file_data *) p1_p;
15809   const char *const p2 = (const char *) p2_p;
15810   return strcmp (p1->filename, p2) == 0;
15811 }
15812
15813 static hashval_t
15814 file_table_hash (const void *p_p)
15815 {
15816   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15817   return htab_hash_string (p->filename);
15818 }
15819
15820 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15821    dwarf2out.c) and return its "index".  The index of each (known) filename is
15822    just a unique number which is associated with only that one filename.  We
15823    need such numbers for the sake of generating labels (in the .debug_sfnames
15824    section) and references to those files numbers (in the .debug_srcinfo
15825    and.debug_macinfo sections).  If the filename given as an argument is not
15826    found in our current list, add it to the list and assign it the next
15827    available unique index number.  In order to speed up searches, we remember
15828    the index of the filename was looked up last.  This handles the majority of
15829    all searches.  */
15830
15831 static struct dwarf_file_data *
15832 lookup_filename (const char *file_name)
15833 {
15834   void ** slot;
15835   struct dwarf_file_data * created;
15836
15837   /* Check to see if the file name that was searched on the previous
15838      call matches this file name.  If so, return the index.  */
15839   if (file_table_last_lookup
15840       && (file_name == file_table_last_lookup->filename
15841           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15842     return file_table_last_lookup;
15843
15844   /* Didn't match the previous lookup, search the table.  */
15845   slot = htab_find_slot_with_hash (file_table, file_name,
15846                                    htab_hash_string (file_name), INSERT);
15847   if (*slot)
15848     return (struct dwarf_file_data *) *slot;
15849
15850   created = GGC_NEW (struct dwarf_file_data);
15851   created->filename = file_name;
15852   created->emitted_number = 0;
15853   *slot = created;
15854   return created;
15855 }
15856
15857 /* If the assembler will construct the file table, then translate the compiler
15858    internal file table number into the assembler file table number, and emit
15859    a .file directive if we haven't already emitted one yet.  The file table
15860    numbers are different because we prune debug info for unused variables and
15861    types, which may include filenames.  */
15862
15863 static int
15864 maybe_emit_file (struct dwarf_file_data * fd)
15865 {
15866   if (! fd->emitted_number)
15867     {
15868       if (last_emitted_file)
15869         fd->emitted_number = last_emitted_file->emitted_number + 1;
15870       else
15871         fd->emitted_number = 1;
15872       last_emitted_file = fd;
15873
15874       if (DWARF2_ASM_LINE_DEBUG_INFO)
15875         {
15876           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15877           output_quoted_string (asm_out_file,
15878                                 remap_debug_filename (fd->filename));
15879           fputc ('\n', asm_out_file);
15880         }
15881     }
15882
15883   return fd->emitted_number;
15884 }
15885
15886 /* Called by the final INSN scan whenever we see a var location.  We
15887    use it to drop labels in the right places, and throw the location in
15888    our lookup table.  */
15889
15890 static void
15891 dwarf2out_var_location (rtx loc_note)
15892 {
15893   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15894   struct var_loc_node *newloc;
15895   rtx prev_insn;
15896   static rtx last_insn;
15897   static const char *last_label;
15898   tree decl;
15899
15900   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15901     return;
15902   prev_insn = PREV_INSN (loc_note);
15903
15904   newloc = GGC_CNEW (struct var_loc_node);
15905   /* If the insn we processed last time is the previous insn
15906      and it is also a var location note, use the label we emitted
15907      last time.  */
15908   if (last_insn != NULL_RTX
15909       && last_insn == prev_insn
15910       && NOTE_P (prev_insn)
15911       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15912     {
15913       newloc->label = last_label;
15914     }
15915   else
15916     {
15917       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15918       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15919       loclabel_num++;
15920       newloc->label = ggc_strdup (loclabel);
15921     }
15922   newloc->var_loc_note = loc_note;
15923   newloc->next = NULL;
15924
15925   if (cfun && in_cold_section_p)
15926     newloc->section_label = crtl->subsections.cold_section_label;
15927   else
15928     newloc->section_label = text_section_label;
15929
15930   last_insn = loc_note;
15931   last_label = newloc->label;
15932   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15933   add_var_loc_to_decl (decl, newloc);
15934 }
15935
15936 /* We need to reset the locations at the beginning of each
15937    function. We can't do this in the end_function hook, because the
15938    declarations that use the locations won't have been output when
15939    that hook is called.  Also compute have_multiple_function_sections here.  */
15940
15941 static void
15942 dwarf2out_begin_function (tree fun)
15943 {
15944   htab_empty (decl_loc_table);
15945
15946   if (function_section (fun) != text_section)
15947     have_multiple_function_sections = true;
15948
15949   dwarf2out_note_section_used ();
15950 }
15951
15952 /* Output a label to mark the beginning of a source code line entry
15953    and record information relating to this source line, in
15954    'line_info_table' for later output of the .debug_line section.  */
15955
15956 static void
15957 dwarf2out_source_line (unsigned int line, const char *filename)
15958 {
15959   if (debug_info_level >= DINFO_LEVEL_NORMAL
15960       && line != 0)
15961     {
15962       int file_num = maybe_emit_file (lookup_filename (filename));
15963
15964       switch_to_section (current_function_section ());
15965
15966       /* If requested, emit something human-readable.  */
15967       if (flag_debug_asm)
15968         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15969                  filename, line);
15970
15971       if (DWARF2_ASM_LINE_DEBUG_INFO)
15972         {
15973           /* Emit the .loc directive understood by GNU as.  */
15974           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15975
15976           /* Indicate that line number info exists.  */
15977           line_info_table_in_use++;
15978         }
15979       else if (function_section (current_function_decl) != text_section)
15980         {
15981           dw_separate_line_info_ref line_info;
15982           targetm.asm_out.internal_label (asm_out_file,
15983                                           SEPARATE_LINE_CODE_LABEL,
15984                                           separate_line_info_table_in_use);
15985
15986           /* Expand the line info table if necessary.  */
15987           if (separate_line_info_table_in_use
15988               == separate_line_info_table_allocated)
15989             {
15990               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15991               separate_line_info_table
15992                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15993                                  separate_line_info_table,
15994                                  separate_line_info_table_allocated);
15995               memset (separate_line_info_table
15996                        + separate_line_info_table_in_use,
15997                       0,
15998                       (LINE_INFO_TABLE_INCREMENT
15999                        * sizeof (dw_separate_line_info_entry)));
16000             }
16001
16002           /* Add the new entry at the end of the line_info_table.  */
16003           line_info
16004             = &separate_line_info_table[separate_line_info_table_in_use++];
16005           line_info->dw_file_num = file_num;
16006           line_info->dw_line_num = line;
16007           line_info->function = current_function_funcdef_no;
16008         }
16009       else
16010         {
16011           dw_line_info_ref line_info;
16012
16013           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16014                                      line_info_table_in_use);
16015
16016           /* Expand the line info table if necessary.  */
16017           if (line_info_table_in_use == line_info_table_allocated)
16018             {
16019               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16020               line_info_table
16021                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16022                                  line_info_table_allocated);
16023               memset (line_info_table + line_info_table_in_use, 0,
16024                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16025             }
16026
16027           /* Add the new entry at the end of the line_info_table.  */
16028           line_info = &line_info_table[line_info_table_in_use++];
16029           line_info->dw_file_num = file_num;
16030           line_info->dw_line_num = line;
16031         }
16032     }
16033 }
16034
16035 /* Record the beginning of a new source file.  */
16036
16037 static void
16038 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16039 {
16040   if (flag_eliminate_dwarf2_dups)
16041     {
16042       /* Record the beginning of the file for break_out_includes.  */
16043       dw_die_ref bincl_die;
16044
16045       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16046       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16047     }
16048
16049   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16050     {
16051       int file_num = maybe_emit_file (lookup_filename (filename));
16052
16053       switch_to_section (debug_macinfo_section);
16054       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16055       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16056                                    lineno);
16057
16058       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16059     }
16060 }
16061
16062 /* Record the end of a source file.  */
16063
16064 static void
16065 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16066 {
16067   if (flag_eliminate_dwarf2_dups)
16068     /* Record the end of the file for break_out_includes.  */
16069     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16070
16071   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16072     {
16073       switch_to_section (debug_macinfo_section);
16074       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16075     }
16076 }
16077
16078 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16079    the tail part of the directive line, i.e. the part which is past the
16080    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16081
16082 static void
16083 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16084                   const char *buffer ATTRIBUTE_UNUSED)
16085 {
16086   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16087     {
16088       switch_to_section (debug_macinfo_section);
16089       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16090       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16091       dw2_asm_output_nstring (buffer, -1, "The macro");
16092     }
16093 }
16094
16095 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16096    the tail part of the directive line, i.e. the part which is past the
16097    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16098
16099 static void
16100 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16101                  const char *buffer ATTRIBUTE_UNUSED)
16102 {
16103   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16104     {
16105       switch_to_section (debug_macinfo_section);
16106       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16107       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16108       dw2_asm_output_nstring (buffer, -1, "The macro");
16109     }
16110 }
16111
16112 /* Set up for Dwarf output at the start of compilation.  */
16113
16114 static void
16115 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16116 {
16117   /* Allocate the file_table.  */
16118   file_table = htab_create_ggc (50, file_table_hash,
16119                                 file_table_eq, NULL);
16120
16121   /* Allocate the decl_die_table.  */
16122   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16123                                     decl_die_table_eq, NULL);
16124
16125   /* Allocate the decl_loc_table.  */
16126   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16127                                     decl_loc_table_eq, NULL);
16128
16129   /* Allocate the initial hunk of the decl_scope_table.  */
16130   decl_scope_table = VEC_alloc (tree, gc, 256);
16131
16132   /* Allocate the initial hunk of the abbrev_die_table.  */
16133   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16134   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16135   /* Zero-th entry is allocated, but unused.  */
16136   abbrev_die_table_in_use = 1;
16137
16138   /* Allocate the initial hunk of the line_info_table.  */
16139   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16140   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16141
16142   /* Zero-th entry is allocated, but unused.  */
16143   line_info_table_in_use = 1;
16144
16145   /* Allocate the pubtypes and pubnames vectors.  */
16146   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16147   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16148
16149   /* Generate the initial DIE for the .debug section.  Note that the (string)
16150      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16151      will (typically) be a relative pathname and that this pathname should be
16152      taken as being relative to the directory from which the compiler was
16153      invoked when the given (base) source file was compiled.  We will fill
16154      in this value in dwarf2out_finish.  */
16155   comp_unit_die = gen_compile_unit_die (NULL);
16156
16157   incomplete_types = VEC_alloc (tree, gc, 64);
16158
16159   used_rtx_array = VEC_alloc (rtx, gc, 32);
16160
16161   debug_info_section = get_section (DEBUG_INFO_SECTION,
16162                                     SECTION_DEBUG, NULL);
16163   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16164                                       SECTION_DEBUG, NULL);
16165   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16166                                        SECTION_DEBUG, NULL);
16167   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16168                                        SECTION_DEBUG, NULL);
16169   debug_line_section = get_section (DEBUG_LINE_SECTION,
16170                                     SECTION_DEBUG, NULL);
16171   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16172                                    SECTION_DEBUG, NULL);
16173   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16174                                         SECTION_DEBUG, NULL);
16175 #ifdef DEBUG_PUBTYPES_SECTION
16176   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16177                                         SECTION_DEBUG, NULL);
16178 #endif
16179   debug_str_section = get_section (DEBUG_STR_SECTION,
16180                                    DEBUG_STR_SECTION_FLAGS, NULL);
16181   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16182                                       SECTION_DEBUG, NULL);
16183   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16184                                      SECTION_DEBUG, NULL);
16185
16186   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16187   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16188                                DEBUG_ABBREV_SECTION_LABEL, 0);
16189   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16190   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16191                                COLD_TEXT_SECTION_LABEL, 0);
16192   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16193
16194   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16195                                DEBUG_INFO_SECTION_LABEL, 0);
16196   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16197                                DEBUG_LINE_SECTION_LABEL, 0);
16198   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16199                                DEBUG_RANGES_SECTION_LABEL, 0);
16200   switch_to_section (debug_abbrev_section);
16201   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16202   switch_to_section (debug_info_section);
16203   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16204   switch_to_section (debug_line_section);
16205   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16206
16207   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16208     {
16209       switch_to_section (debug_macinfo_section);
16210       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16211                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16212       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16213     }
16214
16215   switch_to_section (text_section);
16216   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16217   if (flag_reorder_blocks_and_partition)
16218     {
16219       cold_text_section = unlikely_text_section ();
16220       switch_to_section (cold_text_section);
16221       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16222     }
16223
16224 }
16225
16226 /* Called before cgraph_optimize starts outputtting functions, variables
16227    and toplevel asms into assembly.  */
16228
16229 static void
16230 dwarf2out_assembly_start (void)
16231 {
16232   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
16233     {
16234 #ifndef TARGET_UNWIND_INFO
16235       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
16236 #endif
16237         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
16238     }
16239 }
16240
16241 /* A helper function for dwarf2out_finish called through
16242    ht_forall.  Emit one queued .debug_str string.  */
16243
16244 static int
16245 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16246 {
16247   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16248
16249   if (node->form == DW_FORM_strp)
16250     {
16251       switch_to_section (debug_str_section);
16252       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16253       assemble_string (node->str, strlen (node->str) + 1);
16254     }
16255
16256   return 1;
16257 }
16258
16259 #if ENABLE_ASSERT_CHECKING
16260 /* Verify that all marks are clear.  */
16261
16262 static void
16263 verify_marks_clear (dw_die_ref die)
16264 {
16265   dw_die_ref c;
16266
16267   gcc_assert (! die->die_mark);
16268   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16269 }
16270 #endif /* ENABLE_ASSERT_CHECKING */
16271
16272 /* Clear the marks for a die and its children.
16273    Be cool if the mark isn't set.  */
16274
16275 static void
16276 prune_unmark_dies (dw_die_ref die)
16277 {
16278   dw_die_ref c;
16279
16280   if (die->die_mark)
16281     die->die_mark = 0;
16282   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16283 }
16284
16285 /* Given DIE that we're marking as used, find any other dies
16286    it references as attributes and mark them as used.  */
16287
16288 static void
16289 prune_unused_types_walk_attribs (dw_die_ref die)
16290 {
16291   dw_attr_ref a;
16292   unsigned ix;
16293
16294   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16295     {
16296       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16297         {
16298           /* A reference to another DIE.
16299              Make sure that it will get emitted.  */
16300           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16301         }
16302       /* Set the string's refcount to 0 so that prune_unused_types_mark
16303          accounts properly for it.  */
16304       if (AT_class (a) == dw_val_class_str)
16305         a->dw_attr_val.v.val_str->refcount = 0;
16306     }
16307 }
16308
16309
16310 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16311    to DIE's children.  */
16312
16313 static void
16314 prune_unused_types_mark (dw_die_ref die, int dokids)
16315 {
16316   dw_die_ref c;
16317
16318   if (die->die_mark == 0)
16319     {
16320       /* We haven't done this node yet.  Mark it as used.  */
16321       die->die_mark = 1;
16322
16323       /* We also have to mark its parents as used.
16324          (But we don't want to mark our parents' kids due to this.)  */
16325       if (die->die_parent)
16326         prune_unused_types_mark (die->die_parent, 0);
16327
16328       /* Mark any referenced nodes.  */
16329       prune_unused_types_walk_attribs (die);
16330
16331       /* If this node is a specification,
16332          also mark the definition, if it exists.  */
16333       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16334         prune_unused_types_mark (die->die_definition, 1);
16335     }
16336
16337   if (dokids && die->die_mark != 2)
16338     {
16339       /* We need to walk the children, but haven't done so yet.
16340          Remember that we've walked the kids.  */
16341       die->die_mark = 2;
16342
16343       /* If this is an array type, we need to make sure our
16344          kids get marked, even if they're types.  */
16345       if (die->die_tag == DW_TAG_array_type)
16346         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16347       else
16348         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16349     }
16350 }
16351
16352 /* For local classes, look if any static member functions were emitted
16353    and if so, mark them.  */
16354
16355 static void
16356 prune_unused_types_walk_local_classes (dw_die_ref die)
16357 {
16358   dw_die_ref c;
16359
16360   if (die->die_mark == 2)
16361     return;
16362
16363   switch (die->die_tag)
16364     {
16365     case DW_TAG_structure_type:
16366     case DW_TAG_union_type:
16367     case DW_TAG_class_type:
16368       break;
16369
16370     case DW_TAG_subprogram:
16371       if (!get_AT_flag (die, DW_AT_declaration)
16372           || die->die_definition != NULL)
16373         prune_unused_types_mark (die, 1);
16374       return;
16375
16376     default:
16377       return;
16378     }
16379
16380   /* Mark children.  */
16381   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16382 }
16383
16384 /* Walk the tree DIE and mark types that we actually use.  */
16385
16386 static void
16387 prune_unused_types_walk (dw_die_ref die)
16388 {
16389   dw_die_ref c;
16390
16391   /* Don't do anything if this node is already marked and
16392      children have been marked as well.  */
16393   if (die->die_mark == 2)
16394     return;
16395
16396   switch (die->die_tag)
16397     {
16398     case DW_TAG_structure_type:
16399     case DW_TAG_union_type:
16400     case DW_TAG_class_type:
16401       if (die->die_perennial_p)
16402         break;
16403
16404       for (c = die->die_parent; c; c = c->die_parent)
16405         if (c->die_tag == DW_TAG_subprogram)
16406           break;
16407
16408       /* Finding used static member functions inside of classes
16409          is needed just for local classes, because for other classes
16410          static member function DIEs with DW_AT_specification
16411          are emitted outside of the DW_TAG_*_type.  If we ever change
16412          it, we'd need to call this even for non-local classes.  */
16413       if (c)
16414         prune_unused_types_walk_local_classes (die);
16415
16416       /* It's a type node --- don't mark it.  */
16417       return;
16418
16419     case DW_TAG_const_type:
16420     case DW_TAG_packed_type:
16421     case DW_TAG_pointer_type:
16422     case DW_TAG_reference_type:
16423     case DW_TAG_volatile_type:
16424     case DW_TAG_typedef:
16425     case DW_TAG_array_type:
16426     case DW_TAG_interface_type:
16427     case DW_TAG_friend:
16428     case DW_TAG_variant_part:
16429     case DW_TAG_enumeration_type:
16430     case DW_TAG_subroutine_type:
16431     case DW_TAG_string_type:
16432     case DW_TAG_set_type:
16433     case DW_TAG_subrange_type:
16434     case DW_TAG_ptr_to_member_type:
16435     case DW_TAG_file_type:
16436       if (die->die_perennial_p)
16437         break;
16438
16439       /* It's a type node --- don't mark it.  */
16440       return;
16441
16442     default:
16443       /* Mark everything else.  */
16444       break;
16445   }
16446
16447   if (die->die_mark == 0)
16448     {
16449       die->die_mark = 1;
16450
16451       /* Now, mark any dies referenced from here.  */
16452       prune_unused_types_walk_attribs (die);
16453     }
16454
16455   die->die_mark = 2;
16456
16457   /* Mark children.  */
16458   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16459 }
16460
16461 /* Increment the string counts on strings referred to from DIE's
16462    attributes.  */
16463
16464 static void
16465 prune_unused_types_update_strings (dw_die_ref die)
16466 {
16467   dw_attr_ref a;
16468   unsigned ix;
16469
16470   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16471     if (AT_class (a) == dw_val_class_str)
16472       {
16473         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16474         s->refcount++;
16475         /* Avoid unnecessarily putting strings that are used less than
16476            twice in the hash table.  */
16477         if (s->refcount
16478             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16479           {
16480             void ** slot;
16481             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16482                                              htab_hash_string (s->str),
16483                                              INSERT);
16484             gcc_assert (*slot == NULL);
16485             *slot = s;
16486           }
16487       }
16488 }
16489
16490 /* Remove from the tree DIE any dies that aren't marked.  */
16491
16492 static void
16493 prune_unused_types_prune (dw_die_ref die)
16494 {
16495   dw_die_ref c;
16496
16497   gcc_assert (die->die_mark);
16498   prune_unused_types_update_strings (die);
16499
16500   if (! die->die_child)
16501     return;
16502
16503   c = die->die_child;
16504   do {
16505     dw_die_ref prev = c;
16506     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16507       if (c == die->die_child)
16508         {
16509           /* No marked children between 'prev' and the end of the list.  */
16510           if (prev == c)
16511             /* No marked children at all.  */
16512             die->die_child = NULL;
16513           else
16514             {
16515               prev->die_sib = c->die_sib;
16516               die->die_child = prev;
16517             }
16518           return;
16519         }
16520
16521     if (c != prev->die_sib)
16522       prev->die_sib = c;
16523     prune_unused_types_prune (c);
16524   } while (c != die->die_child);
16525 }
16526
16527
16528 /* Remove dies representing declarations that we never use.  */
16529
16530 static void
16531 prune_unused_types (void)
16532 {
16533   unsigned int i;
16534   limbo_die_node *node;
16535   pubname_ref pub;
16536
16537 #if ENABLE_ASSERT_CHECKING
16538   /* All the marks should already be clear.  */
16539   verify_marks_clear (comp_unit_die);
16540   for (node = limbo_die_list; node; node = node->next)
16541     verify_marks_clear (node->die);
16542 #endif /* ENABLE_ASSERT_CHECKING */
16543
16544   /* Mark types that are used in global variables.  */
16545   premark_types_used_by_global_vars ();
16546
16547   /* Set the mark on nodes that are actually used.  */
16548   prune_unused_types_walk (comp_unit_die);
16549   for (node = limbo_die_list; node; node = node->next)
16550     prune_unused_types_walk (node->die);
16551
16552   /* Also set the mark on nodes referenced from the
16553      pubname_table or arange_table.  */
16554   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16555     prune_unused_types_mark (pub->die, 1);
16556   for (i = 0; i < arange_table_in_use; i++)
16557     prune_unused_types_mark (arange_table[i], 1);
16558
16559   /* Get rid of nodes that aren't marked; and update the string counts.  */
16560   if (debug_str_hash)
16561     htab_empty (debug_str_hash);
16562   prune_unused_types_prune (comp_unit_die);
16563   for (node = limbo_die_list; node; node = node->next)
16564     prune_unused_types_prune (node->die);
16565
16566   /* Leave the marks clear.  */
16567   prune_unmark_dies (comp_unit_die);
16568   for (node = limbo_die_list; node; node = node->next)
16569     prune_unmark_dies (node->die);
16570 }
16571
16572 /* Set the parameter to true if there are any relative pathnames in
16573    the file table.  */
16574 static int
16575 file_table_relative_p (void ** slot, void *param)
16576 {
16577   bool *p = (bool *) param;
16578   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16579   if (!IS_ABSOLUTE_PATH (d->filename))
16580     {
16581       *p = true;
16582       return 0;
16583     }
16584   return 1;
16585 }
16586
16587 /* Output stuff that dwarf requires at the end of every file,
16588    and generate the DWARF-2 debugging info.  */
16589
16590 static void
16591 dwarf2out_finish (const char *filename)
16592 {
16593   limbo_die_node *node, *next_node;
16594   dw_die_ref die = 0;
16595   unsigned int i;
16596
16597   /* Add the name for the main input file now.  We delayed this from
16598      dwarf2out_init to avoid complications with PCH.  */
16599   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16600   if (!IS_ABSOLUTE_PATH (filename))
16601     add_comp_dir_attribute (comp_unit_die);
16602   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16603     {
16604       bool p = false;
16605       htab_traverse (file_table, file_table_relative_p, &p);
16606       if (p)
16607         add_comp_dir_attribute (comp_unit_die);
16608     }
16609
16610   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16611     {
16612       add_location_or_const_value_attribute (
16613         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16614         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16615         DW_AT_location);
16616     }
16617
16618   /* Traverse the limbo die list, and add parent/child links.  The only
16619      dies without parents that should be here are concrete instances of
16620      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16621      For concrete instances, we can get the parent die from the abstract
16622      instance.  */
16623   for (node = limbo_die_list; node; node = next_node)
16624     {
16625       next_node = node->next;
16626       die = node->die;
16627
16628       if (die->die_parent == NULL)
16629         {
16630           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16631
16632           if (origin)
16633             add_child_die (origin->die_parent, die);
16634           else if (die == comp_unit_die)
16635             ;
16636           else if (errorcount > 0 || sorrycount > 0)
16637             /* It's OK to be confused by errors in the input.  */
16638             add_child_die (comp_unit_die, die);
16639           else
16640             {
16641               /* In certain situations, the lexical block containing a
16642                  nested function can be optimized away, which results
16643                  in the nested function die being orphaned.  Likewise
16644                  with the return type of that nested function.  Force
16645                  this to be a child of the containing function.
16646
16647                  It may happen that even the containing function got fully
16648                  inlined and optimized out.  In that case we are lost and
16649                  assign the empty child.  This should not be big issue as
16650                  the function is likely unreachable too.  */
16651               tree context = NULL_TREE;
16652
16653               gcc_assert (node->created_for);
16654
16655               if (DECL_P (node->created_for))
16656                 context = DECL_CONTEXT (node->created_for);
16657               else if (TYPE_P (node->created_for))
16658                 context = TYPE_CONTEXT (node->created_for);
16659
16660               gcc_assert (context
16661                           && (TREE_CODE (context) == FUNCTION_DECL
16662                               || TREE_CODE (context) == NAMESPACE_DECL));
16663
16664               origin = lookup_decl_die (context);
16665               if (origin)
16666                 add_child_die (origin, die);
16667               else
16668                 add_child_die (comp_unit_die, die);
16669             }
16670         }
16671     }
16672
16673   limbo_die_list = NULL;
16674
16675   /* Walk through the list of incomplete types again, trying once more to
16676      emit full debugging info for them.  */
16677   retry_incomplete_types ();
16678
16679   if (flag_eliminate_unused_debug_types)
16680     prune_unused_types ();
16681
16682   /* Generate separate CUs for each of the include files we've seen.
16683      They will go into limbo_die_list.  */
16684   if (flag_eliminate_dwarf2_dups)
16685     break_out_includes (comp_unit_die);
16686
16687   /* Traverse the DIE's and add add sibling attributes to those DIE's
16688      that have children.  */
16689   add_sibling_attributes (comp_unit_die);
16690   for (node = limbo_die_list; node; node = node->next)
16691     add_sibling_attributes (node->die);
16692
16693   /* Output a terminator label for the .text section.  */
16694   switch_to_section (text_section);
16695   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16696   if (flag_reorder_blocks_and_partition)
16697     {
16698       switch_to_section (unlikely_text_section ());
16699       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16700     }
16701
16702   /* We can only use the low/high_pc attributes if all of the code was
16703      in .text.  */
16704   if (!have_multiple_function_sections)
16705     {
16706       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16707       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16708     }
16709
16710   else
16711     {
16712       unsigned fde_idx = 0;
16713
16714       /* We need to give .debug_loc and .debug_ranges an appropriate
16715          "base address".  Use zero so that these addresses become
16716          absolute.  Historically, we've emitted the unexpected
16717          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16718          Emit both to give time for other tools to adapt.  */
16719       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16720       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16721
16722       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16723                          add_ranges_by_labels (text_section_label,
16724                                                text_end_label));
16725       if (flag_reorder_blocks_and_partition)
16726         add_ranges_by_labels (cold_text_section_label,
16727                               cold_end_label);
16728
16729       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16730         {
16731           dw_fde_ref fde = &fde_table[fde_idx];
16732
16733           if (fde->dw_fde_switched_sections)
16734             {
16735               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16736                                     fde->dw_fde_hot_section_end_label);
16737               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16738                                     fde->dw_fde_unlikely_section_end_label);
16739             }
16740           else
16741             add_ranges_by_labels (fde->dw_fde_begin,
16742                                   fde->dw_fde_end);
16743         }
16744
16745       add_ranges (NULL);
16746     }
16747
16748   /* Output location list section if necessary.  */
16749   if (have_location_lists)
16750     {
16751       /* Output the location lists info.  */
16752       switch_to_section (debug_loc_section);
16753       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16754                                    DEBUG_LOC_SECTION_LABEL, 0);
16755       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16756       output_location_lists (die);
16757     }
16758
16759   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16760     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16761                     debug_line_section_label);
16762
16763   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16764     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16765
16766   /* Output all of the compilation units.  We put the main one last so that
16767      the offsets are available to output_pubnames.  */
16768   for (node = limbo_die_list; node; node = node->next)
16769     output_comp_unit (node->die, 0);
16770
16771   /* Output the main compilation unit if non-empty or if .debug_macinfo
16772      has been emitted.  */
16773   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16774
16775   /* Output the abbreviation table.  */
16776   switch_to_section (debug_abbrev_section);
16777   output_abbrev_section ();
16778
16779   /* Output public names table if necessary.  */
16780   if (!VEC_empty (pubname_entry, pubname_table))
16781     {
16782       switch_to_section (debug_pubnames_section);
16783       output_pubnames (pubname_table);
16784     }
16785
16786 #ifdef DEBUG_PUBTYPES_SECTION
16787   /* Output public types table if necessary.  */
16788   if (!VEC_empty (pubname_entry, pubtype_table))
16789     {
16790       switch_to_section (debug_pubtypes_section);
16791       output_pubnames (pubtype_table);
16792     }
16793 #endif
16794
16795   /* Output the address range information.  We only put functions in the arange
16796      table, so don't write it out if we don't have any.  */
16797   if (fde_table_in_use)
16798     {
16799       switch_to_section (debug_aranges_section);
16800       output_aranges ();
16801     }
16802
16803   /* Output ranges section if necessary.  */
16804   if (ranges_table_in_use)
16805     {
16806       switch_to_section (debug_ranges_section);
16807       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16808       output_ranges ();
16809     }
16810
16811   /* Output the source line correspondence table.  We must do this
16812      even if there is no line information.  Otherwise, on an empty
16813      translation unit, we will generate a present, but empty,
16814      .debug_info section.  IRIX 6.5 `nm' will then complain when
16815      examining the file.  This is done late so that any filenames
16816      used by the debug_info section are marked as 'used'.  */
16817   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16818     {
16819       switch_to_section (debug_line_section);
16820       output_line_info ();
16821     }
16822
16823   /* Have to end the macro section.  */
16824   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16825     {
16826       switch_to_section (debug_macinfo_section);
16827       dw2_asm_output_data (1, 0, "End compilation unit");
16828     }
16829
16830   /* If we emitted any DW_FORM_strp form attribute, output the string
16831      table too.  */
16832   if (debug_str_hash)
16833     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16834 }
16835 #else
16836
16837 /* This should never be used, but its address is needed for comparisons.  */
16838 const struct gcc_debug_hooks dwarf2_debug_hooks;
16839
16840 #endif /* DWARF2_DEBUGGING_INFO */
16841
16842 #include "gt-dwarf2out.h"