OSDN Git Service

PR rtl-optimization/36998
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* Decide whether to emit frame unwind via assembler directives.  */
134
135 int
136 dwarf2out_do_cfi_asm (void)
137 {
138   int enc;
139
140 #ifdef MIPS_DEBUGGING_INFO
141   return false;
142 #endif
143   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
144     return false;
145   if (!eh_personality_libfunc)
146     return true;
147   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
148     return false;
149
150   /* Make sure the personality encoding is one the assembler can support.
151      In particular, aligned addresses can't be handled.  */
152   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
153   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
154     return false;
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158
159   return true;
160 }
161
162 /* The size of the target's pointer type.  */
163 #ifndef PTR_SIZE
164 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
165 #endif
166
167 /* Array of RTXes referenced by the debugging information, which therefore
168    must be kept around forever.  */
169 static GTY(()) VEC(rtx,gc) *used_rtx_array;
170
171 /* A pointer to the base of a list of incomplete types which might be
172    completed at some later time.  incomplete_types_list needs to be a
173    VEC(tree,gc) because we want to tell the garbage collector about
174    it.  */
175 static GTY(()) VEC(tree,gc) *incomplete_types;
176
177 /* A pointer to the base of a table of references to declaration
178    scopes.  This table is a display which tracks the nesting
179    of declaration scopes at the current scope and containing
180    scopes.  This table is used to find the proper place to
181    define type declaration DIE's.  */
182 static GTY(()) VEC(tree,gc) *decl_scope_table;
183
184 /* Pointers to various DWARF2 sections.  */
185 static GTY(()) section *debug_info_section;
186 static GTY(()) section *debug_abbrev_section;
187 static GTY(()) section *debug_aranges_section;
188 static GTY(()) section *debug_macinfo_section;
189 static GTY(()) section *debug_line_section;
190 static GTY(()) section *debug_loc_section;
191 static GTY(()) section *debug_pubnames_section;
192 static GTY(()) section *debug_pubtypes_section;
193 static GTY(()) section *debug_str_section;
194 static GTY(()) section *debug_ranges_section;
195 static GTY(()) section *debug_frame_section;
196
197 /* How to start an assembler comment.  */
198 #ifndef ASM_COMMENT_START
199 #define ASM_COMMENT_START ";#"
200 #endif
201
202 typedef struct dw_cfi_struct *dw_cfi_ref;
203 typedef struct dw_fde_struct *dw_fde_ref;
204 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
205
206 /* Call frames are described using a sequence of Call Frame
207    Information instructions.  The register number, offset
208    and address fields are provided as possible operands;
209    their use is selected by the opcode field.  */
210
211 enum dw_cfi_oprnd_type {
212   dw_cfi_oprnd_unused,
213   dw_cfi_oprnd_reg_num,
214   dw_cfi_oprnd_offset,
215   dw_cfi_oprnd_addr,
216   dw_cfi_oprnd_loc
217 };
218
219 typedef union dw_cfi_oprnd_struct GTY(())
220 {
221   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
222   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
223   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
224   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
225 }
226 dw_cfi_oprnd;
227
228 typedef struct dw_cfi_struct GTY(())
229 {
230   dw_cfi_ref dw_cfi_next;
231   enum dwarf_call_frame_info dw_cfi_opc;
232   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
233     dw_cfi_oprnd1;
234   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
235     dw_cfi_oprnd2;
236 }
237 dw_cfi_node;
238
239 /* This is how we define the location of the CFA. We use to handle it
240    as REG + OFFSET all the time,  but now it can be more complex.
241    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
242    Instead of passing around REG and OFFSET, we pass a copy
243    of this structure.  */
244 typedef struct cfa_loc GTY(())
245 {
246   HOST_WIDE_INT offset;
247   HOST_WIDE_INT base_offset;
248   unsigned int reg;
249   int indirect;            /* 1 if CFA is accessed via a dereference.  */
250 } dw_cfa_location;
251
252 /* All call frame descriptions (FDE's) in the GCC generated DWARF
253    refer to a single Common Information Entry (CIE), defined at
254    the beginning of the .debug_frame section.  This use of a single
255    CIE obviates the need to keep track of multiple CIE's
256    in the DWARF generation routines below.  */
257
258 typedef struct dw_fde_struct GTY(())
259 {
260   tree decl;
261   const char *dw_fde_begin;
262   const char *dw_fde_current_label;
263   const char *dw_fde_end;
264   const char *dw_fde_hot_section_label;
265   const char *dw_fde_hot_section_end_label;
266   const char *dw_fde_unlikely_section_label;
267   const char *dw_fde_unlikely_section_end_label;
268   bool dw_fde_switched_sections;
269   dw_cfi_ref dw_fde_cfi;
270   unsigned funcdef_number;
271   HOST_WIDE_INT stack_realignment;
272   /* Dynamic realign argument pointer register.  */
273   unsigned int drap_reg;
274   /* Virtual dynamic realign argument pointer register.  */
275   unsigned int vdrap_reg;
276   unsigned all_throwers_are_sibcalls : 1;
277   unsigned nothrow : 1;
278   unsigned uses_eh_lsda : 1;
279   /* Whether we did stack realign in this call frame.  */
280   unsigned stack_realign : 1;
281   /* Whether dynamic realign argument pointer register has been saved.  */
282   unsigned drap_reg_saved: 1;
283 }
284 dw_fde_node;
285
286 /* Maximum size (in bytes) of an artificially generated label.  */
287 #define MAX_ARTIFICIAL_LABEL_BYTES      30
288
289 /* The size of addresses as they appear in the Dwarf 2 data.
290    Some architectures use word addresses to refer to code locations,
291    but Dwarf 2 info always uses byte addresses.  On such machines,
292    Dwarf 2 addresses need to be larger than the architecture's
293    pointers.  */
294 #ifndef DWARF2_ADDR_SIZE
295 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
296 #endif
297
298 /* The size in bytes of a DWARF field indicating an offset or length
299    relative to a debug info section, specified to be 4 bytes in the
300    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
301    as PTR_SIZE.  */
302
303 #ifndef DWARF_OFFSET_SIZE
304 #define DWARF_OFFSET_SIZE 4
305 #endif
306
307 /* According to the (draft) DWARF 3 specification, the initial length
308    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
309    bytes are 0xffffffff, followed by the length stored in the next 8
310    bytes.
311
312    However, the SGI/MIPS ABI uses an initial length which is equal to
313    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
314
315 #ifndef DWARF_INITIAL_LENGTH_SIZE
316 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
317 #endif
318
319 #define DWARF_VERSION 2
320
321 /* Round SIZE up to the nearest BOUNDARY.  */
322 #define DWARF_ROUND(SIZE,BOUNDARY) \
323   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
324
325 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
326 #ifndef DWARF_CIE_DATA_ALIGNMENT
327 #ifdef STACK_GROWS_DOWNWARD
328 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
329 #else
330 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
331 #endif
332 #endif
333
334 /* CIE identifier.  */
335 #if HOST_BITS_PER_WIDE_INT >= 64
336 #define DWARF_CIE_ID \
337   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
338 #else
339 #define DWARF_CIE_ID DW_CIE_ID
340 #endif
341
342 /* A pointer to the base of a table that contains frame description
343    information for each routine.  */
344 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
345
346 /* Number of elements currently allocated for fde_table.  */
347 static GTY(()) unsigned fde_table_allocated;
348
349 /* Number of elements in fde_table currently in use.  */
350 static GTY(()) unsigned fde_table_in_use;
351
352 /* Size (in elements) of increments by which we may expand the
353    fde_table.  */
354 #define FDE_TABLE_INCREMENT 256
355
356 /* Get the current fde_table entry we should use.  */
357
358 static inline dw_fde_ref
359 current_fde (void)
360 {
361   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
362 }
363
364 /* A list of call frame insns for the CIE.  */
365 static GTY(()) dw_cfi_ref cie_cfi_head;
366
367 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
368 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
369    attribute that accelerates the lookup of the FDE associated
370    with the subprogram.  This variable holds the table index of the FDE
371    associated with the current function (body) definition.  */
372 static unsigned current_funcdef_fde;
373 #endif
374
375 struct indirect_string_node GTY(())
376 {
377   const char *str;
378   unsigned int refcount;
379   unsigned int form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
415                                           HOST_WIDE_INT);
416 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
417 static void output_cfi_directive (dw_cfi_ref);
418 static void output_call_frame_info (int);
419 static void dwarf2out_note_section_used (void);
420 static void dwarf2out_stack_adjust (rtx, bool);
421 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
422 static void flush_queued_reg_saves (void);
423 static bool clobbers_queued_reg_save (const_rtx);
424 static void dwarf2out_frame_debug_expr (rtx, const char *);
425
426 /* Support for complex CFA locations.  */
427 static void output_cfa_loc (dw_cfi_ref);
428 static void output_cfa_loc_raw (dw_cfi_ref);
429 static void get_cfa_from_loc_descr (dw_cfa_location *,
430                                     struct dw_loc_descr_struct *);
431 static struct dw_loc_descr_struct *build_cfa_loc
432   (dw_cfa_location *, HOST_WIDE_INT);
433 static struct dw_loc_descr_struct *build_cfa_aligned_loc
434   (HOST_WIDE_INT, HOST_WIDE_INT);
435 static void def_cfa_1 (const char *, dw_cfa_location *);
436
437 /* How to start an assembler comment.  */
438 #ifndef ASM_COMMENT_START
439 #define ASM_COMMENT_START ";#"
440 #endif
441
442 /* Data and reference forms for relocatable data.  */
443 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
444 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
445
446 #ifndef DEBUG_FRAME_SECTION
447 #define DEBUG_FRAME_SECTION     ".debug_frame"
448 #endif
449
450 #ifndef FUNC_BEGIN_LABEL
451 #define FUNC_BEGIN_LABEL        "LFB"
452 #endif
453
454 #ifndef FUNC_END_LABEL
455 #define FUNC_END_LABEL          "LFE"
456 #endif
457
458 #ifndef FRAME_BEGIN_LABEL
459 #define FRAME_BEGIN_LABEL       "Lframe"
460 #endif
461 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
462 #define CIE_END_LABEL           "LECIE"
463 #define FDE_LABEL               "LSFDE"
464 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
465 #define FDE_END_LABEL           "LEFDE"
466 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
467 #define LINE_NUMBER_END_LABEL   "LELT"
468 #define LN_PROLOG_AS_LABEL      "LASLTP"
469 #define LN_PROLOG_END_LABEL     "LELTP"
470 #define DIE_LABEL_PREFIX        "DW"
471
472 /* The DWARF 2 CFA column which tracks the return address.  Normally this
473    is the column for PC, or the first column after all of the hard
474    registers.  */
475 #ifndef DWARF_FRAME_RETURN_COLUMN
476 #ifdef PC_REGNUM
477 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
478 #else
479 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
480 #endif
481 #endif
482
483 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
484    default, we just provide columns for all registers.  */
485 #ifndef DWARF_FRAME_REGNUM
486 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
487 #endif
488 \f
489 /* Hook used by __throw.  */
490
491 rtx
492 expand_builtin_dwarf_sp_column (void)
493 {
494   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
495   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
496 }
497
498 /* Return a pointer to a copy of the section string name S with all
499    attributes stripped off, and an asterisk prepended (for assemble_name).  */
500
501 static inline char *
502 stripattributes (const char *s)
503 {
504   char *stripped = XNEWVEC (char, strlen (s) + 2);
505   char *p = stripped;
506
507   *p++ = '*';
508
509   while (*s && *s != ',')
510     *p++ = *s++;
511
512   *p = '\0';
513   return stripped;
514 }
515
516 /* MEM is a memory reference for the register size table, each element of
517    which has mode MODE.  Initialize column C as a return address column.  */
518
519 static void
520 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
521 {
522   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
523   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
524   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525 }
526
527 /* Generate code to initialize the register size table.  */
528
529 void
530 expand_builtin_init_dwarf_reg_sizes (tree address)
531 {
532   unsigned int i;
533   enum machine_mode mode = TYPE_MODE (char_type_node);
534   rtx addr = expand_normal (address);
535   rtx mem = gen_rtx_MEM (BLKmode, addr);
536   bool wrote_return_column = false;
537
538   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
539     {
540       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
541
542       if (rnum < DWARF_FRAME_REGISTERS)
543         {
544           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
545           enum machine_mode save_mode = reg_raw_mode[i];
546           HOST_WIDE_INT size;
547
548           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
549             save_mode = choose_hard_reg_mode (i, 1, true);
550           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
551             {
552               if (save_mode == VOIDmode)
553                 continue;
554               wrote_return_column = true;
555             }
556           size = GET_MODE_SIZE (save_mode);
557           if (offset < 0)
558             continue;
559
560           emit_move_insn (adjust_address (mem, mode, offset),
561                           gen_int_mode (size, mode));
562         }
563     }
564
565   if (!wrote_return_column)
566     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
567
568 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
569   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
570 #endif
571
572   targetm.init_dwarf_reg_sizes_extra (address);
573 }
574
575 /* Convert a DWARF call frame info. operation to its string name */
576
577 static const char *
578 dwarf_cfi_name (unsigned int cfi_opc)
579 {
580   switch (cfi_opc)
581     {
582     case DW_CFA_advance_loc:
583       return "DW_CFA_advance_loc";
584     case DW_CFA_offset:
585       return "DW_CFA_offset";
586     case DW_CFA_restore:
587       return "DW_CFA_restore";
588     case DW_CFA_nop:
589       return "DW_CFA_nop";
590     case DW_CFA_set_loc:
591       return "DW_CFA_set_loc";
592     case DW_CFA_advance_loc1:
593       return "DW_CFA_advance_loc1";
594     case DW_CFA_advance_loc2:
595       return "DW_CFA_advance_loc2";
596     case DW_CFA_advance_loc4:
597       return "DW_CFA_advance_loc4";
598     case DW_CFA_offset_extended:
599       return "DW_CFA_offset_extended";
600     case DW_CFA_restore_extended:
601       return "DW_CFA_restore_extended";
602     case DW_CFA_undefined:
603       return "DW_CFA_undefined";
604     case DW_CFA_same_value:
605       return "DW_CFA_same_value";
606     case DW_CFA_register:
607       return "DW_CFA_register";
608     case DW_CFA_remember_state:
609       return "DW_CFA_remember_state";
610     case DW_CFA_restore_state:
611       return "DW_CFA_restore_state";
612     case DW_CFA_def_cfa:
613       return "DW_CFA_def_cfa";
614     case DW_CFA_def_cfa_register:
615       return "DW_CFA_def_cfa_register";
616     case DW_CFA_def_cfa_offset:
617       return "DW_CFA_def_cfa_offset";
618
619     /* DWARF 3 */
620     case DW_CFA_def_cfa_expression:
621       return "DW_CFA_def_cfa_expression";
622     case DW_CFA_expression:
623       return "DW_CFA_expression";
624     case DW_CFA_offset_extended_sf:
625       return "DW_CFA_offset_extended_sf";
626     case DW_CFA_def_cfa_sf:
627       return "DW_CFA_def_cfa_sf";
628     case DW_CFA_def_cfa_offset_sf:
629       return "DW_CFA_def_cfa_offset_sf";
630
631     /* SGI/MIPS specific */
632     case DW_CFA_MIPS_advance_loc8:
633       return "DW_CFA_MIPS_advance_loc8";
634
635     /* GNU extensions */
636     case DW_CFA_GNU_window_save:
637       return "DW_CFA_GNU_window_save";
638     case DW_CFA_GNU_args_size:
639       return "DW_CFA_GNU_args_size";
640     case DW_CFA_GNU_negative_offset_extended:
641       return "DW_CFA_GNU_negative_offset_extended";
642
643     default:
644       return "DW_CFA_<unknown>";
645     }
646 }
647
648 /* Return a pointer to a newly allocated Call Frame Instruction.  */
649
650 static inline dw_cfi_ref
651 new_cfi (void)
652 {
653   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
654
655   cfi->dw_cfi_next = NULL;
656   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
657   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
658
659   return cfi;
660 }
661
662 /* Add a Call Frame Instruction to list of instructions.  */
663
664 static inline void
665 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
666 {
667   dw_cfi_ref *p;
668   dw_fde_ref fde = current_fde ();
669
670   /* When DRAP is used, CFA is defined with an expression.  Redefine
671      CFA may lead to a different CFA value.   */
672   if (fde && fde->drap_reg != INVALID_REGNUM)
673     switch (cfi->dw_cfi_opc)
674       {
675         case DW_CFA_def_cfa_register:
676         case DW_CFA_def_cfa_offset:
677         case DW_CFA_def_cfa_offset_sf:
678         case DW_CFA_def_cfa:
679         case DW_CFA_def_cfa_sf:
680           gcc_unreachable ();
681
682         default:
683           break;
684       }
685
686   /* Find the end of the chain.  */
687   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
688     ;
689
690   *p = cfi;
691 }
692
693 /* Generate a new label for the CFI info to refer to.  */
694
695 char *
696 dwarf2out_cfi_label (void)
697 {
698   static char label[20];
699
700   if (dwarf2out_do_cfi_asm ())
701     {
702       /* In this case, we will be emitting the asm directive instead of
703          the label, so just return a placeholder to keep the rest of the
704          interfaces happy.  */
705       strcpy (label, "<do not output>");
706     }
707   else
708     {
709       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
710       ASM_OUTPUT_LABEL (asm_out_file, label);
711     }
712
713   return label;
714 }
715
716 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
717    or to the CIE if LABEL is NULL.  */
718
719 static void
720 add_fde_cfi (const char *label, dw_cfi_ref cfi)
721 {
722   dw_cfi_ref *list_head = &cie_cfi_head;
723
724   if (dwarf2out_do_cfi_asm ())
725     {
726       if (label)
727         {
728           output_cfi_directive (cfi);
729
730           /* We still have to add the cfi to the list so that
731              lookup_cfa works later on.  */
732           list_head = &current_fde ()->dw_fde_cfi;
733         }
734       /* ??? If this is a CFI for the CIE, we don't emit.  This
735          assumes that the standard CIE contents that the assembler
736          uses matches the standard CIE contents that the compiler
737          uses.  This is probably a bad assumption.  I'm not quite
738          sure how to address this for now.  */
739     }
740   else if (label)
741     {
742       dw_fde_ref fde = current_fde ();
743
744       gcc_assert (fde != NULL);
745
746       if (*label == 0)
747         label = dwarf2out_cfi_label ();
748
749       if (fde->dw_fde_current_label == NULL
750           || strcmp (label, fde->dw_fde_current_label) != 0)
751         {
752           dw_cfi_ref xcfi;
753
754           label = xstrdup (label);
755
756           /* Set the location counter to the new label.  */
757           xcfi = new_cfi ();
758           /* If we have a current label, advance from there, otherwise
759              set the location directly using set_loc.  */
760           xcfi->dw_cfi_opc = fde->dw_fde_current_label
761                              ? DW_CFA_advance_loc4
762                              : DW_CFA_set_loc;
763           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
764           add_cfi (&fde->dw_fde_cfi, xcfi);
765
766           fde->dw_fde_current_label = label;
767         }
768
769       list_head = &fde->dw_fde_cfi;
770     }
771
772   add_cfi (list_head, cfi);
773 }
774
775 /* Subroutine of lookup_cfa.  */
776
777 static void
778 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
779 {
780   switch (cfi->dw_cfi_opc)
781     {
782     case DW_CFA_def_cfa_offset:
783     case DW_CFA_def_cfa_offset_sf:
784       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
785       break;
786     case DW_CFA_def_cfa_register:
787       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
788       break;
789     case DW_CFA_def_cfa:
790     case DW_CFA_def_cfa_sf:
791       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
792       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
793       break;
794     case DW_CFA_def_cfa_expression:
795       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
796       break;
797     default:
798       break;
799     }
800 }
801
802 /* Find the previous value for the CFA.  */
803
804 static void
805 lookup_cfa (dw_cfa_location *loc)
806 {
807   dw_cfi_ref cfi;
808   dw_fde_ref fde;
809
810   loc->reg = INVALID_REGNUM;
811   loc->offset = 0;
812   loc->indirect = 0;
813   loc->base_offset = 0;
814
815   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
816     lookup_cfa_1 (cfi, loc);
817
818   fde = current_fde ();
819   if (fde)
820     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
821       lookup_cfa_1 (cfi, loc);
822 }
823
824 /* The current rule for calculating the DWARF2 canonical frame address.  */
825 static dw_cfa_location cfa;
826
827 /* The register used for saving registers to the stack, and its offset
828    from the CFA.  */
829 static dw_cfa_location cfa_store;
830
831 /* The running total of the size of arguments pushed onto the stack.  */
832 static HOST_WIDE_INT args_size;
833
834 /* The last args_size we actually output.  */
835 static HOST_WIDE_INT old_args_size;
836
837 /* Entry point to update the canonical frame address (CFA).
838    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
839    calculated from REG+OFFSET.  */
840
841 void
842 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
843 {
844   dw_cfa_location loc;
845   loc.indirect = 0;
846   loc.base_offset = 0;
847   loc.reg = reg;
848   loc.offset = offset;
849   def_cfa_1 (label, &loc);
850 }
851
852 /* Determine if two dw_cfa_location structures define the same data.  */
853
854 static bool
855 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
856 {
857   return (loc1->reg == loc2->reg
858           && loc1->offset == loc2->offset
859           && loc1->indirect == loc2->indirect
860           && (loc1->indirect == 0
861               || loc1->base_offset == loc2->base_offset));
862 }
863
864 /* This routine does the actual work.  The CFA is now calculated from
865    the dw_cfa_location structure.  */
866
867 static void
868 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
869 {
870   dw_cfi_ref cfi;
871   dw_cfa_location old_cfa, loc;
872
873   cfa = *loc_p;
874   loc = *loc_p;
875
876   if (cfa_store.reg == loc.reg && loc.indirect == 0)
877     cfa_store.offset = loc.offset;
878
879   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
880   lookup_cfa (&old_cfa);
881
882   /* If nothing changed, no need to issue any call frame instructions.  */
883   if (cfa_equal_p (&loc, &old_cfa))
884     return;
885
886   cfi = new_cfi ();
887
888   if (loc.reg == old_cfa.reg && !loc.indirect)
889     {
890       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
891          the CFA register did not change but the offset did.  The data 
892          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
893          in the assembler via the .cfi_def_cfa_offset directive.  */
894       if (loc.offset < 0)
895         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
896       else
897         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
898       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
899     }
900
901 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
902   else if (loc.offset == old_cfa.offset
903            && old_cfa.reg != INVALID_REGNUM
904            && !loc.indirect)
905     {
906       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
907          indicating the CFA register has changed to <register> but the
908          offset has not changed.  */
909       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
910       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
911     }
912 #endif
913
914   else if (loc.indirect == 0)
915     {
916       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
917          indicating the CFA register has changed to <register> with
918          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
919          happens in output_cfi, or in the assembler via the .cfi_def_cfa
920          directive.  */
921       if (loc.offset < 0)
922         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
923       else
924         cfi->dw_cfi_opc = DW_CFA_def_cfa;
925       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
926       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
927     }
928   else
929     {
930       /* Construct a DW_CFA_def_cfa_expression instruction to
931          calculate the CFA using a full location expression since no
932          register-offset pair is available.  */
933       struct dw_loc_descr_struct *loc_list;
934
935       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
936       loc_list = build_cfa_loc (&loc, 0);
937       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
938     }
939
940   add_fde_cfi (label, cfi);
941 }
942
943 /* Add the CFI for saving a register.  REG is the CFA column number.
944    LABEL is passed to add_fde_cfi.
945    If SREG is -1, the register is saved at OFFSET from the CFA;
946    otherwise it is saved in SREG.  */
947
948 static void
949 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
950 {
951   dw_cfi_ref cfi = new_cfi ();
952   dw_fde_ref fde = current_fde ();
953
954   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
955
956   /* When stack is aligned, store REG using DW_CFA_expression with
957      FP.  */
958   if (fde
959       && fde->stack_realign
960       && sreg == INVALID_REGNUM)
961     {
962       cfi->dw_cfi_opc = DW_CFA_expression;
963       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
964       cfi->dw_cfi_oprnd1.dw_cfi_loc
965         = build_cfa_aligned_loc (offset, fde->stack_realignment);
966     }
967   else if (sreg == INVALID_REGNUM)
968     {
969       if (offset < 0)
970         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
971       else if (reg & ~0x3f)
972         cfi->dw_cfi_opc = DW_CFA_offset_extended;
973       else
974         cfi->dw_cfi_opc = DW_CFA_offset;
975       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
976     }
977   else if (sreg == reg)
978     cfi->dw_cfi_opc = DW_CFA_same_value;
979   else
980     {
981       cfi->dw_cfi_opc = DW_CFA_register;
982       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
983     }
984
985   add_fde_cfi (label, cfi);
986 }
987
988 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
989    This CFI tells the unwinder that it needs to restore the window registers
990    from the previous frame's window save area.
991
992    ??? Perhaps we should note in the CIE where windows are saved (instead of
993    assuming 0(cfa)) and what registers are in the window.  */
994
995 void
996 dwarf2out_window_save (const char *label)
997 {
998   dw_cfi_ref cfi = new_cfi ();
999
1000   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1001   add_fde_cfi (label, cfi);
1002 }
1003
1004 /* Add a CFI to update the running total of the size of arguments
1005    pushed onto the stack.  */
1006
1007 void
1008 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1009 {
1010   dw_cfi_ref cfi;
1011
1012   if (size == old_args_size)
1013     return;
1014
1015   old_args_size = size;
1016
1017   cfi = new_cfi ();
1018   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1019   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1020   add_fde_cfi (label, cfi);
1021 }
1022
1023 /* Entry point for saving a register to the stack.  REG is the GCC register
1024    number.  LABEL and OFFSET are passed to reg_save.  */
1025
1026 void
1027 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1028 {
1029   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1030 }
1031
1032 /* Entry point for saving the return address in the stack.
1033    LABEL and OFFSET are passed to reg_save.  */
1034
1035 void
1036 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1037 {
1038   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1039 }
1040
1041 /* Entry point for saving the return address in a register.
1042    LABEL and SREG are passed to reg_save.  */
1043
1044 void
1045 dwarf2out_return_reg (const char *label, unsigned int sreg)
1046 {
1047   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1048 }
1049
1050 #ifdef DWARF2_UNWIND_INFO
1051 /* Record the initial position of the return address.  RTL is
1052    INCOMING_RETURN_ADDR_RTX.  */
1053
1054 static void
1055 initial_return_save (rtx rtl)
1056 {
1057   unsigned int reg = INVALID_REGNUM;
1058   HOST_WIDE_INT offset = 0;
1059
1060   switch (GET_CODE (rtl))
1061     {
1062     case REG:
1063       /* RA is in a register.  */
1064       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1065       break;
1066
1067     case MEM:
1068       /* RA is on the stack.  */
1069       rtl = XEXP (rtl, 0);
1070       switch (GET_CODE (rtl))
1071         {
1072         case REG:
1073           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1074           offset = 0;
1075           break;
1076
1077         case PLUS:
1078           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1079           offset = INTVAL (XEXP (rtl, 1));
1080           break;
1081
1082         case MINUS:
1083           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1084           offset = -INTVAL (XEXP (rtl, 1));
1085           break;
1086
1087         default:
1088           gcc_unreachable ();
1089         }
1090
1091       break;
1092
1093     case PLUS:
1094       /* The return address is at some offset from any value we can
1095          actually load.  For instance, on the SPARC it is in %i7+8. Just
1096          ignore the offset for now; it doesn't matter for unwinding frames.  */
1097       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1098       initial_return_save (XEXP (rtl, 0));
1099       return;
1100
1101     default:
1102       gcc_unreachable ();
1103     }
1104
1105   if (reg != DWARF_FRAME_RETURN_COLUMN)
1106     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1107 }
1108 #endif
1109
1110 /* Given a SET, calculate the amount of stack adjustment it
1111    contains.  */
1112
1113 static HOST_WIDE_INT
1114 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1115                      HOST_WIDE_INT cur_offset)
1116 {
1117   const_rtx src = SET_SRC (pattern);
1118   const_rtx dest = SET_DEST (pattern);
1119   HOST_WIDE_INT offset = 0;
1120   enum rtx_code code;
1121
1122   if (dest == stack_pointer_rtx)
1123     {
1124       code = GET_CODE (src);
1125
1126       /* Assume (set (reg sp) (reg whatever)) sets args_size
1127          level to 0.  */
1128       if (code == REG && src != stack_pointer_rtx)
1129         {
1130           offset = -cur_args_size;
1131 #ifndef STACK_GROWS_DOWNWARD
1132           offset = -offset;
1133 #endif
1134           return offset - cur_offset;
1135         }
1136
1137       if (! (code == PLUS || code == MINUS)
1138           || XEXP (src, 0) != stack_pointer_rtx
1139           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1140         return 0;
1141
1142       /* (set (reg sp) (plus (reg sp) (const_int))) */
1143       offset = INTVAL (XEXP (src, 1));
1144       if (code == PLUS)
1145         offset = -offset;
1146       return offset;
1147     }
1148
1149   if (MEM_P (src) && !MEM_P (dest))
1150     dest = src;
1151   if (MEM_P (dest))
1152     {
1153       /* (set (mem (pre_dec (reg sp))) (foo)) */
1154       src = XEXP (dest, 0);
1155       code = GET_CODE (src);
1156
1157       switch (code)
1158         {
1159         case PRE_MODIFY:
1160         case POST_MODIFY:
1161           if (XEXP (src, 0) == stack_pointer_rtx)
1162             {
1163               rtx val = XEXP (XEXP (src, 1), 1);
1164               /* We handle only adjustments by constant amount.  */
1165               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1166                           && GET_CODE (val) == CONST_INT);
1167               offset = -INTVAL (val);
1168               break;
1169             }
1170           return 0;
1171
1172         case PRE_DEC:
1173         case POST_DEC:
1174           if (XEXP (src, 0) == stack_pointer_rtx)
1175             {
1176               offset = GET_MODE_SIZE (GET_MODE (dest));
1177               break;
1178             }
1179           return 0;
1180
1181         case PRE_INC:
1182         case POST_INC:
1183           if (XEXP (src, 0) == stack_pointer_rtx)
1184             {
1185               offset = -GET_MODE_SIZE (GET_MODE (dest));
1186               break;
1187             }
1188           return 0;
1189
1190         default:
1191           return 0;
1192         }
1193     }
1194   else
1195     return 0;
1196
1197   return offset;
1198 }
1199
1200 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1201    indexed by INSN_UID.  */
1202
1203 static HOST_WIDE_INT *barrier_args_size;
1204
1205 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1206
1207 static HOST_WIDE_INT
1208 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1209                              VEC (rtx, heap) **next)
1210 {
1211   HOST_WIDE_INT offset = 0;
1212   int i;
1213
1214   if (! RTX_FRAME_RELATED_P (insn))
1215     {
1216       if (prologue_epilogue_contains (insn)
1217           || sibcall_epilogue_contains (insn))
1218         /* Nothing */;
1219       else if (GET_CODE (PATTERN (insn)) == SET)
1220         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1221       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1222                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1223         {
1224           /* There may be stack adjustments inside compound insns.  Search
1225              for them.  */
1226           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1227             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1228               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1229                                              cur_args_size, offset);
1230         }
1231     }
1232   else
1233     {
1234       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1235
1236       if (expr)
1237         {
1238           expr = XEXP (expr, 0);
1239           if (GET_CODE (expr) == PARALLEL
1240               || GET_CODE (expr) == SEQUENCE)
1241             for (i = 1; i < XVECLEN (expr, 0); i++)
1242               {
1243                 rtx elem = XVECEXP (expr, 0, i);
1244
1245                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1246                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1247               }
1248         }
1249     }
1250
1251 #ifndef STACK_GROWS_DOWNWARD
1252   offset = -offset;
1253 #endif
1254
1255   cur_args_size += offset;
1256   if (cur_args_size < 0)
1257     cur_args_size = 0;
1258
1259   if (JUMP_P (insn))
1260     {
1261       rtx dest = JUMP_LABEL (insn);
1262
1263       if (dest)
1264         {
1265           if (barrier_args_size [INSN_UID (dest)] < 0)
1266             {
1267               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1268               VEC_safe_push (rtx, heap, *next, dest);
1269             }
1270         }
1271     }
1272
1273   return cur_args_size;
1274 }
1275
1276 /* Walk the whole function and compute args_size on BARRIERs.  */
1277
1278 static void
1279 compute_barrier_args_size (void)
1280 {
1281   int max_uid = get_max_uid (), i;
1282   rtx insn;
1283   VEC (rtx, heap) *worklist, *next, *tmp;
1284
1285   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1286   for (i = 0; i < max_uid; i++)
1287     barrier_args_size[i] = -1;
1288
1289   worklist = VEC_alloc (rtx, heap, 20);
1290   next = VEC_alloc (rtx, heap, 20);
1291   insn = get_insns ();
1292   barrier_args_size[INSN_UID (insn)] = 0;
1293   VEC_quick_push (rtx, worklist, insn);
1294   for (;;)
1295     {
1296       while (!VEC_empty (rtx, worklist))
1297         {
1298           rtx prev, body, first_insn;
1299           HOST_WIDE_INT cur_args_size;
1300
1301           first_insn = insn = VEC_pop (rtx, worklist);
1302           cur_args_size = barrier_args_size[INSN_UID (insn)];
1303           prev = prev_nonnote_insn (insn);
1304           if (prev && BARRIER_P (prev))
1305             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1306
1307           for (; insn; insn = NEXT_INSN (insn))
1308             {
1309               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1310                 continue;
1311               if (BARRIER_P (insn))
1312                 break;
1313
1314               if (LABEL_P (insn))
1315                 {
1316                   if (insn == first_insn)
1317                     continue;
1318                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1319                     {
1320                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1321                       continue;
1322                     }
1323                   else
1324                     {
1325                       /* The insns starting with this label have been
1326                          already scanned or are in the worklist.  */
1327                       break;
1328                     }
1329                 }
1330
1331               body = PATTERN (insn);
1332               if (GET_CODE (body) == SEQUENCE)
1333                 {
1334                   HOST_WIDE_INT dest_args_size = cur_args_size;
1335                   for (i = 1; i < XVECLEN (body, 0); i++)
1336                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1337                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1338                       dest_args_size
1339                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1340                                                        dest_args_size, &next);
1341                     else
1342                       cur_args_size
1343                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1344                                                        cur_args_size, &next);
1345
1346                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1347                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1348                                                  dest_args_size, &next);
1349                   else
1350                     cur_args_size
1351                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1352                                                      cur_args_size, &next);
1353                 }
1354               else
1355                 cur_args_size
1356                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1357             }
1358         }
1359
1360       if (VEC_empty (rtx, next))
1361         break;
1362
1363       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1364       tmp = next;
1365       next = worklist;
1366       worklist = tmp;
1367       VEC_truncate (rtx, next, 0);
1368     }
1369
1370   VEC_free (rtx, heap, worklist);
1371   VEC_free (rtx, heap, next);
1372 }
1373
1374
1375 /* Check INSN to see if it looks like a push or a stack adjustment, and
1376    make a note of it if it does.  EH uses this information to find out how
1377    much extra space it needs to pop off the stack.  */
1378
1379 static void
1380 dwarf2out_stack_adjust (rtx insn, bool after_p)
1381 {
1382   HOST_WIDE_INT offset;
1383   const char *label;
1384   int i;
1385
1386   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1387      with this function.  Proper support would require all frame-related
1388      insns to be marked, and to be able to handle saving state around
1389      epilogues textually in the middle of the function.  */
1390   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1391     return;
1392
1393   /* If INSN is an instruction from target of an annulled branch, the
1394      effects are for the target only and so current argument size
1395      shouldn't change at all.  */
1396   if (final_sequence
1397       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1398       && INSN_FROM_TARGET_P (insn))
1399     return;
1400
1401   /* If only calls can throw, and we have a frame pointer,
1402      save up adjustments until we see the CALL_INSN.  */
1403   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1404     {
1405       if (CALL_P (insn) && !after_p)
1406         {
1407           /* Extract the size of the args from the CALL rtx itself.  */
1408           insn = PATTERN (insn);
1409           if (GET_CODE (insn) == PARALLEL)
1410             insn = XVECEXP (insn, 0, 0);
1411           if (GET_CODE (insn) == SET)
1412             insn = SET_SRC (insn);
1413           gcc_assert (GET_CODE (insn) == CALL);
1414           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1415         }
1416       return;
1417     }
1418
1419   if (CALL_P (insn) && !after_p)
1420     {
1421       if (!flag_asynchronous_unwind_tables)
1422         dwarf2out_args_size ("", args_size);
1423       return;
1424     }
1425   else if (BARRIER_P (insn))
1426     {
1427       /* Don't call compute_barrier_args_size () if the only
1428          BARRIER is at the end of function.  */
1429       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1430         compute_barrier_args_size ();
1431       if (barrier_args_size == NULL)
1432         offset = 0;
1433       else
1434         {
1435           offset = barrier_args_size[INSN_UID (insn)];
1436           if (offset < 0)
1437             offset = 0;
1438         }
1439
1440       offset -= args_size;
1441 #ifndef STACK_GROWS_DOWNWARD
1442       offset = -offset;
1443 #endif
1444     }
1445   else if (GET_CODE (PATTERN (insn)) == SET)
1446     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1447   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1448            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1449     {
1450       /* There may be stack adjustments inside compound insns.  Search
1451          for them.  */
1452       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1453         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1454           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1455                                          args_size, offset);
1456     }
1457   else
1458     return;
1459
1460   if (offset == 0)
1461     return;
1462
1463   label = dwarf2out_cfi_label ();
1464   dwarf2out_args_size_adjust (offset, label);
1465 }
1466
1467 /* Adjust args_size based on stack adjustment OFFSET.  */
1468
1469 static void
1470 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1471 {
1472   if (cfa.reg == STACK_POINTER_REGNUM)
1473     cfa.offset += offset;
1474
1475   if (cfa_store.reg == STACK_POINTER_REGNUM)
1476     cfa_store.offset += offset;
1477
1478 #ifndef STACK_GROWS_DOWNWARD
1479   offset = -offset;
1480 #endif
1481
1482   args_size += offset;
1483   if (args_size < 0)
1484     args_size = 0;
1485
1486   def_cfa_1 (label, &cfa);
1487   if (flag_asynchronous_unwind_tables)
1488     dwarf2out_args_size (label, args_size);
1489 }
1490
1491 #endif
1492
1493 /* We delay emitting a register save until either (a) we reach the end
1494    of the prologue or (b) the register is clobbered.  This clusters
1495    register saves so that there are fewer pc advances.  */
1496
1497 struct queued_reg_save GTY(())
1498 {
1499   struct queued_reg_save *next;
1500   rtx reg;
1501   HOST_WIDE_INT cfa_offset;
1502   rtx saved_reg;
1503 };
1504
1505 static GTY(()) struct queued_reg_save *queued_reg_saves;
1506
1507 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1508 struct reg_saved_in_data GTY(()) {
1509   rtx orig_reg;
1510   rtx saved_in_reg;
1511 };
1512
1513 /* A list of registers saved in other registers.
1514    The list intentionally has a small maximum capacity of 4; if your
1515    port needs more than that, you might consider implementing a
1516    more efficient data structure.  */
1517 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1518 static GTY(()) size_t num_regs_saved_in_regs;
1519
1520 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1521 static const char *last_reg_save_label;
1522
1523 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1524    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1525
1526 static void
1527 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1528 {
1529   struct queued_reg_save *q;
1530
1531   /* Duplicates waste space, but it's also necessary to remove them
1532      for correctness, since the queue gets output in reverse
1533      order.  */
1534   for (q = queued_reg_saves; q != NULL; q = q->next)
1535     if (REGNO (q->reg) == REGNO (reg))
1536       break;
1537
1538   if (q == NULL)
1539     {
1540       q = GGC_NEW (struct queued_reg_save);
1541       q->next = queued_reg_saves;
1542       queued_reg_saves = q;
1543     }
1544
1545   q->reg = reg;
1546   q->cfa_offset = offset;
1547   q->saved_reg = sreg;
1548
1549   last_reg_save_label = label;
1550 }
1551
1552 /* Output all the entries in QUEUED_REG_SAVES.  */
1553
1554 static void
1555 flush_queued_reg_saves (void)
1556 {
1557   struct queued_reg_save *q;
1558
1559   for (q = queued_reg_saves; q; q = q->next)
1560     {
1561       size_t i;
1562       unsigned int reg, sreg;
1563
1564       for (i = 0; i < num_regs_saved_in_regs; i++)
1565         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1566           break;
1567       if (q->saved_reg && i == num_regs_saved_in_regs)
1568         {
1569           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1570           num_regs_saved_in_regs++;
1571         }
1572       if (i != num_regs_saved_in_regs)
1573         {
1574           regs_saved_in_regs[i].orig_reg = q->reg;
1575           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1576         }
1577
1578       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1579       if (q->saved_reg)
1580         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1581       else
1582         sreg = INVALID_REGNUM;
1583       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1584     }
1585
1586   queued_reg_saves = NULL;
1587   last_reg_save_label = NULL;
1588 }
1589
1590 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1591    location for?  Or, does it clobber a register which we've previously
1592    said that some other register is saved in, and for which we now
1593    have a new location for?  */
1594
1595 static bool
1596 clobbers_queued_reg_save (const_rtx insn)
1597 {
1598   struct queued_reg_save *q;
1599
1600   for (q = queued_reg_saves; q; q = q->next)
1601     {
1602       size_t i;
1603       if (modified_in_p (q->reg, insn))
1604         return true;
1605       for (i = 0; i < num_regs_saved_in_regs; i++)
1606         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1607             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1608           return true;
1609     }
1610
1611   return false;
1612 }
1613
1614 /* Entry point for saving the first register into the second.  */
1615
1616 void
1617 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1618 {
1619   size_t i;
1620   unsigned int regno, sregno;
1621
1622   for (i = 0; i < num_regs_saved_in_regs; i++)
1623     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1624       break;
1625   if (i == num_regs_saved_in_regs)
1626     {
1627       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1628       num_regs_saved_in_regs++;
1629     }
1630   regs_saved_in_regs[i].orig_reg = reg;
1631   regs_saved_in_regs[i].saved_in_reg = sreg;
1632
1633   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1634   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1635   reg_save (label, regno, sregno, 0);
1636 }
1637
1638 /* What register, if any, is currently saved in REG?  */
1639
1640 static rtx
1641 reg_saved_in (rtx reg)
1642 {
1643   unsigned int regn = REGNO (reg);
1644   size_t i;
1645   struct queued_reg_save *q;
1646
1647   for (q = queued_reg_saves; q; q = q->next)
1648     if (q->saved_reg && regn == REGNO (q->saved_reg))
1649       return q->reg;
1650
1651   for (i = 0; i < num_regs_saved_in_regs; i++)
1652     if (regs_saved_in_regs[i].saved_in_reg
1653         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1654       return regs_saved_in_regs[i].orig_reg;
1655
1656   return NULL_RTX;
1657 }
1658
1659
1660 /* A temporary register holding an integral value used in adjusting SP
1661    or setting up the store_reg.  The "offset" field holds the integer
1662    value, not an offset.  */
1663 static dw_cfa_location cfa_temp;
1664
1665 /* Record call frame debugging information for an expression EXPR,
1666    which either sets SP or FP (adjusting how we calculate the frame
1667    address) or saves a register to the stack or another register.
1668    LABEL indicates the address of EXPR.
1669
1670    This function encodes a state machine mapping rtxes to actions on
1671    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1672    users need not read the source code.
1673
1674   The High-Level Picture
1675
1676   Changes in the register we use to calculate the CFA: Currently we
1677   assume that if you copy the CFA register into another register, we
1678   should take the other one as the new CFA register; this seems to
1679   work pretty well.  If it's wrong for some target, it's simple
1680   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1681
1682   Changes in the register we use for saving registers to the stack:
1683   This is usually SP, but not always.  Again, we deduce that if you
1684   copy SP into another register (and SP is not the CFA register),
1685   then the new register is the one we will be using for register
1686   saves.  This also seems to work.
1687
1688   Register saves: There's not much guesswork about this one; if
1689   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1690   register save, and the register used to calculate the destination
1691   had better be the one we think we're using for this purpose.
1692   It's also assumed that a copy from a call-saved register to another
1693   register is saving that register if RTX_FRAME_RELATED_P is set on
1694   that instruction.  If the copy is from a call-saved register to
1695   the *same* register, that means that the register is now the same
1696   value as in the caller.
1697
1698   Except: If the register being saved is the CFA register, and the
1699   offset is nonzero, we are saving the CFA, so we assume we have to
1700   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1701   the intent is to save the value of SP from the previous frame.
1702
1703   In addition, if a register has previously been saved to a different
1704   register,
1705
1706   Invariants / Summaries of Rules
1707
1708   cfa          current rule for calculating the CFA.  It usually
1709                consists of a register and an offset.
1710   cfa_store    register used by prologue code to save things to the stack
1711                cfa_store.offset is the offset from the value of
1712                cfa_store.reg to the actual CFA
1713   cfa_temp     register holding an integral value.  cfa_temp.offset
1714                stores the value, which will be used to adjust the
1715                stack pointer.  cfa_temp is also used like cfa_store,
1716                to track stores to the stack via fp or a temp reg.
1717
1718   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1719                with cfa.reg as the first operand changes the cfa.reg and its
1720                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1721                cfa_temp.offset.
1722
1723   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1724                expression yielding a constant.  This sets cfa_temp.reg
1725                and cfa_temp.offset.
1726
1727   Rule 5:      Create a new register cfa_store used to save items to the
1728                stack.
1729
1730   Rules 10-14: Save a register to the stack.  Define offset as the
1731                difference of the original location and cfa_store's
1732                location (or cfa_temp's location if cfa_temp is used).
1733
1734   Rules 16-20: If AND operation happens on sp in prologue, we assume
1735                stack is realigned.  We will use a group of DW_OP_XXX
1736                expressions to represent the location of the stored
1737                register instead of CFA+offset.
1738
1739   The Rules
1740
1741   "{a,b}" indicates a choice of a xor b.
1742   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1743
1744   Rule 1:
1745   (set <reg1> <reg2>:cfa.reg)
1746   effects: cfa.reg = <reg1>
1747            cfa.offset unchanged
1748            cfa_temp.reg = <reg1>
1749            cfa_temp.offset = cfa.offset
1750
1751   Rule 2:
1752   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1753                               {<const_int>,<reg>:cfa_temp.reg}))
1754   effects: cfa.reg = sp if fp used
1755            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1756            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1757              if cfa_store.reg==sp
1758
1759   Rule 3:
1760   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1761   effects: cfa.reg = fp
1762            cfa_offset += +/- <const_int>
1763
1764   Rule 4:
1765   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1766   constraints: <reg1> != fp
1767                <reg1> != sp
1768   effects: cfa.reg = <reg1>
1769            cfa_temp.reg = <reg1>
1770            cfa_temp.offset = cfa.offset
1771
1772   Rule 5:
1773   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1774   constraints: <reg1> != fp
1775                <reg1> != sp
1776   effects: cfa_store.reg = <reg1>
1777            cfa_store.offset = cfa.offset - cfa_temp.offset
1778
1779   Rule 6:
1780   (set <reg> <const_int>)
1781   effects: cfa_temp.reg = <reg>
1782            cfa_temp.offset = <const_int>
1783
1784   Rule 7:
1785   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1786   effects: cfa_temp.reg = <reg1>
1787            cfa_temp.offset |= <const_int>
1788
1789   Rule 8:
1790   (set <reg> (high <exp>))
1791   effects: none
1792
1793   Rule 9:
1794   (set <reg> (lo_sum <exp> <const_int>))
1795   effects: cfa_temp.reg = <reg>
1796            cfa_temp.offset = <const_int>
1797
1798   Rule 10:
1799   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1800   effects: cfa_store.offset -= <const_int>
1801            cfa.offset = cfa_store.offset if cfa.reg == sp
1802            cfa.reg = sp
1803            cfa.base_offset = -cfa_store.offset
1804
1805   Rule 11:
1806   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1807   effects: cfa_store.offset += -/+ mode_size(mem)
1808            cfa.offset = cfa_store.offset if cfa.reg == sp
1809            cfa.reg = sp
1810            cfa.base_offset = -cfa_store.offset
1811
1812   Rule 12:
1813   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1814
1815        <reg2>)
1816   effects: cfa.reg = <reg1>
1817            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1818
1819   Rule 13:
1820   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1821   effects: cfa.reg = <reg1>
1822            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1823
1824   Rule 14:
1825   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1826   effects: cfa.reg = <reg1>
1827            cfa.base_offset = -cfa_temp.offset
1828            cfa_temp.offset -= mode_size(mem)
1829
1830   Rule 15:
1831   (set <reg> {unspec, unspec_volatile})
1832   effects: target-dependent
1833
1834   Rule 16:
1835   (set sp (and: sp <const_int>))
1836   constraints: cfa_store.reg == sp
1837   effects: current_fde.stack_realign = 1
1838            cfa_store.offset = 0
1839            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1840
1841   Rule 17:
1842   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1843   effects: cfa_store.offset += -/+ mode_size(mem)
1844
1845   Rule 18:
1846   (set (mem ({pre_inc, pre_dec} sp)) fp)
1847   constraints: fde->stack_realign == 1
1848   effects: cfa_store.offset = 0
1849            cfa.reg != HARD_FRAME_POINTER_REGNUM
1850
1851   Rule 19:
1852   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1853   constraints: fde->stack_realign == 1
1854                && cfa.offset == 0
1855                && cfa.indirect == 0
1856                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1857   effects: Use DW_CFA_def_cfa_expression to define cfa
1858            cfa.reg == fde->drap_reg
1859
1860   Rule 20:
1861   (set reg fde->drap_reg)
1862   constraints: fde->vdrap_reg == INVALID_REGNUM
1863   effects: fde->vdrap_reg = reg.
1864   (set mem fde->drap_reg)
1865   constraints: fde->drap_reg_saved == 1
1866   effects: none.  */
1867
1868 static void
1869 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1870 {
1871   rtx src, dest, span;
1872   HOST_WIDE_INT offset;
1873   dw_fde_ref fde;
1874
1875   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1876      the PARALLEL independently. The first element is always processed if
1877      it is a SET. This is for backward compatibility.   Other elements
1878      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1879      flag is set in them.  */
1880   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1881     {
1882       int par_index;
1883       int limit = XVECLEN (expr, 0);
1884       rtx elem;
1885
1886       /* PARALLELs have strict read-modify-write semantics, so we
1887          ought to evaluate every rvalue before changing any lvalue.
1888          It's cumbersome to do that in general, but there's an
1889          easy approximation that is enough for all current users:
1890          handle register saves before register assignments.  */
1891       if (GET_CODE (expr) == PARALLEL)
1892         for (par_index = 0; par_index < limit; par_index++)
1893           {
1894             elem = XVECEXP (expr, 0, par_index);
1895             if (GET_CODE (elem) == SET
1896                 && MEM_P (SET_DEST (elem))
1897                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1898               dwarf2out_frame_debug_expr (elem, label);
1899           }
1900
1901       for (par_index = 0; par_index < limit; par_index++)
1902         {
1903           elem = XVECEXP (expr, 0, par_index);
1904           if (GET_CODE (elem) == SET
1905               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1906               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1907             dwarf2out_frame_debug_expr (elem, label);
1908           else if (GET_CODE (elem) == SET
1909                    && par_index != 0
1910                    && !RTX_FRAME_RELATED_P (elem))
1911             {
1912               /* Stack adjustment combining might combine some post-prologue
1913                  stack adjustment into a prologue stack adjustment.  */
1914               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1915
1916               if (offset != 0)
1917                 dwarf2out_args_size_adjust (offset, label);
1918             }
1919         }
1920       return;
1921     }
1922
1923   gcc_assert (GET_CODE (expr) == SET);
1924
1925   src = SET_SRC (expr);
1926   dest = SET_DEST (expr);
1927
1928   if (REG_P (src))
1929     {
1930       rtx rsi = reg_saved_in (src);
1931       if (rsi)
1932         src = rsi;
1933     }
1934
1935   fde = current_fde ();
1936
1937   if (GET_CODE (src) == REG
1938       && fde
1939       && fde->drap_reg == REGNO (src)
1940       && (fde->drap_reg_saved
1941           || GET_CODE (dest) == REG))
1942     {
1943       /* Rule 20 */
1944       /* If we are saving dynamic realign argument pointer to a
1945          register, the destination is virtual dynamic realign
1946          argument pointer.  It may be used to access argument.  */
1947       if (GET_CODE (dest) == REG)
1948         {
1949           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1950           fde->vdrap_reg = REGNO (dest);
1951         }
1952       return;
1953     }
1954
1955   switch (GET_CODE (dest))
1956     {
1957     case REG:
1958       switch (GET_CODE (src))
1959         {
1960           /* Setting FP from SP.  */
1961         case REG:
1962           if (cfa.reg == (unsigned) REGNO (src))
1963             {
1964               /* Rule 1 */
1965               /* Update the CFA rule wrt SP or FP.  Make sure src is
1966                  relative to the current CFA register.
1967
1968                  We used to require that dest be either SP or FP, but the
1969                  ARM copies SP to a temporary register, and from there to
1970                  FP.  So we just rely on the backends to only set
1971                  RTX_FRAME_RELATED_P on appropriate insns.  */
1972               cfa.reg = REGNO (dest);
1973               cfa_temp.reg = cfa.reg;
1974               cfa_temp.offset = cfa.offset;
1975             }
1976           else
1977             {
1978               /* Saving a register in a register.  */
1979               gcc_assert (!fixed_regs [REGNO (dest)]
1980                           /* For the SPARC and its register window.  */
1981                           || (DWARF_FRAME_REGNUM (REGNO (src))
1982                               == DWARF_FRAME_RETURN_COLUMN));
1983
1984               /* After stack is aligned, we can only save SP in FP
1985                  if drap register is used.  In this case, we have
1986                  to restore stack pointer with the CFA value and we
1987                  don't generate this DWARF information.  */
1988               if (fde
1989                   && fde->stack_realign
1990                   && REGNO (src) == STACK_POINTER_REGNUM)
1991                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1992                             && fde->drap_reg != INVALID_REGNUM
1993                             && cfa.reg != REGNO (src));
1994               else
1995                 queue_reg_save (label, src, dest, 0);
1996             }
1997           break;
1998
1999         case PLUS:
2000         case MINUS:
2001         case LO_SUM:
2002           if (dest == stack_pointer_rtx)
2003             {
2004               /* Rule 2 */
2005               /* Adjusting SP.  */
2006               switch (GET_CODE (XEXP (src, 1)))
2007                 {
2008                 case CONST_INT:
2009                   offset = INTVAL (XEXP (src, 1));
2010                   break;
2011                 case REG:
2012                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2013                               == cfa_temp.reg);
2014                   offset = cfa_temp.offset;
2015                   break;
2016                 default:
2017                   gcc_unreachable ();
2018                 }
2019
2020               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2021                 {
2022                   /* Restoring SP from FP in the epilogue.  */
2023                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2024                   cfa.reg = STACK_POINTER_REGNUM;
2025                 }
2026               else if (GET_CODE (src) == LO_SUM)
2027                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2028                 ;
2029               else
2030                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2031
2032               if (GET_CODE (src) != MINUS)
2033                 offset = -offset;
2034               if (cfa.reg == STACK_POINTER_REGNUM)
2035                 cfa.offset += offset;
2036               if (cfa_store.reg == STACK_POINTER_REGNUM)
2037                 cfa_store.offset += offset;
2038             }
2039           else if (dest == hard_frame_pointer_rtx)
2040             {
2041               /* Rule 3 */
2042               /* Either setting the FP from an offset of the SP,
2043                  or adjusting the FP */
2044               gcc_assert (frame_pointer_needed);
2045
2046               gcc_assert (REG_P (XEXP (src, 0))
2047                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2048                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2049               offset = INTVAL (XEXP (src, 1));
2050               if (GET_CODE (src) != MINUS)
2051                 offset = -offset;
2052               cfa.offset += offset;
2053               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2054             }
2055           else
2056             {
2057               gcc_assert (GET_CODE (src) != MINUS);
2058
2059               /* Rule 4 */
2060               if (REG_P (XEXP (src, 0))
2061                   && REGNO (XEXP (src, 0)) == cfa.reg
2062                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2063                 {
2064                   /* Setting a temporary CFA register that will be copied
2065                      into the FP later on.  */
2066                   offset = - INTVAL (XEXP (src, 1));
2067                   cfa.offset += offset;
2068                   cfa.reg = REGNO (dest);
2069                   /* Or used to save regs to the stack.  */
2070                   cfa_temp.reg = cfa.reg;
2071                   cfa_temp.offset = cfa.offset;
2072                 }
2073
2074               /* Rule 5 */
2075               else if (REG_P (XEXP (src, 0))
2076                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2077                        && XEXP (src, 1) == stack_pointer_rtx)
2078                 {
2079                   /* Setting a scratch register that we will use instead
2080                      of SP for saving registers to the stack.  */
2081                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2082                   cfa_store.reg = REGNO (dest);
2083                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2084                 }
2085
2086               /* Rule 9 */
2087               else if (GET_CODE (src) == LO_SUM
2088                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2089                 {
2090                   cfa_temp.reg = REGNO (dest);
2091                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2092                 }
2093               else
2094                 gcc_unreachable ();
2095             }
2096           break;
2097
2098           /* Rule 6 */
2099         case CONST_INT:
2100           cfa_temp.reg = REGNO (dest);
2101           cfa_temp.offset = INTVAL (src);
2102           break;
2103
2104           /* Rule 7 */
2105         case IOR:
2106           gcc_assert (REG_P (XEXP (src, 0))
2107                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2108                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2109
2110           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2111             cfa_temp.reg = REGNO (dest);
2112           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2113           break;
2114
2115           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2116              which will fill in all of the bits.  */
2117           /* Rule 8 */
2118         case HIGH:
2119           break;
2120
2121           /* Rule 15 */
2122         case UNSPEC:
2123         case UNSPEC_VOLATILE:
2124           gcc_assert (targetm.dwarf_handle_frame_unspec);
2125           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2126           return;
2127
2128           /* Rule 16 */
2129         case AND:
2130           /* If this AND operation happens on stack pointer in prologue,
2131              we assume the stack is realigned and we extract the
2132              alignment.  */
2133           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2134             {
2135               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2136               fde->stack_realign = 1;
2137               fde->stack_realignment = INTVAL (XEXP (src, 1));
2138               cfa_store.offset = 0;
2139
2140               if (cfa.reg != STACK_POINTER_REGNUM
2141                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2142                 fde->drap_reg = cfa.reg;
2143             }
2144           return;
2145
2146         default:
2147           gcc_unreachable ();
2148         }
2149
2150       def_cfa_1 (label, &cfa);
2151       break;
2152
2153     case MEM:
2154
2155       /* Saving a register to the stack.  Make sure dest is relative to the
2156          CFA register.  */
2157       switch (GET_CODE (XEXP (dest, 0)))
2158         {
2159           /* Rule 10 */
2160           /* With a push.  */
2161         case PRE_MODIFY:
2162           /* We can't handle variable size modifications.  */
2163           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2164                       == CONST_INT);
2165           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2166
2167           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2168                       && cfa_store.reg == STACK_POINTER_REGNUM);
2169
2170           cfa_store.offset += offset;
2171           if (cfa.reg == STACK_POINTER_REGNUM)
2172             cfa.offset = cfa_store.offset;
2173
2174           offset = -cfa_store.offset;
2175           break;
2176
2177           /* Rule 11 */
2178         case PRE_INC:
2179         case PRE_DEC:
2180           offset = GET_MODE_SIZE (GET_MODE (dest));
2181           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2182             offset = -offset;
2183
2184           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2185                        == STACK_POINTER_REGNUM)
2186                       && cfa_store.reg == STACK_POINTER_REGNUM);
2187
2188           cfa_store.offset += offset;
2189
2190           /* Rule 18: If stack is aligned, we will use FP as a
2191              reference to represent the address of the stored
2192              regiser.  */
2193           if (fde
2194               && fde->stack_realign
2195               && src == hard_frame_pointer_rtx)
2196             {
2197               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2198               cfa_store.offset = 0;
2199             }
2200
2201           if (cfa.reg == STACK_POINTER_REGNUM)
2202             cfa.offset = cfa_store.offset;
2203
2204           offset = -cfa_store.offset;
2205           break;
2206
2207           /* Rule 12 */
2208           /* With an offset.  */
2209         case PLUS:
2210         case MINUS:
2211         case LO_SUM:
2212           {
2213             int regno;
2214
2215             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2216                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2217             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2218             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2219               offset = -offset;
2220
2221             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2222
2223             if (cfa_store.reg == (unsigned) regno)
2224               offset -= cfa_store.offset;
2225             else
2226               {
2227                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2228                 offset -= cfa_temp.offset;
2229               }
2230           }
2231           break;
2232
2233           /* Rule 13 */
2234           /* Without an offset.  */
2235         case REG:
2236           {
2237             int regno = REGNO (XEXP (dest, 0));
2238
2239             if (cfa_store.reg == (unsigned) regno)
2240               offset = -cfa_store.offset;
2241             else
2242               {
2243                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2244                 offset = -cfa_temp.offset;
2245               }
2246           }
2247           break;
2248
2249           /* Rule 14 */
2250         case POST_INC:
2251           gcc_assert (cfa_temp.reg
2252                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2253           offset = -cfa_temp.offset;
2254           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2255           break;
2256
2257         default:
2258           gcc_unreachable ();
2259         }
2260
2261         /* Rule 17 */
2262         /* If the source operand of this MEM operation is not a
2263            register, basically the source is return address.  Here
2264            we only care how much stack grew and we don't save it.  */
2265       if (!REG_P (src))
2266         break;
2267
2268       if (REGNO (src) != STACK_POINTER_REGNUM
2269           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2270           && (unsigned) REGNO (src) == cfa.reg)
2271         {
2272           /* We're storing the current CFA reg into the stack.  */
2273
2274           if (cfa.offset == 0)
2275             {
2276               /* Rule 19 */
2277               /* If stack is aligned, putting CFA reg into stack means
2278                  we can no longer use reg + offset to represent CFA.
2279                  Here we use DW_CFA_def_cfa_expression instead.  The
2280                  result of this expression equals to the original CFA
2281                  value.  */
2282               if (fde
2283                   && fde->stack_realign
2284                   && cfa.indirect == 0
2285                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2286                 {
2287                   dw_cfa_location cfa_exp;
2288
2289                   gcc_assert (fde->drap_reg == cfa.reg);
2290
2291                   cfa_exp.indirect = 1;
2292                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2293                   cfa_exp.base_offset = offset;
2294                   cfa_exp.offset = 0;
2295
2296                   fde->drap_reg_saved = 1;
2297
2298                   def_cfa_1 (label, &cfa_exp);
2299                   break;
2300                 }
2301
2302               /* If the source register is exactly the CFA, assume
2303                  we're saving SP like any other register; this happens
2304                  on the ARM.  */
2305               def_cfa_1 (label, &cfa);
2306               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2307               break;
2308             }
2309           else
2310             {
2311               /* Otherwise, we'll need to look in the stack to
2312                  calculate the CFA.  */
2313               rtx x = XEXP (dest, 0);
2314
2315               if (!REG_P (x))
2316                 x = XEXP (x, 0);
2317               gcc_assert (REG_P (x));
2318
2319               cfa.reg = REGNO (x);
2320               cfa.base_offset = offset;
2321               cfa.indirect = 1;
2322               def_cfa_1 (label, &cfa);
2323               break;
2324             }
2325         }
2326
2327       def_cfa_1 (label, &cfa);
2328       {
2329         span = targetm.dwarf_register_span (src);
2330
2331         if (!span)
2332           queue_reg_save (label, src, NULL_RTX, offset);
2333         else
2334           {
2335             /* We have a PARALLEL describing where the contents of SRC
2336                live.  Queue register saves for each piece of the
2337                PARALLEL.  */
2338             int par_index;
2339             int limit;
2340             HOST_WIDE_INT span_offset = offset;
2341
2342             gcc_assert (GET_CODE (span) == PARALLEL);
2343
2344             limit = XVECLEN (span, 0);
2345             for (par_index = 0; par_index < limit; par_index++)
2346               {
2347                 rtx elem = XVECEXP (span, 0, par_index);
2348
2349                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2350                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2351               }
2352           }
2353       }
2354       break;
2355
2356     default:
2357       gcc_unreachable ();
2358     }
2359 }
2360
2361 /* Record call frame debugging information for INSN, which either
2362    sets SP or FP (adjusting how we calculate the frame address) or saves a
2363    register to the stack.  If INSN is NULL_RTX, initialize our state.
2364
2365    If AFTER_P is false, we're being called before the insn is emitted,
2366    otherwise after.  Call instructions get invoked twice.  */
2367
2368 void
2369 dwarf2out_frame_debug (rtx insn, bool after_p)
2370 {
2371   const char *label;
2372   rtx src;
2373
2374   if (insn == NULL_RTX)
2375     {
2376       size_t i;
2377
2378       /* Flush any queued register saves.  */
2379       flush_queued_reg_saves ();
2380
2381       /* Set up state for generating call frame debug info.  */
2382       lookup_cfa (&cfa);
2383       gcc_assert (cfa.reg
2384                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2385
2386       cfa.reg = STACK_POINTER_REGNUM;
2387       cfa_store = cfa;
2388       cfa_temp.reg = -1;
2389       cfa_temp.offset = 0;
2390
2391       for (i = 0; i < num_regs_saved_in_regs; i++)
2392         {
2393           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2394           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2395         }
2396       num_regs_saved_in_regs = 0;
2397
2398       if (barrier_args_size)
2399         {
2400           XDELETEVEC (barrier_args_size);
2401           barrier_args_size = NULL;
2402         }
2403       return;
2404     }
2405
2406   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2407     flush_queued_reg_saves ();
2408
2409   if (! RTX_FRAME_RELATED_P (insn))
2410     {
2411       if (!ACCUMULATE_OUTGOING_ARGS)
2412         dwarf2out_stack_adjust (insn, after_p);
2413       return;
2414     }
2415
2416   label = dwarf2out_cfi_label ();
2417   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2418   if (src)
2419     insn = XEXP (src, 0);
2420   else
2421     insn = PATTERN (insn);
2422
2423   dwarf2out_frame_debug_expr (insn, label);
2424 }
2425
2426 #endif
2427
2428 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2429 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2430  (enum dwarf_call_frame_info cfi);
2431
2432 static enum dw_cfi_oprnd_type
2433 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2434 {
2435   switch (cfi)
2436     {
2437     case DW_CFA_nop:
2438     case DW_CFA_GNU_window_save:
2439       return dw_cfi_oprnd_unused;
2440
2441     case DW_CFA_set_loc:
2442     case DW_CFA_advance_loc1:
2443     case DW_CFA_advance_loc2:
2444     case DW_CFA_advance_loc4:
2445     case DW_CFA_MIPS_advance_loc8:
2446       return dw_cfi_oprnd_addr;
2447
2448     case DW_CFA_offset:
2449     case DW_CFA_offset_extended:
2450     case DW_CFA_def_cfa:
2451     case DW_CFA_offset_extended_sf:
2452     case DW_CFA_def_cfa_sf:
2453     case DW_CFA_restore_extended:
2454     case DW_CFA_undefined:
2455     case DW_CFA_same_value:
2456     case DW_CFA_def_cfa_register:
2457     case DW_CFA_register:
2458       return dw_cfi_oprnd_reg_num;
2459
2460     case DW_CFA_def_cfa_offset:
2461     case DW_CFA_GNU_args_size:
2462     case DW_CFA_def_cfa_offset_sf:
2463       return dw_cfi_oprnd_offset;
2464
2465     case DW_CFA_def_cfa_expression:
2466     case DW_CFA_expression:
2467       return dw_cfi_oprnd_loc;
2468
2469     default:
2470       gcc_unreachable ();
2471     }
2472 }
2473
2474 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2475 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2476  (enum dwarf_call_frame_info cfi);
2477
2478 static enum dw_cfi_oprnd_type
2479 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2480 {
2481   switch (cfi)
2482     {
2483     case DW_CFA_def_cfa:
2484     case DW_CFA_def_cfa_sf:
2485     case DW_CFA_offset:
2486     case DW_CFA_offset_extended_sf:
2487     case DW_CFA_offset_extended:
2488       return dw_cfi_oprnd_offset;
2489
2490     case DW_CFA_register:
2491       return dw_cfi_oprnd_reg_num;
2492
2493     default:
2494       return dw_cfi_oprnd_unused;
2495     }
2496 }
2497
2498 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2499
2500 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2501    switch to the data section instead, and write out a synthetic label
2502    for collect2.  */
2503
2504 static void
2505 switch_to_eh_frame_section (void)
2506 {
2507   tree label;
2508
2509 #ifdef EH_FRAME_SECTION_NAME
2510   if (eh_frame_section == 0)
2511     {
2512       int flags;
2513
2514       if (EH_TABLES_CAN_BE_READ_ONLY)
2515         {
2516           int fde_encoding;
2517           int per_encoding;
2518           int lsda_encoding;
2519
2520           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2521                                                        /*global=*/0);
2522           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2523                                                        /*global=*/1);
2524           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2525                                                         /*global=*/0);
2526           flags = ((! flag_pic
2527                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2528                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2529                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2530                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2531                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2532                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2533                    ? 0 : SECTION_WRITE);
2534         }
2535       else
2536         flags = SECTION_WRITE;
2537       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2538     }
2539 #endif
2540
2541   if (eh_frame_section)
2542     switch_to_section (eh_frame_section);
2543   else
2544     {
2545       /* We have no special eh_frame section.  Put the information in
2546          the data section and emit special labels to guide collect2.  */
2547       switch_to_section (data_section);
2548       label = get_file_function_name ("F");
2549       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2550       targetm.asm_out.globalize_label (asm_out_file,
2551                                        IDENTIFIER_POINTER (label));
2552       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2553     }
2554 }
2555
2556 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2557
2558 static HOST_WIDE_INT
2559 div_data_align (HOST_WIDE_INT off)
2560 {
2561   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2562   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2563   return r;
2564 }
2565
2566 /* Output a Call Frame Information opcode and its operand(s).  */
2567
2568 static void
2569 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2570 {
2571   unsigned long r;
2572   HOST_WIDE_INT off;
2573
2574   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2575     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2576                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2577                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2578                          ((unsigned HOST_WIDE_INT)
2579                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2580   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2581     {
2582       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2583       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2584                            "DW_CFA_offset, column 0x%lx", r);
2585       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2586       dw2_asm_output_data_uleb128 (off, NULL);
2587     }
2588   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2589     {
2590       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2591       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2592                            "DW_CFA_restore, column 0x%lx", r);
2593     }
2594   else
2595     {
2596       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2597                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2598
2599       switch (cfi->dw_cfi_opc)
2600         {
2601         case DW_CFA_set_loc:
2602           if (for_eh)
2603             dw2_asm_output_encoded_addr_rtx (
2604                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2605                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2606                 false, NULL);
2607           else
2608             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2609                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2610           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2611           break;
2612
2613         case DW_CFA_advance_loc1:
2614           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2615                                 fde->dw_fde_current_label, NULL);
2616           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2617           break;
2618
2619         case DW_CFA_advance_loc2:
2620           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2621                                 fde->dw_fde_current_label, NULL);
2622           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2623           break;
2624
2625         case DW_CFA_advance_loc4:
2626           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2627                                 fde->dw_fde_current_label, NULL);
2628           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2629           break;
2630
2631         case DW_CFA_MIPS_advance_loc8:
2632           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2633                                 fde->dw_fde_current_label, NULL);
2634           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2635           break;
2636
2637         case DW_CFA_offset_extended:
2638           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2639           dw2_asm_output_data_uleb128 (r, NULL);
2640           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2641           dw2_asm_output_data_uleb128 (off, NULL);
2642           break;
2643
2644         case DW_CFA_def_cfa:
2645           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2646           dw2_asm_output_data_uleb128 (r, NULL);
2647           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2648           break;
2649
2650         case DW_CFA_offset_extended_sf:
2651           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2652           dw2_asm_output_data_uleb128 (r, NULL);
2653           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2654           dw2_asm_output_data_sleb128 (off, NULL);
2655           break;
2656
2657         case DW_CFA_def_cfa_sf:
2658           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2659           dw2_asm_output_data_uleb128 (r, NULL);
2660           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2661           dw2_asm_output_data_sleb128 (off, NULL);
2662           break;
2663
2664         case DW_CFA_restore_extended:
2665         case DW_CFA_undefined:
2666         case DW_CFA_same_value:
2667         case DW_CFA_def_cfa_register:
2668           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2669           dw2_asm_output_data_uleb128 (r, NULL);
2670           break;
2671
2672         case DW_CFA_register:
2673           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2674           dw2_asm_output_data_uleb128 (r, NULL);
2675           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2676           dw2_asm_output_data_uleb128 (r, NULL);
2677           break;
2678
2679         case DW_CFA_def_cfa_offset:
2680         case DW_CFA_GNU_args_size:
2681           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2682           break;
2683
2684         case DW_CFA_def_cfa_offset_sf:
2685           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2686           dw2_asm_output_data_sleb128 (off, NULL);
2687           break;
2688
2689         case DW_CFA_GNU_window_save:
2690           break;
2691
2692         case DW_CFA_def_cfa_expression:
2693         case DW_CFA_expression:
2694           output_cfa_loc (cfi);
2695           break;
2696
2697         case DW_CFA_GNU_negative_offset_extended:
2698           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2699           gcc_unreachable ();
2700
2701         default:
2702           break;
2703         }
2704     }
2705 }
2706
2707 /* Similar, but do it via assembler directives instead.  */
2708
2709 static void
2710 output_cfi_directive (dw_cfi_ref cfi)
2711 {
2712   unsigned long r, r2;
2713
2714   switch (cfi->dw_cfi_opc)
2715     {
2716     case DW_CFA_advance_loc:
2717     case DW_CFA_advance_loc1:
2718     case DW_CFA_advance_loc2:
2719     case DW_CFA_advance_loc4:
2720     case DW_CFA_MIPS_advance_loc8:
2721     case DW_CFA_set_loc:
2722       /* Should only be created by add_fde_cfi in a code path not
2723          followed when emitting via directives.  The assembler is
2724          going to take care of this for us.  */
2725       gcc_unreachable ();
2726
2727     case DW_CFA_offset:
2728     case DW_CFA_offset_extended:
2729     case DW_CFA_offset_extended_sf:
2730       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2731       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2732                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2733       break;
2734
2735     case DW_CFA_restore:
2736     case DW_CFA_restore_extended:
2737       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2738       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2739       break;
2740
2741     case DW_CFA_undefined:
2742       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2743       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2744       break;
2745
2746     case DW_CFA_same_value:
2747       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2748       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2749       break;
2750
2751     case DW_CFA_def_cfa:
2752     case DW_CFA_def_cfa_sf:
2753       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2754       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2755                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2756       break;
2757
2758     case DW_CFA_def_cfa_register:
2759       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2760       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2761       break;
2762
2763     case DW_CFA_register:
2764       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2765       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2766       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2767       break;
2768
2769     case DW_CFA_def_cfa_offset:
2770     case DW_CFA_def_cfa_offset_sf:
2771       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2772                HOST_WIDE_INT_PRINT_DEC"\n",
2773                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2774       break;
2775
2776     case DW_CFA_GNU_args_size:
2777       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2778       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2779       if (flag_debug_asm)
2780         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2781                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2782       fputc ('\n', asm_out_file);
2783       break;
2784
2785     case DW_CFA_GNU_window_save:
2786       fprintf (asm_out_file, "\t.cfi_window_save\n");
2787       break;
2788
2789     case DW_CFA_def_cfa_expression:
2790     case DW_CFA_expression:
2791       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2792       output_cfa_loc_raw (cfi);
2793       fputc ('\n', asm_out_file);
2794       break;
2795
2796     default:
2797       gcc_unreachable ();
2798     }
2799 }
2800
2801 /* Output the call frame information used to record information
2802    that relates to calculating the frame pointer, and records the
2803    location of saved registers.  */
2804
2805 static void
2806 output_call_frame_info (int for_eh)
2807 {
2808   unsigned int i;
2809   dw_fde_ref fde;
2810   dw_cfi_ref cfi;
2811   char l1[20], l2[20], section_start_label[20];
2812   bool any_lsda_needed = false;
2813   char augmentation[6];
2814   int augmentation_size;
2815   int fde_encoding = DW_EH_PE_absptr;
2816   int per_encoding = DW_EH_PE_absptr;
2817   int lsda_encoding = DW_EH_PE_absptr;
2818   int return_reg;
2819
2820   /* Don't emit a CIE if there won't be any FDEs.  */
2821   if (fde_table_in_use == 0)
2822     return;
2823
2824   /* Nothing to do if the assembler's doing it all.  */
2825   if (dwarf2out_do_cfi_asm ())
2826     return;
2827
2828   /* If we make FDEs linkonce, we may have to emit an empty label for
2829      an FDE that wouldn't otherwise be emitted.  We want to avoid
2830      having an FDE kept around when the function it refers to is
2831      discarded.  Example where this matters: a primary function
2832      template in C++ requires EH information, but an explicit
2833      specialization doesn't.  */
2834   if (TARGET_USES_WEAK_UNWIND_INFO
2835       && ! flag_asynchronous_unwind_tables
2836       && flag_exceptions
2837       && for_eh)
2838     for (i = 0; i < fde_table_in_use; i++)
2839       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2840           && !fde_table[i].uses_eh_lsda
2841           && ! DECL_WEAK (fde_table[i].decl))
2842         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2843                                       for_eh, /* empty */ 1);
2844
2845   /* If we don't have any functions we'll want to unwind out of, don't
2846      emit any EH unwind information.  Note that if exceptions aren't
2847      enabled, we won't have collected nothrow information, and if we
2848      asked for asynchronous tables, we always want this info.  */
2849   if (for_eh)
2850     {
2851       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2852
2853       for (i = 0; i < fde_table_in_use; i++)
2854         if (fde_table[i].uses_eh_lsda)
2855           any_eh_needed = any_lsda_needed = true;
2856         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2857           any_eh_needed = true;
2858         else if (! fde_table[i].nothrow
2859                  && ! fde_table[i].all_throwers_are_sibcalls)
2860           any_eh_needed = true;
2861
2862       if (! any_eh_needed)
2863         return;
2864     }
2865
2866   /* We're going to be generating comments, so turn on app.  */
2867   if (flag_debug_asm)
2868     app_enable ();
2869
2870   if (for_eh)
2871     switch_to_eh_frame_section ();
2872   else
2873     {
2874       if (!debug_frame_section)
2875         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2876                                            SECTION_DEBUG, NULL);
2877       switch_to_section (debug_frame_section);
2878     }
2879
2880   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2881   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2882
2883   /* Output the CIE.  */
2884   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2885   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2886   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2887     dw2_asm_output_data (4, 0xffffffff,
2888       "Initial length escape value indicating 64-bit DWARF extension");
2889   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2890                         "Length of Common Information Entry");
2891   ASM_OUTPUT_LABEL (asm_out_file, l1);
2892
2893   /* Now that the CIE pointer is PC-relative for EH,
2894      use 0 to identify the CIE.  */
2895   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2896                        (for_eh ? 0 : DWARF_CIE_ID),
2897                        "CIE Identifier Tag");
2898
2899   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2900
2901   augmentation[0] = 0;
2902   augmentation_size = 0;
2903   if (for_eh)
2904     {
2905       char *p;
2906
2907       /* Augmentation:
2908          z      Indicates that a uleb128 is present to size the
2909                 augmentation section.
2910          L      Indicates the encoding (and thus presence) of
2911                 an LSDA pointer in the FDE augmentation.
2912          R      Indicates a non-default pointer encoding for
2913                 FDE code pointers.
2914          P      Indicates the presence of an encoding + language
2915                 personality routine in the CIE augmentation.  */
2916
2917       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2918       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2919       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2920
2921       p = augmentation + 1;
2922       if (eh_personality_libfunc)
2923         {
2924           *p++ = 'P';
2925           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2926           assemble_external_libcall (eh_personality_libfunc);
2927         }
2928       if (any_lsda_needed)
2929         {
2930           *p++ = 'L';
2931           augmentation_size += 1;
2932         }
2933       if (fde_encoding != DW_EH_PE_absptr)
2934         {
2935           *p++ = 'R';
2936           augmentation_size += 1;
2937         }
2938       if (p > augmentation + 1)
2939         {
2940           augmentation[0] = 'z';
2941           *p = '\0';
2942         }
2943
2944       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2945       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2946         {
2947           int offset = (  4             /* Length */
2948                         + 4             /* CIE Id */
2949                         + 1             /* CIE version */
2950                         + strlen (augmentation) + 1     /* Augmentation */
2951                         + size_of_uleb128 (1)           /* Code alignment */
2952                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2953                         + 1             /* RA column */
2954                         + 1             /* Augmentation size */
2955                         + 1             /* Personality encoding */ );
2956           int pad = -offset & (PTR_SIZE - 1);
2957
2958           augmentation_size += pad;
2959
2960           /* Augmentations should be small, so there's scarce need to
2961              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2962           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2963         }
2964     }
2965
2966   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2967   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2968   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2969                                "CIE Data Alignment Factor");
2970
2971   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2972   if (DW_CIE_VERSION == 1)
2973     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2974   else
2975     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2976
2977   if (augmentation[0])
2978     {
2979       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2980       if (eh_personality_libfunc)
2981         {
2982           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2983                                eh_data_format_name (per_encoding));
2984           dw2_asm_output_encoded_addr_rtx (per_encoding,
2985                                            eh_personality_libfunc,
2986                                            true, NULL);
2987         }
2988
2989       if (any_lsda_needed)
2990         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2991                              eh_data_format_name (lsda_encoding));
2992
2993       if (fde_encoding != DW_EH_PE_absptr)
2994         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2995                              eh_data_format_name (fde_encoding));
2996     }
2997
2998   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2999     output_cfi (cfi, NULL, for_eh);
3000
3001   /* Pad the CIE out to an address sized boundary.  */
3002   ASM_OUTPUT_ALIGN (asm_out_file,
3003                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3004   ASM_OUTPUT_LABEL (asm_out_file, l2);
3005
3006   /* Loop through all of the FDE's.  */
3007   for (i = 0; i < fde_table_in_use; i++)
3008     {
3009       fde = &fde_table[i];
3010
3011       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3012       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3013           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3014           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3015           && !fde->uses_eh_lsda)
3016         continue;
3017
3018       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3019       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3020       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3021       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3022       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3023         dw2_asm_output_data (4, 0xffffffff,
3024                              "Initial length escape value indicating 64-bit DWARF extension");
3025       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3026                             "FDE Length");
3027       ASM_OUTPUT_LABEL (asm_out_file, l1);
3028
3029       if (for_eh)
3030         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3031       else
3032         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3033                                debug_frame_section, "FDE CIE offset");
3034
3035       if (for_eh)
3036         {
3037           if (fde->dw_fde_switched_sections)
3038             {
3039               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3040                                       fde->dw_fde_unlikely_section_label);
3041               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3042                                       fde->dw_fde_hot_section_label);
3043               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3044               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3045               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3046                                                "FDE initial location");
3047               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3048                                     fde->dw_fde_hot_section_end_label,
3049                                     fde->dw_fde_hot_section_label,
3050                                     "FDE address range");
3051               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3052                                                "FDE initial location");
3053               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3054                                     fde->dw_fde_unlikely_section_end_label,
3055                                     fde->dw_fde_unlikely_section_label,
3056                                     "FDE address range");
3057             }
3058           else
3059             {
3060               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3061               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3062               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3063                                                sym_ref,
3064                                                false,
3065                                                "FDE initial location");
3066               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3067                                     fde->dw_fde_end, fde->dw_fde_begin,
3068                                     "FDE address range");
3069             }
3070         }
3071       else
3072         {
3073           if (fde->dw_fde_switched_sections)
3074             {
3075               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3076                                    fde->dw_fde_hot_section_label,
3077                                    "FDE initial location");
3078               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3079                                     fde->dw_fde_hot_section_end_label,
3080                                     fde->dw_fde_hot_section_label,
3081                                     "FDE address range");
3082               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3083                                    fde->dw_fde_unlikely_section_label,
3084                                    "FDE initial location");
3085               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3086                                     fde->dw_fde_unlikely_section_end_label,
3087                                     fde->dw_fde_unlikely_section_label,
3088                                     "FDE address range");
3089             }
3090           else
3091             {
3092               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3093                                    "FDE initial location");
3094               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3095                                     fde->dw_fde_end, fde->dw_fde_begin,
3096                                     "FDE address range");
3097             }
3098         }
3099
3100       if (augmentation[0])
3101         {
3102           if (any_lsda_needed)
3103             {
3104               int size = size_of_encoded_value (lsda_encoding);
3105
3106               if (lsda_encoding == DW_EH_PE_aligned)
3107                 {
3108                   int offset = (  4             /* Length */
3109                                 + 4             /* CIE offset */
3110                                 + 2 * size_of_encoded_value (fde_encoding)
3111                                 + 1             /* Augmentation size */ );
3112                   int pad = -offset & (PTR_SIZE - 1);
3113
3114                   size += pad;
3115                   gcc_assert (size_of_uleb128 (size) == 1);
3116                 }
3117
3118               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3119
3120               if (fde->uses_eh_lsda)
3121                 {
3122                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3123                                                fde->funcdef_number);
3124                   dw2_asm_output_encoded_addr_rtx (
3125                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3126                         false, "Language Specific Data Area");
3127                 }
3128               else
3129                 {
3130                   if (lsda_encoding == DW_EH_PE_aligned)
3131                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3132                   dw2_asm_output_data
3133                     (size_of_encoded_value (lsda_encoding), 0,
3134                      "Language Specific Data Area (none)");
3135                 }
3136             }
3137           else
3138             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3139         }
3140
3141       /* Loop through the Call Frame Instructions associated with
3142          this FDE.  */
3143       fde->dw_fde_current_label = fde->dw_fde_begin;
3144       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3145         output_cfi (cfi, fde, for_eh);
3146
3147       /* Pad the FDE out to an address sized boundary.  */
3148       ASM_OUTPUT_ALIGN (asm_out_file,
3149                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3150       ASM_OUTPUT_LABEL (asm_out_file, l2);
3151     }
3152
3153   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3154     dw2_asm_output_data (4, 0, "End of Table");
3155 #ifdef MIPS_DEBUGGING_INFO
3156   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3157      get a value of 0.  Putting .align 0 after the label fixes it.  */
3158   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3159 #endif
3160
3161   /* Turn off app to make assembly quicker.  */
3162   if (flag_debug_asm)
3163     app_disable ();
3164 }
3165
3166 /* Output a marker (i.e. a label) for the beginning of a function, before
3167    the prologue.  */
3168
3169 void
3170 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3171                           const char *file ATTRIBUTE_UNUSED)
3172 {
3173   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3174   char * dup_label;
3175   dw_fde_ref fde;
3176
3177   current_function_func_begin_label = NULL;
3178
3179 #ifdef TARGET_UNWIND_INFO
3180   /* ??? current_function_func_begin_label is also used by except.c
3181      for call-site information.  We must emit this label if it might
3182      be used.  */
3183   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3184       && ! dwarf2out_do_frame ())
3185     return;
3186 #else
3187   if (! dwarf2out_do_frame ())
3188     return;
3189 #endif
3190
3191   switch_to_section (function_section (current_function_decl));
3192   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3193                                current_function_funcdef_no);
3194   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3195                           current_function_funcdef_no);
3196   dup_label = xstrdup (label);
3197   current_function_func_begin_label = dup_label;
3198
3199 #ifdef TARGET_UNWIND_INFO
3200   /* We can elide the fde allocation if we're not emitting debug info.  */
3201   if (! dwarf2out_do_frame ())
3202     return;
3203 #endif
3204
3205   /* Expand the fde table if necessary.  */
3206   if (fde_table_in_use == fde_table_allocated)
3207     {
3208       fde_table_allocated += FDE_TABLE_INCREMENT;
3209       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3210       memset (fde_table + fde_table_in_use, 0,
3211               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3212     }
3213
3214   /* Record the FDE associated with this function.  */
3215   current_funcdef_fde = fde_table_in_use;
3216
3217   /* Add the new FDE at the end of the fde_table.  */
3218   fde = &fde_table[fde_table_in_use++];
3219   fde->decl = current_function_decl;
3220   fde->dw_fde_begin = dup_label;
3221   fde->dw_fde_current_label = dup_label;
3222   fde->dw_fde_hot_section_label = NULL;
3223   fde->dw_fde_hot_section_end_label = NULL;
3224   fde->dw_fde_unlikely_section_label = NULL;
3225   fde->dw_fde_unlikely_section_end_label = NULL;
3226   fde->dw_fde_switched_sections = false;
3227   fde->dw_fde_end = NULL;
3228   fde->dw_fde_cfi = NULL;
3229   fde->funcdef_number = current_function_funcdef_no;
3230   fde->nothrow = TREE_NOTHROW (current_function_decl);
3231   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3232   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3233   fde->drap_reg = INVALID_REGNUM;
3234   fde->vdrap_reg = INVALID_REGNUM;
3235
3236   args_size = old_args_size = 0;
3237
3238   /* We only want to output line number information for the genuine dwarf2
3239      prologue case, not the eh frame case.  */
3240 #ifdef DWARF2_DEBUGGING_INFO
3241   if (file)
3242     dwarf2out_source_line (line, file);
3243 #endif
3244
3245   if (dwarf2out_do_cfi_asm ())
3246     {
3247       int enc;
3248       rtx ref;
3249
3250       fprintf (asm_out_file, "\t.cfi_startproc\n");
3251
3252       if (eh_personality_libfunc)
3253         {
3254           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3255           ref = eh_personality_libfunc;
3256
3257           /* ??? The GAS support isn't entirely consistent.  We have to
3258              handle indirect support ourselves, but PC-relative is done
3259              in the assembler.  Further, the assembler can't handle any
3260              of the weirder relocation types.  */
3261           if (enc & DW_EH_PE_indirect)
3262             ref = dw2_force_const_mem (ref, true);
3263
3264           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3265           output_addr_const (asm_out_file, ref);
3266           fputc ('\n', asm_out_file);
3267         }
3268
3269       if (crtl->uses_eh_lsda)
3270         {
3271           char lab[20];
3272
3273           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3274           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3275                                        current_function_funcdef_no);
3276           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3277           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3278
3279           if (enc & DW_EH_PE_indirect)
3280             ref = dw2_force_const_mem (ref, true);
3281
3282           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3283           output_addr_const (asm_out_file, ref);
3284           fputc ('\n', asm_out_file);
3285         }
3286     }
3287 }
3288
3289 /* Output a marker (i.e. a label) for the absolute end of the generated code
3290    for a function definition.  This gets called *after* the epilogue code has
3291    been generated.  */
3292
3293 void
3294 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3295                         const char *file ATTRIBUTE_UNUSED)
3296 {
3297   dw_fde_ref fde;
3298   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3299
3300   if (dwarf2out_do_cfi_asm ())
3301     fprintf (asm_out_file, "\t.cfi_endproc\n");
3302
3303   /* Output a label to mark the endpoint of the code generated for this
3304      function.  */
3305   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3306                                current_function_funcdef_no);
3307   ASM_OUTPUT_LABEL (asm_out_file, label);
3308   fde = current_fde ();
3309   gcc_assert (fde != NULL);
3310   fde->dw_fde_end = xstrdup (label);
3311 }
3312
3313 void
3314 dwarf2out_frame_init (void)
3315 {
3316   /* Allocate the initial hunk of the fde_table.  */
3317   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3318   fde_table_allocated = FDE_TABLE_INCREMENT;
3319   fde_table_in_use = 0;
3320
3321   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3322      sake of lookup_cfa.  */
3323
3324   /* On entry, the Canonical Frame Address is at SP.  */
3325   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3326
3327 #ifdef DWARF2_UNWIND_INFO
3328   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3329     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3330 #endif
3331 }
3332
3333 void
3334 dwarf2out_frame_finish (void)
3335 {
3336   /* Output call frame information.  */
3337   if (DWARF2_FRAME_INFO)
3338     output_call_frame_info (0);
3339
3340 #ifndef TARGET_UNWIND_INFO
3341   /* Output another copy for the unwinder.  */
3342   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3343     output_call_frame_info (1);
3344 #endif
3345 }
3346
3347 /* Note that the current function section is being used for code.  */
3348
3349 static void
3350 dwarf2out_note_section_used (void)
3351 {
3352   section *sec = current_function_section ();
3353   if (sec == text_section)
3354     text_section_used = true;
3355   else if (sec == cold_text_section)
3356     cold_text_section_used = true;
3357 }
3358
3359 void
3360 dwarf2out_switch_text_section (void)
3361 {
3362   dw_fde_ref fde = current_fde ();
3363
3364   gcc_assert (cfun && fde);
3365
3366   fde->dw_fde_switched_sections = true;
3367   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3368   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3369   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3370   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3371   have_multiple_function_sections = true;
3372
3373   /* Reset the current label on switching text sections, so that we
3374      don't attempt to advance_loc4 between labels in different sections.  */
3375   fde->dw_fde_current_label = NULL;
3376
3377   /* There is no need to mark used sections when not debugging.  */
3378   if (cold_text_section != NULL)
3379     dwarf2out_note_section_used ();
3380 }
3381 #endif
3382 \f
3383 /* And now, the subset of the debugging information support code necessary
3384    for emitting location expressions.  */
3385
3386 /* Data about a single source file.  */
3387 struct dwarf_file_data GTY(())
3388 {
3389   const char * filename;
3390   int emitted_number;
3391 };
3392
3393 /* We need some way to distinguish DW_OP_addr with a direct symbol
3394    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3395 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3396
3397
3398 typedef struct dw_val_struct *dw_val_ref;
3399 typedef struct die_struct *dw_die_ref;
3400 typedef const struct die_struct *const_dw_die_ref;
3401 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3402 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3403
3404 /* Each DIE may have a series of attribute/value pairs.  Values
3405    can take on several forms.  The forms that are used in this
3406    implementation are listed below.  */
3407
3408 enum dw_val_class
3409 {
3410   dw_val_class_addr,
3411   dw_val_class_offset,
3412   dw_val_class_loc,
3413   dw_val_class_loc_list,
3414   dw_val_class_range_list,
3415   dw_val_class_const,
3416   dw_val_class_unsigned_const,
3417   dw_val_class_long_long,
3418   dw_val_class_vec,
3419   dw_val_class_flag,
3420   dw_val_class_die_ref,
3421   dw_val_class_fde_ref,
3422   dw_val_class_lbl_id,
3423   dw_val_class_lineptr,
3424   dw_val_class_str,
3425   dw_val_class_macptr,
3426   dw_val_class_file
3427 };
3428
3429 /* Describe a double word constant value.  */
3430 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3431
3432 typedef struct dw_long_long_struct GTY(())
3433 {
3434   unsigned long hi;
3435   unsigned long low;
3436 }
3437 dw_long_long_const;
3438
3439 /* Describe a floating point constant value, or a vector constant value.  */
3440
3441 typedef struct dw_vec_struct GTY(())
3442 {
3443   unsigned char * GTY((length ("%h.length"))) array;
3444   unsigned length;
3445   unsigned elt_size;
3446 }
3447 dw_vec_const;
3448
3449 /* The dw_val_node describes an attribute's value, as it is
3450    represented internally.  */
3451
3452 typedef struct dw_val_struct GTY(())
3453 {
3454   enum dw_val_class val_class;
3455   union dw_val_struct_union
3456     {
3457       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3458       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3459       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3460       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3461       HOST_WIDE_INT GTY ((default)) val_int;
3462       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3463       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3464       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3465       struct dw_val_die_union
3466         {
3467           dw_die_ref die;
3468           int external;
3469         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3470       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3471       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3472       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3473       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3474       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3475     }
3476   GTY ((desc ("%1.val_class"))) v;
3477 }
3478 dw_val_node;
3479
3480 /* Locations in memory are described using a sequence of stack machine
3481    operations.  */
3482
3483 typedef struct dw_loc_descr_struct GTY(())
3484 {
3485   dw_loc_descr_ref dw_loc_next;
3486   enum dwarf_location_atom dw_loc_opc;
3487   dw_val_node dw_loc_oprnd1;
3488   dw_val_node dw_loc_oprnd2;
3489   int dw_loc_addr;
3490 }
3491 dw_loc_descr_node;
3492
3493 /* Location lists are ranges + location descriptions for that range,
3494    so you can track variables that are in different places over
3495    their entire life.  */
3496 typedef struct dw_loc_list_struct GTY(())
3497 {
3498   dw_loc_list_ref dw_loc_next;
3499   const char *begin; /* Label for begin address of range */
3500   const char *end;  /* Label for end address of range */
3501   char *ll_symbol; /* Label for beginning of location list.
3502                       Only on head of list */
3503   const char *section; /* Section this loclist is relative to */
3504   dw_loc_descr_ref expr;
3505 } dw_loc_list_node;
3506
3507 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3508
3509 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3510
3511 /* Convert a DWARF stack opcode into its string name.  */
3512
3513 static const char *
3514 dwarf_stack_op_name (unsigned int op)
3515 {
3516   switch (op)
3517     {
3518     case DW_OP_addr:
3519     case INTERNAL_DW_OP_tls_addr:
3520       return "DW_OP_addr";
3521     case DW_OP_deref:
3522       return "DW_OP_deref";
3523     case DW_OP_const1u:
3524       return "DW_OP_const1u";
3525     case DW_OP_const1s:
3526       return "DW_OP_const1s";
3527     case DW_OP_const2u:
3528       return "DW_OP_const2u";
3529     case DW_OP_const2s:
3530       return "DW_OP_const2s";
3531     case DW_OP_const4u:
3532       return "DW_OP_const4u";
3533     case DW_OP_const4s:
3534       return "DW_OP_const4s";
3535     case DW_OP_const8u:
3536       return "DW_OP_const8u";
3537     case DW_OP_const8s:
3538       return "DW_OP_const8s";
3539     case DW_OP_constu:
3540       return "DW_OP_constu";
3541     case DW_OP_consts:
3542       return "DW_OP_consts";
3543     case DW_OP_dup:
3544       return "DW_OP_dup";
3545     case DW_OP_drop:
3546       return "DW_OP_drop";
3547     case DW_OP_over:
3548       return "DW_OP_over";
3549     case DW_OP_pick:
3550       return "DW_OP_pick";
3551     case DW_OP_swap:
3552       return "DW_OP_swap";
3553     case DW_OP_rot:
3554       return "DW_OP_rot";
3555     case DW_OP_xderef:
3556       return "DW_OP_xderef";
3557     case DW_OP_abs:
3558       return "DW_OP_abs";
3559     case DW_OP_and:
3560       return "DW_OP_and";
3561     case DW_OP_div:
3562       return "DW_OP_div";
3563     case DW_OP_minus:
3564       return "DW_OP_minus";
3565     case DW_OP_mod:
3566       return "DW_OP_mod";
3567     case DW_OP_mul:
3568       return "DW_OP_mul";
3569     case DW_OP_neg:
3570       return "DW_OP_neg";
3571     case DW_OP_not:
3572       return "DW_OP_not";
3573     case DW_OP_or:
3574       return "DW_OP_or";
3575     case DW_OP_plus:
3576       return "DW_OP_plus";
3577     case DW_OP_plus_uconst:
3578       return "DW_OP_plus_uconst";
3579     case DW_OP_shl:
3580       return "DW_OP_shl";
3581     case DW_OP_shr:
3582       return "DW_OP_shr";
3583     case DW_OP_shra:
3584       return "DW_OP_shra";
3585     case DW_OP_xor:
3586       return "DW_OP_xor";
3587     case DW_OP_bra:
3588       return "DW_OP_bra";
3589     case DW_OP_eq:
3590       return "DW_OP_eq";
3591     case DW_OP_ge:
3592       return "DW_OP_ge";
3593     case DW_OP_gt:
3594       return "DW_OP_gt";
3595     case DW_OP_le:
3596       return "DW_OP_le";
3597     case DW_OP_lt:
3598       return "DW_OP_lt";
3599     case DW_OP_ne:
3600       return "DW_OP_ne";
3601     case DW_OP_skip:
3602       return "DW_OP_skip";
3603     case DW_OP_lit0:
3604       return "DW_OP_lit0";
3605     case DW_OP_lit1:
3606       return "DW_OP_lit1";
3607     case DW_OP_lit2:
3608       return "DW_OP_lit2";
3609     case DW_OP_lit3:
3610       return "DW_OP_lit3";
3611     case DW_OP_lit4:
3612       return "DW_OP_lit4";
3613     case DW_OP_lit5:
3614       return "DW_OP_lit5";
3615     case DW_OP_lit6:
3616       return "DW_OP_lit6";
3617     case DW_OP_lit7:
3618       return "DW_OP_lit7";
3619     case DW_OP_lit8:
3620       return "DW_OP_lit8";
3621     case DW_OP_lit9:
3622       return "DW_OP_lit9";
3623     case DW_OP_lit10:
3624       return "DW_OP_lit10";
3625     case DW_OP_lit11:
3626       return "DW_OP_lit11";
3627     case DW_OP_lit12:
3628       return "DW_OP_lit12";
3629     case DW_OP_lit13:
3630       return "DW_OP_lit13";
3631     case DW_OP_lit14:
3632       return "DW_OP_lit14";
3633     case DW_OP_lit15:
3634       return "DW_OP_lit15";
3635     case DW_OP_lit16:
3636       return "DW_OP_lit16";
3637     case DW_OP_lit17:
3638       return "DW_OP_lit17";
3639     case DW_OP_lit18:
3640       return "DW_OP_lit18";
3641     case DW_OP_lit19:
3642       return "DW_OP_lit19";
3643     case DW_OP_lit20:
3644       return "DW_OP_lit20";
3645     case DW_OP_lit21:
3646       return "DW_OP_lit21";
3647     case DW_OP_lit22:
3648       return "DW_OP_lit22";
3649     case DW_OP_lit23:
3650       return "DW_OP_lit23";
3651     case DW_OP_lit24:
3652       return "DW_OP_lit24";
3653     case DW_OP_lit25:
3654       return "DW_OP_lit25";
3655     case DW_OP_lit26:
3656       return "DW_OP_lit26";
3657     case DW_OP_lit27:
3658       return "DW_OP_lit27";
3659     case DW_OP_lit28:
3660       return "DW_OP_lit28";
3661     case DW_OP_lit29:
3662       return "DW_OP_lit29";
3663     case DW_OP_lit30:
3664       return "DW_OP_lit30";
3665     case DW_OP_lit31:
3666       return "DW_OP_lit31";
3667     case DW_OP_reg0:
3668       return "DW_OP_reg0";
3669     case DW_OP_reg1:
3670       return "DW_OP_reg1";
3671     case DW_OP_reg2:
3672       return "DW_OP_reg2";
3673     case DW_OP_reg3:
3674       return "DW_OP_reg3";
3675     case DW_OP_reg4:
3676       return "DW_OP_reg4";
3677     case DW_OP_reg5:
3678       return "DW_OP_reg5";
3679     case DW_OP_reg6:
3680       return "DW_OP_reg6";
3681     case DW_OP_reg7:
3682       return "DW_OP_reg7";
3683     case DW_OP_reg8:
3684       return "DW_OP_reg8";
3685     case DW_OP_reg9:
3686       return "DW_OP_reg9";
3687     case DW_OP_reg10:
3688       return "DW_OP_reg10";
3689     case DW_OP_reg11:
3690       return "DW_OP_reg11";
3691     case DW_OP_reg12:
3692       return "DW_OP_reg12";
3693     case DW_OP_reg13:
3694       return "DW_OP_reg13";
3695     case DW_OP_reg14:
3696       return "DW_OP_reg14";
3697     case DW_OP_reg15:
3698       return "DW_OP_reg15";
3699     case DW_OP_reg16:
3700       return "DW_OP_reg16";
3701     case DW_OP_reg17:
3702       return "DW_OP_reg17";
3703     case DW_OP_reg18:
3704       return "DW_OP_reg18";
3705     case DW_OP_reg19:
3706       return "DW_OP_reg19";
3707     case DW_OP_reg20:
3708       return "DW_OP_reg20";
3709     case DW_OP_reg21:
3710       return "DW_OP_reg21";
3711     case DW_OP_reg22:
3712       return "DW_OP_reg22";
3713     case DW_OP_reg23:
3714       return "DW_OP_reg23";
3715     case DW_OP_reg24:
3716       return "DW_OP_reg24";
3717     case DW_OP_reg25:
3718       return "DW_OP_reg25";
3719     case DW_OP_reg26:
3720       return "DW_OP_reg26";
3721     case DW_OP_reg27:
3722       return "DW_OP_reg27";
3723     case DW_OP_reg28:
3724       return "DW_OP_reg28";
3725     case DW_OP_reg29:
3726       return "DW_OP_reg29";
3727     case DW_OP_reg30:
3728       return "DW_OP_reg30";
3729     case DW_OP_reg31:
3730       return "DW_OP_reg31";
3731     case DW_OP_breg0:
3732       return "DW_OP_breg0";
3733     case DW_OP_breg1:
3734       return "DW_OP_breg1";
3735     case DW_OP_breg2:
3736       return "DW_OP_breg2";
3737     case DW_OP_breg3:
3738       return "DW_OP_breg3";
3739     case DW_OP_breg4:
3740       return "DW_OP_breg4";
3741     case DW_OP_breg5:
3742       return "DW_OP_breg5";
3743     case DW_OP_breg6:
3744       return "DW_OP_breg6";
3745     case DW_OP_breg7:
3746       return "DW_OP_breg7";
3747     case DW_OP_breg8:
3748       return "DW_OP_breg8";
3749     case DW_OP_breg9:
3750       return "DW_OP_breg9";
3751     case DW_OP_breg10:
3752       return "DW_OP_breg10";
3753     case DW_OP_breg11:
3754       return "DW_OP_breg11";
3755     case DW_OP_breg12:
3756       return "DW_OP_breg12";
3757     case DW_OP_breg13:
3758       return "DW_OP_breg13";
3759     case DW_OP_breg14:
3760       return "DW_OP_breg14";
3761     case DW_OP_breg15:
3762       return "DW_OP_breg15";
3763     case DW_OP_breg16:
3764       return "DW_OP_breg16";
3765     case DW_OP_breg17:
3766       return "DW_OP_breg17";
3767     case DW_OP_breg18:
3768       return "DW_OP_breg18";
3769     case DW_OP_breg19:
3770       return "DW_OP_breg19";
3771     case DW_OP_breg20:
3772       return "DW_OP_breg20";
3773     case DW_OP_breg21:
3774       return "DW_OP_breg21";
3775     case DW_OP_breg22:
3776       return "DW_OP_breg22";
3777     case DW_OP_breg23:
3778       return "DW_OP_breg23";
3779     case DW_OP_breg24:
3780       return "DW_OP_breg24";
3781     case DW_OP_breg25:
3782       return "DW_OP_breg25";
3783     case DW_OP_breg26:
3784       return "DW_OP_breg26";
3785     case DW_OP_breg27:
3786       return "DW_OP_breg27";
3787     case DW_OP_breg28:
3788       return "DW_OP_breg28";
3789     case DW_OP_breg29:
3790       return "DW_OP_breg29";
3791     case DW_OP_breg30:
3792       return "DW_OP_breg30";
3793     case DW_OP_breg31:
3794       return "DW_OP_breg31";
3795     case DW_OP_regx:
3796       return "DW_OP_regx";
3797     case DW_OP_fbreg:
3798       return "DW_OP_fbreg";
3799     case DW_OP_bregx:
3800       return "DW_OP_bregx";
3801     case DW_OP_piece:
3802       return "DW_OP_piece";
3803     case DW_OP_deref_size:
3804       return "DW_OP_deref_size";
3805     case DW_OP_xderef_size:
3806       return "DW_OP_xderef_size";
3807     case DW_OP_nop:
3808       return "DW_OP_nop";
3809     case DW_OP_push_object_address:
3810       return "DW_OP_push_object_address";
3811     case DW_OP_call2:
3812       return "DW_OP_call2";
3813     case DW_OP_call4:
3814       return "DW_OP_call4";
3815     case DW_OP_call_ref:
3816       return "DW_OP_call_ref";
3817     case DW_OP_GNU_push_tls_address:
3818       return "DW_OP_GNU_push_tls_address";
3819     case DW_OP_GNU_uninit:
3820       return "DW_OP_GNU_uninit";
3821     default:
3822       return "OP_<unknown>";
3823     }
3824 }
3825
3826 /* Return a pointer to a newly allocated location description.  Location
3827    descriptions are simple expression terms that can be strung
3828    together to form more complicated location (address) descriptions.  */
3829
3830 static inline dw_loc_descr_ref
3831 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3832                unsigned HOST_WIDE_INT oprnd2)
3833 {
3834   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3835
3836   descr->dw_loc_opc = op;
3837   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3838   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3839   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3840   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3841
3842   return descr;
3843 }
3844
3845 /* Return a pointer to a newly allocated location description for
3846    REG and OFFSET.  */
3847
3848 static inline dw_loc_descr_ref
3849 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3850 {
3851   if (offset)
3852     {
3853       if (reg <= 31)
3854         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3855       else
3856         return new_loc_descr (DW_OP_bregx, reg, offset);
3857     }
3858   else if (reg <= 31)
3859     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3860   else
3861    return new_loc_descr (DW_OP_regx, reg, 0);
3862 }
3863
3864 /* Add a location description term to a location description expression.  */
3865
3866 static inline void
3867 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3868 {
3869   dw_loc_descr_ref *d;
3870
3871   /* Find the end of the chain.  */
3872   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3873     ;
3874
3875   *d = descr;
3876 }
3877
3878 /* Return the size of a location descriptor.  */
3879
3880 static unsigned long
3881 size_of_loc_descr (dw_loc_descr_ref loc)
3882 {
3883   unsigned long size = 1;
3884
3885   switch (loc->dw_loc_opc)
3886     {
3887     case DW_OP_addr:
3888     case INTERNAL_DW_OP_tls_addr:
3889       size += DWARF2_ADDR_SIZE;
3890       break;
3891     case DW_OP_const1u:
3892     case DW_OP_const1s:
3893       size += 1;
3894       break;
3895     case DW_OP_const2u:
3896     case DW_OP_const2s:
3897       size += 2;
3898       break;
3899     case DW_OP_const4u:
3900     case DW_OP_const4s:
3901       size += 4;
3902       break;
3903     case DW_OP_const8u:
3904     case DW_OP_const8s:
3905       size += 8;
3906       break;
3907     case DW_OP_constu:
3908       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3909       break;
3910     case DW_OP_consts:
3911       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3912       break;
3913     case DW_OP_pick:
3914       size += 1;
3915       break;
3916     case DW_OP_plus_uconst:
3917       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3918       break;
3919     case DW_OP_skip:
3920     case DW_OP_bra:
3921       size += 2;
3922       break;
3923     case DW_OP_breg0:
3924     case DW_OP_breg1:
3925     case DW_OP_breg2:
3926     case DW_OP_breg3:
3927     case DW_OP_breg4:
3928     case DW_OP_breg5:
3929     case DW_OP_breg6:
3930     case DW_OP_breg7:
3931     case DW_OP_breg8:
3932     case DW_OP_breg9:
3933     case DW_OP_breg10:
3934     case DW_OP_breg11:
3935     case DW_OP_breg12:
3936     case DW_OP_breg13:
3937     case DW_OP_breg14:
3938     case DW_OP_breg15:
3939     case DW_OP_breg16:
3940     case DW_OP_breg17:
3941     case DW_OP_breg18:
3942     case DW_OP_breg19:
3943     case DW_OP_breg20:
3944     case DW_OP_breg21:
3945     case DW_OP_breg22:
3946     case DW_OP_breg23:
3947     case DW_OP_breg24:
3948     case DW_OP_breg25:
3949     case DW_OP_breg26:
3950     case DW_OP_breg27:
3951     case DW_OP_breg28:
3952     case DW_OP_breg29:
3953     case DW_OP_breg30:
3954     case DW_OP_breg31:
3955       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3956       break;
3957     case DW_OP_regx:
3958       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3959       break;
3960     case DW_OP_fbreg:
3961       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3962       break;
3963     case DW_OP_bregx:
3964       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3965       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3966       break;
3967     case DW_OP_piece:
3968       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3969       break;
3970     case DW_OP_deref_size:
3971     case DW_OP_xderef_size:
3972       size += 1;
3973       break;
3974     case DW_OP_call2:
3975       size += 2;
3976       break;
3977     case DW_OP_call4:
3978       size += 4;
3979       break;
3980     case DW_OP_call_ref:
3981       size += DWARF2_ADDR_SIZE;
3982       break;
3983     default:
3984       break;
3985     }
3986
3987   return size;
3988 }
3989
3990 /* Return the size of a series of location descriptors.  */
3991
3992 static unsigned long
3993 size_of_locs (dw_loc_descr_ref loc)
3994 {
3995   dw_loc_descr_ref l;
3996   unsigned long size;
3997
3998   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3999      field, to avoid writing to a PCH file.  */
4000   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4001     {
4002       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4003         break;
4004       size += size_of_loc_descr (l);
4005     }
4006   if (! l)
4007     return size;
4008
4009   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4010     {
4011       l->dw_loc_addr = size;
4012       size += size_of_loc_descr (l);
4013     }
4014
4015   return size;
4016 }
4017
4018 /* Output location description stack opcode's operands (if any).  */
4019
4020 static void
4021 output_loc_operands (dw_loc_descr_ref loc)
4022 {
4023   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4024   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4025
4026   switch (loc->dw_loc_opc)
4027     {
4028 #ifdef DWARF2_DEBUGGING_INFO
4029     case DW_OP_addr:
4030       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4031       break;
4032     case DW_OP_const2u:
4033     case DW_OP_const2s:
4034       dw2_asm_output_data (2, val1->v.val_int, NULL);
4035       break;
4036     case DW_OP_const4u:
4037     case DW_OP_const4s:
4038       dw2_asm_output_data (4, val1->v.val_int, NULL);
4039       break;
4040     case DW_OP_const8u:
4041     case DW_OP_const8s:
4042       gcc_assert (HOST_BITS_PER_LONG >= 64);
4043       dw2_asm_output_data (8, val1->v.val_int, NULL);
4044       break;
4045     case DW_OP_skip:
4046     case DW_OP_bra:
4047       {
4048         int offset;
4049
4050         gcc_assert (val1->val_class == dw_val_class_loc);
4051         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4052
4053         dw2_asm_output_data (2, offset, NULL);
4054       }
4055       break;
4056 #else
4057     case DW_OP_addr:
4058     case DW_OP_const2u:
4059     case DW_OP_const2s:
4060     case DW_OP_const4u:
4061     case DW_OP_const4s:
4062     case DW_OP_const8u:
4063     case DW_OP_const8s:
4064     case DW_OP_skip:
4065     case DW_OP_bra:
4066       /* We currently don't make any attempt to make sure these are
4067          aligned properly like we do for the main unwind info, so
4068          don't support emitting things larger than a byte if we're
4069          only doing unwinding.  */
4070       gcc_unreachable ();
4071 #endif
4072     case DW_OP_const1u:
4073     case DW_OP_const1s:
4074       dw2_asm_output_data (1, val1->v.val_int, NULL);
4075       break;
4076     case DW_OP_constu:
4077       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4078       break;
4079     case DW_OP_consts:
4080       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4081       break;
4082     case DW_OP_pick:
4083       dw2_asm_output_data (1, val1->v.val_int, NULL);
4084       break;
4085     case DW_OP_plus_uconst:
4086       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4087       break;
4088     case DW_OP_breg0:
4089     case DW_OP_breg1:
4090     case DW_OP_breg2:
4091     case DW_OP_breg3:
4092     case DW_OP_breg4:
4093     case DW_OP_breg5:
4094     case DW_OP_breg6:
4095     case DW_OP_breg7:
4096     case DW_OP_breg8:
4097     case DW_OP_breg9:
4098     case DW_OP_breg10:
4099     case DW_OP_breg11:
4100     case DW_OP_breg12:
4101     case DW_OP_breg13:
4102     case DW_OP_breg14:
4103     case DW_OP_breg15:
4104     case DW_OP_breg16:
4105     case DW_OP_breg17:
4106     case DW_OP_breg18:
4107     case DW_OP_breg19:
4108     case DW_OP_breg20:
4109     case DW_OP_breg21:
4110     case DW_OP_breg22:
4111     case DW_OP_breg23:
4112     case DW_OP_breg24:
4113     case DW_OP_breg25:
4114     case DW_OP_breg26:
4115     case DW_OP_breg27:
4116     case DW_OP_breg28:
4117     case DW_OP_breg29:
4118     case DW_OP_breg30:
4119     case DW_OP_breg31:
4120       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4121       break;
4122     case DW_OP_regx:
4123       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4124       break;
4125     case DW_OP_fbreg:
4126       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4127       break;
4128     case DW_OP_bregx:
4129       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4130       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4131       break;
4132     case DW_OP_piece:
4133       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4134       break;
4135     case DW_OP_deref_size:
4136     case DW_OP_xderef_size:
4137       dw2_asm_output_data (1, val1->v.val_int, NULL);
4138       break;
4139
4140     case INTERNAL_DW_OP_tls_addr:
4141       if (targetm.asm_out.output_dwarf_dtprel)
4142         {
4143           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4144                                                DWARF2_ADDR_SIZE,
4145                                                val1->v.val_addr);
4146           fputc ('\n', asm_out_file);
4147         }
4148       else
4149         gcc_unreachable ();
4150       break;
4151
4152     default:
4153       /* Other codes have no operands.  */
4154       break;
4155     }
4156 }
4157
4158 /* Output a sequence of location operations.  */
4159
4160 static void
4161 output_loc_sequence (dw_loc_descr_ref loc)
4162 {
4163   for (; loc != NULL; loc = loc->dw_loc_next)
4164     {
4165       /* Output the opcode.  */
4166       dw2_asm_output_data (1, loc->dw_loc_opc,
4167                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4168
4169       /* Output the operand(s) (if any).  */
4170       output_loc_operands (loc);
4171     }
4172 }
4173
4174 /* Output location description stack opcode's operands (if any).
4175    The output is single bytes on a line, suitable for .cfi_escape.  */
4176
4177 static void
4178 output_loc_operands_raw (dw_loc_descr_ref loc)
4179 {
4180   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4181   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4182
4183   switch (loc->dw_loc_opc)
4184     {
4185     case DW_OP_addr:
4186       /* We cannot output addresses in .cfi_escape, only bytes.  */
4187       gcc_unreachable ();
4188
4189     case DW_OP_const1u:
4190     case DW_OP_const1s:
4191     case DW_OP_pick:
4192     case DW_OP_deref_size:
4193     case DW_OP_xderef_size:
4194       fputc (',', asm_out_file);
4195       dw2_asm_output_data_raw (1, val1->v.val_int);
4196       break;
4197
4198     case DW_OP_const2u:
4199     case DW_OP_const2s:
4200       fputc (',', asm_out_file);
4201       dw2_asm_output_data_raw (2, val1->v.val_int);
4202       break;
4203
4204     case DW_OP_const4u:
4205     case DW_OP_const4s:
4206       fputc (',', asm_out_file);
4207       dw2_asm_output_data_raw (4, val1->v.val_int);
4208       break;
4209
4210     case DW_OP_const8u:
4211     case DW_OP_const8s:
4212       gcc_assert (HOST_BITS_PER_LONG >= 64);
4213       fputc (',', asm_out_file);
4214       dw2_asm_output_data_raw (8, val1->v.val_int);
4215       break;
4216
4217     case DW_OP_skip:
4218     case DW_OP_bra:
4219       {
4220         int offset;
4221
4222         gcc_assert (val1->val_class == dw_val_class_loc);
4223         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4224
4225         fputc (',', asm_out_file);
4226         dw2_asm_output_data_raw (2, offset);
4227       }
4228       break;
4229
4230     case DW_OP_constu:
4231     case DW_OP_plus_uconst:
4232     case DW_OP_regx:
4233     case DW_OP_piece:
4234       fputc (',', asm_out_file);
4235       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4236       break;
4237
4238     case DW_OP_consts:
4239     case DW_OP_breg0:
4240     case DW_OP_breg1:
4241     case DW_OP_breg2:
4242     case DW_OP_breg3:
4243     case DW_OP_breg4:
4244     case DW_OP_breg5:
4245     case DW_OP_breg6:
4246     case DW_OP_breg7:
4247     case DW_OP_breg8:
4248     case DW_OP_breg9:
4249     case DW_OP_breg10:
4250     case DW_OP_breg11:
4251     case DW_OP_breg12:
4252     case DW_OP_breg13:
4253     case DW_OP_breg14:
4254     case DW_OP_breg15:
4255     case DW_OP_breg16:
4256     case DW_OP_breg17:
4257     case DW_OP_breg18:
4258     case DW_OP_breg19:
4259     case DW_OP_breg20:
4260     case DW_OP_breg21:
4261     case DW_OP_breg22:
4262     case DW_OP_breg23:
4263     case DW_OP_breg24:
4264     case DW_OP_breg25:
4265     case DW_OP_breg26:
4266     case DW_OP_breg27:
4267     case DW_OP_breg28:
4268     case DW_OP_breg29:
4269     case DW_OP_breg30:
4270     case DW_OP_breg31:
4271     case DW_OP_fbreg:
4272       fputc (',', asm_out_file);
4273       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4274       break;
4275
4276     case DW_OP_bregx:
4277       fputc (',', asm_out_file);
4278       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4279       fputc (',', asm_out_file);
4280       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4281       break;
4282
4283     case INTERNAL_DW_OP_tls_addr:
4284       gcc_unreachable ();
4285
4286     default:
4287       /* Other codes have no operands.  */
4288       break;
4289     }
4290 }
4291
4292 static void
4293 output_loc_sequence_raw (dw_loc_descr_ref loc)
4294 {
4295   while (1)
4296     {
4297       /* Output the opcode.  */
4298       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4299       output_loc_operands_raw (loc);
4300
4301       if (!loc->dw_loc_next)
4302         break;
4303       loc = loc->dw_loc_next;
4304
4305       fputc (',', asm_out_file);
4306     }
4307 }
4308
4309 /* This routine will generate the correct assembly data for a location
4310    description based on a cfi entry with a complex address.  */
4311
4312 static void
4313 output_cfa_loc (dw_cfi_ref cfi)
4314 {
4315   dw_loc_descr_ref loc;
4316   unsigned long size;
4317
4318   if (cfi->dw_cfi_opc == DW_CFA_expression)
4319     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4320
4321   /* Output the size of the block.  */
4322   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4323   size = size_of_locs (loc);
4324   dw2_asm_output_data_uleb128 (size, NULL);
4325
4326   /* Now output the operations themselves.  */
4327   output_loc_sequence (loc);
4328 }
4329
4330 /* Similar, but used for .cfi_escape.  */
4331
4332 static void
4333 output_cfa_loc_raw (dw_cfi_ref cfi)
4334 {
4335   dw_loc_descr_ref loc;
4336   unsigned long size;
4337
4338   if (cfi->dw_cfi_opc == DW_CFA_expression)
4339     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4340
4341   /* Output the size of the block.  */
4342   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4343   size = size_of_locs (loc);
4344   dw2_asm_output_data_uleb128_raw (size);
4345   fputc (',', asm_out_file);
4346
4347   /* Now output the operations themselves.  */
4348   output_loc_sequence_raw (loc);
4349 }
4350
4351 /* This function builds a dwarf location descriptor sequence from a
4352    dw_cfa_location, adding the given OFFSET to the result of the
4353    expression.  */
4354
4355 static struct dw_loc_descr_struct *
4356 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4357 {
4358   struct dw_loc_descr_struct *head, *tmp;
4359
4360   offset += cfa->offset;
4361
4362   if (cfa->indirect)
4363     {
4364       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4365       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4366       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4367       add_loc_descr (&head, tmp);
4368       if (offset != 0)
4369         {
4370           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4371           add_loc_descr (&head, tmp);
4372         }
4373     }
4374   else
4375     head = new_reg_loc_descr (cfa->reg, offset);
4376
4377   return head;
4378 }
4379
4380 /* This function builds a dwarf location descriptor sequence for
4381    the address at OFFSET from the CFA when stack is aligned to
4382    ALIGNMENT byte.  */
4383
4384 static struct dw_loc_descr_struct *
4385 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4386 {
4387   struct dw_loc_descr_struct *head;
4388   unsigned int dwarf_fp
4389     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4390
4391  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4392   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4393     {
4394       head = new_reg_loc_descr (dwarf_fp, 0);
4395       add_loc_descr (&head, int_loc_descriptor (alignment));
4396       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4397
4398       add_loc_descr (&head, int_loc_descriptor (offset));
4399       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4400     }
4401   else
4402     head = new_reg_loc_descr (dwarf_fp, offset);
4403   return head;
4404 }
4405
4406 /* This function fills in aa dw_cfa_location structure from a dwarf location
4407    descriptor sequence.  */
4408
4409 static void
4410 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4411 {
4412   struct dw_loc_descr_struct *ptr;
4413   cfa->offset = 0;
4414   cfa->base_offset = 0;
4415   cfa->indirect = 0;
4416   cfa->reg = -1;
4417
4418   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4419     {
4420       enum dwarf_location_atom op = ptr->dw_loc_opc;
4421
4422       switch (op)
4423         {
4424         case DW_OP_reg0:
4425         case DW_OP_reg1:
4426         case DW_OP_reg2:
4427         case DW_OP_reg3:
4428         case DW_OP_reg4:
4429         case DW_OP_reg5:
4430         case DW_OP_reg6:
4431         case DW_OP_reg7:
4432         case DW_OP_reg8:
4433         case DW_OP_reg9:
4434         case DW_OP_reg10:
4435         case DW_OP_reg11:
4436         case DW_OP_reg12:
4437         case DW_OP_reg13:
4438         case DW_OP_reg14:
4439         case DW_OP_reg15:
4440         case DW_OP_reg16:
4441         case DW_OP_reg17:
4442         case DW_OP_reg18:
4443         case DW_OP_reg19:
4444         case DW_OP_reg20:
4445         case DW_OP_reg21:
4446         case DW_OP_reg22:
4447         case DW_OP_reg23:
4448         case DW_OP_reg24:
4449         case DW_OP_reg25:
4450         case DW_OP_reg26:
4451         case DW_OP_reg27:
4452         case DW_OP_reg28:
4453         case DW_OP_reg29:
4454         case DW_OP_reg30:
4455         case DW_OP_reg31:
4456           cfa->reg = op - DW_OP_reg0;
4457           break;
4458         case DW_OP_regx:
4459           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4460           break;
4461         case DW_OP_breg0:
4462         case DW_OP_breg1:
4463         case DW_OP_breg2:
4464         case DW_OP_breg3:
4465         case DW_OP_breg4:
4466         case DW_OP_breg5:
4467         case DW_OP_breg6:
4468         case DW_OP_breg7:
4469         case DW_OP_breg8:
4470         case DW_OP_breg9:
4471         case DW_OP_breg10:
4472         case DW_OP_breg11:
4473         case DW_OP_breg12:
4474         case DW_OP_breg13:
4475         case DW_OP_breg14:
4476         case DW_OP_breg15:
4477         case DW_OP_breg16:
4478         case DW_OP_breg17:
4479         case DW_OP_breg18:
4480         case DW_OP_breg19:
4481         case DW_OP_breg20:
4482         case DW_OP_breg21:
4483         case DW_OP_breg22:
4484         case DW_OP_breg23:
4485         case DW_OP_breg24:
4486         case DW_OP_breg25:
4487         case DW_OP_breg26:
4488         case DW_OP_breg27:
4489         case DW_OP_breg28:
4490         case DW_OP_breg29:
4491         case DW_OP_breg30:
4492         case DW_OP_breg31:
4493           cfa->reg = op - DW_OP_breg0;
4494           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4495           break;
4496         case DW_OP_bregx:
4497           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4498           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4499           break;
4500         case DW_OP_deref:
4501           cfa->indirect = 1;
4502           break;
4503         case DW_OP_plus_uconst:
4504           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4505           break;
4506         default:
4507           internal_error ("DW_LOC_OP %s not implemented",
4508                           dwarf_stack_op_name (ptr->dw_loc_opc));
4509         }
4510     }
4511 }
4512 #endif /* .debug_frame support */
4513 \f
4514 /* And now, the support for symbolic debugging information.  */
4515 #ifdef DWARF2_DEBUGGING_INFO
4516
4517 /* .debug_str support.  */
4518 static int output_indirect_string (void **, void *);
4519
4520 static void dwarf2out_init (const char *);
4521 static void dwarf2out_finish (const char *);
4522 static void dwarf2out_define (unsigned int, const char *);
4523 static void dwarf2out_undef (unsigned int, const char *);
4524 static void dwarf2out_start_source_file (unsigned, const char *);
4525 static void dwarf2out_end_source_file (unsigned);
4526 static void dwarf2out_begin_block (unsigned, unsigned);
4527 static void dwarf2out_end_block (unsigned, unsigned);
4528 static bool dwarf2out_ignore_block (const_tree);
4529 static void dwarf2out_global_decl (tree);
4530 static void dwarf2out_type_decl (tree, int);
4531 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4532 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4533                                                  dw_die_ref);
4534 static void dwarf2out_abstract_function (tree);
4535 static void dwarf2out_var_location (rtx);
4536 static void dwarf2out_begin_function (tree);
4537
4538 /* The debug hooks structure.  */
4539
4540 const struct gcc_debug_hooks dwarf2_debug_hooks =
4541 {
4542   dwarf2out_init,
4543   dwarf2out_finish,
4544   dwarf2out_define,
4545   dwarf2out_undef,
4546   dwarf2out_start_source_file,
4547   dwarf2out_end_source_file,
4548   dwarf2out_begin_block,
4549   dwarf2out_end_block,
4550   dwarf2out_ignore_block,
4551   dwarf2out_source_line,
4552   dwarf2out_begin_prologue,
4553   debug_nothing_int_charstar,   /* end_prologue */
4554   dwarf2out_end_epilogue,
4555   dwarf2out_begin_function,
4556   debug_nothing_int,            /* end_function */
4557   dwarf2out_decl,               /* function_decl */
4558   dwarf2out_global_decl,
4559   dwarf2out_type_decl,          /* type_decl */
4560   dwarf2out_imported_module_or_decl,
4561   debug_nothing_tree,           /* deferred_inline_function */
4562   /* The DWARF 2 backend tries to reduce debugging bloat by not
4563      emitting the abstract description of inline functions until
4564      something tries to reference them.  */
4565   dwarf2out_abstract_function,  /* outlining_inline_function */
4566   debug_nothing_rtx,            /* label */
4567   debug_nothing_int,            /* handle_pch */
4568   dwarf2out_var_location,
4569   dwarf2out_switch_text_section,
4570   1                             /* start_end_main_source_file */
4571 };
4572 #endif
4573 \f
4574 /* NOTE: In the comments in this file, many references are made to
4575    "Debugging Information Entries".  This term is abbreviated as `DIE'
4576    throughout the remainder of this file.  */
4577
4578 /* An internal representation of the DWARF output is built, and then
4579    walked to generate the DWARF debugging info.  The walk of the internal
4580    representation is done after the entire program has been compiled.
4581    The types below are used to describe the internal representation.  */
4582
4583 /* Various DIE's use offsets relative to the beginning of the
4584    .debug_info section to refer to each other.  */
4585
4586 typedef long int dw_offset;
4587
4588 /* Define typedefs here to avoid circular dependencies.  */
4589
4590 typedef struct dw_attr_struct *dw_attr_ref;
4591 typedef struct dw_line_info_struct *dw_line_info_ref;
4592 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4593 typedef struct pubname_struct *pubname_ref;
4594 typedef struct dw_ranges_struct *dw_ranges_ref;
4595 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4596
4597 /* Each entry in the line_info_table maintains the file and
4598    line number associated with the label generated for that
4599    entry.  The label gives the PC value associated with
4600    the line number entry.  */
4601
4602 typedef struct dw_line_info_struct GTY(())
4603 {
4604   unsigned long dw_file_num;
4605   unsigned long dw_line_num;
4606 }
4607 dw_line_info_entry;
4608
4609 /* Line information for functions in separate sections; each one gets its
4610    own sequence.  */
4611 typedef struct dw_separate_line_info_struct GTY(())
4612 {
4613   unsigned long dw_file_num;
4614   unsigned long dw_line_num;
4615   unsigned long function;
4616 }
4617 dw_separate_line_info_entry;
4618
4619 /* Each DIE attribute has a field specifying the attribute kind,
4620    a link to the next attribute in the chain, and an attribute value.
4621    Attributes are typically linked below the DIE they modify.  */
4622
4623 typedef struct dw_attr_struct GTY(())
4624 {
4625   enum dwarf_attribute dw_attr;
4626   dw_val_node dw_attr_val;
4627 }
4628 dw_attr_node;
4629
4630 DEF_VEC_O(dw_attr_node);
4631 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4632
4633 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4634    The children of each node form a circular list linked by
4635    die_sib.  die_child points to the node *before* the "first" child node.  */
4636
4637 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4638 {
4639   enum dwarf_tag die_tag;
4640   char *die_symbol;
4641   VEC(dw_attr_node,gc) * die_attr;
4642   dw_die_ref die_parent;
4643   dw_die_ref die_child;
4644   dw_die_ref die_sib;
4645   dw_die_ref die_definition; /* ref from a specification to its definition */
4646   dw_offset die_offset;
4647   unsigned long die_abbrev;
4648   int die_mark;
4649   /* Die is used and must not be pruned as unused.  */
4650   int die_perennial_p;
4651   unsigned int decl_id;
4652 }
4653 die_node;
4654
4655 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4656 #define FOR_EACH_CHILD(die, c, expr) do {       \
4657   c = die->die_child;                           \
4658   if (c) do {                                   \
4659     c = c->die_sib;                             \
4660     expr;                                       \
4661   } while (c != die->die_child);                \
4662 } while (0)
4663
4664 /* The pubname structure */
4665
4666 typedef struct pubname_struct GTY(())
4667 {
4668   dw_die_ref die;
4669   const char *name;
4670 }
4671 pubname_entry;
4672
4673 DEF_VEC_O(pubname_entry);
4674 DEF_VEC_ALLOC_O(pubname_entry, gc);
4675
4676 struct dw_ranges_struct GTY(())
4677 {
4678   /* If this is positive, it's a block number, otherwise it's a
4679      bitwise-negated index into dw_ranges_by_label.  */
4680   int num;
4681 };
4682
4683 struct dw_ranges_by_label_struct GTY(())
4684 {
4685   const char *begin;
4686   const char *end;
4687 };
4688
4689 /* The limbo die list structure.  */
4690 typedef struct limbo_die_struct GTY(())
4691 {
4692   dw_die_ref die;
4693   tree created_for;
4694   struct limbo_die_struct *next;
4695 }
4696 limbo_die_node;
4697
4698 /* How to start an assembler comment.  */
4699 #ifndef ASM_COMMENT_START
4700 #define ASM_COMMENT_START ";#"
4701 #endif
4702
4703 /* Define a macro which returns nonzero for a TYPE_DECL which was
4704    implicitly generated for a tagged type.
4705
4706    Note that unlike the gcc front end (which generates a NULL named
4707    TYPE_DECL node for each complete tagged type, each array type, and
4708    each function type node created) the g++ front end generates a
4709    _named_ TYPE_DECL node for each tagged type node created.
4710    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4711    generate a DW_TAG_typedef DIE for them.  */
4712
4713 #define TYPE_DECL_IS_STUB(decl)                         \
4714   (DECL_NAME (decl) == NULL_TREE                        \
4715    || (DECL_ARTIFICIAL (decl)                           \
4716        && is_tagged_type (TREE_TYPE (decl))             \
4717        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4718            /* This is necessary for stub decls that     \
4719               appear in nested inline functions.  */    \
4720            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4721                && (decl_ultimate_origin (decl)          \
4722                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4723
4724 /* Information concerning the compilation unit's programming
4725    language, and compiler version.  */
4726
4727 /* Fixed size portion of the DWARF compilation unit header.  */
4728 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4729   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4730
4731 /* Fixed size portion of public names info.  */
4732 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4733
4734 /* Fixed size portion of the address range info.  */
4735 #define DWARF_ARANGES_HEADER_SIZE                                       \
4736   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4737                 DWARF2_ADDR_SIZE * 2)                                   \
4738    - DWARF_INITIAL_LENGTH_SIZE)
4739
4740 /* Size of padding portion in the address range info.  It must be
4741    aligned to twice the pointer size.  */
4742 #define DWARF_ARANGES_PAD_SIZE \
4743   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4744                 DWARF2_ADDR_SIZE * 2)                              \
4745    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4746
4747 /* Use assembler line directives if available.  */
4748 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4749 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4750 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4751 #else
4752 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4753 #endif
4754 #endif
4755
4756 /* Minimum line offset in a special line info. opcode.
4757    This value was chosen to give a reasonable range of values.  */
4758 #define DWARF_LINE_BASE  -10
4759
4760 /* First special line opcode - leave room for the standard opcodes.  */
4761 #define DWARF_LINE_OPCODE_BASE  10
4762
4763 /* Range of line offsets in a special line info. opcode.  */
4764 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4765
4766 /* Flag that indicates the initial value of the is_stmt_start flag.
4767    In the present implementation, we do not mark any lines as
4768    the beginning of a source statement, because that information
4769    is not made available by the GCC front-end.  */
4770 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4771
4772 #ifdef DWARF2_DEBUGGING_INFO
4773 /* This location is used by calc_die_sizes() to keep track
4774    the offset of each DIE within the .debug_info section.  */
4775 static unsigned long next_die_offset;
4776 #endif
4777
4778 /* Record the root of the DIE's built for the current compilation unit.  */
4779 static GTY(()) dw_die_ref comp_unit_die;
4780
4781 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4782 static GTY(()) limbo_die_node *limbo_die_list;
4783
4784 /* Filenames referenced by this compilation unit.  */
4785 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4786
4787 /* A hash table of references to DIE's that describe declarations.
4788    The key is a DECL_UID() which is a unique number identifying each decl.  */
4789 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4790
4791 /* A hash table of references to DIE's that describe COMMON blocks.
4792    The key is DECL_UID() ^ die_parent.  */
4793 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4794
4795 /* Node of the variable location list.  */
4796 struct var_loc_node GTY ((chain_next ("%h.next")))
4797 {
4798   rtx GTY (()) var_loc_note;
4799   const char * GTY (()) label;
4800   const char * GTY (()) section_label;
4801   struct var_loc_node * GTY (()) next;
4802 };
4803
4804 /* Variable location list.  */
4805 struct var_loc_list_def GTY (())
4806 {
4807   struct var_loc_node * GTY (()) first;
4808
4809   /* Do not mark the last element of the chained list because
4810      it is marked through the chain.  */
4811   struct var_loc_node * GTY ((skip ("%h"))) last;
4812
4813   /* DECL_UID of the variable decl.  */
4814   unsigned int decl_id;
4815 };
4816 typedef struct var_loc_list_def var_loc_list;
4817
4818
4819 /* Table of decl location linked lists.  */
4820 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4821
4822 /* A pointer to the base of a list of references to DIE's that
4823    are uniquely identified by their tag, presence/absence of
4824    children DIE's, and list of attribute/value pairs.  */
4825 static GTY((length ("abbrev_die_table_allocated")))
4826   dw_die_ref *abbrev_die_table;
4827
4828 /* Number of elements currently allocated for abbrev_die_table.  */
4829 static GTY(()) unsigned abbrev_die_table_allocated;
4830
4831 /* Number of elements in type_die_table currently in use.  */
4832 static GTY(()) unsigned abbrev_die_table_in_use;
4833
4834 /* Size (in elements) of increments by which we may expand the
4835    abbrev_die_table.  */
4836 #define ABBREV_DIE_TABLE_INCREMENT 256
4837
4838 /* A pointer to the base of a table that contains line information
4839    for each source code line in .text in the compilation unit.  */
4840 static GTY((length ("line_info_table_allocated")))
4841      dw_line_info_ref line_info_table;
4842
4843 /* Number of elements currently allocated for line_info_table.  */
4844 static GTY(()) unsigned line_info_table_allocated;
4845
4846 /* Number of elements in line_info_table currently in use.  */
4847 static GTY(()) unsigned line_info_table_in_use;
4848
4849 /* A pointer to the base of a table that contains line information
4850    for each source code line outside of .text in the compilation unit.  */
4851 static GTY ((length ("separate_line_info_table_allocated")))
4852      dw_separate_line_info_ref separate_line_info_table;
4853
4854 /* Number of elements currently allocated for separate_line_info_table.  */
4855 static GTY(()) unsigned separate_line_info_table_allocated;
4856
4857 /* Number of elements in separate_line_info_table currently in use.  */
4858 static GTY(()) unsigned separate_line_info_table_in_use;
4859
4860 /* Size (in elements) of increments by which we may expand the
4861    line_info_table.  */
4862 #define LINE_INFO_TABLE_INCREMENT 1024
4863
4864 /* A pointer to the base of a table that contains a list of publicly
4865    accessible names.  */
4866 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4867
4868 /* A pointer to the base of a table that contains a list of publicly
4869    accessible types.  */
4870 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4871
4872 /* Array of dies for which we should generate .debug_arange info.  */
4873 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4874
4875 /* Number of elements currently allocated for arange_table.  */
4876 static GTY(()) unsigned arange_table_allocated;
4877
4878 /* Number of elements in arange_table currently in use.  */
4879 static GTY(()) unsigned arange_table_in_use;
4880
4881 /* Size (in elements) of increments by which we may expand the
4882    arange_table.  */
4883 #define ARANGE_TABLE_INCREMENT 64
4884
4885 /* Array of dies for which we should generate .debug_ranges info.  */
4886 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4887
4888 /* Number of elements currently allocated for ranges_table.  */
4889 static GTY(()) unsigned ranges_table_allocated;
4890
4891 /* Number of elements in ranges_table currently in use.  */
4892 static GTY(()) unsigned ranges_table_in_use;
4893
4894 /* Array of pairs of labels referenced in ranges_table.  */
4895 static GTY ((length ("ranges_by_label_allocated")))
4896      dw_ranges_by_label_ref ranges_by_label;
4897
4898 /* Number of elements currently allocated for ranges_by_label.  */
4899 static GTY(()) unsigned ranges_by_label_allocated;
4900
4901 /* Number of elements in ranges_by_label currently in use.  */
4902 static GTY(()) unsigned ranges_by_label_in_use;
4903
4904 /* Size (in elements) of increments by which we may expand the
4905    ranges_table.  */
4906 #define RANGES_TABLE_INCREMENT 64
4907
4908 /* Whether we have location lists that need outputting */
4909 static GTY(()) bool have_location_lists;
4910
4911 /* Unique label counter.  */
4912 static GTY(()) unsigned int loclabel_num;
4913
4914 #ifdef DWARF2_DEBUGGING_INFO
4915 /* Record whether the function being analyzed contains inlined functions.  */
4916 static int current_function_has_inlines;
4917 #endif
4918 #if 0 && defined (MIPS_DEBUGGING_INFO)
4919 static int comp_unit_has_inlines;
4920 #endif
4921
4922 /* The last file entry emitted by maybe_emit_file().  */
4923 static GTY(()) struct dwarf_file_data * last_emitted_file;
4924
4925 /* Number of internal labels generated by gen_internal_sym().  */
4926 static GTY(()) int label_num;
4927
4928 /* Cached result of previous call to lookup_filename.  */
4929 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4930
4931 #ifdef DWARF2_DEBUGGING_INFO
4932
4933 /* Offset from the "steady-state frame pointer" to the frame base,
4934    within the current function.  */
4935 static HOST_WIDE_INT frame_pointer_fb_offset;
4936
4937 /* Forward declarations for functions defined in this file.  */
4938
4939 static int is_pseudo_reg (const_rtx);
4940 static tree type_main_variant (tree);
4941 static int is_tagged_type (const_tree);
4942 static const char *dwarf_tag_name (unsigned);
4943 static const char *dwarf_attr_name (unsigned);
4944 static const char *dwarf_form_name (unsigned);
4945 static tree decl_ultimate_origin (const_tree);
4946 static tree block_ultimate_origin (const_tree);
4947 static tree decl_class_context (tree);
4948 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4949 static inline enum dw_val_class AT_class (dw_attr_ref);
4950 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4951 static inline unsigned AT_flag (dw_attr_ref);
4952 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4953 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4954 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4955 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4956 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4957                               unsigned long);
4958 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4959                                unsigned int, unsigned char *);
4960 static hashval_t debug_str_do_hash (const void *);
4961 static int debug_str_eq (const void *, const void *);
4962 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4963 static inline const char *AT_string (dw_attr_ref);
4964 static int AT_string_form (dw_attr_ref);
4965 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4966 static void add_AT_specification (dw_die_ref, dw_die_ref);
4967 static inline dw_die_ref AT_ref (dw_attr_ref);
4968 static inline int AT_ref_external (dw_attr_ref);
4969 static inline void set_AT_ref_external (dw_attr_ref, int);
4970 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4971 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4972 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4973 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4974                              dw_loc_list_ref);
4975 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4976 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4977 static inline rtx AT_addr (dw_attr_ref);
4978 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4979 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4980 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4981 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4982                            unsigned HOST_WIDE_INT);
4983 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4984                                unsigned long);
4985 static inline const char *AT_lbl (dw_attr_ref);
4986 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4987 static const char *get_AT_low_pc (dw_die_ref);
4988 static const char *get_AT_hi_pc (dw_die_ref);
4989 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4990 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4991 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4992 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4993 static bool is_c_family (void);
4994 static bool is_cxx (void);
4995 static bool is_java (void);
4996 static bool is_fortran (void);
4997 static bool is_ada (void);
4998 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4999 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5000 static void add_child_die (dw_die_ref, dw_die_ref);
5001 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5002 static dw_die_ref lookup_type_die (tree);
5003 static void equate_type_number_to_die (tree, dw_die_ref);
5004 static hashval_t decl_die_table_hash (const void *);
5005 static int decl_die_table_eq (const void *, const void *);
5006 static dw_die_ref lookup_decl_die (tree);
5007 static hashval_t common_block_die_table_hash (const void *);
5008 static int common_block_die_table_eq (const void *, const void *);
5009 static hashval_t decl_loc_table_hash (const void *);
5010 static int decl_loc_table_eq (const void *, const void *);
5011 static var_loc_list *lookup_decl_loc (const_tree);
5012 static void equate_decl_number_to_die (tree, dw_die_ref);
5013 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5014 static void print_spaces (FILE *);
5015 static void print_die (dw_die_ref, FILE *);
5016 static void print_dwarf_line_table (FILE *);
5017 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5018 static dw_die_ref pop_compile_unit (dw_die_ref);
5019 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5020 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5021 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5022 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5023 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5024 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5025 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5026 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5027 static void compute_section_prefix (dw_die_ref);
5028 static int is_type_die (dw_die_ref);
5029 static int is_comdat_die (dw_die_ref);
5030 static int is_symbol_die (dw_die_ref);
5031 static void assign_symbol_names (dw_die_ref);
5032 static void break_out_includes (dw_die_ref);
5033 static hashval_t htab_cu_hash (const void *);
5034 static int htab_cu_eq (const void *, const void *);
5035 static void htab_cu_del (void *);
5036 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5037 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5038 static void add_sibling_attributes (dw_die_ref);
5039 static void build_abbrev_table (dw_die_ref);
5040 static void output_location_lists (dw_die_ref);
5041 static int constant_size (unsigned HOST_WIDE_INT);
5042 static unsigned long size_of_die (dw_die_ref);
5043 static void calc_die_sizes (dw_die_ref);
5044 static void mark_dies (dw_die_ref);
5045 static void unmark_dies (dw_die_ref);
5046 static void unmark_all_dies (dw_die_ref);
5047 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5048 static unsigned long size_of_aranges (void);
5049 static enum dwarf_form value_format (dw_attr_ref);
5050 static void output_value_format (dw_attr_ref);
5051 static void output_abbrev_section (void);
5052 static void output_die_symbol (dw_die_ref);
5053 static void output_die (dw_die_ref);
5054 static void output_compilation_unit_header (void);
5055 static void output_comp_unit (dw_die_ref, int);
5056 static const char *dwarf2_name (tree, int);
5057 static void add_pubname (tree, dw_die_ref);
5058 static void add_pubname_string (const char *, dw_die_ref);
5059 static void add_pubtype (tree, dw_die_ref);
5060 static void output_pubnames (VEC (pubname_entry,gc) *);
5061 static void add_arange (tree, dw_die_ref);
5062 static void output_aranges (void);
5063 static unsigned int add_ranges_num (int);
5064 static unsigned int add_ranges (const_tree);
5065 static unsigned int add_ranges_by_labels (const char *, const char *);
5066 static void output_ranges (void);
5067 static void output_line_info (void);
5068 static void output_file_names (void);
5069 static dw_die_ref base_type_die (tree);
5070 static int is_base_type (tree);
5071 static bool is_subrange_type (const_tree);
5072 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5073 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5074 static int type_is_enum (const_tree);
5075 static unsigned int dbx_reg_number (const_rtx);
5076 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5077 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5078 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5079                                                 enum var_init_status);
5080 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5081                                                      enum var_init_status);
5082 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5083                                          enum var_init_status);
5084 static int is_based_loc (const_rtx);
5085 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5086                                             enum var_init_status);
5087 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5088                                                enum var_init_status);
5089 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5090 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5091 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5092 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5093 static tree field_type (const_tree);
5094 static unsigned int simple_type_align_in_bits (const_tree);
5095 static unsigned int simple_decl_align_in_bits (const_tree);
5096 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5097 static HOST_WIDE_INT field_byte_offset (const_tree);
5098 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5099                                          dw_loc_descr_ref);
5100 static void add_data_member_location_attribute (dw_die_ref, tree);
5101 static void add_const_value_attribute (dw_die_ref, rtx);
5102 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5103 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5104 static void insert_float (const_rtx, unsigned char *);
5105 static rtx rtl_for_decl_location (tree);
5106 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5107                                                    enum dwarf_attribute);
5108 static void tree_add_const_value_attribute (dw_die_ref, tree);
5109 static void add_name_attribute (dw_die_ref, const char *);
5110 static void add_comp_dir_attribute (dw_die_ref);
5111 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5112 static void add_subscript_info (dw_die_ref, tree, bool);
5113 static void add_byte_size_attribute (dw_die_ref, tree);
5114 static void add_bit_offset_attribute (dw_die_ref, tree);
5115 static void add_bit_size_attribute (dw_die_ref, tree);
5116 static void add_prototyped_attribute (dw_die_ref, tree);
5117 static void add_abstract_origin_attribute (dw_die_ref, tree);
5118 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5119 static void add_src_coords_attributes (dw_die_ref, tree);
5120 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5121 static void push_decl_scope (tree);
5122 static void pop_decl_scope (void);
5123 static dw_die_ref scope_die_for (tree, dw_die_ref);
5124 static inline int local_scope_p (dw_die_ref);
5125 static inline int class_or_namespace_scope_p (dw_die_ref);
5126 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5127 static void add_calling_convention_attribute (dw_die_ref, tree);
5128 static const char *type_tag (const_tree);
5129 static tree member_declared_type (const_tree);
5130 #if 0
5131 static const char *decl_start_label (tree);
5132 #endif
5133 static void gen_array_type_die (tree, dw_die_ref);
5134 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5135 #if 0
5136 static void gen_entry_point_die (tree, dw_die_ref);
5137 #endif
5138 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5139 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5140 static void gen_inlined_union_type_die (tree, dw_die_ref);
5141 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5142 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5143 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5144 static void gen_formal_types_die (tree, dw_die_ref);
5145 static void gen_subprogram_die (tree, dw_die_ref);
5146 static void gen_variable_die (tree, dw_die_ref);
5147 static void gen_const_die (tree, dw_die_ref);
5148 static void gen_label_die (tree, dw_die_ref);
5149 static void gen_lexical_block_die (tree, dw_die_ref, int);
5150 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5151 static void gen_field_die (tree, dw_die_ref);
5152 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5153 static dw_die_ref gen_compile_unit_die (const char *);
5154 static void gen_inheritance_die (tree, tree, dw_die_ref);
5155 static void gen_member_die (tree, dw_die_ref);
5156 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5157                                                 enum debug_info_usage);
5158 static void gen_subroutine_type_die (tree, dw_die_ref);
5159 static void gen_typedef_die (tree, dw_die_ref);
5160 static void gen_type_die (tree, dw_die_ref);
5161 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5162 static void gen_block_die (tree, dw_die_ref, int);
5163 static void decls_for_scope (tree, dw_die_ref, int);
5164 static int is_redundant_typedef (const_tree);
5165 static void gen_namespace_die (tree);
5166 static void gen_decl_die (tree, dw_die_ref);
5167 static dw_die_ref force_decl_die (tree);
5168 static dw_die_ref force_type_die (tree);
5169 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5170 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5171 static struct dwarf_file_data * lookup_filename (const char *);
5172 static void retry_incomplete_types (void);
5173 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5174 static void splice_child_die (dw_die_ref, dw_die_ref);
5175 static int file_info_cmp (const void *, const void *);
5176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5177                                      const char *, const char *, unsigned);
5178 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5179                                        const char *, const char *,
5180                                        const char *);
5181 static void output_loc_list (dw_loc_list_ref);
5182 static char *gen_internal_sym (const char *);
5183
5184 static void prune_unmark_dies (dw_die_ref);
5185 static void prune_unused_types_mark (dw_die_ref, int);
5186 static void prune_unused_types_walk (dw_die_ref);
5187 static void prune_unused_types_walk_attribs (dw_die_ref);
5188 static void prune_unused_types_prune (dw_die_ref);
5189 static void prune_unused_types (void);
5190 static int maybe_emit_file (struct dwarf_file_data *fd);
5191
5192 /* Section names used to hold DWARF debugging information.  */
5193 #ifndef DEBUG_INFO_SECTION
5194 #define DEBUG_INFO_SECTION      ".debug_info"
5195 #endif
5196 #ifndef DEBUG_ABBREV_SECTION
5197 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5198 #endif
5199 #ifndef DEBUG_ARANGES_SECTION
5200 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5201 #endif
5202 #ifndef DEBUG_MACINFO_SECTION
5203 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5204 #endif
5205 #ifndef DEBUG_LINE_SECTION
5206 #define DEBUG_LINE_SECTION      ".debug_line"
5207 #endif
5208 #ifndef DEBUG_LOC_SECTION
5209 #define DEBUG_LOC_SECTION       ".debug_loc"
5210 #endif
5211 #ifndef DEBUG_PUBNAMES_SECTION
5212 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5213 #endif
5214 #ifndef DEBUG_STR_SECTION
5215 #define DEBUG_STR_SECTION       ".debug_str"
5216 #endif
5217 #ifndef DEBUG_RANGES_SECTION
5218 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5219 #endif
5220
5221 /* Standard ELF section names for compiled code and data.  */
5222 #ifndef TEXT_SECTION_NAME
5223 #define TEXT_SECTION_NAME       ".text"
5224 #endif
5225
5226 /* Section flags for .debug_str section.  */
5227 #define DEBUG_STR_SECTION_FLAGS \
5228   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5229    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5230    : SECTION_DEBUG)
5231
5232 /* Labels we insert at beginning sections we can reference instead of
5233    the section names themselves.  */
5234
5235 #ifndef TEXT_SECTION_LABEL
5236 #define TEXT_SECTION_LABEL              "Ltext"
5237 #endif
5238 #ifndef COLD_TEXT_SECTION_LABEL
5239 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5240 #endif
5241 #ifndef DEBUG_LINE_SECTION_LABEL
5242 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5243 #endif
5244 #ifndef DEBUG_INFO_SECTION_LABEL
5245 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5246 #endif
5247 #ifndef DEBUG_ABBREV_SECTION_LABEL
5248 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5249 #endif
5250 #ifndef DEBUG_LOC_SECTION_LABEL
5251 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5252 #endif
5253 #ifndef DEBUG_RANGES_SECTION_LABEL
5254 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5255 #endif
5256 #ifndef DEBUG_MACINFO_SECTION_LABEL
5257 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5258 #endif
5259
5260 /* Definitions of defaults for formats and names of various special
5261    (artificial) labels which may be generated within this file (when the -g
5262    options is used and DWARF2_DEBUGGING_INFO is in effect.
5263    If necessary, these may be overridden from within the tm.h file, but
5264    typically, overriding these defaults is unnecessary.  */
5265
5266 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5267 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5268 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5269 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5270 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5271 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5272 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5273 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5274 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5275 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5276
5277 #ifndef TEXT_END_LABEL
5278 #define TEXT_END_LABEL          "Letext"
5279 #endif
5280 #ifndef COLD_END_LABEL
5281 #define COLD_END_LABEL          "Letext_cold"
5282 #endif
5283 #ifndef BLOCK_BEGIN_LABEL
5284 #define BLOCK_BEGIN_LABEL       "LBB"
5285 #endif
5286 #ifndef BLOCK_END_LABEL
5287 #define BLOCK_END_LABEL         "LBE"
5288 #endif
5289 #ifndef LINE_CODE_LABEL
5290 #define LINE_CODE_LABEL         "LM"
5291 #endif
5292 #ifndef SEPARATE_LINE_CODE_LABEL
5293 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5294 #endif
5295
5296 \f
5297 /* We allow a language front-end to designate a function that is to be
5298    called to "demangle" any name before it is put into a DIE.  */
5299
5300 static const char *(*demangle_name_func) (const char *);
5301
5302 void
5303 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5304 {
5305   demangle_name_func = func;
5306 }
5307
5308 /* Test if rtl node points to a pseudo register.  */
5309
5310 static inline int
5311 is_pseudo_reg (const_rtx rtl)
5312 {
5313   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5314           || (GET_CODE (rtl) == SUBREG
5315               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5316 }
5317
5318 /* Return a reference to a type, with its const and volatile qualifiers
5319    removed.  */
5320
5321 static inline tree
5322 type_main_variant (tree type)
5323 {
5324   type = TYPE_MAIN_VARIANT (type);
5325
5326   /* ??? There really should be only one main variant among any group of
5327      variants of a given type (and all of the MAIN_VARIANT values for all
5328      members of the group should point to that one type) but sometimes the C
5329      front-end messes this up for array types, so we work around that bug
5330      here.  */
5331   if (TREE_CODE (type) == ARRAY_TYPE)
5332     while (type != TYPE_MAIN_VARIANT (type))
5333       type = TYPE_MAIN_VARIANT (type);
5334
5335   return type;
5336 }
5337
5338 /* Return nonzero if the given type node represents a tagged type.  */
5339
5340 static inline int
5341 is_tagged_type (const_tree type)
5342 {
5343   enum tree_code code = TREE_CODE (type);
5344
5345   return (code == RECORD_TYPE || code == UNION_TYPE
5346           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5347 }
5348
5349 /* Convert a DIE tag into its string name.  */
5350
5351 static const char *
5352 dwarf_tag_name (unsigned int tag)
5353 {
5354   switch (tag)
5355     {
5356     case DW_TAG_padding:
5357       return "DW_TAG_padding";
5358     case DW_TAG_array_type:
5359       return "DW_TAG_array_type";
5360     case DW_TAG_class_type:
5361       return "DW_TAG_class_type";
5362     case DW_TAG_entry_point:
5363       return "DW_TAG_entry_point";
5364     case DW_TAG_enumeration_type:
5365       return "DW_TAG_enumeration_type";
5366     case DW_TAG_formal_parameter:
5367       return "DW_TAG_formal_parameter";
5368     case DW_TAG_imported_declaration:
5369       return "DW_TAG_imported_declaration";
5370     case DW_TAG_label:
5371       return "DW_TAG_label";
5372     case DW_TAG_lexical_block:
5373       return "DW_TAG_lexical_block";
5374     case DW_TAG_member:
5375       return "DW_TAG_member";
5376     case DW_TAG_pointer_type:
5377       return "DW_TAG_pointer_type";
5378     case DW_TAG_reference_type:
5379       return "DW_TAG_reference_type";
5380     case DW_TAG_compile_unit:
5381       return "DW_TAG_compile_unit";
5382     case DW_TAG_string_type:
5383       return "DW_TAG_string_type";
5384     case DW_TAG_structure_type:
5385       return "DW_TAG_structure_type";
5386     case DW_TAG_subroutine_type:
5387       return "DW_TAG_subroutine_type";
5388     case DW_TAG_typedef:
5389       return "DW_TAG_typedef";
5390     case DW_TAG_union_type:
5391       return "DW_TAG_union_type";
5392     case DW_TAG_unspecified_parameters:
5393       return "DW_TAG_unspecified_parameters";
5394     case DW_TAG_variant:
5395       return "DW_TAG_variant";
5396     case DW_TAG_common_block:
5397       return "DW_TAG_common_block";
5398     case DW_TAG_common_inclusion:
5399       return "DW_TAG_common_inclusion";
5400     case DW_TAG_inheritance:
5401       return "DW_TAG_inheritance";
5402     case DW_TAG_inlined_subroutine:
5403       return "DW_TAG_inlined_subroutine";
5404     case DW_TAG_module:
5405       return "DW_TAG_module";
5406     case DW_TAG_ptr_to_member_type:
5407       return "DW_TAG_ptr_to_member_type";
5408     case DW_TAG_set_type:
5409       return "DW_TAG_set_type";
5410     case DW_TAG_subrange_type:
5411       return "DW_TAG_subrange_type";
5412     case DW_TAG_with_stmt:
5413       return "DW_TAG_with_stmt";
5414     case DW_TAG_access_declaration:
5415       return "DW_TAG_access_declaration";
5416     case DW_TAG_base_type:
5417       return "DW_TAG_base_type";
5418     case DW_TAG_catch_block:
5419       return "DW_TAG_catch_block";
5420     case DW_TAG_const_type:
5421       return "DW_TAG_const_type";
5422     case DW_TAG_constant:
5423       return "DW_TAG_constant";
5424     case DW_TAG_enumerator:
5425       return "DW_TAG_enumerator";
5426     case DW_TAG_file_type:
5427       return "DW_TAG_file_type";
5428     case DW_TAG_friend:
5429       return "DW_TAG_friend";
5430     case DW_TAG_namelist:
5431       return "DW_TAG_namelist";
5432     case DW_TAG_namelist_item:
5433       return "DW_TAG_namelist_item";
5434     case DW_TAG_packed_type:
5435       return "DW_TAG_packed_type";
5436     case DW_TAG_subprogram:
5437       return "DW_TAG_subprogram";
5438     case DW_TAG_template_type_param:
5439       return "DW_TAG_template_type_param";
5440     case DW_TAG_template_value_param:
5441       return "DW_TAG_template_value_param";
5442     case DW_TAG_thrown_type:
5443       return "DW_TAG_thrown_type";
5444     case DW_TAG_try_block:
5445       return "DW_TAG_try_block";
5446     case DW_TAG_variant_part:
5447       return "DW_TAG_variant_part";
5448     case DW_TAG_variable:
5449       return "DW_TAG_variable";
5450     case DW_TAG_volatile_type:
5451       return "DW_TAG_volatile_type";
5452     case DW_TAG_dwarf_procedure:
5453       return "DW_TAG_dwarf_procedure";
5454     case DW_TAG_restrict_type:
5455       return "DW_TAG_restrict_type";
5456     case DW_TAG_interface_type:
5457       return "DW_TAG_interface_type";
5458     case DW_TAG_namespace:
5459       return "DW_TAG_namespace";
5460     case DW_TAG_imported_module:
5461       return "DW_TAG_imported_module";
5462     case DW_TAG_unspecified_type:
5463       return "DW_TAG_unspecified_type";
5464     case DW_TAG_partial_unit:
5465       return "DW_TAG_partial_unit";
5466     case DW_TAG_imported_unit:
5467       return "DW_TAG_imported_unit";
5468     case DW_TAG_condition:
5469       return "DW_TAG_condition";
5470     case DW_TAG_shared_type:
5471       return "DW_TAG_shared_type";
5472     case DW_TAG_MIPS_loop:
5473       return "DW_TAG_MIPS_loop";
5474     case DW_TAG_format_label:
5475       return "DW_TAG_format_label";
5476     case DW_TAG_function_template:
5477       return "DW_TAG_function_template";
5478     case DW_TAG_class_template:
5479       return "DW_TAG_class_template";
5480     case DW_TAG_GNU_BINCL:
5481       return "DW_TAG_GNU_BINCL";
5482     case DW_TAG_GNU_EINCL:
5483       return "DW_TAG_GNU_EINCL";
5484     default:
5485       return "DW_TAG_<unknown>";
5486     }
5487 }
5488
5489 /* Convert a DWARF attribute code into its string name.  */
5490
5491 static const char *
5492 dwarf_attr_name (unsigned int attr)
5493 {
5494   switch (attr)
5495     {
5496     case DW_AT_sibling:
5497       return "DW_AT_sibling";
5498     case DW_AT_location:
5499       return "DW_AT_location";
5500     case DW_AT_name:
5501       return "DW_AT_name";
5502     case DW_AT_ordering:
5503       return "DW_AT_ordering";
5504     case DW_AT_subscr_data:
5505       return "DW_AT_subscr_data";
5506     case DW_AT_byte_size:
5507       return "DW_AT_byte_size";
5508     case DW_AT_bit_offset:
5509       return "DW_AT_bit_offset";
5510     case DW_AT_bit_size:
5511       return "DW_AT_bit_size";
5512     case DW_AT_element_list:
5513       return "DW_AT_element_list";
5514     case DW_AT_stmt_list:
5515       return "DW_AT_stmt_list";
5516     case DW_AT_low_pc:
5517       return "DW_AT_low_pc";
5518     case DW_AT_high_pc:
5519       return "DW_AT_high_pc";
5520     case DW_AT_language:
5521       return "DW_AT_language";
5522     case DW_AT_member:
5523       return "DW_AT_member";
5524     case DW_AT_discr:
5525       return "DW_AT_discr";
5526     case DW_AT_discr_value:
5527       return "DW_AT_discr_value";
5528     case DW_AT_visibility:
5529       return "DW_AT_visibility";
5530     case DW_AT_import:
5531       return "DW_AT_import";
5532     case DW_AT_string_length:
5533       return "DW_AT_string_length";
5534     case DW_AT_common_reference:
5535       return "DW_AT_common_reference";
5536     case DW_AT_comp_dir:
5537       return "DW_AT_comp_dir";
5538     case DW_AT_const_value:
5539       return "DW_AT_const_value";
5540     case DW_AT_containing_type:
5541       return "DW_AT_containing_type";
5542     case DW_AT_default_value:
5543       return "DW_AT_default_value";
5544     case DW_AT_inline:
5545       return "DW_AT_inline";
5546     case DW_AT_is_optional:
5547       return "DW_AT_is_optional";
5548     case DW_AT_lower_bound:
5549       return "DW_AT_lower_bound";
5550     case DW_AT_producer:
5551       return "DW_AT_producer";
5552     case DW_AT_prototyped:
5553       return "DW_AT_prototyped";
5554     case DW_AT_return_addr:
5555       return "DW_AT_return_addr";
5556     case DW_AT_start_scope:
5557       return "DW_AT_start_scope";
5558     case DW_AT_bit_stride:
5559       return "DW_AT_bit_stride";
5560     case DW_AT_upper_bound:
5561       return "DW_AT_upper_bound";
5562     case DW_AT_abstract_origin:
5563       return "DW_AT_abstract_origin";
5564     case DW_AT_accessibility:
5565       return "DW_AT_accessibility";
5566     case DW_AT_address_class:
5567       return "DW_AT_address_class";
5568     case DW_AT_artificial:
5569       return "DW_AT_artificial";
5570     case DW_AT_base_types:
5571       return "DW_AT_base_types";
5572     case DW_AT_calling_convention:
5573       return "DW_AT_calling_convention";
5574     case DW_AT_count:
5575       return "DW_AT_count";
5576     case DW_AT_data_member_location:
5577       return "DW_AT_data_member_location";
5578     case DW_AT_decl_column:
5579       return "DW_AT_decl_column";
5580     case DW_AT_decl_file:
5581       return "DW_AT_decl_file";
5582     case DW_AT_decl_line:
5583       return "DW_AT_decl_line";
5584     case DW_AT_declaration:
5585       return "DW_AT_declaration";
5586     case DW_AT_discr_list:
5587       return "DW_AT_discr_list";
5588     case DW_AT_encoding:
5589       return "DW_AT_encoding";
5590     case DW_AT_external:
5591       return "DW_AT_external";
5592     case DW_AT_frame_base:
5593       return "DW_AT_frame_base";
5594     case DW_AT_friend:
5595       return "DW_AT_friend";
5596     case DW_AT_identifier_case:
5597       return "DW_AT_identifier_case";
5598     case DW_AT_macro_info:
5599       return "DW_AT_macro_info";
5600     case DW_AT_namelist_items:
5601       return "DW_AT_namelist_items";
5602     case DW_AT_priority:
5603       return "DW_AT_priority";
5604     case DW_AT_segment:
5605       return "DW_AT_segment";
5606     case DW_AT_specification:
5607       return "DW_AT_specification";
5608     case DW_AT_static_link:
5609       return "DW_AT_static_link";
5610     case DW_AT_type:
5611       return "DW_AT_type";
5612     case DW_AT_use_location:
5613       return "DW_AT_use_location";
5614     case DW_AT_variable_parameter:
5615       return "DW_AT_variable_parameter";
5616     case DW_AT_virtuality:
5617       return "DW_AT_virtuality";
5618     case DW_AT_vtable_elem_location:
5619       return "DW_AT_vtable_elem_location";
5620
5621     case DW_AT_allocated:
5622       return "DW_AT_allocated";
5623     case DW_AT_associated:
5624       return "DW_AT_associated";
5625     case DW_AT_data_location:
5626       return "DW_AT_data_location";
5627     case DW_AT_byte_stride:
5628       return "DW_AT_byte_stride";
5629     case DW_AT_entry_pc:
5630       return "DW_AT_entry_pc";
5631     case DW_AT_use_UTF8:
5632       return "DW_AT_use_UTF8";
5633     case DW_AT_extension:
5634       return "DW_AT_extension";
5635     case DW_AT_ranges:
5636       return "DW_AT_ranges";
5637     case DW_AT_trampoline:
5638       return "DW_AT_trampoline";
5639     case DW_AT_call_column:
5640       return "DW_AT_call_column";
5641     case DW_AT_call_file:
5642       return "DW_AT_call_file";
5643     case DW_AT_call_line:
5644       return "DW_AT_call_line";
5645
5646     case DW_AT_MIPS_fde:
5647       return "DW_AT_MIPS_fde";
5648     case DW_AT_MIPS_loop_begin:
5649       return "DW_AT_MIPS_loop_begin";
5650     case DW_AT_MIPS_tail_loop_begin:
5651       return "DW_AT_MIPS_tail_loop_begin";
5652     case DW_AT_MIPS_epilog_begin:
5653       return "DW_AT_MIPS_epilog_begin";
5654     case DW_AT_MIPS_loop_unroll_factor:
5655       return "DW_AT_MIPS_loop_unroll_factor";
5656     case DW_AT_MIPS_software_pipeline_depth:
5657       return "DW_AT_MIPS_software_pipeline_depth";
5658     case DW_AT_MIPS_linkage_name:
5659       return "DW_AT_MIPS_linkage_name";
5660     case DW_AT_MIPS_stride:
5661       return "DW_AT_MIPS_stride";
5662     case DW_AT_MIPS_abstract_name:
5663       return "DW_AT_MIPS_abstract_name";
5664     case DW_AT_MIPS_clone_origin:
5665       return "DW_AT_MIPS_clone_origin";
5666     case DW_AT_MIPS_has_inlines:
5667       return "DW_AT_MIPS_has_inlines";
5668
5669     case DW_AT_sf_names:
5670       return "DW_AT_sf_names";
5671     case DW_AT_src_info:
5672       return "DW_AT_src_info";
5673     case DW_AT_mac_info:
5674       return "DW_AT_mac_info";
5675     case DW_AT_src_coords:
5676       return "DW_AT_src_coords";
5677     case DW_AT_body_begin:
5678       return "DW_AT_body_begin";
5679     case DW_AT_body_end:
5680       return "DW_AT_body_end";
5681     case DW_AT_GNU_vector:
5682       return "DW_AT_GNU_vector";
5683
5684     case DW_AT_VMS_rtnbeg_pd_address:
5685       return "DW_AT_VMS_rtnbeg_pd_address";
5686
5687     default:
5688       return "DW_AT_<unknown>";
5689     }
5690 }
5691
5692 /* Convert a DWARF value form code into its string name.  */
5693
5694 static const char *
5695 dwarf_form_name (unsigned int form)
5696 {
5697   switch (form)
5698     {
5699     case DW_FORM_addr:
5700       return "DW_FORM_addr";
5701     case DW_FORM_block2:
5702       return "DW_FORM_block2";
5703     case DW_FORM_block4:
5704       return "DW_FORM_block4";
5705     case DW_FORM_data2:
5706       return "DW_FORM_data2";
5707     case DW_FORM_data4:
5708       return "DW_FORM_data4";
5709     case DW_FORM_data8:
5710       return "DW_FORM_data8";
5711     case DW_FORM_string:
5712       return "DW_FORM_string";
5713     case DW_FORM_block:
5714       return "DW_FORM_block";
5715     case DW_FORM_block1:
5716       return "DW_FORM_block1";
5717     case DW_FORM_data1:
5718       return "DW_FORM_data1";
5719     case DW_FORM_flag:
5720       return "DW_FORM_flag";
5721     case DW_FORM_sdata:
5722       return "DW_FORM_sdata";
5723     case DW_FORM_strp:
5724       return "DW_FORM_strp";
5725     case DW_FORM_udata:
5726       return "DW_FORM_udata";
5727     case DW_FORM_ref_addr:
5728       return "DW_FORM_ref_addr";
5729     case DW_FORM_ref1:
5730       return "DW_FORM_ref1";
5731     case DW_FORM_ref2:
5732       return "DW_FORM_ref2";
5733     case DW_FORM_ref4:
5734       return "DW_FORM_ref4";
5735     case DW_FORM_ref8:
5736       return "DW_FORM_ref8";
5737     case DW_FORM_ref_udata:
5738       return "DW_FORM_ref_udata";
5739     case DW_FORM_indirect:
5740       return "DW_FORM_indirect";
5741     default:
5742       return "DW_FORM_<unknown>";
5743     }
5744 }
5745 \f
5746 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5747    instance of an inlined instance of a decl which is local to an inline
5748    function, so we have to trace all of the way back through the origin chain
5749    to find out what sort of node actually served as the original seed for the
5750    given block.  */
5751
5752 static tree
5753 decl_ultimate_origin (const_tree decl)
5754 {
5755   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5756     return NULL_TREE;
5757
5758   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5759      nodes in the function to point to themselves; ignore that if
5760      we're trying to output the abstract instance of this function.  */
5761   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5762     return NULL_TREE;
5763
5764   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5765      most distant ancestor, this should never happen.  */
5766   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5767
5768   return DECL_ABSTRACT_ORIGIN (decl);
5769 }
5770
5771 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5772    instance of an inlined instance of a block which is local to an inline
5773    function, so we have to trace all of the way back through the origin chain
5774    to find out what sort of node actually served as the original seed for the
5775    given block.  */
5776
5777 static tree
5778 block_ultimate_origin (const_tree block)
5779 {
5780   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5781
5782   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5783      nodes in the function to point to themselves; ignore that if
5784      we're trying to output the abstract instance of this function.  */
5785   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5786     return NULL_TREE;
5787
5788   if (immediate_origin == NULL_TREE)
5789     return NULL_TREE;
5790   else
5791     {
5792       tree ret_val;
5793       tree lookahead = immediate_origin;
5794
5795       do
5796         {
5797           ret_val = lookahead;
5798           lookahead = (TREE_CODE (ret_val) == BLOCK
5799                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5800         }
5801       while (lookahead != NULL && lookahead != ret_val);
5802
5803       /* The block's abstract origin chain may not be the *ultimate* origin of
5804          the block. It could lead to a DECL that has an abstract origin set.
5805          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5806          will give us if it has one).  Note that DECL's abstract origins are
5807          supposed to be the most distant ancestor (or so decl_ultimate_origin
5808          claims), so we don't need to loop following the DECL origins.  */
5809       if (DECL_P (ret_val))
5810         return DECL_ORIGIN (ret_val);
5811
5812       return ret_val;
5813     }
5814 }
5815
5816 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5817    of a virtual function may refer to a base class, so we check the 'this'
5818    parameter.  */
5819
5820 static tree
5821 decl_class_context (tree decl)
5822 {
5823   tree context = NULL_TREE;
5824
5825   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5826     context = DECL_CONTEXT (decl);
5827   else
5828     context = TYPE_MAIN_VARIANT
5829       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5830
5831   if (context && !TYPE_P (context))
5832     context = NULL_TREE;
5833
5834   return context;
5835 }
5836 \f
5837 /* Add an attribute/value pair to a DIE.  */
5838
5839 static inline void
5840 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5841 {
5842   /* Maybe this should be an assert?  */
5843   if (die == NULL)
5844     return;
5845
5846   if (die->die_attr == NULL)
5847     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5848   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5849 }
5850
5851 static inline enum dw_val_class
5852 AT_class (dw_attr_ref a)
5853 {
5854   return a->dw_attr_val.val_class;
5855 }
5856
5857 /* Add a flag value attribute to a DIE.  */
5858
5859 static inline void
5860 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5861 {
5862   dw_attr_node attr;
5863
5864   attr.dw_attr = attr_kind;
5865   attr.dw_attr_val.val_class = dw_val_class_flag;
5866   attr.dw_attr_val.v.val_flag = flag;
5867   add_dwarf_attr (die, &attr);
5868 }
5869
5870 static inline unsigned
5871 AT_flag (dw_attr_ref a)
5872 {
5873   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5874   return a->dw_attr_val.v.val_flag;
5875 }
5876
5877 /* Add a signed integer attribute value to a DIE.  */
5878
5879 static inline void
5880 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5881 {
5882   dw_attr_node attr;
5883
5884   attr.dw_attr = attr_kind;
5885   attr.dw_attr_val.val_class = dw_val_class_const;
5886   attr.dw_attr_val.v.val_int = int_val;
5887   add_dwarf_attr (die, &attr);
5888 }
5889
5890 static inline HOST_WIDE_INT
5891 AT_int (dw_attr_ref a)
5892 {
5893   gcc_assert (a && AT_class (a) == dw_val_class_const);
5894   return a->dw_attr_val.v.val_int;
5895 }
5896
5897 /* Add an unsigned integer attribute value to a DIE.  */
5898
5899 static inline void
5900 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5901                  unsigned HOST_WIDE_INT unsigned_val)
5902 {
5903   dw_attr_node attr;
5904
5905   attr.dw_attr = attr_kind;
5906   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5907   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5908   add_dwarf_attr (die, &attr);
5909 }
5910
5911 static inline unsigned HOST_WIDE_INT
5912 AT_unsigned (dw_attr_ref a)
5913 {
5914   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5915   return a->dw_attr_val.v.val_unsigned;
5916 }
5917
5918 /* Add an unsigned double integer attribute value to a DIE.  */
5919
5920 static inline void
5921 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5922                   long unsigned int val_hi, long unsigned int val_low)
5923 {
5924   dw_attr_node attr;
5925
5926   attr.dw_attr = attr_kind;
5927   attr.dw_attr_val.val_class = dw_val_class_long_long;
5928   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5929   attr.dw_attr_val.v.val_long_long.low = val_low;
5930   add_dwarf_attr (die, &attr);
5931 }
5932
5933 /* Add a floating point attribute value to a DIE and return it.  */
5934
5935 static inline void
5936 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5937             unsigned int length, unsigned int elt_size, unsigned char *array)
5938 {
5939   dw_attr_node attr;
5940
5941   attr.dw_attr = attr_kind;
5942   attr.dw_attr_val.val_class = dw_val_class_vec;
5943   attr.dw_attr_val.v.val_vec.length = length;
5944   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5945   attr.dw_attr_val.v.val_vec.array = array;
5946   add_dwarf_attr (die, &attr);
5947 }
5948
5949 /* Hash and equality functions for debug_str_hash.  */
5950
5951 static hashval_t
5952 debug_str_do_hash (const void *x)
5953 {
5954   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5955 }
5956
5957 static int
5958 debug_str_eq (const void *x1, const void *x2)
5959 {
5960   return strcmp ((((const struct indirect_string_node *)x1)->str),
5961                  (const char *)x2) == 0;
5962 }
5963
5964 /* Add a string attribute value to a DIE.  */
5965
5966 static inline void
5967 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5968 {
5969   dw_attr_node attr;
5970   struct indirect_string_node *node;
5971   void **slot;
5972
5973   if (! debug_str_hash)
5974     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5975                                       debug_str_eq, NULL);
5976
5977   slot = htab_find_slot_with_hash (debug_str_hash, str,
5978                                    htab_hash_string (str), INSERT);
5979   if (*slot == NULL)
5980     {
5981       node = (struct indirect_string_node *)
5982                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5983       node->str = ggc_strdup (str);
5984       *slot = node;
5985     }
5986   else
5987     node = (struct indirect_string_node *) *slot;
5988
5989   node->refcount++;
5990
5991   attr.dw_attr = attr_kind;
5992   attr.dw_attr_val.val_class = dw_val_class_str;
5993   attr.dw_attr_val.v.val_str = node;
5994   add_dwarf_attr (die, &attr);
5995 }
5996
5997 static inline const char *
5998 AT_string (dw_attr_ref a)
5999 {
6000   gcc_assert (a && AT_class (a) == dw_val_class_str);
6001   return a->dw_attr_val.v.val_str->str;
6002 }
6003
6004 /* Find out whether a string should be output inline in DIE
6005    or out-of-line in .debug_str section.  */
6006
6007 static int
6008 AT_string_form (dw_attr_ref a)
6009 {
6010   struct indirect_string_node *node;
6011   unsigned int len;
6012   char label[32];
6013
6014   gcc_assert (a && AT_class (a) == dw_val_class_str);
6015
6016   node = a->dw_attr_val.v.val_str;
6017   if (node->form)
6018     return node->form;
6019
6020   len = strlen (node->str) + 1;
6021
6022   /* If the string is shorter or equal to the size of the reference, it is
6023      always better to put it inline.  */
6024   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6025     return node->form = DW_FORM_string;
6026
6027   /* If we cannot expect the linker to merge strings in .debug_str
6028      section, only put it into .debug_str if it is worth even in this
6029      single module.  */
6030   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6031       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6032     return node->form = DW_FORM_string;
6033
6034   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6035   ++dw2_string_counter;
6036   node->label = xstrdup (label);
6037
6038   return node->form = DW_FORM_strp;
6039 }
6040
6041 /* Add a DIE reference attribute value to a DIE.  */
6042
6043 static inline void
6044 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6045 {
6046   dw_attr_node attr;
6047
6048   attr.dw_attr = attr_kind;
6049   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6050   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6051   attr.dw_attr_val.v.val_die_ref.external = 0;
6052   add_dwarf_attr (die, &attr);
6053 }
6054
6055 /* Add an AT_specification attribute to a DIE, and also make the back
6056    pointer from the specification to the definition.  */
6057
6058 static inline void
6059 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6060 {
6061   add_AT_die_ref (die, DW_AT_specification, targ_die);
6062   gcc_assert (!targ_die->die_definition);
6063   targ_die->die_definition = die;
6064 }
6065
6066 static inline dw_die_ref
6067 AT_ref (dw_attr_ref a)
6068 {
6069   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6070   return a->dw_attr_val.v.val_die_ref.die;
6071 }
6072
6073 static inline int
6074 AT_ref_external (dw_attr_ref a)
6075 {
6076   if (a && AT_class (a) == dw_val_class_die_ref)
6077     return a->dw_attr_val.v.val_die_ref.external;
6078
6079   return 0;
6080 }
6081
6082 static inline void
6083 set_AT_ref_external (dw_attr_ref a, int i)
6084 {
6085   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6086   a->dw_attr_val.v.val_die_ref.external = i;
6087 }
6088
6089 /* Add an FDE reference attribute value to a DIE.  */
6090
6091 static inline void
6092 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6093 {
6094   dw_attr_node attr;
6095
6096   attr.dw_attr = attr_kind;
6097   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6098   attr.dw_attr_val.v.val_fde_index = targ_fde;
6099   add_dwarf_attr (die, &attr);
6100 }
6101
6102 /* Add a location description attribute value to a DIE.  */
6103
6104 static inline void
6105 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6106 {
6107   dw_attr_node attr;
6108
6109   attr.dw_attr = attr_kind;
6110   attr.dw_attr_val.val_class = dw_val_class_loc;
6111   attr.dw_attr_val.v.val_loc = loc;
6112   add_dwarf_attr (die, &attr);
6113 }
6114
6115 static inline dw_loc_descr_ref
6116 AT_loc (dw_attr_ref a)
6117 {
6118   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6119   return a->dw_attr_val.v.val_loc;
6120 }
6121
6122 static inline void
6123 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6124 {
6125   dw_attr_node attr;
6126
6127   attr.dw_attr = attr_kind;
6128   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6129   attr.dw_attr_val.v.val_loc_list = loc_list;
6130   add_dwarf_attr (die, &attr);
6131   have_location_lists = true;
6132 }
6133
6134 static inline dw_loc_list_ref
6135 AT_loc_list (dw_attr_ref a)
6136 {
6137   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6138   return a->dw_attr_val.v.val_loc_list;
6139 }
6140
6141 /* Add an address constant attribute value to a DIE.  */
6142
6143 static inline void
6144 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6145 {
6146   dw_attr_node attr;
6147
6148   attr.dw_attr = attr_kind;
6149   attr.dw_attr_val.val_class = dw_val_class_addr;
6150   attr.dw_attr_val.v.val_addr = addr;
6151   add_dwarf_attr (die, &attr);
6152 }
6153
6154 /* Get the RTX from to an address DIE attribute.  */
6155
6156 static inline rtx
6157 AT_addr (dw_attr_ref a)
6158 {
6159   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6160   return a->dw_attr_val.v.val_addr;
6161 }
6162
6163 /* Add a file attribute value to a DIE.  */
6164
6165 static inline void
6166 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6167              struct dwarf_file_data *fd)
6168 {
6169   dw_attr_node attr;
6170
6171   attr.dw_attr = attr_kind;
6172   attr.dw_attr_val.val_class = dw_val_class_file;
6173   attr.dw_attr_val.v.val_file = fd;
6174   add_dwarf_attr (die, &attr);
6175 }
6176
6177 /* Get the dwarf_file_data from a file DIE attribute.  */
6178
6179 static inline struct dwarf_file_data *
6180 AT_file (dw_attr_ref a)
6181 {
6182   gcc_assert (a && AT_class (a) == dw_val_class_file);
6183   return a->dw_attr_val.v.val_file;
6184 }
6185
6186 /* Add a label identifier attribute value to a DIE.  */
6187
6188 static inline void
6189 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6190 {
6191   dw_attr_node attr;
6192
6193   attr.dw_attr = attr_kind;
6194   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6195   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6196   add_dwarf_attr (die, &attr);
6197 }
6198
6199 /* Add a section offset attribute value to a DIE, an offset into the
6200    debug_line section.  */
6201
6202 static inline void
6203 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6204                 const char *label)
6205 {
6206   dw_attr_node attr;
6207
6208   attr.dw_attr = attr_kind;
6209   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6210   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6211   add_dwarf_attr (die, &attr);
6212 }
6213
6214 /* Add a section offset attribute value to a DIE, an offset into the
6215    debug_macinfo section.  */
6216
6217 static inline void
6218 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6219                const char *label)
6220 {
6221   dw_attr_node attr;
6222
6223   attr.dw_attr = attr_kind;
6224   attr.dw_attr_val.val_class = dw_val_class_macptr;
6225   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6226   add_dwarf_attr (die, &attr);
6227 }
6228
6229 /* Add an offset attribute value to a DIE.  */
6230
6231 static inline void
6232 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6233                unsigned HOST_WIDE_INT offset)
6234 {
6235   dw_attr_node attr;
6236
6237   attr.dw_attr = attr_kind;
6238   attr.dw_attr_val.val_class = dw_val_class_offset;
6239   attr.dw_attr_val.v.val_offset = offset;
6240   add_dwarf_attr (die, &attr);
6241 }
6242
6243 /* Add an range_list attribute value to a DIE.  */
6244
6245 static void
6246 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6247                    long unsigned int offset)
6248 {
6249   dw_attr_node attr;
6250
6251   attr.dw_attr = attr_kind;
6252   attr.dw_attr_val.val_class = dw_val_class_range_list;
6253   attr.dw_attr_val.v.val_offset = offset;
6254   add_dwarf_attr (die, &attr);
6255 }
6256
6257 static inline const char *
6258 AT_lbl (dw_attr_ref a)
6259 {
6260   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6261                     || AT_class (a) == dw_val_class_lineptr
6262                     || AT_class (a) == dw_val_class_macptr));
6263   return a->dw_attr_val.v.val_lbl_id;
6264 }
6265
6266 /* Get the attribute of type attr_kind.  */
6267
6268 static dw_attr_ref
6269 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6270 {
6271   dw_attr_ref a;
6272   unsigned ix;
6273   dw_die_ref spec = NULL;
6274
6275   if (! die)
6276     return NULL;
6277
6278   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6279     if (a->dw_attr == attr_kind)
6280       return a;
6281     else if (a->dw_attr == DW_AT_specification
6282              || a->dw_attr == DW_AT_abstract_origin)
6283       spec = AT_ref (a);
6284
6285   if (spec)
6286     return get_AT (spec, attr_kind);
6287
6288   return NULL;
6289 }
6290
6291 /* Return the "low pc" attribute value, typically associated with a subprogram
6292    DIE.  Return null if the "low pc" attribute is either not present, or if it
6293    cannot be represented as an assembler label identifier.  */
6294
6295 static inline const char *
6296 get_AT_low_pc (dw_die_ref die)
6297 {
6298   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6299
6300   return a ? AT_lbl (a) : NULL;
6301 }
6302
6303 /* Return the "high pc" attribute value, typically associated with a subprogram
6304    DIE.  Return null if the "high pc" attribute is either not present, or if it
6305    cannot be represented as an assembler label identifier.  */
6306
6307 static inline const char *
6308 get_AT_hi_pc (dw_die_ref die)
6309 {
6310   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6311
6312   return a ? AT_lbl (a) : NULL;
6313 }
6314
6315 /* Return the value of the string attribute designated by ATTR_KIND, or
6316    NULL if it is not present.  */
6317
6318 static inline const char *
6319 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6320 {
6321   dw_attr_ref a = get_AT (die, attr_kind);
6322
6323   return a ? AT_string (a) : NULL;
6324 }
6325
6326 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6327    if it is not present.  */
6328
6329 static inline int
6330 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6331 {
6332   dw_attr_ref a = get_AT (die, attr_kind);
6333
6334   return a ? AT_flag (a) : 0;
6335 }
6336
6337 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6338    if it is not present.  */
6339
6340 static inline unsigned
6341 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6342 {
6343   dw_attr_ref a = get_AT (die, attr_kind);
6344
6345   return a ? AT_unsigned (a) : 0;
6346 }
6347
6348 static inline dw_die_ref
6349 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6350 {
6351   dw_attr_ref a = get_AT (die, attr_kind);
6352
6353   return a ? AT_ref (a) : NULL;
6354 }
6355
6356 static inline struct dwarf_file_data *
6357 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6358 {
6359   dw_attr_ref a = get_AT (die, attr_kind);
6360
6361   return a ? AT_file (a) : NULL;
6362 }
6363
6364 /* Return TRUE if the language is C or C++.  */
6365
6366 static inline bool
6367 is_c_family (void)
6368 {
6369   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6370
6371   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6372           || lang == DW_LANG_C99
6373           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6374 }
6375
6376 /* Return TRUE if the language is C++.  */
6377
6378 static inline bool
6379 is_cxx (void)
6380 {
6381   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6382
6383   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6384 }
6385
6386 /* Return TRUE if the language is Fortran.  */
6387
6388 static inline bool
6389 is_fortran (void)
6390 {
6391   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6392
6393   return (lang == DW_LANG_Fortran77
6394           || lang == DW_LANG_Fortran90
6395           || lang == DW_LANG_Fortran95);
6396 }
6397
6398 /* Return TRUE if the language is Java.  */
6399
6400 static inline bool
6401 is_java (void)
6402 {
6403   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6404
6405   return lang == DW_LANG_Java;
6406 }
6407
6408 /* Return TRUE if the language is Ada.  */
6409
6410 static inline bool
6411 is_ada (void)
6412 {
6413   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6414
6415   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6416 }
6417
6418 /* Remove the specified attribute if present.  */
6419
6420 static void
6421 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6422 {
6423   dw_attr_ref a;
6424   unsigned ix;
6425
6426   if (! die)
6427     return;
6428
6429   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6430     if (a->dw_attr == attr_kind)
6431       {
6432         if (AT_class (a) == dw_val_class_str)
6433           if (a->dw_attr_val.v.val_str->refcount)
6434             a->dw_attr_val.v.val_str->refcount--;
6435
6436         /* VEC_ordered_remove should help reduce the number of abbrevs
6437            that are needed.  */
6438         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6439         return;
6440       }
6441 }
6442
6443 /* Remove CHILD from its parent.  PREV must have the property that
6444    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6445
6446 static void
6447 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6448 {
6449   gcc_assert (child->die_parent == prev->die_parent);
6450   gcc_assert (prev->die_sib == child);
6451   if (prev == child)
6452     {
6453       gcc_assert (child->die_parent->die_child == child);
6454       prev = NULL;
6455     }
6456   else
6457     prev->die_sib = child->die_sib;
6458   if (child->die_parent->die_child == child)
6459     child->die_parent->die_child = prev;
6460 }
6461
6462 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6463    matches TAG.  */
6464
6465 static void
6466 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6467 {
6468   dw_die_ref c;
6469
6470   c = die->die_child;
6471   if (c) do {
6472     dw_die_ref prev = c;
6473     c = c->die_sib;
6474     while (c->die_tag == tag)
6475       {
6476         remove_child_with_prev (c, prev);
6477         /* Might have removed every child.  */
6478         if (c == c->die_sib)
6479           return;
6480         c = c->die_sib;
6481       }
6482   } while (c != die->die_child);
6483 }
6484
6485 /* Add a CHILD_DIE as the last child of DIE.  */
6486
6487 static void
6488 add_child_die (dw_die_ref die, dw_die_ref child_die)
6489 {
6490   /* FIXME this should probably be an assert.  */
6491   if (! die || ! child_die)
6492     return;
6493   gcc_assert (die != child_die);
6494
6495   child_die->die_parent = die;
6496   if (die->die_child)
6497     {
6498       child_die->die_sib = die->die_child->die_sib;
6499       die->die_child->die_sib = child_die;
6500     }
6501   else
6502     child_die->die_sib = child_die;
6503   die->die_child = child_die;
6504 }
6505
6506 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6507    is the specification, to the end of PARENT's list of children.
6508    This is done by removing and re-adding it.  */
6509
6510 static void
6511 splice_child_die (dw_die_ref parent, dw_die_ref child)
6512 {
6513   dw_die_ref p;
6514
6515   /* We want the declaration DIE from inside the class, not the
6516      specification DIE at toplevel.  */
6517   if (child->die_parent != parent)
6518     {
6519       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6520
6521       if (tmp)
6522         child = tmp;
6523     }
6524
6525   gcc_assert (child->die_parent == parent
6526               || (child->die_parent
6527                   == get_AT_ref (parent, DW_AT_specification)));
6528
6529   for (p = child->die_parent->die_child; ; p = p->die_sib)
6530     if (p->die_sib == child)
6531       {
6532         remove_child_with_prev (child, p);
6533         break;
6534       }
6535
6536   add_child_die (parent, child);
6537 }
6538
6539 /* Return a pointer to a newly created DIE node.  */
6540
6541 static inline dw_die_ref
6542 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6543 {
6544   dw_die_ref die = GGC_CNEW (die_node);
6545
6546   die->die_tag = tag_value;
6547
6548   if (parent_die != NULL)
6549     add_child_die (parent_die, die);
6550   else
6551     {
6552       limbo_die_node *limbo_node;
6553
6554       limbo_node = GGC_CNEW (limbo_die_node);
6555       limbo_node->die = die;
6556       limbo_node->created_for = t;
6557       limbo_node->next = limbo_die_list;
6558       limbo_die_list = limbo_node;
6559     }
6560
6561   return die;
6562 }
6563
6564 /* Return the DIE associated with the given type specifier.  */
6565
6566 static inline dw_die_ref
6567 lookup_type_die (tree type)
6568 {
6569   return TYPE_SYMTAB_DIE (type);
6570 }
6571
6572 /* Equate a DIE to a given type specifier.  */
6573
6574 static inline void
6575 equate_type_number_to_die (tree type, dw_die_ref type_die)
6576 {
6577   TYPE_SYMTAB_DIE (type) = type_die;
6578 }
6579
6580 /* Returns a hash value for X (which really is a die_struct).  */
6581
6582 static hashval_t
6583 decl_die_table_hash (const void *x)
6584 {
6585   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6586 }
6587
6588 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6589
6590 static int
6591 decl_die_table_eq (const void *x, const void *y)
6592 {
6593   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6594 }
6595
6596 /* Return the DIE associated with a given declaration.  */
6597
6598 static inline dw_die_ref
6599 lookup_decl_die (tree decl)
6600 {
6601   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6602 }
6603
6604 /* Returns a hash value for X (which really is a var_loc_list).  */
6605
6606 static hashval_t
6607 decl_loc_table_hash (const void *x)
6608 {
6609   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6610 }
6611
6612 /* Return nonzero if decl_id of var_loc_list X is the same as
6613    UID of decl *Y.  */
6614
6615 static int
6616 decl_loc_table_eq (const void *x, const void *y)
6617 {
6618   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6619 }
6620
6621 /* Return the var_loc list associated with a given declaration.  */
6622
6623 static inline var_loc_list *
6624 lookup_decl_loc (const_tree decl)
6625 {
6626   return (var_loc_list *)
6627     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6628 }
6629
6630 /* Equate a DIE to a particular declaration.  */
6631
6632 static void
6633 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6634 {
6635   unsigned int decl_id = DECL_UID (decl);
6636   void **slot;
6637
6638   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6639   *slot = decl_die;
6640   decl_die->decl_id = decl_id;
6641 }
6642
6643 /* Add a variable location node to the linked list for DECL.  */
6644
6645 static void
6646 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6647 {
6648   unsigned int decl_id = DECL_UID (decl);
6649   var_loc_list *temp;
6650   void **slot;
6651
6652   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6653   if (*slot == NULL)
6654     {
6655       temp = GGC_CNEW (var_loc_list);
6656       temp->decl_id = decl_id;
6657       *slot = temp;
6658     }
6659   else
6660     temp = (var_loc_list *) *slot;
6661
6662   if (temp->last)
6663     {
6664       /* If the current location is the same as the end of the list,
6665          and either both or neither of the locations is uninitialized,
6666          we have nothing to do.  */
6667       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6668                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6669           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6670                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6671               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6672                    == VAR_INIT_STATUS_UNINITIALIZED)
6673                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6674                       == VAR_INIT_STATUS_UNINITIALIZED))))
6675         {
6676           /* Add LOC to the end of list and update LAST.  */
6677           temp->last->next = loc;
6678           temp->last = loc;
6679         }
6680     }
6681   /* Do not add empty location to the beginning of the list.  */
6682   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6683     {
6684       temp->first = loc;
6685       temp->last = loc;
6686     }
6687 }
6688 \f
6689 /* Keep track of the number of spaces used to indent the
6690    output of the debugging routines that print the structure of
6691    the DIE internal representation.  */
6692 static int print_indent;
6693
6694 /* Indent the line the number of spaces given by print_indent.  */
6695
6696 static inline void
6697 print_spaces (FILE *outfile)
6698 {
6699   fprintf (outfile, "%*s", print_indent, "");
6700 }
6701
6702 /* Print the information associated with a given DIE, and its children.
6703    This routine is a debugging aid only.  */
6704
6705 static void
6706 print_die (dw_die_ref die, FILE *outfile)
6707 {
6708   dw_attr_ref a;
6709   dw_die_ref c;
6710   unsigned ix;
6711
6712   print_spaces (outfile);
6713   fprintf (outfile, "DIE %4ld: %s\n",
6714            die->die_offset, dwarf_tag_name (die->die_tag));
6715   print_spaces (outfile);
6716   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6717   fprintf (outfile, " offset: %ld\n", die->die_offset);
6718
6719   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6720     {
6721       print_spaces (outfile);
6722       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6723
6724       switch (AT_class (a))
6725         {
6726         case dw_val_class_addr:
6727           fprintf (outfile, "address");
6728           break;
6729         case dw_val_class_offset:
6730           fprintf (outfile, "offset");
6731           break;
6732         case dw_val_class_loc:
6733           fprintf (outfile, "location descriptor");
6734           break;
6735         case dw_val_class_loc_list:
6736           fprintf (outfile, "location list -> label:%s",
6737                    AT_loc_list (a)->ll_symbol);
6738           break;
6739         case dw_val_class_range_list:
6740           fprintf (outfile, "range list");
6741           break;
6742         case dw_val_class_const:
6743           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6744           break;
6745         case dw_val_class_unsigned_const:
6746           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6747           break;
6748         case dw_val_class_long_long:
6749           fprintf (outfile, "constant (%lu,%lu)",
6750                    a->dw_attr_val.v.val_long_long.hi,
6751                    a->dw_attr_val.v.val_long_long.low);
6752           break;
6753         case dw_val_class_vec:
6754           fprintf (outfile, "floating-point or vector constant");
6755           break;
6756         case dw_val_class_flag:
6757           fprintf (outfile, "%u", AT_flag (a));
6758           break;
6759         case dw_val_class_die_ref:
6760           if (AT_ref (a) != NULL)
6761             {
6762               if (AT_ref (a)->die_symbol)
6763                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6764               else
6765                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6766             }
6767           else
6768             fprintf (outfile, "die -> <null>");
6769           break;
6770         case dw_val_class_lbl_id:
6771         case dw_val_class_lineptr:
6772         case dw_val_class_macptr:
6773           fprintf (outfile, "label: %s", AT_lbl (a));
6774           break;
6775         case dw_val_class_str:
6776           if (AT_string (a) != NULL)
6777             fprintf (outfile, "\"%s\"", AT_string (a));
6778           else
6779             fprintf (outfile, "<null>");
6780           break;
6781         case dw_val_class_file:
6782           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6783                    AT_file (a)->emitted_number);
6784           break;
6785         default:
6786           break;
6787         }
6788
6789       fprintf (outfile, "\n");
6790     }
6791
6792   if (die->die_child != NULL)
6793     {
6794       print_indent += 4;
6795       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6796       print_indent -= 4;
6797     }
6798   if (print_indent == 0)
6799     fprintf (outfile, "\n");
6800 }
6801
6802 /* Print the contents of the source code line number correspondence table.
6803    This routine is a debugging aid only.  */
6804
6805 static void
6806 print_dwarf_line_table (FILE *outfile)
6807 {
6808   unsigned i;
6809   dw_line_info_ref line_info;
6810
6811   fprintf (outfile, "\n\nDWARF source line information\n");
6812   for (i = 1; i < line_info_table_in_use; i++)
6813     {
6814       line_info = &line_info_table[i];
6815       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6816                line_info->dw_file_num,
6817                line_info->dw_line_num);
6818     }
6819
6820   fprintf (outfile, "\n\n");
6821 }
6822
6823 /* Print the information collected for a given DIE.  */
6824
6825 void
6826 debug_dwarf_die (dw_die_ref die)
6827 {
6828   print_die (die, stderr);
6829 }
6830
6831 /* Print all DWARF information collected for the compilation unit.
6832    This routine is a debugging aid only.  */
6833
6834 void
6835 debug_dwarf (void)
6836 {
6837   print_indent = 0;
6838   print_die (comp_unit_die, stderr);
6839   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6840     print_dwarf_line_table (stderr);
6841 }
6842 \f
6843 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6844    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6845    DIE that marks the start of the DIEs for this include file.  */
6846
6847 static dw_die_ref
6848 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6849 {
6850   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6851   dw_die_ref new_unit = gen_compile_unit_die (filename);
6852
6853   new_unit->die_sib = old_unit;
6854   return new_unit;
6855 }
6856
6857 /* Close an include-file CU and reopen the enclosing one.  */
6858
6859 static dw_die_ref
6860 pop_compile_unit (dw_die_ref old_unit)
6861 {
6862   dw_die_ref new_unit = old_unit->die_sib;
6863
6864   old_unit->die_sib = NULL;
6865   return new_unit;
6866 }
6867
6868 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6869 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6870
6871 /* Calculate the checksum of a location expression.  */
6872
6873 static inline void
6874 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6875 {
6876   CHECKSUM (loc->dw_loc_opc);
6877   CHECKSUM (loc->dw_loc_oprnd1);
6878   CHECKSUM (loc->dw_loc_oprnd2);
6879 }
6880
6881 /* Calculate the checksum of an attribute.  */
6882
6883 static void
6884 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6885 {
6886   dw_loc_descr_ref loc;
6887   rtx r;
6888
6889   CHECKSUM (at->dw_attr);
6890
6891   /* We don't care that this was compiled with a different compiler
6892      snapshot; if the output is the same, that's what matters.  */
6893   if (at->dw_attr == DW_AT_producer)
6894     return;
6895
6896   switch (AT_class (at))
6897     {
6898     case dw_val_class_const:
6899       CHECKSUM (at->dw_attr_val.v.val_int);
6900       break;
6901     case dw_val_class_unsigned_const:
6902       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6903       break;
6904     case dw_val_class_long_long:
6905       CHECKSUM (at->dw_attr_val.v.val_long_long);
6906       break;
6907     case dw_val_class_vec:
6908       CHECKSUM (at->dw_attr_val.v.val_vec);
6909       break;
6910     case dw_val_class_flag:
6911       CHECKSUM (at->dw_attr_val.v.val_flag);
6912       break;
6913     case dw_val_class_str:
6914       CHECKSUM_STRING (AT_string (at));
6915       break;
6916
6917     case dw_val_class_addr:
6918       r = AT_addr (at);
6919       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6920       CHECKSUM_STRING (XSTR (r, 0));
6921       break;
6922
6923     case dw_val_class_offset:
6924       CHECKSUM (at->dw_attr_val.v.val_offset);
6925       break;
6926
6927     case dw_val_class_loc:
6928       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6929         loc_checksum (loc, ctx);
6930       break;
6931
6932     case dw_val_class_die_ref:
6933       die_checksum (AT_ref (at), ctx, mark);
6934       break;
6935
6936     case dw_val_class_fde_ref:
6937     case dw_val_class_lbl_id:
6938     case dw_val_class_lineptr:
6939     case dw_val_class_macptr:
6940       break;
6941
6942     case dw_val_class_file:
6943       CHECKSUM_STRING (AT_file (at)->filename);
6944       break;
6945
6946     default:
6947       break;
6948     }
6949 }
6950
6951 /* Calculate the checksum of a DIE.  */
6952
6953 static void
6954 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6955 {
6956   dw_die_ref c;
6957   dw_attr_ref a;
6958   unsigned ix;
6959
6960   /* To avoid infinite recursion.  */
6961   if (die->die_mark)
6962     {
6963       CHECKSUM (die->die_mark);
6964       return;
6965     }
6966   die->die_mark = ++(*mark);
6967
6968   CHECKSUM (die->die_tag);
6969
6970   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6971     attr_checksum (a, ctx, mark);
6972
6973   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6974 }
6975
6976 #undef CHECKSUM
6977 #undef CHECKSUM_STRING
6978
6979 /* Do the location expressions look same?  */
6980 static inline int
6981 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6982 {
6983   return loc1->dw_loc_opc == loc2->dw_loc_opc
6984          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6985          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6986 }
6987
6988 /* Do the values look the same?  */
6989 static int
6990 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6991 {
6992   dw_loc_descr_ref loc1, loc2;
6993   rtx r1, r2;
6994
6995   if (v1->val_class != v2->val_class)
6996     return 0;
6997
6998   switch (v1->val_class)
6999     {
7000     case dw_val_class_const:
7001       return v1->v.val_int == v2->v.val_int;
7002     case dw_val_class_unsigned_const:
7003       return v1->v.val_unsigned == v2->v.val_unsigned;
7004     case dw_val_class_long_long:
7005       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7006              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7007     case dw_val_class_vec:
7008       if (v1->v.val_vec.length != v2->v.val_vec.length
7009           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7010         return 0;
7011       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7012                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7013         return 0;
7014       return 1;
7015     case dw_val_class_flag:
7016       return v1->v.val_flag == v2->v.val_flag;
7017     case dw_val_class_str:
7018       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7019
7020     case dw_val_class_addr:
7021       r1 = v1->v.val_addr;
7022       r2 = v2->v.val_addr;
7023       if (GET_CODE (r1) != GET_CODE (r2))
7024         return 0;
7025       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7026       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7027
7028     case dw_val_class_offset:
7029       return v1->v.val_offset == v2->v.val_offset;
7030
7031     case dw_val_class_loc:
7032       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7033            loc1 && loc2;
7034            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7035         if (!same_loc_p (loc1, loc2, mark))
7036           return 0;
7037       return !loc1 && !loc2;
7038
7039     case dw_val_class_die_ref:
7040       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7041
7042     case dw_val_class_fde_ref:
7043     case dw_val_class_lbl_id:
7044     case dw_val_class_lineptr:
7045     case dw_val_class_macptr:
7046       return 1;
7047
7048     case dw_val_class_file:
7049       return v1->v.val_file == v2->v.val_file;
7050
7051     default:
7052       return 1;
7053     }
7054 }
7055
7056 /* Do the attributes look the same?  */
7057
7058 static int
7059 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7060 {
7061   if (at1->dw_attr != at2->dw_attr)
7062     return 0;
7063
7064   /* We don't care that this was compiled with a different compiler
7065      snapshot; if the output is the same, that's what matters. */
7066   if (at1->dw_attr == DW_AT_producer)
7067     return 1;
7068
7069   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7070 }
7071
7072 /* Do the dies look the same?  */
7073
7074 static int
7075 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7076 {
7077   dw_die_ref c1, c2;
7078   dw_attr_ref a1;
7079   unsigned ix;
7080
7081   /* To avoid infinite recursion.  */
7082   if (die1->die_mark)
7083     return die1->die_mark == die2->die_mark;
7084   die1->die_mark = die2->die_mark = ++(*mark);
7085
7086   if (die1->die_tag != die2->die_tag)
7087     return 0;
7088
7089   if (VEC_length (dw_attr_node, die1->die_attr)
7090       != VEC_length (dw_attr_node, die2->die_attr))
7091     return 0;
7092
7093   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7094     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7095       return 0;
7096
7097   c1 = die1->die_child;
7098   c2 = die2->die_child;
7099   if (! c1)
7100     {
7101       if (c2)
7102         return 0;
7103     }
7104   else
7105     for (;;)
7106       {
7107         if (!same_die_p (c1, c2, mark))
7108           return 0;
7109         c1 = c1->die_sib;
7110         c2 = c2->die_sib;
7111         if (c1 == die1->die_child)
7112           {
7113             if (c2 == die2->die_child)
7114               break;
7115             else
7116               return 0;
7117           }
7118     }
7119
7120   return 1;
7121 }
7122
7123 /* Do the dies look the same?  Wrapper around same_die_p.  */
7124
7125 static int
7126 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7127 {
7128   int mark = 0;
7129   int ret = same_die_p (die1, die2, &mark);
7130
7131   unmark_all_dies (die1);
7132   unmark_all_dies (die2);
7133
7134   return ret;
7135 }
7136
7137 /* The prefix to attach to symbols on DIEs in the current comdat debug
7138    info section.  */
7139 static char *comdat_symbol_id;
7140
7141 /* The index of the current symbol within the current comdat CU.  */
7142 static unsigned int comdat_symbol_number;
7143
7144 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7145    children, and set comdat_symbol_id accordingly.  */
7146
7147 static void
7148 compute_section_prefix (dw_die_ref unit_die)
7149 {
7150   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7151   const char *base = die_name ? lbasename (die_name) : "anonymous";
7152   char *name = XALLOCAVEC (char, strlen (base) + 64);
7153   char *p;
7154   int i, mark;
7155   unsigned char checksum[16];
7156   struct md5_ctx ctx;
7157
7158   /* Compute the checksum of the DIE, then append part of it as hex digits to
7159      the name filename of the unit.  */
7160
7161   md5_init_ctx (&ctx);
7162   mark = 0;
7163   die_checksum (unit_die, &ctx, &mark);
7164   unmark_all_dies (unit_die);
7165   md5_finish_ctx (&ctx, checksum);
7166
7167   sprintf (name, "%s.", base);
7168   clean_symbol_name (name);
7169
7170   p = name + strlen (name);
7171   for (i = 0; i < 4; i++)
7172     {
7173       sprintf (p, "%.2x", checksum[i]);
7174       p += 2;
7175     }
7176
7177   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7178   comdat_symbol_number = 0;
7179 }
7180
7181 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7182
7183 static int
7184 is_type_die (dw_die_ref die)
7185 {
7186   switch (die->die_tag)
7187     {
7188     case DW_TAG_array_type:
7189     case DW_TAG_class_type:
7190     case DW_TAG_interface_type:
7191     case DW_TAG_enumeration_type:
7192     case DW_TAG_pointer_type:
7193     case DW_TAG_reference_type:
7194     case DW_TAG_string_type:
7195     case DW_TAG_structure_type:
7196     case DW_TAG_subroutine_type:
7197     case DW_TAG_union_type:
7198     case DW_TAG_ptr_to_member_type:
7199     case DW_TAG_set_type:
7200     case DW_TAG_subrange_type:
7201     case DW_TAG_base_type:
7202     case DW_TAG_const_type:
7203     case DW_TAG_file_type:
7204     case DW_TAG_packed_type:
7205     case DW_TAG_volatile_type:
7206     case DW_TAG_typedef:
7207       return 1;
7208     default:
7209       return 0;
7210     }
7211 }
7212
7213 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7214    Basically, we want to choose the bits that are likely to be shared between
7215    compilations (types) and leave out the bits that are specific to individual
7216    compilations (functions).  */
7217
7218 static int
7219 is_comdat_die (dw_die_ref c)
7220 {
7221   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7222      we do for stabs.  The advantage is a greater likelihood of sharing between
7223      objects that don't include headers in the same order (and therefore would
7224      put the base types in a different comdat).  jason 8/28/00 */
7225
7226   if (c->die_tag == DW_TAG_base_type)
7227     return 0;
7228
7229   if (c->die_tag == DW_TAG_pointer_type
7230       || c->die_tag == DW_TAG_reference_type
7231       || c->die_tag == DW_TAG_const_type
7232       || c->die_tag == DW_TAG_volatile_type)
7233     {
7234       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7235
7236       return t ? is_comdat_die (t) : 0;
7237     }
7238
7239   return is_type_die (c);
7240 }
7241
7242 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7243    compilation unit.  */
7244
7245 static int
7246 is_symbol_die (dw_die_ref c)
7247 {
7248   return (is_type_die (c)
7249           || (get_AT (c, DW_AT_declaration)
7250               && !get_AT (c, DW_AT_specification))
7251           || c->die_tag == DW_TAG_namespace
7252           || c->die_tag == DW_TAG_module);
7253 }
7254
7255 static char *
7256 gen_internal_sym (const char *prefix)
7257 {
7258   char buf[256];
7259
7260   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7261   return xstrdup (buf);
7262 }
7263
7264 /* Assign symbols to all worthy DIEs under DIE.  */
7265
7266 static void
7267 assign_symbol_names (dw_die_ref die)
7268 {
7269   dw_die_ref c;
7270
7271   if (is_symbol_die (die))
7272     {
7273       if (comdat_symbol_id)
7274         {
7275           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7276
7277           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7278                    comdat_symbol_id, comdat_symbol_number++);
7279           die->die_symbol = xstrdup (p);
7280         }
7281       else
7282         die->die_symbol = gen_internal_sym ("LDIE");
7283     }
7284
7285   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7286 }
7287
7288 struct cu_hash_table_entry
7289 {
7290   dw_die_ref cu;
7291   unsigned min_comdat_num, max_comdat_num;
7292   struct cu_hash_table_entry *next;
7293 };
7294
7295 /* Routines to manipulate hash table of CUs.  */
7296 static hashval_t
7297 htab_cu_hash (const void *of)
7298 {
7299   const struct cu_hash_table_entry *const entry =
7300     (const struct cu_hash_table_entry *) of;
7301
7302   return htab_hash_string (entry->cu->die_symbol);
7303 }
7304
7305 static int
7306 htab_cu_eq (const void *of1, const void *of2)
7307 {
7308   const struct cu_hash_table_entry *const entry1 =
7309     (const struct cu_hash_table_entry *) of1;
7310   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7311
7312   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7313 }
7314
7315 static void
7316 htab_cu_del (void *what)
7317 {
7318   struct cu_hash_table_entry *next,
7319     *entry = (struct cu_hash_table_entry *) what;
7320
7321   while (entry)
7322     {
7323       next = entry->next;
7324       free (entry);
7325       entry = next;
7326     }
7327 }
7328
7329 /* Check whether we have already seen this CU and set up SYM_NUM
7330    accordingly.  */
7331 static int
7332 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7333 {
7334   struct cu_hash_table_entry dummy;
7335   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7336
7337   dummy.max_comdat_num = 0;
7338
7339   slot = (struct cu_hash_table_entry **)
7340     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7341         INSERT);
7342   entry = *slot;
7343
7344   for (; entry; last = entry, entry = entry->next)
7345     {
7346       if (same_die_p_wrap (cu, entry->cu))
7347         break;
7348     }
7349
7350   if (entry)
7351     {
7352       *sym_num = entry->min_comdat_num;
7353       return 1;
7354     }
7355
7356   entry = XCNEW (struct cu_hash_table_entry);
7357   entry->cu = cu;
7358   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7359   entry->next = *slot;
7360   *slot = entry;
7361
7362   return 0;
7363 }
7364
7365 /* Record SYM_NUM to record of CU in HTABLE.  */
7366 static void
7367 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7368 {
7369   struct cu_hash_table_entry **slot, *entry;
7370
7371   slot = (struct cu_hash_table_entry **)
7372     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7373         NO_INSERT);
7374   entry = *slot;
7375
7376   entry->max_comdat_num = sym_num;
7377 }
7378
7379 /* Traverse the DIE (which is always comp_unit_die), and set up
7380    additional compilation units for each of the include files we see
7381    bracketed by BINCL/EINCL.  */
7382
7383 static void
7384 break_out_includes (dw_die_ref die)
7385 {
7386   dw_die_ref c;
7387   dw_die_ref unit = NULL;
7388   limbo_die_node *node, **pnode;
7389   htab_t cu_hash_table;
7390
7391   c = die->die_child;
7392   if (c) do {
7393     dw_die_ref prev = c;
7394     c = c->die_sib;
7395     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7396            || (unit && is_comdat_die (c)))
7397       {
7398         dw_die_ref next = c->die_sib;
7399
7400         /* This DIE is for a secondary CU; remove it from the main one.  */
7401         remove_child_with_prev (c, prev);
7402
7403         if (c->die_tag == DW_TAG_GNU_BINCL)
7404           unit = push_new_compile_unit (unit, c);
7405         else if (c->die_tag == DW_TAG_GNU_EINCL)
7406           unit = pop_compile_unit (unit);
7407         else
7408           add_child_die (unit, c);
7409         c = next;
7410         if (c == die->die_child)
7411           break;
7412       }
7413   } while (c != die->die_child);
7414
7415 #if 0
7416   /* We can only use this in debugging, since the frontend doesn't check
7417      to make sure that we leave every include file we enter.  */
7418   gcc_assert (!unit);
7419 #endif
7420
7421   assign_symbol_names (die);
7422   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7423   for (node = limbo_die_list, pnode = &limbo_die_list;
7424        node;
7425        node = node->next)
7426     {
7427       int is_dupl;
7428
7429       compute_section_prefix (node->die);
7430       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7431                         &comdat_symbol_number);
7432       assign_symbol_names (node->die);
7433       if (is_dupl)
7434         *pnode = node->next;
7435       else
7436         {
7437           pnode = &node->next;
7438           record_comdat_symbol_number (node->die, cu_hash_table,
7439                 comdat_symbol_number);
7440         }
7441     }
7442   htab_delete (cu_hash_table);
7443 }
7444
7445 /* Traverse the DIE and add a sibling attribute if it may have the
7446    effect of speeding up access to siblings.  To save some space,
7447    avoid generating sibling attributes for DIE's without children.  */
7448
7449 static void
7450 add_sibling_attributes (dw_die_ref die)
7451 {
7452   dw_die_ref c;
7453
7454   if (! die->die_child)
7455     return;
7456
7457   if (die->die_parent && die != die->die_parent->die_child)
7458     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7459
7460   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7461 }
7462
7463 /* Output all location lists for the DIE and its children.  */
7464
7465 static void
7466 output_location_lists (dw_die_ref die)
7467 {
7468   dw_die_ref c;
7469   dw_attr_ref a;
7470   unsigned ix;
7471
7472   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7473     if (AT_class (a) == dw_val_class_loc_list)
7474       output_loc_list (AT_loc_list (a));
7475
7476   FOR_EACH_CHILD (die, c, output_location_lists (c));
7477 }
7478
7479 /* The format of each DIE (and its attribute value pairs) is encoded in an
7480    abbreviation table.  This routine builds the abbreviation table and assigns
7481    a unique abbreviation id for each abbreviation entry.  The children of each
7482    die are visited recursively.  */
7483
7484 static void
7485 build_abbrev_table (dw_die_ref die)
7486 {
7487   unsigned long abbrev_id;
7488   unsigned int n_alloc;
7489   dw_die_ref c;
7490   dw_attr_ref a;
7491   unsigned ix;
7492
7493   /* Scan the DIE references, and mark as external any that refer to
7494      DIEs from other CUs (i.e. those which are not marked).  */
7495   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7496     if (AT_class (a) == dw_val_class_die_ref
7497         && AT_ref (a)->die_mark == 0)
7498       {
7499         gcc_assert (AT_ref (a)->die_symbol);
7500
7501         set_AT_ref_external (a, 1);
7502       }
7503
7504   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7505     {
7506       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7507       dw_attr_ref die_a, abbrev_a;
7508       unsigned ix;
7509       bool ok = true;
7510
7511       if (abbrev->die_tag != die->die_tag)
7512         continue;
7513       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7514         continue;
7515
7516       if (VEC_length (dw_attr_node, abbrev->die_attr)
7517           != VEC_length (dw_attr_node, die->die_attr))
7518         continue;
7519
7520       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7521         {
7522           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7523           if ((abbrev_a->dw_attr != die_a->dw_attr)
7524               || (value_format (abbrev_a) != value_format (die_a)))
7525             {
7526               ok = false;
7527               break;
7528             }
7529         }
7530       if (ok)
7531         break;
7532     }
7533
7534   if (abbrev_id >= abbrev_die_table_in_use)
7535     {
7536       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7537         {
7538           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7539           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7540                                             n_alloc);
7541
7542           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7543                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7544           abbrev_die_table_allocated = n_alloc;
7545         }
7546
7547       ++abbrev_die_table_in_use;
7548       abbrev_die_table[abbrev_id] = die;
7549     }
7550
7551   die->die_abbrev = abbrev_id;
7552   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7553 }
7554 \f
7555 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7556
7557 static int
7558 constant_size (unsigned HOST_WIDE_INT value)
7559 {
7560   int log;
7561
7562   if (value == 0)
7563     log = 0;
7564   else
7565     log = floor_log2 (value);
7566
7567   log = log / 8;
7568   log = 1 << (floor_log2 (log) + 1);
7569
7570   return log;
7571 }
7572
7573 /* Return the size of a DIE as it is represented in the
7574    .debug_info section.  */
7575
7576 static unsigned long
7577 size_of_die (dw_die_ref die)
7578 {
7579   unsigned long size = 0;
7580   dw_attr_ref a;
7581   unsigned ix;
7582
7583   size += size_of_uleb128 (die->die_abbrev);
7584   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7585     {
7586       switch (AT_class (a))
7587         {
7588         case dw_val_class_addr:
7589           size += DWARF2_ADDR_SIZE;
7590           break;
7591         case dw_val_class_offset:
7592           size += DWARF_OFFSET_SIZE;
7593           break;
7594         case dw_val_class_loc:
7595           {
7596             unsigned long lsize = size_of_locs (AT_loc (a));
7597
7598             /* Block length.  */
7599             size += constant_size (lsize);
7600             size += lsize;
7601           }
7602           break;
7603         case dw_val_class_loc_list:
7604           size += DWARF_OFFSET_SIZE;
7605           break;
7606         case dw_val_class_range_list:
7607           size += DWARF_OFFSET_SIZE;
7608           break;
7609         case dw_val_class_const:
7610           size += size_of_sleb128 (AT_int (a));
7611           break;
7612         case dw_val_class_unsigned_const:
7613           size += constant_size (AT_unsigned (a));
7614           break;
7615         case dw_val_class_long_long:
7616           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7617           break;
7618         case dw_val_class_vec:
7619           size += constant_size (a->dw_attr_val.v.val_vec.length
7620                                  * a->dw_attr_val.v.val_vec.elt_size)
7621                   + a->dw_attr_val.v.val_vec.length
7622                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7623           break;
7624         case dw_val_class_flag:
7625           size += 1;
7626           break;
7627         case dw_val_class_die_ref:
7628           if (AT_ref_external (a))
7629             size += DWARF2_ADDR_SIZE;
7630           else
7631             size += DWARF_OFFSET_SIZE;
7632           break;
7633         case dw_val_class_fde_ref:
7634           size += DWARF_OFFSET_SIZE;
7635           break;
7636         case dw_val_class_lbl_id:
7637           size += DWARF2_ADDR_SIZE;
7638           break;
7639         case dw_val_class_lineptr:
7640         case dw_val_class_macptr:
7641           size += DWARF_OFFSET_SIZE;
7642           break;
7643         case dw_val_class_str:
7644           if (AT_string_form (a) == DW_FORM_strp)
7645             size += DWARF_OFFSET_SIZE;
7646           else
7647             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7648           break;
7649         case dw_val_class_file:
7650           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7651           break;
7652         default:
7653           gcc_unreachable ();
7654         }
7655     }
7656
7657   return size;
7658 }
7659
7660 /* Size the debugging information associated with a given DIE.  Visits the
7661    DIE's children recursively.  Updates the global variable next_die_offset, on
7662    each time through.  Uses the current value of next_die_offset to update the
7663    die_offset field in each DIE.  */
7664
7665 static void
7666 calc_die_sizes (dw_die_ref die)
7667 {
7668   dw_die_ref c;
7669
7670   die->die_offset = next_die_offset;
7671   next_die_offset += size_of_die (die);
7672
7673   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7674
7675   if (die->die_child != NULL)
7676     /* Count the null byte used to terminate sibling lists.  */
7677     next_die_offset += 1;
7678 }
7679
7680 /* Set the marks for a die and its children.  We do this so
7681    that we know whether or not a reference needs to use FORM_ref_addr; only
7682    DIEs in the same CU will be marked.  We used to clear out the offset
7683    and use that as the flag, but ran into ordering problems.  */
7684
7685 static void
7686 mark_dies (dw_die_ref die)
7687 {
7688   dw_die_ref c;
7689
7690   gcc_assert (!die->die_mark);
7691
7692   die->die_mark = 1;
7693   FOR_EACH_CHILD (die, c, mark_dies (c));
7694 }
7695
7696 /* Clear the marks for a die and its children.  */
7697
7698 static void
7699 unmark_dies (dw_die_ref die)
7700 {
7701   dw_die_ref c;
7702
7703   gcc_assert (die->die_mark);
7704
7705   die->die_mark = 0;
7706   FOR_EACH_CHILD (die, c, unmark_dies (c));
7707 }
7708
7709 /* Clear the marks for a die, its children and referred dies.  */
7710
7711 static void
7712 unmark_all_dies (dw_die_ref die)
7713 {
7714   dw_die_ref c;
7715   dw_attr_ref a;
7716   unsigned ix;
7717
7718   if (!die->die_mark)
7719     return;
7720   die->die_mark = 0;
7721
7722   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7723
7724   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7725     if (AT_class (a) == dw_val_class_die_ref)
7726       unmark_all_dies (AT_ref (a));
7727 }
7728
7729 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7730    generated for the compilation unit.  */
7731
7732 static unsigned long
7733 size_of_pubnames (VEC (pubname_entry, gc) * names)
7734 {
7735   unsigned long size;
7736   unsigned i;
7737   pubname_ref p;
7738
7739   size = DWARF_PUBNAMES_HEADER_SIZE;
7740   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7741     if (names != pubtype_table
7742         || p->die->die_offset != 0
7743         || !flag_eliminate_unused_debug_types)
7744       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7745
7746   size += DWARF_OFFSET_SIZE;
7747   return size;
7748 }
7749
7750 /* Return the size of the information in the .debug_aranges section.  */
7751
7752 static unsigned long
7753 size_of_aranges (void)
7754 {
7755   unsigned long size;
7756
7757   size = DWARF_ARANGES_HEADER_SIZE;
7758
7759   /* Count the address/length pair for this compilation unit.  */
7760   if (text_section_used)
7761     size += 2 * DWARF2_ADDR_SIZE;
7762   if (cold_text_section_used)
7763     size += 2 * DWARF2_ADDR_SIZE;
7764   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7765
7766   /* Count the two zero words used to terminated the address range table.  */
7767   size += 2 * DWARF2_ADDR_SIZE;
7768   return size;
7769 }
7770 \f
7771 /* Select the encoding of an attribute value.  */
7772
7773 static enum dwarf_form
7774 value_format (dw_attr_ref a)
7775 {
7776   switch (a->dw_attr_val.val_class)
7777     {
7778     case dw_val_class_addr:
7779       return DW_FORM_addr;
7780     case dw_val_class_range_list:
7781     case dw_val_class_offset:
7782     case dw_val_class_loc_list:
7783       switch (DWARF_OFFSET_SIZE)
7784         {
7785         case 4:
7786           return DW_FORM_data4;
7787         case 8:
7788           return DW_FORM_data8;
7789         default:
7790           gcc_unreachable ();
7791         }
7792     case dw_val_class_loc:
7793       switch (constant_size (size_of_locs (AT_loc (a))))
7794         {
7795         case 1:
7796           return DW_FORM_block1;
7797         case 2:
7798           return DW_FORM_block2;
7799         default:
7800           gcc_unreachable ();
7801         }
7802     case dw_val_class_const:
7803       return DW_FORM_sdata;
7804     case dw_val_class_unsigned_const:
7805       switch (constant_size (AT_unsigned (a)))
7806         {
7807         case 1:
7808           return DW_FORM_data1;
7809         case 2:
7810           return DW_FORM_data2;
7811         case 4:
7812           return DW_FORM_data4;
7813         case 8:
7814           return DW_FORM_data8;
7815         default:
7816           gcc_unreachable ();
7817         }
7818     case dw_val_class_long_long:
7819       return DW_FORM_block1;
7820     case dw_val_class_vec:
7821       switch (constant_size (a->dw_attr_val.v.val_vec.length
7822                              * a->dw_attr_val.v.val_vec.elt_size))
7823         {
7824         case 1:
7825           return DW_FORM_block1;
7826         case 2:
7827           return DW_FORM_block2;
7828         case 4:
7829           return DW_FORM_block4;
7830         default:
7831           gcc_unreachable ();
7832         }
7833     case dw_val_class_flag:
7834       return DW_FORM_flag;
7835     case dw_val_class_die_ref:
7836       if (AT_ref_external (a))
7837         return DW_FORM_ref_addr;
7838       else
7839         return DW_FORM_ref;
7840     case dw_val_class_fde_ref:
7841       return DW_FORM_data;
7842     case dw_val_class_lbl_id:
7843       return DW_FORM_addr;
7844     case dw_val_class_lineptr:
7845     case dw_val_class_macptr:
7846       return DW_FORM_data;
7847     case dw_val_class_str:
7848       return AT_string_form (a);
7849     case dw_val_class_file:
7850       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7851         {
7852         case 1:
7853           return DW_FORM_data1;
7854         case 2:
7855           return DW_FORM_data2;
7856         case 4:
7857           return DW_FORM_data4;
7858         default:
7859           gcc_unreachable ();
7860         }
7861
7862     default:
7863       gcc_unreachable ();
7864     }
7865 }
7866
7867 /* Output the encoding of an attribute value.  */
7868
7869 static void
7870 output_value_format (dw_attr_ref a)
7871 {
7872   enum dwarf_form form = value_format (a);
7873
7874   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7875 }
7876
7877 /* Output the .debug_abbrev section which defines the DIE abbreviation
7878    table.  */
7879
7880 static void
7881 output_abbrev_section (void)
7882 {
7883   unsigned long abbrev_id;
7884
7885   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7886     {
7887       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7888       unsigned ix;
7889       dw_attr_ref a_attr;
7890
7891       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7892       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7893                                    dwarf_tag_name (abbrev->die_tag));
7894
7895       if (abbrev->die_child != NULL)
7896         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7897       else
7898         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7899
7900       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7901            ix++)
7902         {
7903           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7904                                        dwarf_attr_name (a_attr->dw_attr));
7905           output_value_format (a_attr);
7906         }
7907
7908       dw2_asm_output_data (1, 0, NULL);
7909       dw2_asm_output_data (1, 0, NULL);
7910     }
7911
7912   /* Terminate the table.  */
7913   dw2_asm_output_data (1, 0, NULL);
7914 }
7915
7916 /* Output a symbol we can use to refer to this DIE from another CU.  */
7917
7918 static inline void
7919 output_die_symbol (dw_die_ref die)
7920 {
7921   char *sym = die->die_symbol;
7922
7923   if (sym == 0)
7924     return;
7925
7926   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7927     /* We make these global, not weak; if the target doesn't support
7928        .linkonce, it doesn't support combining the sections, so debugging
7929        will break.  */
7930     targetm.asm_out.globalize_label (asm_out_file, sym);
7931
7932   ASM_OUTPUT_LABEL (asm_out_file, sym);
7933 }
7934
7935 /* Return a new location list, given the begin and end range, and the
7936    expression. gensym tells us whether to generate a new internal symbol for
7937    this location list node, which is done for the head of the list only.  */
7938
7939 static inline dw_loc_list_ref
7940 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7941               const char *section, unsigned int gensym)
7942 {
7943   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7944
7945   retlist->begin = begin;
7946   retlist->end = end;
7947   retlist->expr = expr;
7948   retlist->section = section;
7949   if (gensym)
7950     retlist->ll_symbol = gen_internal_sym ("LLST");
7951
7952   return retlist;
7953 }
7954
7955 /* Add a location description expression to a location list.  */
7956
7957 static inline void
7958 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7959                            const char *begin, const char *end,
7960                            const char *section)
7961 {
7962   dw_loc_list_ref *d;
7963
7964   /* Find the end of the chain.  */
7965   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7966     ;
7967
7968   /* Add a new location list node to the list.  */
7969   *d = new_loc_list (descr, begin, end, section, 0);
7970 }
7971
7972 /* Output the location list given to us.  */
7973
7974 static void
7975 output_loc_list (dw_loc_list_ref list_head)
7976 {
7977   dw_loc_list_ref curr = list_head;
7978
7979   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7980
7981   /* Walk the location list, and output each range + expression.  */
7982   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7983     {
7984       unsigned long size;
7985       /* Don't output an entry that starts and ends at the same address.  */
7986       if (strcmp (curr->begin, curr->end) == 0)
7987         continue;
7988       if (!have_multiple_function_sections)
7989         {
7990           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7991                                 "Location list begin address (%s)",
7992                                 list_head->ll_symbol);
7993           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7994                                 "Location list end address (%s)",
7995                                 list_head->ll_symbol);
7996         }
7997       else
7998         {
7999           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8000                                "Location list begin address (%s)",
8001                                list_head->ll_symbol);
8002           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8003                                "Location list end address (%s)",
8004                                list_head->ll_symbol);
8005         }
8006       size = size_of_locs (curr->expr);
8007
8008       /* Output the block length for this list of location operations.  */
8009       gcc_assert (size <= 0xffff);
8010       dw2_asm_output_data (2, size, "%s", "Location expression size");
8011
8012       output_loc_sequence (curr->expr);
8013     }
8014
8015   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8016                        "Location list terminator begin (%s)",
8017                        list_head->ll_symbol);
8018   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8019                        "Location list terminator end (%s)",
8020                        list_head->ll_symbol);
8021 }
8022
8023 /* Output the DIE and its attributes.  Called recursively to generate
8024    the definitions of each child DIE.  */
8025
8026 static void
8027 output_die (dw_die_ref die)
8028 {
8029   dw_attr_ref a;
8030   dw_die_ref c;
8031   unsigned long size;
8032   unsigned ix;
8033
8034   /* If someone in another CU might refer to us, set up a symbol for
8035      them to point to.  */
8036   if (die->die_symbol)
8037     output_die_symbol (die);
8038
8039   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8040                                (unsigned long)die->die_offset,
8041                                dwarf_tag_name (die->die_tag));
8042
8043   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8044     {
8045       const char *name = dwarf_attr_name (a->dw_attr);
8046
8047       switch (AT_class (a))
8048         {
8049         case dw_val_class_addr:
8050           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8051           break;
8052
8053         case dw_val_class_offset:
8054           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8055                                "%s", name);
8056           break;
8057
8058         case dw_val_class_range_list:
8059           {
8060             char *p = strchr (ranges_section_label, '\0');
8061
8062             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8063                      a->dw_attr_val.v.val_offset);
8064             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8065                                    debug_ranges_section, "%s", name);
8066             *p = '\0';
8067           }
8068           break;
8069
8070         case dw_val_class_loc:
8071           size = size_of_locs (AT_loc (a));
8072
8073           /* Output the block length for this list of location operations.  */
8074           dw2_asm_output_data (constant_size (size), size, "%s", name);
8075
8076           output_loc_sequence (AT_loc (a));
8077           break;
8078
8079         case dw_val_class_const:
8080           /* ??? It would be slightly more efficient to use a scheme like is
8081              used for unsigned constants below, but gdb 4.x does not sign
8082              extend.  Gdb 5.x does sign extend.  */
8083           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8084           break;
8085
8086         case dw_val_class_unsigned_const:
8087           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8088                                AT_unsigned (a), "%s", name);
8089           break;
8090
8091         case dw_val_class_long_long:
8092           {
8093             unsigned HOST_WIDE_INT first, second;
8094
8095             dw2_asm_output_data (1,
8096                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8097                                  "%s", name);
8098
8099             if (WORDS_BIG_ENDIAN)
8100               {
8101                 first = a->dw_attr_val.v.val_long_long.hi;
8102                 second = a->dw_attr_val.v.val_long_long.low;
8103               }
8104             else
8105               {
8106                 first = a->dw_attr_val.v.val_long_long.low;
8107                 second = a->dw_attr_val.v.val_long_long.hi;
8108               }
8109
8110             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8111                                  first, "long long constant");
8112             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8113                                  second, NULL);
8114           }
8115           break;
8116
8117         case dw_val_class_vec:
8118           {
8119             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8120             unsigned int len = a->dw_attr_val.v.val_vec.length;
8121             unsigned int i;
8122             unsigned char *p;
8123
8124             dw2_asm_output_data (constant_size (len * elt_size),
8125                                  len * elt_size, "%s", name);
8126             if (elt_size > sizeof (HOST_WIDE_INT))
8127               {
8128                 elt_size /= 2;
8129                 len *= 2;
8130               }
8131             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8132                  i < len;
8133                  i++, p += elt_size)
8134               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8135                                    "fp or vector constant word %u", i);
8136             break;
8137           }
8138
8139         case dw_val_class_flag:
8140           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8141           break;
8142
8143         case dw_val_class_loc_list:
8144           {
8145             char *sym = AT_loc_list (a)->ll_symbol;
8146
8147             gcc_assert (sym);
8148             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8149                                    "%s", name);
8150           }
8151           break;
8152
8153         case dw_val_class_die_ref:
8154           if (AT_ref_external (a))
8155             {
8156               char *sym = AT_ref (a)->die_symbol;
8157
8158               gcc_assert (sym);
8159               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8160                                      "%s", name);
8161             }
8162           else
8163             {
8164               gcc_assert (AT_ref (a)->die_offset);
8165               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8166                                    "%s", name);
8167             }
8168           break;
8169
8170         case dw_val_class_fde_ref:
8171           {
8172             char l1[20];
8173
8174             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8175                                          a->dw_attr_val.v.val_fde_index * 2);
8176             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8177                                    "%s", name);
8178           }
8179           break;
8180
8181         case dw_val_class_lbl_id:
8182           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8183           break;
8184
8185         case dw_val_class_lineptr:
8186           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8187                                  debug_line_section, "%s", name);
8188           break;
8189
8190         case dw_val_class_macptr:
8191           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8192                                  debug_macinfo_section, "%s", name);
8193           break;
8194
8195         case dw_val_class_str:
8196           if (AT_string_form (a) == DW_FORM_strp)
8197             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8198                                    a->dw_attr_val.v.val_str->label,
8199                                    debug_str_section,
8200                                    "%s: \"%s\"", name, AT_string (a));
8201           else
8202             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8203           break;
8204
8205         case dw_val_class_file:
8206           {
8207             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8208
8209             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8210                                  a->dw_attr_val.v.val_file->filename);
8211             break;
8212           }
8213
8214         default:
8215           gcc_unreachable ();
8216         }
8217     }
8218
8219   FOR_EACH_CHILD (die, c, output_die (c));
8220
8221   /* Add null byte to terminate sibling list.  */
8222   if (die->die_child != NULL)
8223     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8224                          (unsigned long) die->die_offset);
8225 }
8226
8227 /* Output the compilation unit that appears at the beginning of the
8228    .debug_info section, and precedes the DIE descriptions.  */
8229
8230 static void
8231 output_compilation_unit_header (void)
8232 {
8233   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8234     dw2_asm_output_data (4, 0xffffffff,
8235       "Initial length escape value indicating 64-bit DWARF extension");
8236   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8237                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8238                        "Length of Compilation Unit Info");
8239   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8240   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8241                          debug_abbrev_section,
8242                          "Offset Into Abbrev. Section");
8243   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8244 }
8245
8246 /* Output the compilation unit DIE and its children.  */
8247
8248 static void
8249 output_comp_unit (dw_die_ref die, int output_if_empty)
8250 {
8251   const char *secname;
8252   char *oldsym, *tmp;
8253
8254   /* Unless we are outputting main CU, we may throw away empty ones.  */
8255   if (!output_if_empty && die->die_child == NULL)
8256     return;
8257
8258   /* Even if there are no children of this DIE, we must output the information
8259      about the compilation unit.  Otherwise, on an empty translation unit, we
8260      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8261      will then complain when examining the file.  First mark all the DIEs in
8262      this CU so we know which get local refs.  */
8263   mark_dies (die);
8264
8265   build_abbrev_table (die);
8266
8267   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8268   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8269   calc_die_sizes (die);
8270
8271   oldsym = die->die_symbol;
8272   if (oldsym)
8273     {
8274       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8275
8276       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8277       secname = tmp;
8278       die->die_symbol = NULL;
8279       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8280     }
8281   else
8282     switch_to_section (debug_info_section);
8283
8284   /* Output debugging information.  */
8285   output_compilation_unit_header ();
8286   output_die (die);
8287
8288   /* Leave the marks on the main CU, so we can check them in
8289      output_pubnames.  */
8290   if (oldsym)
8291     {
8292       unmark_dies (die);
8293       die->die_symbol = oldsym;
8294     }
8295 }
8296
8297 /* Return the DWARF2/3 pubname associated with a decl.  */
8298
8299 static const char *
8300 dwarf2_name (tree decl, int scope)
8301 {
8302   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8303 }
8304
8305 /* Add a new entry to .debug_pubnames if appropriate.  */
8306
8307 static void
8308 add_pubname_string (const char *str, dw_die_ref die)
8309 {
8310   pubname_entry e;
8311
8312   e.die = die;
8313   e.name = xstrdup (str);
8314   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8315 }
8316
8317 static void
8318 add_pubname (tree decl, dw_die_ref die)
8319 {
8320
8321   if (TREE_PUBLIC (decl))
8322     add_pubname_string (dwarf2_name (decl, 1), die);
8323 }
8324
8325 /* Add a new entry to .debug_pubtypes if appropriate.  */
8326
8327 static void
8328 add_pubtype (tree decl, dw_die_ref die)
8329 {
8330   pubname_entry e;
8331
8332   e.name = NULL;
8333   if ((TREE_PUBLIC (decl)
8334        || die->die_parent == comp_unit_die)
8335       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8336     {
8337       e.die = die;
8338       if (TYPE_P (decl))
8339         {
8340           if (TYPE_NAME (decl))
8341             {
8342               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8343                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8344               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8345                        && DECL_NAME (TYPE_NAME (decl)))
8346                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8347               else
8348                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8349             }
8350         }
8351       else
8352         e.name = xstrdup (dwarf2_name (decl, 1));
8353
8354       /* If we don't have a name for the type, there's no point in adding
8355          it to the table.  */
8356       if (e.name && e.name[0] != '\0')
8357         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8358     }
8359 }
8360
8361 /* Output the public names table used to speed up access to externally
8362    visible names; or the public types table used to find type definitions.  */
8363
8364 static void
8365 output_pubnames (VEC (pubname_entry, gc) * names)
8366 {
8367   unsigned i;
8368   unsigned long pubnames_length = size_of_pubnames (names);
8369   pubname_ref pub;
8370
8371   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8372     dw2_asm_output_data (4, 0xffffffff,
8373       "Initial length escape value indicating 64-bit DWARF extension");
8374   if (names == pubname_table)
8375     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8376                          "Length of Public Names Info");
8377   else
8378     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8379                          "Length of Public Type Names Info");
8380   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8381   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8382                          debug_info_section,
8383                          "Offset of Compilation Unit Info");
8384   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8385                        "Compilation Unit Length");
8386
8387   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8388     {
8389       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8390       if (names == pubname_table)
8391         gcc_assert (pub->die->die_mark);
8392
8393       if (names != pubtype_table
8394           || pub->die->die_offset != 0
8395           || !flag_eliminate_unused_debug_types)
8396         {
8397           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8398                                "DIE offset");
8399
8400           dw2_asm_output_nstring (pub->name, -1, "external name");
8401         }
8402     }
8403
8404   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8405 }
8406
8407 /* Add a new entry to .debug_aranges if appropriate.  */
8408
8409 static void
8410 add_arange (tree decl, dw_die_ref die)
8411 {
8412   if (! DECL_SECTION_NAME (decl))
8413     return;
8414
8415   if (arange_table_in_use == arange_table_allocated)
8416     {
8417       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8418       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8419                                     arange_table_allocated);
8420       memset (arange_table + arange_table_in_use, 0,
8421               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8422     }
8423
8424   arange_table[arange_table_in_use++] = die;
8425 }
8426
8427 /* Output the information that goes into the .debug_aranges table.
8428    Namely, define the beginning and ending address range of the
8429    text section generated for this compilation unit.  */
8430
8431 static void
8432 output_aranges (void)
8433 {
8434   unsigned i;
8435   unsigned long aranges_length = size_of_aranges ();
8436
8437   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8438     dw2_asm_output_data (4, 0xffffffff,
8439       "Initial length escape value indicating 64-bit DWARF extension");
8440   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8441                        "Length of Address Ranges Info");
8442   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8443   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8444                          debug_info_section,
8445                          "Offset of Compilation Unit Info");
8446   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8447   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8448
8449   /* We need to align to twice the pointer size here.  */
8450   if (DWARF_ARANGES_PAD_SIZE)
8451     {
8452       /* Pad using a 2 byte words so that padding is correct for any
8453          pointer size.  */
8454       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8455                            2 * DWARF2_ADDR_SIZE);
8456       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8457         dw2_asm_output_data (2, 0, NULL);
8458     }
8459
8460   /* It is necessary not to output these entries if the sections were
8461      not used; if the sections were not used, the length will be 0 and
8462      the address may end up as 0 if the section is discarded by ld
8463      --gc-sections, leaving an invalid (0, 0) entry that can be
8464      confused with the terminator.  */
8465   if (text_section_used)
8466     {
8467       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8468       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8469                             text_section_label, "Length");
8470     }
8471   if (cold_text_section_used)
8472     {
8473       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8474                            "Address");
8475       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8476                             cold_text_section_label, "Length");
8477     }
8478
8479   for (i = 0; i < arange_table_in_use; i++)
8480     {
8481       dw_die_ref die = arange_table[i];
8482
8483       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8484       gcc_assert (die->die_mark);
8485
8486       if (die->die_tag == DW_TAG_subprogram)
8487         {
8488           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8489                                "Address");
8490           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8491                                 get_AT_low_pc (die), "Length");
8492         }
8493       else
8494         {
8495           /* A static variable; extract the symbol from DW_AT_location.
8496              Note that this code isn't currently hit, as we only emit
8497              aranges for functions (jason 9/23/99).  */
8498           dw_attr_ref a = get_AT (die, DW_AT_location);
8499           dw_loc_descr_ref loc;
8500
8501           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8502
8503           loc = AT_loc (a);
8504           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8505
8506           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8507                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8508           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8509                                get_AT_unsigned (die, DW_AT_byte_size),
8510                                "Length");
8511         }
8512     }
8513
8514   /* Output the terminator words.  */
8515   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8516   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8517 }
8518
8519 /* Add a new entry to .debug_ranges.  Return the offset at which it
8520    was placed.  */
8521
8522 static unsigned int
8523 add_ranges_num (int num)
8524 {
8525   unsigned int in_use = ranges_table_in_use;
8526
8527   if (in_use == ranges_table_allocated)
8528     {
8529       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8530       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8531                                     ranges_table_allocated);
8532       memset (ranges_table + ranges_table_in_use, 0,
8533               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8534     }
8535
8536   ranges_table[in_use].num = num;
8537   ranges_table_in_use = in_use + 1;
8538
8539   return in_use * 2 * DWARF2_ADDR_SIZE;
8540 }
8541
8542 /* Add a new entry to .debug_ranges corresponding to a block, or a
8543    range terminator if BLOCK is NULL.  */
8544
8545 static unsigned int
8546 add_ranges (const_tree block)
8547 {
8548   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8549 }
8550
8551 /* Add a new entry to .debug_ranges corresponding to a pair of
8552    labels.  */
8553
8554 static unsigned int
8555 add_ranges_by_labels (const char *begin, const char *end)
8556 {
8557   unsigned int in_use = ranges_by_label_in_use;
8558
8559   if (in_use == ranges_by_label_allocated)
8560     {
8561       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8562       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8563                                        ranges_by_label,
8564                                        ranges_by_label_allocated);
8565       memset (ranges_by_label + ranges_by_label_in_use, 0,
8566               RANGES_TABLE_INCREMENT
8567               * sizeof (struct dw_ranges_by_label_struct));
8568     }
8569
8570   ranges_by_label[in_use].begin = begin;
8571   ranges_by_label[in_use].end = end;
8572   ranges_by_label_in_use = in_use + 1;
8573
8574   return add_ranges_num (-(int)in_use - 1);
8575 }
8576
8577 static void
8578 output_ranges (void)
8579 {
8580   unsigned i;
8581   static const char *const start_fmt = "Offset 0x%x";
8582   const char *fmt = start_fmt;
8583
8584   for (i = 0; i < ranges_table_in_use; i++)
8585     {
8586       int block_num = ranges_table[i].num;
8587
8588       if (block_num > 0)
8589         {
8590           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8591           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8592
8593           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8594           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8595
8596           /* If all code is in the text section, then the compilation
8597              unit base address defaults to DW_AT_low_pc, which is the
8598              base of the text section.  */
8599           if (!have_multiple_function_sections)
8600             {
8601               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8602                                     text_section_label,
8603                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8604               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8605                                     text_section_label, NULL);
8606             }
8607
8608           /* Otherwise, the compilation unit base address is zero,
8609              which allows us to use absolute addresses, and not worry
8610              about whether the target supports cross-section
8611              arithmetic.  */
8612           else
8613             {
8614               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8615                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8616               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8617             }
8618
8619           fmt = NULL;
8620         }
8621
8622       /* Negative block_num stands for an index into ranges_by_label.  */
8623       else if (block_num < 0)
8624         {
8625           int lab_idx = - block_num - 1;
8626
8627           if (!have_multiple_function_sections)
8628             {
8629               gcc_unreachable ();
8630 #if 0
8631               /* If we ever use add_ranges_by_labels () for a single
8632                  function section, all we have to do is to take out
8633                  the #if 0 above.  */
8634               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8635                                     ranges_by_label[lab_idx].begin,
8636                                     text_section_label,
8637                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8638               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8639                                     ranges_by_label[lab_idx].end,
8640                                     text_section_label, NULL);
8641 #endif
8642             }
8643           else
8644             {
8645               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8646                                    ranges_by_label[lab_idx].begin,
8647                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8648               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8649                                    ranges_by_label[lab_idx].end,
8650                                    NULL);
8651             }
8652         }
8653       else
8654         {
8655           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8656           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8657           fmt = start_fmt;
8658         }
8659     }
8660 }
8661
8662 /* Data structure containing information about input files.  */
8663 struct file_info
8664 {
8665   const char *path;     /* Complete file name.  */
8666   const char *fname;    /* File name part.  */
8667   int length;           /* Length of entire string.  */
8668   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8669   int dir_idx;          /* Index in directory table.  */
8670 };
8671
8672 /* Data structure containing information about directories with source
8673    files.  */
8674 struct dir_info
8675 {
8676   const char *path;     /* Path including directory name.  */
8677   int length;           /* Path length.  */
8678   int prefix;           /* Index of directory entry which is a prefix.  */
8679   int count;            /* Number of files in this directory.  */
8680   int dir_idx;          /* Index of directory used as base.  */
8681 };
8682
8683 /* Callback function for file_info comparison.  We sort by looking at
8684    the directories in the path.  */
8685
8686 static int
8687 file_info_cmp (const void *p1, const void *p2)
8688 {
8689   const struct file_info *const s1 = (const struct file_info *) p1;
8690   const struct file_info *const s2 = (const struct file_info *) p2;
8691   const unsigned char *cp1;
8692   const unsigned char *cp2;
8693
8694   /* Take care of file names without directories.  We need to make sure that
8695      we return consistent values to qsort since some will get confused if
8696      we return the same value when identical operands are passed in opposite
8697      orders.  So if neither has a directory, return 0 and otherwise return
8698      1 or -1 depending on which one has the directory.  */
8699   if ((s1->path == s1->fname || s2->path == s2->fname))
8700     return (s2->path == s2->fname) - (s1->path == s1->fname);
8701
8702   cp1 = (const unsigned char *) s1->path;
8703   cp2 = (const unsigned char *) s2->path;
8704
8705   while (1)
8706     {
8707       ++cp1;
8708       ++cp2;
8709       /* Reached the end of the first path?  If so, handle like above.  */
8710       if ((cp1 == (const unsigned char *) s1->fname)
8711           || (cp2 == (const unsigned char *) s2->fname))
8712         return ((cp2 == (const unsigned char *) s2->fname)
8713                 - (cp1 == (const unsigned char *) s1->fname));
8714
8715       /* Character of current path component the same?  */
8716       else if (*cp1 != *cp2)
8717         return *cp1 - *cp2;
8718     }
8719 }
8720
8721 struct file_name_acquire_data
8722 {
8723   struct file_info *files;
8724   int used_files;
8725   int max_files;
8726 };
8727
8728 /* Traversal function for the hash table.  */
8729
8730 static int
8731 file_name_acquire (void ** slot, void *data)
8732 {
8733   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8734   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8735   struct file_info *fi;
8736   const char *f;
8737
8738   gcc_assert (fnad->max_files >= d->emitted_number);
8739
8740   if (! d->emitted_number)
8741     return 1;
8742
8743   gcc_assert (fnad->max_files != fnad->used_files);
8744
8745   fi = fnad->files + fnad->used_files++;
8746
8747   /* Skip all leading "./".  */
8748   f = d->filename;
8749   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8750     f += 2;
8751
8752   /* Create a new array entry.  */
8753   fi->path = f;
8754   fi->length = strlen (f);
8755   fi->file_idx = d;
8756
8757   /* Search for the file name part.  */
8758   f = strrchr (f, DIR_SEPARATOR);
8759 #if defined (DIR_SEPARATOR_2)
8760   {
8761     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8762
8763     if (g != NULL)
8764       {
8765         if (f == NULL || f < g)
8766           f = g;
8767       }
8768   }
8769 #endif
8770
8771   fi->fname = f == NULL ? fi->path : f + 1;
8772   return 1;
8773 }
8774
8775 /* Output the directory table and the file name table.  We try to minimize
8776    the total amount of memory needed.  A heuristic is used to avoid large
8777    slowdowns with many input files.  */
8778
8779 static void
8780 output_file_names (void)
8781 {
8782   struct file_name_acquire_data fnad;
8783   int numfiles;
8784   struct file_info *files;
8785   struct dir_info *dirs;
8786   int *saved;
8787   int *savehere;
8788   int *backmap;
8789   int ndirs;
8790   int idx_offset;
8791   int i;
8792   int idx;
8793
8794   if (!last_emitted_file)
8795     {
8796       dw2_asm_output_data (1, 0, "End directory table");
8797       dw2_asm_output_data (1, 0, "End file name table");
8798       return;
8799     }
8800
8801   numfiles = last_emitted_file->emitted_number;
8802
8803   /* Allocate the various arrays we need.  */
8804   files = XALLOCAVEC (struct file_info, numfiles);
8805   dirs = XALLOCAVEC (struct dir_info, numfiles);
8806
8807   fnad.files = files;
8808   fnad.used_files = 0;
8809   fnad.max_files = numfiles;
8810   htab_traverse (file_table, file_name_acquire, &fnad);
8811   gcc_assert (fnad.used_files == fnad.max_files);
8812
8813   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8814
8815   /* Find all the different directories used.  */
8816   dirs[0].path = files[0].path;
8817   dirs[0].length = files[0].fname - files[0].path;
8818   dirs[0].prefix = -1;
8819   dirs[0].count = 1;
8820   dirs[0].dir_idx = 0;
8821   files[0].dir_idx = 0;
8822   ndirs = 1;
8823
8824   for (i = 1; i < numfiles; i++)
8825     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8826         && memcmp (dirs[ndirs - 1].path, files[i].path,
8827                    dirs[ndirs - 1].length) == 0)
8828       {
8829         /* Same directory as last entry.  */
8830         files[i].dir_idx = ndirs - 1;
8831         ++dirs[ndirs - 1].count;
8832       }
8833     else
8834       {
8835         int j;
8836
8837         /* This is a new directory.  */
8838         dirs[ndirs].path = files[i].path;
8839         dirs[ndirs].length = files[i].fname - files[i].path;
8840         dirs[ndirs].count = 1;
8841         dirs[ndirs].dir_idx = ndirs;
8842         files[i].dir_idx = ndirs;
8843
8844         /* Search for a prefix.  */
8845         dirs[ndirs].prefix = -1;
8846         for (j = 0; j < ndirs; j++)
8847           if (dirs[j].length < dirs[ndirs].length
8848               && dirs[j].length > 1
8849               && (dirs[ndirs].prefix == -1
8850                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8851               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8852             dirs[ndirs].prefix = j;
8853
8854         ++ndirs;
8855       }
8856
8857   /* Now to the actual work.  We have to find a subset of the directories which
8858      allow expressing the file name using references to the directory table
8859      with the least amount of characters.  We do not do an exhaustive search
8860      where we would have to check out every combination of every single
8861      possible prefix.  Instead we use a heuristic which provides nearly optimal
8862      results in most cases and never is much off.  */
8863   saved = XALLOCAVEC (int, ndirs);
8864   savehere = XALLOCAVEC (int, ndirs);
8865
8866   memset (saved, '\0', ndirs * sizeof (saved[0]));
8867   for (i = 0; i < ndirs; i++)
8868     {
8869       int j;
8870       int total;
8871
8872       /* We can always save some space for the current directory.  But this
8873          does not mean it will be enough to justify adding the directory.  */
8874       savehere[i] = dirs[i].length;
8875       total = (savehere[i] - saved[i]) * dirs[i].count;
8876
8877       for (j = i + 1; j < ndirs; j++)
8878         {
8879           savehere[j] = 0;
8880           if (saved[j] < dirs[i].length)
8881             {
8882               /* Determine whether the dirs[i] path is a prefix of the
8883                  dirs[j] path.  */
8884               int k;
8885
8886               k = dirs[j].prefix;
8887               while (k != -1 && k != (int) i)
8888                 k = dirs[k].prefix;
8889
8890               if (k == (int) i)
8891                 {
8892                   /* Yes it is.  We can possibly save some memory by
8893                      writing the filenames in dirs[j] relative to
8894                      dirs[i].  */
8895                   savehere[j] = dirs[i].length;
8896                   total += (savehere[j] - saved[j]) * dirs[j].count;
8897                 }
8898             }
8899         }
8900
8901       /* Check whether we can save enough to justify adding the dirs[i]
8902          directory.  */
8903       if (total > dirs[i].length + 1)
8904         {
8905           /* It's worthwhile adding.  */
8906           for (j = i; j < ndirs; j++)
8907             if (savehere[j] > 0)
8908               {
8909                 /* Remember how much we saved for this directory so far.  */
8910                 saved[j] = savehere[j];
8911
8912                 /* Remember the prefix directory.  */
8913                 dirs[j].dir_idx = i;
8914               }
8915         }
8916     }
8917
8918   /* Emit the directory name table.  */
8919   idx = 1;
8920   idx_offset = dirs[0].length > 0 ? 1 : 0;
8921   for (i = 1 - idx_offset; i < ndirs; i++)
8922     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8923                             "Directory Entry: 0x%x", i + idx_offset);
8924
8925   dw2_asm_output_data (1, 0, "End directory table");
8926
8927   /* We have to emit them in the order of emitted_number since that's
8928      used in the debug info generation.  To do this efficiently we
8929      generate a back-mapping of the indices first.  */
8930   backmap = XALLOCAVEC (int, numfiles);
8931   for (i = 0; i < numfiles; i++)
8932     backmap[files[i].file_idx->emitted_number - 1] = i;
8933
8934   /* Now write all the file names.  */
8935   for (i = 0; i < numfiles; i++)
8936     {
8937       int file_idx = backmap[i];
8938       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8939
8940       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8941                               "File Entry: 0x%x", (unsigned) i + 1);
8942
8943       /* Include directory index.  */
8944       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8945
8946       /* Modification time.  */
8947       dw2_asm_output_data_uleb128 (0, NULL);
8948
8949       /* File length in bytes.  */
8950       dw2_asm_output_data_uleb128 (0, NULL);
8951     }
8952
8953   dw2_asm_output_data (1, 0, "End file name table");
8954 }
8955
8956
8957 /* Output the source line number correspondence information.  This
8958    information goes into the .debug_line section.  */
8959
8960 static void
8961 output_line_info (void)
8962 {
8963   char l1[20], l2[20], p1[20], p2[20];
8964   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8965   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8966   unsigned opc;
8967   unsigned n_op_args;
8968   unsigned long lt_index;
8969   unsigned long current_line;
8970   long line_offset;
8971   long line_delta;
8972   unsigned long current_file;
8973   unsigned long function;
8974
8975   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8976   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8977   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8978   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8979
8980   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8981     dw2_asm_output_data (4, 0xffffffff,
8982       "Initial length escape value indicating 64-bit DWARF extension");
8983   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8984                         "Length of Source Line Info");
8985   ASM_OUTPUT_LABEL (asm_out_file, l1);
8986
8987   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8988   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8989   ASM_OUTPUT_LABEL (asm_out_file, p1);
8990
8991   /* Define the architecture-dependent minimum instruction length (in
8992    bytes).  In this implementation of DWARF, this field is used for
8993    information purposes only.  Since GCC generates assembly language,
8994    we have no a priori knowledge of how many instruction bytes are
8995    generated for each source line, and therefore can use only the
8996    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8997    commands.  Accordingly, we fix this as `1', which is "correct
8998    enough" for all architectures, and don't let the target override.  */
8999   dw2_asm_output_data (1, 1,
9000                        "Minimum Instruction Length");
9001
9002   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9003                        "Default is_stmt_start flag");
9004   dw2_asm_output_data (1, DWARF_LINE_BASE,
9005                        "Line Base Value (Special Opcodes)");
9006   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9007                        "Line Range Value (Special Opcodes)");
9008   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9009                        "Special Opcode Base");
9010
9011   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9012     {
9013       switch (opc)
9014         {
9015         case DW_LNS_advance_pc:
9016         case DW_LNS_advance_line:
9017         case DW_LNS_set_file:
9018         case DW_LNS_set_column:
9019         case DW_LNS_fixed_advance_pc:
9020           n_op_args = 1;
9021           break;
9022         default:
9023           n_op_args = 0;
9024           break;
9025         }
9026
9027       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9028                            opc, n_op_args);
9029     }
9030
9031   /* Write out the information about the files we use.  */
9032   output_file_names ();
9033   ASM_OUTPUT_LABEL (asm_out_file, p2);
9034
9035   /* We used to set the address register to the first location in the text
9036      section here, but that didn't accomplish anything since we already
9037      have a line note for the opening brace of the first function.  */
9038
9039   /* Generate the line number to PC correspondence table, encoded as
9040      a series of state machine operations.  */
9041   current_file = 1;
9042   current_line = 1;
9043
9044   if (cfun && in_cold_section_p)
9045     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9046   else
9047     strcpy (prev_line_label, text_section_label);
9048   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9049     {
9050       dw_line_info_ref line_info = &line_info_table[lt_index];
9051
9052 #if 0
9053       /* Disable this optimization for now; GDB wants to see two line notes
9054          at the beginning of a function so it can find the end of the
9055          prologue.  */
9056
9057       /* Don't emit anything for redundant notes.  Just updating the
9058          address doesn't accomplish anything, because we already assume
9059          that anything after the last address is this line.  */
9060       if (line_info->dw_line_num == current_line
9061           && line_info->dw_file_num == current_file)
9062         continue;
9063 #endif
9064
9065       /* Emit debug info for the address of the current line.
9066
9067          Unfortunately, we have little choice here currently, and must always
9068          use the most general form.  GCC does not know the address delta
9069          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9070          attributes which will give an upper bound on the address range.  We
9071          could perhaps use length attributes to determine when it is safe to
9072          use DW_LNS_fixed_advance_pc.  */
9073
9074       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9075       if (0)
9076         {
9077           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9078           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9079                                "DW_LNS_fixed_advance_pc");
9080           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9081         }
9082       else
9083         {
9084           /* This can handle any delta.  This takes
9085              4+DWARF2_ADDR_SIZE bytes.  */
9086           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9087           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9088           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9089           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9090         }
9091
9092       strcpy (prev_line_label, line_label);
9093
9094       /* Emit debug info for the source file of the current line, if
9095          different from the previous line.  */
9096       if (line_info->dw_file_num != current_file)
9097         {
9098           current_file = line_info->dw_file_num;
9099           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9100           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9101         }
9102
9103       /* Emit debug info for the current line number, choosing the encoding
9104          that uses the least amount of space.  */
9105       if (line_info->dw_line_num != current_line)
9106         {
9107           line_offset = line_info->dw_line_num - current_line;
9108           line_delta = line_offset - DWARF_LINE_BASE;
9109           current_line = line_info->dw_line_num;
9110           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9111             /* This can handle deltas from -10 to 234, using the current
9112                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9113                takes 1 byte.  */
9114             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9115                                  "line %lu", current_line);
9116           else
9117             {
9118               /* This can handle any delta.  This takes at least 4 bytes,
9119                  depending on the value being encoded.  */
9120               dw2_asm_output_data (1, DW_LNS_advance_line,
9121                                    "advance to line %lu", current_line);
9122               dw2_asm_output_data_sleb128 (line_offset, NULL);
9123               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9124             }
9125         }
9126       else
9127         /* We still need to start a new row, so output a copy insn.  */
9128         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9129     }
9130
9131   /* Emit debug info for the address of the end of the function.  */
9132   if (0)
9133     {
9134       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9135                            "DW_LNS_fixed_advance_pc");
9136       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9137     }
9138   else
9139     {
9140       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9141       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9142       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9143       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9144     }
9145
9146   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9147   dw2_asm_output_data_uleb128 (1, NULL);
9148   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9149
9150   function = 0;
9151   current_file = 1;
9152   current_line = 1;
9153   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9154     {
9155       dw_separate_line_info_ref line_info
9156         = &separate_line_info_table[lt_index];
9157
9158 #if 0
9159       /* Don't emit anything for redundant notes.  */
9160       if (line_info->dw_line_num == current_line
9161           && line_info->dw_file_num == current_file
9162           && line_info->function == function)
9163         goto cont;
9164 #endif
9165
9166       /* Emit debug info for the address of the current line.  If this is
9167          a new function, or the first line of a function, then we need
9168          to handle it differently.  */
9169       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9170                                    lt_index);
9171       if (function != line_info->function)
9172         {
9173           function = line_info->function;
9174
9175           /* Set the address register to the first line in the function.  */
9176           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9177           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9178           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9179           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9180         }
9181       else
9182         {
9183           /* ??? See the DW_LNS_advance_pc comment above.  */
9184           if (0)
9185             {
9186               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9187                                    "DW_LNS_fixed_advance_pc");
9188               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9189             }
9190           else
9191             {
9192               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9193               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9194               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9195               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9196             }
9197         }
9198
9199       strcpy (prev_line_label, line_label);
9200
9201       /* Emit debug info for the source file of the current line, if
9202          different from the previous line.  */
9203       if (line_info->dw_file_num != current_file)
9204         {
9205           current_file = line_info->dw_file_num;
9206           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9207           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9208         }
9209
9210       /* Emit debug info for the current line number, choosing the encoding
9211          that uses the least amount of space.  */
9212       if (line_info->dw_line_num != current_line)
9213         {
9214           line_offset = line_info->dw_line_num - current_line;
9215           line_delta = line_offset - DWARF_LINE_BASE;
9216           current_line = line_info->dw_line_num;
9217           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9218             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9219                                  "line %lu", current_line);
9220           else
9221             {
9222               dw2_asm_output_data (1, DW_LNS_advance_line,
9223                                    "advance to line %lu", current_line);
9224               dw2_asm_output_data_sleb128 (line_offset, NULL);
9225               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9226             }
9227         }
9228       else
9229         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9230
9231 #if 0
9232     cont:
9233 #endif
9234
9235       lt_index++;
9236
9237       /* If we're done with a function, end its sequence.  */
9238       if (lt_index == separate_line_info_table_in_use
9239           || separate_line_info_table[lt_index].function != function)
9240         {
9241           current_file = 1;
9242           current_line = 1;
9243
9244           /* Emit debug info for the address of the end of the function.  */
9245           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9246           if (0)
9247             {
9248               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9249                                    "DW_LNS_fixed_advance_pc");
9250               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9251             }
9252           else
9253             {
9254               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9255               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9256               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9257               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9258             }
9259
9260           /* Output the marker for the end of this sequence.  */
9261           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9262           dw2_asm_output_data_uleb128 (1, NULL);
9263           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9264         }
9265     }
9266
9267   /* Output the marker for the end of the line number info.  */
9268   ASM_OUTPUT_LABEL (asm_out_file, l2);
9269 }
9270 \f
9271 /* Given a pointer to a tree node for some base type, return a pointer to
9272    a DIE that describes the given type.
9273
9274    This routine must only be called for GCC type nodes that correspond to
9275    Dwarf base (fundamental) types.  */
9276
9277 static dw_die_ref
9278 base_type_die (tree type)
9279 {
9280   dw_die_ref base_type_result;
9281   enum dwarf_type encoding;
9282
9283   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9284     return 0;
9285
9286   switch (TREE_CODE (type))
9287     {
9288     case INTEGER_TYPE:
9289       if (TYPE_STRING_FLAG (type))
9290         {
9291           if (TYPE_UNSIGNED (type))
9292             encoding = DW_ATE_unsigned_char;
9293           else
9294             encoding = DW_ATE_signed_char;
9295         }
9296       else if (TYPE_UNSIGNED (type))
9297         encoding = DW_ATE_unsigned;
9298       else
9299         encoding = DW_ATE_signed;
9300       break;
9301
9302     case REAL_TYPE:
9303       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9304         encoding = DW_ATE_decimal_float;
9305       else
9306         encoding = DW_ATE_float;
9307       break;
9308
9309     case FIXED_POINT_TYPE:
9310       if (TYPE_UNSIGNED (type))
9311         encoding = DW_ATE_unsigned_fixed;
9312       else
9313         encoding = DW_ATE_signed_fixed;
9314       break;
9315
9316       /* Dwarf2 doesn't know anything about complex ints, so use
9317          a user defined type for it.  */
9318     case COMPLEX_TYPE:
9319       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9320         encoding = DW_ATE_complex_float;
9321       else
9322         encoding = DW_ATE_lo_user;
9323       break;
9324
9325     case BOOLEAN_TYPE:
9326       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9327       encoding = DW_ATE_boolean;
9328       break;
9329
9330     default:
9331       /* No other TREE_CODEs are Dwarf fundamental types.  */
9332       gcc_unreachable ();
9333     }
9334
9335   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9336
9337   /* This probably indicates a bug.  */
9338   if (! TYPE_NAME (type))
9339     add_name_attribute (base_type_result, "__unknown__");
9340
9341   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9342                    int_size_in_bytes (type));
9343   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9344
9345   return base_type_result;
9346 }
9347
9348 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9349    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9350
9351 static inline int
9352 is_base_type (tree type)
9353 {
9354   switch (TREE_CODE (type))
9355     {
9356     case ERROR_MARK:
9357     case VOID_TYPE:
9358     case INTEGER_TYPE:
9359     case REAL_TYPE:
9360     case FIXED_POINT_TYPE:
9361     case COMPLEX_TYPE:
9362     case BOOLEAN_TYPE:
9363       return 1;
9364
9365     case ARRAY_TYPE:
9366     case RECORD_TYPE:
9367     case UNION_TYPE:
9368     case QUAL_UNION_TYPE:
9369     case ENUMERAL_TYPE:
9370     case FUNCTION_TYPE:
9371     case METHOD_TYPE:
9372     case POINTER_TYPE:
9373     case REFERENCE_TYPE:
9374     case OFFSET_TYPE:
9375     case LANG_TYPE:
9376     case VECTOR_TYPE:
9377       return 0;
9378
9379     default:
9380       gcc_unreachable ();
9381     }
9382
9383   return 0;
9384 }
9385
9386 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9387    node, return the size in bits for the type if it is a constant, or else
9388    return the alignment for the type if the type's size is not constant, or
9389    else return BITS_PER_WORD if the type actually turns out to be an
9390    ERROR_MARK node.  */
9391
9392 static inline unsigned HOST_WIDE_INT
9393 simple_type_size_in_bits (const_tree type)
9394 {
9395   if (TREE_CODE (type) == ERROR_MARK)
9396     return BITS_PER_WORD;
9397   else if (TYPE_SIZE (type) == NULL_TREE)
9398     return 0;
9399   else if (host_integerp (TYPE_SIZE (type), 1))
9400     return tree_low_cst (TYPE_SIZE (type), 1);
9401   else
9402     return TYPE_ALIGN (type);
9403 }
9404
9405 /* Return true if the debug information for the given type should be
9406    emitted as a subrange type.  */
9407
9408 static inline bool
9409 is_subrange_type (const_tree type)
9410 {
9411   tree subtype = TREE_TYPE (type);
9412
9413   /* Subrange types are identified by the fact that they are integer
9414      types, and that they have a subtype which is either an integer type
9415      or an enumeral type.  */
9416
9417   if (TREE_CODE (type) != INTEGER_TYPE
9418       || subtype == NULL_TREE)
9419     return false;
9420
9421   if (TREE_CODE (subtype) != INTEGER_TYPE
9422       && TREE_CODE (subtype) != ENUMERAL_TYPE
9423       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9424     return false;
9425
9426   if (TREE_CODE (type) == TREE_CODE (subtype)
9427       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9428       && TYPE_MIN_VALUE (type) != NULL
9429       && TYPE_MIN_VALUE (subtype) != NULL
9430       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9431       && TYPE_MAX_VALUE (type) != NULL
9432       && TYPE_MAX_VALUE (subtype) != NULL
9433       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9434     {
9435       /* The type and its subtype have the same representation.  If in
9436          addition the two types also have the same name, then the given
9437          type is not a subrange type, but rather a plain base type.  */
9438       /* FIXME: brobecker/2004-03-22:
9439          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9440          therefore be sufficient to check the TYPE_SIZE node pointers
9441          rather than checking the actual size.  Unfortunately, we have
9442          found some cases, such as in the Ada "integer" type, where
9443          this is not the case.  Until this problem is solved, we need to
9444          keep checking the actual size.  */
9445       tree type_name = TYPE_NAME (type);
9446       tree subtype_name = TYPE_NAME (subtype);
9447
9448       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9449         type_name = DECL_NAME (type_name);
9450
9451       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9452         subtype_name = DECL_NAME (subtype_name);
9453
9454       if (type_name == subtype_name)
9455         return false;
9456     }
9457
9458   return true;
9459 }
9460
9461 /*  Given a pointer to a tree node for a subrange type, return a pointer
9462     to a DIE that describes the given type.  */
9463
9464 static dw_die_ref
9465 subrange_type_die (tree type, dw_die_ref context_die)
9466 {
9467   dw_die_ref subrange_die;
9468   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9469
9470   if (context_die == NULL)
9471     context_die = comp_unit_die;
9472
9473   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9474
9475   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9476     {
9477       /* The size of the subrange type and its base type do not match,
9478          so we need to generate a size attribute for the subrange type.  */
9479       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9480     }
9481
9482   if (TYPE_MIN_VALUE (type) != NULL)
9483     add_bound_info (subrange_die, DW_AT_lower_bound,
9484                     TYPE_MIN_VALUE (type));
9485   if (TYPE_MAX_VALUE (type) != NULL)
9486     add_bound_info (subrange_die, DW_AT_upper_bound,
9487                     TYPE_MAX_VALUE (type));
9488
9489   return subrange_die;
9490 }
9491
9492 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9493    entry that chains various modifiers in front of the given type.  */
9494
9495 static dw_die_ref
9496 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9497                    dw_die_ref context_die)
9498 {
9499   enum tree_code code = TREE_CODE (type);
9500   dw_die_ref mod_type_die;
9501   dw_die_ref sub_die = NULL;
9502   tree item_type = NULL;
9503   tree qualified_type;
9504   tree name;
9505
9506   if (code == ERROR_MARK)
9507     return NULL;
9508
9509   /* See if we already have the appropriately qualified variant of
9510      this type.  */
9511   qualified_type
9512     = get_qualified_type (type,
9513                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9514                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9515
9516   /* If we do, then we can just use its DIE, if it exists.  */
9517   if (qualified_type)
9518     {
9519       mod_type_die = lookup_type_die (qualified_type);
9520       if (mod_type_die)
9521         return mod_type_die;
9522     }
9523
9524   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9525
9526   /* Handle C typedef types.  */
9527   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9528     {
9529       tree dtype = TREE_TYPE (name);
9530
9531       if (qualified_type == dtype)
9532         {
9533           /* For a named type, use the typedef.  */
9534           gen_type_die (qualified_type, context_die);
9535           return lookup_type_die (qualified_type);
9536         }
9537       else if (is_const_type < TYPE_READONLY (dtype)
9538                || is_volatile_type < TYPE_VOLATILE (dtype)
9539                || (is_const_type <= TYPE_READONLY (dtype)
9540                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9541                    && DECL_ORIGINAL_TYPE (name) != type))
9542         /* cv-unqualified version of named type.  Just use the unnamed
9543            type to which it refers.  */
9544         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9545                                   is_const_type, is_volatile_type,
9546                                   context_die);
9547       /* Else cv-qualified version of named type; fall through.  */
9548     }
9549
9550   if (is_const_type)
9551     {
9552       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9553       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9554     }
9555   else if (is_volatile_type)
9556     {
9557       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9558       sub_die = modified_type_die (type, 0, 0, context_die);
9559     }
9560   else if (code == POINTER_TYPE)
9561     {
9562       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9563       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9564                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9565       item_type = TREE_TYPE (type);
9566     }
9567   else if (code == REFERENCE_TYPE)
9568     {
9569       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9570       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9571                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9572       item_type = TREE_TYPE (type);
9573     }
9574   else if (is_subrange_type (type))
9575     {
9576       mod_type_die = subrange_type_die (type, context_die);
9577       item_type = TREE_TYPE (type);
9578     }
9579   else if (is_base_type (type))
9580     mod_type_die = base_type_die (type);
9581   else
9582     {
9583       gen_type_die (type, context_die);
9584
9585       /* We have to get the type_main_variant here (and pass that to the
9586          `lookup_type_die' routine) because the ..._TYPE node we have
9587          might simply be a *copy* of some original type node (where the
9588          copy was created to help us keep track of typedef names) and
9589          that copy might have a different TYPE_UID from the original
9590          ..._TYPE node.  */
9591       if (TREE_CODE (type) != VECTOR_TYPE)
9592         return lookup_type_die (type_main_variant (type));
9593       else
9594         /* Vectors have the debugging information in the type,
9595            not the main variant.  */
9596         return lookup_type_die (type);
9597     }
9598
9599   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9600      don't output a DW_TAG_typedef, since there isn't one in the
9601      user's program; just attach a DW_AT_name to the type.  */
9602   if (name
9603       && (TREE_CODE (name) != TYPE_DECL
9604           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9605     {
9606       if (TREE_CODE (name) == TYPE_DECL)
9607         /* Could just call add_name_and_src_coords_attributes here,
9608            but since this is a builtin type it doesn't have any
9609            useful source coordinates anyway.  */
9610         name = DECL_NAME (name);
9611       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9612     }
9613
9614   if (qualified_type)
9615     equate_type_number_to_die (qualified_type, mod_type_die);
9616
9617   if (item_type)
9618     /* We must do this after the equate_type_number_to_die call, in case
9619        this is a recursive type.  This ensures that the modified_type_die
9620        recursion will terminate even if the type is recursive.  Recursive
9621        types are possible in Ada.  */
9622     sub_die = modified_type_die (item_type,
9623                                  TYPE_READONLY (item_type),
9624                                  TYPE_VOLATILE (item_type),
9625                                  context_die);
9626
9627   if (sub_die != NULL)
9628     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9629
9630   return mod_type_die;
9631 }
9632
9633 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9634    an enumerated type.  */
9635
9636 static inline int
9637 type_is_enum (const_tree type)
9638 {
9639   return TREE_CODE (type) == ENUMERAL_TYPE;
9640 }
9641
9642 /* Return the DBX register number described by a given RTL node.  */
9643
9644 static unsigned int
9645 dbx_reg_number (const_rtx rtl)
9646 {
9647   unsigned regno = REGNO (rtl);
9648
9649   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9650
9651 #ifdef LEAF_REG_REMAP
9652   if (current_function_uses_only_leaf_regs)
9653     {
9654       int leaf_reg = LEAF_REG_REMAP (regno);
9655       if (leaf_reg != -1)
9656         regno = (unsigned) leaf_reg;
9657     }
9658 #endif
9659
9660   return DBX_REGISTER_NUMBER (regno);
9661 }
9662
9663 /* Optionally add a DW_OP_piece term to a location description expression.
9664    DW_OP_piece is only added if the location description expression already
9665    doesn't end with DW_OP_piece.  */
9666
9667 static void
9668 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9669 {
9670   dw_loc_descr_ref loc;
9671
9672   if (*list_head != NULL)
9673     {
9674       /* Find the end of the chain.  */
9675       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9676         ;
9677
9678       if (loc->dw_loc_opc != DW_OP_piece)
9679         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9680     }
9681 }
9682
9683 /* Return a location descriptor that designates a machine register or
9684    zero if there is none.  */
9685
9686 static dw_loc_descr_ref
9687 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9688 {
9689   rtx regs;
9690
9691   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9692     return 0;
9693
9694   regs = targetm.dwarf_register_span (rtl);
9695
9696   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9697     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9698   else
9699     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9700 }
9701
9702 /* Return a location descriptor that designates a machine register for
9703    a given hard register number.  */
9704
9705 static dw_loc_descr_ref
9706 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9707 {
9708   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9709
9710   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9711     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9712
9713   return reg_loc_descr;
9714 }
9715
9716 /* Given an RTL of a register, return a location descriptor that
9717    designates a value that spans more than one register.  */
9718
9719 static dw_loc_descr_ref
9720 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9721                              enum var_init_status initialized)
9722 {
9723   int nregs, size, i;
9724   unsigned reg;
9725   dw_loc_descr_ref loc_result = NULL;
9726
9727   reg = REGNO (rtl);
9728 #ifdef LEAF_REG_REMAP
9729   if (current_function_uses_only_leaf_regs)
9730     {
9731       int leaf_reg = LEAF_REG_REMAP (reg);
9732       if (leaf_reg != -1)
9733         reg = (unsigned) leaf_reg;
9734     }
9735 #endif
9736   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9737   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9738
9739   /* Simple, contiguous registers.  */
9740   if (regs == NULL_RTX)
9741     {
9742       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9743
9744       loc_result = NULL;
9745       while (nregs--)
9746         {
9747           dw_loc_descr_ref t;
9748
9749           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9750                                       VAR_INIT_STATUS_INITIALIZED);
9751           add_loc_descr (&loc_result, t);
9752           add_loc_descr_op_piece (&loc_result, size);
9753           ++reg;
9754         }
9755       return loc_result;
9756     }
9757
9758   /* Now onto stupid register sets in non contiguous locations.  */
9759
9760   gcc_assert (GET_CODE (regs) == PARALLEL);
9761
9762   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9763   loc_result = NULL;
9764
9765   for (i = 0; i < XVECLEN (regs, 0); ++i)
9766     {
9767       dw_loc_descr_ref t;
9768
9769       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9770                                   VAR_INIT_STATUS_INITIALIZED);
9771       add_loc_descr (&loc_result, t);
9772       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9773       add_loc_descr_op_piece (&loc_result, size);
9774     }
9775
9776   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9777     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9778   return loc_result;
9779 }
9780
9781 #endif /* DWARF2_DEBUGGING_INFO */
9782
9783 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9784
9785 /* Return a location descriptor that designates a constant.  */
9786
9787 static dw_loc_descr_ref
9788 int_loc_descriptor (HOST_WIDE_INT i)
9789 {
9790   enum dwarf_location_atom op;
9791
9792   /* Pick the smallest representation of a constant, rather than just
9793      defaulting to the LEB encoding.  */
9794   if (i >= 0)
9795     {
9796       if (i <= 31)
9797         op = DW_OP_lit0 + i;
9798       else if (i <= 0xff)
9799         op = DW_OP_const1u;
9800       else if (i <= 0xffff)
9801         op = DW_OP_const2u;
9802       else if (HOST_BITS_PER_WIDE_INT == 32
9803                || i <= 0xffffffff)
9804         op = DW_OP_const4u;
9805       else
9806         op = DW_OP_constu;
9807     }
9808   else
9809     {
9810       if (i >= -0x80)
9811         op = DW_OP_const1s;
9812       else if (i >= -0x8000)
9813         op = DW_OP_const2s;
9814       else if (HOST_BITS_PER_WIDE_INT == 32
9815                || i >= -0x80000000)
9816         op = DW_OP_const4s;
9817       else
9818         op = DW_OP_consts;
9819     }
9820
9821   return new_loc_descr (op, i, 0);
9822 }
9823 #endif
9824
9825 #ifdef DWARF2_DEBUGGING_INFO
9826
9827 /* Return a location descriptor that designates a base+offset location.  */
9828
9829 static dw_loc_descr_ref
9830 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9831                  enum var_init_status initialized)
9832 {
9833   unsigned int regno;
9834   dw_loc_descr_ref result;
9835   dw_fde_ref fde = current_fde ();
9836
9837   /* We only use "frame base" when we're sure we're talking about the
9838      post-prologue local stack frame.  We do this by *not* running
9839      register elimination until this point, and recognizing the special
9840      argument pointer and soft frame pointer rtx's.  */
9841   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9842     {
9843       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9844
9845       if (elim != reg)
9846         {
9847           if (GET_CODE (elim) == PLUS)
9848             {
9849               offset += INTVAL (XEXP (elim, 1));
9850               elim = XEXP (elim, 0);
9851             }
9852           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9853                        && (elim == hard_frame_pointer_rtx
9854                            || elim == stack_pointer_rtx))
9855                       || elim == (frame_pointer_needed
9856                                   ? hard_frame_pointer_rtx
9857                                   : stack_pointer_rtx));
9858
9859           /* If drap register is used to align stack, use frame
9860              pointer + offset to access stack variables.  If stack
9861              is aligned without drap, use stack pointer + offset to
9862              access stack variables.  */
9863           if (crtl->stack_realign_tried
9864               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9865               && reg == frame_pointer_rtx)
9866             {
9867               int base_reg
9868                 = DWARF_FRAME_REGNUM (cfa.indirect
9869                                       ? HARD_FRAME_POINTER_REGNUM
9870                                       : STACK_POINTER_REGNUM);
9871               return new_reg_loc_descr (base_reg, offset);
9872             }
9873
9874           offset += frame_pointer_fb_offset;
9875           return new_loc_descr (DW_OP_fbreg, offset, 0);
9876         }
9877     }
9878   else if (fde
9879            && fde->drap_reg != INVALID_REGNUM
9880            && (fde->drap_reg == REGNO (reg)
9881                || fde->vdrap_reg == REGNO (reg)))
9882     {
9883       /* Use cfa+offset to represent the location of arguments passed
9884          on stack when drap is used to align stack.  */
9885       return new_loc_descr (DW_OP_fbreg, offset, 0);
9886     }
9887
9888   regno = dbx_reg_number (reg);
9889   if (regno <= 31)
9890     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9891   else
9892     result = new_loc_descr (DW_OP_bregx, regno, offset);
9893
9894   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9895     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9896
9897   return result;
9898 }
9899
9900 /* Return true if this RTL expression describes a base+offset calculation.  */
9901
9902 static inline int
9903 is_based_loc (const_rtx rtl)
9904 {
9905   return (GET_CODE (rtl) == PLUS
9906           && ((REG_P (XEXP (rtl, 0))
9907                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9908                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9909 }
9910
9911 /* Return a descriptor that describes the concatenation of N locations
9912    used to form the address of a memory location.  */
9913
9914 static dw_loc_descr_ref
9915 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9916                             enum var_init_status initialized)
9917 {
9918   unsigned int i;
9919   dw_loc_descr_ref cc_loc_result = NULL;
9920   unsigned int n = XVECLEN (concatn, 0);
9921
9922   for (i = 0; i < n; ++i)
9923     {
9924       dw_loc_descr_ref ref;
9925       rtx x = XVECEXP (concatn, 0, i);
9926
9927       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9928       if (ref == NULL)
9929         return NULL;
9930
9931       add_loc_descr (&cc_loc_result, ref);
9932       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9933     }
9934
9935   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9936     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9937
9938   return cc_loc_result;
9939 }
9940
9941 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9942    failed.  */
9943
9944 static dw_loc_descr_ref
9945 tls_mem_loc_descriptor (rtx mem)
9946 {
9947   tree base;
9948   dw_loc_descr_ref loc_result, loc_result2;
9949
9950   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9951     return NULL;
9952
9953   base = get_base_address (MEM_EXPR (mem));
9954   if (base == NULL
9955       || TREE_CODE (base) != VAR_DECL
9956       || !DECL_THREAD_LOCAL_P (base))
9957     return NULL;
9958
9959   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9960   if (loc_result == NULL)
9961     return NULL;
9962
9963   if (INTVAL (MEM_OFFSET (mem)))
9964     {
9965       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9966         add_loc_descr (&loc_result,
9967                        new_loc_descr (DW_OP_plus_uconst,
9968                                       INTVAL (MEM_OFFSET (mem)), 0));
9969       else
9970         {
9971           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9972                                             VAR_INIT_STATUS_INITIALIZED);
9973           if (loc_result2 == 0)
9974             return NULL;
9975           add_loc_descr (&loc_result, loc_result2);
9976           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9977         }
9978     }
9979
9980   return loc_result;
9981 }
9982
9983 /* The following routine converts the RTL for a variable or parameter
9984    (resident in memory) into an equivalent Dwarf representation of a
9985    mechanism for getting the address of that same variable onto the top of a
9986    hypothetical "address evaluation" stack.
9987
9988    When creating memory location descriptors, we are effectively transforming
9989    the RTL for a memory-resident object into its Dwarf postfix expression
9990    equivalent.  This routine recursively descends an RTL tree, turning
9991    it into Dwarf postfix code as it goes.
9992
9993    MODE is the mode of the memory reference, needed to handle some
9994    autoincrement addressing modes.
9995
9996    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9997    location list for RTL.
9998
9999    Return 0 if we can't represent the location.  */
10000
10001 static dw_loc_descr_ref
10002 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10003                     enum var_init_status initialized)
10004 {
10005   dw_loc_descr_ref mem_loc_result = NULL;
10006   enum dwarf_location_atom op;
10007
10008   /* Note that for a dynamically sized array, the location we will generate a
10009      description of here will be the lowest numbered location which is
10010      actually within the array.  That's *not* necessarily the same as the
10011      zeroth element of the array.  */
10012
10013   rtl = targetm.delegitimize_address (rtl);
10014
10015   switch (GET_CODE (rtl))
10016     {
10017     case POST_INC:
10018     case POST_DEC:
10019     case POST_MODIFY:
10020       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10021          just fall into the SUBREG code.  */
10022
10023       /* ... fall through ...  */
10024
10025     case SUBREG:
10026       /* The case of a subreg may arise when we have a local (register)
10027          variable or a formal (register) parameter which doesn't quite fill
10028          up an entire register.  For now, just assume that it is
10029          legitimate to make the Dwarf info refer to the whole register which
10030          contains the given subreg.  */
10031       rtl = XEXP (rtl, 0);
10032
10033       /* ... fall through ...  */
10034
10035     case REG:
10036       /* Whenever a register number forms a part of the description of the
10037          method for calculating the (dynamic) address of a memory resident
10038          object, DWARF rules require the register number be referred to as
10039          a "base register".  This distinction is not based in any way upon
10040          what category of register the hardware believes the given register
10041          belongs to.  This is strictly DWARF terminology we're dealing with
10042          here. Note that in cases where the location of a memory-resident
10043          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10044          OP_CONST (0)) the actual DWARF location descriptor that we generate
10045          may just be OP_BASEREG (basereg).  This may look deceptively like
10046          the object in question was allocated to a register (rather than in
10047          memory) so DWARF consumers need to be aware of the subtle
10048          distinction between OP_REG and OP_BASEREG.  */
10049       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10050         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10051       else if (stack_realign_drap
10052                && crtl->drap_reg
10053                && crtl->args.internal_arg_pointer == rtl
10054                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10055         {
10056           /* If RTL is internal_arg_pointer, which has been optimized
10057              out, use DRAP instead.  */
10058           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10059                                             VAR_INIT_STATUS_INITIALIZED);
10060         }
10061       break;
10062
10063     case MEM:
10064       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10065                                            VAR_INIT_STATUS_INITIALIZED);
10066       if (mem_loc_result == NULL)
10067         mem_loc_result = tls_mem_loc_descriptor (rtl);
10068       if (mem_loc_result != 0)
10069         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10070       break;
10071
10072     case LO_SUM:
10073          rtl = XEXP (rtl, 1);
10074
10075       /* ... fall through ...  */
10076
10077     case LABEL_REF:
10078       /* Some ports can transform a symbol ref into a label ref, because
10079          the symbol ref is too far away and has to be dumped into a constant
10080          pool.  */
10081     case CONST:
10082     case SYMBOL_REF:
10083       /* Alternatively, the symbol in the constant pool might be referenced
10084          by a different symbol.  */
10085       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10086         {
10087           bool marked;
10088           rtx tmp = get_pool_constant_mark (rtl, &marked);
10089
10090           if (GET_CODE (tmp) == SYMBOL_REF)
10091             {
10092               rtl = tmp;
10093               if (CONSTANT_POOL_ADDRESS_P (tmp))
10094                 get_pool_constant_mark (tmp, &marked);
10095               else
10096                 marked = true;
10097             }
10098
10099           /* If all references to this pool constant were optimized away,
10100              it was not output and thus we can't represent it.
10101              FIXME: might try to use DW_OP_const_value here, though
10102              DW_OP_piece complicates it.  */
10103           if (!marked)
10104             return 0;
10105         }
10106
10107       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10108       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10109       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10110       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10111       break;
10112
10113     case PRE_MODIFY:
10114       /* Extract the PLUS expression nested inside and fall into
10115          PLUS code below.  */
10116       rtl = XEXP (rtl, 1);
10117       goto plus;
10118
10119     case PRE_INC:
10120     case PRE_DEC:
10121       /* Turn these into a PLUS expression and fall into the PLUS code
10122          below.  */
10123       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10124                           GEN_INT (GET_CODE (rtl) == PRE_INC
10125                                    ? GET_MODE_UNIT_SIZE (mode)
10126                                    : -GET_MODE_UNIT_SIZE (mode)));
10127
10128       /* ... fall through ...  */
10129
10130     case PLUS:
10131     plus:
10132       if (is_based_loc (rtl))
10133         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10134                                           INTVAL (XEXP (rtl, 1)),
10135                                           VAR_INIT_STATUS_INITIALIZED);
10136       else
10137         {
10138           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10139                                                VAR_INIT_STATUS_INITIALIZED);
10140           if (mem_loc_result == 0)
10141             break;
10142
10143           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10144               && INTVAL (XEXP (rtl, 1)) >= 0)
10145             add_loc_descr (&mem_loc_result,
10146                            new_loc_descr (DW_OP_plus_uconst,
10147                                           INTVAL (XEXP (rtl, 1)), 0));
10148           else
10149             {
10150               dw_loc_descr_ref mem_loc_result2
10151                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10152                                       VAR_INIT_STATUS_INITIALIZED);
10153               if (mem_loc_result2 == 0)
10154                 break;
10155               add_loc_descr (&mem_loc_result, mem_loc_result2);
10156               add_loc_descr (&mem_loc_result,
10157                              new_loc_descr (DW_OP_plus, 0, 0));
10158             }
10159         }
10160       break;
10161
10162     /* If a pseudo-reg is optimized away, it is possible for it to
10163        be replaced with a MEM containing a multiply or shift.  */
10164     case MULT:
10165       op = DW_OP_mul;
10166       goto do_binop;
10167
10168     case ASHIFT:
10169       op = DW_OP_shl;
10170       goto do_binop;
10171
10172     case ASHIFTRT:
10173       op = DW_OP_shra;
10174       goto do_binop;
10175
10176     case LSHIFTRT:
10177       op = DW_OP_shr;
10178       goto do_binop;
10179
10180     do_binop:
10181       {
10182         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10183                                                    VAR_INIT_STATUS_INITIALIZED);
10184         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10185                                                    VAR_INIT_STATUS_INITIALIZED);
10186
10187         if (op0 == 0 || op1 == 0)
10188           break;
10189
10190         mem_loc_result = op0;
10191         add_loc_descr (&mem_loc_result, op1);
10192         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10193         break;
10194       }
10195
10196     case CONST_INT:
10197       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10198       break;
10199
10200     case CONCATN:
10201       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10202                                                    VAR_INIT_STATUS_INITIALIZED);
10203       break;
10204
10205     case UNSPEC:
10206       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10207          can't express it in the debug info.  This can happen e.g. with some
10208          TLS UNSPECs.  */
10209       break;
10210
10211     default:
10212       gcc_unreachable ();
10213     }
10214
10215   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10216     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10217
10218   return mem_loc_result;
10219 }
10220
10221 /* Return a descriptor that describes the concatenation of two locations.
10222    This is typically a complex variable.  */
10223
10224 static dw_loc_descr_ref
10225 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10226 {
10227   dw_loc_descr_ref cc_loc_result = NULL;
10228   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10229   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10230
10231   if (x0_ref == 0 || x1_ref == 0)
10232     return 0;
10233
10234   cc_loc_result = x0_ref;
10235   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10236
10237   add_loc_descr (&cc_loc_result, x1_ref);
10238   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10239
10240   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10241     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10242
10243   return cc_loc_result;
10244 }
10245
10246 /* Return a descriptor that describes the concatenation of N
10247    locations.  */
10248
10249 static dw_loc_descr_ref
10250 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10251 {
10252   unsigned int i;
10253   dw_loc_descr_ref cc_loc_result = NULL;
10254   unsigned int n = XVECLEN (concatn, 0);
10255
10256   for (i = 0; i < n; ++i)
10257     {
10258       dw_loc_descr_ref ref;
10259       rtx x = XVECEXP (concatn, 0, i);
10260
10261       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10262       if (ref == NULL)
10263         return NULL;
10264
10265       add_loc_descr (&cc_loc_result, ref);
10266       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10267     }
10268
10269   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10270     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10271
10272   return cc_loc_result;
10273 }
10274
10275 /* Output a proper Dwarf location descriptor for a variable or parameter
10276    which is either allocated in a register or in a memory location.  For a
10277    register, we just generate an OP_REG and the register number.  For a
10278    memory location we provide a Dwarf postfix expression describing how to
10279    generate the (dynamic) address of the object onto the address stack.
10280
10281    If we don't know how to describe it, return 0.  */
10282
10283 static dw_loc_descr_ref
10284 loc_descriptor (rtx rtl, enum var_init_status initialized)
10285 {
10286   dw_loc_descr_ref loc_result = NULL;
10287
10288   switch (GET_CODE (rtl))
10289     {
10290     case SUBREG:
10291       /* The case of a subreg may arise when we have a local (register)
10292          variable or a formal (register) parameter which doesn't quite fill
10293          up an entire register.  For now, just assume that it is
10294          legitimate to make the Dwarf info refer to the whole register which
10295          contains the given subreg.  */
10296       rtl = SUBREG_REG (rtl);
10297
10298       /* ... fall through ...  */
10299
10300     case REG:
10301       loc_result = reg_loc_descriptor (rtl, initialized);
10302       break;
10303
10304     case MEM:
10305       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10306                                        initialized);
10307       if (loc_result == NULL)
10308         loc_result = tls_mem_loc_descriptor (rtl);
10309       break;
10310
10311     case CONCAT:
10312       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10313                                           initialized);
10314       break;
10315
10316     case CONCATN:
10317       loc_result = concatn_loc_descriptor (rtl, initialized);
10318       break;
10319
10320     case VAR_LOCATION:
10321       /* Single part.  */
10322       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10323         {
10324           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10325           break;
10326         }
10327
10328       rtl = XEXP (rtl, 1);
10329       /* FALLTHRU */
10330
10331     case PARALLEL:
10332       {
10333         rtvec par_elems = XVEC (rtl, 0);
10334         int num_elem = GET_NUM_ELEM (par_elems);
10335         enum machine_mode mode;
10336         int i;
10337
10338         /* Create the first one, so we have something to add to.  */
10339         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10340                                      initialized);
10341         if (loc_result == NULL)
10342           return NULL;
10343         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10344         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10345         for (i = 1; i < num_elem; i++)
10346           {
10347             dw_loc_descr_ref temp;
10348
10349             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10350                                    initialized);
10351             if (temp == NULL)
10352               return NULL;
10353             add_loc_descr (&loc_result, temp);
10354             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10355             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10356           }
10357       }
10358       break;
10359
10360     default:
10361       gcc_unreachable ();
10362     }
10363
10364   return loc_result;
10365 }
10366
10367 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10368    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10369    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10370    top-level invocation, and we require the address of LOC; is 0 if we require
10371    the value of LOC.  */
10372
10373 static dw_loc_descr_ref
10374 loc_descriptor_from_tree_1 (tree loc, int want_address)
10375 {
10376   dw_loc_descr_ref ret, ret1;
10377   int have_address = 0;
10378   enum dwarf_location_atom op;
10379
10380   /* ??? Most of the time we do not take proper care for sign/zero
10381      extending the values properly.  Hopefully this won't be a real
10382      problem...  */
10383
10384   switch (TREE_CODE (loc))
10385     {
10386     case ERROR_MARK:
10387       return 0;
10388
10389     case PLACEHOLDER_EXPR:
10390       /* This case involves extracting fields from an object to determine the
10391          position of other fields.  We don't try to encode this here.  The
10392          only user of this is Ada, which encodes the needed information using
10393          the names of types.  */
10394       return 0;
10395
10396     case CALL_EXPR:
10397       return 0;
10398
10399     case PREINCREMENT_EXPR:
10400     case PREDECREMENT_EXPR:
10401     case POSTINCREMENT_EXPR:
10402     case POSTDECREMENT_EXPR:
10403       /* There are no opcodes for these operations.  */
10404       return 0;
10405
10406     case ADDR_EXPR:
10407       /* If we already want an address, there's nothing we can do.  */
10408       if (want_address)
10409         return 0;
10410
10411       /* Otherwise, process the argument and look for the address.  */
10412       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10413
10414     case VAR_DECL:
10415       if (DECL_THREAD_LOCAL_P (loc))
10416         {
10417           rtx rtl;
10418           unsigned first_op;
10419           unsigned second_op;
10420
10421           if (targetm.have_tls)
10422             {
10423               /* If this is not defined, we have no way to emit the
10424                  data.  */
10425               if (!targetm.asm_out.output_dwarf_dtprel)
10426                 return 0;
10427
10428                /* The way DW_OP_GNU_push_tls_address is specified, we
10429                   can only look up addresses of objects in the current
10430                   module.  */
10431               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10432                 return 0;
10433               first_op = INTERNAL_DW_OP_tls_addr;
10434               second_op = DW_OP_GNU_push_tls_address;
10435             }
10436           else
10437             {
10438               if (!targetm.emutls.debug_form_tls_address)
10439                 return 0;
10440               loc = emutls_decl (loc);
10441               first_op = DW_OP_addr;
10442               second_op = DW_OP_form_tls_address;
10443             }
10444
10445           rtl = rtl_for_decl_location (loc);
10446           if (rtl == NULL_RTX)
10447             return 0;
10448
10449           if (!MEM_P (rtl))
10450             return 0;
10451           rtl = XEXP (rtl, 0);
10452           if (! CONSTANT_P (rtl))
10453             return 0;
10454
10455           ret = new_loc_descr (first_op, 0, 0);
10456           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10457           ret->dw_loc_oprnd1.v.val_addr = rtl;
10458
10459           ret1 = new_loc_descr (second_op, 0, 0);
10460           add_loc_descr (&ret, ret1);
10461
10462           have_address = 1;
10463           break;
10464         }
10465       /* FALLTHRU */
10466
10467     case PARM_DECL:
10468       if (DECL_HAS_VALUE_EXPR_P (loc))
10469         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10470                                            want_address);
10471       /* FALLTHRU */
10472
10473     case RESULT_DECL:
10474     case FUNCTION_DECL:
10475       {
10476         rtx rtl = rtl_for_decl_location (loc);
10477
10478         if (rtl == NULL_RTX)
10479           return 0;
10480         else if (GET_CODE (rtl) == CONST_INT)
10481           {
10482             HOST_WIDE_INT val = INTVAL (rtl);
10483             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10484               val &= GET_MODE_MASK (DECL_MODE (loc));
10485             ret = int_loc_descriptor (val);
10486           }
10487         else if (GET_CODE (rtl) == CONST_STRING)
10488           return 0;
10489         else if (CONSTANT_P (rtl))
10490           {
10491             ret = new_loc_descr (DW_OP_addr, 0, 0);
10492             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10493             ret->dw_loc_oprnd1.v.val_addr = rtl;
10494           }
10495         else
10496           {
10497             enum machine_mode mode;
10498
10499             /* Certain constructs can only be represented at top-level.  */
10500             if (want_address == 2)
10501               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10502
10503             mode = GET_MODE (rtl);
10504             if (MEM_P (rtl))
10505               {
10506                 rtl = XEXP (rtl, 0);
10507                 have_address = 1;
10508               }
10509             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10510           }
10511       }
10512       break;
10513
10514     case INDIRECT_REF:
10515       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10516       have_address = 1;
10517       break;
10518
10519     case COMPOUND_EXPR:
10520       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10521
10522     CASE_CONVERT:
10523     case VIEW_CONVERT_EXPR:
10524     case SAVE_EXPR:
10525     case MODIFY_EXPR:
10526       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10527
10528     case COMPONENT_REF:
10529     case BIT_FIELD_REF:
10530     case ARRAY_REF:
10531     case ARRAY_RANGE_REF:
10532       {
10533         tree obj, offset;
10534         HOST_WIDE_INT bitsize, bitpos, bytepos;
10535         enum machine_mode mode;
10536         int volatilep;
10537         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10538
10539         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10540                                    &unsignedp, &volatilep, false);
10541
10542         if (obj == loc)
10543           return 0;
10544
10545         ret = loc_descriptor_from_tree_1 (obj, 1);
10546         if (ret == 0
10547             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10548           return 0;
10549
10550         if (offset != NULL_TREE)
10551           {
10552             /* Variable offset.  */
10553             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10554             if (ret1 == 0)
10555               return 0;
10556             add_loc_descr (&ret, ret1);
10557             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10558           }
10559
10560         bytepos = bitpos / BITS_PER_UNIT;
10561         if (bytepos > 0)
10562           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10563         else if (bytepos < 0)
10564           {
10565             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10566             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10567           }
10568
10569         have_address = 1;
10570         break;
10571       }
10572
10573     case INTEGER_CST:
10574       if (host_integerp (loc, 0))
10575         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10576       else
10577         return 0;
10578       break;
10579
10580     case CONSTRUCTOR:
10581       {
10582         /* Get an RTL for this, if something has been emitted.  */
10583         rtx rtl = lookup_constant_def (loc);
10584         enum machine_mode mode;
10585
10586         if (!rtl || !MEM_P (rtl))
10587           return 0;
10588         mode = GET_MODE (rtl);
10589         rtl = XEXP (rtl, 0);
10590         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10591         have_address = 1;
10592         break;
10593       }
10594
10595     case TRUTH_AND_EXPR:
10596     case TRUTH_ANDIF_EXPR:
10597     case BIT_AND_EXPR:
10598       op = DW_OP_and;
10599       goto do_binop;
10600
10601     case TRUTH_XOR_EXPR:
10602     case BIT_XOR_EXPR:
10603       op = DW_OP_xor;
10604       goto do_binop;
10605
10606     case TRUTH_OR_EXPR:
10607     case TRUTH_ORIF_EXPR:
10608     case BIT_IOR_EXPR:
10609       op = DW_OP_or;
10610       goto do_binop;
10611
10612     case FLOOR_DIV_EXPR:
10613     case CEIL_DIV_EXPR:
10614     case ROUND_DIV_EXPR:
10615     case TRUNC_DIV_EXPR:
10616       op = DW_OP_div;
10617       goto do_binop;
10618
10619     case MINUS_EXPR:
10620       op = DW_OP_minus;
10621       goto do_binop;
10622
10623     case FLOOR_MOD_EXPR:
10624     case CEIL_MOD_EXPR:
10625     case ROUND_MOD_EXPR:
10626     case TRUNC_MOD_EXPR:
10627       op = DW_OP_mod;
10628       goto do_binop;
10629
10630     case MULT_EXPR:
10631       op = DW_OP_mul;
10632       goto do_binop;
10633
10634     case LSHIFT_EXPR:
10635       op = DW_OP_shl;
10636       goto do_binop;
10637
10638     case RSHIFT_EXPR:
10639       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10640       goto do_binop;
10641
10642     case POINTER_PLUS_EXPR:
10643     case PLUS_EXPR:
10644       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10645           && host_integerp (TREE_OPERAND (loc, 1), 0))
10646         {
10647           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10648           if (ret == 0)
10649             return 0;
10650
10651           add_loc_descr (&ret,
10652                          new_loc_descr (DW_OP_plus_uconst,
10653                                         tree_low_cst (TREE_OPERAND (loc, 1),
10654                                                       0),
10655                                         0));
10656           break;
10657         }
10658
10659       op = DW_OP_plus;
10660       goto do_binop;
10661
10662     case LE_EXPR:
10663       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10664         return 0;
10665
10666       op = DW_OP_le;
10667       goto do_binop;
10668
10669     case GE_EXPR:
10670       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10671         return 0;
10672
10673       op = DW_OP_ge;
10674       goto do_binop;
10675
10676     case LT_EXPR:
10677       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10678         return 0;
10679
10680       op = DW_OP_lt;
10681       goto do_binop;
10682
10683     case GT_EXPR:
10684       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10685         return 0;
10686
10687       op = DW_OP_gt;
10688       goto do_binop;
10689
10690     case EQ_EXPR:
10691       op = DW_OP_eq;
10692       goto do_binop;
10693
10694     case NE_EXPR:
10695       op = DW_OP_ne;
10696       goto do_binop;
10697
10698     do_binop:
10699       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10700       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10701       if (ret == 0 || ret1 == 0)
10702         return 0;
10703
10704       add_loc_descr (&ret, ret1);
10705       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10706       break;
10707
10708     case TRUTH_NOT_EXPR:
10709     case BIT_NOT_EXPR:
10710       op = DW_OP_not;
10711       goto do_unop;
10712
10713     case ABS_EXPR:
10714       op = DW_OP_abs;
10715       goto do_unop;
10716
10717     case NEGATE_EXPR:
10718       op = DW_OP_neg;
10719       goto do_unop;
10720
10721     do_unop:
10722       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10723       if (ret == 0)
10724         return 0;
10725
10726       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10727       break;
10728
10729     case MIN_EXPR:
10730     case MAX_EXPR:
10731       {
10732         const enum tree_code code =
10733           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10734
10735         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10736                       build2 (code, integer_type_node,
10737                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10738                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10739       }
10740
10741       /* ... fall through ...  */
10742
10743     case COND_EXPR:
10744       {
10745         dw_loc_descr_ref lhs
10746           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10747         dw_loc_descr_ref rhs
10748           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10749         dw_loc_descr_ref bra_node, jump_node, tmp;
10750
10751         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10752         if (ret == 0 || lhs == 0 || rhs == 0)
10753           return 0;
10754
10755         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10756         add_loc_descr (&ret, bra_node);
10757
10758         add_loc_descr (&ret, rhs);
10759         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10760         add_loc_descr (&ret, jump_node);
10761
10762         add_loc_descr (&ret, lhs);
10763         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10764         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10765
10766         /* ??? Need a node to point the skip at.  Use a nop.  */
10767         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10768         add_loc_descr (&ret, tmp);
10769         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10770         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10771       }
10772       break;
10773
10774     case FIX_TRUNC_EXPR:
10775       return 0;
10776
10777     default:
10778       /* Leave front-end specific codes as simply unknown.  This comes
10779          up, for instance, with the C STMT_EXPR.  */
10780       if ((unsigned int) TREE_CODE (loc)
10781           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10782         return 0;
10783
10784 #ifdef ENABLE_CHECKING
10785       /* Otherwise this is a generic code; we should just lists all of
10786          these explicitly.  We forgot one.  */
10787       gcc_unreachable ();
10788 #else
10789       /* In a release build, we want to degrade gracefully: better to
10790          generate incomplete debugging information than to crash.  */
10791       return NULL;
10792 #endif
10793     }
10794
10795   /* Show if we can't fill the request for an address.  */
10796   if (want_address && !have_address)
10797     return 0;
10798
10799   /* If we've got an address and don't want one, dereference.  */
10800   if (!want_address && have_address && ret)
10801     {
10802       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10803
10804       if (size > DWARF2_ADDR_SIZE || size == -1)
10805         return 0;
10806       else if (size == DWARF2_ADDR_SIZE)
10807         op = DW_OP_deref;
10808       else
10809         op = DW_OP_deref_size;
10810
10811       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10812     }
10813
10814   return ret;
10815 }
10816
10817 static inline dw_loc_descr_ref
10818 loc_descriptor_from_tree (tree loc)
10819 {
10820   return loc_descriptor_from_tree_1 (loc, 2);
10821 }
10822
10823 /* Given a value, round it up to the lowest multiple of `boundary'
10824    which is not less than the value itself.  */
10825
10826 static inline HOST_WIDE_INT
10827 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10828 {
10829   return (((value + boundary - 1) / boundary) * boundary);
10830 }
10831
10832 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10833    pointer to the declared type for the relevant field variable, or return
10834    `integer_type_node' if the given node turns out to be an
10835    ERROR_MARK node.  */
10836
10837 static inline tree
10838 field_type (const_tree decl)
10839 {
10840   tree type;
10841
10842   if (TREE_CODE (decl) == ERROR_MARK)
10843     return integer_type_node;
10844
10845   type = DECL_BIT_FIELD_TYPE (decl);
10846   if (type == NULL_TREE)
10847     type = TREE_TYPE (decl);
10848
10849   return type;
10850 }
10851
10852 /* Given a pointer to a tree node, return the alignment in bits for
10853    it, or else return BITS_PER_WORD if the node actually turns out to
10854    be an ERROR_MARK node.  */
10855
10856 static inline unsigned
10857 simple_type_align_in_bits (const_tree type)
10858 {
10859   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10860 }
10861
10862 static inline unsigned
10863 simple_decl_align_in_bits (const_tree decl)
10864 {
10865   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10866 }
10867
10868 /* Return the result of rounding T up to ALIGN.  */
10869
10870 static inline HOST_WIDE_INT
10871 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10872 {
10873   /* We must be careful if T is negative because HOST_WIDE_INT can be
10874      either "above" or "below" unsigned int as per the C promotion
10875      rules, depending on the host, thus making the signedness of the
10876      direct multiplication and division unpredictable.  */
10877   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10878
10879   u += align - 1;
10880   u /= align;
10881   u *= align;
10882
10883   return (HOST_WIDE_INT) u;
10884 }
10885
10886 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10887    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10888    or return 0 if we are unable to determine what that offset is, either
10889    because the argument turns out to be a pointer to an ERROR_MARK node, or
10890    because the offset is actually variable.  (We can't handle the latter case
10891    just yet).  */
10892
10893 static HOST_WIDE_INT
10894 field_byte_offset (const_tree decl)
10895 {
10896   HOST_WIDE_INT object_offset_in_bits;
10897   HOST_WIDE_INT bitpos_int;
10898
10899   if (TREE_CODE (decl) == ERROR_MARK)
10900     return 0;
10901
10902   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10903
10904   /* We cannot yet cope with fields whose positions are variable, so
10905      for now, when we see such things, we simply return 0.  Someday, we may
10906      be able to handle such cases, but it will be damn difficult.  */
10907   if (! host_integerp (bit_position (decl), 0))
10908     return 0;
10909
10910   bitpos_int = int_bit_position (decl);
10911
10912 #ifdef PCC_BITFIELD_TYPE_MATTERS
10913   if (PCC_BITFIELD_TYPE_MATTERS)
10914     {
10915       tree type;
10916       tree field_size_tree;
10917       HOST_WIDE_INT deepest_bitpos;
10918       unsigned HOST_WIDE_INT field_size_in_bits;
10919       unsigned int type_align_in_bits;
10920       unsigned int decl_align_in_bits;
10921       unsigned HOST_WIDE_INT type_size_in_bits;
10922
10923       type = field_type (decl);
10924       field_size_tree = DECL_SIZE (decl);
10925
10926       /* The size could be unspecified if there was an error, or for
10927          a flexible array member.  */
10928       if (! field_size_tree)
10929         field_size_tree = bitsize_zero_node;
10930
10931       /* If we don't know the size of the field, pretend it's a full word.  */
10932       if (host_integerp (field_size_tree, 1))
10933         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10934       else
10935         field_size_in_bits = BITS_PER_WORD;
10936
10937       type_size_in_bits = simple_type_size_in_bits (type);
10938       type_align_in_bits = simple_type_align_in_bits (type);
10939       decl_align_in_bits = simple_decl_align_in_bits (decl);
10940
10941       /* The GCC front-end doesn't make any attempt to keep track of the
10942          starting bit offset (relative to the start of the containing
10943          structure type) of the hypothetical "containing object" for a
10944          bit-field.  Thus, when computing the byte offset value for the
10945          start of the "containing object" of a bit-field, we must deduce
10946          this information on our own. This can be rather tricky to do in
10947          some cases.  For example, handling the following structure type
10948          definition when compiling for an i386/i486 target (which only
10949          aligns long long's to 32-bit boundaries) can be very tricky:
10950
10951          struct S { int field1; long long field2:31; };
10952
10953          Fortunately, there is a simple rule-of-thumb which can be used
10954          in such cases.  When compiling for an i386/i486, GCC will
10955          allocate 8 bytes for the structure shown above.  It decides to
10956          do this based upon one simple rule for bit-field allocation.
10957          GCC allocates each "containing object" for each bit-field at
10958          the first (i.e. lowest addressed) legitimate alignment boundary
10959          (based upon the required minimum alignment for the declared
10960          type of the field) which it can possibly use, subject to the
10961          condition that there is still enough available space remaining
10962          in the containing object (when allocated at the selected point)
10963          to fully accommodate all of the bits of the bit-field itself.
10964
10965          This simple rule makes it obvious why GCC allocates 8 bytes for
10966          each object of the structure type shown above.  When looking
10967          for a place to allocate the "containing object" for `field2',
10968          the compiler simply tries to allocate a 64-bit "containing
10969          object" at each successive 32-bit boundary (starting at zero)
10970          until it finds a place to allocate that 64- bit field such that
10971          at least 31 contiguous (and previously unallocated) bits remain
10972          within that selected 64 bit field.  (As it turns out, for the
10973          example above, the compiler finds it is OK to allocate the
10974          "containing object" 64-bit field at bit-offset zero within the
10975          structure type.)
10976
10977          Here we attempt to work backwards from the limited set of facts
10978          we're given, and we try to deduce from those facts, where GCC
10979          must have believed that the containing object started (within
10980          the structure type). The value we deduce is then used (by the
10981          callers of this routine) to generate DW_AT_location and
10982          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10983          the case of DW_AT_location, regular fields as well).  */
10984
10985       /* Figure out the bit-distance from the start of the structure to
10986          the "deepest" bit of the bit-field.  */
10987       deepest_bitpos = bitpos_int + field_size_in_bits;
10988
10989       /* This is the tricky part.  Use some fancy footwork to deduce
10990          where the lowest addressed bit of the containing object must
10991          be.  */
10992       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10993
10994       /* Round up to type_align by default.  This works best for
10995          bitfields.  */
10996       object_offset_in_bits
10997         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10998
10999       if (object_offset_in_bits > bitpos_int)
11000         {
11001           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11002
11003           /* Round up to decl_align instead.  */
11004           object_offset_in_bits
11005             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11006         }
11007     }
11008   else
11009 #endif
11010     object_offset_in_bits = bitpos_int;
11011
11012   return object_offset_in_bits / BITS_PER_UNIT;
11013 }
11014 \f
11015 /* The following routines define various Dwarf attributes and any data
11016    associated with them.  */
11017
11018 /* Add a location description attribute value to a DIE.
11019
11020    This emits location attributes suitable for whole variables and
11021    whole parameters.  Note that the location attributes for struct fields are
11022    generated by the routine `data_member_location_attribute' below.  */
11023
11024 static inline void
11025 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11026                              dw_loc_descr_ref descr)
11027 {
11028   if (descr != 0)
11029     add_AT_loc (die, attr_kind, descr);
11030 }
11031
11032 /* Attach the specialized form of location attribute used for data members of
11033    struct and union types.  In the special case of a FIELD_DECL node which
11034    represents a bit-field, the "offset" part of this special location
11035    descriptor must indicate the distance in bytes from the lowest-addressed
11036    byte of the containing struct or union type to the lowest-addressed byte of
11037    the "containing object" for the bit-field.  (See the `field_byte_offset'
11038    function above).
11039
11040    For any given bit-field, the "containing object" is a hypothetical object
11041    (of some integral or enum type) within which the given bit-field lives.  The
11042    type of this hypothetical "containing object" is always the same as the
11043    declared type of the individual bit-field itself (for GCC anyway... the
11044    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11045    bytes) of the hypothetical "containing object" which will be given in the
11046    DW_AT_byte_size attribute for this bit-field.  (See the
11047    `byte_size_attribute' function below.)  It is also used when calculating the
11048    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11049    function below.)  */
11050
11051 static void
11052 add_data_member_location_attribute (dw_die_ref die, tree decl)
11053 {
11054   HOST_WIDE_INT offset;
11055   dw_loc_descr_ref loc_descr = 0;
11056
11057   if (TREE_CODE (decl) == TREE_BINFO)
11058     {
11059       /* We're working on the TAG_inheritance for a base class.  */
11060       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11061         {
11062           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11063              aren't at a fixed offset from all (sub)objects of the same
11064              type.  We need to extract the appropriate offset from our
11065              vtable.  The following dwarf expression means
11066
11067                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11068
11069              This is specific to the V3 ABI, of course.  */
11070
11071           dw_loc_descr_ref tmp;
11072
11073           /* Make a copy of the object address.  */
11074           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11075           add_loc_descr (&loc_descr, tmp);
11076
11077           /* Extract the vtable address.  */
11078           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11079           add_loc_descr (&loc_descr, tmp);
11080
11081           /* Calculate the address of the offset.  */
11082           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11083           gcc_assert (offset < 0);
11084
11085           tmp = int_loc_descriptor (-offset);
11086           add_loc_descr (&loc_descr, tmp);
11087           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11088           add_loc_descr (&loc_descr, tmp);
11089
11090           /* Extract the offset.  */
11091           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11092           add_loc_descr (&loc_descr, tmp);
11093
11094           /* Add it to the object address.  */
11095           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11096           add_loc_descr (&loc_descr, tmp);
11097         }
11098       else
11099         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11100     }
11101   else
11102     offset = field_byte_offset (decl);
11103
11104   if (! loc_descr)
11105     {
11106       enum dwarf_location_atom op;
11107
11108       /* The DWARF2 standard says that we should assume that the structure
11109          address is already on the stack, so we can specify a structure field
11110          address by using DW_OP_plus_uconst.  */
11111
11112 #ifdef MIPS_DEBUGGING_INFO
11113       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11114          operator correctly.  It works only if we leave the offset on the
11115          stack.  */
11116       op = DW_OP_constu;
11117 #else
11118       op = DW_OP_plus_uconst;
11119 #endif
11120
11121       loc_descr = new_loc_descr (op, offset, 0);
11122     }
11123
11124   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11125 }
11126
11127 /* Writes integer values to dw_vec_const array.  */
11128
11129 static void
11130 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11131 {
11132   while (size != 0)
11133     {
11134       *dest++ = val & 0xff;
11135       val >>= 8;
11136       --size;
11137     }
11138 }
11139
11140 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11141
11142 static HOST_WIDE_INT
11143 extract_int (const unsigned char *src, unsigned int size)
11144 {
11145   HOST_WIDE_INT val = 0;
11146
11147   src += size;
11148   while (size != 0)
11149     {
11150       val <<= 8;
11151       val |= *--src & 0xff;
11152       --size;
11153     }
11154   return val;
11155 }
11156
11157 /* Writes floating point values to dw_vec_const array.  */
11158
11159 static void
11160 insert_float (const_rtx rtl, unsigned char *array)
11161 {
11162   REAL_VALUE_TYPE rv;
11163   long val[4];
11164   int i;
11165
11166   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11167   real_to_target (val, &rv, GET_MODE (rtl));
11168
11169   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11170   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11171     {
11172       insert_int (val[i], 4, array);
11173       array += 4;
11174     }
11175 }
11176
11177 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11178    does not have a "location" either in memory or in a register.  These
11179    things can arise in GNU C when a constant is passed as an actual parameter
11180    to an inlined function.  They can also arise in C++ where declared
11181    constants do not necessarily get memory "homes".  */
11182
11183 static void
11184 add_const_value_attribute (dw_die_ref die, rtx rtl)
11185 {
11186   switch (GET_CODE (rtl))
11187     {
11188     case CONST_INT:
11189       {
11190         HOST_WIDE_INT val = INTVAL (rtl);
11191
11192         if (val < 0)
11193           add_AT_int (die, DW_AT_const_value, val);
11194         else
11195           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11196       }
11197       break;
11198
11199     case CONST_DOUBLE:
11200       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11201          floating-point constant.  A CONST_DOUBLE is used whenever the
11202          constant requires more than one word in order to be adequately
11203          represented.  We output CONST_DOUBLEs as blocks.  */
11204       {
11205         enum machine_mode mode = GET_MODE (rtl);
11206
11207         if (SCALAR_FLOAT_MODE_P (mode))
11208           {
11209             unsigned int length = GET_MODE_SIZE (mode);
11210             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11211
11212             insert_float (rtl, array);
11213             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11214           }
11215         else
11216           {
11217             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11218             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11219
11220             add_AT_long_long (die, DW_AT_const_value,
11221                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11222           }
11223       }
11224       break;
11225
11226     case CONST_VECTOR:
11227       {
11228         enum machine_mode mode = GET_MODE (rtl);
11229         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11230         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11231         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11232         unsigned int i;
11233         unsigned char *p;
11234
11235         switch (GET_MODE_CLASS (mode))
11236           {
11237           case MODE_VECTOR_INT:
11238             for (i = 0, p = array; i < length; i++, p += elt_size)
11239               {
11240                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11241                 HOST_WIDE_INT lo, hi;
11242
11243                 switch (GET_CODE (elt))
11244                   {
11245                   case CONST_INT:
11246                     lo = INTVAL (elt);
11247                     hi = -(lo < 0);
11248                     break;
11249
11250                   case CONST_DOUBLE:
11251                     lo = CONST_DOUBLE_LOW (elt);
11252                     hi = CONST_DOUBLE_HIGH (elt);
11253                     break;
11254
11255                   default:
11256                     gcc_unreachable ();
11257                   }
11258
11259                 if (elt_size <= sizeof (HOST_WIDE_INT))
11260                   insert_int (lo, elt_size, p);
11261                 else
11262                   {
11263                     unsigned char *p0 = p;
11264                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11265
11266                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11267                     if (WORDS_BIG_ENDIAN)
11268                       {
11269                         p0 = p1;
11270                         p1 = p;
11271                       }
11272                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11273                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11274                   }
11275               }
11276             break;
11277
11278           case MODE_VECTOR_FLOAT:
11279             for (i = 0, p = array; i < length; i++, p += elt_size)
11280               {
11281                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11282                 insert_float (elt, p);
11283               }
11284             break;
11285
11286           default:
11287             gcc_unreachable ();
11288           }
11289
11290         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11291       }
11292       break;
11293
11294     case CONST_STRING:
11295       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11296       break;
11297
11298     case SYMBOL_REF:
11299     case LABEL_REF:
11300     case CONST:
11301       add_AT_addr (die, DW_AT_const_value, rtl);
11302       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11303       break;
11304
11305     case PLUS:
11306       /* In cases where an inlined instance of an inline function is passed
11307          the address of an `auto' variable (which is local to the caller) we
11308          can get a situation where the DECL_RTL of the artificial local
11309          variable (for the inlining) which acts as a stand-in for the
11310          corresponding formal parameter (of the inline function) will look
11311          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11312          exactly a compile-time constant expression, but it isn't the address
11313          of the (artificial) local variable either.  Rather, it represents the
11314          *value* which the artificial local variable always has during its
11315          lifetime.  We currently have no way to represent such quasi-constant
11316          values in Dwarf, so for now we just punt and generate nothing.  */
11317       break;
11318
11319     default:
11320       /* No other kinds of rtx should be possible here.  */
11321       gcc_unreachable ();
11322     }
11323
11324 }
11325
11326 /* Determine whether the evaluation of EXPR references any variables
11327    or functions which aren't otherwise used (and therefore may not be
11328    output).  */
11329 static tree
11330 reference_to_unused (tree * tp, int * walk_subtrees,
11331                      void * data ATTRIBUTE_UNUSED)
11332 {
11333   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11334     *walk_subtrees = 0;
11335
11336   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11337       && ! TREE_ASM_WRITTEN (*tp))
11338     return *tp;
11339   /* ???  The C++ FE emits debug information for using decls, so
11340      putting gcc_unreachable here falls over.  See PR31899.  For now
11341      be conservative.  */
11342   else if (!cgraph_global_info_ready
11343            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11344     return *tp;
11345   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11346     {
11347       struct varpool_node *node = varpool_node (*tp);
11348       if (!node->needed)
11349         return *tp;
11350     }
11351   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11352            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11353     {
11354       struct cgraph_node *node = cgraph_node (*tp);
11355       if (!node->output)
11356         return *tp;
11357     }
11358   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11359     return *tp;
11360
11361   return NULL_TREE;
11362 }
11363
11364 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11365    for use in a later add_const_value_attribute call.  */
11366
11367 static rtx
11368 rtl_for_decl_init (tree init, tree type)
11369 {
11370   rtx rtl = NULL_RTX;
11371
11372   /* If a variable is initialized with a string constant without embedded
11373      zeros, build CONST_STRING.  */
11374   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11375     {
11376       tree enttype = TREE_TYPE (type);
11377       tree domain = TYPE_DOMAIN (type);
11378       enum machine_mode mode = TYPE_MODE (enttype);
11379
11380       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11381           && domain
11382           && integer_zerop (TYPE_MIN_VALUE (domain))
11383           && compare_tree_int (TYPE_MAX_VALUE (domain),
11384                                TREE_STRING_LENGTH (init) - 1) == 0
11385           && ((size_t) TREE_STRING_LENGTH (init)
11386               == strlen (TREE_STRING_POINTER (init)) + 1))
11387         rtl = gen_rtx_CONST_STRING (VOIDmode,
11388                                     ggc_strdup (TREE_STRING_POINTER (init)));
11389     }
11390   /* Other aggregates, and complex values, could be represented using
11391      CONCAT: FIXME!  */
11392   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11393     ;
11394   /* Vectors only work if their mode is supported by the target.
11395      FIXME: generic vectors ought to work too.  */
11396   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11397     ;
11398   /* If the initializer is something that we know will expand into an
11399      immediate RTL constant, expand it now.  We must be careful not to
11400      reference variables which won't be output.  */
11401   else if (initializer_constant_valid_p (init, type)
11402            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11403     {
11404       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11405          possible.  */
11406       if (TREE_CODE (type) == VECTOR_TYPE)
11407         switch (TREE_CODE (init))
11408           {
11409           case VECTOR_CST:
11410             break;
11411           case CONSTRUCTOR:
11412             if (TREE_CONSTANT (init))
11413               {
11414                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11415                 bool constant_p = true;
11416                 tree value;
11417                 unsigned HOST_WIDE_INT ix;
11418
11419                 /* Even when ctor is constant, it might contain non-*_CST
11420                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11421                    belong into VECTOR_CST nodes.  */
11422                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11423                   if (!CONSTANT_CLASS_P (value))
11424                     {
11425                       constant_p = false;
11426                       break;
11427                     }
11428
11429                 if (constant_p)
11430                   {
11431                     init = build_vector_from_ctor (type, elts);
11432                     break;
11433                   }
11434               }
11435             /* FALLTHRU */
11436
11437           default:
11438             return NULL;
11439           }
11440
11441       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11442
11443       /* If expand_expr returns a MEM, it wasn't immediate.  */
11444       gcc_assert (!rtl || !MEM_P (rtl));
11445     }
11446
11447   return rtl;
11448 }
11449
11450 /* Generate RTL for the variable DECL to represent its location.  */
11451
11452 static rtx
11453 rtl_for_decl_location (tree decl)
11454 {
11455   rtx rtl;
11456
11457   /* Here we have to decide where we are going to say the parameter "lives"
11458      (as far as the debugger is concerned).  We only have a couple of
11459      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11460
11461      DECL_RTL normally indicates where the parameter lives during most of the
11462      activation of the function.  If optimization is enabled however, this
11463      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11464      that the parameter doesn't really live anywhere (as far as the code
11465      generation parts of GCC are concerned) during most of the function's
11466      activation.  That will happen (for example) if the parameter is never
11467      referenced within the function.
11468
11469      We could just generate a location descriptor here for all non-NULL
11470      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11471      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11472      where DECL_RTL is NULL or is a pseudo-reg.
11473
11474      Note however that we can only get away with using DECL_INCOMING_RTL as
11475      a backup substitute for DECL_RTL in certain limited cases.  In cases
11476      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11477      we can be sure that the parameter was passed using the same type as it is
11478      declared to have within the function, and that its DECL_INCOMING_RTL
11479      points us to a place where a value of that type is passed.
11480
11481      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11482      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11483      because in these cases DECL_INCOMING_RTL points us to a value of some
11484      type which is *different* from the type of the parameter itself.  Thus,
11485      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11486      such cases, the debugger would end up (for example) trying to fetch a
11487      `float' from a place which actually contains the first part of a
11488      `double'.  That would lead to really incorrect and confusing
11489      output at debug-time.
11490
11491      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11492      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11493      are a couple of exceptions however.  On little-endian machines we can
11494      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11495      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11496      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11497      when (on a little-endian machine) a non-prototyped function has a
11498      parameter declared to be of type `short' or `char'.  In such cases,
11499      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11500      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11501      passed `int' value.  If the debugger then uses that address to fetch
11502      a `short' or a `char' (on a little-endian machine) the result will be
11503      the correct data, so we allow for such exceptional cases below.
11504
11505      Note that our goal here is to describe the place where the given formal
11506      parameter lives during most of the function's activation (i.e. between the
11507      end of the prologue and the start of the epilogue).  We'll do that as best
11508      as we can. Note however that if the given formal parameter is modified
11509      sometime during the execution of the function, then a stack backtrace (at
11510      debug-time) will show the function as having been called with the *new*
11511      value rather than the value which was originally passed in.  This happens
11512      rarely enough that it is not a major problem, but it *is* a problem, and
11513      I'd like to fix it.
11514
11515      A future version of dwarf2out.c may generate two additional attributes for
11516      any given DW_TAG_formal_parameter DIE which will describe the "passed
11517      type" and the "passed location" for the given formal parameter in addition
11518      to the attributes we now generate to indicate the "declared type" and the
11519      "active location" for each parameter.  This additional set of attributes
11520      could be used by debuggers for stack backtraces. Separately, note that
11521      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11522      This happens (for example) for inlined-instances of inline function formal
11523      parameters which are never referenced.  This really shouldn't be
11524      happening.  All PARM_DECL nodes should get valid non-NULL
11525      DECL_INCOMING_RTL values.  FIXME.  */
11526
11527   /* Use DECL_RTL as the "location" unless we find something better.  */
11528   rtl = DECL_RTL_IF_SET (decl);
11529
11530   /* When generating abstract instances, ignore everything except
11531      constants, symbols living in memory, and symbols living in
11532      fixed registers.  */
11533   if (! reload_completed)
11534     {
11535       if (rtl
11536           && (CONSTANT_P (rtl)
11537               || (MEM_P (rtl)
11538                   && CONSTANT_P (XEXP (rtl, 0)))
11539               || (REG_P (rtl)
11540                   && TREE_CODE (decl) == VAR_DECL
11541                   && TREE_STATIC (decl))))
11542         {
11543           rtl = targetm.delegitimize_address (rtl);
11544           return rtl;
11545         }
11546       rtl = NULL_RTX;
11547     }
11548   else if (TREE_CODE (decl) == PARM_DECL)
11549     {
11550       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11551         {
11552           tree declared_type = TREE_TYPE (decl);
11553           tree passed_type = DECL_ARG_TYPE (decl);
11554           enum machine_mode dmode = TYPE_MODE (declared_type);
11555           enum machine_mode pmode = TYPE_MODE (passed_type);
11556
11557           /* This decl represents a formal parameter which was optimized out.
11558              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11559              all cases where (rtl == NULL_RTX) just below.  */
11560           if (dmode == pmode)
11561             rtl = DECL_INCOMING_RTL (decl);
11562           else if (SCALAR_INT_MODE_P (dmode)
11563                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11564                    && DECL_INCOMING_RTL (decl))
11565             {
11566               rtx inc = DECL_INCOMING_RTL (decl);
11567               if (REG_P (inc))
11568                 rtl = inc;
11569               else if (MEM_P (inc))
11570                 {
11571                   if (BYTES_BIG_ENDIAN)
11572                     rtl = adjust_address_nv (inc, dmode,
11573                                              GET_MODE_SIZE (pmode)
11574                                              - GET_MODE_SIZE (dmode));
11575                   else
11576                     rtl = inc;
11577                 }
11578             }
11579         }
11580
11581       /* If the parm was passed in registers, but lives on the stack, then
11582          make a big endian correction if the mode of the type of the
11583          parameter is not the same as the mode of the rtl.  */
11584       /* ??? This is the same series of checks that are made in dbxout.c before
11585          we reach the big endian correction code there.  It isn't clear if all
11586          of these checks are necessary here, but keeping them all is the safe
11587          thing to do.  */
11588       else if (MEM_P (rtl)
11589                && XEXP (rtl, 0) != const0_rtx
11590                && ! CONSTANT_P (XEXP (rtl, 0))
11591                /* Not passed in memory.  */
11592                && !MEM_P (DECL_INCOMING_RTL (decl))
11593                /* Not passed by invisible reference.  */
11594                && (!REG_P (XEXP (rtl, 0))
11595                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11596                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11597 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11598                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11599 #endif
11600                      )
11601                /* Big endian correction check.  */
11602                && BYTES_BIG_ENDIAN
11603                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11604                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11605                    < UNITS_PER_WORD))
11606         {
11607           int offset = (UNITS_PER_WORD
11608                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11609
11610           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11611                              plus_constant (XEXP (rtl, 0), offset));
11612         }
11613     }
11614   else if (TREE_CODE (decl) == VAR_DECL
11615            && rtl
11616            && MEM_P (rtl)
11617            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11618            && BYTES_BIG_ENDIAN)
11619     {
11620       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11621       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11622
11623       /* If a variable is declared "register" yet is smaller than
11624          a register, then if we store the variable to memory, it
11625          looks like we're storing a register-sized value, when in
11626          fact we are not.  We need to adjust the offset of the
11627          storage location to reflect the actual value's bytes,
11628          else gdb will not be able to display it.  */
11629       if (rsize > dsize)
11630         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11631                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11632     }
11633
11634   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11635      and will have been substituted directly into all expressions that use it.
11636      C does not have such a concept, but C++ and other languages do.  */
11637   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11638     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11639
11640   if (rtl)
11641     rtl = targetm.delegitimize_address (rtl);
11642
11643   /* If we don't look past the constant pool, we risk emitting a
11644      reference to a constant pool entry that isn't referenced from
11645      code, and thus is not emitted.  */
11646   if (rtl)
11647     rtl = avoid_constant_pool_reference (rtl);
11648
11649   return rtl;
11650 }
11651
11652 /* We need to figure out what section we should use as the base for the
11653    address ranges where a given location is valid.
11654    1. If this particular DECL has a section associated with it, use that.
11655    2. If this function has a section associated with it, use that.
11656    3. Otherwise, use the text section.
11657    XXX: If you split a variable across multiple sections, we won't notice.  */
11658
11659 static const char *
11660 secname_for_decl (const_tree decl)
11661 {
11662   const char *secname;
11663
11664   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11665     {
11666       tree sectree = DECL_SECTION_NAME (decl);
11667       secname = TREE_STRING_POINTER (sectree);
11668     }
11669   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11670     {
11671       tree sectree = DECL_SECTION_NAME (current_function_decl);
11672       secname = TREE_STRING_POINTER (sectree);
11673     }
11674   else if (cfun && in_cold_section_p)
11675     secname = crtl->subsections.cold_section_label;
11676   else
11677     secname = text_section_label;
11678
11679   return secname;
11680 }
11681
11682 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11683    returned.  If so, the decl for the COMMON block is returned, and the
11684    value is the offset into the common block for the symbol.  */
11685
11686 static tree
11687 fortran_common (tree decl, HOST_WIDE_INT *value)
11688 {
11689   tree val_expr, cvar;
11690   enum machine_mode mode;
11691   HOST_WIDE_INT bitsize, bitpos;
11692   tree offset;
11693   int volatilep = 0, unsignedp = 0;
11694
11695   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11696      it does not have a value (the offset into the common area), or if it
11697      is thread local (as opposed to global) then it isn't common, and shouldn't
11698      be handled as such.  */
11699   if (TREE_CODE (decl) != VAR_DECL
11700       || !TREE_PUBLIC (decl)
11701       || !TREE_STATIC (decl)
11702       || !DECL_HAS_VALUE_EXPR_P (decl)
11703       || !is_fortran ())
11704     return NULL_TREE;
11705
11706   val_expr = DECL_VALUE_EXPR (decl);
11707   if (TREE_CODE (val_expr) != COMPONENT_REF)
11708     return NULL_TREE;
11709
11710   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11711                               &mode, &unsignedp, &volatilep, true);
11712
11713   if (cvar == NULL_TREE
11714       || TREE_CODE (cvar) != VAR_DECL
11715       || DECL_ARTIFICIAL (cvar)
11716       || !TREE_PUBLIC (cvar))
11717     return NULL_TREE;
11718
11719   *value = 0;
11720   if (offset != NULL)
11721     {
11722       if (!host_integerp (offset, 0))
11723         return NULL_TREE;
11724       *value = tree_low_cst (offset, 0);
11725     }
11726   if (bitpos != 0)
11727     *value += bitpos / BITS_PER_UNIT;
11728
11729   return cvar;
11730 }
11731
11732 /* Dereference a location expression LOC if DECL is passed by invisible
11733    reference.  */
11734
11735 static dw_loc_descr_ref
11736 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11737 {
11738   HOST_WIDE_INT size;
11739   enum dwarf_location_atom op;
11740
11741   if (loc == NULL)
11742     return NULL;
11743
11744   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11745       || !DECL_BY_REFERENCE (decl))
11746     return loc;
11747
11748   size = int_size_in_bytes (TREE_TYPE (decl));
11749   if (size > DWARF2_ADDR_SIZE || size == -1)
11750     return 0;
11751   else if (size == DWARF2_ADDR_SIZE)
11752     op = DW_OP_deref;
11753   else
11754     op = DW_OP_deref_size;
11755   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11756   return loc;
11757 }
11758
11759 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11760    data attribute for a variable or a parameter.  We generate the
11761    DW_AT_const_value attribute only in those cases where the given variable
11762    or parameter does not have a true "location" either in memory or in a
11763    register.  This can happen (for example) when a constant is passed as an
11764    actual argument in a call to an inline function.  (It's possible that
11765    these things can crop up in other ways also.)  Note that one type of
11766    constant value which can be passed into an inlined function is a constant
11767    pointer.  This can happen for example if an actual argument in an inlined
11768    function call evaluates to a compile-time constant address.  */
11769
11770 static void
11771 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11772                                        enum dwarf_attribute attr)
11773 {
11774   rtx rtl;
11775   dw_loc_descr_ref descr;
11776   var_loc_list *loc_list;
11777   struct var_loc_node *node;
11778   if (TREE_CODE (decl) == ERROR_MARK)
11779     return;
11780
11781   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11782               || TREE_CODE (decl) == RESULT_DECL);
11783
11784   /* See if we possibly have multiple locations for this variable.  */
11785   loc_list = lookup_decl_loc (decl);
11786
11787   /* If it truly has multiple locations, the first and last node will
11788      differ.  */
11789   if (loc_list && loc_list->first != loc_list->last)
11790     {
11791       const char *endname, *secname;
11792       dw_loc_list_ref list;
11793       rtx varloc;
11794       enum var_init_status initialized;
11795
11796       /* Now that we know what section we are using for a base,
11797          actually construct the list of locations.
11798          The first location information is what is passed to the
11799          function that creates the location list, and the remaining
11800          locations just get added on to that list.
11801          Note that we only know the start address for a location
11802          (IE location changes), so to build the range, we use
11803          the range [current location start, next location start].
11804          This means we have to special case the last node, and generate
11805          a range of [last location start, end of function label].  */
11806
11807       node = loc_list->first;
11808       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11809       secname = secname_for_decl (decl);
11810
11811       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11812         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11813       else
11814         initialized = VAR_INIT_STATUS_INITIALIZED;
11815
11816       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11817       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11818       node = node->next;
11819
11820       for (; node->next; node = node->next)
11821         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11822           {
11823             /* The variable has a location between NODE->LABEL and
11824                NODE->NEXT->LABEL.  */
11825             enum var_init_status initialized =
11826               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11827             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11828             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11829                                       decl);
11830             add_loc_descr_to_loc_list (&list, descr,
11831                                        node->label, node->next->label, secname);
11832           }
11833
11834       /* If the variable has a location at the last label
11835          it keeps its location until the end of function.  */
11836       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11837         {
11838           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11839           enum var_init_status initialized =
11840             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11841
11842           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11843           if (!current_function_decl)
11844             endname = text_end_label;
11845           else
11846             {
11847               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11848                                            current_function_funcdef_no);
11849               endname = ggc_strdup (label_id);
11850             }
11851           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11852                                     decl);
11853           add_loc_descr_to_loc_list (&list, descr,
11854                                      node->label, endname, secname);
11855         }
11856
11857       /* Finally, add the location list to the DIE, and we are done.  */
11858       add_AT_loc_list (die, attr, list);
11859       return;
11860     }
11861
11862   /* Try to get some constant RTL for this decl, and use that as the value of
11863      the location.  */
11864
11865   rtl = rtl_for_decl_location (decl);
11866   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11867     {
11868       add_const_value_attribute (die, rtl);
11869       return;
11870     }
11871
11872   /* If we have tried to generate the location otherwise, and it
11873      didn't work out (we wouldn't be here if we did), and we have a one entry
11874      location list, try generating a location from that.  */
11875   if (loc_list && loc_list->first)
11876     {
11877       enum var_init_status status;
11878       node = loc_list->first;
11879       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11880       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11881       if (descr)
11882         {
11883           descr = loc_by_reference (descr, decl);
11884           add_AT_location_description (die, attr, descr);
11885           return;
11886         }
11887     }
11888
11889   /* We couldn't get any rtl, so try directly generating the location
11890      description from the tree.  */
11891   descr = loc_descriptor_from_tree (decl);
11892   if (descr)
11893     {
11894       descr = loc_by_reference (descr, decl);
11895       add_AT_location_description (die, attr, descr);
11896       return;
11897     }
11898   /* None of that worked, so it must not really have a location;
11899      try adding a constant value attribute from the DECL_INITIAL.  */
11900   tree_add_const_value_attribute (die, decl);
11901 }
11902
11903 /* Helper function for tree_add_const_value_attribute.  Natively encode
11904    initializer INIT into an array.  Return true if successful.  */
11905
11906 static bool
11907 native_encode_initializer (tree init, unsigned char *array, int size)
11908 {
11909   tree type;
11910
11911   if (init == NULL_TREE)
11912     return false;
11913
11914   STRIP_NOPS (init);
11915   switch (TREE_CODE (init))
11916     {
11917     case STRING_CST:
11918       type = TREE_TYPE (init);
11919       if (TREE_CODE (type) == ARRAY_TYPE)
11920         {
11921           tree enttype = TREE_TYPE (type);
11922           enum machine_mode mode = TYPE_MODE (enttype);
11923
11924           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11925             return false;
11926           if (int_size_in_bytes (type) != size)
11927             return false;
11928           if (size > TREE_STRING_LENGTH (init))
11929             {
11930               memcpy (array, TREE_STRING_POINTER (init),
11931                       TREE_STRING_LENGTH (init));
11932               memset (array + TREE_STRING_LENGTH (init),
11933                       '\0', size - TREE_STRING_LENGTH (init));
11934             }
11935           else
11936             memcpy (array, TREE_STRING_POINTER (init), size);
11937           return true;
11938         }
11939       return false;
11940     case CONSTRUCTOR:
11941       type = TREE_TYPE (init);
11942       if (int_size_in_bytes (type) != size)
11943         return false;
11944       if (TREE_CODE (type) == ARRAY_TYPE)
11945         {
11946           HOST_WIDE_INT min_index;
11947           unsigned HOST_WIDE_INT cnt;
11948           int curpos = 0, fieldsize;
11949           constructor_elt *ce;
11950
11951           if (TYPE_DOMAIN (type) == NULL_TREE
11952               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11953             return false;
11954
11955           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11956           if (fieldsize <= 0)
11957             return false;
11958
11959           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11960           memset (array, '\0', size);
11961           for (cnt = 0;
11962                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11963                cnt++)
11964             {
11965               tree val = ce->value;
11966               tree index = ce->index;
11967               int pos = curpos;
11968               if (index && TREE_CODE (index) == RANGE_EXPR)
11969                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11970                       * fieldsize;
11971               else if (index)
11972                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11973
11974               if (val)
11975                 {
11976                   STRIP_NOPS (val);
11977                   if (!native_encode_initializer (val, array + pos, fieldsize))
11978                     return false;
11979                 }
11980               curpos = pos + fieldsize;
11981               if (index && TREE_CODE (index) == RANGE_EXPR)
11982                 {
11983                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11984                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11985                   while (count > 0)
11986                     {
11987                       if (val)
11988                         memcpy (array + curpos, array + pos, fieldsize);
11989                       curpos += fieldsize;
11990                     }
11991                 }
11992               gcc_assert (curpos <= size);
11993             }
11994           return true;
11995         }
11996       else if (TREE_CODE (type) == RECORD_TYPE
11997                || TREE_CODE (type) == UNION_TYPE)
11998         {
11999           tree field = NULL_TREE;
12000           unsigned HOST_WIDE_INT cnt;
12001           constructor_elt *ce;
12002
12003           if (int_size_in_bytes (type) != size)
12004             return false;
12005
12006           if (TREE_CODE (type) == RECORD_TYPE)
12007             field = TYPE_FIELDS (type);
12008
12009           for (cnt = 0;
12010                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12011                cnt++, field = field ? TREE_CHAIN (field) : 0)
12012             {
12013               tree val = ce->value;
12014               int pos, fieldsize;
12015
12016               if (ce->index != 0)
12017                 field = ce->index;
12018
12019               if (val)
12020                 STRIP_NOPS (val);
12021
12022               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12023                 return false;
12024
12025               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12026                   && TYPE_DOMAIN (TREE_TYPE (field))
12027                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12028                 return false;
12029               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12030                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12031                 return false;
12032               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12033               pos = int_byte_position (field);
12034               gcc_assert (pos + fieldsize <= size);
12035               if (val
12036                   && !native_encode_initializer (val, array + pos, fieldsize))
12037                 return false;
12038             }
12039           return true;
12040         }
12041       return false;
12042     case VIEW_CONVERT_EXPR:
12043     case NON_LVALUE_EXPR:
12044       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12045     default:
12046       return native_encode_expr (init, array, size) == size;
12047     }
12048 }
12049
12050 /* If we don't have a copy of this variable in memory for some reason (such
12051    as a C++ member constant that doesn't have an out-of-line definition),
12052    we should tell the debugger about the constant value.  */
12053
12054 static void
12055 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12056 {
12057   tree init;
12058   tree type = TREE_TYPE (decl);
12059   rtx rtl;
12060
12061   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12062     return;
12063
12064   init = DECL_INITIAL (decl);
12065   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12066     /* OK */;
12067   else
12068     return;
12069
12070   rtl = rtl_for_decl_init (init, type);
12071   if (rtl)
12072     add_const_value_attribute (var_die, rtl);
12073   /* If the host and target are sane, try harder.  */
12074   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12075            && initializer_constant_valid_p (init, type))
12076     {
12077       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12078       if (size > 0 && (int) size == size)
12079         {
12080           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12081
12082           if (native_encode_initializer (init, array, size))
12083             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12084         }
12085     }
12086 }
12087
12088 /* Convert the CFI instructions for the current function into a
12089    location list.  This is used for DW_AT_frame_base when we targeting
12090    a dwarf2 consumer that does not support the dwarf3
12091    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12092    expressions.  */
12093
12094 static dw_loc_list_ref
12095 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12096 {
12097   dw_fde_ref fde;
12098   dw_loc_list_ref list, *list_tail;
12099   dw_cfi_ref cfi;
12100   dw_cfa_location last_cfa, next_cfa;
12101   const char *start_label, *last_label, *section;
12102
12103   fde = current_fde ();
12104   gcc_assert (fde != NULL);
12105
12106   section = secname_for_decl (current_function_decl);
12107   list_tail = &list;
12108   list = NULL;
12109
12110   next_cfa.reg = INVALID_REGNUM;
12111   next_cfa.offset = 0;
12112   next_cfa.indirect = 0;
12113   next_cfa.base_offset = 0;
12114
12115   start_label = fde->dw_fde_begin;
12116
12117   /* ??? Bald assumption that the CIE opcode list does not contain
12118      advance opcodes.  */
12119   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12120     lookup_cfa_1 (cfi, &next_cfa);
12121
12122   last_cfa = next_cfa;
12123   last_label = start_label;
12124
12125   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12126     switch (cfi->dw_cfi_opc)
12127       {
12128       case DW_CFA_set_loc:
12129       case DW_CFA_advance_loc1:
12130       case DW_CFA_advance_loc2:
12131       case DW_CFA_advance_loc4:
12132         if (!cfa_equal_p (&last_cfa, &next_cfa))
12133           {
12134             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12135                                        start_label, last_label, section,
12136                                        list == NULL);
12137
12138             list_tail = &(*list_tail)->dw_loc_next;
12139             last_cfa = next_cfa;
12140             start_label = last_label;
12141           }
12142         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12143         break;
12144
12145       case DW_CFA_advance_loc:
12146         /* The encoding is complex enough that we should never emit this.  */
12147       case DW_CFA_remember_state:
12148       case DW_CFA_restore_state:
12149         /* We don't handle these two in this function.  It would be possible
12150            if it were to be required.  */
12151         gcc_unreachable ();
12152
12153       default:
12154         lookup_cfa_1 (cfi, &next_cfa);
12155         break;
12156       }
12157
12158   if (!cfa_equal_p (&last_cfa, &next_cfa))
12159     {
12160       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12161                                  start_label, last_label, section,
12162                                  list == NULL);
12163       list_tail = &(*list_tail)->dw_loc_next;
12164       start_label = last_label;
12165     }
12166   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12167                              start_label, fde->dw_fde_end, section,
12168                              list == NULL);
12169
12170   return list;
12171 }
12172
12173 /* Compute a displacement from the "steady-state frame pointer" to the
12174    frame base (often the same as the CFA), and store it in
12175    frame_pointer_fb_offset.  OFFSET is added to the displacement
12176    before the latter is negated.  */
12177
12178 static void
12179 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12180 {
12181   rtx reg, elim;
12182
12183 #ifdef FRAME_POINTER_CFA_OFFSET
12184   reg = frame_pointer_rtx;
12185   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12186 #else
12187   reg = arg_pointer_rtx;
12188   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12189 #endif
12190
12191   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12192   if (GET_CODE (elim) == PLUS)
12193     {
12194       offset += INTVAL (XEXP (elim, 1));
12195       elim = XEXP (elim, 0);
12196     }
12197
12198   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12199                && (elim == hard_frame_pointer_rtx
12200                    || elim == stack_pointer_rtx))
12201               || elim == (frame_pointer_needed
12202                           ? hard_frame_pointer_rtx
12203                           : stack_pointer_rtx));
12204
12205   frame_pointer_fb_offset = -offset;
12206 }
12207
12208 /* Generate a DW_AT_name attribute given some string value to be included as
12209    the value of the attribute.  */
12210
12211 static void
12212 add_name_attribute (dw_die_ref die, const char *name_string)
12213 {
12214   if (name_string != NULL && *name_string != 0)
12215     {
12216       if (demangle_name_func)
12217         name_string = (*demangle_name_func) (name_string);
12218
12219       add_AT_string (die, DW_AT_name, name_string);
12220     }
12221 }
12222
12223 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12224
12225 static void
12226 add_comp_dir_attribute (dw_die_ref die)
12227 {
12228   const char *wd = get_src_pwd ();
12229   if (wd != NULL)
12230     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12231 }
12232
12233 /* Given a tree node describing an array bound (either lower or upper) output
12234    a representation for that bound.  */
12235
12236 static void
12237 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12238 {
12239   switch (TREE_CODE (bound))
12240     {
12241     case ERROR_MARK:
12242       return;
12243
12244     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12245     case INTEGER_CST:
12246       if (! host_integerp (bound, 0)
12247           || (bound_attr == DW_AT_lower_bound
12248               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12249                   || (is_fortran () && integer_onep (bound)))))
12250         /* Use the default.  */
12251         ;
12252       else
12253         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12254       break;
12255
12256     CASE_CONVERT:
12257     case VIEW_CONVERT_EXPR:
12258       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12259       break;
12260
12261     case SAVE_EXPR:
12262       break;
12263
12264     case VAR_DECL:
12265     case PARM_DECL:
12266     case RESULT_DECL:
12267       {
12268         dw_die_ref decl_die = lookup_decl_die (bound);
12269         dw_loc_descr_ref loc;
12270
12271         /* ??? Can this happen, or should the variable have been bound
12272            first?  Probably it can, since I imagine that we try to create
12273            the types of parameters in the order in which they exist in
12274            the list, and won't have created a forward reference to a
12275            later parameter.  */
12276         if (decl_die != NULL)
12277           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12278         else
12279           {
12280             loc = loc_descriptor_from_tree_1 (bound, 0);
12281             add_AT_location_description (subrange_die, bound_attr, loc);
12282           }
12283         break;
12284       }
12285
12286     default:
12287       {
12288         /* Otherwise try to create a stack operation procedure to
12289            evaluate the value of the array bound.  */
12290
12291         dw_die_ref ctx, decl_die;
12292         dw_loc_descr_ref loc;
12293
12294         loc = loc_descriptor_from_tree (bound);
12295         if (loc == NULL)
12296           break;
12297
12298         if (current_function_decl == 0)
12299           ctx = comp_unit_die;
12300         else
12301           ctx = lookup_decl_die (current_function_decl);
12302
12303         decl_die = new_die (DW_TAG_variable, ctx, bound);
12304         add_AT_flag (decl_die, DW_AT_artificial, 1);
12305         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12306         add_AT_loc (decl_die, DW_AT_location, loc);
12307
12308         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12309         break;
12310       }
12311     }
12312 }
12313
12314 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12315    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12316    Note that the block of subscript information for an array type also
12317    includes information about the element type of the given array type.  */
12318
12319 static void
12320 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12321 {
12322   unsigned dimension_number;
12323   tree lower, upper;
12324   dw_die_ref subrange_die;
12325
12326   for (dimension_number = 0;
12327        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12328        type = TREE_TYPE (type), dimension_number++)
12329     {
12330       tree domain = TYPE_DOMAIN (type);
12331
12332       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12333         break;
12334
12335       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12336          and (in GNU C only) variable bounds.  Handle all three forms
12337          here.  */
12338       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12339       if (domain)
12340         {
12341           /* We have an array type with specified bounds.  */
12342           lower = TYPE_MIN_VALUE (domain);
12343           upper = TYPE_MAX_VALUE (domain);
12344
12345           /* Define the index type.  */
12346           if (TREE_TYPE (domain))
12347             {
12348               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12349                  TREE_TYPE field.  We can't emit debug info for this
12350                  because it is an unnamed integral type.  */
12351               if (TREE_CODE (domain) == INTEGER_TYPE
12352                   && TYPE_NAME (domain) == NULL_TREE
12353                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12354                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12355                 ;
12356               else
12357                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12358                                     type_die);
12359             }
12360
12361           /* ??? If upper is NULL, the array has unspecified length,
12362              but it does have a lower bound.  This happens with Fortran
12363                dimension arr(N:*)
12364              Since the debugger is definitely going to need to know N
12365              to produce useful results, go ahead and output the lower
12366              bound solo, and hope the debugger can cope.  */
12367
12368           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12369           if (upper)
12370             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12371         }
12372
12373       /* Otherwise we have an array type with an unspecified length.  The
12374          DWARF-2 spec does not say how to handle this; let's just leave out the
12375          bounds.  */
12376     }
12377 }
12378
12379 static void
12380 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12381 {
12382   unsigned size;
12383
12384   switch (TREE_CODE (tree_node))
12385     {
12386     case ERROR_MARK:
12387       size = 0;
12388       break;
12389     case ENUMERAL_TYPE:
12390     case RECORD_TYPE:
12391     case UNION_TYPE:
12392     case QUAL_UNION_TYPE:
12393       size = int_size_in_bytes (tree_node);
12394       break;
12395     case FIELD_DECL:
12396       /* For a data member of a struct or union, the DW_AT_byte_size is
12397          generally given as the number of bytes normally allocated for an
12398          object of the *declared* type of the member itself.  This is true
12399          even for bit-fields.  */
12400       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12401       break;
12402     default:
12403       gcc_unreachable ();
12404     }
12405
12406   /* Note that `size' might be -1 when we get to this point.  If it is, that
12407      indicates that the byte size of the entity in question is variable.  We
12408      have no good way of expressing this fact in Dwarf at the present time,
12409      so just let the -1 pass on through.  */
12410   add_AT_unsigned (die, DW_AT_byte_size, size);
12411 }
12412
12413 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12414    which specifies the distance in bits from the highest order bit of the
12415    "containing object" for the bit-field to the highest order bit of the
12416    bit-field itself.
12417
12418    For any given bit-field, the "containing object" is a hypothetical object
12419    (of some integral or enum type) within which the given bit-field lives.  The
12420    type of this hypothetical "containing object" is always the same as the
12421    declared type of the individual bit-field itself.  The determination of the
12422    exact location of the "containing object" for a bit-field is rather
12423    complicated.  It's handled by the `field_byte_offset' function (above).
12424
12425    Note that it is the size (in bytes) of the hypothetical "containing object"
12426    which will be given in the DW_AT_byte_size attribute for this bit-field.
12427    (See `byte_size_attribute' above).  */
12428
12429 static inline void
12430 add_bit_offset_attribute (dw_die_ref die, tree decl)
12431 {
12432   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12433   tree type = DECL_BIT_FIELD_TYPE (decl);
12434   HOST_WIDE_INT bitpos_int;
12435   HOST_WIDE_INT highest_order_object_bit_offset;
12436   HOST_WIDE_INT highest_order_field_bit_offset;
12437   HOST_WIDE_INT unsigned bit_offset;
12438
12439   /* Must be a field and a bit field.  */
12440   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12441
12442   /* We can't yet handle bit-fields whose offsets are variable, so if we
12443      encounter such things, just return without generating any attribute
12444      whatsoever.  Likewise for variable or too large size.  */
12445   if (! host_integerp (bit_position (decl), 0)
12446       || ! host_integerp (DECL_SIZE (decl), 1))
12447     return;
12448
12449   bitpos_int = int_bit_position (decl);
12450
12451   /* Note that the bit offset is always the distance (in bits) from the
12452      highest-order bit of the "containing object" to the highest-order bit of
12453      the bit-field itself.  Since the "high-order end" of any object or field
12454      is different on big-endian and little-endian machines, the computation
12455      below must take account of these differences.  */
12456   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12457   highest_order_field_bit_offset = bitpos_int;
12458
12459   if (! BYTES_BIG_ENDIAN)
12460     {
12461       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12462       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12463     }
12464
12465   bit_offset
12466     = (! BYTES_BIG_ENDIAN
12467        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12468        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12469
12470   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12471 }
12472
12473 /* For a FIELD_DECL node which represents a bit field, output an attribute
12474    which specifies the length in bits of the given field.  */
12475
12476 static inline void
12477 add_bit_size_attribute (dw_die_ref die, tree decl)
12478 {
12479   /* Must be a field and a bit field.  */
12480   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12481               && DECL_BIT_FIELD_TYPE (decl));
12482
12483   if (host_integerp (DECL_SIZE (decl), 1))
12484     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12485 }
12486
12487 /* If the compiled language is ANSI C, then add a 'prototyped'
12488    attribute, if arg types are given for the parameters of a function.  */
12489
12490 static inline void
12491 add_prototyped_attribute (dw_die_ref die, tree func_type)
12492 {
12493   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12494       && TYPE_ARG_TYPES (func_type) != NULL)
12495     add_AT_flag (die, DW_AT_prototyped, 1);
12496 }
12497
12498 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12499    by looking in either the type declaration or object declaration
12500    equate table.  */
12501
12502 static inline void
12503 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12504 {
12505   dw_die_ref origin_die = NULL;
12506
12507   if (TREE_CODE (origin) != FUNCTION_DECL)
12508     {
12509       /* We may have gotten separated from the block for the inlined
12510          function, if we're in an exception handler or some such; make
12511          sure that the abstract function has been written out.
12512
12513          Doing this for nested functions is wrong, however; functions are
12514          distinct units, and our context might not even be inline.  */
12515       tree fn = origin;
12516
12517       if (TYPE_P (fn))
12518         fn = TYPE_STUB_DECL (fn);
12519
12520       fn = decl_function_context (fn);
12521       if (fn)
12522         dwarf2out_abstract_function (fn);
12523     }
12524
12525   if (DECL_P (origin))
12526     origin_die = lookup_decl_die (origin);
12527   else if (TYPE_P (origin))
12528     origin_die = lookup_type_die (origin);
12529
12530   /* XXX: Functions that are never lowered don't always have correct block
12531      trees (in the case of java, they simply have no block tree, in some other
12532      languages).  For these functions, there is nothing we can really do to
12533      output correct debug info for inlined functions in all cases.  Rather
12534      than die, we'll just produce deficient debug info now, in that we will
12535      have variables without a proper abstract origin.  In the future, when all
12536      functions are lowered, we should re-add a gcc_assert (origin_die)
12537      here.  */
12538
12539   if (origin_die)
12540       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12541 }
12542
12543 /* We do not currently support the pure_virtual attribute.  */
12544
12545 static inline void
12546 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12547 {
12548   if (DECL_VINDEX (func_decl))
12549     {
12550       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12551
12552       if (host_integerp (DECL_VINDEX (func_decl), 0))
12553         add_AT_loc (die, DW_AT_vtable_elem_location,
12554                     new_loc_descr (DW_OP_constu,
12555                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12556                                    0));
12557
12558       /* GNU extension: Record what type this method came from originally.  */
12559       if (debug_info_level > DINFO_LEVEL_TERSE)
12560         add_AT_die_ref (die, DW_AT_containing_type,
12561                         lookup_type_die (DECL_CONTEXT (func_decl)));
12562     }
12563 }
12564 \f
12565 /* Add source coordinate attributes for the given decl.  */
12566
12567 static void
12568 add_src_coords_attributes (dw_die_ref die, tree decl)
12569 {
12570   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12571
12572   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12573   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12574 }
12575
12576 /* Add a DW_AT_name attribute and source coordinate attribute for the
12577    given decl, but only if it actually has a name.  */
12578
12579 static void
12580 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12581 {
12582   tree decl_name;
12583
12584   decl_name = DECL_NAME (decl);
12585   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12586     {
12587       add_name_attribute (die, dwarf2_name (decl, 0));
12588       if (! DECL_ARTIFICIAL (decl))
12589         add_src_coords_attributes (die, decl);
12590
12591       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12592           && TREE_PUBLIC (decl)
12593           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12594           && !DECL_ABSTRACT (decl)
12595           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12596           && !is_fortran ())
12597         add_AT_string (die, DW_AT_MIPS_linkage_name,
12598                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12599     }
12600
12601 #ifdef VMS_DEBUGGING_INFO
12602   /* Get the function's name, as described by its RTL.  This may be different
12603      from the DECL_NAME name used in the source file.  */
12604   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12605     {
12606       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12607                    XEXP (DECL_RTL (decl), 0));
12608       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12609     }
12610 #endif
12611 }
12612
12613 /* Push a new declaration scope.  */
12614
12615 static void
12616 push_decl_scope (tree scope)
12617 {
12618   VEC_safe_push (tree, gc, decl_scope_table, scope);
12619 }
12620
12621 /* Pop a declaration scope.  */
12622
12623 static inline void
12624 pop_decl_scope (void)
12625 {
12626   VEC_pop (tree, decl_scope_table);
12627 }
12628
12629 /* Return the DIE for the scope that immediately contains this type.
12630    Non-named types get global scope.  Named types nested in other
12631    types get their containing scope if it's open, or global scope
12632    otherwise.  All other types (i.e. function-local named types) get
12633    the current active scope.  */
12634
12635 static dw_die_ref
12636 scope_die_for (tree t, dw_die_ref context_die)
12637 {
12638   dw_die_ref scope_die = NULL;
12639   tree containing_scope;
12640   int i;
12641
12642   /* Non-types always go in the current scope.  */
12643   gcc_assert (TYPE_P (t));
12644
12645   containing_scope = TYPE_CONTEXT (t);
12646
12647   /* Use the containing namespace if it was passed in (for a declaration).  */
12648   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12649     {
12650       if (context_die == lookup_decl_die (containing_scope))
12651         /* OK */;
12652       else
12653         containing_scope = NULL_TREE;
12654     }
12655
12656   /* Ignore function type "scopes" from the C frontend.  They mean that
12657      a tagged type is local to a parmlist of a function declarator, but
12658      that isn't useful to DWARF.  */
12659   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12660     containing_scope = NULL_TREE;
12661
12662   if (containing_scope == NULL_TREE)
12663     scope_die = comp_unit_die;
12664   else if (TYPE_P (containing_scope))
12665     {
12666       /* For types, we can just look up the appropriate DIE.  But
12667          first we check to see if we're in the middle of emitting it
12668          so we know where the new DIE should go.  */
12669       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12670         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12671           break;
12672
12673       if (i < 0)
12674         {
12675           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12676                       || TREE_ASM_WRITTEN (containing_scope));
12677
12678           /* If none of the current dies are suitable, we get file scope.  */
12679           scope_die = comp_unit_die;
12680         }
12681       else
12682         scope_die = lookup_type_die (containing_scope);
12683     }
12684   else
12685     scope_die = context_die;
12686
12687   return scope_die;
12688 }
12689
12690 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12691
12692 static inline int
12693 local_scope_p (dw_die_ref context_die)
12694 {
12695   for (; context_die; context_die = context_die->die_parent)
12696     if (context_die->die_tag == DW_TAG_inlined_subroutine
12697         || context_die->die_tag == DW_TAG_subprogram)
12698       return 1;
12699
12700   return 0;
12701 }
12702
12703 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12704    whether or not to treat a DIE in this context as a declaration.  */
12705
12706 static inline int
12707 class_or_namespace_scope_p (dw_die_ref context_die)
12708 {
12709   return (context_die
12710           && (context_die->die_tag == DW_TAG_structure_type
12711               || context_die->die_tag == DW_TAG_class_type
12712               || context_die->die_tag == DW_TAG_interface_type
12713               || context_die->die_tag == DW_TAG_union_type
12714               || context_die->die_tag == DW_TAG_namespace));
12715 }
12716
12717 /* Many forms of DIEs require a "type description" attribute.  This
12718    routine locates the proper "type descriptor" die for the type given
12719    by 'type', and adds a DW_AT_type attribute below the given die.  */
12720
12721 static void
12722 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12723                     int decl_volatile, dw_die_ref context_die)
12724 {
12725   enum tree_code code  = TREE_CODE (type);
12726   dw_die_ref type_die  = NULL;
12727
12728   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12729      or fixed-point type, use the inner type.  This is because we have no
12730      support for unnamed types in base_type_die.  This can happen if this is
12731      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12732   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12733       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12734     type = TREE_TYPE (type), code = TREE_CODE (type);
12735
12736   if (code == ERROR_MARK
12737       /* Handle a special case.  For functions whose return type is void, we
12738          generate *no* type attribute.  (Note that no object may have type
12739          `void', so this only applies to function return types).  */
12740       || code == VOID_TYPE)
12741     return;
12742
12743   type_die = modified_type_die (type,
12744                                 decl_const || TYPE_READONLY (type),
12745                                 decl_volatile || TYPE_VOLATILE (type),
12746                                 context_die);
12747
12748   if (type_die != NULL)
12749     add_AT_die_ref (object_die, DW_AT_type, type_die);
12750 }
12751
12752 /* Given an object die, add the calling convention attribute for the
12753    function call type.  */
12754 static void
12755 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12756 {
12757   enum dwarf_calling_convention value = DW_CC_normal;
12758
12759   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12760
12761   /* DWARF doesn't provide a way to identify a program's source-level
12762      entry point.  DW_AT_calling_convention attributes are only meant
12763      to describe functions' calling conventions.  However, lacking a
12764      better way to signal the Fortran main program, we use this for the
12765      time being, following existing custom.  */
12766   if (is_fortran ()
12767       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12768     value = DW_CC_program;
12769
12770   /* Only add the attribute if the backend requests it, and
12771      is not DW_CC_normal.  */
12772   if (value && (value != DW_CC_normal))
12773     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12774 }
12775
12776 /* Given a tree pointer to a struct, class, union, or enum type node, return
12777    a pointer to the (string) tag name for the given type, or zero if the type
12778    was declared without a tag.  */
12779
12780 static const char *
12781 type_tag (const_tree type)
12782 {
12783   const char *name = 0;
12784
12785   if (TYPE_NAME (type) != 0)
12786     {
12787       tree t = 0;
12788
12789       /* Find the IDENTIFIER_NODE for the type name.  */
12790       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12791         t = TYPE_NAME (type);
12792
12793       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12794          a TYPE_DECL node, regardless of whether or not a `typedef' was
12795          involved.  */
12796       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12797                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12798         {
12799           /* We want to be extra verbose.  Don't call dwarf_name if
12800              DECL_NAME isn't set.  The default hook for decl_printable_name
12801              doesn't like that, and in this context it's correct to return
12802              0, instead of "<anonymous>" or the like.  */
12803           if (DECL_NAME (TYPE_NAME (type)))
12804             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12805         }
12806
12807       /* Now get the name as a string, or invent one.  */
12808       if (!name && t != 0)
12809         name = IDENTIFIER_POINTER (t);
12810     }
12811
12812   return (name == 0 || *name == '\0') ? 0 : name;
12813 }
12814
12815 /* Return the type associated with a data member, make a special check
12816    for bit field types.  */
12817
12818 static inline tree
12819 member_declared_type (const_tree member)
12820 {
12821   return (DECL_BIT_FIELD_TYPE (member)
12822           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12823 }
12824
12825 /* Get the decl's label, as described by its RTL. This may be different
12826    from the DECL_NAME name used in the source file.  */
12827
12828 #if 0
12829 static const char *
12830 decl_start_label (tree decl)
12831 {
12832   rtx x;
12833   const char *fnname;
12834
12835   x = DECL_RTL (decl);
12836   gcc_assert (MEM_P (x));
12837
12838   x = XEXP (x, 0);
12839   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12840
12841   fnname = XSTR (x, 0);
12842   return fnname;
12843 }
12844 #endif
12845 \f
12846 /* These routines generate the internal representation of the DIE's for
12847    the compilation unit.  Debugging information is collected by walking
12848    the declaration trees passed in from dwarf2out_decl().  */
12849
12850 static void
12851 gen_array_type_die (tree type, dw_die_ref context_die)
12852 {
12853   dw_die_ref scope_die = scope_die_for (type, context_die);
12854   dw_die_ref array_die;
12855
12856   /* GNU compilers represent multidimensional array types as sequences of one
12857      dimensional array types whose element types are themselves array types.
12858      We sometimes squish that down to a single array_type DIE with multiple
12859      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12860      say that we are allowed to do this kind of compression in C, because
12861      there is no difference between an array of arrays and a multidimensional
12862      array.  We don't do this for Ada to remain as close as possible to the
12863      actual representation, which is especially important against the language
12864      flexibilty wrt arrays of variable size.  */
12865
12866   bool collapse_nested_arrays = !is_ada ();
12867   tree element_type;
12868
12869   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12870      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12871   if (TYPE_STRING_FLAG (type)
12872       && TREE_CODE (type) == ARRAY_TYPE
12873       && is_fortran ()
12874       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12875     {
12876       HOST_WIDE_INT size;
12877
12878       array_die = new_die (DW_TAG_string_type, scope_die, type);
12879       add_name_attribute (array_die, type_tag (type));
12880       equate_type_number_to_die (type, array_die);
12881       size = int_size_in_bytes (type);
12882       if (size >= 0)
12883         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12884       else if (TYPE_DOMAIN (type) != NULL_TREE
12885                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12886                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12887         {
12888           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12889           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12890
12891           size = int_size_in_bytes (TREE_TYPE (szdecl));
12892           if (loc && size > 0)
12893             {
12894               add_AT_loc (array_die, DW_AT_string_length, loc);
12895               if (size != DWARF2_ADDR_SIZE)
12896                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12897             }
12898         }
12899       return;
12900     }
12901
12902   /* ??? The SGI dwarf reader fails for array of array of enum types
12903      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12904      array type comes before the outer array type.  We thus call gen_type_die
12905      before we new_die and must prevent nested array types collapsing for this
12906      target.  */
12907
12908 #ifdef MIPS_DEBUGGING_INFO
12909   gen_type_die (TREE_TYPE (type), context_die);
12910   collapse_nested_arrays = false;
12911 #endif
12912
12913   array_die = new_die (DW_TAG_array_type, scope_die, type);
12914   add_name_attribute (array_die, type_tag (type));
12915   equate_type_number_to_die (type, array_die);
12916
12917   if (TREE_CODE (type) == VECTOR_TYPE)
12918     {
12919       /* The frontend feeds us a representation for the vector as a struct
12920          containing an array.  Pull out the array type.  */
12921       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12922       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12923     }
12924
12925   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12926   if (is_fortran ()
12927       && TREE_CODE (type) == ARRAY_TYPE
12928       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12929       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12930     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12931
12932 #if 0
12933   /* We default the array ordering.  SDB will probably do
12934      the right things even if DW_AT_ordering is not present.  It's not even
12935      an issue until we start to get into multidimensional arrays anyway.  If
12936      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12937      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12938      and when we find out that we need to put these in, we will only do so
12939      for multidimensional arrays.  */
12940   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12941 #endif
12942
12943 #ifdef MIPS_DEBUGGING_INFO
12944   /* The SGI compilers handle arrays of unknown bound by setting
12945      AT_declaration and not emitting any subrange DIEs.  */
12946   if (! TYPE_DOMAIN (type))
12947     add_AT_flag (array_die, DW_AT_declaration, 1);
12948   else
12949 #endif
12950     add_subscript_info (array_die, type, collapse_nested_arrays);
12951
12952   /* Add representation of the type of the elements of this array type and
12953      emit the corresponding DIE if we haven't done it already.  */  
12954   element_type = TREE_TYPE (type);
12955   if (collapse_nested_arrays)
12956     while (TREE_CODE (element_type) == ARRAY_TYPE)
12957       {
12958         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12959           break;
12960         element_type = TREE_TYPE (element_type);
12961       }
12962
12963 #ifndef MIPS_DEBUGGING_INFO
12964   gen_type_die (element_type, context_die);
12965 #endif
12966
12967   add_type_attribute (array_die, element_type, 0, 0, context_die);
12968
12969   if (get_AT (array_die, DW_AT_name))
12970     add_pubtype (type, array_die);
12971 }
12972
12973 static dw_loc_descr_ref
12974 descr_info_loc (tree val, tree base_decl)
12975 {
12976   HOST_WIDE_INT size;
12977   dw_loc_descr_ref loc, loc2;
12978   enum dwarf_location_atom op;
12979
12980   if (val == base_decl)
12981     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12982
12983   switch (TREE_CODE (val))
12984     {
12985     CASE_CONVERT:
12986       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12987     case VAR_DECL:
12988       return loc_descriptor_from_tree_1 (val, 0);
12989     case INTEGER_CST:
12990       if (host_integerp (val, 0))
12991         return int_loc_descriptor (tree_low_cst (val, 0));
12992       break;
12993     case INDIRECT_REF:
12994       size = int_size_in_bytes (TREE_TYPE (val));
12995       if (size < 0)
12996         break;
12997       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12998       if (!loc)
12999         break;
13000       if (size == DWARF2_ADDR_SIZE)
13001         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13002       else
13003         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13004       return loc;
13005     case POINTER_PLUS_EXPR:
13006     case PLUS_EXPR:
13007       if (host_integerp (TREE_OPERAND (val, 1), 1)
13008           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13009              < 16384)
13010         {
13011           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13012           if (!loc)
13013             break;
13014           add_loc_descr (&loc,
13015                          new_loc_descr (DW_OP_plus_uconst,
13016                                         tree_low_cst (TREE_OPERAND (val, 1),
13017                                                       1), 0));
13018         }
13019       else
13020         {
13021           op = DW_OP_plus;
13022         do_binop:
13023           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13024           if (!loc)
13025             break;
13026           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13027           if (!loc2)
13028             break;
13029           add_loc_descr (&loc, loc2);
13030           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13031         }
13032       return loc;
13033     case MINUS_EXPR:
13034       op = DW_OP_minus;
13035       goto do_binop;
13036     case MULT_EXPR:
13037       op = DW_OP_mul;
13038       goto do_binop;
13039     case EQ_EXPR:
13040       op = DW_OP_eq;
13041       goto do_binop;
13042     case NE_EXPR:
13043       op = DW_OP_ne;
13044       goto do_binop;
13045     default:
13046       break;
13047     }
13048   return NULL;
13049 }
13050
13051 static void
13052 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13053                       tree val, tree base_decl)
13054 {
13055   dw_loc_descr_ref loc;
13056
13057   if (host_integerp (val, 0))
13058     {
13059       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13060       return;
13061     }
13062
13063   loc = descr_info_loc (val, base_decl);
13064   if (!loc)
13065     return;
13066
13067   add_AT_loc (die, attr, loc);
13068 }
13069
13070 /* This routine generates DIE for array with hidden descriptor, details
13071    are filled into *info by a langhook.  */
13072
13073 static void
13074 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13075                           dw_die_ref context_die)
13076 {
13077   dw_die_ref scope_die = scope_die_for (type, context_die);
13078   dw_die_ref array_die;
13079   int dim;
13080
13081   array_die = new_die (DW_TAG_array_type, scope_die, type);
13082   add_name_attribute (array_die, type_tag (type));
13083   equate_type_number_to_die (type, array_die);
13084
13085   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13086   if (is_fortran ()
13087       && info->ndimensions >= 2)
13088     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13089
13090   if (info->data_location)
13091     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13092                           info->base_decl);
13093   if (info->associated)
13094     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13095                           info->base_decl);
13096   if (info->allocated)
13097     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13098                           info->base_decl);
13099
13100   for (dim = 0; dim < info->ndimensions; dim++)
13101     {
13102       dw_die_ref subrange_die
13103         = new_die (DW_TAG_subrange_type, array_die, NULL);
13104
13105       if (info->dimen[dim].lower_bound)
13106         {
13107           /* If it is the default value, omit it.  */
13108           if ((is_c_family () || is_java ())
13109               && integer_zerop (info->dimen[dim].lower_bound))
13110             ;
13111           else if (is_fortran ()
13112                    && integer_onep (info->dimen[dim].lower_bound))
13113             ;
13114           else
13115             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13116                                   info->dimen[dim].lower_bound,
13117                                   info->base_decl);
13118         }
13119       if (info->dimen[dim].upper_bound)
13120         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13121                               info->dimen[dim].upper_bound,
13122                               info->base_decl);
13123       if (info->dimen[dim].stride)
13124         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13125                               info->dimen[dim].stride,
13126                               info->base_decl);
13127     }
13128
13129   gen_type_die (info->element_type, context_die);
13130   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13131
13132   if (get_AT (array_die, DW_AT_name))
13133     add_pubtype (type, array_die);
13134 }
13135
13136 #if 0
13137 static void
13138 gen_entry_point_die (tree decl, dw_die_ref context_die)
13139 {
13140   tree origin = decl_ultimate_origin (decl);
13141   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13142
13143   if (origin != NULL)
13144     add_abstract_origin_attribute (decl_die, origin);
13145   else
13146     {
13147       add_name_and_src_coords_attributes (decl_die, decl);
13148       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13149                           0, 0, context_die);
13150     }
13151
13152   if (DECL_ABSTRACT (decl))
13153     equate_decl_number_to_die (decl, decl_die);
13154   else
13155     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13156 }
13157 #endif
13158
13159 /* Walk through the list of incomplete types again, trying once more to
13160    emit full debugging info for them.  */
13161
13162 static void
13163 retry_incomplete_types (void)
13164 {
13165   int i;
13166
13167   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13168     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13169 }
13170
13171 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
13172
13173 static void
13174 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
13175 {
13176   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
13177
13178   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13179      be incomplete and such types are not marked.  */
13180   add_abstract_origin_attribute (type_die, type);
13181 }
13182
13183 /* Determine what tag to use for a record type.  */
13184
13185 static enum dwarf_tag
13186 record_type_tag (tree type)
13187 {
13188   if (! lang_hooks.types.classify_record)
13189     return DW_TAG_structure_type;
13190
13191   switch (lang_hooks.types.classify_record (type))
13192     {
13193     case RECORD_IS_STRUCT:
13194       return DW_TAG_structure_type;
13195
13196     case RECORD_IS_CLASS:
13197       return DW_TAG_class_type;
13198
13199     case RECORD_IS_INTERFACE:
13200       return DW_TAG_interface_type;
13201
13202     default:
13203       gcc_unreachable ();
13204     }
13205 }
13206
13207 /* Generate a DIE to represent an inlined instance of a structure type.  */
13208
13209 static void
13210 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
13211 {
13212   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
13213
13214   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13215      be incomplete and such types are not marked.  */
13216   add_abstract_origin_attribute (type_die, type);
13217 }
13218
13219 /* Generate a DIE to represent an inlined instance of a union type.  */
13220
13221 static void
13222 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
13223 {
13224   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
13225
13226   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13227      be incomplete and such types are not marked.  */
13228   add_abstract_origin_attribute (type_die, type);
13229 }
13230
13231 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13232    include all of the information about the enumeration values also. Each
13233    enumerated type name/value is listed as a child of the enumerated type
13234    DIE.  */
13235
13236 static dw_die_ref
13237 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13238 {
13239   dw_die_ref type_die = lookup_type_die (type);
13240
13241   if (type_die == NULL)
13242     {
13243       type_die = new_die (DW_TAG_enumeration_type,
13244                           scope_die_for (type, context_die), type);
13245       equate_type_number_to_die (type, type_die);
13246       add_name_attribute (type_die, type_tag (type));
13247     }
13248   else if (! TYPE_SIZE (type))
13249     return type_die;
13250   else
13251     remove_AT (type_die, DW_AT_declaration);
13252
13253   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13254      given enum type is incomplete, do not generate the DW_AT_byte_size
13255      attribute or the DW_AT_element_list attribute.  */
13256   if (TYPE_SIZE (type))
13257     {
13258       tree link;
13259
13260       TREE_ASM_WRITTEN (type) = 1;
13261       add_byte_size_attribute (type_die, type);
13262       if (TYPE_STUB_DECL (type) != NULL_TREE)
13263         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13264
13265       /* If the first reference to this type was as the return type of an
13266          inline function, then it may not have a parent.  Fix this now.  */
13267       if (type_die->die_parent == NULL)
13268         add_child_die (scope_die_for (type, context_die), type_die);
13269
13270       for (link = TYPE_VALUES (type);
13271            link != NULL; link = TREE_CHAIN (link))
13272         {
13273           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13274           tree value = TREE_VALUE (link);
13275
13276           add_name_attribute (enum_die,
13277                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13278
13279           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13280             /* DWARF2 does not provide a way of indicating whether or
13281                not enumeration constants are signed or unsigned.  GDB
13282                always assumes the values are signed, so we output all
13283                values as if they were signed.  That means that
13284                enumeration constants with very large unsigned values
13285                will appear to have negative values in the debugger.  */
13286             add_AT_int (enum_die, DW_AT_const_value,
13287                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13288         }
13289     }
13290   else
13291     add_AT_flag (type_die, DW_AT_declaration, 1);
13292
13293   if (get_AT (type_die, DW_AT_name))
13294     add_pubtype (type, type_die);
13295
13296   return type_die;
13297 }
13298
13299 /* Generate a DIE to represent either a real live formal parameter decl or to
13300    represent just the type of some formal parameter position in some function
13301    type.
13302
13303    Note that this routine is a bit unusual because its argument may be a
13304    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13305    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13306    node.  If it's the former then this function is being called to output a
13307    DIE to represent a formal parameter object (or some inlining thereof).  If
13308    it's the latter, then this function is only being called to output a
13309    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13310    argument type of some subprogram type.  */
13311
13312 static dw_die_ref
13313 gen_formal_parameter_die (tree node, dw_die_ref context_die)
13314 {
13315   dw_die_ref parm_die
13316     = new_die (DW_TAG_formal_parameter, context_die, node);
13317   tree origin;
13318
13319   switch (TREE_CODE_CLASS (TREE_CODE (node)))
13320     {
13321     case tcc_declaration:
13322       origin = decl_ultimate_origin (node);
13323       if (origin != NULL)
13324         add_abstract_origin_attribute (parm_die, origin);
13325       else
13326         {
13327           tree type = TREE_TYPE (node);
13328           add_name_and_src_coords_attributes (parm_die, node);
13329           if (DECL_BY_REFERENCE (node))
13330             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13331                                 context_die);
13332           else
13333             add_type_attribute (parm_die, type,
13334                                 TREE_READONLY (node),
13335                                 TREE_THIS_VOLATILE (node),
13336                                 context_die);
13337           if (DECL_ARTIFICIAL (node))
13338             add_AT_flag (parm_die, DW_AT_artificial, 1);
13339         }
13340
13341       equate_decl_number_to_die (node, parm_die);
13342       if (! DECL_ABSTRACT (node))
13343         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
13344
13345       break;
13346
13347     case tcc_type:
13348       /* We were called with some kind of a ..._TYPE node.  */
13349       add_type_attribute (parm_die, node, 0, 0, context_die);
13350       break;
13351
13352     default:
13353       gcc_unreachable ();
13354     }
13355
13356   return parm_die;
13357 }
13358
13359 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13360    at the end of an (ANSI prototyped) formal parameters list.  */
13361
13362 static void
13363 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13364 {
13365   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13366 }
13367
13368 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13369    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13370    parameters as specified in some function type specification (except for
13371    those which appear as part of a function *definition*).  */
13372
13373 static void
13374 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13375 {
13376   tree link;
13377   tree formal_type = NULL;
13378   tree first_parm_type;
13379   tree arg;
13380
13381   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13382     {
13383       arg = DECL_ARGUMENTS (function_or_method_type);
13384       function_or_method_type = TREE_TYPE (function_or_method_type);
13385     }
13386   else
13387     arg = NULL_TREE;
13388
13389   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13390
13391   /* Make our first pass over the list of formal parameter types and output a
13392      DW_TAG_formal_parameter DIE for each one.  */
13393   for (link = first_parm_type; link; )
13394     {
13395       dw_die_ref parm_die;
13396
13397       formal_type = TREE_VALUE (link);
13398       if (formal_type == void_type_node)
13399         break;
13400
13401       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13402       parm_die = gen_formal_parameter_die (formal_type, context_die);
13403       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13404            && link == first_parm_type)
13405           || (arg && DECL_ARTIFICIAL (arg)))
13406         add_AT_flag (parm_die, DW_AT_artificial, 1);
13407
13408       link = TREE_CHAIN (link);
13409       if (arg)
13410         arg = TREE_CHAIN (arg);
13411     }
13412
13413   /* If this function type has an ellipsis, add a
13414      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13415   if (formal_type != void_type_node)
13416     gen_unspecified_parameters_die (function_or_method_type, context_die);
13417
13418   /* Make our second (and final) pass over the list of formal parameter types
13419      and output DIEs to represent those types (as necessary).  */
13420   for (link = TYPE_ARG_TYPES (function_or_method_type);
13421        link && TREE_VALUE (link);
13422        link = TREE_CHAIN (link))
13423     gen_type_die (TREE_VALUE (link), context_die);
13424 }
13425
13426 /* We want to generate the DIE for TYPE so that we can generate the
13427    die for MEMBER, which has been defined; we will need to refer back
13428    to the member declaration nested within TYPE.  If we're trying to
13429    generate minimal debug info for TYPE, processing TYPE won't do the
13430    trick; we need to attach the member declaration by hand.  */
13431
13432 static void
13433 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13434 {
13435   gen_type_die (type, context_die);
13436
13437   /* If we're trying to avoid duplicate debug info, we may not have
13438      emitted the member decl for this function.  Emit it now.  */
13439   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13440       && ! lookup_decl_die (member))
13441     {
13442       dw_die_ref type_die;
13443       gcc_assert (!decl_ultimate_origin (member));
13444
13445       push_decl_scope (type);
13446       type_die = lookup_type_die (type);
13447       if (TREE_CODE (member) == FUNCTION_DECL)
13448         gen_subprogram_die (member, type_die);
13449       else if (TREE_CODE (member) == FIELD_DECL)
13450         {
13451           /* Ignore the nameless fields that are used to skip bits but handle
13452              C++ anonymous unions and structs.  */
13453           if (DECL_NAME (member) != NULL_TREE
13454               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13455               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13456             {
13457               gen_type_die (member_declared_type (member), type_die);
13458               gen_field_die (member, type_die);
13459             }
13460         }
13461       else
13462         gen_variable_die (member, type_die);
13463
13464       pop_decl_scope ();
13465     }
13466 }
13467
13468 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13469    may later generate inlined and/or out-of-line instances of.  */
13470
13471 static void
13472 dwarf2out_abstract_function (tree decl)
13473 {
13474   dw_die_ref old_die;
13475   tree save_fn;
13476   tree context;
13477   int was_abstract = DECL_ABSTRACT (decl);
13478
13479   /* Make sure we have the actual abstract inline, not a clone.  */
13480   decl = DECL_ORIGIN (decl);
13481
13482   old_die = lookup_decl_die (decl);
13483   if (old_die && get_AT (old_die, DW_AT_inline))
13484     /* We've already generated the abstract instance.  */
13485     return;
13486
13487   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13488      we don't get confused by DECL_ABSTRACT.  */
13489   if (debug_info_level > DINFO_LEVEL_TERSE)
13490     {
13491       context = decl_class_context (decl);
13492       if (context)
13493         gen_type_die_for_member
13494           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13495     }
13496
13497   /* Pretend we've just finished compiling this function.  */
13498   save_fn = current_function_decl;
13499   current_function_decl = decl;
13500   push_cfun (DECL_STRUCT_FUNCTION (decl));
13501
13502   set_decl_abstract_flags (decl, 1);
13503   dwarf2out_decl (decl);
13504   if (! was_abstract)
13505     set_decl_abstract_flags (decl, 0);
13506
13507   current_function_decl = save_fn;
13508   pop_cfun ();
13509 }
13510
13511 /* Helper function of premark_used_types() which gets called through
13512    htab_traverse_resize().
13513
13514    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13515    marked as unused by prune_unused_types.  */
13516 static int
13517 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13518 {
13519   tree type;
13520   dw_die_ref die;
13521
13522   type = (tree) *slot;
13523   die = lookup_type_die (type);
13524   if (die != NULL)
13525     die->die_perennial_p = 1;
13526   return 1;
13527 }
13528
13529 /* Mark all members of used_types_hash as perennial.  */
13530 static void
13531 premark_used_types (void)
13532 {
13533   if (cfun && cfun->used_types_hash)
13534     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13535 }
13536
13537 /* Generate a DIE to represent a declared function (either file-scope or
13538    block-local).  */
13539
13540 static void
13541 gen_subprogram_die (tree decl, dw_die_ref context_die)
13542 {
13543   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13544   tree origin = decl_ultimate_origin (decl);
13545   dw_die_ref subr_die;
13546   tree fn_arg_types;
13547   tree outer_scope;
13548   dw_die_ref old_die = lookup_decl_die (decl);
13549   int declaration = (current_function_decl != decl
13550                      || class_or_namespace_scope_p (context_die));
13551
13552   premark_used_types ();
13553
13554   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13555      started to generate the abstract instance of an inline, decided to output
13556      its containing class, and proceeded to emit the declaration of the inline
13557      from the member list for the class.  If so, DECLARATION takes priority;
13558      we'll get back to the abstract instance when done with the class.  */
13559
13560   /* The class-scope declaration DIE must be the primary DIE.  */
13561   if (origin && declaration && class_or_namespace_scope_p (context_die))
13562     {
13563       origin = NULL;
13564       gcc_assert (!old_die);
13565     }
13566
13567   /* Now that the C++ front end lazily declares artificial member fns, we
13568      might need to retrofit the declaration into its class.  */
13569   if (!declaration && !origin && !old_die
13570       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13571       && !class_or_namespace_scope_p (context_die)
13572       && debug_info_level > DINFO_LEVEL_TERSE)
13573     old_die = force_decl_die (decl);
13574
13575   if (origin != NULL)
13576     {
13577       gcc_assert (!declaration || local_scope_p (context_die));
13578
13579       /* Fixup die_parent for the abstract instance of a nested
13580          inline function.  */
13581       if (old_die && old_die->die_parent == NULL)
13582         add_child_die (context_die, old_die);
13583
13584       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13585       add_abstract_origin_attribute (subr_die, origin);
13586     }
13587   else if (old_die)
13588     {
13589       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13590       struct dwarf_file_data * file_index = lookup_filename (s.file);
13591
13592       if (!get_AT_flag (old_die, DW_AT_declaration)
13593           /* We can have a normal definition following an inline one in the
13594              case of redefinition of GNU C extern inlines.
13595              It seems reasonable to use AT_specification in this case.  */
13596           && !get_AT (old_die, DW_AT_inline))
13597         {
13598           /* Detect and ignore this case, where we are trying to output
13599              something we have already output.  */
13600           return;
13601         }
13602
13603       /* If the definition comes from the same place as the declaration,
13604          maybe use the old DIE.  We always want the DIE for this function
13605          that has the *_pc attributes to be under comp_unit_die so the
13606          debugger can find it.  We also need to do this for abstract
13607          instances of inlines, since the spec requires the out-of-line copy
13608          to have the same parent.  For local class methods, this doesn't
13609          apply; we just use the old DIE.  */
13610       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13611           && (DECL_ARTIFICIAL (decl)
13612               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13613                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13614                       == (unsigned) s.line))))
13615         {
13616           subr_die = old_die;
13617
13618           /* Clear out the declaration attribute and the formal parameters.
13619              Do not remove all children, because it is possible that this
13620              declaration die was forced using force_decl_die(). In such
13621              cases die that forced declaration die (e.g. TAG_imported_module)
13622              is one of the children that we do not want to remove.  */
13623           remove_AT (subr_die, DW_AT_declaration);
13624           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13625         }
13626       else
13627         {
13628           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13629           add_AT_specification (subr_die, old_die);
13630           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13631             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13632           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13633             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13634         }
13635     }
13636   else
13637     {
13638       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13639
13640       if (TREE_PUBLIC (decl))
13641         add_AT_flag (subr_die, DW_AT_external, 1);
13642
13643       add_name_and_src_coords_attributes (subr_die, decl);
13644       if (debug_info_level > DINFO_LEVEL_TERSE)
13645         {
13646           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13647           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13648                               0, 0, context_die);
13649         }
13650
13651       add_pure_or_virtual_attribute (subr_die, decl);
13652       if (DECL_ARTIFICIAL (decl))
13653         add_AT_flag (subr_die, DW_AT_artificial, 1);
13654
13655       if (TREE_PROTECTED (decl))
13656         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13657       else if (TREE_PRIVATE (decl))
13658         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13659     }
13660
13661   if (declaration)
13662     {
13663       if (!old_die || !get_AT (old_die, DW_AT_inline))
13664         {
13665           add_AT_flag (subr_die, DW_AT_declaration, 1);
13666
13667           /* The first time we see a member function, it is in the context of
13668              the class to which it belongs.  We make sure of this by emitting
13669              the class first.  The next time is the definition, which is
13670              handled above.  The two may come from the same source text.
13671
13672              Note that force_decl_die() forces function declaration die. It is
13673              later reused to represent definition.  */
13674           equate_decl_number_to_die (decl, subr_die);
13675         }
13676     }
13677   else if (DECL_ABSTRACT (decl))
13678     {
13679       if (DECL_DECLARED_INLINE_P (decl))
13680         {
13681           if (cgraph_function_possibly_inlined_p (decl))
13682             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13683           else
13684             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13685         }
13686       else
13687         {
13688           if (cgraph_function_possibly_inlined_p (decl))
13689             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13690           else
13691             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13692         }
13693
13694       if (DECL_DECLARED_INLINE_P (decl)
13695           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13696         add_AT_flag (subr_die, DW_AT_artificial, 1);
13697
13698       equate_decl_number_to_die (decl, subr_die);
13699     }
13700   else if (!DECL_EXTERNAL (decl))
13701     {
13702       HOST_WIDE_INT cfa_fb_offset;
13703
13704       if (!old_die || !get_AT (old_die, DW_AT_inline))
13705         equate_decl_number_to_die (decl, subr_die);
13706
13707       if (!flag_reorder_blocks_and_partition)
13708         {
13709           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13710                                        current_function_funcdef_no);
13711           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13712           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13713                                        current_function_funcdef_no);
13714           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13715
13716           add_pubname (decl, subr_die);
13717           add_arange (decl, subr_die);
13718         }
13719       else
13720         {  /* Do nothing for now; maybe need to duplicate die, one for
13721               hot section and one for cold section, then use the hot/cold
13722               section begin/end labels to generate the aranges...  */
13723           /*
13724             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13725             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13726             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13727             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13728
13729             add_pubname (decl, subr_die);
13730             add_arange (decl, subr_die);
13731             add_arange (decl, subr_die);
13732            */
13733         }
13734
13735 #ifdef MIPS_DEBUGGING_INFO
13736       /* Add a reference to the FDE for this routine.  */
13737       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13738 #endif
13739
13740       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13741
13742       /* We define the "frame base" as the function's CFA.  This is more
13743          convenient for several reasons: (1) It's stable across the prologue
13744          and epilogue, which makes it better than just a frame pointer,
13745          (2) With dwarf3, there exists a one-byte encoding that allows us
13746          to reference the .debug_frame data by proxy, but failing that,
13747          (3) We can at least reuse the code inspection and interpretation
13748          code that determines the CFA position at various points in the
13749          function.  */
13750       /* ??? Use some command-line or configury switch to enable the use
13751          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13752          consumers that understand it; fall back to "pure" dwarf2 and
13753          convert the CFA data into a location list.  */
13754       {
13755         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13756         if (list->dw_loc_next)
13757           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13758         else
13759           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13760       }
13761
13762       /* Compute a displacement from the "steady-state frame pointer" to
13763          the CFA.  The former is what all stack slots and argument slots
13764          will reference in the rtl; the later is what we've told the
13765          debugger about.  We'll need to adjust all frame_base references
13766          by this displacement.  */
13767       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13768
13769       if (cfun->static_chain_decl)
13770         add_AT_location_description (subr_die, DW_AT_static_link,
13771                  loc_descriptor_from_tree (cfun->static_chain_decl));
13772     }
13773
13774   /* Now output descriptions of the arguments for this function. This gets
13775      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13776      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13777      `...' at the end of the formal parameter list.  In order to find out if
13778      there was a trailing ellipsis or not, we must instead look at the type
13779      associated with the FUNCTION_DECL.  This will be a node of type
13780      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13781      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13782      an ellipsis at the end.  */
13783
13784   /* In the case where we are describing a mere function declaration, all we
13785      need to do here (and all we *can* do here) is to describe the *types* of
13786      its formal parameters.  */
13787   if (debug_info_level <= DINFO_LEVEL_TERSE)
13788     ;
13789   else if (declaration)
13790     gen_formal_types_die (decl, subr_die);
13791   else
13792     {
13793       /* Generate DIEs to represent all known formal parameters.  */
13794       tree arg_decls = DECL_ARGUMENTS (decl);
13795       tree parm;
13796
13797       /* When generating DIEs, generate the unspecified_parameters DIE
13798          instead if we come across the arg "__builtin_va_alist" */
13799       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13800         if (TREE_CODE (parm) == PARM_DECL)
13801           {
13802             if (DECL_NAME (parm)
13803                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13804                             "__builtin_va_alist"))
13805               gen_unspecified_parameters_die (parm, subr_die);
13806             else
13807               gen_decl_die (parm, subr_die);
13808           }
13809
13810       /* Decide whether we need an unspecified_parameters DIE at the end.
13811          There are 2 more cases to do this for: 1) the ansi ... declaration -
13812          this is detectable when the end of the arg list is not a
13813          void_type_node 2) an unprototyped function declaration (not a
13814          definition).  This just means that we have no info about the
13815          parameters at all.  */
13816       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13817       if (fn_arg_types != NULL)
13818         {
13819           /* This is the prototyped case, check for....  */
13820           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13821             gen_unspecified_parameters_die (decl, subr_die);
13822         }
13823       else if (DECL_INITIAL (decl) == NULL_TREE)
13824         gen_unspecified_parameters_die (decl, subr_die);
13825     }
13826
13827   /* Output Dwarf info for all of the stuff within the body of the function
13828      (if it has one - it may be just a declaration).  */
13829   outer_scope = DECL_INITIAL (decl);
13830
13831   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13832      a function.  This BLOCK actually represents the outermost binding contour
13833      for the function, i.e. the contour in which the function's formal
13834      parameters and labels get declared. Curiously, it appears that the front
13835      end doesn't actually put the PARM_DECL nodes for the current function onto
13836      the BLOCK_VARS list for this outer scope, but are strung off of the
13837      DECL_ARGUMENTS list for the function instead.
13838
13839      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13840      the LABEL_DECL nodes for the function however, and we output DWARF info
13841      for those in decls_for_scope.  Just within the `outer_scope' there will be
13842      a BLOCK node representing the function's outermost pair of curly braces,
13843      and any blocks used for the base and member initializers of a C++
13844      constructor function.  */
13845   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13846     {
13847       /* Emit a DW_TAG_variable DIE for a named return value.  */
13848       if (DECL_NAME (DECL_RESULT (decl)))
13849         gen_decl_die (DECL_RESULT (decl), subr_die);
13850
13851       current_function_has_inlines = 0;
13852       decls_for_scope (outer_scope, subr_die, 0);
13853
13854 #if 0 && defined (MIPS_DEBUGGING_INFO)
13855       if (current_function_has_inlines)
13856         {
13857           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13858           if (! comp_unit_has_inlines)
13859             {
13860               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13861               comp_unit_has_inlines = 1;
13862             }
13863         }
13864 #endif
13865     }
13866   /* Add the calling convention attribute if requested.  */
13867   add_calling_convention_attribute (subr_die, decl);
13868
13869 }
13870
13871 /* Returns a hash value for X (which really is a die_struct).  */
13872
13873 static hashval_t
13874 common_block_die_table_hash (const void *x)
13875 {
13876   const_dw_die_ref d = (const_dw_die_ref) x;
13877   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13878 }
13879
13880 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13881    as decl_id and die_parent of die_struct Y.  */
13882
13883 static int
13884 common_block_die_table_eq (const void *x, const void *y)
13885 {
13886   const_dw_die_ref d = (const_dw_die_ref) x;
13887   const_dw_die_ref e = (const_dw_die_ref) y;
13888   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13889 }
13890
13891 /* Generate a DIE to represent a declared data object.  */
13892
13893 static void
13894 gen_variable_die (tree decl, dw_die_ref context_die)
13895 {
13896   HOST_WIDE_INT off;
13897   tree com_decl;
13898   dw_die_ref var_die;
13899   tree origin = decl_ultimate_origin (decl);
13900   dw_die_ref old_die = lookup_decl_die (decl);
13901   int declaration = (DECL_EXTERNAL (decl)
13902                      /* If DECL is COMDAT and has not actually been
13903                         emitted, we cannot take its address; there
13904                         might end up being no definition anywhere in
13905                         the program.  For example, consider the C++
13906                         test case:
13907
13908                           template <class T>
13909                           struct S { static const int i = 7; };
13910
13911                           template <class T>
13912                           const int S<T>::i;
13913
13914                           int f() { return S<int>::i; }
13915
13916                         Here, S<int>::i is not DECL_EXTERNAL, but no
13917                         definition is required, so the compiler will
13918                         not emit a definition.  */
13919                      || (TREE_CODE (decl) == VAR_DECL
13920                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13921                      || class_or_namespace_scope_p (context_die));
13922
13923   com_decl = fortran_common (decl, &off);
13924
13925   /* Symbol in common gets emitted as a child of the common block, in the form
13926      of a data member.  */
13927   if (com_decl)
13928     {
13929       tree field;
13930       dw_die_ref com_die;
13931       dw_loc_descr_ref loc;
13932       die_node com_die_arg;
13933
13934       var_die = lookup_decl_die (decl);
13935       if (var_die)
13936         {
13937           if (get_AT (var_die, DW_AT_location) == NULL)
13938             {
13939               loc = loc_descriptor_from_tree (com_decl);
13940               if (loc)
13941                 {
13942                   if (off)
13943                     {
13944                       /* Optimize the common case.  */
13945                       if (loc->dw_loc_opc == DW_OP_addr
13946                           && loc->dw_loc_next == NULL
13947                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13948                              == SYMBOL_REF)
13949                         loc->dw_loc_oprnd1.v.val_addr
13950                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13951                         else
13952                           add_loc_descr (&loc,
13953                                          new_loc_descr (DW_OP_plus_uconst,
13954                                                         off, 0));
13955                     }
13956                   add_AT_loc (var_die, DW_AT_location, loc);
13957                   remove_AT (var_die, DW_AT_declaration);
13958                 }
13959             }
13960           return;
13961         }
13962
13963       if (common_block_die_table == NULL)
13964         common_block_die_table
13965           = htab_create_ggc (10, common_block_die_table_hash,
13966                              common_block_die_table_eq, NULL);
13967
13968       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13969       com_die_arg.decl_id = DECL_UID (com_decl);
13970       com_die_arg.die_parent = context_die;
13971       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13972       loc = loc_descriptor_from_tree (com_decl);
13973       if (com_die == NULL)
13974         {
13975           const char *cnam
13976             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13977           void **slot;
13978
13979           com_die = new_die (DW_TAG_common_block, context_die, decl);
13980           add_name_and_src_coords_attributes (com_die, com_decl);
13981           if (loc)
13982             {
13983               add_AT_loc (com_die, DW_AT_location, loc);
13984               /* Avoid sharing the same loc descriptor between
13985                  DW_TAG_common_block and DW_TAG_variable.  */
13986               loc = loc_descriptor_from_tree (com_decl);
13987             }
13988           else if (DECL_EXTERNAL (decl))
13989             add_AT_flag (com_die, DW_AT_declaration, 1);
13990           add_pubname_string (cnam, com_die); /* ??? needed? */
13991           com_die->decl_id = DECL_UID (com_decl);
13992           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13993           *slot = (void *) com_die;
13994         }
13995       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13996         {
13997           add_AT_loc (com_die, DW_AT_location, loc);
13998           loc = loc_descriptor_from_tree (com_decl);
13999           remove_AT (com_die, DW_AT_declaration);
14000         }
14001       var_die = new_die (DW_TAG_variable, com_die, decl);
14002       add_name_and_src_coords_attributes (var_die, decl);
14003       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14004                           TREE_THIS_VOLATILE (decl), context_die);
14005       add_AT_flag (var_die, DW_AT_external, 1);
14006       if (loc)
14007         {
14008           if (off)
14009             {
14010               /* Optimize the common case.  */
14011               if (loc->dw_loc_opc == DW_OP_addr
14012                   && loc->dw_loc_next == NULL
14013                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14014                 loc->dw_loc_oprnd1.v.val_addr
14015                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14016               else
14017                 add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
14018                                                     off, 0));
14019             }
14020           add_AT_loc (var_die, DW_AT_location, loc);
14021         }
14022       else if (DECL_EXTERNAL (decl))
14023         add_AT_flag (var_die, DW_AT_declaration, 1);
14024       equate_decl_number_to_die (decl, var_die);
14025       return;
14026     }
14027
14028   var_die = new_die (DW_TAG_variable, context_die, decl);
14029
14030   if (origin != NULL)
14031     add_abstract_origin_attribute (var_die, origin);
14032
14033   /* Loop unrolling can create multiple blocks that refer to the same
14034      static variable, so we must test for the DW_AT_declaration flag.
14035
14036      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14037      copy decls and set the DECL_ABSTRACT flag on them instead of
14038      sharing them.
14039
14040      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14041
14042      ??? The declare_in_namespace support causes us to get two DIEs for one
14043      variable, both of which are declarations.  We want to avoid considering
14044      one to be a specification, so we must test that this DIE is not a
14045      declaration.  */
14046   else if (old_die && TREE_STATIC (decl) && ! declaration
14047            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14048     {
14049       /* This is a definition of a C++ class level static.  */
14050       add_AT_specification (var_die, old_die);
14051       if (DECL_NAME (decl))
14052         {
14053           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14054           struct dwarf_file_data * file_index = lookup_filename (s.file);
14055
14056           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14057             add_AT_file (var_die, DW_AT_decl_file, file_index);
14058
14059           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14060             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14061         }
14062     }
14063   else
14064     {
14065       tree type = TREE_TYPE (decl);
14066
14067       add_name_and_src_coords_attributes (var_die, decl);
14068       if ((TREE_CODE (decl) == PARM_DECL
14069            || TREE_CODE (decl) == RESULT_DECL)
14070           && DECL_BY_REFERENCE (decl))
14071         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14072       else
14073         add_type_attribute (var_die, type, TREE_READONLY (decl),
14074                             TREE_THIS_VOLATILE (decl), context_die);
14075
14076       if (TREE_PUBLIC (decl))
14077         add_AT_flag (var_die, DW_AT_external, 1);
14078
14079       if (DECL_ARTIFICIAL (decl))
14080         add_AT_flag (var_die, DW_AT_artificial, 1);
14081
14082       if (TREE_PROTECTED (decl))
14083         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14084       else if (TREE_PRIVATE (decl))
14085         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14086     }
14087
14088   if (declaration)
14089     add_AT_flag (var_die, DW_AT_declaration, 1);
14090
14091   if (DECL_ABSTRACT (decl) || declaration)
14092     equate_decl_number_to_die (decl, var_die);
14093
14094   if (! declaration && ! DECL_ABSTRACT (decl))
14095     {
14096       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
14097       add_pubname (decl, var_die);
14098     }
14099   else
14100     tree_add_const_value_attribute (var_die, decl);
14101 }
14102
14103 /* Generate a DIE to represent a named constant.  */
14104
14105 static void
14106 gen_const_die (tree decl, dw_die_ref context_die)
14107 {
14108   dw_die_ref const_die;
14109   tree type = TREE_TYPE (decl);
14110
14111   const_die = new_die (DW_TAG_constant, context_die, decl);
14112   add_name_and_src_coords_attributes (const_die, decl);
14113   add_type_attribute (const_die, type, 1, 0, context_die);
14114   if (TREE_PUBLIC (decl))
14115     add_AT_flag (const_die, DW_AT_external, 1);
14116   if (DECL_ARTIFICIAL (decl))
14117     add_AT_flag (const_die, DW_AT_artificial, 1);
14118   tree_add_const_value_attribute (const_die, decl);
14119 }
14120
14121 /* Generate a DIE to represent a label identifier.  */
14122
14123 static void
14124 gen_label_die (tree decl, dw_die_ref context_die)
14125 {
14126   tree origin = decl_ultimate_origin (decl);
14127   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14128   rtx insn;
14129   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14130
14131   if (origin != NULL)
14132     add_abstract_origin_attribute (lbl_die, origin);
14133   else
14134     add_name_and_src_coords_attributes (lbl_die, decl);
14135
14136   if (DECL_ABSTRACT (decl))
14137     equate_decl_number_to_die (decl, lbl_die);
14138   else
14139     {
14140       insn = DECL_RTL_IF_SET (decl);
14141
14142       /* Deleted labels are programmer specified labels which have been
14143          eliminated because of various optimizations.  We still emit them
14144          here so that it is possible to put breakpoints on them.  */
14145       if (insn
14146           && (LABEL_P (insn)
14147               || ((NOTE_P (insn)
14148                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14149         {
14150           /* When optimization is enabled (via -O) some parts of the compiler
14151              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14152              represent source-level labels which were explicitly declared by
14153              the user.  This really shouldn't be happening though, so catch
14154              it if it ever does happen.  */
14155           gcc_assert (!INSN_DELETED_P (insn));
14156
14157           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14158           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14159         }
14160     }
14161 }
14162
14163 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14164    attributes to the DIE for a block STMT, to describe where the inlined
14165    function was called from.  This is similar to add_src_coords_attributes.  */
14166
14167 static inline void
14168 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14169 {
14170   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14171
14172   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14173   add_AT_unsigned (die, DW_AT_call_line, s.line);
14174 }
14175
14176
14177 /* If STMT's abstract origin is a function declaration and STMT's
14178    first subblock's abstract origin is the function's outermost block,
14179    then we're looking at the main entry point.  */
14180 static bool
14181 is_inlined_entry_point (const_tree stmt)
14182 {
14183   tree decl, block;
14184
14185   if (!stmt || TREE_CODE (stmt) != BLOCK)
14186     return false;
14187
14188   decl = block_ultimate_origin (stmt);
14189
14190   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
14191     return false;
14192
14193   block = BLOCK_SUBBLOCKS (stmt);
14194
14195   if (block)
14196     {
14197       if (TREE_CODE (block) != BLOCK)
14198         return false;
14199
14200       block = block_ultimate_origin (block);
14201     }
14202
14203   return block == DECL_INITIAL (decl);
14204 }
14205
14206 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14207    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14208
14209 static inline void
14210 add_high_low_attributes (tree stmt, dw_die_ref die)
14211 {
14212   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14213
14214   if (BLOCK_FRAGMENT_CHAIN (stmt))
14215     {
14216       tree chain;
14217
14218       if (is_inlined_entry_point (stmt))
14219         {
14220           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14221                                        BLOCK_NUMBER (stmt));
14222           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14223         }
14224
14225       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14226
14227       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14228       do
14229         {
14230           add_ranges (chain);
14231           chain = BLOCK_FRAGMENT_CHAIN (chain);
14232         }
14233       while (chain);
14234       add_ranges (NULL);
14235     }
14236   else
14237     {
14238       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14239                                    BLOCK_NUMBER (stmt));
14240       add_AT_lbl_id (die, DW_AT_low_pc, label);
14241       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14242                                    BLOCK_NUMBER (stmt));
14243       add_AT_lbl_id (die, DW_AT_high_pc, label);
14244     }
14245 }
14246
14247 /* Generate a DIE for a lexical block.  */
14248
14249 static void
14250 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14251 {
14252   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14253
14254   if (! BLOCK_ABSTRACT (stmt))
14255     add_high_low_attributes (stmt, stmt_die);
14256
14257   decls_for_scope (stmt, stmt_die, depth);
14258 }
14259
14260 /* Generate a DIE for an inlined subprogram.  */
14261
14262 static void
14263 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14264 {
14265   tree decl = block_ultimate_origin (stmt);
14266
14267   /* Emit info for the abstract instance first, if we haven't yet.  We
14268      must emit this even if the block is abstract, otherwise when we
14269      emit the block below (or elsewhere), we may end up trying to emit
14270      a die whose origin die hasn't been emitted, and crashing.  */
14271   dwarf2out_abstract_function (decl);
14272
14273   if (! BLOCK_ABSTRACT (stmt))
14274     {
14275       dw_die_ref subr_die
14276         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14277
14278       add_abstract_origin_attribute (subr_die, decl);
14279       add_high_low_attributes (stmt, subr_die);
14280       add_call_src_coords_attributes (stmt, subr_die);
14281
14282       decls_for_scope (stmt, subr_die, depth);
14283       current_function_has_inlines = 1;
14284     }
14285   else
14286     /* We may get here if we're the outer block of function A that was
14287        inlined into function B that was inlined into function C.  When
14288        generating debugging info for C, dwarf2out_abstract_function(B)
14289        would mark all inlined blocks as abstract, including this one.
14290        So, we wouldn't (and shouldn't) expect labels to be generated
14291        for this one.  Instead, just emit debugging info for
14292        declarations within the block.  This is particularly important
14293        in the case of initializers of arguments passed from B to us:
14294        if they're statement expressions containing declarations, we
14295        wouldn't generate dies for their abstract variables, and then,
14296        when generating dies for the real variables, we'd die (pun
14297        intended :-)  */
14298     gen_lexical_block_die (stmt, context_die, depth);
14299 }
14300
14301 /* Generate a DIE for a field in a record, or structure.  */
14302
14303 static void
14304 gen_field_die (tree decl, dw_die_ref context_die)
14305 {
14306   dw_die_ref decl_die;
14307
14308   if (TREE_TYPE (decl) == error_mark_node)
14309     return;
14310
14311   decl_die = new_die (DW_TAG_member, context_die, decl);
14312   add_name_and_src_coords_attributes (decl_die, decl);
14313   add_type_attribute (decl_die, member_declared_type (decl),
14314                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14315                       context_die);
14316
14317   if (DECL_BIT_FIELD_TYPE (decl))
14318     {
14319       add_byte_size_attribute (decl_die, decl);
14320       add_bit_size_attribute (decl_die, decl);
14321       add_bit_offset_attribute (decl_die, decl);
14322     }
14323
14324   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14325     add_data_member_location_attribute (decl_die, decl);
14326
14327   if (DECL_ARTIFICIAL (decl))
14328     add_AT_flag (decl_die, DW_AT_artificial, 1);
14329
14330   if (TREE_PROTECTED (decl))
14331     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14332   else if (TREE_PRIVATE (decl))
14333     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14334
14335   /* Equate decl number to die, so that we can look up this decl later on.  */
14336   equate_decl_number_to_die (decl, decl_die);
14337 }
14338
14339 #if 0
14340 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14341    Use modified_type_die instead.
14342    We keep this code here just in case these types of DIEs may be needed to
14343    represent certain things in other languages (e.g. Pascal) someday.  */
14344
14345 static void
14346 gen_pointer_type_die (tree type, dw_die_ref context_die)
14347 {
14348   dw_die_ref ptr_die
14349     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14350
14351   equate_type_number_to_die (type, ptr_die);
14352   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14353   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14354 }
14355
14356 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14357    Use modified_type_die instead.
14358    We keep this code here just in case these types of DIEs may be needed to
14359    represent certain things in other languages (e.g. Pascal) someday.  */
14360
14361 static void
14362 gen_reference_type_die (tree type, dw_die_ref context_die)
14363 {
14364   dw_die_ref ref_die
14365     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14366
14367   equate_type_number_to_die (type, ref_die);
14368   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14369   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14370 }
14371 #endif
14372
14373 /* Generate a DIE for a pointer to a member type.  */
14374
14375 static void
14376 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14377 {
14378   dw_die_ref ptr_die
14379     = new_die (DW_TAG_ptr_to_member_type,
14380                scope_die_for (type, context_die), type);
14381
14382   equate_type_number_to_die (type, ptr_die);
14383   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14384                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14385   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14386 }
14387
14388 /* Generate the DIE for the compilation unit.  */
14389
14390 static dw_die_ref
14391 gen_compile_unit_die (const char *filename)
14392 {
14393   dw_die_ref die;
14394   char producer[250];
14395   const char *language_string = lang_hooks.name;
14396   int language;
14397
14398   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14399
14400   if (filename)
14401     {
14402       add_name_attribute (die, filename);
14403       /* Don't add cwd for <built-in>.  */
14404       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14405         add_comp_dir_attribute (die);
14406     }
14407
14408   sprintf (producer, "%s %s", language_string, version_string);
14409
14410 #ifdef MIPS_DEBUGGING_INFO
14411   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14412      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14413      not appear in the producer string, the debugger reaches the conclusion
14414      that the object file is stripped and has no debugging information.
14415      To get the MIPS/SGI debugger to believe that there is debugging
14416      information in the object file, we add a -g to the producer string.  */
14417   if (debug_info_level > DINFO_LEVEL_TERSE)
14418     strcat (producer, " -g");
14419 #endif
14420
14421   add_AT_string (die, DW_AT_producer, producer);
14422
14423   if (strcmp (language_string, "GNU C++") == 0)
14424     language = DW_LANG_C_plus_plus;
14425   else if (strcmp (language_string, "GNU Ada") == 0)
14426     language = DW_LANG_Ada95;
14427   else if (strcmp (language_string, "GNU F77") == 0)
14428     language = DW_LANG_Fortran77;
14429   else if (strcmp (language_string, "GNU Fortran") == 0)
14430     language = DW_LANG_Fortran95;
14431   else if (strcmp (language_string, "GNU Pascal") == 0)
14432     language = DW_LANG_Pascal83;
14433   else if (strcmp (language_string, "GNU Java") == 0)
14434     language = DW_LANG_Java;
14435   else if (strcmp (language_string, "GNU Objective-C") == 0)
14436     language = DW_LANG_ObjC;
14437   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14438     language = DW_LANG_ObjC_plus_plus;
14439   else
14440     language = DW_LANG_C89;
14441
14442   add_AT_unsigned (die, DW_AT_language, language);
14443   return die;
14444 }
14445
14446 /* Generate the DIE for a base class.  */
14447
14448 static void
14449 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14450 {
14451   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14452
14453   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14454   add_data_member_location_attribute (die, binfo);
14455
14456   if (BINFO_VIRTUAL_P (binfo))
14457     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14458
14459   if (access == access_public_node)
14460     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14461   else if (access == access_protected_node)
14462     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14463 }
14464
14465 /* Generate a DIE for a class member.  */
14466
14467 static void
14468 gen_member_die (tree type, dw_die_ref context_die)
14469 {
14470   tree member;
14471   tree binfo = TYPE_BINFO (type);
14472   dw_die_ref child;
14473
14474   /* If this is not an incomplete type, output descriptions of each of its
14475      members. Note that as we output the DIEs necessary to represent the
14476      members of this record or union type, we will also be trying to output
14477      DIEs to represent the *types* of those members. However the `type'
14478      function (above) will specifically avoid generating type DIEs for member
14479      types *within* the list of member DIEs for this (containing) type except
14480      for those types (of members) which are explicitly marked as also being
14481      members of this (containing) type themselves.  The g++ front- end can
14482      force any given type to be treated as a member of some other (containing)
14483      type by setting the TYPE_CONTEXT of the given (member) type to point to
14484      the TREE node representing the appropriate (containing) type.  */
14485
14486   /* First output info about the base classes.  */
14487   if (binfo)
14488     {
14489       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14490       int i;
14491       tree base;
14492
14493       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14494         gen_inheritance_die (base,
14495                              (accesses ? VEC_index (tree, accesses, i)
14496                               : access_public_node), context_die);
14497     }
14498
14499   /* Now output info about the data members and type members.  */
14500   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14501     {
14502       /* If we thought we were generating minimal debug info for TYPE
14503          and then changed our minds, some of the member declarations
14504          may have already been defined.  Don't define them again, but
14505          do put them in the right order.  */
14506
14507       child = lookup_decl_die (member);
14508       if (child)
14509         splice_child_die (context_die, child);
14510       else
14511         gen_decl_die (member, context_die);
14512     }
14513
14514   /* Now output info about the function members (if any).  */
14515   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14516     {
14517       /* Don't include clones in the member list.  */
14518       if (DECL_ABSTRACT_ORIGIN (member))
14519         continue;
14520
14521       child = lookup_decl_die (member);
14522       if (child)
14523         splice_child_die (context_die, child);
14524       else
14525         gen_decl_die (member, context_die);
14526     }
14527 }
14528
14529 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14530    is set, we pretend that the type was never defined, so we only get the
14531    member DIEs needed by later specification DIEs.  */
14532
14533 static void
14534 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14535                                 enum debug_info_usage usage)
14536 {
14537   dw_die_ref type_die = lookup_type_die (type);
14538   dw_die_ref scope_die = 0;
14539   int nested = 0;
14540   int complete = (TYPE_SIZE (type)
14541                   && (! TYPE_STUB_DECL (type)
14542                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14543   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14544   complete = complete && should_emit_struct_debug (type, usage);
14545
14546   if (type_die && ! complete)
14547     return;
14548
14549   if (TYPE_CONTEXT (type) != NULL_TREE
14550       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14551           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14552     nested = 1;
14553
14554   scope_die = scope_die_for (type, context_die);
14555
14556   if (! type_die || (nested && scope_die == comp_unit_die))
14557     /* First occurrence of type or toplevel definition of nested class.  */
14558     {
14559       dw_die_ref old_die = type_die;
14560
14561       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14562                           ? record_type_tag (type) : DW_TAG_union_type,
14563                           scope_die, type);
14564       equate_type_number_to_die (type, type_die);
14565       if (old_die)
14566         add_AT_specification (type_die, old_die);
14567       else
14568         add_name_attribute (type_die, type_tag (type));
14569     }
14570   else
14571     remove_AT (type_die, DW_AT_declaration);
14572
14573   /* If this type has been completed, then give it a byte_size attribute and
14574      then give a list of members.  */
14575   if (complete && !ns_decl)
14576     {
14577       /* Prevent infinite recursion in cases where the type of some member of
14578          this type is expressed in terms of this type itself.  */
14579       TREE_ASM_WRITTEN (type) = 1;
14580       add_byte_size_attribute (type_die, type);
14581       if (TYPE_STUB_DECL (type) != NULL_TREE)
14582         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14583
14584       /* If the first reference to this type was as the return type of an
14585          inline function, then it may not have a parent.  Fix this now.  */
14586       if (type_die->die_parent == NULL)
14587         add_child_die (scope_die, type_die);
14588
14589       push_decl_scope (type);
14590       gen_member_die (type, type_die);
14591       pop_decl_scope ();
14592
14593       /* GNU extension: Record what type our vtable lives in.  */
14594       if (TYPE_VFIELD (type))
14595         {
14596           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14597
14598           gen_type_die (vtype, context_die);
14599           add_AT_die_ref (type_die, DW_AT_containing_type,
14600                           lookup_type_die (vtype));
14601         }
14602     }
14603   else
14604     {
14605       add_AT_flag (type_die, DW_AT_declaration, 1);
14606
14607       /* We don't need to do this for function-local types.  */
14608       if (TYPE_STUB_DECL (type)
14609           && ! decl_function_context (TYPE_STUB_DECL (type)))
14610         VEC_safe_push (tree, gc, incomplete_types, type);
14611     }
14612
14613   if (get_AT (type_die, DW_AT_name))
14614     add_pubtype (type, type_die);
14615 }
14616
14617 /* Generate a DIE for a subroutine _type_.  */
14618
14619 static void
14620 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14621 {
14622   tree return_type = TREE_TYPE (type);
14623   dw_die_ref subr_die
14624     = new_die (DW_TAG_subroutine_type,
14625                scope_die_for (type, context_die), type);
14626
14627   equate_type_number_to_die (type, subr_die);
14628   add_prototyped_attribute (subr_die, type);
14629   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14630   gen_formal_types_die (type, subr_die);
14631
14632   if (get_AT (subr_die, DW_AT_name))
14633     add_pubtype (type, subr_die);
14634 }
14635
14636 /* Generate a DIE for a type definition.  */
14637
14638 static void
14639 gen_typedef_die (tree decl, dw_die_ref context_die)
14640 {
14641   dw_die_ref type_die;
14642   tree origin;
14643
14644   if (TREE_ASM_WRITTEN (decl))
14645     return;
14646
14647   TREE_ASM_WRITTEN (decl) = 1;
14648   type_die = new_die (DW_TAG_typedef, context_die, decl);
14649   origin = decl_ultimate_origin (decl);
14650   if (origin != NULL)
14651     add_abstract_origin_attribute (type_die, origin);
14652   else
14653     {
14654       tree type;
14655
14656       add_name_and_src_coords_attributes (type_die, decl);
14657       if (DECL_ORIGINAL_TYPE (decl))
14658         {
14659           type = DECL_ORIGINAL_TYPE (decl);
14660
14661           gcc_assert (type != TREE_TYPE (decl));
14662           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14663         }
14664       else
14665         type = TREE_TYPE (decl);
14666
14667       add_type_attribute (type_die, type, TREE_READONLY (decl),
14668                           TREE_THIS_VOLATILE (decl), context_die);
14669     }
14670
14671   if (DECL_ABSTRACT (decl))
14672     equate_decl_number_to_die (decl, type_die);
14673
14674   if (get_AT (type_die, DW_AT_name))
14675     add_pubtype (decl, type_die);
14676 }
14677
14678 /* Generate a type description DIE.  */
14679
14680 static void
14681 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14682                                 enum debug_info_usage usage)
14683 {
14684   int need_pop;
14685   struct array_descr_info info;
14686
14687   if (type == NULL_TREE || type == error_mark_node)
14688     return;
14689
14690   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14691       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14692     {
14693       if (TREE_ASM_WRITTEN (type))
14694         return;
14695
14696       /* Prevent broken recursion; we can't hand off to the same type.  */
14697       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14698
14699       TREE_ASM_WRITTEN (type) = 1;
14700       gen_decl_die (TYPE_NAME (type), context_die);
14701       return;
14702     }
14703
14704   /* If this is an array type with hidden descriptor, handle it first.  */
14705   if (!TREE_ASM_WRITTEN (type)
14706       && lang_hooks.types.get_array_descr_info
14707       && lang_hooks.types.get_array_descr_info (type, &info))
14708     {
14709       gen_descr_array_type_die (type, &info, context_die);
14710       TREE_ASM_WRITTEN (type) = 1;
14711       return;
14712     }
14713
14714   /* We are going to output a DIE to represent the unqualified version
14715      of this type (i.e. without any const or volatile qualifiers) so
14716      get the main variant (i.e. the unqualified version) of this type
14717      now.  (Vectors are special because the debugging info is in the
14718      cloned type itself).  */
14719   if (TREE_CODE (type) != VECTOR_TYPE)
14720     type = type_main_variant (type);
14721
14722   if (TREE_ASM_WRITTEN (type))
14723     return;
14724
14725   switch (TREE_CODE (type))
14726     {
14727     case ERROR_MARK:
14728       break;
14729
14730     case POINTER_TYPE:
14731     case REFERENCE_TYPE:
14732       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14733          ensures that the gen_type_die recursion will terminate even if the
14734          type is recursive.  Recursive types are possible in Ada.  */
14735       /* ??? We could perhaps do this for all types before the switch
14736          statement.  */
14737       TREE_ASM_WRITTEN (type) = 1;
14738
14739       /* For these types, all that is required is that we output a DIE (or a
14740          set of DIEs) to represent the "basis" type.  */
14741       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14742                                 DINFO_USAGE_IND_USE);
14743       break;
14744
14745     case OFFSET_TYPE:
14746       /* This code is used for C++ pointer-to-data-member types.
14747          Output a description of the relevant class type.  */
14748       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14749                                         DINFO_USAGE_IND_USE);
14750
14751       /* Output a description of the type of the object pointed to.  */
14752       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14753                                         DINFO_USAGE_IND_USE);
14754
14755       /* Now output a DIE to represent this pointer-to-data-member type
14756          itself.  */
14757       gen_ptr_to_mbr_type_die (type, context_die);
14758       break;
14759
14760     case FUNCTION_TYPE:
14761       /* Force out return type (in case it wasn't forced out already).  */
14762       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14763                                         DINFO_USAGE_DIR_USE);
14764       gen_subroutine_type_die (type, context_die);
14765       break;
14766
14767     case METHOD_TYPE:
14768       /* Force out return type (in case it wasn't forced out already).  */
14769       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14770                                         DINFO_USAGE_DIR_USE);
14771       gen_subroutine_type_die (type, context_die);
14772       break;
14773
14774     case ARRAY_TYPE:
14775       gen_array_type_die (type, context_die);
14776       break;
14777
14778     case VECTOR_TYPE:
14779       gen_array_type_die (type, context_die);
14780       break;
14781
14782     case ENUMERAL_TYPE:
14783     case RECORD_TYPE:
14784     case UNION_TYPE:
14785     case QUAL_UNION_TYPE:
14786       /* If this is a nested type whose containing class hasn't been written
14787          out yet, writing it out will cover this one, too.  This does not apply
14788          to instantiations of member class templates; they need to be added to
14789          the containing class as they are generated.  FIXME: This hurts the
14790          idea of combining type decls from multiple TUs, since we can't predict
14791          what set of template instantiations we'll get.  */
14792       if (TYPE_CONTEXT (type)
14793           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14794           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14795         {
14796           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14797
14798           if (TREE_ASM_WRITTEN (type))
14799             return;
14800
14801           /* If that failed, attach ourselves to the stub.  */
14802           push_decl_scope (TYPE_CONTEXT (type));
14803           context_die = lookup_type_die (TYPE_CONTEXT (type));
14804           need_pop = 1;
14805         }
14806       else
14807         {
14808           context_die = declare_in_namespace (type, context_die);
14809           need_pop = 0;
14810         }
14811
14812       if (TREE_CODE (type) == ENUMERAL_TYPE)
14813         {
14814           /* This might have been written out by the call to
14815              declare_in_namespace.  */
14816           if (!TREE_ASM_WRITTEN (type))
14817             gen_enumeration_type_die (type, context_die);
14818         }
14819       else
14820         gen_struct_or_union_type_die (type, context_die, usage);
14821
14822       if (need_pop)
14823         pop_decl_scope ();
14824
14825       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14826          it up if it is ever completed.  gen_*_type_die will set it for us
14827          when appropriate.  */
14828       return;
14829
14830     case VOID_TYPE:
14831     case INTEGER_TYPE:
14832     case REAL_TYPE:
14833     case FIXED_POINT_TYPE:
14834     case COMPLEX_TYPE:
14835     case BOOLEAN_TYPE:
14836       /* No DIEs needed for fundamental types.  */
14837       break;
14838
14839     case LANG_TYPE:
14840       /* No Dwarf representation currently defined.  */
14841       break;
14842
14843     default:
14844       gcc_unreachable ();
14845     }
14846
14847   TREE_ASM_WRITTEN (type) = 1;
14848 }
14849
14850 static void
14851 gen_type_die (tree type, dw_die_ref context_die)
14852 {
14853   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14854 }
14855
14856 /* Generate a DIE for a tagged type instantiation.  */
14857
14858 static void
14859 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14860 {
14861   if (type == NULL_TREE || type == error_mark_node)
14862     return;
14863
14864   /* We are going to output a DIE to represent the unqualified version of
14865      this type (i.e. without any const or volatile qualifiers) so make sure
14866      that we have the main variant (i.e. the unqualified version) of this
14867      type now.  */
14868   gcc_assert (type == type_main_variant (type));
14869
14870   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14871      an instance of an unresolved type.  */
14872
14873   switch (TREE_CODE (type))
14874     {
14875     case ERROR_MARK:
14876       break;
14877
14878     case ENUMERAL_TYPE:
14879       gen_inlined_enumeration_type_die (type, context_die);
14880       break;
14881
14882     case RECORD_TYPE:
14883       gen_inlined_structure_type_die (type, context_die);
14884       break;
14885
14886     case UNION_TYPE:
14887     case QUAL_UNION_TYPE:
14888       gen_inlined_union_type_die (type, context_die);
14889       break;
14890
14891     default:
14892       gcc_unreachable ();
14893     }
14894 }
14895
14896 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14897    things which are local to the given block.  */
14898
14899 static void
14900 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14901 {
14902   int must_output_die = 0;
14903   tree origin;
14904   tree decl;
14905   enum tree_code origin_code;
14906
14907   /* Ignore blocks that are NULL.  */
14908   if (stmt == NULL_TREE)
14909     return;
14910
14911   /* If the block is one fragment of a non-contiguous block, do not
14912      process the variables, since they will have been done by the
14913      origin block.  Do process subblocks.  */
14914   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14915     {
14916       tree sub;
14917
14918       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14919         gen_block_die (sub, context_die, depth + 1);
14920
14921       return;
14922     }
14923
14924   /* Determine the "ultimate origin" of this block.  This block may be an
14925      inlined instance of an inlined instance of inline function, so we have
14926      to trace all of the way back through the origin chain to find out what
14927      sort of node actually served as the original seed for the creation of
14928      the current block.  */
14929   origin = block_ultimate_origin (stmt);
14930   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14931
14932   /* Determine if we need to output any Dwarf DIEs at all to represent this
14933      block.  */
14934   if (origin_code == FUNCTION_DECL)
14935     /* The outer scopes for inlinings *must* always be represented.  We
14936        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14937     must_output_die = 1;
14938   else
14939     {
14940       /* In the case where the current block represents an inlining of the
14941          "body block" of an inline function, we must *NOT* output any DIE for
14942          this block because we have already output a DIE to represent the whole
14943          inlined function scope and the "body block" of any function doesn't
14944          really represent a different scope according to ANSI C rules.  So we
14945          check here to make sure that this block does not represent a "body
14946          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14947       if (! is_body_block (origin ? origin : stmt))
14948         {
14949           /* Determine if this block directly contains any "significant"
14950              local declarations which we will need to output DIEs for.  */
14951           if (debug_info_level > DINFO_LEVEL_TERSE)
14952             /* We are not in terse mode so *any* local declaration counts
14953                as being a "significant" one.  */
14954             must_output_die = (BLOCK_VARS (stmt) != NULL
14955                                && (TREE_USED (stmt)
14956                                    || TREE_ASM_WRITTEN (stmt)
14957                                    || BLOCK_ABSTRACT (stmt)));
14958           else
14959             /* We are in terse mode, so only local (nested) function
14960                definitions count as "significant" local declarations.  */
14961             for (decl = BLOCK_VARS (stmt);
14962                  decl != NULL; decl = TREE_CHAIN (decl))
14963               if (TREE_CODE (decl) == FUNCTION_DECL
14964                   && DECL_INITIAL (decl))
14965                 {
14966                   must_output_die = 1;
14967                   break;
14968                 }
14969         }
14970     }
14971
14972   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14973      DIE for any block which contains no significant local declarations at
14974      all.  Rather, in such cases we just call `decls_for_scope' so that any
14975      needed Dwarf info for any sub-blocks will get properly generated. Note
14976      that in terse mode, our definition of what constitutes a "significant"
14977      local declaration gets restricted to include only inlined function
14978      instances and local (nested) function definitions.  */
14979   if (must_output_die)
14980     {
14981       if (origin_code == FUNCTION_DECL)
14982         gen_inlined_subroutine_die (stmt, context_die, depth);
14983       else
14984         gen_lexical_block_die (stmt, context_die, depth);
14985     }
14986   else
14987     decls_for_scope (stmt, context_die, depth);
14988 }
14989
14990 /* Generate all of the decls declared within a given scope and (recursively)
14991    all of its sub-blocks.  */
14992
14993 static void
14994 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14995 {
14996   tree decl;
14997   tree subblocks;
14998
14999   /* Ignore NULL blocks.  */
15000   if (stmt == NULL_TREE)
15001     return;
15002
15003   if (TREE_USED (stmt))
15004     {
15005       /* Output the DIEs to represent all of the data objects and typedefs
15006          declared directly within this block but not within any nested
15007          sub-blocks.  Also, nested function and tag DIEs have been
15008          generated with a parent of NULL; fix that up now.  */
15009       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15010         {
15011           dw_die_ref die;
15012
15013           if (TREE_CODE (decl) == FUNCTION_DECL)
15014             die = lookup_decl_die (decl);
15015           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
15016             die = lookup_type_die (TREE_TYPE (decl));
15017           else
15018             die = NULL;
15019
15020           if (die != NULL && die->die_parent == NULL)
15021             add_child_die (context_die, die);
15022           /* Do not produce debug information for static variables since
15023              these might be optimized out.  We are called for these later
15024              in varpool_analyze_pending_decls.
15025
15026              But *do* produce it for Fortran COMMON variables because,
15027              even though they are static, their names can differ depending
15028              on the scope, which we need to preserve.  */
15029           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
15030               && !(is_fortran () && TREE_PUBLIC (decl)))
15031             ;
15032           else if (TREE_CODE (decl) == IMPORTED_DECL)
15033             dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
15034                                                  stmt, context_die);
15035           else
15036             gen_decl_die (decl, context_die);
15037         }
15038     }
15039
15040   /* If we're at -g1, we're not interested in subblocks.  */
15041   if (debug_info_level <= DINFO_LEVEL_TERSE)
15042     return;
15043
15044   /* Output the DIEs to represent all sub-blocks (and the items declared
15045      therein) of this block.  */
15046   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15047        subblocks != NULL;
15048        subblocks = BLOCK_CHAIN (subblocks))
15049     gen_block_die (subblocks, context_die, depth + 1);
15050 }
15051
15052 /* Is this a typedef we can avoid emitting?  */
15053
15054 static inline int
15055 is_redundant_typedef (const_tree decl)
15056 {
15057   if (TYPE_DECL_IS_STUB (decl))
15058     return 1;
15059
15060   if (DECL_ARTIFICIAL (decl)
15061       && DECL_CONTEXT (decl)
15062       && is_tagged_type (DECL_CONTEXT (decl))
15063       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15064       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15065     /* Also ignore the artificial member typedef for the class name.  */
15066     return 1;
15067
15068   return 0;
15069 }
15070
15071 /* Returns the DIE for a context.  */
15072
15073 static inline dw_die_ref
15074 get_context_die (tree context)
15075 {
15076   if (context)
15077     {
15078       /* Find die that represents this context.  */
15079       if (TYPE_P (context))
15080         return force_type_die (context);
15081       else
15082         return force_decl_die (context);
15083     }
15084   return comp_unit_die;
15085 }
15086
15087 /* Returns the DIE for decl.  A DIE will always be returned.  */
15088
15089 static dw_die_ref
15090 force_decl_die (tree decl)
15091 {
15092   dw_die_ref decl_die;
15093   unsigned saved_external_flag;
15094   tree save_fn = NULL_TREE;
15095   decl_die = lookup_decl_die (decl);
15096   if (!decl_die)
15097     {
15098       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15099
15100       decl_die = lookup_decl_die (decl);
15101       if (decl_die)
15102         return decl_die;
15103
15104       switch (TREE_CODE (decl))
15105         {
15106         case FUNCTION_DECL:
15107           /* Clear current_function_decl, so that gen_subprogram_die thinks
15108              that this is a declaration. At this point, we just want to force
15109              declaration die.  */
15110           save_fn = current_function_decl;
15111           current_function_decl = NULL_TREE;
15112           gen_subprogram_die (decl, context_die);
15113           current_function_decl = save_fn;
15114           break;
15115
15116         case VAR_DECL:
15117           /* Set external flag to force declaration die. Restore it after
15118            gen_decl_die() call.  */
15119           saved_external_flag = DECL_EXTERNAL (decl);
15120           DECL_EXTERNAL (decl) = 1;
15121           gen_decl_die (decl, context_die);
15122           DECL_EXTERNAL (decl) = saved_external_flag;
15123           break;
15124
15125         case NAMESPACE_DECL:
15126           dwarf2out_decl (decl);
15127           break;
15128
15129         default:
15130           gcc_unreachable ();
15131         }
15132
15133       /* We should be able to find the DIE now.  */
15134       if (!decl_die)
15135         decl_die = lookup_decl_die (decl);
15136       gcc_assert (decl_die);
15137     }
15138
15139   return decl_die;
15140 }
15141
15142 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15143    always returned.  */
15144
15145 static dw_die_ref
15146 force_type_die (tree type)
15147 {
15148   dw_die_ref type_die;
15149
15150   type_die = lookup_type_die (type);
15151   if (!type_die)
15152     {
15153       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15154
15155       type_die = modified_type_die (type, TYPE_READONLY (type),
15156                                     TYPE_VOLATILE (type), context_die);
15157       gcc_assert (type_die);
15158     }
15159   return type_die;
15160 }
15161
15162 /* Force out any required namespaces to be able to output DECL,
15163    and return the new context_die for it, if it's changed.  */
15164
15165 static dw_die_ref
15166 setup_namespace_context (tree thing, dw_die_ref context_die)
15167 {
15168   tree context = (DECL_P (thing)
15169                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15170   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15171     /* Force out the namespace.  */
15172     context_die = force_decl_die (context);
15173
15174   return context_die;
15175 }
15176
15177 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15178    type) within its namespace, if appropriate.
15179
15180    For compatibility with older debuggers, namespace DIEs only contain
15181    declarations; all definitions are emitted at CU scope.  */
15182
15183 static dw_die_ref
15184 declare_in_namespace (tree thing, dw_die_ref context_die)
15185 {
15186   dw_die_ref ns_context;
15187
15188   if (debug_info_level <= DINFO_LEVEL_TERSE)
15189     return context_die;
15190
15191   /* If this decl is from an inlined function, then don't try to emit it in its
15192      namespace, as we will get confused.  It would have already been emitted
15193      when the abstract instance of the inline function was emitted anyways.  */
15194   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15195     return context_die;
15196
15197   ns_context = setup_namespace_context (thing, context_die);
15198
15199   if (ns_context != context_die)
15200     {
15201       if (is_fortran ())
15202         return ns_context;
15203       if (DECL_P (thing))
15204         gen_decl_die (thing, ns_context);
15205       else
15206         gen_type_die (thing, ns_context);
15207     }
15208   return context_die;
15209 }
15210
15211 /* Generate a DIE for a namespace or namespace alias.  */
15212
15213 static void
15214 gen_namespace_die (tree decl)
15215 {
15216   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
15217
15218   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15219      they are an alias of.  */
15220   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15221     {
15222       /* Output a real namespace or module.  */
15223       dw_die_ref namespace_die
15224         = new_die (is_fortran () ? DW_TAG_module : DW_TAG_namespace,
15225                    context_die, decl);
15226       /* For Fortran modules defined in different CU don't add src coords.  */
15227       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15228         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15229       else
15230         add_name_and_src_coords_attributes (namespace_die, decl);
15231       if (DECL_EXTERNAL (decl))
15232         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15233       equate_decl_number_to_die (decl, namespace_die);
15234     }
15235   else
15236     {
15237       /* Output a namespace alias.  */
15238
15239       /* Force out the namespace we are an alias of, if necessary.  */
15240       dw_die_ref origin_die
15241         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15242
15243       /* Now create the namespace alias DIE.  */
15244       dw_die_ref namespace_die
15245         = new_die (DW_TAG_imported_declaration, context_die, decl);
15246       add_name_and_src_coords_attributes (namespace_die, decl);
15247       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15248       equate_decl_number_to_die (decl, namespace_die);
15249     }
15250 }
15251
15252 /* Generate Dwarf debug information for a decl described by DECL.  */
15253
15254 static void
15255 gen_decl_die (tree decl, dw_die_ref context_die)
15256 {
15257   tree origin;
15258
15259   if (DECL_P (decl) && DECL_IGNORED_P (decl))
15260     return;
15261
15262   switch (TREE_CODE (decl))
15263     {
15264     case ERROR_MARK:
15265       break;
15266
15267     case CONST_DECL:
15268       if (!is_fortran ())
15269         {
15270           /* The individual enumerators of an enum type get output when we output
15271              the Dwarf representation of the relevant enum type itself.  */
15272           break;
15273         }
15274
15275       /* Emit its type.  */
15276       gen_type_die (TREE_TYPE (decl), context_die);
15277
15278       /* And its containing namespace.  */
15279       context_die = declare_in_namespace (decl, context_die);
15280
15281       gen_const_die (decl, context_die);
15282       break;
15283
15284     case FUNCTION_DECL:
15285       /* Don't output any DIEs to represent mere function declarations,
15286          unless they are class members or explicit block externs.  */
15287       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
15288           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
15289         break;
15290
15291 #if 0
15292       /* FIXME */
15293       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15294          on local redeclarations of global functions.  That seems broken.  */
15295       if (current_function_decl != decl)
15296         /* This is only a declaration.  */;
15297 #endif
15298
15299       /* If we're emitting a clone, emit info for the abstract instance.  */
15300       if (DECL_ORIGIN (decl) != decl)
15301         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
15302
15303       /* If we're emitting an out-of-line copy of an inline function,
15304          emit info for the abstract instance and set up to refer to it.  */
15305       else if (cgraph_function_possibly_inlined_p (decl)
15306                && ! DECL_ABSTRACT (decl)
15307                && ! class_or_namespace_scope_p (context_die)
15308                /* dwarf2out_abstract_function won't emit a die if this is just
15309                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15310                   that case, because that works only if we have a die.  */
15311                && DECL_INITIAL (decl) != NULL_TREE)
15312         {
15313           dwarf2out_abstract_function (decl);
15314           set_decl_origin_self (decl);
15315         }
15316
15317       /* Otherwise we're emitting the primary DIE for this decl.  */
15318       else if (debug_info_level > DINFO_LEVEL_TERSE)
15319         {
15320           /* Before we describe the FUNCTION_DECL itself, make sure that we
15321              have described its return type.  */
15322           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15323
15324           /* And its virtual context.  */
15325           if (DECL_VINDEX (decl) != NULL_TREE)
15326             gen_type_die (DECL_CONTEXT (decl), context_die);
15327
15328           /* And its containing type.  */
15329           origin = decl_class_context (decl);
15330           if (origin != NULL_TREE)
15331             gen_type_die_for_member (origin, decl, context_die);
15332
15333           /* And its containing namespace.  */
15334           context_die = declare_in_namespace (decl, context_die);
15335         }
15336
15337       /* Now output a DIE to represent the function itself.  */
15338       gen_subprogram_die (decl, context_die);
15339       break;
15340
15341     case TYPE_DECL:
15342       /* If we are in terse mode, don't generate any DIEs to represent any
15343          actual typedefs.  */
15344       if (debug_info_level <= DINFO_LEVEL_TERSE)
15345         break;
15346
15347       /* In the special case of a TYPE_DECL node representing the declaration
15348          of some type tag, if the given TYPE_DECL is marked as having been
15349          instantiated from some other (original) TYPE_DECL node (e.g. one which
15350          was generated within the original definition of an inline function) we
15351          have to generate a special (abbreviated) DW_TAG_structure_type,
15352          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
15353       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
15354           && is_tagged_type (TREE_TYPE (decl)))
15355         {
15356           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
15357           break;
15358         }
15359
15360       if (is_redundant_typedef (decl))
15361         gen_type_die (TREE_TYPE (decl), context_die);
15362       else
15363         /* Output a DIE to represent the typedef itself.  */
15364         gen_typedef_die (decl, context_die);
15365       break;
15366
15367     case LABEL_DECL:
15368       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15369         gen_label_die (decl, context_die);
15370       break;
15371
15372     case VAR_DECL:
15373     case RESULT_DECL:
15374       /* If we are in terse mode, don't generate any DIEs to represent any
15375          variable declarations or definitions.  */
15376       if (debug_info_level <= DINFO_LEVEL_TERSE)
15377         break;
15378
15379       /* Output any DIEs that are needed to specify the type of this data
15380          object.  */
15381       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
15382         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15383       else
15384         gen_type_die (TREE_TYPE (decl), context_die);
15385
15386       /* And its containing type.  */
15387       origin = decl_class_context (decl);
15388       if (origin != NULL_TREE)
15389         gen_type_die_for_member (origin, decl, context_die);
15390
15391       /* And its containing namespace.  */
15392       context_die = declare_in_namespace (decl, context_die);
15393
15394       /* Now output the DIE to represent the data object itself.  This gets
15395          complicated because of the possibility that the VAR_DECL really
15396          represents an inlined instance of a formal parameter for an inline
15397          function.  */
15398       origin = decl_ultimate_origin (decl);
15399       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15400         gen_formal_parameter_die (decl, context_die);
15401       else
15402         gen_variable_die (decl, context_die);
15403       break;
15404
15405     case FIELD_DECL:
15406       /* Ignore the nameless fields that are used to skip bits but handle C++
15407          anonymous unions and structs.  */
15408       if (DECL_NAME (decl) != NULL_TREE
15409           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15410           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15411         {
15412           gen_type_die (member_declared_type (decl), context_die);
15413           gen_field_die (decl, context_die);
15414         }
15415       break;
15416
15417     case PARM_DECL:
15418       if (DECL_BY_REFERENCE (decl))
15419         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15420       else
15421         gen_type_die (TREE_TYPE (decl), context_die);
15422       gen_formal_parameter_die (decl, context_die);
15423       break;
15424
15425     case NAMESPACE_DECL:
15426     case IMPORTED_DECL:
15427       gen_namespace_die (decl);
15428       break;
15429
15430     default:
15431       /* Probably some frontend-internal decl.  Assume we don't care.  */
15432       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15433       break;
15434     }
15435 }
15436 \f
15437 /* Output debug information for global decl DECL.  Called from toplev.c after
15438    compilation proper has finished.  */
15439
15440 static void
15441 dwarf2out_global_decl (tree decl)
15442 {
15443   /* Output DWARF2 information for file-scope tentative data object
15444      declarations, file-scope (extern) function declarations (which
15445      had no corresponding body) and file-scope tagged type declarations
15446      and definitions which have not yet been forced out.  */
15447   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15448     dwarf2out_decl (decl);
15449 }
15450
15451 /* Output debug information for type decl DECL.  Called from toplev.c
15452    and from language front ends (to record built-in types).  */
15453 static void
15454 dwarf2out_type_decl (tree decl, int local)
15455 {
15456   if (!local)
15457     dwarf2out_decl (decl);
15458 }
15459
15460 /* Output debug information for imported module or decl DECL.
15461    NAME is non-NULL name in the lexical block if the decl has been renamed.
15462    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15463    that DECL belongs to.
15464    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15465 static void
15466 dwarf2out_imported_module_or_decl_1 (tree decl,
15467                                      tree name,
15468                                      tree lexical_block,
15469                                      dw_die_ref lexical_block_die)
15470 {
15471   expanded_location xloc;
15472   dw_die_ref imported_die = NULL;
15473   dw_die_ref at_import_die;
15474
15475   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15476     {
15477       if (is_base_type (TREE_TYPE (decl)))
15478         at_import_die = base_type_die (TREE_TYPE (decl));
15479       else
15480         at_import_die = force_type_die (TREE_TYPE (decl));
15481       /* For namespace N { typedef void T; } using N::T; base_type_die
15482          returns NULL, but DW_TAG_imported_declaration requires
15483          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15484       if (!at_import_die)
15485         {
15486           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15487           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15488           at_import_die = lookup_type_die (TREE_TYPE (decl));
15489           gcc_assert (at_import_die);
15490         }
15491     }
15492   else if (TREE_CODE (decl) == IMPORTED_DECL)
15493     {
15494       tree imported_ns_decl;
15495       /* IMPORTED_DECL nodes that are not imported namespace are just not
15496          supported yet.  */
15497       gcc_assert (DECL_INITIAL (decl)
15498                   && TREE_CODE (DECL_INITIAL (decl)) == NAMESPACE_DECL);
15499       imported_ns_decl = DECL_INITIAL (decl);
15500       at_import_die = lookup_decl_die (imported_ns_decl);
15501       if (!at_import_die)
15502         at_import_die = force_decl_die (imported_ns_decl);
15503       gcc_assert (at_import_die);
15504     }
15505   else
15506     {
15507       at_import_die = lookup_decl_die (decl);
15508       if (!at_import_die)
15509         {
15510           /* If we're trying to avoid duplicate debug info, we may not have
15511              emitted the member decl for this field.  Emit it now.  */
15512           if (TREE_CODE (decl) == FIELD_DECL)
15513             {
15514               tree type = DECL_CONTEXT (decl);
15515
15516               if (TYPE_CONTEXT (type)
15517                   && TYPE_P (TYPE_CONTEXT (type))
15518                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15519                                                 DINFO_USAGE_DIR_USE))
15520                 return;
15521               gen_type_die_for_member (type, decl,
15522                                        get_context_die (TYPE_CONTEXT (type)));
15523             }
15524           at_import_die = force_decl_die (decl);
15525         }
15526     }
15527
15528   if (TREE_CODE (decl) == NAMESPACE_DECL)
15529     imported_die = new_die (DW_TAG_imported_module,
15530                             lexical_block_die,
15531                             lexical_block);
15532   else
15533     imported_die = new_die (DW_TAG_imported_declaration,
15534                             lexical_block_die,
15535                             lexical_block);
15536
15537   xloc = expand_location (input_location);
15538   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15539   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15540   if (name)
15541     add_AT_string (imported_die, DW_AT_name,
15542                    IDENTIFIER_POINTER (name));
15543   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15544 }
15545
15546 /* Output debug information for imported module or decl DECL.
15547    NAME is non-NULL name in context if the decl has been renamed.
15548    CHILD is true if decl is one of the renamed decls as part of
15549    importing whole module.  */
15550
15551 static void
15552 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15553                                    bool child)
15554 {
15555   /* dw_die_ref at_import_die;  */
15556   dw_die_ref scope_die;
15557
15558   if (debug_info_level <= DINFO_LEVEL_TERSE)
15559     return;
15560
15561   gcc_assert (decl);
15562
15563   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15564      We need decl DIE for reference and scope die. First, get DIE for the decl
15565      itself.  */
15566
15567   /* Get the scope die for decl context. Use comp_unit_die for global module
15568      or decl. If die is not found for non globals, force new die.  */
15569   if (context
15570       && TYPE_P (context)
15571       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15572     return;
15573   scope_die = get_context_die (context);
15574
15575   if (child)
15576     {
15577       gcc_assert (scope_die->die_child);
15578       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15579       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15580       scope_die = scope_die->die_child;
15581     }
15582
15583   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15584   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15585
15586 }
15587
15588 /* Write the debugging output for DECL.  */
15589
15590 void
15591 dwarf2out_decl (tree decl)
15592 {
15593   dw_die_ref context_die = comp_unit_die;
15594
15595   switch (TREE_CODE (decl))
15596     {
15597     case ERROR_MARK:
15598       return;
15599
15600     case FUNCTION_DECL:
15601       /* What we would really like to do here is to filter out all mere
15602          file-scope declarations of file-scope functions which are never
15603          referenced later within this translation unit (and keep all of ones
15604          that *are* referenced later on) but we aren't clairvoyant, so we have
15605          no idea which functions will be referenced in the future (i.e. later
15606          on within the current translation unit). So here we just ignore all
15607          file-scope function declarations which are not also definitions.  If
15608          and when the debugger needs to know something about these functions,
15609          it will have to hunt around and find the DWARF information associated
15610          with the definition of the function.
15611
15612          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15613          nodes represent definitions and which ones represent mere
15614          declarations.  We have to check DECL_INITIAL instead. That's because
15615          the C front-end supports some weird semantics for "extern inline"
15616          function definitions.  These can get inlined within the current
15617          translation unit (and thus, we need to generate Dwarf info for their
15618          abstract instances so that the Dwarf info for the concrete inlined
15619          instances can have something to refer to) but the compiler never
15620          generates any out-of-lines instances of such things (despite the fact
15621          that they *are* definitions).
15622
15623          The important point is that the C front-end marks these "extern
15624          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15625          them anyway. Note that the C++ front-end also plays some similar games
15626          for inline function definitions appearing within include files which
15627          also contain `#pragma interface' pragmas.  */
15628       if (DECL_INITIAL (decl) == NULL_TREE)
15629         return;
15630
15631       /* If we're a nested function, initially use a parent of NULL; if we're
15632          a plain function, this will be fixed up in decls_for_scope.  If
15633          we're a method, it will be ignored, since we already have a DIE.  */
15634       if (decl_function_context (decl)
15635           /* But if we're in terse mode, we don't care about scope.  */
15636           && debug_info_level > DINFO_LEVEL_TERSE)
15637         context_die = NULL;
15638       break;
15639
15640     case VAR_DECL:
15641       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15642          declaration and if the declaration was never even referenced from
15643          within this entire compilation unit.  We suppress these DIEs in
15644          order to save space in the .debug section (by eliminating entries
15645          which are probably useless).  Note that we must not suppress
15646          block-local extern declarations (whether used or not) because that
15647          would screw-up the debugger's name lookup mechanism and cause it to
15648          miss things which really ought to be in scope at a given point.  */
15649       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15650         return;
15651
15652       /* For local statics lookup proper context die.  */
15653       if (TREE_STATIC (decl) && decl_function_context (decl))
15654         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15655
15656       /* If we are in terse mode, don't generate any DIEs to represent any
15657          variable declarations or definitions.  */
15658       if (debug_info_level <= DINFO_LEVEL_TERSE)
15659         return;
15660       break;
15661
15662     case CONST_DECL:
15663       if (debug_info_level <= DINFO_LEVEL_TERSE)
15664         return;
15665       if (!is_fortran ())
15666         return;
15667       if (TREE_STATIC (decl) && decl_function_context (decl))
15668         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15669       break;
15670
15671     case NAMESPACE_DECL:
15672     case IMPORTED_DECL:
15673       if (debug_info_level <= DINFO_LEVEL_TERSE)
15674         return;
15675       if (lookup_decl_die (decl) != NULL)
15676         return;
15677       break;
15678
15679     case TYPE_DECL:
15680       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15681       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15682         return;
15683
15684       /* Don't bother trying to generate any DIEs to represent any of the
15685          normal built-in types for the language we are compiling.  */
15686       if (DECL_IS_BUILTIN (decl))
15687         {
15688           /* OK, we need to generate one for `bool' so GDB knows what type
15689              comparisons have.  */
15690           if (is_cxx ()
15691               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15692               && ! DECL_IGNORED_P (decl))
15693             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15694
15695           return;
15696         }
15697
15698       /* If we are in terse mode, don't generate any DIEs for types.  */
15699       if (debug_info_level <= DINFO_LEVEL_TERSE)
15700         return;
15701
15702       /* If we're a function-scope tag, initially use a parent of NULL;
15703          this will be fixed up in decls_for_scope.  */
15704       if (decl_function_context (decl))
15705         context_die = NULL;
15706
15707       break;
15708
15709     default:
15710       return;
15711     }
15712
15713   gen_decl_die (decl, context_die);
15714 }
15715
15716 /* Output a marker (i.e. a label) for the beginning of the generated code for
15717    a lexical block.  */
15718
15719 static void
15720 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15721                        unsigned int blocknum)
15722 {
15723   switch_to_section (current_function_section ());
15724   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15725 }
15726
15727 /* Output a marker (i.e. a label) for the end of the generated code for a
15728    lexical block.  */
15729
15730 static void
15731 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15732 {
15733   switch_to_section (current_function_section ());
15734   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15735 }
15736
15737 /* Returns nonzero if it is appropriate not to emit any debugging
15738    information for BLOCK, because it doesn't contain any instructions.
15739
15740    Don't allow this for blocks with nested functions or local classes
15741    as we would end up with orphans, and in the presence of scheduling
15742    we may end up calling them anyway.  */
15743
15744 static bool
15745 dwarf2out_ignore_block (const_tree block)
15746 {
15747   tree decl;
15748
15749   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15750     if (TREE_CODE (decl) == FUNCTION_DECL
15751         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15752       return 0;
15753
15754   return 1;
15755 }
15756
15757 /* Hash table routines for file_hash.  */
15758
15759 static int
15760 file_table_eq (const void *p1_p, const void *p2_p)
15761 {
15762   const struct dwarf_file_data *const p1 =
15763     (const struct dwarf_file_data *) p1_p;
15764   const char *const p2 = (const char *) p2_p;
15765   return strcmp (p1->filename, p2) == 0;
15766 }
15767
15768 static hashval_t
15769 file_table_hash (const void *p_p)
15770 {
15771   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15772   return htab_hash_string (p->filename);
15773 }
15774
15775 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15776    dwarf2out.c) and return its "index".  The index of each (known) filename is
15777    just a unique number which is associated with only that one filename.  We
15778    need such numbers for the sake of generating labels (in the .debug_sfnames
15779    section) and references to those files numbers (in the .debug_srcinfo
15780    and.debug_macinfo sections).  If the filename given as an argument is not
15781    found in our current list, add it to the list and assign it the next
15782    available unique index number.  In order to speed up searches, we remember
15783    the index of the filename was looked up last.  This handles the majority of
15784    all searches.  */
15785
15786 static struct dwarf_file_data *
15787 lookup_filename (const char *file_name)
15788 {
15789   void ** slot;
15790   struct dwarf_file_data * created;
15791
15792   /* Check to see if the file name that was searched on the previous
15793      call matches this file name.  If so, return the index.  */
15794   if (file_table_last_lookup
15795       && (file_name == file_table_last_lookup->filename
15796           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15797     return file_table_last_lookup;
15798
15799   /* Didn't match the previous lookup, search the table.  */
15800   slot = htab_find_slot_with_hash (file_table, file_name,
15801                                    htab_hash_string (file_name), INSERT);
15802   if (*slot)
15803     return (struct dwarf_file_data *) *slot;
15804
15805   created = GGC_NEW (struct dwarf_file_data);
15806   created->filename = file_name;
15807   created->emitted_number = 0;
15808   *slot = created;
15809   return created;
15810 }
15811
15812 /* If the assembler will construct the file table, then translate the compiler
15813    internal file table number into the assembler file table number, and emit
15814    a .file directive if we haven't already emitted one yet.  The file table
15815    numbers are different because we prune debug info for unused variables and
15816    types, which may include filenames.  */
15817
15818 static int
15819 maybe_emit_file (struct dwarf_file_data * fd)
15820 {
15821   if (! fd->emitted_number)
15822     {
15823       if (last_emitted_file)
15824         fd->emitted_number = last_emitted_file->emitted_number + 1;
15825       else
15826         fd->emitted_number = 1;
15827       last_emitted_file = fd;
15828
15829       if (DWARF2_ASM_LINE_DEBUG_INFO)
15830         {
15831           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15832           output_quoted_string (asm_out_file,
15833                                 remap_debug_filename (fd->filename));
15834           fputc ('\n', asm_out_file);
15835         }
15836     }
15837
15838   return fd->emitted_number;
15839 }
15840
15841 /* Called by the final INSN scan whenever we see a var location.  We
15842    use it to drop labels in the right places, and throw the location in
15843    our lookup table.  */
15844
15845 static void
15846 dwarf2out_var_location (rtx loc_note)
15847 {
15848   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15849   struct var_loc_node *newloc;
15850   rtx prev_insn;
15851   static rtx last_insn;
15852   static const char *last_label;
15853   tree decl;
15854
15855   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15856     return;
15857   prev_insn = PREV_INSN (loc_note);
15858
15859   newloc = GGC_CNEW (struct var_loc_node);
15860   /* If the insn we processed last time is the previous insn
15861      and it is also a var location note, use the label we emitted
15862      last time.  */
15863   if (last_insn != NULL_RTX
15864       && last_insn == prev_insn
15865       && NOTE_P (prev_insn)
15866       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15867     {
15868       newloc->label = last_label;
15869     }
15870   else
15871     {
15872       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15873       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15874       loclabel_num++;
15875       newloc->label = ggc_strdup (loclabel);
15876     }
15877   newloc->var_loc_note = loc_note;
15878   newloc->next = NULL;
15879
15880   if (cfun && in_cold_section_p)
15881     newloc->section_label = crtl->subsections.cold_section_label;
15882   else
15883     newloc->section_label = text_section_label;
15884
15885   last_insn = loc_note;
15886   last_label = newloc->label;
15887   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15888   add_var_loc_to_decl (decl, newloc);
15889 }
15890
15891 /* We need to reset the locations at the beginning of each
15892    function. We can't do this in the end_function hook, because the
15893    declarations that use the locations won't have been output when
15894    that hook is called.  Also compute have_multiple_function_sections here.  */
15895
15896 static void
15897 dwarf2out_begin_function (tree fun)
15898 {
15899   htab_empty (decl_loc_table);
15900
15901   if (function_section (fun) != text_section)
15902     have_multiple_function_sections = true;
15903
15904   dwarf2out_note_section_used ();
15905 }
15906
15907 /* Output a label to mark the beginning of a source code line entry
15908    and record information relating to this source line, in
15909    'line_info_table' for later output of the .debug_line section.  */
15910
15911 static void
15912 dwarf2out_source_line (unsigned int line, const char *filename)
15913 {
15914   if (debug_info_level >= DINFO_LEVEL_NORMAL
15915       && line != 0)
15916     {
15917       int file_num = maybe_emit_file (lookup_filename (filename));
15918
15919       switch_to_section (current_function_section ());
15920
15921       /* If requested, emit something human-readable.  */
15922       if (flag_debug_asm)
15923         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15924                  filename, line);
15925
15926       if (DWARF2_ASM_LINE_DEBUG_INFO)
15927         {
15928           /* Emit the .loc directive understood by GNU as.  */
15929           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15930
15931           /* Indicate that line number info exists.  */
15932           line_info_table_in_use++;
15933         }
15934       else if (function_section (current_function_decl) != text_section)
15935         {
15936           dw_separate_line_info_ref line_info;
15937           targetm.asm_out.internal_label (asm_out_file,
15938                                           SEPARATE_LINE_CODE_LABEL,
15939                                           separate_line_info_table_in_use);
15940
15941           /* Expand the line info table if necessary.  */
15942           if (separate_line_info_table_in_use
15943               == separate_line_info_table_allocated)
15944             {
15945               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15946               separate_line_info_table
15947                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15948                                  separate_line_info_table,
15949                                  separate_line_info_table_allocated);
15950               memset (separate_line_info_table
15951                        + separate_line_info_table_in_use,
15952                       0,
15953                       (LINE_INFO_TABLE_INCREMENT
15954                        * sizeof (dw_separate_line_info_entry)));
15955             }
15956
15957           /* Add the new entry at the end of the line_info_table.  */
15958           line_info
15959             = &separate_line_info_table[separate_line_info_table_in_use++];
15960           line_info->dw_file_num = file_num;
15961           line_info->dw_line_num = line;
15962           line_info->function = current_function_funcdef_no;
15963         }
15964       else
15965         {
15966           dw_line_info_ref line_info;
15967
15968           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15969                                      line_info_table_in_use);
15970
15971           /* Expand the line info table if necessary.  */
15972           if (line_info_table_in_use == line_info_table_allocated)
15973             {
15974               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15975               line_info_table
15976                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15977                                  line_info_table_allocated);
15978               memset (line_info_table + line_info_table_in_use, 0,
15979                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15980             }
15981
15982           /* Add the new entry at the end of the line_info_table.  */
15983           line_info = &line_info_table[line_info_table_in_use++];
15984           line_info->dw_file_num = file_num;
15985           line_info->dw_line_num = line;
15986         }
15987     }
15988 }
15989
15990 /* Record the beginning of a new source file.  */
15991
15992 static void
15993 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15994 {
15995   if (flag_eliminate_dwarf2_dups)
15996     {
15997       /* Record the beginning of the file for break_out_includes.  */
15998       dw_die_ref bincl_die;
15999
16000       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16001       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16002     }
16003
16004   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16005     {
16006       int file_num = maybe_emit_file (lookup_filename (filename));
16007
16008       switch_to_section (debug_macinfo_section);
16009       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16010       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16011                                    lineno);
16012
16013       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16014     }
16015 }
16016
16017 /* Record the end of a source file.  */
16018
16019 static void
16020 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16021 {
16022   if (flag_eliminate_dwarf2_dups)
16023     /* Record the end of the file for break_out_includes.  */
16024     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16025
16026   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16027     {
16028       switch_to_section (debug_macinfo_section);
16029       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16030     }
16031 }
16032
16033 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16034    the tail part of the directive line, i.e. the part which is past the
16035    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16036
16037 static void
16038 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16039                   const char *buffer ATTRIBUTE_UNUSED)
16040 {
16041   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16042     {
16043       switch_to_section (debug_macinfo_section);
16044       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16045       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16046       dw2_asm_output_nstring (buffer, -1, "The macro");
16047     }
16048 }
16049
16050 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16051    the tail part of the directive line, i.e. the part which is past the
16052    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16053
16054 static void
16055 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16056                  const char *buffer ATTRIBUTE_UNUSED)
16057 {
16058   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16059     {
16060       switch_to_section (debug_macinfo_section);
16061       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16062       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16063       dw2_asm_output_nstring (buffer, -1, "The macro");
16064     }
16065 }
16066
16067 /* Set up for Dwarf output at the start of compilation.  */
16068
16069 static void
16070 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16071 {
16072   /* Allocate the file_table.  */
16073   file_table = htab_create_ggc (50, file_table_hash,
16074                                 file_table_eq, NULL);
16075
16076   /* Allocate the decl_die_table.  */
16077   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16078                                     decl_die_table_eq, NULL);
16079
16080   /* Allocate the decl_loc_table.  */
16081   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16082                                     decl_loc_table_eq, NULL);
16083
16084   /* Allocate the initial hunk of the decl_scope_table.  */
16085   decl_scope_table = VEC_alloc (tree, gc, 256);
16086
16087   /* Allocate the initial hunk of the abbrev_die_table.  */
16088   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16089   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16090   /* Zero-th entry is allocated, but unused.  */
16091   abbrev_die_table_in_use = 1;
16092
16093   /* Allocate the initial hunk of the line_info_table.  */
16094   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16095   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16096
16097   /* Zero-th entry is allocated, but unused.  */
16098   line_info_table_in_use = 1;
16099
16100   /* Allocate the pubtypes and pubnames vectors.  */
16101   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16102   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16103
16104   /* Generate the initial DIE for the .debug section.  Note that the (string)
16105      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16106      will (typically) be a relative pathname and that this pathname should be
16107      taken as being relative to the directory from which the compiler was
16108      invoked when the given (base) source file was compiled.  We will fill
16109      in this value in dwarf2out_finish.  */
16110   comp_unit_die = gen_compile_unit_die (NULL);
16111
16112   incomplete_types = VEC_alloc (tree, gc, 64);
16113
16114   used_rtx_array = VEC_alloc (rtx, gc, 32);
16115
16116   debug_info_section = get_section (DEBUG_INFO_SECTION,
16117                                     SECTION_DEBUG, NULL);
16118   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16119                                       SECTION_DEBUG, NULL);
16120   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16121                                        SECTION_DEBUG, NULL);
16122   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16123                                        SECTION_DEBUG, NULL);
16124   debug_line_section = get_section (DEBUG_LINE_SECTION,
16125                                     SECTION_DEBUG, NULL);
16126   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16127                                    SECTION_DEBUG, NULL);
16128   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16129                                         SECTION_DEBUG, NULL);
16130 #ifdef DEBUG_PUBTYPES_SECTION
16131   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16132                                         SECTION_DEBUG, NULL);
16133 #endif
16134   debug_str_section = get_section (DEBUG_STR_SECTION,
16135                                    DEBUG_STR_SECTION_FLAGS, NULL);
16136   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16137                                       SECTION_DEBUG, NULL);
16138   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16139                                      SECTION_DEBUG, NULL);
16140
16141   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16142   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16143                                DEBUG_ABBREV_SECTION_LABEL, 0);
16144   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16145   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16146                                COLD_TEXT_SECTION_LABEL, 0);
16147   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16148
16149   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16150                                DEBUG_INFO_SECTION_LABEL, 0);
16151   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16152                                DEBUG_LINE_SECTION_LABEL, 0);
16153   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16154                                DEBUG_RANGES_SECTION_LABEL, 0);
16155   switch_to_section (debug_abbrev_section);
16156   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16157   switch_to_section (debug_info_section);
16158   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16159   switch_to_section (debug_line_section);
16160   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16161
16162   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16163     {
16164       switch_to_section (debug_macinfo_section);
16165       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16166                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16167       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16168     }
16169
16170   switch_to_section (text_section);
16171   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16172   if (flag_reorder_blocks_and_partition)
16173     {
16174       cold_text_section = unlikely_text_section ();
16175       switch_to_section (cold_text_section);
16176       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16177     }
16178 }
16179
16180 /* A helper function for dwarf2out_finish called through
16181    ht_forall.  Emit one queued .debug_str string.  */
16182
16183 static int
16184 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16185 {
16186   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16187
16188   if (node->form == DW_FORM_strp)
16189     {
16190       switch_to_section (debug_str_section);
16191       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16192       assemble_string (node->str, strlen (node->str) + 1);
16193     }
16194
16195   return 1;
16196 }
16197
16198 #if ENABLE_ASSERT_CHECKING
16199 /* Verify that all marks are clear.  */
16200
16201 static void
16202 verify_marks_clear (dw_die_ref die)
16203 {
16204   dw_die_ref c;
16205
16206   gcc_assert (! die->die_mark);
16207   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16208 }
16209 #endif /* ENABLE_ASSERT_CHECKING */
16210
16211 /* Clear the marks for a die and its children.
16212    Be cool if the mark isn't set.  */
16213
16214 static void
16215 prune_unmark_dies (dw_die_ref die)
16216 {
16217   dw_die_ref c;
16218
16219   if (die->die_mark)
16220     die->die_mark = 0;
16221   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16222 }
16223
16224 /* Given DIE that we're marking as used, find any other dies
16225    it references as attributes and mark them as used.  */
16226
16227 static void
16228 prune_unused_types_walk_attribs (dw_die_ref die)
16229 {
16230   dw_attr_ref a;
16231   unsigned ix;
16232
16233   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16234     {
16235       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16236         {
16237           /* A reference to another DIE.
16238              Make sure that it will get emitted.  */
16239           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16240         }
16241       /* Set the string's refcount to 0 so that prune_unused_types_mark
16242          accounts properly for it.  */
16243       if (AT_class (a) == dw_val_class_str)
16244         a->dw_attr_val.v.val_str->refcount = 0;
16245     }
16246 }
16247
16248
16249 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16250    to DIE's children.  */
16251
16252 static void
16253 prune_unused_types_mark (dw_die_ref die, int dokids)
16254 {
16255   dw_die_ref c;
16256
16257   if (die->die_mark == 0)
16258     {
16259       /* We haven't done this node yet.  Mark it as used.  */
16260       die->die_mark = 1;
16261
16262       /* We also have to mark its parents as used.
16263          (But we don't want to mark our parents' kids due to this.)  */
16264       if (die->die_parent)
16265         prune_unused_types_mark (die->die_parent, 0);
16266
16267       /* Mark any referenced nodes.  */
16268       prune_unused_types_walk_attribs (die);
16269
16270       /* If this node is a specification,
16271          also mark the definition, if it exists.  */
16272       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16273         prune_unused_types_mark (die->die_definition, 1);
16274     }
16275
16276   if (dokids && die->die_mark != 2)
16277     {
16278       /* We need to walk the children, but haven't done so yet.
16279          Remember that we've walked the kids.  */
16280       die->die_mark = 2;
16281
16282       /* If this is an array type, we need to make sure our
16283          kids get marked, even if they're types.  */
16284       if (die->die_tag == DW_TAG_array_type)
16285         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16286       else
16287         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16288     }
16289 }
16290
16291 /* For local classes, look if any static member functions were emitted
16292    and if so, mark them.  */
16293
16294 static void
16295 prune_unused_types_walk_local_classes (dw_die_ref die)
16296 {
16297   dw_die_ref c;
16298
16299   if (die->die_mark == 2)
16300     return;
16301
16302   switch (die->die_tag)
16303     {
16304     case DW_TAG_structure_type:
16305     case DW_TAG_union_type:
16306     case DW_TAG_class_type:
16307       break;
16308
16309     case DW_TAG_subprogram:
16310       if (!get_AT_flag (die, DW_AT_declaration)
16311           || die->die_definition != NULL)
16312         prune_unused_types_mark (die, 1);
16313       return;
16314
16315     default:
16316       return;
16317     }
16318
16319   /* Mark children.  */
16320   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16321 }
16322
16323 /* Walk the tree DIE and mark types that we actually use.  */
16324
16325 static void
16326 prune_unused_types_walk (dw_die_ref die)
16327 {
16328   dw_die_ref c;
16329
16330   /* Don't do anything if this node is already marked and
16331      children have been marked as well.  */
16332   if (die->die_mark == 2)
16333     return;
16334
16335   switch (die->die_tag)
16336     {
16337     case DW_TAG_structure_type:
16338     case DW_TAG_union_type:
16339     case DW_TAG_class_type:
16340       if (die->die_perennial_p)
16341         break;
16342
16343       for (c = die->die_parent; c; c = c->die_parent)
16344         if (c->die_tag == DW_TAG_subprogram)
16345           break;
16346
16347       /* Finding used static member functions inside of classes
16348          is needed just for local classes, because for other classes
16349          static member function DIEs with DW_AT_specification
16350          are emitted outside of the DW_TAG_*_type.  If we ever change
16351          it, we'd need to call this even for non-local classes.  */
16352       if (c)
16353         prune_unused_types_walk_local_classes (die);
16354
16355       /* It's a type node --- don't mark it.  */
16356       return;
16357
16358     case DW_TAG_const_type:
16359     case DW_TAG_packed_type:
16360     case DW_TAG_pointer_type:
16361     case DW_TAG_reference_type:
16362     case DW_TAG_volatile_type:
16363     case DW_TAG_typedef:
16364     case DW_TAG_array_type:
16365     case DW_TAG_interface_type:
16366     case DW_TAG_friend:
16367     case DW_TAG_variant_part:
16368     case DW_TAG_enumeration_type:
16369     case DW_TAG_subroutine_type:
16370     case DW_TAG_string_type:
16371     case DW_TAG_set_type:
16372     case DW_TAG_subrange_type:
16373     case DW_TAG_ptr_to_member_type:
16374     case DW_TAG_file_type:
16375       if (die->die_perennial_p)
16376         break;
16377
16378       /* It's a type node --- don't mark it.  */
16379       return;
16380
16381     default:
16382       /* Mark everything else.  */
16383       break;
16384   }
16385
16386   if (die->die_mark == 0)
16387     {
16388       die->die_mark = 1;
16389
16390       /* Now, mark any dies referenced from here.  */
16391       prune_unused_types_walk_attribs (die);
16392     }
16393
16394   die->die_mark = 2;
16395
16396   /* Mark children.  */
16397   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16398 }
16399
16400 /* Increment the string counts on strings referred to from DIE's
16401    attributes.  */
16402
16403 static void
16404 prune_unused_types_update_strings (dw_die_ref die)
16405 {
16406   dw_attr_ref a;
16407   unsigned ix;
16408
16409   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16410     if (AT_class (a) == dw_val_class_str)
16411       {
16412         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16413         s->refcount++;
16414         /* Avoid unnecessarily putting strings that are used less than
16415            twice in the hash table.  */
16416         if (s->refcount
16417             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16418           {
16419             void ** slot;
16420             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16421                                              htab_hash_string (s->str),
16422                                              INSERT);
16423             gcc_assert (*slot == NULL);
16424             *slot = s;
16425           }
16426       }
16427 }
16428
16429 /* Remove from the tree DIE any dies that aren't marked.  */
16430
16431 static void
16432 prune_unused_types_prune (dw_die_ref die)
16433 {
16434   dw_die_ref c;
16435
16436   gcc_assert (die->die_mark);
16437   prune_unused_types_update_strings (die);
16438
16439   if (! die->die_child)
16440     return;
16441
16442   c = die->die_child;
16443   do {
16444     dw_die_ref prev = c;
16445     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16446       if (c == die->die_child)
16447         {
16448           /* No marked children between 'prev' and the end of the list.  */
16449           if (prev == c)
16450             /* No marked children at all.  */
16451             die->die_child = NULL;
16452           else
16453             {
16454               prev->die_sib = c->die_sib;
16455               die->die_child = prev;
16456             }
16457           return;
16458         }
16459
16460     if (c != prev->die_sib)
16461       prev->die_sib = c;
16462     prune_unused_types_prune (c);
16463   } while (c != die->die_child);
16464 }
16465
16466
16467 /* Remove dies representing declarations that we never use.  */
16468
16469 static void
16470 prune_unused_types (void)
16471 {
16472   unsigned int i;
16473   limbo_die_node *node;
16474   pubname_ref pub;
16475
16476 #if ENABLE_ASSERT_CHECKING
16477   /* All the marks should already be clear.  */
16478   verify_marks_clear (comp_unit_die);
16479   for (node = limbo_die_list; node; node = node->next)
16480     verify_marks_clear (node->die);
16481 #endif /* ENABLE_ASSERT_CHECKING */
16482
16483   /* Set the mark on nodes that are actually used.  */
16484   prune_unused_types_walk (comp_unit_die);
16485   for (node = limbo_die_list; node; node = node->next)
16486     prune_unused_types_walk (node->die);
16487
16488   /* Also set the mark on nodes referenced from the
16489      pubname_table or arange_table.  */
16490   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16491     prune_unused_types_mark (pub->die, 1);
16492   for (i = 0; i < arange_table_in_use; i++)
16493     prune_unused_types_mark (arange_table[i], 1);
16494
16495   /* Get rid of nodes that aren't marked; and update the string counts.  */
16496   if (debug_str_hash)
16497     htab_empty (debug_str_hash);
16498   prune_unused_types_prune (comp_unit_die);
16499   for (node = limbo_die_list; node; node = node->next)
16500     prune_unused_types_prune (node->die);
16501
16502   /* Leave the marks clear.  */
16503   prune_unmark_dies (comp_unit_die);
16504   for (node = limbo_die_list; node; node = node->next)
16505     prune_unmark_dies (node->die);
16506 }
16507
16508 /* Set the parameter to true if there are any relative pathnames in
16509    the file table.  */
16510 static int
16511 file_table_relative_p (void ** slot, void *param)
16512 {
16513   bool *p = (bool *) param;
16514   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16515   if (!IS_ABSOLUTE_PATH (d->filename))
16516     {
16517       *p = true;
16518       return 0;
16519     }
16520   return 1;
16521 }
16522
16523 /* Output stuff that dwarf requires at the end of every file,
16524    and generate the DWARF-2 debugging info.  */
16525
16526 static void
16527 dwarf2out_finish (const char *filename)
16528 {
16529   limbo_die_node *node, *next_node;
16530   dw_die_ref die = 0;
16531
16532   /* Add the name for the main input file now.  We delayed this from
16533      dwarf2out_init to avoid complications with PCH.  */
16534   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16535   if (!IS_ABSOLUTE_PATH (filename))
16536     add_comp_dir_attribute (comp_unit_die);
16537   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16538     {
16539       bool p = false;
16540       htab_traverse (file_table, file_table_relative_p, &p);
16541       if (p)
16542         add_comp_dir_attribute (comp_unit_die);
16543     }
16544
16545   /* Traverse the limbo die list, and add parent/child links.  The only
16546      dies without parents that should be here are concrete instances of
16547      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16548      For concrete instances, we can get the parent die from the abstract
16549      instance.  */
16550   for (node = limbo_die_list; node; node = next_node)
16551     {
16552       next_node = node->next;
16553       die = node->die;
16554
16555       if (die->die_parent == NULL)
16556         {
16557           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16558
16559           if (origin)
16560             add_child_die (origin->die_parent, die);
16561           else if (die == comp_unit_die)
16562             ;
16563           else if (errorcount > 0 || sorrycount > 0)
16564             /* It's OK to be confused by errors in the input.  */
16565             add_child_die (comp_unit_die, die);
16566           else
16567             {
16568               /* In certain situations, the lexical block containing a
16569                  nested function can be optimized away, which results
16570                  in the nested function die being orphaned.  Likewise
16571                  with the return type of that nested function.  Force
16572                  this to be a child of the containing function.
16573
16574                  It may happen that even the containing function got fully
16575                  inlined and optimized out.  In that case we are lost and
16576                  assign the empty child.  This should not be big issue as
16577                  the function is likely unreachable too.  */
16578               tree context = NULL_TREE;
16579
16580               gcc_assert (node->created_for);
16581
16582               if (DECL_P (node->created_for))
16583                 context = DECL_CONTEXT (node->created_for);
16584               else if (TYPE_P (node->created_for))
16585                 context = TYPE_CONTEXT (node->created_for);
16586
16587               gcc_assert (context
16588                           && (TREE_CODE (context) == FUNCTION_DECL
16589                               || TREE_CODE (context) == NAMESPACE_DECL));
16590
16591               origin = lookup_decl_die (context);
16592               if (origin)
16593                 add_child_die (origin, die);
16594               else
16595                 add_child_die (comp_unit_die, die);
16596             }
16597         }
16598     }
16599
16600   limbo_die_list = NULL;
16601
16602   /* Walk through the list of incomplete types again, trying once more to
16603      emit full debugging info for them.  */
16604   retry_incomplete_types ();
16605
16606   if (flag_eliminate_unused_debug_types)
16607     prune_unused_types ();
16608
16609   /* Generate separate CUs for each of the include files we've seen.
16610      They will go into limbo_die_list.  */
16611   if (flag_eliminate_dwarf2_dups)
16612     break_out_includes (comp_unit_die);
16613
16614   /* Traverse the DIE's and add add sibling attributes to those DIE's
16615      that have children.  */
16616   add_sibling_attributes (comp_unit_die);
16617   for (node = limbo_die_list; node; node = node->next)
16618     add_sibling_attributes (node->die);
16619
16620   /* Output a terminator label for the .text section.  */
16621   switch_to_section (text_section);
16622   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16623   if (flag_reorder_blocks_and_partition)
16624     {
16625       switch_to_section (unlikely_text_section ());
16626       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16627     }
16628
16629   /* We can only use the low/high_pc attributes if all of the code was
16630      in .text.  */
16631   if (!have_multiple_function_sections)
16632     {
16633       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16634       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16635     }
16636
16637   else
16638     {
16639       unsigned fde_idx = 0;
16640
16641       /* We need to give .debug_loc and .debug_ranges an appropriate
16642          "base address".  Use zero so that these addresses become
16643          absolute.  Historically, we've emitted the unexpected
16644          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16645          Emit both to give time for other tools to adapt.  */
16646       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16647       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16648
16649       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16650                          add_ranges_by_labels (text_section_label,
16651                                                text_end_label));
16652       if (flag_reorder_blocks_and_partition)
16653         add_ranges_by_labels (cold_text_section_label,
16654                               cold_end_label);
16655
16656       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16657         {
16658           dw_fde_ref fde = &fde_table[fde_idx];
16659
16660           if (fde->dw_fde_switched_sections)
16661             {
16662               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16663                                     fde->dw_fde_hot_section_end_label);
16664               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16665                                     fde->dw_fde_unlikely_section_end_label);
16666             }
16667           else
16668             add_ranges_by_labels (fde->dw_fde_begin,
16669                                   fde->dw_fde_end);
16670         }
16671
16672       add_ranges (NULL);
16673     }
16674
16675   /* Output location list section if necessary.  */
16676   if (have_location_lists)
16677     {
16678       /* Output the location lists info.  */
16679       switch_to_section (debug_loc_section);
16680       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16681                                    DEBUG_LOC_SECTION_LABEL, 0);
16682       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16683       output_location_lists (die);
16684     }
16685
16686   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16687     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16688                     debug_line_section_label);
16689
16690   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16691     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16692
16693   /* Output all of the compilation units.  We put the main one last so that
16694      the offsets are available to output_pubnames.  */
16695   for (node = limbo_die_list; node; node = node->next)
16696     output_comp_unit (node->die, 0);
16697
16698   output_comp_unit (comp_unit_die, 0);
16699
16700   /* Output the abbreviation table.  */
16701   switch_to_section (debug_abbrev_section);
16702   output_abbrev_section ();
16703
16704   /* Output public names table if necessary.  */
16705   if (!VEC_empty (pubname_entry, pubname_table))
16706     {
16707       switch_to_section (debug_pubnames_section);
16708       output_pubnames (pubname_table);
16709     }
16710
16711 #ifdef DEBUG_PUBTYPES_SECTION
16712   /* Output public types table if necessary.  */
16713   if (!VEC_empty (pubname_entry, pubtype_table))
16714     {
16715       switch_to_section (debug_pubtypes_section);
16716       output_pubnames (pubtype_table);
16717     }
16718 #endif
16719
16720   /* Output the address range information.  We only put functions in the arange
16721      table, so don't write it out if we don't have any.  */
16722   if (fde_table_in_use)
16723     {
16724       switch_to_section (debug_aranges_section);
16725       output_aranges ();
16726     }
16727
16728   /* Output ranges section if necessary.  */
16729   if (ranges_table_in_use)
16730     {
16731       switch_to_section (debug_ranges_section);
16732       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16733       output_ranges ();
16734     }
16735
16736   /* Output the source line correspondence table.  We must do this
16737      even if there is no line information.  Otherwise, on an empty
16738      translation unit, we will generate a present, but empty,
16739      .debug_info section.  IRIX 6.5 `nm' will then complain when
16740      examining the file.  This is done late so that any filenames
16741      used by the debug_info section are marked as 'used'.  */
16742   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16743     {
16744       switch_to_section (debug_line_section);
16745       output_line_info ();
16746     }
16747
16748   /* Have to end the macro section.  */
16749   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16750     {
16751       switch_to_section (debug_macinfo_section);
16752       dw2_asm_output_data (1, 0, "End compilation unit");
16753     }
16754
16755   /* If we emitted any DW_FORM_strp form attribute, output the string
16756      table too.  */
16757   if (debug_str_hash)
16758     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16759 }
16760 #else
16761
16762 /* This should never be used, but its address is needed for comparisons.  */
16763 const struct gcc_debug_hooks dwarf2_debug_hooks;
16764
16765 #endif /* DWARF2_DEBUGGING_INFO */
16766
16767 #include "gt-dwarf2out.h"