OSDN Git Service

0d308c9f0471ae0958f63d5422a269a7010fd2ba
[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, 2009 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Save the result of dwarf2out_do_frame across PCH.  */
114 static GTY(()) bool saved_do_cfi_asm = 0;
115
116 /* Decide whether we want to emit frame unwind information for the current
117    translation unit.  */
118
119 int
120 dwarf2out_do_frame (void)
121 {
122   /* We want to emit correct CFA location expressions or lists, so we
123      have to return true if we're going to output debug info, even if
124      we're not going to output frame or unwind info.  */
125   return (write_symbols == DWARF2_DEBUG
126           || write_symbols == VMS_AND_DWARF2_DEBUG
127           || DWARF2_FRAME_INFO || saved_do_cfi_asm
128 #ifdef DWARF2_UNWIND_INFO
129           || (DWARF2_UNWIND_INFO
130               && (flag_unwind_tables
131                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
132 #endif
133           );
134 }
135
136 /* Decide whether to emit frame unwind via assembler directives.  */
137
138 int
139 dwarf2out_do_cfi_asm (void)
140 {
141   int enc;
142
143 #ifdef MIPS_DEBUGGING_INFO
144   return false;
145 #endif
146   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147     return false;
148   if (saved_do_cfi_asm || !eh_personality_libfunc)
149     return true;
150   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151     return false;
152
153   /* Make sure the personality encoding is one the assembler can support.
154      In particular, aligned addresses can't be handled.  */
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160     return false;
161
162   saved_do_cfi_asm = true;
163   return true;
164 }
165
166 /* The size of the target's pointer type.  */
167 #ifndef PTR_SIZE
168 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169 #endif
170
171 /* Array of RTXes referenced by the debugging information, which therefore
172    must be kept around forever.  */
173 static GTY(()) VEC(rtx,gc) *used_rtx_array;
174
175 /* A pointer to the base of a list of incomplete types which might be
176    completed at some later time.  incomplete_types_list needs to be a
177    VEC(tree,gc) because we want to tell the garbage collector about
178    it.  */
179 static GTY(()) VEC(tree,gc) *incomplete_types;
180
181 /* A pointer to the base of a table of references to declaration
182    scopes.  This table is a display which tracks the nesting
183    of declaration scopes at the current scope and containing
184    scopes.  This table is used to find the proper place to
185    define type declaration DIE's.  */
186 static GTY(()) VEC(tree,gc) *decl_scope_table;
187
188 /* Pointers to various DWARF2 sections.  */
189 static GTY(()) section *debug_info_section;
190 static GTY(()) section *debug_abbrev_section;
191 static GTY(()) section *debug_aranges_section;
192 static GTY(()) section *debug_macinfo_section;
193 static GTY(()) section *debug_line_section;
194 static GTY(()) section *debug_loc_section;
195 static GTY(()) section *debug_pubnames_section;
196 static GTY(()) section *debug_pubtypes_section;
197 static GTY(()) section *debug_str_section;
198 static GTY(()) section *debug_ranges_section;
199 static GTY(()) section *debug_frame_section;
200
201 /* How to start an assembler comment.  */
202 #ifndef ASM_COMMENT_START
203 #define ASM_COMMENT_START ";#"
204 #endif
205
206 typedef struct dw_cfi_struct *dw_cfi_ref;
207 typedef struct dw_fde_struct *dw_fde_ref;
208 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
209
210 /* Call frames are described using a sequence of Call Frame
211    Information instructions.  The register number, offset
212    and address fields are provided as possible operands;
213    their use is selected by the opcode field.  */
214
215 enum dw_cfi_oprnd_type {
216   dw_cfi_oprnd_unused,
217   dw_cfi_oprnd_reg_num,
218   dw_cfi_oprnd_offset,
219   dw_cfi_oprnd_addr,
220   dw_cfi_oprnd_loc
221 };
222
223 typedef union GTY(()) dw_cfi_oprnd_struct {
224   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
225   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
226   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
227   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
228 }
229 dw_cfi_oprnd;
230
231 typedef struct GTY(()) dw_cfi_struct {
232   dw_cfi_ref dw_cfi_next;
233   enum dwarf_call_frame_info dw_cfi_opc;
234   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
235     dw_cfi_oprnd1;
236   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
237     dw_cfi_oprnd2;
238 }
239 dw_cfi_node;
240
241 /* This is how we define the location of the CFA. We use to handle it
242    as REG + OFFSET all the time,  but now it can be more complex.
243    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
244    Instead of passing around REG and OFFSET, we pass a copy
245    of this structure.  */
246 typedef struct GTY(()) cfa_loc {
247   HOST_WIDE_INT offset;
248   HOST_WIDE_INT base_offset;
249   unsigned int reg;
250   int indirect;            /* 1 if CFA is accessed via a dereference.  */
251 } dw_cfa_location;
252
253 /* All call frame descriptions (FDE's) in the GCC generated DWARF
254    refer to a single Common Information Entry (CIE), defined at
255    the beginning of the .debug_frame section.  This use of a single
256    CIE obviates the need to keep track of multiple CIE's
257    in the DWARF generation routines below.  */
258
259 typedef struct GTY(()) dw_fde_struct {
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 GTY(()) indirect_string_node {
376   const char *str;
377   unsigned int refcount;
378   enum dwarf_form form;
379   char *label;
380 };
381
382 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
383
384 static GTY(()) int dw2_string_counter;
385 static GTY(()) unsigned long dwarf2out_cfi_label_num;
386
387 /* True if the compilation unit places functions in more than one section.  */
388 static GTY(()) bool have_multiple_function_sections = false;
389
390 /* Whether the default text and cold text sections have been used at all.  */
391
392 static GTY(()) bool text_section_used = false;
393 static GTY(()) bool cold_text_section_used = false;
394
395 /* The default cold text section.  */
396 static GTY(()) section *cold_text_section;
397
398 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
399
400 /* Forward declarations for functions defined in this file.  */
401
402 static char *stripattributes (const char *);
403 static const char *dwarf_cfi_name (unsigned);
404 static dw_cfi_ref new_cfi (void);
405 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
406 static void add_fde_cfi (const char *, dw_cfi_ref);
407 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
408 static void lookup_cfa (dw_cfa_location *);
409 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
410 #ifdef DWARF2_UNWIND_INFO
411 static void initial_return_save (rtx);
412 #endif
413 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
414                                           HOST_WIDE_INT);
415 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
416 static void output_cfi_directive (dw_cfi_ref);
417 static void output_call_frame_info (int);
418 static void dwarf2out_note_section_used (void);
419 static void dwarf2out_stack_adjust (rtx, bool);
420 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
421 static void flush_queued_reg_saves (void);
422 static bool clobbers_queued_reg_save (const_rtx);
423 static void dwarf2out_frame_debug_expr (rtx, const char *);
424
425 /* Support for complex CFA locations.  */
426 static void output_cfa_loc (dw_cfi_ref);
427 static void output_cfa_loc_raw (dw_cfi_ref);
428 static void get_cfa_from_loc_descr (dw_cfa_location *,
429                                     struct dw_loc_descr_struct *);
430 static struct dw_loc_descr_struct *build_cfa_loc
431   (dw_cfa_location *, HOST_WIDE_INT);
432 static struct dw_loc_descr_struct *build_cfa_aligned_loc
433   (HOST_WIDE_INT, HOST_WIDE_INT);
434 static void def_cfa_1 (const char *, dw_cfa_location *);
435
436 /* How to start an assembler comment.  */
437 #ifndef ASM_COMMENT_START
438 #define ASM_COMMENT_START ";#"
439 #endif
440
441 /* Data and reference forms for relocatable data.  */
442 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
443 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
444
445 #ifndef DEBUG_FRAME_SECTION
446 #define DEBUG_FRAME_SECTION     ".debug_frame"
447 #endif
448
449 #ifndef FUNC_BEGIN_LABEL
450 #define FUNC_BEGIN_LABEL        "LFB"
451 #endif
452
453 #ifndef FUNC_END_LABEL
454 #define FUNC_END_LABEL          "LFE"
455 #endif
456
457 #ifndef FRAME_BEGIN_LABEL
458 #define FRAME_BEGIN_LABEL       "Lframe"
459 #endif
460 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
461 #define CIE_END_LABEL           "LECIE"
462 #define FDE_LABEL               "LSFDE"
463 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
464 #define FDE_END_LABEL           "LEFDE"
465 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
466 #define LINE_NUMBER_END_LABEL   "LELT"
467 #define LN_PROLOG_AS_LABEL      "LASLTP"
468 #define LN_PROLOG_END_LABEL     "LELTP"
469 #define DIE_LABEL_PREFIX        "DW"
470
471 /* The DWARF 2 CFA column which tracks the return address.  Normally this
472    is the column for PC, or the first column after all of the hard
473    registers.  */
474 #ifndef DWARF_FRAME_RETURN_COLUMN
475 #ifdef PC_REGNUM
476 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
477 #else
478 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
479 #endif
480 #endif
481
482 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
483    default, we just provide columns for all registers.  */
484 #ifndef DWARF_FRAME_REGNUM
485 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
486 #endif
487 \f
488 /* Hook used by __throw.  */
489
490 rtx
491 expand_builtin_dwarf_sp_column (void)
492 {
493   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
494   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
495 }
496
497 /* Return a pointer to a copy of the section string name S with all
498    attributes stripped off, and an asterisk prepended (for assemble_name).  */
499
500 static inline char *
501 stripattributes (const char *s)
502 {
503   char *stripped = XNEWVEC (char, strlen (s) + 2);
504   char *p = stripped;
505
506   *p++ = '*';
507
508   while (*s && *s != ',')
509     *p++ = *s++;
510
511   *p = '\0';
512   return stripped;
513 }
514
515 /* MEM is a memory reference for the register size table, each element of
516    which has mode MODE.  Initialize column C as a return address column.  */
517
518 static void
519 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
520 {
521   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
522   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
523   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
524 }
525
526 /* Generate code to initialize the register size table.  */
527
528 void
529 expand_builtin_init_dwarf_reg_sizes (tree address)
530 {
531   unsigned int i;
532   enum machine_mode mode = TYPE_MODE (char_type_node);
533   rtx addr = expand_normal (address);
534   rtx mem = gen_rtx_MEM (BLKmode, addr);
535   bool wrote_return_column = false;
536
537   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
538     {
539       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
540
541       if (rnum < DWARF_FRAME_REGISTERS)
542         {
543           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
544           enum machine_mode save_mode = reg_raw_mode[i];
545           HOST_WIDE_INT size;
546
547           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
548             save_mode = choose_hard_reg_mode (i, 1, true);
549           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
550             {
551               if (save_mode == VOIDmode)
552                 continue;
553               wrote_return_column = true;
554             }
555           size = GET_MODE_SIZE (save_mode);
556           if (offset < 0)
557             continue;
558
559           emit_move_insn (adjust_address (mem, mode, offset),
560                           gen_int_mode (size, mode));
561         }
562     }
563
564   if (!wrote_return_column)
565     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
566
567 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
568   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
569 #endif
570
571   targetm.init_dwarf_reg_sizes_extra (address);
572 }
573
574 /* Convert a DWARF call frame info. operation to its string name */
575
576 static const char *
577 dwarf_cfi_name (unsigned int cfi_opc)
578 {
579   switch (cfi_opc)
580     {
581     case DW_CFA_advance_loc:
582       return "DW_CFA_advance_loc";
583     case DW_CFA_offset:
584       return "DW_CFA_offset";
585     case DW_CFA_restore:
586       return "DW_CFA_restore";
587     case DW_CFA_nop:
588       return "DW_CFA_nop";
589     case DW_CFA_set_loc:
590       return "DW_CFA_set_loc";
591     case DW_CFA_advance_loc1:
592       return "DW_CFA_advance_loc1";
593     case DW_CFA_advance_loc2:
594       return "DW_CFA_advance_loc2";
595     case DW_CFA_advance_loc4:
596       return "DW_CFA_advance_loc4";
597     case DW_CFA_offset_extended:
598       return "DW_CFA_offset_extended";
599     case DW_CFA_restore_extended:
600       return "DW_CFA_restore_extended";
601     case DW_CFA_undefined:
602       return "DW_CFA_undefined";
603     case DW_CFA_same_value:
604       return "DW_CFA_same_value";
605     case DW_CFA_register:
606       return "DW_CFA_register";
607     case DW_CFA_remember_state:
608       return "DW_CFA_remember_state";
609     case DW_CFA_restore_state:
610       return "DW_CFA_restore_state";
611     case DW_CFA_def_cfa:
612       return "DW_CFA_def_cfa";
613     case DW_CFA_def_cfa_register:
614       return "DW_CFA_def_cfa_register";
615     case DW_CFA_def_cfa_offset:
616       return "DW_CFA_def_cfa_offset";
617
618     /* DWARF 3 */
619     case DW_CFA_def_cfa_expression:
620       return "DW_CFA_def_cfa_expression";
621     case DW_CFA_expression:
622       return "DW_CFA_expression";
623     case DW_CFA_offset_extended_sf:
624       return "DW_CFA_offset_extended_sf";
625     case DW_CFA_def_cfa_sf:
626       return "DW_CFA_def_cfa_sf";
627     case DW_CFA_def_cfa_offset_sf:
628       return "DW_CFA_def_cfa_offset_sf";
629
630     /* SGI/MIPS specific */
631     case DW_CFA_MIPS_advance_loc8:
632       return "DW_CFA_MIPS_advance_loc8";
633
634     /* GNU extensions */
635     case DW_CFA_GNU_window_save:
636       return "DW_CFA_GNU_window_save";
637     case DW_CFA_GNU_args_size:
638       return "DW_CFA_GNU_args_size";
639     case DW_CFA_GNU_negative_offset_extended:
640       return "DW_CFA_GNU_negative_offset_extended";
641
642     default:
643       return "DW_CFA_<unknown>";
644     }
645 }
646
647 /* Return a pointer to a newly allocated Call Frame Instruction.  */
648
649 static inline dw_cfi_ref
650 new_cfi (void)
651 {
652   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
653
654   cfi->dw_cfi_next = NULL;
655   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
656   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
657
658   return cfi;
659 }
660
661 /* Add a Call Frame Instruction to list of instructions.  */
662
663 static inline void
664 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
665 {
666   dw_cfi_ref *p;
667   dw_fde_ref fde = current_fde ();
668
669   /* When DRAP is used, CFA is defined with an expression.  Redefine
670      CFA may lead to a different CFA value.   */
671   if (fde && fde->drap_reg != INVALID_REGNUM)
672     switch (cfi->dw_cfi_opc)
673       {
674         case DW_CFA_def_cfa_register:
675         case DW_CFA_def_cfa_offset:
676         case DW_CFA_def_cfa_offset_sf:
677         case DW_CFA_def_cfa:
678         case DW_CFA_def_cfa_sf:
679           gcc_unreachable ();
680
681         default:
682           break;
683       }
684
685   /* Find the end of the chain.  */
686   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
687     ;
688
689   *p = cfi;
690 }
691
692 /* Generate a new label for the CFI info to refer to.  */
693
694 char *
695 dwarf2out_cfi_label (void)
696 {
697   static char label[20];
698
699   if (dwarf2out_do_cfi_asm ())
700     {
701       /* In this case, we will be emitting the asm directive instead of
702          the label, so just return a placeholder to keep the rest of the
703          interfaces happy.  */
704       strcpy (label, "<do not output>");
705     }
706   else
707     {
708       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
709       ASM_OUTPUT_LABEL (asm_out_file, label);
710     }
711
712   return label;
713 }
714
715 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
716    or to the CIE if LABEL is NULL.  */
717
718 static void
719 add_fde_cfi (const char *label, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *list_head = &cie_cfi_head;
722
723   if (dwarf2out_do_cfi_asm ())
724     {
725       if (label)
726         {
727           output_cfi_directive (cfi);
728
729           /* We still have to add the cfi to the list so that
730              lookup_cfa works later on.  */
731           list_head = &current_fde ()->dw_fde_cfi;
732         }
733       /* ??? If this is a CFI for the CIE, we don't emit.  This
734          assumes that the standard CIE contents that the assembler
735          uses matches the standard CIE contents that the compiler
736          uses.  This is probably a bad assumption.  I'm not quite
737          sure how to address this for now.  */
738     }
739   else if (label)
740     {
741       dw_fde_ref fde = current_fde ();
742
743       gcc_assert (fde != NULL);
744
745       if (*label == 0)
746         label = dwarf2out_cfi_label ();
747
748       if (fde->dw_fde_current_label == NULL
749           || strcmp (label, fde->dw_fde_current_label) != 0)
750         {
751           dw_cfi_ref xcfi;
752
753           label = xstrdup (label);
754
755           /* Set the location counter to the new label.  */
756           xcfi = new_cfi ();
757           /* If we have a current label, advance from there, otherwise
758              set the location directly using set_loc.  */
759           xcfi->dw_cfi_opc = fde->dw_fde_current_label
760                              ? DW_CFA_advance_loc4
761                              : DW_CFA_set_loc;
762           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
763           add_cfi (&fde->dw_fde_cfi, xcfi);
764
765           fde->dw_fde_current_label = label;
766         }
767
768       list_head = &fde->dw_fde_cfi;
769     }
770
771   add_cfi (list_head, cfi);
772 }
773
774 /* Subroutine of lookup_cfa.  */
775
776 static void
777 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
778 {
779   switch (cfi->dw_cfi_opc)
780     {
781     case DW_CFA_def_cfa_offset:
782     case DW_CFA_def_cfa_offset_sf:
783       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
784       break;
785     case DW_CFA_def_cfa_register:
786       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
787       break;
788     case DW_CFA_def_cfa:
789     case DW_CFA_def_cfa_sf:
790       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
791       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
792       break;
793     case DW_CFA_def_cfa_expression:
794       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
795       break;
796     default:
797       break;
798     }
799 }
800
801 /* Find the previous value for the CFA.  */
802
803 static void
804 lookup_cfa (dw_cfa_location *loc)
805 {
806   dw_cfi_ref cfi;
807   dw_fde_ref fde;
808
809   loc->reg = INVALID_REGNUM;
810   loc->offset = 0;
811   loc->indirect = 0;
812   loc->base_offset = 0;
813
814   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
815     lookup_cfa_1 (cfi, loc);
816
817   fde = current_fde ();
818   if (fde)
819     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
820       lookup_cfa_1 (cfi, loc);
821 }
822
823 /* The current rule for calculating the DWARF2 canonical frame address.  */
824 static dw_cfa_location cfa;
825
826 /* The register used for saving registers to the stack, and its offset
827    from the CFA.  */
828 static dw_cfa_location cfa_store;
829
830 /* The running total of the size of arguments pushed onto the stack.  */
831 static HOST_WIDE_INT args_size;
832
833 /* The last args_size we actually output.  */
834 static HOST_WIDE_INT old_args_size;
835
836 /* Entry point to update the canonical frame address (CFA).
837    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
838    calculated from REG+OFFSET.  */
839
840 void
841 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
842 {
843   dw_cfa_location loc;
844   loc.indirect = 0;
845   loc.base_offset = 0;
846   loc.reg = reg;
847   loc.offset = offset;
848   def_cfa_1 (label, &loc);
849 }
850
851 /* Determine if two dw_cfa_location structures define the same data.  */
852
853 static bool
854 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
855 {
856   return (loc1->reg == loc2->reg
857           && loc1->offset == loc2->offset
858           && loc1->indirect == loc2->indirect
859           && (loc1->indirect == 0
860               || loc1->base_offset == loc2->base_offset));
861 }
862
863 /* This routine does the actual work.  The CFA is now calculated from
864    the dw_cfa_location structure.  */
865
866 static void
867 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
868 {
869   dw_cfi_ref cfi;
870   dw_cfa_location old_cfa, loc;
871
872   cfa = *loc_p;
873   loc = *loc_p;
874
875   if (cfa_store.reg == loc.reg && loc.indirect == 0)
876     cfa_store.offset = loc.offset;
877
878   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
879   lookup_cfa (&old_cfa);
880
881   /* If nothing changed, no need to issue any call frame instructions.  */
882   if (cfa_equal_p (&loc, &old_cfa))
883     return;
884
885   cfi = new_cfi ();
886
887   if (loc.reg == old_cfa.reg && !loc.indirect)
888     {
889       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
890          the CFA register did not change but the offset did.  The data 
891          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
892          in the assembler via the .cfi_def_cfa_offset directive.  */
893       if (loc.offset < 0)
894         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
895       else
896         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
897       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
898     }
899
900 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
901   else if (loc.offset == old_cfa.offset
902            && old_cfa.reg != INVALID_REGNUM
903            && !loc.indirect)
904     {
905       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
906          indicating the CFA register has changed to <register> but the
907          offset has not changed.  */
908       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
909       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
910     }
911 #endif
912
913   else if (loc.indirect == 0)
914     {
915       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
916          indicating the CFA register has changed to <register> with
917          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
918          happens in output_cfi, or in the assembler via the .cfi_def_cfa
919          directive.  */
920       if (loc.offset < 0)
921         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
922       else
923         cfi->dw_cfi_opc = DW_CFA_def_cfa;
924       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
925       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
926     }
927   else
928     {
929       /* Construct a DW_CFA_def_cfa_expression instruction to
930          calculate the CFA using a full location expression since no
931          register-offset pair is available.  */
932       struct dw_loc_descr_struct *loc_list;
933
934       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
935       loc_list = build_cfa_loc (&loc, 0);
936       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
937     }
938
939   add_fde_cfi (label, cfi);
940 }
941
942 /* Add the CFI for saving a register.  REG is the CFA column number.
943    LABEL is passed to add_fde_cfi.
944    If SREG is -1, the register is saved at OFFSET from the CFA;
945    otherwise it is saved in SREG.  */
946
947 static void
948 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
949 {
950   dw_cfi_ref cfi = new_cfi ();
951   dw_fde_ref fde = current_fde ();
952
953   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
954
955   /* When stack is aligned, store REG using DW_CFA_expression with
956      FP.  */
957   if (fde
958       && fde->stack_realign
959       && sreg == INVALID_REGNUM)
960     {
961       cfi->dw_cfi_opc = DW_CFA_expression;
962       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
963       cfi->dw_cfi_oprnd1.dw_cfi_loc
964         = build_cfa_aligned_loc (offset, fde->stack_realignment);
965     }
966   else if (sreg == INVALID_REGNUM)
967     {
968       if (offset < 0)
969         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
970       else if (reg & ~0x3f)
971         cfi->dw_cfi_opc = DW_CFA_offset_extended;
972       else
973         cfi->dw_cfi_opc = DW_CFA_offset;
974       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
975     }
976   else if (sreg == reg)
977     cfi->dw_cfi_opc = DW_CFA_same_value;
978   else
979     {
980       cfi->dw_cfi_opc = DW_CFA_register;
981       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
982     }
983
984   add_fde_cfi (label, cfi);
985 }
986
987 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
988    This CFI tells the unwinder that it needs to restore the window registers
989    from the previous frame's window save area.
990
991    ??? Perhaps we should note in the CIE where windows are saved (instead of
992    assuming 0(cfa)) and what registers are in the window.  */
993
994 void
995 dwarf2out_window_save (const char *label)
996 {
997   dw_cfi_ref cfi = new_cfi ();
998
999   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1000   add_fde_cfi (label, cfi);
1001 }
1002
1003 /* Add a CFI to update the running total of the size of arguments
1004    pushed onto the stack.  */
1005
1006 void
1007 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1008 {
1009   dw_cfi_ref cfi;
1010
1011   if (size == old_args_size)
1012     return;
1013
1014   old_args_size = size;
1015
1016   cfi = new_cfi ();
1017   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1018   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1019   add_fde_cfi (label, cfi);
1020 }
1021
1022 /* Entry point for saving a register to the stack.  REG is the GCC register
1023    number.  LABEL and OFFSET are passed to reg_save.  */
1024
1025 void
1026 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1027 {
1028   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1029 }
1030
1031 /* Entry point for saving the return address in the stack.
1032    LABEL and OFFSET are passed to reg_save.  */
1033
1034 void
1035 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1036 {
1037   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1038 }
1039
1040 /* Entry point for saving the return address in a register.
1041    LABEL and SREG are passed to reg_save.  */
1042
1043 void
1044 dwarf2out_return_reg (const char *label, unsigned int sreg)
1045 {
1046   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1047 }
1048
1049 #ifdef DWARF2_UNWIND_INFO
1050 /* Record the initial position of the return address.  RTL is
1051    INCOMING_RETURN_ADDR_RTX.  */
1052
1053 static void
1054 initial_return_save (rtx rtl)
1055 {
1056   unsigned int reg = INVALID_REGNUM;
1057   HOST_WIDE_INT offset = 0;
1058
1059   switch (GET_CODE (rtl))
1060     {
1061     case REG:
1062       /* RA is in a register.  */
1063       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1064       break;
1065
1066     case MEM:
1067       /* RA is on the stack.  */
1068       rtl = XEXP (rtl, 0);
1069       switch (GET_CODE (rtl))
1070         {
1071         case REG:
1072           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1073           offset = 0;
1074           break;
1075
1076         case PLUS:
1077           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1078           offset = INTVAL (XEXP (rtl, 1));
1079           break;
1080
1081         case MINUS:
1082           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1083           offset = -INTVAL (XEXP (rtl, 1));
1084           break;
1085
1086         default:
1087           gcc_unreachable ();
1088         }
1089
1090       break;
1091
1092     case PLUS:
1093       /* The return address is at some offset from any value we can
1094          actually load.  For instance, on the SPARC it is in %i7+8. Just
1095          ignore the offset for now; it doesn't matter for unwinding frames.  */
1096       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1097       initial_return_save (XEXP (rtl, 0));
1098       return;
1099
1100     default:
1101       gcc_unreachable ();
1102     }
1103
1104   if (reg != DWARF_FRAME_RETURN_COLUMN)
1105     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1106 }
1107 #endif
1108
1109 /* Given a SET, calculate the amount of stack adjustment it
1110    contains.  */
1111
1112 static HOST_WIDE_INT
1113 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1114                      HOST_WIDE_INT cur_offset)
1115 {
1116   const_rtx src = SET_SRC (pattern);
1117   const_rtx dest = SET_DEST (pattern);
1118   HOST_WIDE_INT offset = 0;
1119   enum rtx_code code;
1120
1121   if (dest == stack_pointer_rtx)
1122     {
1123       code = GET_CODE (src);
1124
1125       /* Assume (set (reg sp) (reg whatever)) sets args_size
1126          level to 0.  */
1127       if (code == REG && src != stack_pointer_rtx)
1128         {
1129           offset = -cur_args_size;
1130 #ifndef STACK_GROWS_DOWNWARD
1131           offset = -offset;
1132 #endif
1133           return offset - cur_offset;
1134         }
1135
1136       if (! (code == PLUS || code == MINUS)
1137           || XEXP (src, 0) != stack_pointer_rtx
1138           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1139         return 0;
1140
1141       /* (set (reg sp) (plus (reg sp) (const_int))) */
1142       offset = INTVAL (XEXP (src, 1));
1143       if (code == PLUS)
1144         offset = -offset;
1145       return offset;
1146     }
1147
1148   if (MEM_P (src) && !MEM_P (dest))
1149     dest = src;
1150   if (MEM_P (dest))
1151     {
1152       /* (set (mem (pre_dec (reg sp))) (foo)) */
1153       src = XEXP (dest, 0);
1154       code = GET_CODE (src);
1155
1156       switch (code)
1157         {
1158         case PRE_MODIFY:
1159         case POST_MODIFY:
1160           if (XEXP (src, 0) == stack_pointer_rtx)
1161             {
1162               rtx val = XEXP (XEXP (src, 1), 1);
1163               /* We handle only adjustments by constant amount.  */
1164               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1165                           && GET_CODE (val) == CONST_INT);
1166               offset = -INTVAL (val);
1167               break;
1168             }
1169           return 0;
1170
1171         case PRE_DEC:
1172         case POST_DEC:
1173           if (XEXP (src, 0) == stack_pointer_rtx)
1174             {
1175               offset = GET_MODE_SIZE (GET_MODE (dest));
1176               break;
1177             }
1178           return 0;
1179
1180         case PRE_INC:
1181         case POST_INC:
1182           if (XEXP (src, 0) == stack_pointer_rtx)
1183             {
1184               offset = -GET_MODE_SIZE (GET_MODE (dest));
1185               break;
1186             }
1187           return 0;
1188
1189         default:
1190           return 0;
1191         }
1192     }
1193   else
1194     return 0;
1195
1196   return offset;
1197 }
1198
1199 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1200    indexed by INSN_UID.  */
1201
1202 static HOST_WIDE_INT *barrier_args_size;
1203
1204 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1205
1206 static HOST_WIDE_INT
1207 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1208                              VEC (rtx, heap) **next)
1209 {
1210   HOST_WIDE_INT offset = 0;
1211   int i;
1212
1213   if (! RTX_FRAME_RELATED_P (insn))
1214     {
1215       if (prologue_epilogue_contains (insn)
1216           || sibcall_epilogue_contains (insn))
1217         /* Nothing */;
1218       else if (GET_CODE (PATTERN (insn)) == SET)
1219         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1220       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1221                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1222         {
1223           /* There may be stack adjustments inside compound insns.  Search
1224              for them.  */
1225           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1226             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1227               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1228                                              cur_args_size, offset);
1229         }
1230     }
1231   else
1232     {
1233       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1234
1235       if (expr)
1236         {
1237           expr = XEXP (expr, 0);
1238           if (GET_CODE (expr) == PARALLEL
1239               || GET_CODE (expr) == SEQUENCE)
1240             for (i = 1; i < XVECLEN (expr, 0); i++)
1241               {
1242                 rtx elem = XVECEXP (expr, 0, i);
1243
1244                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1245                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1246               }
1247         }
1248     }
1249
1250 #ifndef STACK_GROWS_DOWNWARD
1251   offset = -offset;
1252 #endif
1253
1254   cur_args_size += offset;
1255   if (cur_args_size < 0)
1256     cur_args_size = 0;
1257
1258   if (JUMP_P (insn))
1259     {
1260       rtx dest = JUMP_LABEL (insn);
1261
1262       if (dest)
1263         {
1264           if (barrier_args_size [INSN_UID (dest)] < 0)
1265             {
1266               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1267               VEC_safe_push (rtx, heap, *next, dest);
1268             }
1269         }
1270     }
1271
1272   return cur_args_size;
1273 }
1274
1275 /* Walk the whole function and compute args_size on BARRIERs.  */
1276
1277 static void
1278 compute_barrier_args_size (void)
1279 {
1280   int max_uid = get_max_uid (), i;
1281   rtx insn;
1282   VEC (rtx, heap) *worklist, *next, *tmp;
1283
1284   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1285   for (i = 0; i < max_uid; i++)
1286     barrier_args_size[i] = -1;
1287
1288   worklist = VEC_alloc (rtx, heap, 20);
1289   next = VEC_alloc (rtx, heap, 20);
1290   insn = get_insns ();
1291   barrier_args_size[INSN_UID (insn)] = 0;
1292   VEC_quick_push (rtx, worklist, insn);
1293   for (;;)
1294     {
1295       while (!VEC_empty (rtx, worklist))
1296         {
1297           rtx prev, body, first_insn;
1298           HOST_WIDE_INT cur_args_size;
1299
1300           first_insn = insn = VEC_pop (rtx, worklist);
1301           cur_args_size = barrier_args_size[INSN_UID (insn)];
1302           prev = prev_nonnote_insn (insn);
1303           if (prev && BARRIER_P (prev))
1304             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1305
1306           for (; insn; insn = NEXT_INSN (insn))
1307             {
1308               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1309                 continue;
1310               if (BARRIER_P (insn))
1311                 break;
1312
1313               if (LABEL_P (insn))
1314                 {
1315                   if (insn == first_insn)
1316                     continue;
1317                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1318                     {
1319                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1320                       continue;
1321                     }
1322                   else
1323                     {
1324                       /* The insns starting with this label have been
1325                          already scanned or are in the worklist.  */
1326                       break;
1327                     }
1328                 }
1329
1330               body = PATTERN (insn);
1331               if (GET_CODE (body) == SEQUENCE)
1332                 {
1333                   HOST_WIDE_INT dest_args_size = cur_args_size;
1334                   for (i = 1; i < XVECLEN (body, 0); i++)
1335                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1336                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1337                       dest_args_size
1338                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1339                                                        dest_args_size, &next);
1340                     else
1341                       cur_args_size
1342                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1343                                                        cur_args_size, &next);
1344
1345                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1346                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1347                                                  dest_args_size, &next);
1348                   else
1349                     cur_args_size
1350                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1351                                                      cur_args_size, &next);
1352                 }
1353               else
1354                 cur_args_size
1355                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1356             }
1357         }
1358
1359       if (VEC_empty (rtx, next))
1360         break;
1361
1362       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1363       tmp = next;
1364       next = worklist;
1365       worklist = tmp;
1366       VEC_truncate (rtx, next, 0);
1367     }
1368
1369   VEC_free (rtx, heap, worklist);
1370   VEC_free (rtx, heap, next);
1371 }
1372
1373
1374 /* Check INSN to see if it looks like a push or a stack adjustment, and
1375    make a note of it if it does.  EH uses this information to find out how
1376    much extra space it needs to pop off the stack.  */
1377
1378 static void
1379 dwarf2out_stack_adjust (rtx insn, bool after_p)
1380 {
1381   HOST_WIDE_INT offset;
1382   const char *label;
1383   int i;
1384
1385   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1386      with this function.  Proper support would require all frame-related
1387      insns to be marked, and to be able to handle saving state around
1388      epilogues textually in the middle of the function.  */
1389   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1390     return;
1391
1392   /* If INSN is an instruction from target of an annulled branch, the
1393      effects are for the target only and so current argument size
1394      shouldn't change at all.  */
1395   if (final_sequence
1396       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1397       && INSN_FROM_TARGET_P (insn))
1398     return;
1399
1400   /* If only calls can throw, and we have a frame pointer,
1401      save up adjustments until we see the CALL_INSN.  */
1402   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1403     {
1404       if (CALL_P (insn) && !after_p)
1405         {
1406           /* Extract the size of the args from the CALL rtx itself.  */
1407           insn = PATTERN (insn);
1408           if (GET_CODE (insn) == PARALLEL)
1409             insn = XVECEXP (insn, 0, 0);
1410           if (GET_CODE (insn) == SET)
1411             insn = SET_SRC (insn);
1412           gcc_assert (GET_CODE (insn) == CALL);
1413           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1414         }
1415       return;
1416     }
1417
1418   if (CALL_P (insn) && !after_p)
1419     {
1420       if (!flag_asynchronous_unwind_tables)
1421         dwarf2out_args_size ("", args_size);
1422       return;
1423     }
1424   else if (BARRIER_P (insn))
1425     {
1426       /* Don't call compute_barrier_args_size () if the only
1427          BARRIER is at the end of function.  */
1428       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1429         compute_barrier_args_size ();
1430       if (barrier_args_size == NULL)
1431         offset = 0;
1432       else
1433         {
1434           offset = barrier_args_size[INSN_UID (insn)];
1435           if (offset < 0)
1436             offset = 0;
1437         }
1438
1439       offset -= args_size;
1440 #ifndef STACK_GROWS_DOWNWARD
1441       offset = -offset;
1442 #endif
1443     }
1444   else if (GET_CODE (PATTERN (insn)) == SET)
1445     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1446   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1447            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1448     {
1449       /* There may be stack adjustments inside compound insns.  Search
1450          for them.  */
1451       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1452         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1453           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1454                                          args_size, offset);
1455     }
1456   else
1457     return;
1458
1459   if (offset == 0)
1460     return;
1461
1462   label = dwarf2out_cfi_label ();
1463   dwarf2out_args_size_adjust (offset, label);
1464 }
1465
1466 /* Adjust args_size based on stack adjustment OFFSET.  */
1467
1468 static void
1469 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1470 {
1471   if (cfa.reg == STACK_POINTER_REGNUM)
1472     cfa.offset += offset;
1473
1474   if (cfa_store.reg == STACK_POINTER_REGNUM)
1475     cfa_store.offset += offset;
1476
1477 #ifndef STACK_GROWS_DOWNWARD
1478   offset = -offset;
1479 #endif
1480
1481   args_size += offset;
1482   if (args_size < 0)
1483     args_size = 0;
1484
1485   def_cfa_1 (label, &cfa);
1486   if (flag_asynchronous_unwind_tables)
1487     dwarf2out_args_size (label, args_size);
1488 }
1489
1490 #endif
1491
1492 /* We delay emitting a register save until either (a) we reach the end
1493    of the prologue or (b) the register is clobbered.  This clusters
1494    register saves so that there are fewer pc advances.  */
1495
1496 struct GTY(()) queued_reg_save {
1497   struct queued_reg_save *next;
1498   rtx reg;
1499   HOST_WIDE_INT cfa_offset;
1500   rtx saved_reg;
1501 };
1502
1503 static GTY(()) struct queued_reg_save *queued_reg_saves;
1504
1505 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1506 struct GTY(()) reg_saved_in_data {
1507   rtx orig_reg;
1508   rtx saved_in_reg;
1509 };
1510
1511 /* A list of registers saved in other registers.
1512    The list intentionally has a small maximum capacity of 4; if your
1513    port needs more than that, you might consider implementing a
1514    more efficient data structure.  */
1515 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1516 static GTY(()) size_t num_regs_saved_in_regs;
1517
1518 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1519 static const char *last_reg_save_label;
1520
1521 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1522    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1523
1524 static void
1525 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1526 {
1527   struct queued_reg_save *q;
1528
1529   /* Duplicates waste space, but it's also necessary to remove them
1530      for correctness, since the queue gets output in reverse
1531      order.  */
1532   for (q = queued_reg_saves; q != NULL; q = q->next)
1533     if (REGNO (q->reg) == REGNO (reg))
1534       break;
1535
1536   if (q == NULL)
1537     {
1538       q = GGC_NEW (struct queued_reg_save);
1539       q->next = queued_reg_saves;
1540       queued_reg_saves = q;
1541     }
1542
1543   q->reg = reg;
1544   q->cfa_offset = offset;
1545   q->saved_reg = sreg;
1546
1547   last_reg_save_label = label;
1548 }
1549
1550 /* Output all the entries in QUEUED_REG_SAVES.  */
1551
1552 static void
1553 flush_queued_reg_saves (void)
1554 {
1555   struct queued_reg_save *q;
1556
1557   for (q = queued_reg_saves; q; q = q->next)
1558     {
1559       size_t i;
1560       unsigned int reg, sreg;
1561
1562       for (i = 0; i < num_regs_saved_in_regs; i++)
1563         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1564           break;
1565       if (q->saved_reg && i == num_regs_saved_in_regs)
1566         {
1567           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1568           num_regs_saved_in_regs++;
1569         }
1570       if (i != num_regs_saved_in_regs)
1571         {
1572           regs_saved_in_regs[i].orig_reg = q->reg;
1573           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1574         }
1575
1576       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1577       if (q->saved_reg)
1578         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1579       else
1580         sreg = INVALID_REGNUM;
1581       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1582     }
1583
1584   queued_reg_saves = NULL;
1585   last_reg_save_label = NULL;
1586 }
1587
1588 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1589    location for?  Or, does it clobber a register which we've previously
1590    said that some other register is saved in, and for which we now
1591    have a new location for?  */
1592
1593 static bool
1594 clobbers_queued_reg_save (const_rtx insn)
1595 {
1596   struct queued_reg_save *q;
1597
1598   for (q = queued_reg_saves; q; q = q->next)
1599     {
1600       size_t i;
1601       if (modified_in_p (q->reg, insn))
1602         return true;
1603       for (i = 0; i < num_regs_saved_in_regs; i++)
1604         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1605             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1606           return true;
1607     }
1608
1609   return false;
1610 }
1611
1612 /* Entry point for saving the first register into the second.  */
1613
1614 void
1615 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1616 {
1617   size_t i;
1618   unsigned int regno, sregno;
1619
1620   for (i = 0; i < num_regs_saved_in_regs; i++)
1621     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1622       break;
1623   if (i == num_regs_saved_in_regs)
1624     {
1625       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1626       num_regs_saved_in_regs++;
1627     }
1628   regs_saved_in_regs[i].orig_reg = reg;
1629   regs_saved_in_regs[i].saved_in_reg = sreg;
1630
1631   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1632   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1633   reg_save (label, regno, sregno, 0);
1634 }
1635
1636 /* What register, if any, is currently saved in REG?  */
1637
1638 static rtx
1639 reg_saved_in (rtx reg)
1640 {
1641   unsigned int regn = REGNO (reg);
1642   size_t i;
1643   struct queued_reg_save *q;
1644
1645   for (q = queued_reg_saves; q; q = q->next)
1646     if (q->saved_reg && regn == REGNO (q->saved_reg))
1647       return q->reg;
1648
1649   for (i = 0; i < num_regs_saved_in_regs; i++)
1650     if (regs_saved_in_regs[i].saved_in_reg
1651         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1652       return regs_saved_in_regs[i].orig_reg;
1653
1654   return NULL_RTX;
1655 }
1656
1657
1658 /* A temporary register holding an integral value used in adjusting SP
1659    or setting up the store_reg.  The "offset" field holds the integer
1660    value, not an offset.  */
1661 static dw_cfa_location cfa_temp;
1662
1663 /* Record call frame debugging information for an expression EXPR,
1664    which either sets SP or FP (adjusting how we calculate the frame
1665    address) or saves a register to the stack or another register.
1666    LABEL indicates the address of EXPR.
1667
1668    This function encodes a state machine mapping rtxes to actions on
1669    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1670    users need not read the source code.
1671
1672   The High-Level Picture
1673
1674   Changes in the register we use to calculate the CFA: Currently we
1675   assume that if you copy the CFA register into another register, we
1676   should take the other one as the new CFA register; this seems to
1677   work pretty well.  If it's wrong for some target, it's simple
1678   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1679
1680   Changes in the register we use for saving registers to the stack:
1681   This is usually SP, but not always.  Again, we deduce that if you
1682   copy SP into another register (and SP is not the CFA register),
1683   then the new register is the one we will be using for register
1684   saves.  This also seems to work.
1685
1686   Register saves: There's not much guesswork about this one; if
1687   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1688   register save, and the register used to calculate the destination
1689   had better be the one we think we're using for this purpose.
1690   It's also assumed that a copy from a call-saved register to another
1691   register is saving that register if RTX_FRAME_RELATED_P is set on
1692   that instruction.  If the copy is from a call-saved register to
1693   the *same* register, that means that the register is now the same
1694   value as in the caller.
1695
1696   Except: If the register being saved is the CFA register, and the
1697   offset is nonzero, we are saving the CFA, so we assume we have to
1698   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1699   the intent is to save the value of SP from the previous frame.
1700
1701   In addition, if a register has previously been saved to a different
1702   register,
1703
1704   Invariants / Summaries of Rules
1705
1706   cfa          current rule for calculating the CFA.  It usually
1707                consists of a register and an offset.
1708   cfa_store    register used by prologue code to save things to the stack
1709                cfa_store.offset is the offset from the value of
1710                cfa_store.reg to the actual CFA
1711   cfa_temp     register holding an integral value.  cfa_temp.offset
1712                stores the value, which will be used to adjust the
1713                stack pointer.  cfa_temp is also used like cfa_store,
1714                to track stores to the stack via fp or a temp reg.
1715
1716   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1717                with cfa.reg as the first operand changes the cfa.reg and its
1718                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1719                cfa_temp.offset.
1720
1721   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1722                expression yielding a constant.  This sets cfa_temp.reg
1723                and cfa_temp.offset.
1724
1725   Rule 5:      Create a new register cfa_store used to save items to the
1726                stack.
1727
1728   Rules 10-14: Save a register to the stack.  Define offset as the
1729                difference of the original location and cfa_store's
1730                location (or cfa_temp's location if cfa_temp is used).
1731
1732   Rules 16-20: If AND operation happens on sp in prologue, we assume
1733                stack is realigned.  We will use a group of DW_OP_XXX
1734                expressions to represent the location of the stored
1735                register instead of CFA+offset.
1736
1737   The Rules
1738
1739   "{a,b}" indicates a choice of a xor b.
1740   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1741
1742   Rule 1:
1743   (set <reg1> <reg2>:cfa.reg)
1744   effects: cfa.reg = <reg1>
1745            cfa.offset unchanged
1746            cfa_temp.reg = <reg1>
1747            cfa_temp.offset = cfa.offset
1748
1749   Rule 2:
1750   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1751                               {<const_int>,<reg>:cfa_temp.reg}))
1752   effects: cfa.reg = sp if fp used
1753            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1754            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1755              if cfa_store.reg==sp
1756
1757   Rule 3:
1758   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1759   effects: cfa.reg = fp
1760            cfa_offset += +/- <const_int>
1761
1762   Rule 4:
1763   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1764   constraints: <reg1> != fp
1765                <reg1> != sp
1766   effects: cfa.reg = <reg1>
1767            cfa_temp.reg = <reg1>
1768            cfa_temp.offset = cfa.offset
1769
1770   Rule 5:
1771   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1772   constraints: <reg1> != fp
1773                <reg1> != sp
1774   effects: cfa_store.reg = <reg1>
1775            cfa_store.offset = cfa.offset - cfa_temp.offset
1776
1777   Rule 6:
1778   (set <reg> <const_int>)
1779   effects: cfa_temp.reg = <reg>
1780            cfa_temp.offset = <const_int>
1781
1782   Rule 7:
1783   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1784   effects: cfa_temp.reg = <reg1>
1785            cfa_temp.offset |= <const_int>
1786
1787   Rule 8:
1788   (set <reg> (high <exp>))
1789   effects: none
1790
1791   Rule 9:
1792   (set <reg> (lo_sum <exp> <const_int>))
1793   effects: cfa_temp.reg = <reg>
1794            cfa_temp.offset = <const_int>
1795
1796   Rule 10:
1797   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1798   effects: cfa_store.offset -= <const_int>
1799            cfa.offset = cfa_store.offset if cfa.reg == sp
1800            cfa.reg = sp
1801            cfa.base_offset = -cfa_store.offset
1802
1803   Rule 11:
1804   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1805   effects: cfa_store.offset += -/+ mode_size(mem)
1806            cfa.offset = cfa_store.offset if cfa.reg == sp
1807            cfa.reg = sp
1808            cfa.base_offset = -cfa_store.offset
1809
1810   Rule 12:
1811   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1812
1813        <reg2>)
1814   effects: cfa.reg = <reg1>
1815            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1816
1817   Rule 13:
1818   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1819   effects: cfa.reg = <reg1>
1820            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1821
1822   Rule 14:
1823   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1824   effects: cfa.reg = <reg1>
1825            cfa.base_offset = -cfa_temp.offset
1826            cfa_temp.offset -= mode_size(mem)
1827
1828   Rule 15:
1829   (set <reg> {unspec, unspec_volatile})
1830   effects: target-dependent
1831
1832   Rule 16:
1833   (set sp (and: sp <const_int>))
1834   constraints: cfa_store.reg == sp
1835   effects: current_fde.stack_realign = 1
1836            cfa_store.offset = 0
1837            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1838
1839   Rule 17:
1840   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1841   effects: cfa_store.offset += -/+ mode_size(mem)
1842
1843   Rule 18:
1844   (set (mem ({pre_inc, pre_dec} sp)) fp)
1845   constraints: fde->stack_realign == 1
1846   effects: cfa_store.offset = 0
1847            cfa.reg != HARD_FRAME_POINTER_REGNUM
1848
1849   Rule 19:
1850   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1851   constraints: fde->stack_realign == 1
1852                && cfa.offset == 0
1853                && cfa.indirect == 0
1854                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1855   effects: Use DW_CFA_def_cfa_expression to define cfa
1856            cfa.reg == fde->drap_reg
1857
1858   Rule 20:
1859   (set reg fde->drap_reg)
1860   constraints: fde->vdrap_reg == INVALID_REGNUM
1861   effects: fde->vdrap_reg = reg.
1862   (set mem fde->drap_reg)
1863   constraints: fde->drap_reg_saved == 1
1864   effects: none.  */
1865
1866 static void
1867 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1868 {
1869   rtx src, dest, span;
1870   HOST_WIDE_INT offset;
1871   dw_fde_ref fde;
1872
1873   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1874      the PARALLEL independently. The first element is always processed if
1875      it is a SET. This is for backward compatibility.   Other elements
1876      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1877      flag is set in them.  */
1878   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1879     {
1880       int par_index;
1881       int limit = XVECLEN (expr, 0);
1882       rtx elem;
1883
1884       /* PARALLELs have strict read-modify-write semantics, so we
1885          ought to evaluate every rvalue before changing any lvalue.
1886          It's cumbersome to do that in general, but there's an
1887          easy approximation that is enough for all current users:
1888          handle register saves before register assignments.  */
1889       if (GET_CODE (expr) == PARALLEL)
1890         for (par_index = 0; par_index < limit; par_index++)
1891           {
1892             elem = XVECEXP (expr, 0, par_index);
1893             if (GET_CODE (elem) == SET
1894                 && MEM_P (SET_DEST (elem))
1895                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1896               dwarf2out_frame_debug_expr (elem, label);
1897           }
1898
1899       for (par_index = 0; par_index < limit; par_index++)
1900         {
1901           elem = XVECEXP (expr, 0, par_index);
1902           if (GET_CODE (elem) == SET
1903               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1904               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1905             dwarf2out_frame_debug_expr (elem, label);
1906           else if (GET_CODE (elem) == SET
1907                    && par_index != 0
1908                    && !RTX_FRAME_RELATED_P (elem))
1909             {
1910               /* Stack adjustment combining might combine some post-prologue
1911                  stack adjustment into a prologue stack adjustment.  */
1912               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
1913
1914               if (offset != 0)
1915                 dwarf2out_args_size_adjust (offset, label);
1916             }
1917         }
1918       return;
1919     }
1920
1921   gcc_assert (GET_CODE (expr) == SET);
1922
1923   src = SET_SRC (expr);
1924   dest = SET_DEST (expr);
1925
1926   if (REG_P (src))
1927     {
1928       rtx rsi = reg_saved_in (src);
1929       if (rsi)
1930         src = rsi;
1931     }
1932
1933   fde = current_fde ();
1934
1935   if (GET_CODE (src) == REG
1936       && fde
1937       && fde->drap_reg == REGNO (src)
1938       && (fde->drap_reg_saved
1939           || GET_CODE (dest) == REG))
1940     {
1941       /* Rule 20 */
1942       /* If we are saving dynamic realign argument pointer to a
1943          register, the destination is virtual dynamic realign
1944          argument pointer.  It may be used to access argument.  */
1945       if (GET_CODE (dest) == REG)
1946         {
1947           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1948           fde->vdrap_reg = REGNO (dest);
1949         }
1950       return;
1951     }
1952
1953   switch (GET_CODE (dest))
1954     {
1955     case REG:
1956       switch (GET_CODE (src))
1957         {
1958           /* Setting FP from SP.  */
1959         case REG:
1960           if (cfa.reg == (unsigned) REGNO (src))
1961             {
1962               /* Rule 1 */
1963               /* Update the CFA rule wrt SP or FP.  Make sure src is
1964                  relative to the current CFA register.
1965
1966                  We used to require that dest be either SP or FP, but the
1967                  ARM copies SP to a temporary register, and from there to
1968                  FP.  So we just rely on the backends to only set
1969                  RTX_FRAME_RELATED_P on appropriate insns.  */
1970               cfa.reg = REGNO (dest);
1971               cfa_temp.reg = cfa.reg;
1972               cfa_temp.offset = cfa.offset;
1973             }
1974           else
1975             {
1976               /* Saving a register in a register.  */
1977               gcc_assert (!fixed_regs [REGNO (dest)]
1978                           /* For the SPARC and its register window.  */
1979                           || (DWARF_FRAME_REGNUM (REGNO (src))
1980                               == DWARF_FRAME_RETURN_COLUMN));
1981
1982               /* After stack is aligned, we can only save SP in FP
1983                  if drap register is used.  In this case, we have
1984                  to restore stack pointer with the CFA value and we
1985                  don't generate this DWARF information.  */
1986               if (fde
1987                   && fde->stack_realign
1988                   && REGNO (src) == STACK_POINTER_REGNUM)
1989                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1990                             && fde->drap_reg != INVALID_REGNUM
1991                             && cfa.reg != REGNO (src));
1992               else
1993                 queue_reg_save (label, src, dest, 0);
1994             }
1995           break;
1996
1997         case PLUS:
1998         case MINUS:
1999         case LO_SUM:
2000           if (dest == stack_pointer_rtx)
2001             {
2002               /* Rule 2 */
2003               /* Adjusting SP.  */
2004               switch (GET_CODE (XEXP (src, 1)))
2005                 {
2006                 case CONST_INT:
2007                   offset = INTVAL (XEXP (src, 1));
2008                   break;
2009                 case REG:
2010                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2011                               == cfa_temp.reg);
2012                   offset = cfa_temp.offset;
2013                   break;
2014                 default:
2015                   gcc_unreachable ();
2016                 }
2017
2018               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2019                 {
2020                   /* Restoring SP from FP in the epilogue.  */
2021                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2022                   cfa.reg = STACK_POINTER_REGNUM;
2023                 }
2024               else if (GET_CODE (src) == LO_SUM)
2025                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2026                 ;
2027               else
2028                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2029
2030               if (GET_CODE (src) != MINUS)
2031                 offset = -offset;
2032               if (cfa.reg == STACK_POINTER_REGNUM)
2033                 cfa.offset += offset;
2034               if (cfa_store.reg == STACK_POINTER_REGNUM)
2035                 cfa_store.offset += offset;
2036             }
2037           else if (dest == hard_frame_pointer_rtx)
2038             {
2039               /* Rule 3 */
2040               /* Either setting the FP from an offset of the SP,
2041                  or adjusting the FP */
2042               gcc_assert (frame_pointer_needed);
2043
2044               gcc_assert (REG_P (XEXP (src, 0))
2045                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2046                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2047               offset = INTVAL (XEXP (src, 1));
2048               if (GET_CODE (src) != MINUS)
2049                 offset = -offset;
2050               cfa.offset += offset;
2051               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2052             }
2053           else
2054             {
2055               gcc_assert (GET_CODE (src) != MINUS);
2056
2057               /* Rule 4 */
2058               if (REG_P (XEXP (src, 0))
2059                   && REGNO (XEXP (src, 0)) == cfa.reg
2060                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2061                 {
2062                   /* Setting a temporary CFA register that will be copied
2063                      into the FP later on.  */
2064                   offset = - INTVAL (XEXP (src, 1));
2065                   cfa.offset += offset;
2066                   cfa.reg = REGNO (dest);
2067                   /* Or used to save regs to the stack.  */
2068                   cfa_temp.reg = cfa.reg;
2069                   cfa_temp.offset = cfa.offset;
2070                 }
2071
2072               /* Rule 5 */
2073               else if (REG_P (XEXP (src, 0))
2074                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2075                        && XEXP (src, 1) == stack_pointer_rtx)
2076                 {
2077                   /* Setting a scratch register that we will use instead
2078                      of SP for saving registers to the stack.  */
2079                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2080                   cfa_store.reg = REGNO (dest);
2081                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2082                 }
2083
2084               /* Rule 9 */
2085               else if (GET_CODE (src) == LO_SUM
2086                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2087                 {
2088                   cfa_temp.reg = REGNO (dest);
2089                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2090                 }
2091               else
2092                 gcc_unreachable ();
2093             }
2094           break;
2095
2096           /* Rule 6 */
2097         case CONST_INT:
2098           cfa_temp.reg = REGNO (dest);
2099           cfa_temp.offset = INTVAL (src);
2100           break;
2101
2102           /* Rule 7 */
2103         case IOR:
2104           gcc_assert (REG_P (XEXP (src, 0))
2105                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2106                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2107
2108           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2109             cfa_temp.reg = REGNO (dest);
2110           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2111           break;
2112
2113           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2114              which will fill in all of the bits.  */
2115           /* Rule 8 */
2116         case HIGH:
2117           break;
2118
2119           /* Rule 15 */
2120         case UNSPEC:
2121         case UNSPEC_VOLATILE:
2122           gcc_assert (targetm.dwarf_handle_frame_unspec);
2123           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2124           return;
2125
2126           /* Rule 16 */
2127         case AND:
2128           /* If this AND operation happens on stack pointer in prologue,
2129              we assume the stack is realigned and we extract the
2130              alignment.  */
2131           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2132             {
2133               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2134               fde->stack_realign = 1;
2135               fde->stack_realignment = INTVAL (XEXP (src, 1));
2136               cfa_store.offset = 0;
2137
2138               if (cfa.reg != STACK_POINTER_REGNUM
2139                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2140                 fde->drap_reg = cfa.reg;
2141             }
2142           return;
2143
2144         default:
2145           gcc_unreachable ();
2146         }
2147
2148       def_cfa_1 (label, &cfa);
2149       break;
2150
2151     case MEM:
2152
2153       /* Saving a register to the stack.  Make sure dest is relative to the
2154          CFA register.  */
2155       switch (GET_CODE (XEXP (dest, 0)))
2156         {
2157           /* Rule 10 */
2158           /* With a push.  */
2159         case PRE_MODIFY:
2160           /* We can't handle variable size modifications.  */
2161           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2162                       == CONST_INT);
2163           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2164
2165           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2166                       && cfa_store.reg == STACK_POINTER_REGNUM);
2167
2168           cfa_store.offset += offset;
2169           if (cfa.reg == STACK_POINTER_REGNUM)
2170             cfa.offset = cfa_store.offset;
2171
2172           offset = -cfa_store.offset;
2173           break;
2174
2175           /* Rule 11 */
2176         case PRE_INC:
2177         case PRE_DEC:
2178           offset = GET_MODE_SIZE (GET_MODE (dest));
2179           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2180             offset = -offset;
2181
2182           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2183                        == STACK_POINTER_REGNUM)
2184                       && cfa_store.reg == STACK_POINTER_REGNUM);
2185
2186           cfa_store.offset += offset;
2187
2188           /* Rule 18: If stack is aligned, we will use FP as a
2189              reference to represent the address of the stored
2190              regiser.  */
2191           if (fde
2192               && fde->stack_realign
2193               && src == hard_frame_pointer_rtx)
2194             {
2195               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2196               cfa_store.offset = 0;
2197             }
2198
2199           if (cfa.reg == STACK_POINTER_REGNUM)
2200             cfa.offset = cfa_store.offset;
2201
2202           offset = -cfa_store.offset;
2203           break;
2204
2205           /* Rule 12 */
2206           /* With an offset.  */
2207         case PLUS:
2208         case MINUS:
2209         case LO_SUM:
2210           {
2211             int regno;
2212
2213             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2214                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2215             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2216             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2217               offset = -offset;
2218
2219             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2220
2221             if (cfa_store.reg == (unsigned) regno)
2222               offset -= cfa_store.offset;
2223             else
2224               {
2225                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2226                 offset -= cfa_temp.offset;
2227               }
2228           }
2229           break;
2230
2231           /* Rule 13 */
2232           /* Without an offset.  */
2233         case REG:
2234           {
2235             int regno = REGNO (XEXP (dest, 0));
2236
2237             if (cfa_store.reg == (unsigned) regno)
2238               offset = -cfa_store.offset;
2239             else
2240               {
2241                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2242                 offset = -cfa_temp.offset;
2243               }
2244           }
2245           break;
2246
2247           /* Rule 14 */
2248         case POST_INC:
2249           gcc_assert (cfa_temp.reg
2250                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2251           offset = -cfa_temp.offset;
2252           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2253           break;
2254
2255         default:
2256           gcc_unreachable ();
2257         }
2258
2259         /* Rule 17 */
2260         /* If the source operand of this MEM operation is not a
2261            register, basically the source is return address.  Here
2262            we only care how much stack grew and we don't save it.  */
2263       if (!REG_P (src))
2264         break;
2265
2266       if (REGNO (src) != STACK_POINTER_REGNUM
2267           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2268           && (unsigned) REGNO (src) == cfa.reg)
2269         {
2270           /* We're storing the current CFA reg into the stack.  */
2271
2272           if (cfa.offset == 0)
2273             {
2274               /* Rule 19 */
2275               /* If stack is aligned, putting CFA reg into stack means
2276                  we can no longer use reg + offset to represent CFA.
2277                  Here we use DW_CFA_def_cfa_expression instead.  The
2278                  result of this expression equals to the original CFA
2279                  value.  */
2280               if (fde
2281                   && fde->stack_realign
2282                   && cfa.indirect == 0
2283                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2284                 {
2285                   dw_cfa_location cfa_exp;
2286
2287                   gcc_assert (fde->drap_reg == cfa.reg);
2288
2289                   cfa_exp.indirect = 1;
2290                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2291                   cfa_exp.base_offset = offset;
2292                   cfa_exp.offset = 0;
2293
2294                   fde->drap_reg_saved = 1;
2295
2296                   def_cfa_1 (label, &cfa_exp);
2297                   break;
2298                 }
2299
2300               /* If the source register is exactly the CFA, assume
2301                  we're saving SP like any other register; this happens
2302                  on the ARM.  */
2303               def_cfa_1 (label, &cfa);
2304               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2305               break;
2306             }
2307           else
2308             {
2309               /* Otherwise, we'll need to look in the stack to
2310                  calculate the CFA.  */
2311               rtx x = XEXP (dest, 0);
2312
2313               if (!REG_P (x))
2314                 x = XEXP (x, 0);
2315               gcc_assert (REG_P (x));
2316
2317               cfa.reg = REGNO (x);
2318               cfa.base_offset = offset;
2319               cfa.indirect = 1;
2320               def_cfa_1 (label, &cfa);
2321               break;
2322             }
2323         }
2324
2325       def_cfa_1 (label, &cfa);
2326       {
2327         span = targetm.dwarf_register_span (src);
2328
2329         if (!span)
2330           queue_reg_save (label, src, NULL_RTX, offset);
2331         else
2332           {
2333             /* We have a PARALLEL describing where the contents of SRC
2334                live.  Queue register saves for each piece of the
2335                PARALLEL.  */
2336             int par_index;
2337             int limit;
2338             HOST_WIDE_INT span_offset = offset;
2339
2340             gcc_assert (GET_CODE (span) == PARALLEL);
2341
2342             limit = XVECLEN (span, 0);
2343             for (par_index = 0; par_index < limit; par_index++)
2344               {
2345                 rtx elem = XVECEXP (span, 0, par_index);
2346
2347                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2348                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2349               }
2350           }
2351       }
2352       break;
2353
2354     default:
2355       gcc_unreachable ();
2356     }
2357 }
2358
2359 /* Record call frame debugging information for INSN, which either
2360    sets SP or FP (adjusting how we calculate the frame address) or saves a
2361    register to the stack.  If INSN is NULL_RTX, initialize our state.
2362
2363    If AFTER_P is false, we're being called before the insn is emitted,
2364    otherwise after.  Call instructions get invoked twice.  */
2365
2366 void
2367 dwarf2out_frame_debug (rtx insn, bool after_p)
2368 {
2369   const char *label;
2370   rtx src;
2371
2372   if (insn == NULL_RTX)
2373     {
2374       size_t i;
2375
2376       /* Flush any queued register saves.  */
2377       flush_queued_reg_saves ();
2378
2379       /* Set up state for generating call frame debug info.  */
2380       lookup_cfa (&cfa);
2381       gcc_assert (cfa.reg
2382                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2383
2384       cfa.reg = STACK_POINTER_REGNUM;
2385       cfa_store = cfa;
2386       cfa_temp.reg = -1;
2387       cfa_temp.offset = 0;
2388
2389       for (i = 0; i < num_regs_saved_in_regs; i++)
2390         {
2391           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2392           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2393         }
2394       num_regs_saved_in_regs = 0;
2395
2396       if (barrier_args_size)
2397         {
2398           XDELETEVEC (barrier_args_size);
2399           barrier_args_size = NULL;
2400         }
2401       return;
2402     }
2403
2404   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2405     flush_queued_reg_saves ();
2406
2407   if (! RTX_FRAME_RELATED_P (insn))
2408     {
2409       if (!ACCUMULATE_OUTGOING_ARGS)
2410         dwarf2out_stack_adjust (insn, after_p);
2411       return;
2412     }
2413
2414   label = dwarf2out_cfi_label ();
2415   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2416   if (src)
2417     insn = XEXP (src, 0);
2418   else
2419     insn = PATTERN (insn);
2420
2421   dwarf2out_frame_debug_expr (insn, label);
2422 }
2423
2424 #endif
2425
2426 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2427 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2428  (enum dwarf_call_frame_info cfi);
2429
2430 static enum dw_cfi_oprnd_type
2431 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2432 {
2433   switch (cfi)
2434     {
2435     case DW_CFA_nop:
2436     case DW_CFA_GNU_window_save:
2437       return dw_cfi_oprnd_unused;
2438
2439     case DW_CFA_set_loc:
2440     case DW_CFA_advance_loc1:
2441     case DW_CFA_advance_loc2:
2442     case DW_CFA_advance_loc4:
2443     case DW_CFA_MIPS_advance_loc8:
2444       return dw_cfi_oprnd_addr;
2445
2446     case DW_CFA_offset:
2447     case DW_CFA_offset_extended:
2448     case DW_CFA_def_cfa:
2449     case DW_CFA_offset_extended_sf:
2450     case DW_CFA_def_cfa_sf:
2451     case DW_CFA_restore_extended:
2452     case DW_CFA_undefined:
2453     case DW_CFA_same_value:
2454     case DW_CFA_def_cfa_register:
2455     case DW_CFA_register:
2456       return dw_cfi_oprnd_reg_num;
2457
2458     case DW_CFA_def_cfa_offset:
2459     case DW_CFA_GNU_args_size:
2460     case DW_CFA_def_cfa_offset_sf:
2461       return dw_cfi_oprnd_offset;
2462
2463     case DW_CFA_def_cfa_expression:
2464     case DW_CFA_expression:
2465       return dw_cfi_oprnd_loc;
2466
2467     default:
2468       gcc_unreachable ();
2469     }
2470 }
2471
2472 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2473 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2474  (enum dwarf_call_frame_info cfi);
2475
2476 static enum dw_cfi_oprnd_type
2477 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2478 {
2479   switch (cfi)
2480     {
2481     case DW_CFA_def_cfa:
2482     case DW_CFA_def_cfa_sf:
2483     case DW_CFA_offset:
2484     case DW_CFA_offset_extended_sf:
2485     case DW_CFA_offset_extended:
2486       return dw_cfi_oprnd_offset;
2487
2488     case DW_CFA_register:
2489       return dw_cfi_oprnd_reg_num;
2490
2491     default:
2492       return dw_cfi_oprnd_unused;
2493     }
2494 }
2495
2496 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2497
2498 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2499    switch to the data section instead, and write out a synthetic label
2500    for collect2.  */
2501
2502 static void
2503 switch_to_eh_frame_section (void)
2504 {
2505   tree label;
2506
2507 #ifdef EH_FRAME_SECTION_NAME
2508   if (eh_frame_section == 0)
2509     {
2510       int flags;
2511
2512       if (EH_TABLES_CAN_BE_READ_ONLY)
2513         {
2514           int fde_encoding;
2515           int per_encoding;
2516           int lsda_encoding;
2517
2518           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2519                                                        /*global=*/0);
2520           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2521                                                        /*global=*/1);
2522           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2523                                                         /*global=*/0);
2524           flags = ((! flag_pic
2525                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2526                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2527                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2528                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2529                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2530                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2531                    ? 0 : SECTION_WRITE);
2532         }
2533       else
2534         flags = SECTION_WRITE;
2535       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2536     }
2537 #endif
2538
2539   if (eh_frame_section)
2540     switch_to_section (eh_frame_section);
2541   else
2542     {
2543       /* We have no special eh_frame section.  Put the information in
2544          the data section and emit special labels to guide collect2.  */
2545       switch_to_section (data_section);
2546       label = get_file_function_name ("F");
2547       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2548       targetm.asm_out.globalize_label (asm_out_file,
2549                                        IDENTIFIER_POINTER (label));
2550       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2551     }
2552 }
2553
2554 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2555
2556 static HOST_WIDE_INT
2557 div_data_align (HOST_WIDE_INT off)
2558 {
2559   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2560   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2561   return r;
2562 }
2563
2564 /* Output a Call Frame Information opcode and its operand(s).  */
2565
2566 static void
2567 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2568 {
2569   unsigned long r;
2570   HOST_WIDE_INT off;
2571
2572   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2573     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2574                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2575                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2576                          ((unsigned HOST_WIDE_INT)
2577                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2578   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2579     {
2580       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2581       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2582                            "DW_CFA_offset, column 0x%lx", r);
2583       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2584       dw2_asm_output_data_uleb128 (off, NULL);
2585     }
2586   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2587     {
2588       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2589       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2590                            "DW_CFA_restore, column 0x%lx", r);
2591     }
2592   else
2593     {
2594       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2595                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2596
2597       switch (cfi->dw_cfi_opc)
2598         {
2599         case DW_CFA_set_loc:
2600           if (for_eh)
2601             dw2_asm_output_encoded_addr_rtx (
2602                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2603                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2604                 false, NULL);
2605           else
2606             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2607                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2608           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2609           break;
2610
2611         case DW_CFA_advance_loc1:
2612           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2613                                 fde->dw_fde_current_label, NULL);
2614           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2615           break;
2616
2617         case DW_CFA_advance_loc2:
2618           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2619                                 fde->dw_fde_current_label, NULL);
2620           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2621           break;
2622
2623         case DW_CFA_advance_loc4:
2624           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2625                                 fde->dw_fde_current_label, NULL);
2626           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2627           break;
2628
2629         case DW_CFA_MIPS_advance_loc8:
2630           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2631                                 fde->dw_fde_current_label, NULL);
2632           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2633           break;
2634
2635         case DW_CFA_offset_extended:
2636           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2637           dw2_asm_output_data_uleb128 (r, NULL);
2638           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2639           dw2_asm_output_data_uleb128 (off, NULL);
2640           break;
2641
2642         case DW_CFA_def_cfa:
2643           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2644           dw2_asm_output_data_uleb128 (r, NULL);
2645           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2646           break;
2647
2648         case DW_CFA_offset_extended_sf:
2649           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2650           dw2_asm_output_data_uleb128 (r, NULL);
2651           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2652           dw2_asm_output_data_sleb128 (off, NULL);
2653           break;
2654
2655         case DW_CFA_def_cfa_sf:
2656           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2657           dw2_asm_output_data_uleb128 (r, NULL);
2658           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2659           dw2_asm_output_data_sleb128 (off, NULL);
2660           break;
2661
2662         case DW_CFA_restore_extended:
2663         case DW_CFA_undefined:
2664         case DW_CFA_same_value:
2665         case DW_CFA_def_cfa_register:
2666           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2667           dw2_asm_output_data_uleb128 (r, NULL);
2668           break;
2669
2670         case DW_CFA_register:
2671           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2672           dw2_asm_output_data_uleb128 (r, NULL);
2673           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2674           dw2_asm_output_data_uleb128 (r, NULL);
2675           break;
2676
2677         case DW_CFA_def_cfa_offset:
2678         case DW_CFA_GNU_args_size:
2679           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2680           break;
2681
2682         case DW_CFA_def_cfa_offset_sf:
2683           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2684           dw2_asm_output_data_sleb128 (off, NULL);
2685           break;
2686
2687         case DW_CFA_GNU_window_save:
2688           break;
2689
2690         case DW_CFA_def_cfa_expression:
2691         case DW_CFA_expression:
2692           output_cfa_loc (cfi);
2693           break;
2694
2695         case DW_CFA_GNU_negative_offset_extended:
2696           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2697           gcc_unreachable ();
2698
2699         default:
2700           break;
2701         }
2702     }
2703 }
2704
2705 /* Similar, but do it via assembler directives instead.  */
2706
2707 static void
2708 output_cfi_directive (dw_cfi_ref cfi)
2709 {
2710   unsigned long r, r2;
2711
2712   switch (cfi->dw_cfi_opc)
2713     {
2714     case DW_CFA_advance_loc:
2715     case DW_CFA_advance_loc1:
2716     case DW_CFA_advance_loc2:
2717     case DW_CFA_advance_loc4:
2718     case DW_CFA_MIPS_advance_loc8:
2719     case DW_CFA_set_loc:
2720       /* Should only be created by add_fde_cfi in a code path not
2721          followed when emitting via directives.  The assembler is
2722          going to take care of this for us.  */
2723       gcc_unreachable ();
2724
2725     case DW_CFA_offset:
2726     case DW_CFA_offset_extended:
2727     case DW_CFA_offset_extended_sf:
2728       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2729       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2730                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2731       break;
2732
2733     case DW_CFA_restore:
2734     case DW_CFA_restore_extended:
2735       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2736       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2737       break;
2738
2739     case DW_CFA_undefined:
2740       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2741       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2742       break;
2743
2744     case DW_CFA_same_value:
2745       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2746       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2747       break;
2748
2749     case DW_CFA_def_cfa:
2750     case DW_CFA_def_cfa_sf:
2751       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2752       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2753                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2754       break;
2755
2756     case DW_CFA_def_cfa_register:
2757       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2758       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2759       break;
2760
2761     case DW_CFA_register:
2762       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2763       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2764       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2765       break;
2766
2767     case DW_CFA_def_cfa_offset:
2768     case DW_CFA_def_cfa_offset_sf:
2769       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2770                HOST_WIDE_INT_PRINT_DEC"\n",
2771                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2772       break;
2773
2774     case DW_CFA_GNU_args_size:
2775       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2776       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2777       if (flag_debug_asm)
2778         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2779                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2780       fputc ('\n', asm_out_file);
2781       break;
2782
2783     case DW_CFA_GNU_window_save:
2784       fprintf (asm_out_file, "\t.cfi_window_save\n");
2785       break;
2786
2787     case DW_CFA_def_cfa_expression:
2788     case DW_CFA_expression:
2789       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2790       output_cfa_loc_raw (cfi);
2791       fputc ('\n', asm_out_file);
2792       break;
2793
2794     default:
2795       gcc_unreachable ();
2796     }
2797 }
2798
2799 /* Output the call frame information used to record information
2800    that relates to calculating the frame pointer, and records the
2801    location of saved registers.  */
2802
2803 static void
2804 output_call_frame_info (int for_eh)
2805 {
2806   unsigned int i;
2807   dw_fde_ref fde;
2808   dw_cfi_ref cfi;
2809   char l1[20], l2[20], section_start_label[20];
2810   bool any_lsda_needed = false;
2811   char augmentation[6];
2812   int augmentation_size;
2813   int fde_encoding = DW_EH_PE_absptr;
2814   int per_encoding = DW_EH_PE_absptr;
2815   int lsda_encoding = DW_EH_PE_absptr;
2816   int return_reg;
2817
2818   /* Don't emit a CIE if there won't be any FDEs.  */
2819   if (fde_table_in_use == 0)
2820     return;
2821
2822   /* Nothing to do if the assembler's doing it all.  */
2823   if (dwarf2out_do_cfi_asm ())
2824     return;
2825
2826   /* If we make FDEs linkonce, we may have to emit an empty label for
2827      an FDE that wouldn't otherwise be emitted.  We want to avoid
2828      having an FDE kept around when the function it refers to is
2829      discarded.  Example where this matters: a primary function
2830      template in C++ requires EH information, but an explicit
2831      specialization doesn't.  */
2832   if (TARGET_USES_WEAK_UNWIND_INFO
2833       && ! flag_asynchronous_unwind_tables
2834       && flag_exceptions
2835       && for_eh)
2836     for (i = 0; i < fde_table_in_use; i++)
2837       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2838           && !fde_table[i].uses_eh_lsda
2839           && ! DECL_WEAK (fde_table[i].decl))
2840         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2841                                       for_eh, /* empty */ 1);
2842
2843   /* If we don't have any functions we'll want to unwind out of, don't
2844      emit any EH unwind information.  Note that if exceptions aren't
2845      enabled, we won't have collected nothrow information, and if we
2846      asked for asynchronous tables, we always want this info.  */
2847   if (for_eh)
2848     {
2849       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2850
2851       for (i = 0; i < fde_table_in_use; i++)
2852         if (fde_table[i].uses_eh_lsda)
2853           any_eh_needed = any_lsda_needed = true;
2854         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2855           any_eh_needed = true;
2856         else if (! fde_table[i].nothrow
2857                  && ! fde_table[i].all_throwers_are_sibcalls)
2858           any_eh_needed = true;
2859
2860       if (! any_eh_needed)
2861         return;
2862     }
2863
2864   /* We're going to be generating comments, so turn on app.  */
2865   if (flag_debug_asm)
2866     app_enable ();
2867
2868   if (for_eh)
2869     switch_to_eh_frame_section ();
2870   else
2871     {
2872       if (!debug_frame_section)
2873         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2874                                            SECTION_DEBUG, NULL);
2875       switch_to_section (debug_frame_section);
2876     }
2877
2878   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2879   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2880
2881   /* Output the CIE.  */
2882   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2883   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2884   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2885     dw2_asm_output_data (4, 0xffffffff,
2886       "Initial length escape value indicating 64-bit DWARF extension");
2887   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2888                         "Length of Common Information Entry");
2889   ASM_OUTPUT_LABEL (asm_out_file, l1);
2890
2891   /* Now that the CIE pointer is PC-relative for EH,
2892      use 0 to identify the CIE.  */
2893   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2894                        (for_eh ? 0 : DWARF_CIE_ID),
2895                        "CIE Identifier Tag");
2896
2897   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2898
2899   augmentation[0] = 0;
2900   augmentation_size = 0;
2901   if (for_eh)
2902     {
2903       char *p;
2904
2905       /* Augmentation:
2906          z      Indicates that a uleb128 is present to size the
2907                 augmentation section.
2908          L      Indicates the encoding (and thus presence) of
2909                 an LSDA pointer in the FDE augmentation.
2910          R      Indicates a non-default pointer encoding for
2911                 FDE code pointers.
2912          P      Indicates the presence of an encoding + language
2913                 personality routine in the CIE augmentation.  */
2914
2915       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2916       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2917       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2918
2919       p = augmentation + 1;
2920       if (eh_personality_libfunc)
2921         {
2922           *p++ = 'P';
2923           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2924           assemble_external_libcall (eh_personality_libfunc);
2925         }
2926       if (any_lsda_needed)
2927         {
2928           *p++ = 'L';
2929           augmentation_size += 1;
2930         }
2931       if (fde_encoding != DW_EH_PE_absptr)
2932         {
2933           *p++ = 'R';
2934           augmentation_size += 1;
2935         }
2936       if (p > augmentation + 1)
2937         {
2938           augmentation[0] = 'z';
2939           *p = '\0';
2940         }
2941
2942       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2943       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2944         {
2945           int offset = (  4             /* Length */
2946                         + 4             /* CIE Id */
2947                         + 1             /* CIE version */
2948                         + strlen (augmentation) + 1     /* Augmentation */
2949                         + size_of_uleb128 (1)           /* Code alignment */
2950                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2951                         + 1             /* RA column */
2952                         + 1             /* Augmentation size */
2953                         + 1             /* Personality encoding */ );
2954           int pad = -offset & (PTR_SIZE - 1);
2955
2956           augmentation_size += pad;
2957
2958           /* Augmentations should be small, so there's scarce need to
2959              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2960           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2961         }
2962     }
2963
2964   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2965   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2966   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2967                                "CIE Data Alignment Factor");
2968
2969   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2970   if (DW_CIE_VERSION == 1)
2971     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2972   else
2973     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2974
2975   if (augmentation[0])
2976     {
2977       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2978       if (eh_personality_libfunc)
2979         {
2980           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2981                                eh_data_format_name (per_encoding));
2982           dw2_asm_output_encoded_addr_rtx (per_encoding,
2983                                            eh_personality_libfunc,
2984                                            true, NULL);
2985         }
2986
2987       if (any_lsda_needed)
2988         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2989                              eh_data_format_name (lsda_encoding));
2990
2991       if (fde_encoding != DW_EH_PE_absptr)
2992         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2993                              eh_data_format_name (fde_encoding));
2994     }
2995
2996   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2997     output_cfi (cfi, NULL, for_eh);
2998
2999   /* Pad the CIE out to an address sized boundary.  */
3000   ASM_OUTPUT_ALIGN (asm_out_file,
3001                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3002   ASM_OUTPUT_LABEL (asm_out_file, l2);
3003
3004   /* Loop through all of the FDE's.  */
3005   for (i = 0; i < fde_table_in_use; i++)
3006     {
3007       fde = &fde_table[i];
3008
3009       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3010       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3011           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3012           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3013           && !fde->uses_eh_lsda)
3014         continue;
3015
3016       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3017       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3018       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3019       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3020       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3021         dw2_asm_output_data (4, 0xffffffff,
3022                              "Initial length escape value indicating 64-bit DWARF extension");
3023       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3024                             "FDE Length");
3025       ASM_OUTPUT_LABEL (asm_out_file, l1);
3026
3027       if (for_eh)
3028         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3029       else
3030         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3031                                debug_frame_section, "FDE CIE offset");
3032
3033       if (for_eh)
3034         {
3035           if (fde->dw_fde_switched_sections)
3036             {
3037               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3038                                       fde->dw_fde_unlikely_section_label);
3039               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3040                                       fde->dw_fde_hot_section_label);
3041               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3042               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3043               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3044                                                "FDE initial location");
3045               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3046                                     fde->dw_fde_hot_section_end_label,
3047                                     fde->dw_fde_hot_section_label,
3048                                     "FDE address range");
3049               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3050                                                "FDE initial location");
3051               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3052                                     fde->dw_fde_unlikely_section_end_label,
3053                                     fde->dw_fde_unlikely_section_label,
3054                                     "FDE address range");
3055             }
3056           else
3057             {
3058               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3059               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3060               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3061                                                sym_ref,
3062                                                false,
3063                                                "FDE initial location");
3064               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3065                                     fde->dw_fde_end, fde->dw_fde_begin,
3066                                     "FDE address range");
3067             }
3068         }
3069       else
3070         {
3071           if (fde->dw_fde_switched_sections)
3072             {
3073               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3074                                    fde->dw_fde_hot_section_label,
3075                                    "FDE initial location");
3076               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3077                                     fde->dw_fde_hot_section_end_label,
3078                                     fde->dw_fde_hot_section_label,
3079                                     "FDE address range");
3080               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3081                                    fde->dw_fde_unlikely_section_label,
3082                                    "FDE initial location");
3083               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3084                                     fde->dw_fde_unlikely_section_end_label,
3085                                     fde->dw_fde_unlikely_section_label,
3086                                     "FDE address range");
3087             }
3088           else
3089             {
3090               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3091                                    "FDE initial location");
3092               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3093                                     fde->dw_fde_end, fde->dw_fde_begin,
3094                                     "FDE address range");
3095             }
3096         }
3097
3098       if (augmentation[0])
3099         {
3100           if (any_lsda_needed)
3101             {
3102               int size = size_of_encoded_value (lsda_encoding);
3103
3104               if (lsda_encoding == DW_EH_PE_aligned)
3105                 {
3106                   int offset = (  4             /* Length */
3107                                 + 4             /* CIE offset */
3108                                 + 2 * size_of_encoded_value (fde_encoding)
3109                                 + 1             /* Augmentation size */ );
3110                   int pad = -offset & (PTR_SIZE - 1);
3111
3112                   size += pad;
3113                   gcc_assert (size_of_uleb128 (size) == 1);
3114                 }
3115
3116               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3117
3118               if (fde->uses_eh_lsda)
3119                 {
3120                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3121                                                fde->funcdef_number);
3122                   dw2_asm_output_encoded_addr_rtx (
3123                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3124                         false, "Language Specific Data Area");
3125                 }
3126               else
3127                 {
3128                   if (lsda_encoding == DW_EH_PE_aligned)
3129                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3130                   dw2_asm_output_data
3131                     (size_of_encoded_value (lsda_encoding), 0,
3132                      "Language Specific Data Area (none)");
3133                 }
3134             }
3135           else
3136             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3137         }
3138
3139       /* Loop through the Call Frame Instructions associated with
3140          this FDE.  */
3141       fde->dw_fde_current_label = fde->dw_fde_begin;
3142       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3143         output_cfi (cfi, fde, for_eh);
3144
3145       /* Pad the FDE out to an address sized boundary.  */
3146       ASM_OUTPUT_ALIGN (asm_out_file,
3147                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3148       ASM_OUTPUT_LABEL (asm_out_file, l2);
3149     }
3150
3151   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3152     dw2_asm_output_data (4, 0, "End of Table");
3153 #ifdef MIPS_DEBUGGING_INFO
3154   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3155      get a value of 0.  Putting .align 0 after the label fixes it.  */
3156   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3157 #endif
3158
3159   /* Turn off app to make assembly quicker.  */
3160   if (flag_debug_asm)
3161     app_disable ();
3162 }
3163
3164 /* Output a marker (i.e. a label) for the beginning of a function, before
3165    the prologue.  */
3166
3167 void
3168 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3169                           const char *file ATTRIBUTE_UNUSED)
3170 {
3171   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3172   char * dup_label;
3173   dw_fde_ref fde;
3174
3175   current_function_func_begin_label = NULL;
3176
3177 #ifdef TARGET_UNWIND_INFO
3178   /* ??? current_function_func_begin_label is also used by except.c
3179      for call-site information.  We must emit this label if it might
3180      be used.  */
3181   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3182       && ! dwarf2out_do_frame ())
3183     return;
3184 #else
3185   if (! dwarf2out_do_frame ())
3186     return;
3187 #endif
3188
3189   switch_to_section (function_section (current_function_decl));
3190   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3191                                current_function_funcdef_no);
3192   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3193                           current_function_funcdef_no);
3194   dup_label = xstrdup (label);
3195   current_function_func_begin_label = dup_label;
3196
3197 #ifdef TARGET_UNWIND_INFO
3198   /* We can elide the fde allocation if we're not emitting debug info.  */
3199   if (! dwarf2out_do_frame ())
3200     return;
3201 #endif
3202
3203   /* Expand the fde table if necessary.  */
3204   if (fde_table_in_use == fde_table_allocated)
3205     {
3206       fde_table_allocated += FDE_TABLE_INCREMENT;
3207       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3208       memset (fde_table + fde_table_in_use, 0,
3209               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3210     }
3211
3212   /* Record the FDE associated with this function.  */
3213   current_funcdef_fde = fde_table_in_use;
3214
3215   /* Add the new FDE at the end of the fde_table.  */
3216   fde = &fde_table[fde_table_in_use++];
3217   fde->decl = current_function_decl;
3218   fde->dw_fde_begin = dup_label;
3219   fde->dw_fde_current_label = dup_label;
3220   fde->dw_fde_hot_section_label = NULL;
3221   fde->dw_fde_hot_section_end_label = NULL;
3222   fde->dw_fde_unlikely_section_label = NULL;
3223   fde->dw_fde_unlikely_section_end_label = NULL;
3224   fde->dw_fde_switched_sections = false;
3225   fde->dw_fde_end = NULL;
3226   fde->dw_fde_cfi = NULL;
3227   fde->funcdef_number = current_function_funcdef_no;
3228   fde->nothrow = crtl->nothrow;
3229   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3230   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3231   fde->drap_reg = INVALID_REGNUM;
3232   fde->vdrap_reg = INVALID_REGNUM;
3233
3234   args_size = old_args_size = 0;
3235
3236   /* We only want to output line number information for the genuine dwarf2
3237      prologue case, not the eh frame case.  */
3238 #ifdef DWARF2_DEBUGGING_INFO
3239   if (file)
3240     dwarf2out_source_line (line, file);
3241 #endif
3242
3243   if (dwarf2out_do_cfi_asm ())
3244     {
3245       int enc;
3246       rtx ref;
3247
3248       fprintf (asm_out_file, "\t.cfi_startproc\n");
3249
3250       if (eh_personality_libfunc)
3251         {
3252           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3253           ref = eh_personality_libfunc;
3254
3255           /* ??? The GAS support isn't entirely consistent.  We have to
3256              handle indirect support ourselves, but PC-relative is done
3257              in the assembler.  Further, the assembler can't handle any
3258              of the weirder relocation types.  */
3259           if (enc & DW_EH_PE_indirect)
3260             ref = dw2_force_const_mem (ref, true);
3261
3262           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3263           output_addr_const (asm_out_file, ref);
3264           fputc ('\n', asm_out_file);
3265         }
3266
3267       if (crtl->uses_eh_lsda)
3268         {
3269           char lab[20];
3270
3271           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3272           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3273                                        current_function_funcdef_no);
3274           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3275           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3276
3277           if (enc & DW_EH_PE_indirect)
3278             ref = dw2_force_const_mem (ref, true);
3279
3280           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3281           output_addr_const (asm_out_file, ref);
3282           fputc ('\n', asm_out_file);
3283         }
3284     }
3285 }
3286
3287 /* Output a marker (i.e. a label) for the absolute end of the generated code
3288    for a function definition.  This gets called *after* the epilogue code has
3289    been generated.  */
3290
3291 void
3292 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3293                         const char *file ATTRIBUTE_UNUSED)
3294 {
3295   dw_fde_ref fde;
3296   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3297
3298   if (dwarf2out_do_cfi_asm ())
3299     fprintf (asm_out_file, "\t.cfi_endproc\n");
3300
3301   /* Output a label to mark the endpoint of the code generated for this
3302      function.  */
3303   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3304                                current_function_funcdef_no);
3305   ASM_OUTPUT_LABEL (asm_out_file, label);
3306   fde = current_fde ();
3307   gcc_assert (fde != NULL);
3308   fde->dw_fde_end = xstrdup (label);
3309 }
3310
3311 void
3312 dwarf2out_frame_init (void)
3313 {
3314   /* Allocate the initial hunk of the fde_table.  */
3315   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3316   fde_table_allocated = FDE_TABLE_INCREMENT;
3317   fde_table_in_use = 0;
3318
3319   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3320      sake of lookup_cfa.  */
3321
3322   /* On entry, the Canonical Frame Address is at SP.  */
3323   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3324
3325 #ifdef DWARF2_UNWIND_INFO
3326   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3327     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3328 #endif
3329 }
3330
3331 void
3332 dwarf2out_frame_finish (void)
3333 {
3334   /* Output call frame information.  */
3335   if (DWARF2_FRAME_INFO)
3336     output_call_frame_info (0);
3337
3338 #ifndef TARGET_UNWIND_INFO
3339   /* Output another copy for the unwinder.  */
3340   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3341     output_call_frame_info (1);
3342 #endif
3343 }
3344
3345 /* Note that the current function section is being used for code.  */
3346
3347 static void
3348 dwarf2out_note_section_used (void)
3349 {
3350   section *sec = current_function_section ();
3351   if (sec == text_section)
3352     text_section_used = true;
3353   else if (sec == cold_text_section)
3354     cold_text_section_used = true;
3355 }
3356
3357 void
3358 dwarf2out_switch_text_section (void)
3359 {
3360   dw_fde_ref fde = current_fde ();
3361
3362   gcc_assert (cfun && fde);
3363
3364   fde->dw_fde_switched_sections = true;
3365   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3366   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3367   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3368   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3369   have_multiple_function_sections = true;
3370
3371   /* Reset the current label on switching text sections, so that we
3372      don't attempt to advance_loc4 between labels in different sections.  */
3373   fde->dw_fde_current_label = NULL;
3374
3375   /* There is no need to mark used sections when not debugging.  */
3376   if (cold_text_section != NULL)
3377     dwarf2out_note_section_used ();
3378 }
3379 #endif
3380 \f
3381 /* And now, the subset of the debugging information support code necessary
3382    for emitting location expressions.  */
3383
3384 /* Data about a single source file.  */
3385 struct GTY(()) dwarf_file_data {
3386   const char * filename;
3387   int emitted_number;
3388 };
3389
3390 /* We need some way to distinguish DW_OP_addr with a direct symbol
3391    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3392 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3393
3394
3395 typedef struct dw_val_struct *dw_val_ref;
3396 typedef struct die_struct *dw_die_ref;
3397 typedef const struct die_struct *const_dw_die_ref;
3398 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3399 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3400
3401 typedef struct GTY(()) deferred_locations_struct
3402 {
3403   tree variable;
3404   dw_die_ref die;
3405 } deferred_locations;
3406
3407 DEF_VEC_O(deferred_locations);
3408 DEF_VEC_ALLOC_O(deferred_locations,gc);
3409
3410 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3411
3412 /* Each DIE may have a series of attribute/value pairs.  Values
3413    can take on several forms.  The forms that are used in this
3414    implementation are listed below.  */
3415
3416 enum dw_val_class
3417 {
3418   dw_val_class_addr,
3419   dw_val_class_offset,
3420   dw_val_class_loc,
3421   dw_val_class_loc_list,
3422   dw_val_class_range_list,
3423   dw_val_class_const,
3424   dw_val_class_unsigned_const,
3425   dw_val_class_long_long,
3426   dw_val_class_vec,
3427   dw_val_class_flag,
3428   dw_val_class_die_ref,
3429   dw_val_class_fde_ref,
3430   dw_val_class_lbl_id,
3431   dw_val_class_lineptr,
3432   dw_val_class_str,
3433   dw_val_class_macptr,
3434   dw_val_class_file
3435 };
3436
3437 /* Describe a double word constant value.  */
3438 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3439
3440 typedef struct GTY(()) dw_long_long_struct {
3441   unsigned long hi;
3442   unsigned long low;
3443 }
3444 dw_long_long_const;
3445
3446 /* Describe a floating point constant value, or a vector constant value.  */
3447
3448 typedef struct GTY(()) dw_vec_struct {
3449   unsigned char * GTY((length ("%h.length"))) array;
3450   unsigned length;
3451   unsigned elt_size;
3452 }
3453 dw_vec_const;
3454
3455 /* The dw_val_node describes an attribute's value, as it is
3456    represented internally.  */
3457
3458 typedef struct GTY(()) dw_val_struct {
3459   enum dw_val_class val_class;
3460   union dw_val_struct_union
3461     {
3462       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3463       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3464       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3465       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3466       HOST_WIDE_INT GTY ((default)) val_int;
3467       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3468       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3469       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3470       struct dw_val_die_union
3471         {
3472           dw_die_ref die;
3473           int external;
3474         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3475       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3476       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3477       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3478       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3479       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3480     }
3481   GTY ((desc ("%1.val_class"))) v;
3482 }
3483 dw_val_node;
3484
3485 /* Locations in memory are described using a sequence of stack machine
3486    operations.  */
3487
3488 typedef struct GTY(()) dw_loc_descr_struct {
3489   dw_loc_descr_ref dw_loc_next;
3490   enum dwarf_location_atom dw_loc_opc;
3491   int dw_loc_addr;
3492   dw_val_node dw_loc_oprnd1;
3493   dw_val_node dw_loc_oprnd2;
3494 }
3495 dw_loc_descr_node;
3496
3497 /* Location lists are ranges + location descriptions for that range,
3498    so you can track variables that are in different places over
3499    their entire life.  */
3500 typedef struct GTY(()) dw_loc_list_struct {
3501   dw_loc_list_ref dw_loc_next;
3502   const char *begin; /* Label for begin address of range */
3503   const char *end;  /* Label for end address of range */
3504   char *ll_symbol; /* Label for beginning of location list.
3505                       Only on head of list */
3506   const char *section; /* Section this loclist is relative to */
3507   dw_loc_descr_ref expr;
3508 } dw_loc_list_node;
3509
3510 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3511
3512 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3513
3514 /* Convert a DWARF stack opcode into its string name.  */
3515
3516 static const char *
3517 dwarf_stack_op_name (unsigned int op)
3518 {
3519   switch (op)
3520     {
3521     case DW_OP_addr:
3522     case INTERNAL_DW_OP_tls_addr:
3523       return "DW_OP_addr";
3524     case DW_OP_deref:
3525       return "DW_OP_deref";
3526     case DW_OP_const1u:
3527       return "DW_OP_const1u";
3528     case DW_OP_const1s:
3529       return "DW_OP_const1s";
3530     case DW_OP_const2u:
3531       return "DW_OP_const2u";
3532     case DW_OP_const2s:
3533       return "DW_OP_const2s";
3534     case DW_OP_const4u:
3535       return "DW_OP_const4u";
3536     case DW_OP_const4s:
3537       return "DW_OP_const4s";
3538     case DW_OP_const8u:
3539       return "DW_OP_const8u";
3540     case DW_OP_const8s:
3541       return "DW_OP_const8s";
3542     case DW_OP_constu:
3543       return "DW_OP_constu";
3544     case DW_OP_consts:
3545       return "DW_OP_consts";
3546     case DW_OP_dup:
3547       return "DW_OP_dup";
3548     case DW_OP_drop:
3549       return "DW_OP_drop";
3550     case DW_OP_over:
3551       return "DW_OP_over";
3552     case DW_OP_pick:
3553       return "DW_OP_pick";
3554     case DW_OP_swap:
3555       return "DW_OP_swap";
3556     case DW_OP_rot:
3557       return "DW_OP_rot";
3558     case DW_OP_xderef:
3559       return "DW_OP_xderef";
3560     case DW_OP_abs:
3561       return "DW_OP_abs";
3562     case DW_OP_and:
3563       return "DW_OP_and";
3564     case DW_OP_div:
3565       return "DW_OP_div";
3566     case DW_OP_minus:
3567       return "DW_OP_minus";
3568     case DW_OP_mod:
3569       return "DW_OP_mod";
3570     case DW_OP_mul:
3571       return "DW_OP_mul";
3572     case DW_OP_neg:
3573       return "DW_OP_neg";
3574     case DW_OP_not:
3575       return "DW_OP_not";
3576     case DW_OP_or:
3577       return "DW_OP_or";
3578     case DW_OP_plus:
3579       return "DW_OP_plus";
3580     case DW_OP_plus_uconst:
3581       return "DW_OP_plus_uconst";
3582     case DW_OP_shl:
3583       return "DW_OP_shl";
3584     case DW_OP_shr:
3585       return "DW_OP_shr";
3586     case DW_OP_shra:
3587       return "DW_OP_shra";
3588     case DW_OP_xor:
3589       return "DW_OP_xor";
3590     case DW_OP_bra:
3591       return "DW_OP_bra";
3592     case DW_OP_eq:
3593       return "DW_OP_eq";
3594     case DW_OP_ge:
3595       return "DW_OP_ge";
3596     case DW_OP_gt:
3597       return "DW_OP_gt";
3598     case DW_OP_le:
3599       return "DW_OP_le";
3600     case DW_OP_lt:
3601       return "DW_OP_lt";
3602     case DW_OP_ne:
3603       return "DW_OP_ne";
3604     case DW_OP_skip:
3605       return "DW_OP_skip";
3606     case DW_OP_lit0:
3607       return "DW_OP_lit0";
3608     case DW_OP_lit1:
3609       return "DW_OP_lit1";
3610     case DW_OP_lit2:
3611       return "DW_OP_lit2";
3612     case DW_OP_lit3:
3613       return "DW_OP_lit3";
3614     case DW_OP_lit4:
3615       return "DW_OP_lit4";
3616     case DW_OP_lit5:
3617       return "DW_OP_lit5";
3618     case DW_OP_lit6:
3619       return "DW_OP_lit6";
3620     case DW_OP_lit7:
3621       return "DW_OP_lit7";
3622     case DW_OP_lit8:
3623       return "DW_OP_lit8";
3624     case DW_OP_lit9:
3625       return "DW_OP_lit9";
3626     case DW_OP_lit10:
3627       return "DW_OP_lit10";
3628     case DW_OP_lit11:
3629       return "DW_OP_lit11";
3630     case DW_OP_lit12:
3631       return "DW_OP_lit12";
3632     case DW_OP_lit13:
3633       return "DW_OP_lit13";
3634     case DW_OP_lit14:
3635       return "DW_OP_lit14";
3636     case DW_OP_lit15:
3637       return "DW_OP_lit15";
3638     case DW_OP_lit16:
3639       return "DW_OP_lit16";
3640     case DW_OP_lit17:
3641       return "DW_OP_lit17";
3642     case DW_OP_lit18:
3643       return "DW_OP_lit18";
3644     case DW_OP_lit19:
3645       return "DW_OP_lit19";
3646     case DW_OP_lit20:
3647       return "DW_OP_lit20";
3648     case DW_OP_lit21:
3649       return "DW_OP_lit21";
3650     case DW_OP_lit22:
3651       return "DW_OP_lit22";
3652     case DW_OP_lit23:
3653       return "DW_OP_lit23";
3654     case DW_OP_lit24:
3655       return "DW_OP_lit24";
3656     case DW_OP_lit25:
3657       return "DW_OP_lit25";
3658     case DW_OP_lit26:
3659       return "DW_OP_lit26";
3660     case DW_OP_lit27:
3661       return "DW_OP_lit27";
3662     case DW_OP_lit28:
3663       return "DW_OP_lit28";
3664     case DW_OP_lit29:
3665       return "DW_OP_lit29";
3666     case DW_OP_lit30:
3667       return "DW_OP_lit30";
3668     case DW_OP_lit31:
3669       return "DW_OP_lit31";
3670     case DW_OP_reg0:
3671       return "DW_OP_reg0";
3672     case DW_OP_reg1:
3673       return "DW_OP_reg1";
3674     case DW_OP_reg2:
3675       return "DW_OP_reg2";
3676     case DW_OP_reg3:
3677       return "DW_OP_reg3";
3678     case DW_OP_reg4:
3679       return "DW_OP_reg4";
3680     case DW_OP_reg5:
3681       return "DW_OP_reg5";
3682     case DW_OP_reg6:
3683       return "DW_OP_reg6";
3684     case DW_OP_reg7:
3685       return "DW_OP_reg7";
3686     case DW_OP_reg8:
3687       return "DW_OP_reg8";
3688     case DW_OP_reg9:
3689       return "DW_OP_reg9";
3690     case DW_OP_reg10:
3691       return "DW_OP_reg10";
3692     case DW_OP_reg11:
3693       return "DW_OP_reg11";
3694     case DW_OP_reg12:
3695       return "DW_OP_reg12";
3696     case DW_OP_reg13:
3697       return "DW_OP_reg13";
3698     case DW_OP_reg14:
3699       return "DW_OP_reg14";
3700     case DW_OP_reg15:
3701       return "DW_OP_reg15";
3702     case DW_OP_reg16:
3703       return "DW_OP_reg16";
3704     case DW_OP_reg17:
3705       return "DW_OP_reg17";
3706     case DW_OP_reg18:
3707       return "DW_OP_reg18";
3708     case DW_OP_reg19:
3709       return "DW_OP_reg19";
3710     case DW_OP_reg20:
3711       return "DW_OP_reg20";
3712     case DW_OP_reg21:
3713       return "DW_OP_reg21";
3714     case DW_OP_reg22:
3715       return "DW_OP_reg22";
3716     case DW_OP_reg23:
3717       return "DW_OP_reg23";
3718     case DW_OP_reg24:
3719       return "DW_OP_reg24";
3720     case DW_OP_reg25:
3721       return "DW_OP_reg25";
3722     case DW_OP_reg26:
3723       return "DW_OP_reg26";
3724     case DW_OP_reg27:
3725       return "DW_OP_reg27";
3726     case DW_OP_reg28:
3727       return "DW_OP_reg28";
3728     case DW_OP_reg29:
3729       return "DW_OP_reg29";
3730     case DW_OP_reg30:
3731       return "DW_OP_reg30";
3732     case DW_OP_reg31:
3733       return "DW_OP_reg31";
3734     case DW_OP_breg0:
3735       return "DW_OP_breg0";
3736     case DW_OP_breg1:
3737       return "DW_OP_breg1";
3738     case DW_OP_breg2:
3739       return "DW_OP_breg2";
3740     case DW_OP_breg3:
3741       return "DW_OP_breg3";
3742     case DW_OP_breg4:
3743       return "DW_OP_breg4";
3744     case DW_OP_breg5:
3745       return "DW_OP_breg5";
3746     case DW_OP_breg6:
3747       return "DW_OP_breg6";
3748     case DW_OP_breg7:
3749       return "DW_OP_breg7";
3750     case DW_OP_breg8:
3751       return "DW_OP_breg8";
3752     case DW_OP_breg9:
3753       return "DW_OP_breg9";
3754     case DW_OP_breg10:
3755       return "DW_OP_breg10";
3756     case DW_OP_breg11:
3757       return "DW_OP_breg11";
3758     case DW_OP_breg12:
3759       return "DW_OP_breg12";
3760     case DW_OP_breg13:
3761       return "DW_OP_breg13";
3762     case DW_OP_breg14:
3763       return "DW_OP_breg14";
3764     case DW_OP_breg15:
3765       return "DW_OP_breg15";
3766     case DW_OP_breg16:
3767       return "DW_OP_breg16";
3768     case DW_OP_breg17:
3769       return "DW_OP_breg17";
3770     case DW_OP_breg18:
3771       return "DW_OP_breg18";
3772     case DW_OP_breg19:
3773       return "DW_OP_breg19";
3774     case DW_OP_breg20:
3775       return "DW_OP_breg20";
3776     case DW_OP_breg21:
3777       return "DW_OP_breg21";
3778     case DW_OP_breg22:
3779       return "DW_OP_breg22";
3780     case DW_OP_breg23:
3781       return "DW_OP_breg23";
3782     case DW_OP_breg24:
3783       return "DW_OP_breg24";
3784     case DW_OP_breg25:
3785       return "DW_OP_breg25";
3786     case DW_OP_breg26:
3787       return "DW_OP_breg26";
3788     case DW_OP_breg27:
3789       return "DW_OP_breg27";
3790     case DW_OP_breg28:
3791       return "DW_OP_breg28";
3792     case DW_OP_breg29:
3793       return "DW_OP_breg29";
3794     case DW_OP_breg30:
3795       return "DW_OP_breg30";
3796     case DW_OP_breg31:
3797       return "DW_OP_breg31";
3798     case DW_OP_regx:
3799       return "DW_OP_regx";
3800     case DW_OP_fbreg:
3801       return "DW_OP_fbreg";
3802     case DW_OP_bregx:
3803       return "DW_OP_bregx";
3804     case DW_OP_piece:
3805       return "DW_OP_piece";
3806     case DW_OP_deref_size:
3807       return "DW_OP_deref_size";
3808     case DW_OP_xderef_size:
3809       return "DW_OP_xderef_size";
3810     case DW_OP_nop:
3811       return "DW_OP_nop";
3812     case DW_OP_push_object_address:
3813       return "DW_OP_push_object_address";
3814     case DW_OP_call2:
3815       return "DW_OP_call2";
3816     case DW_OP_call4:
3817       return "DW_OP_call4";
3818     case DW_OP_call_ref:
3819       return "DW_OP_call_ref";
3820     case DW_OP_GNU_push_tls_address:
3821       return "DW_OP_GNU_push_tls_address";
3822     case DW_OP_GNU_uninit:
3823       return "DW_OP_GNU_uninit";
3824     default:
3825       return "OP_<unknown>";
3826     }
3827 }
3828
3829 /* Return a pointer to a newly allocated location description.  Location
3830    descriptions are simple expression terms that can be strung
3831    together to form more complicated location (address) descriptions.  */
3832
3833 static inline dw_loc_descr_ref
3834 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3835                unsigned HOST_WIDE_INT oprnd2)
3836 {
3837   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3838
3839   descr->dw_loc_opc = op;
3840   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3841   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3842   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3843   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3844
3845   return descr;
3846 }
3847
3848 /* Return a pointer to a newly allocated location description for
3849    REG and OFFSET.  */
3850
3851 static inline dw_loc_descr_ref
3852 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3853 {
3854   if (reg <= 31)
3855     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
3856                           offset, 0);
3857   else
3858     return new_loc_descr (DW_OP_bregx, reg, offset);
3859 }
3860
3861 /* Add a location description term to a location description expression.  */
3862
3863 static inline void
3864 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3865 {
3866   dw_loc_descr_ref *d;
3867
3868   /* Find the end of the chain.  */
3869   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3870     ;
3871
3872   *d = descr;
3873 }
3874
3875 /* Add a constant OFFSET to a location expression.  */
3876
3877 static void
3878 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
3879 {
3880   dw_loc_descr_ref loc;
3881   HOST_WIDE_INT *p;
3882
3883   gcc_assert (*list_head != NULL);
3884
3885   if (!offset)
3886     return;
3887
3888   /* Find the end of the chain.  */
3889   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
3890     ;
3891
3892   p = NULL;
3893   if (loc->dw_loc_opc == DW_OP_fbreg
3894       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
3895     p = &loc->dw_loc_oprnd1.v.val_int;
3896   else if (loc->dw_loc_opc == DW_OP_bregx)
3897     p = &loc->dw_loc_oprnd2.v.val_int;
3898
3899   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
3900      offset.  Don't optimize if an signed integer overflow would happen.  */
3901   if (p != NULL
3902       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
3903           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
3904     *p += offset;
3905
3906   else if (offset > 0)
3907     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3908
3909   else
3910     {
3911       loc->dw_loc_next = int_loc_descriptor (offset);
3912       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
3913     }
3914 }
3915
3916 /* Return the size of a location descriptor.  */
3917
3918 static unsigned long
3919 size_of_loc_descr (dw_loc_descr_ref loc)
3920 {
3921   unsigned long size = 1;
3922
3923   switch (loc->dw_loc_opc)
3924     {
3925     case DW_OP_addr:
3926     case INTERNAL_DW_OP_tls_addr:
3927       size += DWARF2_ADDR_SIZE;
3928       break;
3929     case DW_OP_const1u:
3930     case DW_OP_const1s:
3931       size += 1;
3932       break;
3933     case DW_OP_const2u:
3934     case DW_OP_const2s:
3935       size += 2;
3936       break;
3937     case DW_OP_const4u:
3938     case DW_OP_const4s:
3939       size += 4;
3940       break;
3941     case DW_OP_const8u:
3942     case DW_OP_const8s:
3943       size += 8;
3944       break;
3945     case DW_OP_constu:
3946       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3947       break;
3948     case DW_OP_consts:
3949       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3950       break;
3951     case DW_OP_pick:
3952       size += 1;
3953       break;
3954     case DW_OP_plus_uconst:
3955       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3956       break;
3957     case DW_OP_skip:
3958     case DW_OP_bra:
3959       size += 2;
3960       break;
3961     case DW_OP_breg0:
3962     case DW_OP_breg1:
3963     case DW_OP_breg2:
3964     case DW_OP_breg3:
3965     case DW_OP_breg4:
3966     case DW_OP_breg5:
3967     case DW_OP_breg6:
3968     case DW_OP_breg7:
3969     case DW_OP_breg8:
3970     case DW_OP_breg9:
3971     case DW_OP_breg10:
3972     case DW_OP_breg11:
3973     case DW_OP_breg12:
3974     case DW_OP_breg13:
3975     case DW_OP_breg14:
3976     case DW_OP_breg15:
3977     case DW_OP_breg16:
3978     case DW_OP_breg17:
3979     case DW_OP_breg18:
3980     case DW_OP_breg19:
3981     case DW_OP_breg20:
3982     case DW_OP_breg21:
3983     case DW_OP_breg22:
3984     case DW_OP_breg23:
3985     case DW_OP_breg24:
3986     case DW_OP_breg25:
3987     case DW_OP_breg26:
3988     case DW_OP_breg27:
3989     case DW_OP_breg28:
3990     case DW_OP_breg29:
3991     case DW_OP_breg30:
3992     case DW_OP_breg31:
3993       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3994       break;
3995     case DW_OP_regx:
3996       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3997       break;
3998     case DW_OP_fbreg:
3999       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4000       break;
4001     case DW_OP_bregx:
4002       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4003       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4004       break;
4005     case DW_OP_piece:
4006       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4007       break;
4008     case DW_OP_deref_size:
4009     case DW_OP_xderef_size:
4010       size += 1;
4011       break;
4012     case DW_OP_call2:
4013       size += 2;
4014       break;
4015     case DW_OP_call4:
4016       size += 4;
4017       break;
4018     case DW_OP_call_ref:
4019       size += DWARF2_ADDR_SIZE;
4020       break;
4021     default:
4022       break;
4023     }
4024
4025   return size;
4026 }
4027
4028 /* Return the size of a series of location descriptors.  */
4029
4030 static unsigned long
4031 size_of_locs (dw_loc_descr_ref loc)
4032 {
4033   dw_loc_descr_ref l;
4034   unsigned long size;
4035
4036   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4037      field, to avoid writing to a PCH file.  */
4038   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4039     {
4040       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4041         break;
4042       size += size_of_loc_descr (l);
4043     }
4044   if (! l)
4045     return size;
4046
4047   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4048     {
4049       l->dw_loc_addr = size;
4050       size += size_of_loc_descr (l);
4051     }
4052
4053   return size;
4054 }
4055
4056 /* Output location description stack opcode's operands (if any).  */
4057
4058 static void
4059 output_loc_operands (dw_loc_descr_ref loc)
4060 {
4061   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4062   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4063
4064   switch (loc->dw_loc_opc)
4065     {
4066 #ifdef DWARF2_DEBUGGING_INFO
4067     case DW_OP_addr:
4068       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4069       break;
4070     case DW_OP_const2u:
4071     case DW_OP_const2s:
4072       dw2_asm_output_data (2, val1->v.val_int, NULL);
4073       break;
4074     case DW_OP_const4u:
4075     case DW_OP_const4s:
4076       dw2_asm_output_data (4, val1->v.val_int, NULL);
4077       break;
4078     case DW_OP_const8u:
4079     case DW_OP_const8s:
4080       gcc_assert (HOST_BITS_PER_LONG >= 64);
4081       dw2_asm_output_data (8, val1->v.val_int, NULL);
4082       break;
4083     case DW_OP_skip:
4084     case DW_OP_bra:
4085       {
4086         int offset;
4087
4088         gcc_assert (val1->val_class == dw_val_class_loc);
4089         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4090
4091         dw2_asm_output_data (2, offset, NULL);
4092       }
4093       break;
4094 #else
4095     case DW_OP_addr:
4096     case DW_OP_const2u:
4097     case DW_OP_const2s:
4098     case DW_OP_const4u:
4099     case DW_OP_const4s:
4100     case DW_OP_const8u:
4101     case DW_OP_const8s:
4102     case DW_OP_skip:
4103     case DW_OP_bra:
4104       /* We currently don't make any attempt to make sure these are
4105          aligned properly like we do for the main unwind info, so
4106          don't support emitting things larger than a byte if we're
4107          only doing unwinding.  */
4108       gcc_unreachable ();
4109 #endif
4110     case DW_OP_const1u:
4111     case DW_OP_const1s:
4112       dw2_asm_output_data (1, val1->v.val_int, NULL);
4113       break;
4114     case DW_OP_constu:
4115       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4116       break;
4117     case DW_OP_consts:
4118       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4119       break;
4120     case DW_OP_pick:
4121       dw2_asm_output_data (1, val1->v.val_int, NULL);
4122       break;
4123     case DW_OP_plus_uconst:
4124       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4125       break;
4126     case DW_OP_breg0:
4127     case DW_OP_breg1:
4128     case DW_OP_breg2:
4129     case DW_OP_breg3:
4130     case DW_OP_breg4:
4131     case DW_OP_breg5:
4132     case DW_OP_breg6:
4133     case DW_OP_breg7:
4134     case DW_OP_breg8:
4135     case DW_OP_breg9:
4136     case DW_OP_breg10:
4137     case DW_OP_breg11:
4138     case DW_OP_breg12:
4139     case DW_OP_breg13:
4140     case DW_OP_breg14:
4141     case DW_OP_breg15:
4142     case DW_OP_breg16:
4143     case DW_OP_breg17:
4144     case DW_OP_breg18:
4145     case DW_OP_breg19:
4146     case DW_OP_breg20:
4147     case DW_OP_breg21:
4148     case DW_OP_breg22:
4149     case DW_OP_breg23:
4150     case DW_OP_breg24:
4151     case DW_OP_breg25:
4152     case DW_OP_breg26:
4153     case DW_OP_breg27:
4154     case DW_OP_breg28:
4155     case DW_OP_breg29:
4156     case DW_OP_breg30:
4157     case DW_OP_breg31:
4158       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4159       break;
4160     case DW_OP_regx:
4161       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4162       break;
4163     case DW_OP_fbreg:
4164       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4165       break;
4166     case DW_OP_bregx:
4167       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4168       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4169       break;
4170     case DW_OP_piece:
4171       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4172       break;
4173     case DW_OP_deref_size:
4174     case DW_OP_xderef_size:
4175       dw2_asm_output_data (1, val1->v.val_int, NULL);
4176       break;
4177
4178     case INTERNAL_DW_OP_tls_addr:
4179       if (targetm.asm_out.output_dwarf_dtprel)
4180         {
4181           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4182                                                DWARF2_ADDR_SIZE,
4183                                                val1->v.val_addr);
4184           fputc ('\n', asm_out_file);
4185         }
4186       else
4187         gcc_unreachable ();
4188       break;
4189
4190     default:
4191       /* Other codes have no operands.  */
4192       break;
4193     }
4194 }
4195
4196 /* Output a sequence of location operations.  */
4197
4198 static void
4199 output_loc_sequence (dw_loc_descr_ref loc)
4200 {
4201   for (; loc != NULL; loc = loc->dw_loc_next)
4202     {
4203       /* Output the opcode.  */
4204       dw2_asm_output_data (1, loc->dw_loc_opc,
4205                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4206
4207       /* Output the operand(s) (if any).  */
4208       output_loc_operands (loc);
4209     }
4210 }
4211
4212 /* Output location description stack opcode's operands (if any).
4213    The output is single bytes on a line, suitable for .cfi_escape.  */
4214
4215 static void
4216 output_loc_operands_raw (dw_loc_descr_ref loc)
4217 {
4218   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4219   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4220
4221   switch (loc->dw_loc_opc)
4222     {
4223     case DW_OP_addr:
4224       /* We cannot output addresses in .cfi_escape, only bytes.  */
4225       gcc_unreachable ();
4226
4227     case DW_OP_const1u:
4228     case DW_OP_const1s:
4229     case DW_OP_pick:
4230     case DW_OP_deref_size:
4231     case DW_OP_xderef_size:
4232       fputc (',', asm_out_file);
4233       dw2_asm_output_data_raw (1, val1->v.val_int);
4234       break;
4235
4236     case DW_OP_const2u:
4237     case DW_OP_const2s:
4238       fputc (',', asm_out_file);
4239       dw2_asm_output_data_raw (2, val1->v.val_int);
4240       break;
4241
4242     case DW_OP_const4u:
4243     case DW_OP_const4s:
4244       fputc (',', asm_out_file);
4245       dw2_asm_output_data_raw (4, val1->v.val_int);
4246       break;
4247
4248     case DW_OP_const8u:
4249     case DW_OP_const8s:
4250       gcc_assert (HOST_BITS_PER_LONG >= 64);
4251       fputc (',', asm_out_file);
4252       dw2_asm_output_data_raw (8, val1->v.val_int);
4253       break;
4254
4255     case DW_OP_skip:
4256     case DW_OP_bra:
4257       {
4258         int offset;
4259
4260         gcc_assert (val1->val_class == dw_val_class_loc);
4261         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4262
4263         fputc (',', asm_out_file);
4264         dw2_asm_output_data_raw (2, offset);
4265       }
4266       break;
4267
4268     case DW_OP_constu:
4269     case DW_OP_plus_uconst:
4270     case DW_OP_regx:
4271     case DW_OP_piece:
4272       fputc (',', asm_out_file);
4273       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4274       break;
4275
4276     case DW_OP_consts:
4277     case DW_OP_breg0:
4278     case DW_OP_breg1:
4279     case DW_OP_breg2:
4280     case DW_OP_breg3:
4281     case DW_OP_breg4:
4282     case DW_OP_breg5:
4283     case DW_OP_breg6:
4284     case DW_OP_breg7:
4285     case DW_OP_breg8:
4286     case DW_OP_breg9:
4287     case DW_OP_breg10:
4288     case DW_OP_breg11:
4289     case DW_OP_breg12:
4290     case DW_OP_breg13:
4291     case DW_OP_breg14:
4292     case DW_OP_breg15:
4293     case DW_OP_breg16:
4294     case DW_OP_breg17:
4295     case DW_OP_breg18:
4296     case DW_OP_breg19:
4297     case DW_OP_breg20:
4298     case DW_OP_breg21:
4299     case DW_OP_breg22:
4300     case DW_OP_breg23:
4301     case DW_OP_breg24:
4302     case DW_OP_breg25:
4303     case DW_OP_breg26:
4304     case DW_OP_breg27:
4305     case DW_OP_breg28:
4306     case DW_OP_breg29:
4307     case DW_OP_breg30:
4308     case DW_OP_breg31:
4309     case DW_OP_fbreg:
4310       fputc (',', asm_out_file);
4311       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4312       break;
4313
4314     case DW_OP_bregx:
4315       fputc (',', asm_out_file);
4316       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4317       fputc (',', asm_out_file);
4318       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4319       break;
4320
4321     case INTERNAL_DW_OP_tls_addr:
4322       gcc_unreachable ();
4323
4324     default:
4325       /* Other codes have no operands.  */
4326       break;
4327     }
4328 }
4329
4330 static void
4331 output_loc_sequence_raw (dw_loc_descr_ref loc)
4332 {
4333   while (1)
4334     {
4335       /* Output the opcode.  */
4336       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4337       output_loc_operands_raw (loc);
4338
4339       if (!loc->dw_loc_next)
4340         break;
4341       loc = loc->dw_loc_next;
4342
4343       fputc (',', asm_out_file);
4344     }
4345 }
4346
4347 /* This routine will generate the correct assembly data for a location
4348    description based on a cfi entry with a complex address.  */
4349
4350 static void
4351 output_cfa_loc (dw_cfi_ref cfi)
4352 {
4353   dw_loc_descr_ref loc;
4354   unsigned long size;
4355
4356   if (cfi->dw_cfi_opc == DW_CFA_expression)
4357     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4358
4359   /* Output the size of the block.  */
4360   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4361   size = size_of_locs (loc);
4362   dw2_asm_output_data_uleb128 (size, NULL);
4363
4364   /* Now output the operations themselves.  */
4365   output_loc_sequence (loc);
4366 }
4367
4368 /* Similar, but used for .cfi_escape.  */
4369
4370 static void
4371 output_cfa_loc_raw (dw_cfi_ref cfi)
4372 {
4373   dw_loc_descr_ref loc;
4374   unsigned long size;
4375
4376   if (cfi->dw_cfi_opc == DW_CFA_expression)
4377     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4378
4379   /* Output the size of the block.  */
4380   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4381   size = size_of_locs (loc);
4382   dw2_asm_output_data_uleb128_raw (size);
4383   fputc (',', asm_out_file);
4384
4385   /* Now output the operations themselves.  */
4386   output_loc_sequence_raw (loc);
4387 }
4388
4389 /* This function builds a dwarf location descriptor sequence from a
4390    dw_cfa_location, adding the given OFFSET to the result of the
4391    expression.  */
4392
4393 static struct dw_loc_descr_struct *
4394 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4395 {
4396   struct dw_loc_descr_struct *head, *tmp;
4397
4398   offset += cfa->offset;
4399
4400   if (cfa->indirect)
4401     {
4402       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4403       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4404       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4405       add_loc_descr (&head, tmp);
4406       if (offset != 0)
4407         {
4408           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4409           add_loc_descr (&head, tmp);
4410         }
4411     }
4412   else
4413     head = new_reg_loc_descr (cfa->reg, offset);
4414
4415   return head;
4416 }
4417
4418 /* This function builds a dwarf location descriptor sequence for
4419    the address at OFFSET from the CFA when stack is aligned to
4420    ALIGNMENT byte.  */
4421
4422 static struct dw_loc_descr_struct *
4423 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4424 {
4425   struct dw_loc_descr_struct *head;
4426   unsigned int dwarf_fp
4427     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4428
4429  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4430   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4431     {
4432       head = new_reg_loc_descr (dwarf_fp, 0);
4433       add_loc_descr (&head, int_loc_descriptor (alignment));
4434       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4435       loc_descr_plus_const (&head, offset);
4436     }
4437   else
4438     head = new_reg_loc_descr (dwarf_fp, offset);
4439   return head;
4440 }
4441
4442 /* This function fills in aa dw_cfa_location structure from a dwarf location
4443    descriptor sequence.  */
4444
4445 static void
4446 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4447 {
4448   struct dw_loc_descr_struct *ptr;
4449   cfa->offset = 0;
4450   cfa->base_offset = 0;
4451   cfa->indirect = 0;
4452   cfa->reg = -1;
4453
4454   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4455     {
4456       enum dwarf_location_atom op = ptr->dw_loc_opc;
4457
4458       switch (op)
4459         {
4460         case DW_OP_reg0:
4461         case DW_OP_reg1:
4462         case DW_OP_reg2:
4463         case DW_OP_reg3:
4464         case DW_OP_reg4:
4465         case DW_OP_reg5:
4466         case DW_OP_reg6:
4467         case DW_OP_reg7:
4468         case DW_OP_reg8:
4469         case DW_OP_reg9:
4470         case DW_OP_reg10:
4471         case DW_OP_reg11:
4472         case DW_OP_reg12:
4473         case DW_OP_reg13:
4474         case DW_OP_reg14:
4475         case DW_OP_reg15:
4476         case DW_OP_reg16:
4477         case DW_OP_reg17:
4478         case DW_OP_reg18:
4479         case DW_OP_reg19:
4480         case DW_OP_reg20:
4481         case DW_OP_reg21:
4482         case DW_OP_reg22:
4483         case DW_OP_reg23:
4484         case DW_OP_reg24:
4485         case DW_OP_reg25:
4486         case DW_OP_reg26:
4487         case DW_OP_reg27:
4488         case DW_OP_reg28:
4489         case DW_OP_reg29:
4490         case DW_OP_reg30:
4491         case DW_OP_reg31:
4492           cfa->reg = op - DW_OP_reg0;
4493           break;
4494         case DW_OP_regx:
4495           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4496           break;
4497         case DW_OP_breg0:
4498         case DW_OP_breg1:
4499         case DW_OP_breg2:
4500         case DW_OP_breg3:
4501         case DW_OP_breg4:
4502         case DW_OP_breg5:
4503         case DW_OP_breg6:
4504         case DW_OP_breg7:
4505         case DW_OP_breg8:
4506         case DW_OP_breg9:
4507         case DW_OP_breg10:
4508         case DW_OP_breg11:
4509         case DW_OP_breg12:
4510         case DW_OP_breg13:
4511         case DW_OP_breg14:
4512         case DW_OP_breg15:
4513         case DW_OP_breg16:
4514         case DW_OP_breg17:
4515         case DW_OP_breg18:
4516         case DW_OP_breg19:
4517         case DW_OP_breg20:
4518         case DW_OP_breg21:
4519         case DW_OP_breg22:
4520         case DW_OP_breg23:
4521         case DW_OP_breg24:
4522         case DW_OP_breg25:
4523         case DW_OP_breg26:
4524         case DW_OP_breg27:
4525         case DW_OP_breg28:
4526         case DW_OP_breg29:
4527         case DW_OP_breg30:
4528         case DW_OP_breg31:
4529           cfa->reg = op - DW_OP_breg0;
4530           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4531           break;
4532         case DW_OP_bregx:
4533           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4534           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4535           break;
4536         case DW_OP_deref:
4537           cfa->indirect = 1;
4538           break;
4539         case DW_OP_plus_uconst:
4540           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4541           break;
4542         default:
4543           internal_error ("DW_LOC_OP %s not implemented",
4544                           dwarf_stack_op_name (ptr->dw_loc_opc));
4545         }
4546     }
4547 }
4548 #endif /* .debug_frame support */
4549 \f
4550 /* And now, the support for symbolic debugging information.  */
4551 #ifdef DWARF2_DEBUGGING_INFO
4552
4553 /* .debug_str support.  */
4554 static int output_indirect_string (void **, void *);
4555
4556 static void dwarf2out_init (const char *);
4557 static void dwarf2out_finish (const char *);
4558 static void dwarf2out_define (unsigned int, const char *);
4559 static void dwarf2out_undef (unsigned int, const char *);
4560 static void dwarf2out_start_source_file (unsigned, const char *);
4561 static void dwarf2out_end_source_file (unsigned);
4562 static void dwarf2out_begin_block (unsigned, unsigned);
4563 static void dwarf2out_end_block (unsigned, unsigned);
4564 static bool dwarf2out_ignore_block (const_tree);
4565 static void dwarf2out_global_decl (tree);
4566 static void dwarf2out_type_decl (tree, int);
4567 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4568 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4569                                                  dw_die_ref);
4570 static void dwarf2out_abstract_function (tree);
4571 static void dwarf2out_var_location (rtx);
4572 static void dwarf2out_begin_function (tree);
4573 static void dwarf2out_set_name (tree, tree);
4574
4575 /* The debug hooks structure.  */
4576
4577 const struct gcc_debug_hooks dwarf2_debug_hooks =
4578 {
4579   dwarf2out_init,
4580   dwarf2out_finish,
4581   dwarf2out_define,
4582   dwarf2out_undef,
4583   dwarf2out_start_source_file,
4584   dwarf2out_end_source_file,
4585   dwarf2out_begin_block,
4586   dwarf2out_end_block,
4587   dwarf2out_ignore_block,
4588   dwarf2out_source_line,
4589   dwarf2out_begin_prologue,
4590   debug_nothing_int_charstar,   /* end_prologue */
4591   dwarf2out_end_epilogue,
4592   dwarf2out_begin_function,
4593   debug_nothing_int,            /* end_function */
4594   dwarf2out_decl,               /* function_decl */
4595   dwarf2out_global_decl,
4596   dwarf2out_type_decl,          /* type_decl */
4597   dwarf2out_imported_module_or_decl,
4598   debug_nothing_tree,           /* deferred_inline_function */
4599   /* The DWARF 2 backend tries to reduce debugging bloat by not
4600      emitting the abstract description of inline functions until
4601      something tries to reference them.  */
4602   dwarf2out_abstract_function,  /* outlining_inline_function */
4603   debug_nothing_rtx,            /* label */
4604   debug_nothing_int,            /* handle_pch */
4605   dwarf2out_var_location,
4606   dwarf2out_switch_text_section,
4607   dwarf2out_set_name,
4608   1                             /* start_end_main_source_file */
4609 };
4610 #endif
4611 \f
4612 /* NOTE: In the comments in this file, many references are made to
4613    "Debugging Information Entries".  This term is abbreviated as `DIE'
4614    throughout the remainder of this file.  */
4615
4616 /* An internal representation of the DWARF output is built, and then
4617    walked to generate the DWARF debugging info.  The walk of the internal
4618    representation is done after the entire program has been compiled.
4619    The types below are used to describe the internal representation.  */
4620
4621 /* Various DIE's use offsets relative to the beginning of the
4622    .debug_info section to refer to each other.  */
4623
4624 typedef long int dw_offset;
4625
4626 /* Define typedefs here to avoid circular dependencies.  */
4627
4628 typedef struct dw_attr_struct *dw_attr_ref;
4629 typedef struct dw_line_info_struct *dw_line_info_ref;
4630 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4631 typedef struct pubname_struct *pubname_ref;
4632 typedef struct dw_ranges_struct *dw_ranges_ref;
4633 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4634
4635 /* Each entry in the line_info_table maintains the file and
4636    line number associated with the label generated for that
4637    entry.  The label gives the PC value associated with
4638    the line number entry.  */
4639
4640 typedef struct GTY(()) dw_line_info_struct {
4641   unsigned long dw_file_num;
4642   unsigned long dw_line_num;
4643 }
4644 dw_line_info_entry;
4645
4646 /* Line information for functions in separate sections; each one gets its
4647    own sequence.  */
4648 typedef struct GTY(()) dw_separate_line_info_struct {
4649   unsigned long dw_file_num;
4650   unsigned long dw_line_num;
4651   unsigned long function;
4652 }
4653 dw_separate_line_info_entry;
4654
4655 /* Each DIE attribute has a field specifying the attribute kind,
4656    a link to the next attribute in the chain, and an attribute value.
4657    Attributes are typically linked below the DIE they modify.  */
4658
4659 typedef struct GTY(()) dw_attr_struct {
4660   enum dwarf_attribute dw_attr;
4661   dw_val_node dw_attr_val;
4662 }
4663 dw_attr_node;
4664
4665 DEF_VEC_O(dw_attr_node);
4666 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4667
4668 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4669    The children of each node form a circular list linked by
4670    die_sib.  die_child points to the node *before* the "first" child node.  */
4671
4672 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
4673   enum dwarf_tag die_tag;
4674   char *die_symbol;
4675   VEC(dw_attr_node,gc) * die_attr;
4676   dw_die_ref die_parent;
4677   dw_die_ref die_child;
4678   dw_die_ref die_sib;
4679   dw_die_ref die_definition; /* ref from a specification to its definition */
4680   dw_offset die_offset;
4681   unsigned long die_abbrev;
4682   int die_mark;
4683   /* Die is used and must not be pruned as unused.  */
4684   int die_perennial_p;
4685   unsigned int decl_id;
4686 }
4687 die_node;
4688
4689 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4690 #define FOR_EACH_CHILD(die, c, expr) do {       \
4691   c = die->die_child;                           \
4692   if (c) do {                                   \
4693     c = c->die_sib;                             \
4694     expr;                                       \
4695   } while (c != die->die_child);                \
4696 } while (0)
4697
4698 /* The pubname structure */
4699
4700 typedef struct GTY(()) pubname_struct {
4701   dw_die_ref die;
4702   const char *name;
4703 }
4704 pubname_entry;
4705
4706 DEF_VEC_O(pubname_entry);
4707 DEF_VEC_ALLOC_O(pubname_entry, gc);
4708
4709 struct GTY(()) dw_ranges_struct {
4710   /* If this is positive, it's a block number, otherwise it's a
4711      bitwise-negated index into dw_ranges_by_label.  */
4712   int num;
4713 };
4714
4715 struct GTY(()) dw_ranges_by_label_struct {
4716   const char *begin;
4717   const char *end;
4718 };
4719
4720 /* The limbo die list structure.  */
4721 typedef struct GTY(()) limbo_die_struct {
4722   dw_die_ref die;
4723   tree created_for;
4724   struct limbo_die_struct *next;
4725 }
4726 limbo_die_node;
4727
4728 /* How to start an assembler comment.  */
4729 #ifndef ASM_COMMENT_START
4730 #define ASM_COMMENT_START ";#"
4731 #endif
4732
4733 /* Define a macro which returns nonzero for a TYPE_DECL which was
4734    implicitly generated for a tagged type.
4735
4736    Note that unlike the gcc front end (which generates a NULL named
4737    TYPE_DECL node for each complete tagged type, each array type, and
4738    each function type node created) the g++ front end generates a
4739    _named_ TYPE_DECL node for each tagged type node created.
4740    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4741    generate a DW_TAG_typedef DIE for them.  */
4742
4743 #define TYPE_DECL_IS_STUB(decl)                         \
4744   (DECL_NAME (decl) == NULL_TREE                        \
4745    || (DECL_ARTIFICIAL (decl)                           \
4746        && is_tagged_type (TREE_TYPE (decl))             \
4747        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4748            /* This is necessary for stub decls that     \
4749               appear in nested inline functions.  */    \
4750            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4751                && (decl_ultimate_origin (decl)          \
4752                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4753
4754 /* Information concerning the compilation unit's programming
4755    language, and compiler version.  */
4756
4757 /* Fixed size portion of the DWARF compilation unit header.  */
4758 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4759   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4760
4761 /* Fixed size portion of public names info.  */
4762 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4763
4764 /* Fixed size portion of the address range info.  */
4765 #define DWARF_ARANGES_HEADER_SIZE                                       \
4766   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4767                 DWARF2_ADDR_SIZE * 2)                                   \
4768    - DWARF_INITIAL_LENGTH_SIZE)
4769
4770 /* Size of padding portion in the address range info.  It must be
4771    aligned to twice the pointer size.  */
4772 #define DWARF_ARANGES_PAD_SIZE \
4773   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4774                 DWARF2_ADDR_SIZE * 2)                              \
4775    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4776
4777 /* Use assembler line directives if available.  */
4778 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4779 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4780 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4781 #else
4782 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4783 #endif
4784 #endif
4785
4786 /* Minimum line offset in a special line info. opcode.
4787    This value was chosen to give a reasonable range of values.  */
4788 #define DWARF_LINE_BASE  -10
4789
4790 /* First special line opcode - leave room for the standard opcodes.  */
4791 #define DWARF_LINE_OPCODE_BASE  10
4792
4793 /* Range of line offsets in a special line info. opcode.  */
4794 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4795
4796 /* Flag that indicates the initial value of the is_stmt_start flag.
4797    In the present implementation, we do not mark any lines as
4798    the beginning of a source statement, because that information
4799    is not made available by the GCC front-end.  */
4800 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4801
4802 #ifdef DWARF2_DEBUGGING_INFO
4803 /* This location is used by calc_die_sizes() to keep track
4804    the offset of each DIE within the .debug_info section.  */
4805 static unsigned long next_die_offset;
4806 #endif
4807
4808 /* Record the root of the DIE's built for the current compilation unit.  */
4809 static GTY(()) dw_die_ref comp_unit_die;
4810
4811 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4812 static GTY(()) limbo_die_node *limbo_die_list;
4813
4814 /* Filenames referenced by this compilation unit.  */
4815 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4816
4817 /* A hash table of references to DIE's that describe declarations.
4818    The key is a DECL_UID() which is a unique number identifying each decl.  */
4819 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4820
4821 /* A hash table of references to DIE's that describe COMMON blocks.
4822    The key is DECL_UID() ^ die_parent.  */
4823 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
4824
4825 /* Node of the variable location list.  */
4826 struct GTY ((chain_next ("%h.next"))) var_loc_node {
4827   rtx GTY (()) var_loc_note;
4828   const char * GTY (()) label;
4829   const char * GTY (()) section_label;
4830   struct var_loc_node * GTY (()) next;
4831 };
4832
4833 /* Variable location list.  */
4834 struct GTY (()) var_loc_list_def {
4835   struct var_loc_node * GTY (()) first;
4836
4837   /* Do not mark the last element of the chained list because
4838      it is marked through the chain.  */
4839   struct var_loc_node * GTY ((skip ("%h"))) last;
4840
4841   /* DECL_UID of the variable decl.  */
4842   unsigned int decl_id;
4843 };
4844 typedef struct var_loc_list_def var_loc_list;
4845
4846
4847 /* Table of decl location linked lists.  */
4848 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4849
4850 /* A pointer to the base of a list of references to DIE's that
4851    are uniquely identified by their tag, presence/absence of
4852    children DIE's, and list of attribute/value pairs.  */
4853 static GTY((length ("abbrev_die_table_allocated")))
4854   dw_die_ref *abbrev_die_table;
4855
4856 /* Number of elements currently allocated for abbrev_die_table.  */
4857 static GTY(()) unsigned abbrev_die_table_allocated;
4858
4859 /* Number of elements in type_die_table currently in use.  */
4860 static GTY(()) unsigned abbrev_die_table_in_use;
4861
4862 /* Size (in elements) of increments by which we may expand the
4863    abbrev_die_table.  */
4864 #define ABBREV_DIE_TABLE_INCREMENT 256
4865
4866 /* A pointer to the base of a table that contains line information
4867    for each source code line in .text in the compilation unit.  */
4868 static GTY((length ("line_info_table_allocated")))
4869      dw_line_info_ref line_info_table;
4870
4871 /* Number of elements currently allocated for line_info_table.  */
4872 static GTY(()) unsigned line_info_table_allocated;
4873
4874 /* Number of elements in line_info_table currently in use.  */
4875 static GTY(()) unsigned line_info_table_in_use;
4876
4877 /* A pointer to the base of a table that contains line information
4878    for each source code line outside of .text in the compilation unit.  */
4879 static GTY ((length ("separate_line_info_table_allocated")))
4880      dw_separate_line_info_ref separate_line_info_table;
4881
4882 /* Number of elements currently allocated for separate_line_info_table.  */
4883 static GTY(()) unsigned separate_line_info_table_allocated;
4884
4885 /* Number of elements in separate_line_info_table currently in use.  */
4886 static GTY(()) unsigned separate_line_info_table_in_use;
4887
4888 /* Size (in elements) of increments by which we may expand the
4889    line_info_table.  */
4890 #define LINE_INFO_TABLE_INCREMENT 1024
4891
4892 /* A pointer to the base of a table that contains a list of publicly
4893    accessible names.  */
4894 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4895
4896 /* A pointer to the base of a table that contains a list of publicly
4897    accessible types.  */
4898 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4899
4900 /* Array of dies for which we should generate .debug_arange info.  */
4901 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4902
4903 /* Number of elements currently allocated for arange_table.  */
4904 static GTY(()) unsigned arange_table_allocated;
4905
4906 /* Number of elements in arange_table currently in use.  */
4907 static GTY(()) unsigned arange_table_in_use;
4908
4909 /* Size (in elements) of increments by which we may expand the
4910    arange_table.  */
4911 #define ARANGE_TABLE_INCREMENT 64
4912
4913 /* Array of dies for which we should generate .debug_ranges info.  */
4914 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4915
4916 /* Number of elements currently allocated for ranges_table.  */
4917 static GTY(()) unsigned ranges_table_allocated;
4918
4919 /* Number of elements in ranges_table currently in use.  */
4920 static GTY(()) unsigned ranges_table_in_use;
4921
4922 /* Array of pairs of labels referenced in ranges_table.  */
4923 static GTY ((length ("ranges_by_label_allocated")))
4924      dw_ranges_by_label_ref ranges_by_label;
4925
4926 /* Number of elements currently allocated for ranges_by_label.  */
4927 static GTY(()) unsigned ranges_by_label_allocated;
4928
4929 /* Number of elements in ranges_by_label currently in use.  */
4930 static GTY(()) unsigned ranges_by_label_in_use;
4931
4932 /* Size (in elements) of increments by which we may expand the
4933    ranges_table.  */
4934 #define RANGES_TABLE_INCREMENT 64
4935
4936 /* Whether we have location lists that need outputting */
4937 static GTY(()) bool have_location_lists;
4938
4939 /* Unique label counter.  */
4940 static GTY(()) unsigned int loclabel_num;
4941
4942 #ifdef DWARF2_DEBUGGING_INFO
4943 /* Record whether the function being analyzed contains inlined functions.  */
4944 static int current_function_has_inlines;
4945 #endif
4946 #if 0 && defined (MIPS_DEBUGGING_INFO)
4947 static int comp_unit_has_inlines;
4948 #endif
4949
4950 /* The last file entry emitted by maybe_emit_file().  */
4951 static GTY(()) struct dwarf_file_data * last_emitted_file;
4952
4953 /* Number of internal labels generated by gen_internal_sym().  */
4954 static GTY(()) int label_num;
4955
4956 /* Cached result of previous call to lookup_filename.  */
4957 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4958
4959 #ifdef DWARF2_DEBUGGING_INFO
4960
4961 /* Offset from the "steady-state frame pointer" to the frame base,
4962    within the current function.  */
4963 static HOST_WIDE_INT frame_pointer_fb_offset;
4964
4965 /* Forward declarations for functions defined in this file.  */
4966
4967 static int is_pseudo_reg (const_rtx);
4968 static tree type_main_variant (tree);
4969 static int is_tagged_type (const_tree);
4970 static const char *dwarf_tag_name (unsigned);
4971 static const char *dwarf_attr_name (unsigned);
4972 static const char *dwarf_form_name (unsigned);
4973 static tree decl_ultimate_origin (const_tree);
4974 static tree decl_class_context (tree);
4975 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4976 static inline enum dw_val_class AT_class (dw_attr_ref);
4977 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4978 static inline unsigned AT_flag (dw_attr_ref);
4979 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4980 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4981 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4982 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4983 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4984                               unsigned long);
4985 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4986                                unsigned int, unsigned char *);
4987 static hashval_t debug_str_do_hash (const void *);
4988 static int debug_str_eq (const void *, const void *);
4989 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4990 static inline const char *AT_string (dw_attr_ref);
4991 static enum dwarf_form AT_string_form (dw_attr_ref);
4992 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4993 static void add_AT_specification (dw_die_ref, dw_die_ref);
4994 static inline dw_die_ref AT_ref (dw_attr_ref);
4995 static inline int AT_ref_external (dw_attr_ref);
4996 static inline void set_AT_ref_external (dw_attr_ref, int);
4997 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4998 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4999 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5000 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5001                              dw_loc_list_ref);
5002 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5003 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5004 static inline rtx AT_addr (dw_attr_ref);
5005 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5006 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5007 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5008 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5009                            unsigned HOST_WIDE_INT);
5010 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5011                                unsigned long);
5012 static inline const char *AT_lbl (dw_attr_ref);
5013 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5014 static const char *get_AT_low_pc (dw_die_ref);
5015 static const char *get_AT_hi_pc (dw_die_ref);
5016 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5017 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5018 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5019 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5020 static bool is_c_family (void);
5021 static bool is_cxx (void);
5022 static bool is_java (void);
5023 static bool is_fortran (void);
5024 static bool is_ada (void);
5025 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5026 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5027 static void add_child_die (dw_die_ref, dw_die_ref);
5028 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5029 static dw_die_ref lookup_type_die (tree);
5030 static void equate_type_number_to_die (tree, dw_die_ref);
5031 static hashval_t decl_die_table_hash (const void *);
5032 static int decl_die_table_eq (const void *, const void *);
5033 static dw_die_ref lookup_decl_die (tree);
5034 static hashval_t common_block_die_table_hash (const void *);
5035 static int common_block_die_table_eq (const void *, const void *);
5036 static hashval_t decl_loc_table_hash (const void *);
5037 static int decl_loc_table_eq (const void *, const void *);
5038 static var_loc_list *lookup_decl_loc (const_tree);
5039 static void equate_decl_number_to_die (tree, dw_die_ref);
5040 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5041 static void print_spaces (FILE *);
5042 static void print_die (dw_die_ref, FILE *);
5043 static void print_dwarf_line_table (FILE *);
5044 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5045 static dw_die_ref pop_compile_unit (dw_die_ref);
5046 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5047 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5048 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5049 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5050 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5051 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5052 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5053 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5054 static void compute_section_prefix (dw_die_ref);
5055 static int is_type_die (dw_die_ref);
5056 static int is_comdat_die (dw_die_ref);
5057 static int is_symbol_die (dw_die_ref);
5058 static void assign_symbol_names (dw_die_ref);
5059 static void break_out_includes (dw_die_ref);
5060 static hashval_t htab_cu_hash (const void *);
5061 static int htab_cu_eq (const void *, const void *);
5062 static void htab_cu_del (void *);
5063 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5064 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5065 static void add_sibling_attributes (dw_die_ref);
5066 static void build_abbrev_table (dw_die_ref);
5067 static void output_location_lists (dw_die_ref);
5068 static int constant_size (unsigned HOST_WIDE_INT);
5069 static unsigned long size_of_die (dw_die_ref);
5070 static void calc_die_sizes (dw_die_ref);
5071 static void mark_dies (dw_die_ref);
5072 static void unmark_dies (dw_die_ref);
5073 static void unmark_all_dies (dw_die_ref);
5074 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5075 static unsigned long size_of_aranges (void);
5076 static enum dwarf_form value_format (dw_attr_ref);
5077 static void output_value_format (dw_attr_ref);
5078 static void output_abbrev_section (void);
5079 static void output_die_symbol (dw_die_ref);
5080 static void output_die (dw_die_ref);
5081 static void output_compilation_unit_header (void);
5082 static void output_comp_unit (dw_die_ref, int);
5083 static const char *dwarf2_name (tree, int);
5084 static void add_pubname (tree, dw_die_ref);
5085 static void add_pubname_string (const char *, dw_die_ref);
5086 static void add_pubtype (tree, dw_die_ref);
5087 static void output_pubnames (VEC (pubname_entry,gc) *);
5088 static void add_arange (tree, dw_die_ref);
5089 static void output_aranges (void);
5090 static unsigned int add_ranges_num (int);
5091 static unsigned int add_ranges (const_tree);
5092 static unsigned int add_ranges_by_labels (const char *, const char *);
5093 static void output_ranges (void);
5094 static void output_line_info (void);
5095 static void output_file_names (void);
5096 static dw_die_ref base_type_die (tree);
5097 static int is_base_type (tree);
5098 static bool is_subrange_type (const_tree);
5099 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5100 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5101 static int type_is_enum (const_tree);
5102 static unsigned int dbx_reg_number (const_rtx);
5103 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5104 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5105 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5106                                                 enum var_init_status);
5107 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5108                                                      enum var_init_status);
5109 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5110                                          enum var_init_status);
5111 static int is_based_loc (const_rtx);
5112 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5113                                             enum var_init_status);
5114 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5115                                                enum var_init_status);
5116 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5117 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5118 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5119 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5120 static tree field_type (const_tree);
5121 static unsigned int simple_type_align_in_bits (const_tree);
5122 static unsigned int simple_decl_align_in_bits (const_tree);
5123 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5124 static HOST_WIDE_INT field_byte_offset (const_tree);
5125 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5126                                          dw_loc_descr_ref);
5127 static void add_data_member_location_attribute (dw_die_ref, tree);
5128 static void add_const_value_attribute (dw_die_ref, rtx);
5129 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5130 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5131 static void insert_float (const_rtx, unsigned char *);
5132 static rtx rtl_for_decl_location (tree);
5133 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5134                                                    enum dwarf_attribute);
5135 static void tree_add_const_value_attribute (dw_die_ref, tree);
5136 static void add_name_attribute (dw_die_ref, const char *);
5137 static void add_comp_dir_attribute (dw_die_ref);
5138 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5139 static void add_subscript_info (dw_die_ref, tree, bool);
5140 static void add_byte_size_attribute (dw_die_ref, tree);
5141 static void add_bit_offset_attribute (dw_die_ref, tree);
5142 static void add_bit_size_attribute (dw_die_ref, tree);
5143 static void add_prototyped_attribute (dw_die_ref, tree);
5144 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5145 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5146 static void add_src_coords_attributes (dw_die_ref, tree);
5147 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5148 static void push_decl_scope (tree);
5149 static void pop_decl_scope (void);
5150 static dw_die_ref scope_die_for (tree, dw_die_ref);
5151 static inline int local_scope_p (dw_die_ref);
5152 static inline int class_scope_p (dw_die_ref);
5153 static inline int class_or_namespace_scope_p (dw_die_ref);
5154 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5155 static void add_calling_convention_attribute (dw_die_ref, tree);
5156 static const char *type_tag (const_tree);
5157 static tree member_declared_type (const_tree);
5158 #if 0
5159 static const char *decl_start_label (tree);
5160 #endif
5161 static void gen_array_type_die (tree, dw_die_ref);
5162 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5163 #if 0
5164 static void gen_entry_point_die (tree, dw_die_ref);
5165 #endif
5166 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5167 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5168 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5169 static void gen_formal_types_die (tree, dw_die_ref);
5170 static void gen_subprogram_die (tree, dw_die_ref);
5171 static void gen_variable_die (tree, tree, dw_die_ref);
5172 static void gen_const_die (tree, dw_die_ref);
5173 static void gen_label_die (tree, dw_die_ref);
5174 static void gen_lexical_block_die (tree, dw_die_ref, int);
5175 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5176 static void gen_field_die (tree, dw_die_ref);
5177 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5178 static dw_die_ref gen_compile_unit_die (const char *);
5179 static void gen_inheritance_die (tree, tree, dw_die_ref);
5180 static void gen_member_die (tree, dw_die_ref);
5181 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5182                                                 enum debug_info_usage);
5183 static void gen_subroutine_type_die (tree, dw_die_ref);
5184 static void gen_typedef_die (tree, dw_die_ref);
5185 static void gen_type_die (tree, dw_die_ref);
5186 static void gen_block_die (tree, dw_die_ref, int);
5187 static void decls_for_scope (tree, dw_die_ref, int);
5188 static int is_redundant_typedef (const_tree);
5189 static void gen_namespace_die (tree, dw_die_ref);
5190 static void gen_decl_die (tree, tree, dw_die_ref);
5191 static dw_die_ref force_decl_die (tree);
5192 static dw_die_ref force_type_die (tree);
5193 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5194 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5195 static struct dwarf_file_data * lookup_filename (const char *);
5196 static void retry_incomplete_types (void);
5197 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5198 static void splice_child_die (dw_die_ref, dw_die_ref);
5199 static int file_info_cmp (const void *, const void *);
5200 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5201                                      const char *, const char *, unsigned);
5202 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5203                                        const char *, const char *,
5204                                        const char *);
5205 static void output_loc_list (dw_loc_list_ref);
5206 static char *gen_internal_sym (const char *);
5207
5208 static void prune_unmark_dies (dw_die_ref);
5209 static void prune_unused_types_mark (dw_die_ref, int);
5210 static void prune_unused_types_walk (dw_die_ref);
5211 static void prune_unused_types_walk_attribs (dw_die_ref);
5212 static void prune_unused_types_prune (dw_die_ref);
5213 static void prune_unused_types (void);
5214 static int maybe_emit_file (struct dwarf_file_data *fd);
5215
5216 /* Section names used to hold DWARF debugging information.  */
5217 #ifndef DEBUG_INFO_SECTION
5218 #define DEBUG_INFO_SECTION      ".debug_info"
5219 #endif
5220 #ifndef DEBUG_ABBREV_SECTION
5221 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5222 #endif
5223 #ifndef DEBUG_ARANGES_SECTION
5224 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5225 #endif
5226 #ifndef DEBUG_MACINFO_SECTION
5227 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5228 #endif
5229 #ifndef DEBUG_LINE_SECTION
5230 #define DEBUG_LINE_SECTION      ".debug_line"
5231 #endif
5232 #ifndef DEBUG_LOC_SECTION
5233 #define DEBUG_LOC_SECTION       ".debug_loc"
5234 #endif
5235 #ifndef DEBUG_PUBNAMES_SECTION
5236 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5237 #endif
5238 #ifndef DEBUG_STR_SECTION
5239 #define DEBUG_STR_SECTION       ".debug_str"
5240 #endif
5241 #ifndef DEBUG_RANGES_SECTION
5242 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5243 #endif
5244
5245 /* Standard ELF section names for compiled code and data.  */
5246 #ifndef TEXT_SECTION_NAME
5247 #define TEXT_SECTION_NAME       ".text"
5248 #endif
5249
5250 /* Section flags for .debug_str section.  */
5251 #define DEBUG_STR_SECTION_FLAGS \
5252   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5253    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5254    : SECTION_DEBUG)
5255
5256 /* Labels we insert at beginning sections we can reference instead of
5257    the section names themselves.  */
5258
5259 #ifndef TEXT_SECTION_LABEL
5260 #define TEXT_SECTION_LABEL              "Ltext"
5261 #endif
5262 #ifndef COLD_TEXT_SECTION_LABEL
5263 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5264 #endif
5265 #ifndef DEBUG_LINE_SECTION_LABEL
5266 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5267 #endif
5268 #ifndef DEBUG_INFO_SECTION_LABEL
5269 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5270 #endif
5271 #ifndef DEBUG_ABBREV_SECTION_LABEL
5272 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5273 #endif
5274 #ifndef DEBUG_LOC_SECTION_LABEL
5275 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5276 #endif
5277 #ifndef DEBUG_RANGES_SECTION_LABEL
5278 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5279 #endif
5280 #ifndef DEBUG_MACINFO_SECTION_LABEL
5281 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5282 #endif
5283
5284 /* Definitions of defaults for formats and names of various special
5285    (artificial) labels which may be generated within this file (when the -g
5286    options is used and DWARF2_DEBUGGING_INFO is in effect.
5287    If necessary, these may be overridden from within the tm.h file, but
5288    typically, overriding these defaults is unnecessary.  */
5289
5290 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5291 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5292 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5293 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5294 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5295 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5296 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5297 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5298 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5299 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5300
5301 #ifndef TEXT_END_LABEL
5302 #define TEXT_END_LABEL          "Letext"
5303 #endif
5304 #ifndef COLD_END_LABEL
5305 #define COLD_END_LABEL          "Letext_cold"
5306 #endif
5307 #ifndef BLOCK_BEGIN_LABEL
5308 #define BLOCK_BEGIN_LABEL       "LBB"
5309 #endif
5310 #ifndef BLOCK_END_LABEL
5311 #define BLOCK_END_LABEL         "LBE"
5312 #endif
5313 #ifndef LINE_CODE_LABEL
5314 #define LINE_CODE_LABEL         "LM"
5315 #endif
5316 #ifndef SEPARATE_LINE_CODE_LABEL
5317 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5318 #endif
5319
5320 \f
5321 /* We allow a language front-end to designate a function that is to be
5322    called to "demangle" any name before it is put into a DIE.  */
5323
5324 static const char *(*demangle_name_func) (const char *);
5325
5326 void
5327 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5328 {
5329   demangle_name_func = func;
5330 }
5331
5332 /* Test if rtl node points to a pseudo register.  */
5333
5334 static inline int
5335 is_pseudo_reg (const_rtx rtl)
5336 {
5337   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5338           || (GET_CODE (rtl) == SUBREG
5339               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5340 }
5341
5342 /* Return a reference to a type, with its const and volatile qualifiers
5343    removed.  */
5344
5345 static inline tree
5346 type_main_variant (tree type)
5347 {
5348   type = TYPE_MAIN_VARIANT (type);
5349
5350   /* ??? There really should be only one main variant among any group of
5351      variants of a given type (and all of the MAIN_VARIANT values for all
5352      members of the group should point to that one type) but sometimes the C
5353      front-end messes this up for array types, so we work around that bug
5354      here.  */
5355   if (TREE_CODE (type) == ARRAY_TYPE)
5356     while (type != TYPE_MAIN_VARIANT (type))
5357       type = TYPE_MAIN_VARIANT (type);
5358
5359   return type;
5360 }
5361
5362 /* Return nonzero if the given type node represents a tagged type.  */
5363
5364 static inline int
5365 is_tagged_type (const_tree type)
5366 {
5367   enum tree_code code = TREE_CODE (type);
5368
5369   return (code == RECORD_TYPE || code == UNION_TYPE
5370           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5371 }
5372
5373 /* Convert a DIE tag into its string name.  */
5374
5375 static const char *
5376 dwarf_tag_name (unsigned int tag)
5377 {
5378   switch (tag)
5379     {
5380     case DW_TAG_padding:
5381       return "DW_TAG_padding";
5382     case DW_TAG_array_type:
5383       return "DW_TAG_array_type";
5384     case DW_TAG_class_type:
5385       return "DW_TAG_class_type";
5386     case DW_TAG_entry_point:
5387       return "DW_TAG_entry_point";
5388     case DW_TAG_enumeration_type:
5389       return "DW_TAG_enumeration_type";
5390     case DW_TAG_formal_parameter:
5391       return "DW_TAG_formal_parameter";
5392     case DW_TAG_imported_declaration:
5393       return "DW_TAG_imported_declaration";
5394     case DW_TAG_label:
5395       return "DW_TAG_label";
5396     case DW_TAG_lexical_block:
5397       return "DW_TAG_lexical_block";
5398     case DW_TAG_member:
5399       return "DW_TAG_member";
5400     case DW_TAG_pointer_type:
5401       return "DW_TAG_pointer_type";
5402     case DW_TAG_reference_type:
5403       return "DW_TAG_reference_type";
5404     case DW_TAG_compile_unit:
5405       return "DW_TAG_compile_unit";
5406     case DW_TAG_string_type:
5407       return "DW_TAG_string_type";
5408     case DW_TAG_structure_type:
5409       return "DW_TAG_structure_type";
5410     case DW_TAG_subroutine_type:
5411       return "DW_TAG_subroutine_type";
5412     case DW_TAG_typedef:
5413       return "DW_TAG_typedef";
5414     case DW_TAG_union_type:
5415       return "DW_TAG_union_type";
5416     case DW_TAG_unspecified_parameters:
5417       return "DW_TAG_unspecified_parameters";
5418     case DW_TAG_variant:
5419       return "DW_TAG_variant";
5420     case DW_TAG_common_block:
5421       return "DW_TAG_common_block";
5422     case DW_TAG_common_inclusion:
5423       return "DW_TAG_common_inclusion";
5424     case DW_TAG_inheritance:
5425       return "DW_TAG_inheritance";
5426     case DW_TAG_inlined_subroutine:
5427       return "DW_TAG_inlined_subroutine";
5428     case DW_TAG_module:
5429       return "DW_TAG_module";
5430     case DW_TAG_ptr_to_member_type:
5431       return "DW_TAG_ptr_to_member_type";
5432     case DW_TAG_set_type:
5433       return "DW_TAG_set_type";
5434     case DW_TAG_subrange_type:
5435       return "DW_TAG_subrange_type";
5436     case DW_TAG_with_stmt:
5437       return "DW_TAG_with_stmt";
5438     case DW_TAG_access_declaration:
5439       return "DW_TAG_access_declaration";
5440     case DW_TAG_base_type:
5441       return "DW_TAG_base_type";
5442     case DW_TAG_catch_block:
5443       return "DW_TAG_catch_block";
5444     case DW_TAG_const_type:
5445       return "DW_TAG_const_type";
5446     case DW_TAG_constant:
5447       return "DW_TAG_constant";
5448     case DW_TAG_enumerator:
5449       return "DW_TAG_enumerator";
5450     case DW_TAG_file_type:
5451       return "DW_TAG_file_type";
5452     case DW_TAG_friend:
5453       return "DW_TAG_friend";
5454     case DW_TAG_namelist:
5455       return "DW_TAG_namelist";
5456     case DW_TAG_namelist_item:
5457       return "DW_TAG_namelist_item";
5458     case DW_TAG_packed_type:
5459       return "DW_TAG_packed_type";
5460     case DW_TAG_subprogram:
5461       return "DW_TAG_subprogram";
5462     case DW_TAG_template_type_param:
5463       return "DW_TAG_template_type_param";
5464     case DW_TAG_template_value_param:
5465       return "DW_TAG_template_value_param";
5466     case DW_TAG_thrown_type:
5467       return "DW_TAG_thrown_type";
5468     case DW_TAG_try_block:
5469       return "DW_TAG_try_block";
5470     case DW_TAG_variant_part:
5471       return "DW_TAG_variant_part";
5472     case DW_TAG_variable:
5473       return "DW_TAG_variable";
5474     case DW_TAG_volatile_type:
5475       return "DW_TAG_volatile_type";
5476     case DW_TAG_dwarf_procedure:
5477       return "DW_TAG_dwarf_procedure";
5478     case DW_TAG_restrict_type:
5479       return "DW_TAG_restrict_type";
5480     case DW_TAG_interface_type:
5481       return "DW_TAG_interface_type";
5482     case DW_TAG_namespace:
5483       return "DW_TAG_namespace";
5484     case DW_TAG_imported_module:
5485       return "DW_TAG_imported_module";
5486     case DW_TAG_unspecified_type:
5487       return "DW_TAG_unspecified_type";
5488     case DW_TAG_partial_unit:
5489       return "DW_TAG_partial_unit";
5490     case DW_TAG_imported_unit:
5491       return "DW_TAG_imported_unit";
5492     case DW_TAG_condition:
5493       return "DW_TAG_condition";
5494     case DW_TAG_shared_type:
5495       return "DW_TAG_shared_type";
5496     case DW_TAG_MIPS_loop:
5497       return "DW_TAG_MIPS_loop";
5498     case DW_TAG_format_label:
5499       return "DW_TAG_format_label";
5500     case DW_TAG_function_template:
5501       return "DW_TAG_function_template";
5502     case DW_TAG_class_template:
5503       return "DW_TAG_class_template";
5504     case DW_TAG_GNU_BINCL:
5505       return "DW_TAG_GNU_BINCL";
5506     case DW_TAG_GNU_EINCL:
5507       return "DW_TAG_GNU_EINCL";
5508     default:
5509       return "DW_TAG_<unknown>";
5510     }
5511 }
5512
5513 /* Convert a DWARF attribute code into its string name.  */
5514
5515 static const char *
5516 dwarf_attr_name (unsigned int attr)
5517 {
5518   switch (attr)
5519     {
5520     case DW_AT_sibling:
5521       return "DW_AT_sibling";
5522     case DW_AT_location:
5523       return "DW_AT_location";
5524     case DW_AT_name:
5525       return "DW_AT_name";
5526     case DW_AT_ordering:
5527       return "DW_AT_ordering";
5528     case DW_AT_subscr_data:
5529       return "DW_AT_subscr_data";
5530     case DW_AT_byte_size:
5531       return "DW_AT_byte_size";
5532     case DW_AT_bit_offset:
5533       return "DW_AT_bit_offset";
5534     case DW_AT_bit_size:
5535       return "DW_AT_bit_size";
5536     case DW_AT_element_list:
5537       return "DW_AT_element_list";
5538     case DW_AT_stmt_list:
5539       return "DW_AT_stmt_list";
5540     case DW_AT_low_pc:
5541       return "DW_AT_low_pc";
5542     case DW_AT_high_pc:
5543       return "DW_AT_high_pc";
5544     case DW_AT_language:
5545       return "DW_AT_language";
5546     case DW_AT_member:
5547       return "DW_AT_member";
5548     case DW_AT_discr:
5549       return "DW_AT_discr";
5550     case DW_AT_discr_value:
5551       return "DW_AT_discr_value";
5552     case DW_AT_visibility:
5553       return "DW_AT_visibility";
5554     case DW_AT_import:
5555       return "DW_AT_import";
5556     case DW_AT_string_length:
5557       return "DW_AT_string_length";
5558     case DW_AT_common_reference:
5559       return "DW_AT_common_reference";
5560     case DW_AT_comp_dir:
5561       return "DW_AT_comp_dir";
5562     case DW_AT_const_value:
5563       return "DW_AT_const_value";
5564     case DW_AT_containing_type:
5565       return "DW_AT_containing_type";
5566     case DW_AT_default_value:
5567       return "DW_AT_default_value";
5568     case DW_AT_inline:
5569       return "DW_AT_inline";
5570     case DW_AT_is_optional:
5571       return "DW_AT_is_optional";
5572     case DW_AT_lower_bound:
5573       return "DW_AT_lower_bound";
5574     case DW_AT_producer:
5575       return "DW_AT_producer";
5576     case DW_AT_prototyped:
5577       return "DW_AT_prototyped";
5578     case DW_AT_return_addr:
5579       return "DW_AT_return_addr";
5580     case DW_AT_start_scope:
5581       return "DW_AT_start_scope";
5582     case DW_AT_bit_stride:
5583       return "DW_AT_bit_stride";
5584     case DW_AT_upper_bound:
5585       return "DW_AT_upper_bound";
5586     case DW_AT_abstract_origin:
5587       return "DW_AT_abstract_origin";
5588     case DW_AT_accessibility:
5589       return "DW_AT_accessibility";
5590     case DW_AT_address_class:
5591       return "DW_AT_address_class";
5592     case DW_AT_artificial:
5593       return "DW_AT_artificial";
5594     case DW_AT_base_types:
5595       return "DW_AT_base_types";
5596     case DW_AT_calling_convention:
5597       return "DW_AT_calling_convention";
5598     case DW_AT_count:
5599       return "DW_AT_count";
5600     case DW_AT_data_member_location:
5601       return "DW_AT_data_member_location";
5602     case DW_AT_decl_column:
5603       return "DW_AT_decl_column";
5604     case DW_AT_decl_file:
5605       return "DW_AT_decl_file";
5606     case DW_AT_decl_line:
5607       return "DW_AT_decl_line";
5608     case DW_AT_declaration:
5609       return "DW_AT_declaration";
5610     case DW_AT_discr_list:
5611       return "DW_AT_discr_list";
5612     case DW_AT_encoding:
5613       return "DW_AT_encoding";
5614     case DW_AT_external:
5615       return "DW_AT_external";
5616     case DW_AT_explicit:
5617       return "DW_AT_explicit";
5618     case DW_AT_frame_base:
5619       return "DW_AT_frame_base";
5620     case DW_AT_friend:
5621       return "DW_AT_friend";
5622     case DW_AT_identifier_case:
5623       return "DW_AT_identifier_case";
5624     case DW_AT_macro_info:
5625       return "DW_AT_macro_info";
5626     case DW_AT_namelist_items:
5627       return "DW_AT_namelist_items";
5628     case DW_AT_priority:
5629       return "DW_AT_priority";
5630     case DW_AT_segment:
5631       return "DW_AT_segment";
5632     case DW_AT_specification:
5633       return "DW_AT_specification";
5634     case DW_AT_static_link:
5635       return "DW_AT_static_link";
5636     case DW_AT_type:
5637       return "DW_AT_type";
5638     case DW_AT_use_location:
5639       return "DW_AT_use_location";
5640     case DW_AT_variable_parameter:
5641       return "DW_AT_variable_parameter";
5642     case DW_AT_virtuality:
5643       return "DW_AT_virtuality";
5644     case DW_AT_vtable_elem_location:
5645       return "DW_AT_vtable_elem_location";
5646
5647     case DW_AT_allocated:
5648       return "DW_AT_allocated";
5649     case DW_AT_associated:
5650       return "DW_AT_associated";
5651     case DW_AT_data_location:
5652       return "DW_AT_data_location";
5653     case DW_AT_byte_stride:
5654       return "DW_AT_byte_stride";
5655     case DW_AT_entry_pc:
5656       return "DW_AT_entry_pc";
5657     case DW_AT_use_UTF8:
5658       return "DW_AT_use_UTF8";
5659     case DW_AT_extension:
5660       return "DW_AT_extension";
5661     case DW_AT_ranges:
5662       return "DW_AT_ranges";
5663     case DW_AT_trampoline:
5664       return "DW_AT_trampoline";
5665     case DW_AT_call_column:
5666       return "DW_AT_call_column";
5667     case DW_AT_call_file:
5668       return "DW_AT_call_file";
5669     case DW_AT_call_line:
5670       return "DW_AT_call_line";
5671
5672     case DW_AT_MIPS_fde:
5673       return "DW_AT_MIPS_fde";
5674     case DW_AT_MIPS_loop_begin:
5675       return "DW_AT_MIPS_loop_begin";
5676     case DW_AT_MIPS_tail_loop_begin:
5677       return "DW_AT_MIPS_tail_loop_begin";
5678     case DW_AT_MIPS_epilog_begin:
5679       return "DW_AT_MIPS_epilog_begin";
5680     case DW_AT_MIPS_loop_unroll_factor:
5681       return "DW_AT_MIPS_loop_unroll_factor";
5682     case DW_AT_MIPS_software_pipeline_depth:
5683       return "DW_AT_MIPS_software_pipeline_depth";
5684     case DW_AT_MIPS_linkage_name:
5685       return "DW_AT_MIPS_linkage_name";
5686     case DW_AT_MIPS_stride:
5687       return "DW_AT_MIPS_stride";
5688     case DW_AT_MIPS_abstract_name:
5689       return "DW_AT_MIPS_abstract_name";
5690     case DW_AT_MIPS_clone_origin:
5691       return "DW_AT_MIPS_clone_origin";
5692     case DW_AT_MIPS_has_inlines:
5693       return "DW_AT_MIPS_has_inlines";
5694
5695     case DW_AT_sf_names:
5696       return "DW_AT_sf_names";
5697     case DW_AT_src_info:
5698       return "DW_AT_src_info";
5699     case DW_AT_mac_info:
5700       return "DW_AT_mac_info";
5701     case DW_AT_src_coords:
5702       return "DW_AT_src_coords";
5703     case DW_AT_body_begin:
5704       return "DW_AT_body_begin";
5705     case DW_AT_body_end:
5706       return "DW_AT_body_end";
5707     case DW_AT_GNU_vector:
5708       return "DW_AT_GNU_vector";
5709
5710     case DW_AT_VMS_rtnbeg_pd_address:
5711       return "DW_AT_VMS_rtnbeg_pd_address";
5712
5713     default:
5714       return "DW_AT_<unknown>";
5715     }
5716 }
5717
5718 /* Convert a DWARF value form code into its string name.  */
5719
5720 static const char *
5721 dwarf_form_name (unsigned int form)
5722 {
5723   switch (form)
5724     {
5725     case DW_FORM_addr:
5726       return "DW_FORM_addr";
5727     case DW_FORM_block2:
5728       return "DW_FORM_block2";
5729     case DW_FORM_block4:
5730       return "DW_FORM_block4";
5731     case DW_FORM_data2:
5732       return "DW_FORM_data2";
5733     case DW_FORM_data4:
5734       return "DW_FORM_data4";
5735     case DW_FORM_data8:
5736       return "DW_FORM_data8";
5737     case DW_FORM_string:
5738       return "DW_FORM_string";
5739     case DW_FORM_block:
5740       return "DW_FORM_block";
5741     case DW_FORM_block1:
5742       return "DW_FORM_block1";
5743     case DW_FORM_data1:
5744       return "DW_FORM_data1";
5745     case DW_FORM_flag:
5746       return "DW_FORM_flag";
5747     case DW_FORM_sdata:
5748       return "DW_FORM_sdata";
5749     case DW_FORM_strp:
5750       return "DW_FORM_strp";
5751     case DW_FORM_udata:
5752       return "DW_FORM_udata";
5753     case DW_FORM_ref_addr:
5754       return "DW_FORM_ref_addr";
5755     case DW_FORM_ref1:
5756       return "DW_FORM_ref1";
5757     case DW_FORM_ref2:
5758       return "DW_FORM_ref2";
5759     case DW_FORM_ref4:
5760       return "DW_FORM_ref4";
5761     case DW_FORM_ref8:
5762       return "DW_FORM_ref8";
5763     case DW_FORM_ref_udata:
5764       return "DW_FORM_ref_udata";
5765     case DW_FORM_indirect:
5766       return "DW_FORM_indirect";
5767     default:
5768       return "DW_FORM_<unknown>";
5769     }
5770 }
5771 \f
5772 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5773    instance of an inlined instance of a decl which is local to an inline
5774    function, so we have to trace all of the way back through the origin chain
5775    to find out what sort of node actually served as the original seed for the
5776    given block.  */
5777
5778 static tree
5779 decl_ultimate_origin (const_tree decl)
5780 {
5781   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5782     return NULL_TREE;
5783
5784   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5785      nodes in the function to point to themselves; ignore that if
5786      we're trying to output the abstract instance of this function.  */
5787   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5788     return NULL_TREE;
5789
5790   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5791      most distant ancestor, this should never happen.  */
5792   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5793
5794   return DECL_ABSTRACT_ORIGIN (decl);
5795 }
5796
5797 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5798    of a virtual function may refer to a base class, so we check the 'this'
5799    parameter.  */
5800
5801 static tree
5802 decl_class_context (tree decl)
5803 {
5804   tree context = NULL_TREE;
5805
5806   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5807     context = DECL_CONTEXT (decl);
5808   else
5809     context = TYPE_MAIN_VARIANT
5810       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5811
5812   if (context && !TYPE_P (context))
5813     context = NULL_TREE;
5814
5815   return context;
5816 }
5817 \f
5818 /* Add an attribute/value pair to a DIE.  */
5819
5820 static inline void
5821 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5822 {
5823   /* Maybe this should be an assert?  */
5824   if (die == NULL)
5825     return;
5826
5827   if (die->die_attr == NULL)
5828     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5829   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5830 }
5831
5832 static inline enum dw_val_class
5833 AT_class (dw_attr_ref a)
5834 {
5835   return a->dw_attr_val.val_class;
5836 }
5837
5838 /* Add a flag value attribute to a DIE.  */
5839
5840 static inline void
5841 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5842 {
5843   dw_attr_node attr;
5844
5845   attr.dw_attr = attr_kind;
5846   attr.dw_attr_val.val_class = dw_val_class_flag;
5847   attr.dw_attr_val.v.val_flag = flag;
5848   add_dwarf_attr (die, &attr);
5849 }
5850
5851 static inline unsigned
5852 AT_flag (dw_attr_ref a)
5853 {
5854   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5855   return a->dw_attr_val.v.val_flag;
5856 }
5857
5858 /* Add a signed integer attribute value to a DIE.  */
5859
5860 static inline void
5861 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5862 {
5863   dw_attr_node attr;
5864
5865   attr.dw_attr = attr_kind;
5866   attr.dw_attr_val.val_class = dw_val_class_const;
5867   attr.dw_attr_val.v.val_int = int_val;
5868   add_dwarf_attr (die, &attr);
5869 }
5870
5871 static inline HOST_WIDE_INT
5872 AT_int (dw_attr_ref a)
5873 {
5874   gcc_assert (a && AT_class (a) == dw_val_class_const);
5875   return a->dw_attr_val.v.val_int;
5876 }
5877
5878 /* Add an unsigned integer attribute value to a DIE.  */
5879
5880 static inline void
5881 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5882                  unsigned HOST_WIDE_INT unsigned_val)
5883 {
5884   dw_attr_node attr;
5885
5886   attr.dw_attr = attr_kind;
5887   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5888   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5889   add_dwarf_attr (die, &attr);
5890 }
5891
5892 static inline unsigned HOST_WIDE_INT
5893 AT_unsigned (dw_attr_ref a)
5894 {
5895   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5896   return a->dw_attr_val.v.val_unsigned;
5897 }
5898
5899 /* Add an unsigned double integer attribute value to a DIE.  */
5900
5901 static inline void
5902 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5903                   long unsigned int val_hi, long unsigned int val_low)
5904 {
5905   dw_attr_node attr;
5906
5907   attr.dw_attr = attr_kind;
5908   attr.dw_attr_val.val_class = dw_val_class_long_long;
5909   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5910   attr.dw_attr_val.v.val_long_long.low = val_low;
5911   add_dwarf_attr (die, &attr);
5912 }
5913
5914 /* Add a floating point attribute value to a DIE and return it.  */
5915
5916 static inline void
5917 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5918             unsigned int length, unsigned int elt_size, unsigned char *array)
5919 {
5920   dw_attr_node attr;
5921
5922   attr.dw_attr = attr_kind;
5923   attr.dw_attr_val.val_class = dw_val_class_vec;
5924   attr.dw_attr_val.v.val_vec.length = length;
5925   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5926   attr.dw_attr_val.v.val_vec.array = array;
5927   add_dwarf_attr (die, &attr);
5928 }
5929
5930 /* Hash and equality functions for debug_str_hash.  */
5931
5932 static hashval_t
5933 debug_str_do_hash (const void *x)
5934 {
5935   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5936 }
5937
5938 static int
5939 debug_str_eq (const void *x1, const void *x2)
5940 {
5941   return strcmp ((((const struct indirect_string_node *)x1)->str),
5942                  (const char *)x2) == 0;
5943 }
5944
5945 static struct indirect_string_node *
5946 find_AT_string (const char *str)
5947 {
5948   struct indirect_string_node *node;
5949   void **slot;
5950
5951   if (! debug_str_hash)
5952     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5953                                       debug_str_eq, NULL);
5954
5955   slot = htab_find_slot_with_hash (debug_str_hash, str,
5956                                    htab_hash_string (str), INSERT);
5957   if (*slot == NULL)
5958     {
5959       node = (struct indirect_string_node *)
5960                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5961       node->str = ggc_strdup (str);
5962       *slot = node;
5963     }
5964   else
5965     node = (struct indirect_string_node *) *slot;
5966
5967   node->refcount++;
5968   return node;
5969 }
5970
5971 /* Add a string attribute value to a DIE.  */
5972
5973 static inline void
5974 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5975 {
5976   dw_attr_node attr;
5977   struct indirect_string_node *node;
5978
5979   node = find_AT_string (str);
5980
5981   attr.dw_attr = attr_kind;
5982   attr.dw_attr_val.val_class = dw_val_class_str;
5983   attr.dw_attr_val.v.val_str = node;
5984   add_dwarf_attr (die, &attr);
5985 }
5986
5987 static inline const char *
5988 AT_string (dw_attr_ref a)
5989 {
5990   gcc_assert (a && AT_class (a) == dw_val_class_str);
5991   return a->dw_attr_val.v.val_str->str;
5992 }
5993
5994 /* Find out whether a string should be output inline in DIE
5995    or out-of-line in .debug_str section.  */
5996
5997 static enum dwarf_form
5998 AT_string_form (dw_attr_ref a)
5999 {
6000   struct indirect_string_node *node;
6001   unsigned int len;
6002   char label[32];
6003
6004   gcc_assert (a && AT_class (a) == dw_val_class_str);
6005
6006   node = a->dw_attr_val.v.val_str;
6007   if (node->form)
6008     return node->form;
6009
6010   len = strlen (node->str) + 1;
6011
6012   /* If the string is shorter or equal to the size of the reference, it is
6013      always better to put it inline.  */
6014   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6015     return node->form = DW_FORM_string;
6016
6017   /* If we cannot expect the linker to merge strings in .debug_str
6018      section, only put it into .debug_str if it is worth even in this
6019      single module.  */
6020   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6021       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6022     return node->form = DW_FORM_string;
6023
6024   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6025   ++dw2_string_counter;
6026   node->label = xstrdup (label);
6027
6028   return node->form = DW_FORM_strp;
6029 }
6030
6031 /* Add a DIE reference attribute value to a DIE.  */
6032
6033 static inline void
6034 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6035 {
6036   dw_attr_node attr;
6037
6038   attr.dw_attr = attr_kind;
6039   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6040   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6041   attr.dw_attr_val.v.val_die_ref.external = 0;
6042   add_dwarf_attr (die, &attr);
6043 }
6044
6045 /* Add an AT_specification attribute to a DIE, and also make the back
6046    pointer from the specification to the definition.  */
6047
6048 static inline void
6049 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6050 {
6051   add_AT_die_ref (die, DW_AT_specification, targ_die);
6052   gcc_assert (!targ_die->die_definition);
6053   targ_die->die_definition = die;
6054 }
6055
6056 static inline dw_die_ref
6057 AT_ref (dw_attr_ref a)
6058 {
6059   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6060   return a->dw_attr_val.v.val_die_ref.die;
6061 }
6062
6063 static inline int
6064 AT_ref_external (dw_attr_ref a)
6065 {
6066   if (a && AT_class (a) == dw_val_class_die_ref)
6067     return a->dw_attr_val.v.val_die_ref.external;
6068
6069   return 0;
6070 }
6071
6072 static inline void
6073 set_AT_ref_external (dw_attr_ref a, int i)
6074 {
6075   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6076   a->dw_attr_val.v.val_die_ref.external = i;
6077 }
6078
6079 /* Add an FDE reference attribute value to a DIE.  */
6080
6081 static inline void
6082 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6083 {
6084   dw_attr_node attr;
6085
6086   attr.dw_attr = attr_kind;
6087   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6088   attr.dw_attr_val.v.val_fde_index = targ_fde;
6089   add_dwarf_attr (die, &attr);
6090 }
6091
6092 /* Add a location description attribute value to a DIE.  */
6093
6094 static inline void
6095 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6096 {
6097   dw_attr_node attr;
6098
6099   attr.dw_attr = attr_kind;
6100   attr.dw_attr_val.val_class = dw_val_class_loc;
6101   attr.dw_attr_val.v.val_loc = loc;
6102   add_dwarf_attr (die, &attr);
6103 }
6104
6105 static inline dw_loc_descr_ref
6106 AT_loc (dw_attr_ref a)
6107 {
6108   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6109   return a->dw_attr_val.v.val_loc;
6110 }
6111
6112 static inline void
6113 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6114 {
6115   dw_attr_node attr;
6116
6117   attr.dw_attr = attr_kind;
6118   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6119   attr.dw_attr_val.v.val_loc_list = loc_list;
6120   add_dwarf_attr (die, &attr);
6121   have_location_lists = true;
6122 }
6123
6124 static inline dw_loc_list_ref
6125 AT_loc_list (dw_attr_ref a)
6126 {
6127   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6128   return a->dw_attr_val.v.val_loc_list;
6129 }
6130
6131 /* Add an address constant attribute value to a DIE.  */
6132
6133 static inline void
6134 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6135 {
6136   dw_attr_node attr;
6137
6138   attr.dw_attr = attr_kind;
6139   attr.dw_attr_val.val_class = dw_val_class_addr;
6140   attr.dw_attr_val.v.val_addr = addr;
6141   add_dwarf_attr (die, &attr);
6142 }
6143
6144 /* Get the RTX from to an address DIE attribute.  */
6145
6146 static inline rtx
6147 AT_addr (dw_attr_ref a)
6148 {
6149   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6150   return a->dw_attr_val.v.val_addr;
6151 }
6152
6153 /* Add a file attribute value to a DIE.  */
6154
6155 static inline void
6156 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6157              struct dwarf_file_data *fd)
6158 {
6159   dw_attr_node attr;
6160
6161   attr.dw_attr = attr_kind;
6162   attr.dw_attr_val.val_class = dw_val_class_file;
6163   attr.dw_attr_val.v.val_file = fd;
6164   add_dwarf_attr (die, &attr);
6165 }
6166
6167 /* Get the dwarf_file_data from a file DIE attribute.  */
6168
6169 static inline struct dwarf_file_data *
6170 AT_file (dw_attr_ref a)
6171 {
6172   gcc_assert (a && AT_class (a) == dw_val_class_file);
6173   return a->dw_attr_val.v.val_file;
6174 }
6175
6176 /* Add a label identifier attribute value to a DIE.  */
6177
6178 static inline void
6179 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6180 {
6181   dw_attr_node attr;
6182
6183   attr.dw_attr = attr_kind;
6184   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6185   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6186   add_dwarf_attr (die, &attr);
6187 }
6188
6189 /* Add a section offset attribute value to a DIE, an offset into the
6190    debug_line section.  */
6191
6192 static inline void
6193 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6194                 const char *label)
6195 {
6196   dw_attr_node attr;
6197
6198   attr.dw_attr = attr_kind;
6199   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6200   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6201   add_dwarf_attr (die, &attr);
6202 }
6203
6204 /* Add a section offset attribute value to a DIE, an offset into the
6205    debug_macinfo section.  */
6206
6207 static inline void
6208 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6209                const char *label)
6210 {
6211   dw_attr_node attr;
6212
6213   attr.dw_attr = attr_kind;
6214   attr.dw_attr_val.val_class = dw_val_class_macptr;
6215   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6216   add_dwarf_attr (die, &attr);
6217 }
6218
6219 /* Add an offset attribute value to a DIE.  */
6220
6221 static inline void
6222 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6223                unsigned HOST_WIDE_INT offset)
6224 {
6225   dw_attr_node attr;
6226
6227   attr.dw_attr = attr_kind;
6228   attr.dw_attr_val.val_class = dw_val_class_offset;
6229   attr.dw_attr_val.v.val_offset = offset;
6230   add_dwarf_attr (die, &attr);
6231 }
6232
6233 /* Add an range_list attribute value to a DIE.  */
6234
6235 static void
6236 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6237                    long unsigned int offset)
6238 {
6239   dw_attr_node attr;
6240
6241   attr.dw_attr = attr_kind;
6242   attr.dw_attr_val.val_class = dw_val_class_range_list;
6243   attr.dw_attr_val.v.val_offset = offset;
6244   add_dwarf_attr (die, &attr);
6245 }
6246
6247 static inline const char *
6248 AT_lbl (dw_attr_ref a)
6249 {
6250   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6251                     || AT_class (a) == dw_val_class_lineptr
6252                     || AT_class (a) == dw_val_class_macptr));
6253   return a->dw_attr_val.v.val_lbl_id;
6254 }
6255
6256 /* Get the attribute of type attr_kind.  */
6257
6258 static dw_attr_ref
6259 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6260 {
6261   dw_attr_ref a;
6262   unsigned ix;
6263   dw_die_ref spec = NULL;
6264
6265   if (! die)
6266     return NULL;
6267
6268   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6269     if (a->dw_attr == attr_kind)
6270       return a;
6271     else if (a->dw_attr == DW_AT_specification
6272              || a->dw_attr == DW_AT_abstract_origin)
6273       spec = AT_ref (a);
6274
6275   if (spec)
6276     return get_AT (spec, attr_kind);
6277
6278   return NULL;
6279 }
6280
6281 /* Return the "low pc" attribute value, typically associated with a subprogram
6282    DIE.  Return null if the "low pc" attribute is either not present, or if it
6283    cannot be represented as an assembler label identifier.  */
6284
6285 static inline const char *
6286 get_AT_low_pc (dw_die_ref die)
6287 {
6288   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6289
6290   return a ? AT_lbl (a) : NULL;
6291 }
6292
6293 /* Return the "high pc" attribute value, typically associated with a subprogram
6294    DIE.  Return null if the "high pc" attribute is either not present, or if it
6295    cannot be represented as an assembler label identifier.  */
6296
6297 static inline const char *
6298 get_AT_hi_pc (dw_die_ref die)
6299 {
6300   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6301
6302   return a ? AT_lbl (a) : NULL;
6303 }
6304
6305 /* Return the value of the string attribute designated by ATTR_KIND, or
6306    NULL if it is not present.  */
6307
6308 static inline const char *
6309 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6310 {
6311   dw_attr_ref a = get_AT (die, attr_kind);
6312
6313   return a ? AT_string (a) : NULL;
6314 }
6315
6316 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6317    if it is not present.  */
6318
6319 static inline int
6320 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6321 {
6322   dw_attr_ref a = get_AT (die, attr_kind);
6323
6324   return a ? AT_flag (a) : 0;
6325 }
6326
6327 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6328    if it is not present.  */
6329
6330 static inline unsigned
6331 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6332 {
6333   dw_attr_ref a = get_AT (die, attr_kind);
6334
6335   return a ? AT_unsigned (a) : 0;
6336 }
6337
6338 static inline dw_die_ref
6339 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6340 {
6341   dw_attr_ref a = get_AT (die, attr_kind);
6342
6343   return a ? AT_ref (a) : NULL;
6344 }
6345
6346 static inline struct dwarf_file_data *
6347 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6348 {
6349   dw_attr_ref a = get_AT (die, attr_kind);
6350
6351   return a ? AT_file (a) : NULL;
6352 }
6353
6354 /* Return TRUE if the language is C or C++.  */
6355
6356 static inline bool
6357 is_c_family (void)
6358 {
6359   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6360
6361   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6362           || lang == DW_LANG_C99
6363           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6364 }
6365
6366 /* Return TRUE if the language is C++.  */
6367
6368 static inline bool
6369 is_cxx (void)
6370 {
6371   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6372
6373   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6374 }
6375
6376 /* Return TRUE if the language is Fortran.  */
6377
6378 static inline bool
6379 is_fortran (void)
6380 {
6381   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6382
6383   return (lang == DW_LANG_Fortran77
6384           || lang == DW_LANG_Fortran90
6385           || lang == DW_LANG_Fortran95);
6386 }
6387
6388 /* Return TRUE if the language is Java.  */
6389
6390 static inline bool
6391 is_java (void)
6392 {
6393   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6394
6395   return lang == DW_LANG_Java;
6396 }
6397
6398 /* Return TRUE if the language is Ada.  */
6399
6400 static inline bool
6401 is_ada (void)
6402 {
6403   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6404
6405   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6406 }
6407
6408 /* Remove the specified attribute if present.  */
6409
6410 static void
6411 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6412 {
6413   dw_attr_ref a;
6414   unsigned ix;
6415
6416   if (! die)
6417     return;
6418
6419   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6420     if (a->dw_attr == attr_kind)
6421       {
6422         if (AT_class (a) == dw_val_class_str)
6423           if (a->dw_attr_val.v.val_str->refcount)
6424             a->dw_attr_val.v.val_str->refcount--;
6425
6426         /* VEC_ordered_remove should help reduce the number of abbrevs
6427            that are needed.  */
6428         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6429         return;
6430       }
6431 }
6432
6433 /* Remove CHILD from its parent.  PREV must have the property that
6434    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6435
6436 static void
6437 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6438 {
6439   gcc_assert (child->die_parent == prev->die_parent);
6440   gcc_assert (prev->die_sib == child);
6441   if (prev == child)
6442     {
6443       gcc_assert (child->die_parent->die_child == child);
6444       prev = NULL;
6445     }
6446   else
6447     prev->die_sib = child->die_sib;
6448   if (child->die_parent->die_child == child)
6449     child->die_parent->die_child = prev;
6450 }
6451
6452 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6453    matches TAG.  */
6454
6455 static void
6456 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6457 {
6458   dw_die_ref c;
6459
6460   c = die->die_child;
6461   if (c) do {
6462     dw_die_ref prev = c;
6463     c = c->die_sib;
6464     while (c->die_tag == tag)
6465       {
6466         remove_child_with_prev (c, prev);
6467         /* Might have removed every child.  */
6468         if (c == c->die_sib)
6469           return;
6470         c = c->die_sib;
6471       }
6472   } while (c != die->die_child);
6473 }
6474
6475 /* Add a CHILD_DIE as the last child of DIE.  */
6476
6477 static void
6478 add_child_die (dw_die_ref die, dw_die_ref child_die)
6479 {
6480   /* FIXME this should probably be an assert.  */
6481   if (! die || ! child_die)
6482     return;
6483   gcc_assert (die != child_die);
6484
6485   child_die->die_parent = die;
6486   if (die->die_child)
6487     {
6488       child_die->die_sib = die->die_child->die_sib;
6489       die->die_child->die_sib = child_die;
6490     }
6491   else
6492     child_die->die_sib = child_die;
6493   die->die_child = child_die;
6494 }
6495
6496 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6497    is the specification, to the end of PARENT's list of children.
6498    This is done by removing and re-adding it.  */
6499
6500 static void
6501 splice_child_die (dw_die_ref parent, dw_die_ref child)
6502 {
6503   dw_die_ref p;
6504
6505   /* We want the declaration DIE from inside the class, not the
6506      specification DIE at toplevel.  */
6507   if (child->die_parent != parent)
6508     {
6509       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6510
6511       if (tmp)
6512         child = tmp;
6513     }
6514
6515   gcc_assert (child->die_parent == parent
6516               || (child->die_parent
6517                   == get_AT_ref (parent, DW_AT_specification)));
6518
6519   for (p = child->die_parent->die_child; ; p = p->die_sib)
6520     if (p->die_sib == child)
6521       {
6522         remove_child_with_prev (child, p);
6523         break;
6524       }
6525
6526   add_child_die (parent, child);
6527 }
6528
6529 /* Return a pointer to a newly created DIE node.  */
6530
6531 static inline dw_die_ref
6532 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6533 {
6534   dw_die_ref die = GGC_CNEW (die_node);
6535
6536   die->die_tag = tag_value;
6537
6538   if (parent_die != NULL)
6539     add_child_die (parent_die, die);
6540   else
6541     {
6542       limbo_die_node *limbo_node;
6543
6544       limbo_node = GGC_CNEW (limbo_die_node);
6545       limbo_node->die = die;
6546       limbo_node->created_for = t;
6547       limbo_node->next = limbo_die_list;
6548       limbo_die_list = limbo_node;
6549     }
6550
6551   return die;
6552 }
6553
6554 /* Return the DIE associated with the given type specifier.  */
6555
6556 static inline dw_die_ref
6557 lookup_type_die (tree type)
6558 {
6559   return TYPE_SYMTAB_DIE (type);
6560 }
6561
6562 /* Equate a DIE to a given type specifier.  */
6563
6564 static inline void
6565 equate_type_number_to_die (tree type, dw_die_ref type_die)
6566 {
6567   TYPE_SYMTAB_DIE (type) = type_die;
6568 }
6569
6570 /* Returns a hash value for X (which really is a die_struct).  */
6571
6572 static hashval_t
6573 decl_die_table_hash (const void *x)
6574 {
6575   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6576 }
6577
6578 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6579
6580 static int
6581 decl_die_table_eq (const void *x, const void *y)
6582 {
6583   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6584 }
6585
6586 /* Return the DIE associated with a given declaration.  */
6587
6588 static inline dw_die_ref
6589 lookup_decl_die (tree decl)
6590 {
6591   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6592 }
6593
6594 /* Returns a hash value for X (which really is a var_loc_list).  */
6595
6596 static hashval_t
6597 decl_loc_table_hash (const void *x)
6598 {
6599   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6600 }
6601
6602 /* Return nonzero if decl_id of var_loc_list X is the same as
6603    UID of decl *Y.  */
6604
6605 static int
6606 decl_loc_table_eq (const void *x, const void *y)
6607 {
6608   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6609 }
6610
6611 /* Return the var_loc list associated with a given declaration.  */
6612
6613 static inline var_loc_list *
6614 lookup_decl_loc (const_tree decl)
6615 {
6616   return (var_loc_list *)
6617     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6618 }
6619
6620 /* Equate a DIE to a particular declaration.  */
6621
6622 static void
6623 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6624 {
6625   unsigned int decl_id = DECL_UID (decl);
6626   void **slot;
6627
6628   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6629   *slot = decl_die;
6630   decl_die->decl_id = decl_id;
6631 }
6632
6633 /* Add a variable location node to the linked list for DECL.  */
6634
6635 static void
6636 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6637 {
6638   unsigned int decl_id = DECL_UID (decl);
6639   var_loc_list *temp;
6640   void **slot;
6641
6642   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6643   if (*slot == NULL)
6644     {
6645       temp = GGC_CNEW (var_loc_list);
6646       temp->decl_id = decl_id;
6647       *slot = temp;
6648     }
6649   else
6650     temp = (var_loc_list *) *slot;
6651
6652   if (temp->last)
6653     {
6654       /* If the current location is the same as the end of the list,
6655          and either both or neither of the locations is uninitialized,
6656          we have nothing to do.  */
6657       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6658                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6659           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6660                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6661               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6662                    == VAR_INIT_STATUS_UNINITIALIZED)
6663                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6664                       == VAR_INIT_STATUS_UNINITIALIZED))))
6665         {
6666           /* Add LOC to the end of list and update LAST.  */
6667           temp->last->next = loc;
6668           temp->last = loc;
6669         }
6670     }
6671   /* Do not add empty location to the beginning of the list.  */
6672   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6673     {
6674       temp->first = loc;
6675       temp->last = loc;
6676     }
6677 }
6678 \f
6679 /* Keep track of the number of spaces used to indent the
6680    output of the debugging routines that print the structure of
6681    the DIE internal representation.  */
6682 static int print_indent;
6683
6684 /* Indent the line the number of spaces given by print_indent.  */
6685
6686 static inline void
6687 print_spaces (FILE *outfile)
6688 {
6689   fprintf (outfile, "%*s", print_indent, "");
6690 }
6691
6692 /* Print the information associated with a given DIE, and its children.
6693    This routine is a debugging aid only.  */
6694
6695 static void
6696 print_die (dw_die_ref die, FILE *outfile)
6697 {
6698   dw_attr_ref a;
6699   dw_die_ref c;
6700   unsigned ix;
6701
6702   print_spaces (outfile);
6703   fprintf (outfile, "DIE %4ld: %s\n",
6704            die->die_offset, dwarf_tag_name (die->die_tag));
6705   print_spaces (outfile);
6706   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6707   fprintf (outfile, " offset: %ld\n", die->die_offset);
6708
6709   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6710     {
6711       print_spaces (outfile);
6712       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6713
6714       switch (AT_class (a))
6715         {
6716         case dw_val_class_addr:
6717           fprintf (outfile, "address");
6718           break;
6719         case dw_val_class_offset:
6720           fprintf (outfile, "offset");
6721           break;
6722         case dw_val_class_loc:
6723           fprintf (outfile, "location descriptor");
6724           break;
6725         case dw_val_class_loc_list:
6726           fprintf (outfile, "location list -> label:%s",
6727                    AT_loc_list (a)->ll_symbol);
6728           break;
6729         case dw_val_class_range_list:
6730           fprintf (outfile, "range list");
6731           break;
6732         case dw_val_class_const:
6733           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6734           break;
6735         case dw_val_class_unsigned_const:
6736           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6737           break;
6738         case dw_val_class_long_long:
6739           fprintf (outfile, "constant (%lu,%lu)",
6740                    a->dw_attr_val.v.val_long_long.hi,
6741                    a->dw_attr_val.v.val_long_long.low);
6742           break;
6743         case dw_val_class_vec:
6744           fprintf (outfile, "floating-point or vector constant");
6745           break;
6746         case dw_val_class_flag:
6747           fprintf (outfile, "%u", AT_flag (a));
6748           break;
6749         case dw_val_class_die_ref:
6750           if (AT_ref (a) != NULL)
6751             {
6752               if (AT_ref (a)->die_symbol)
6753                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6754               else
6755                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6756             }
6757           else
6758             fprintf (outfile, "die -> <null>");
6759           break;
6760         case dw_val_class_lbl_id:
6761         case dw_val_class_lineptr:
6762         case dw_val_class_macptr:
6763           fprintf (outfile, "label: %s", AT_lbl (a));
6764           break;
6765         case dw_val_class_str:
6766           if (AT_string (a) != NULL)
6767             fprintf (outfile, "\"%s\"", AT_string (a));
6768           else
6769             fprintf (outfile, "<null>");
6770           break;
6771         case dw_val_class_file:
6772           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6773                    AT_file (a)->emitted_number);
6774           break;
6775         default:
6776           break;
6777         }
6778
6779       fprintf (outfile, "\n");
6780     }
6781
6782   if (die->die_child != NULL)
6783     {
6784       print_indent += 4;
6785       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6786       print_indent -= 4;
6787     }
6788   if (print_indent == 0)
6789     fprintf (outfile, "\n");
6790 }
6791
6792 /* Print the contents of the source code line number correspondence table.
6793    This routine is a debugging aid only.  */
6794
6795 static void
6796 print_dwarf_line_table (FILE *outfile)
6797 {
6798   unsigned i;
6799   dw_line_info_ref line_info;
6800
6801   fprintf (outfile, "\n\nDWARF source line information\n");
6802   for (i = 1; i < line_info_table_in_use; i++)
6803     {
6804       line_info = &line_info_table[i];
6805       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6806                line_info->dw_file_num,
6807                line_info->dw_line_num);
6808     }
6809
6810   fprintf (outfile, "\n\n");
6811 }
6812
6813 /* Print the information collected for a given DIE.  */
6814
6815 void
6816 debug_dwarf_die (dw_die_ref die)
6817 {
6818   print_die (die, stderr);
6819 }
6820
6821 /* Print all DWARF information collected for the compilation unit.
6822    This routine is a debugging aid only.  */
6823
6824 void
6825 debug_dwarf (void)
6826 {
6827   print_indent = 0;
6828   print_die (comp_unit_die, stderr);
6829   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6830     print_dwarf_line_table (stderr);
6831 }
6832 \f
6833 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6834    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6835    DIE that marks the start of the DIEs for this include file.  */
6836
6837 static dw_die_ref
6838 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6839 {
6840   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6841   dw_die_ref new_unit = gen_compile_unit_die (filename);
6842
6843   new_unit->die_sib = old_unit;
6844   return new_unit;
6845 }
6846
6847 /* Close an include-file CU and reopen the enclosing one.  */
6848
6849 static dw_die_ref
6850 pop_compile_unit (dw_die_ref old_unit)
6851 {
6852   dw_die_ref new_unit = old_unit->die_sib;
6853
6854   old_unit->die_sib = NULL;
6855   return new_unit;
6856 }
6857
6858 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6859 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6860
6861 /* Calculate the checksum of a location expression.  */
6862
6863 static inline void
6864 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6865 {
6866   CHECKSUM (loc->dw_loc_opc);
6867   CHECKSUM (loc->dw_loc_oprnd1);
6868   CHECKSUM (loc->dw_loc_oprnd2);
6869 }
6870
6871 /* Calculate the checksum of an attribute.  */
6872
6873 static void
6874 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6875 {
6876   dw_loc_descr_ref loc;
6877   rtx r;
6878
6879   CHECKSUM (at->dw_attr);
6880
6881   /* We don't care that this was compiled with a different compiler
6882      snapshot; if the output is the same, that's what matters.  */
6883   if (at->dw_attr == DW_AT_producer)
6884     return;
6885
6886   switch (AT_class (at))
6887     {
6888     case dw_val_class_const:
6889       CHECKSUM (at->dw_attr_val.v.val_int);
6890       break;
6891     case dw_val_class_unsigned_const:
6892       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6893       break;
6894     case dw_val_class_long_long:
6895       CHECKSUM (at->dw_attr_val.v.val_long_long);
6896       break;
6897     case dw_val_class_vec:
6898       CHECKSUM (at->dw_attr_val.v.val_vec);
6899       break;
6900     case dw_val_class_flag:
6901       CHECKSUM (at->dw_attr_val.v.val_flag);
6902       break;
6903     case dw_val_class_str:
6904       CHECKSUM_STRING (AT_string (at));
6905       break;
6906
6907     case dw_val_class_addr:
6908       r = AT_addr (at);
6909       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6910       CHECKSUM_STRING (XSTR (r, 0));
6911       break;
6912
6913     case dw_val_class_offset:
6914       CHECKSUM (at->dw_attr_val.v.val_offset);
6915       break;
6916
6917     case dw_val_class_loc:
6918       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6919         loc_checksum (loc, ctx);
6920       break;
6921
6922     case dw_val_class_die_ref:
6923       die_checksum (AT_ref (at), ctx, mark);
6924       break;
6925
6926     case dw_val_class_fde_ref:
6927     case dw_val_class_lbl_id:
6928     case dw_val_class_lineptr:
6929     case dw_val_class_macptr:
6930       break;
6931
6932     case dw_val_class_file:
6933       CHECKSUM_STRING (AT_file (at)->filename);
6934       break;
6935
6936     default:
6937       break;
6938     }
6939 }
6940
6941 /* Calculate the checksum of a DIE.  */
6942
6943 static void
6944 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6945 {
6946   dw_die_ref c;
6947   dw_attr_ref a;
6948   unsigned ix;
6949
6950   /* To avoid infinite recursion.  */
6951   if (die->die_mark)
6952     {
6953       CHECKSUM (die->die_mark);
6954       return;
6955     }
6956   die->die_mark = ++(*mark);
6957
6958   CHECKSUM (die->die_tag);
6959
6960   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6961     attr_checksum (a, ctx, mark);
6962
6963   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6964 }
6965
6966 #undef CHECKSUM
6967 #undef CHECKSUM_STRING
6968
6969 /* Do the location expressions look same?  */
6970 static inline int
6971 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6972 {
6973   return loc1->dw_loc_opc == loc2->dw_loc_opc
6974          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6975          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6976 }
6977
6978 /* Do the values look the same?  */
6979 static int
6980 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6981 {
6982   dw_loc_descr_ref loc1, loc2;
6983   rtx r1, r2;
6984
6985   if (v1->val_class != v2->val_class)
6986     return 0;
6987
6988   switch (v1->val_class)
6989     {
6990     case dw_val_class_const:
6991       return v1->v.val_int == v2->v.val_int;
6992     case dw_val_class_unsigned_const:
6993       return v1->v.val_unsigned == v2->v.val_unsigned;
6994     case dw_val_class_long_long:
6995       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6996              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6997     case dw_val_class_vec:
6998       if (v1->v.val_vec.length != v2->v.val_vec.length
6999           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7000         return 0;
7001       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7002                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7003         return 0;
7004       return 1;
7005     case dw_val_class_flag:
7006       return v1->v.val_flag == v2->v.val_flag;
7007     case dw_val_class_str:
7008       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7009
7010     case dw_val_class_addr:
7011       r1 = v1->v.val_addr;
7012       r2 = v2->v.val_addr;
7013       if (GET_CODE (r1) != GET_CODE (r2))
7014         return 0;
7015       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7016       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7017
7018     case dw_val_class_offset:
7019       return v1->v.val_offset == v2->v.val_offset;
7020
7021     case dw_val_class_loc:
7022       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7023            loc1 && loc2;
7024            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7025         if (!same_loc_p (loc1, loc2, mark))
7026           return 0;
7027       return !loc1 && !loc2;
7028
7029     case dw_val_class_die_ref:
7030       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7031
7032     case dw_val_class_fde_ref:
7033     case dw_val_class_lbl_id:
7034     case dw_val_class_lineptr:
7035     case dw_val_class_macptr:
7036       return 1;
7037
7038     case dw_val_class_file:
7039       return v1->v.val_file == v2->v.val_file;
7040
7041     default:
7042       return 1;
7043     }
7044 }
7045
7046 /* Do the attributes look the same?  */
7047
7048 static int
7049 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7050 {
7051   if (at1->dw_attr != at2->dw_attr)
7052     return 0;
7053
7054   /* We don't care that this was compiled with a different compiler
7055      snapshot; if the output is the same, that's what matters. */
7056   if (at1->dw_attr == DW_AT_producer)
7057     return 1;
7058
7059   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7060 }
7061
7062 /* Do the dies look the same?  */
7063
7064 static int
7065 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7066 {
7067   dw_die_ref c1, c2;
7068   dw_attr_ref a1;
7069   unsigned ix;
7070
7071   /* To avoid infinite recursion.  */
7072   if (die1->die_mark)
7073     return die1->die_mark == die2->die_mark;
7074   die1->die_mark = die2->die_mark = ++(*mark);
7075
7076   if (die1->die_tag != die2->die_tag)
7077     return 0;
7078
7079   if (VEC_length (dw_attr_node, die1->die_attr)
7080       != VEC_length (dw_attr_node, die2->die_attr))
7081     return 0;
7082
7083   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7084     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7085       return 0;
7086
7087   c1 = die1->die_child;
7088   c2 = die2->die_child;
7089   if (! c1)
7090     {
7091       if (c2)
7092         return 0;
7093     }
7094   else
7095     for (;;)
7096       {
7097         if (!same_die_p (c1, c2, mark))
7098           return 0;
7099         c1 = c1->die_sib;
7100         c2 = c2->die_sib;
7101         if (c1 == die1->die_child)
7102           {
7103             if (c2 == die2->die_child)
7104               break;
7105             else
7106               return 0;
7107           }
7108     }
7109
7110   return 1;
7111 }
7112
7113 /* Do the dies look the same?  Wrapper around same_die_p.  */
7114
7115 static int
7116 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7117 {
7118   int mark = 0;
7119   int ret = same_die_p (die1, die2, &mark);
7120
7121   unmark_all_dies (die1);
7122   unmark_all_dies (die2);
7123
7124   return ret;
7125 }
7126
7127 /* The prefix to attach to symbols on DIEs in the current comdat debug
7128    info section.  */
7129 static char *comdat_symbol_id;
7130
7131 /* The index of the current symbol within the current comdat CU.  */
7132 static unsigned int comdat_symbol_number;
7133
7134 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7135    children, and set comdat_symbol_id accordingly.  */
7136
7137 static void
7138 compute_section_prefix (dw_die_ref unit_die)
7139 {
7140   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7141   const char *base = die_name ? lbasename (die_name) : "anonymous";
7142   char *name = XALLOCAVEC (char, strlen (base) + 64);
7143   char *p;
7144   int i, mark;
7145   unsigned char checksum[16];
7146   struct md5_ctx ctx;
7147
7148   /* Compute the checksum of the DIE, then append part of it as hex digits to
7149      the name filename of the unit.  */
7150
7151   md5_init_ctx (&ctx);
7152   mark = 0;
7153   die_checksum (unit_die, &ctx, &mark);
7154   unmark_all_dies (unit_die);
7155   md5_finish_ctx (&ctx, checksum);
7156
7157   sprintf (name, "%s.", base);
7158   clean_symbol_name (name);
7159
7160   p = name + strlen (name);
7161   for (i = 0; i < 4; i++)
7162     {
7163       sprintf (p, "%.2x", checksum[i]);
7164       p += 2;
7165     }
7166
7167   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7168   comdat_symbol_number = 0;
7169 }
7170
7171 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7172
7173 static int
7174 is_type_die (dw_die_ref die)
7175 {
7176   switch (die->die_tag)
7177     {
7178     case DW_TAG_array_type:
7179     case DW_TAG_class_type:
7180     case DW_TAG_interface_type:
7181     case DW_TAG_enumeration_type:
7182     case DW_TAG_pointer_type:
7183     case DW_TAG_reference_type:
7184     case DW_TAG_string_type:
7185     case DW_TAG_structure_type:
7186     case DW_TAG_subroutine_type:
7187     case DW_TAG_union_type:
7188     case DW_TAG_ptr_to_member_type:
7189     case DW_TAG_set_type:
7190     case DW_TAG_subrange_type:
7191     case DW_TAG_base_type:
7192     case DW_TAG_const_type:
7193     case DW_TAG_file_type:
7194     case DW_TAG_packed_type:
7195     case DW_TAG_volatile_type:
7196     case DW_TAG_typedef:
7197       return 1;
7198     default:
7199       return 0;
7200     }
7201 }
7202
7203 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7204    Basically, we want to choose the bits that are likely to be shared between
7205    compilations (types) and leave out the bits that are specific to individual
7206    compilations (functions).  */
7207
7208 static int
7209 is_comdat_die (dw_die_ref c)
7210 {
7211   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7212      we do for stabs.  The advantage is a greater likelihood of sharing between
7213      objects that don't include headers in the same order (and therefore would
7214      put the base types in a different comdat).  jason 8/28/00 */
7215
7216   if (c->die_tag == DW_TAG_base_type)
7217     return 0;
7218
7219   if (c->die_tag == DW_TAG_pointer_type
7220       || c->die_tag == DW_TAG_reference_type
7221       || c->die_tag == DW_TAG_const_type
7222       || c->die_tag == DW_TAG_volatile_type)
7223     {
7224       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7225
7226       return t ? is_comdat_die (t) : 0;
7227     }
7228
7229   return is_type_die (c);
7230 }
7231
7232 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7233    compilation unit.  */
7234
7235 static int
7236 is_symbol_die (dw_die_ref c)
7237 {
7238   return (is_type_die (c)
7239           || (get_AT (c, DW_AT_declaration)
7240               && !get_AT (c, DW_AT_specification))
7241           || c->die_tag == DW_TAG_namespace
7242           || c->die_tag == DW_TAG_module);
7243 }
7244
7245 static char *
7246 gen_internal_sym (const char *prefix)
7247 {
7248   char buf[256];
7249
7250   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7251   return xstrdup (buf);
7252 }
7253
7254 /* Assign symbols to all worthy DIEs under DIE.  */
7255
7256 static void
7257 assign_symbol_names (dw_die_ref die)
7258 {
7259   dw_die_ref c;
7260
7261   if (is_symbol_die (die))
7262     {
7263       if (comdat_symbol_id)
7264         {
7265           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7266
7267           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7268                    comdat_symbol_id, comdat_symbol_number++);
7269           die->die_symbol = xstrdup (p);
7270         }
7271       else
7272         die->die_symbol = gen_internal_sym ("LDIE");
7273     }
7274
7275   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7276 }
7277
7278 struct cu_hash_table_entry
7279 {
7280   dw_die_ref cu;
7281   unsigned min_comdat_num, max_comdat_num;
7282   struct cu_hash_table_entry *next;
7283 };
7284
7285 /* Routines to manipulate hash table of CUs.  */
7286 static hashval_t
7287 htab_cu_hash (const void *of)
7288 {
7289   const struct cu_hash_table_entry *const entry =
7290     (const struct cu_hash_table_entry *) of;
7291
7292   return htab_hash_string (entry->cu->die_symbol);
7293 }
7294
7295 static int
7296 htab_cu_eq (const void *of1, const void *of2)
7297 {
7298   const struct cu_hash_table_entry *const entry1 =
7299     (const struct cu_hash_table_entry *) of1;
7300   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7301
7302   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7303 }
7304
7305 static void
7306 htab_cu_del (void *what)
7307 {
7308   struct cu_hash_table_entry *next,
7309     *entry = (struct cu_hash_table_entry *) what;
7310
7311   while (entry)
7312     {
7313       next = entry->next;
7314       free (entry);
7315       entry = next;
7316     }
7317 }
7318
7319 /* Check whether we have already seen this CU and set up SYM_NUM
7320    accordingly.  */
7321 static int
7322 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7323 {
7324   struct cu_hash_table_entry dummy;
7325   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7326
7327   dummy.max_comdat_num = 0;
7328
7329   slot = (struct cu_hash_table_entry **)
7330     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7331         INSERT);
7332   entry = *slot;
7333
7334   for (; entry; last = entry, entry = entry->next)
7335     {
7336       if (same_die_p_wrap (cu, entry->cu))
7337         break;
7338     }
7339
7340   if (entry)
7341     {
7342       *sym_num = entry->min_comdat_num;
7343       return 1;
7344     }
7345
7346   entry = XCNEW (struct cu_hash_table_entry);
7347   entry->cu = cu;
7348   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7349   entry->next = *slot;
7350   *slot = entry;
7351
7352   return 0;
7353 }
7354
7355 /* Record SYM_NUM to record of CU in HTABLE.  */
7356 static void
7357 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7358 {
7359   struct cu_hash_table_entry **slot, *entry;
7360
7361   slot = (struct cu_hash_table_entry **)
7362     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7363         NO_INSERT);
7364   entry = *slot;
7365
7366   entry->max_comdat_num = sym_num;
7367 }
7368
7369 /* Traverse the DIE (which is always comp_unit_die), and set up
7370    additional compilation units for each of the include files we see
7371    bracketed by BINCL/EINCL.  */
7372
7373 static void
7374 break_out_includes (dw_die_ref die)
7375 {
7376   dw_die_ref c;
7377   dw_die_ref unit = NULL;
7378   limbo_die_node *node, **pnode;
7379   htab_t cu_hash_table;
7380
7381   c = die->die_child;
7382   if (c) do {
7383     dw_die_ref prev = c;
7384     c = c->die_sib;
7385     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7386            || (unit && is_comdat_die (c)))
7387       {
7388         dw_die_ref next = c->die_sib;
7389
7390         /* This DIE is for a secondary CU; remove it from the main one.  */
7391         remove_child_with_prev (c, prev);
7392
7393         if (c->die_tag == DW_TAG_GNU_BINCL)
7394           unit = push_new_compile_unit (unit, c);
7395         else if (c->die_tag == DW_TAG_GNU_EINCL)
7396           unit = pop_compile_unit (unit);
7397         else
7398           add_child_die (unit, c);
7399         c = next;
7400         if (c == die->die_child)
7401           break;
7402       }
7403   } while (c != die->die_child);
7404
7405 #if 0
7406   /* We can only use this in debugging, since the frontend doesn't check
7407      to make sure that we leave every include file we enter.  */
7408   gcc_assert (!unit);
7409 #endif
7410
7411   assign_symbol_names (die);
7412   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7413   for (node = limbo_die_list, pnode = &limbo_die_list;
7414        node;
7415        node = node->next)
7416     {
7417       int is_dupl;
7418
7419       compute_section_prefix (node->die);
7420       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7421                         &comdat_symbol_number);
7422       assign_symbol_names (node->die);
7423       if (is_dupl)
7424         *pnode = node->next;
7425       else
7426         {
7427           pnode = &node->next;
7428           record_comdat_symbol_number (node->die, cu_hash_table,
7429                 comdat_symbol_number);
7430         }
7431     }
7432   htab_delete (cu_hash_table);
7433 }
7434
7435 /* Traverse the DIE and add a sibling attribute if it may have the
7436    effect of speeding up access to siblings.  To save some space,
7437    avoid generating sibling attributes for DIE's without children.  */
7438
7439 static void
7440 add_sibling_attributes (dw_die_ref die)
7441 {
7442   dw_die_ref c;
7443
7444   if (! die->die_child)
7445     return;
7446
7447   if (die->die_parent && die != die->die_parent->die_child)
7448     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7449
7450   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7451 }
7452
7453 /* Output all location lists for the DIE and its children.  */
7454
7455 static void
7456 output_location_lists (dw_die_ref die)
7457 {
7458   dw_die_ref c;
7459   dw_attr_ref a;
7460   unsigned ix;
7461
7462   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7463     if (AT_class (a) == dw_val_class_loc_list)
7464       output_loc_list (AT_loc_list (a));
7465
7466   FOR_EACH_CHILD (die, c, output_location_lists (c));
7467 }
7468
7469 /* The format of each DIE (and its attribute value pairs) is encoded in an
7470    abbreviation table.  This routine builds the abbreviation table and assigns
7471    a unique abbreviation id for each abbreviation entry.  The children of each
7472    die are visited recursively.  */
7473
7474 static void
7475 build_abbrev_table (dw_die_ref die)
7476 {
7477   unsigned long abbrev_id;
7478   unsigned int n_alloc;
7479   dw_die_ref c;
7480   dw_attr_ref a;
7481   unsigned ix;
7482
7483   /* Scan the DIE references, and mark as external any that refer to
7484      DIEs from other CUs (i.e. those which are not marked).  */
7485   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7486     if (AT_class (a) == dw_val_class_die_ref
7487         && AT_ref (a)->die_mark == 0)
7488       {
7489         gcc_assert (AT_ref (a)->die_symbol);
7490
7491         set_AT_ref_external (a, 1);
7492       }
7493
7494   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7495     {
7496       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7497       dw_attr_ref die_a, abbrev_a;
7498       unsigned ix;
7499       bool ok = true;
7500
7501       if (abbrev->die_tag != die->die_tag)
7502         continue;
7503       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7504         continue;
7505
7506       if (VEC_length (dw_attr_node, abbrev->die_attr)
7507           != VEC_length (dw_attr_node, die->die_attr))
7508         continue;
7509
7510       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7511         {
7512           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7513           if ((abbrev_a->dw_attr != die_a->dw_attr)
7514               || (value_format (abbrev_a) != value_format (die_a)))
7515             {
7516               ok = false;
7517               break;
7518             }
7519         }
7520       if (ok)
7521         break;
7522     }
7523
7524   if (abbrev_id >= abbrev_die_table_in_use)
7525     {
7526       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7527         {
7528           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7529           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7530                                             n_alloc);
7531
7532           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7533                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7534           abbrev_die_table_allocated = n_alloc;
7535         }
7536
7537       ++abbrev_die_table_in_use;
7538       abbrev_die_table[abbrev_id] = die;
7539     }
7540
7541   die->die_abbrev = abbrev_id;
7542   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7543 }
7544 \f
7545 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7546
7547 static int
7548 constant_size (unsigned HOST_WIDE_INT value)
7549 {
7550   int log;
7551
7552   if (value == 0)
7553     log = 0;
7554   else
7555     log = floor_log2 (value);
7556
7557   log = log / 8;
7558   log = 1 << (floor_log2 (log) + 1);
7559
7560   return log;
7561 }
7562
7563 /* Return the size of a DIE as it is represented in the
7564    .debug_info section.  */
7565
7566 static unsigned long
7567 size_of_die (dw_die_ref die)
7568 {
7569   unsigned long size = 0;
7570   dw_attr_ref a;
7571   unsigned ix;
7572
7573   size += size_of_uleb128 (die->die_abbrev);
7574   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7575     {
7576       switch (AT_class (a))
7577         {
7578         case dw_val_class_addr:
7579           size += DWARF2_ADDR_SIZE;
7580           break;
7581         case dw_val_class_offset:
7582           size += DWARF_OFFSET_SIZE;
7583           break;
7584         case dw_val_class_loc:
7585           {
7586             unsigned long lsize = size_of_locs (AT_loc (a));
7587
7588             /* Block length.  */
7589             size += constant_size (lsize);
7590             size += lsize;
7591           }
7592           break;
7593         case dw_val_class_loc_list:
7594           size += DWARF_OFFSET_SIZE;
7595           break;
7596         case dw_val_class_range_list:
7597           size += DWARF_OFFSET_SIZE;
7598           break;
7599         case dw_val_class_const:
7600           size += size_of_sleb128 (AT_int (a));
7601           break;
7602         case dw_val_class_unsigned_const:
7603           size += constant_size (AT_unsigned (a));
7604           break;
7605         case dw_val_class_long_long:
7606           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7607           break;
7608         case dw_val_class_vec:
7609           size += constant_size (a->dw_attr_val.v.val_vec.length
7610                                  * a->dw_attr_val.v.val_vec.elt_size)
7611                   + a->dw_attr_val.v.val_vec.length
7612                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7613           break;
7614         case dw_val_class_flag:
7615           size += 1;
7616           break;
7617         case dw_val_class_die_ref:
7618           if (AT_ref_external (a))
7619             size += DWARF2_ADDR_SIZE;
7620           else
7621             size += DWARF_OFFSET_SIZE;
7622           break;
7623         case dw_val_class_fde_ref:
7624           size += DWARF_OFFSET_SIZE;
7625           break;
7626         case dw_val_class_lbl_id:
7627           size += DWARF2_ADDR_SIZE;
7628           break;
7629         case dw_val_class_lineptr:
7630         case dw_val_class_macptr:
7631           size += DWARF_OFFSET_SIZE;
7632           break;
7633         case dw_val_class_str:
7634           if (AT_string_form (a) == DW_FORM_strp)
7635             size += DWARF_OFFSET_SIZE;
7636           else
7637             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7638           break;
7639         case dw_val_class_file:
7640           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7641           break;
7642         default:
7643           gcc_unreachable ();
7644         }
7645     }
7646
7647   return size;
7648 }
7649
7650 /* Size the debugging information associated with a given DIE.  Visits the
7651    DIE's children recursively.  Updates the global variable next_die_offset, on
7652    each time through.  Uses the current value of next_die_offset to update the
7653    die_offset field in each DIE.  */
7654
7655 static void
7656 calc_die_sizes (dw_die_ref die)
7657 {
7658   dw_die_ref c;
7659
7660   die->die_offset = next_die_offset;
7661   next_die_offset += size_of_die (die);
7662
7663   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7664
7665   if (die->die_child != NULL)
7666     /* Count the null byte used to terminate sibling lists.  */
7667     next_die_offset += 1;
7668 }
7669
7670 /* Set the marks for a die and its children.  We do this so
7671    that we know whether or not a reference needs to use FORM_ref_addr; only
7672    DIEs in the same CU will be marked.  We used to clear out the offset
7673    and use that as the flag, but ran into ordering problems.  */
7674
7675 static void
7676 mark_dies (dw_die_ref die)
7677 {
7678   dw_die_ref c;
7679
7680   gcc_assert (!die->die_mark);
7681
7682   die->die_mark = 1;
7683   FOR_EACH_CHILD (die, c, mark_dies (c));
7684 }
7685
7686 /* Clear the marks for a die and its children.  */
7687
7688 static void
7689 unmark_dies (dw_die_ref die)
7690 {
7691   dw_die_ref c;
7692
7693   gcc_assert (die->die_mark);
7694
7695   die->die_mark = 0;
7696   FOR_EACH_CHILD (die, c, unmark_dies (c));
7697 }
7698
7699 /* Clear the marks for a die, its children and referred dies.  */
7700
7701 static void
7702 unmark_all_dies (dw_die_ref die)
7703 {
7704   dw_die_ref c;
7705   dw_attr_ref a;
7706   unsigned ix;
7707
7708   if (!die->die_mark)
7709     return;
7710   die->die_mark = 0;
7711
7712   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7713
7714   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7715     if (AT_class (a) == dw_val_class_die_ref)
7716       unmark_all_dies (AT_ref (a));
7717 }
7718
7719 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7720    generated for the compilation unit.  */
7721
7722 static unsigned long
7723 size_of_pubnames (VEC (pubname_entry, gc) * names)
7724 {
7725   unsigned long size;
7726   unsigned i;
7727   pubname_ref p;
7728
7729   size = DWARF_PUBNAMES_HEADER_SIZE;
7730   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7731     if (names != pubtype_table
7732         || p->die->die_offset != 0
7733         || !flag_eliminate_unused_debug_types)
7734       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7735
7736   size += DWARF_OFFSET_SIZE;
7737   return size;
7738 }
7739
7740 /* Return the size of the information in the .debug_aranges section.  */
7741
7742 static unsigned long
7743 size_of_aranges (void)
7744 {
7745   unsigned long size;
7746
7747   size = DWARF_ARANGES_HEADER_SIZE;
7748
7749   /* Count the address/length pair for this compilation unit.  */
7750   if (text_section_used)
7751     size += 2 * DWARF2_ADDR_SIZE;
7752   if (cold_text_section_used)
7753     size += 2 * DWARF2_ADDR_SIZE;
7754   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7755
7756   /* Count the two zero words used to terminated the address range table.  */
7757   size += 2 * DWARF2_ADDR_SIZE;
7758   return size;
7759 }
7760 \f
7761 /* Select the encoding of an attribute value.  */
7762
7763 static enum dwarf_form
7764 value_format (dw_attr_ref a)
7765 {
7766   switch (a->dw_attr_val.val_class)
7767     {
7768     case dw_val_class_addr:
7769       return DW_FORM_addr;
7770     case dw_val_class_range_list:
7771     case dw_val_class_offset:
7772     case dw_val_class_loc_list:
7773       switch (DWARF_OFFSET_SIZE)
7774         {
7775         case 4:
7776           return DW_FORM_data4;
7777         case 8:
7778           return DW_FORM_data8;
7779         default:
7780           gcc_unreachable ();
7781         }
7782     case dw_val_class_loc:
7783       switch (constant_size (size_of_locs (AT_loc (a))))
7784         {
7785         case 1:
7786           return DW_FORM_block1;
7787         case 2:
7788           return DW_FORM_block2;
7789         default:
7790           gcc_unreachable ();
7791         }
7792     case dw_val_class_const:
7793       return DW_FORM_sdata;
7794     case dw_val_class_unsigned_const:
7795       switch (constant_size (AT_unsigned (a)))
7796         {
7797         case 1:
7798           return DW_FORM_data1;
7799         case 2:
7800           return DW_FORM_data2;
7801         case 4:
7802           return DW_FORM_data4;
7803         case 8:
7804           return DW_FORM_data8;
7805         default:
7806           gcc_unreachable ();
7807         }
7808     case dw_val_class_long_long:
7809       return DW_FORM_block1;
7810     case dw_val_class_vec:
7811       switch (constant_size (a->dw_attr_val.v.val_vec.length
7812                              * a->dw_attr_val.v.val_vec.elt_size))
7813         {
7814         case 1:
7815           return DW_FORM_block1;
7816         case 2:
7817           return DW_FORM_block2;
7818         case 4:
7819           return DW_FORM_block4;
7820         default:
7821           gcc_unreachable ();
7822         }
7823     case dw_val_class_flag:
7824       return DW_FORM_flag;
7825     case dw_val_class_die_ref:
7826       if (AT_ref_external (a))
7827         return DW_FORM_ref_addr;
7828       else
7829         return DW_FORM_ref;
7830     case dw_val_class_fde_ref:
7831       return DW_FORM_data;
7832     case dw_val_class_lbl_id:
7833       return DW_FORM_addr;
7834     case dw_val_class_lineptr:
7835     case dw_val_class_macptr:
7836       return DW_FORM_data;
7837     case dw_val_class_str:
7838       return AT_string_form (a);
7839     case dw_val_class_file:
7840       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7841         {
7842         case 1:
7843           return DW_FORM_data1;
7844         case 2:
7845           return DW_FORM_data2;
7846         case 4:
7847           return DW_FORM_data4;
7848         default:
7849           gcc_unreachable ();
7850         }
7851
7852     default:
7853       gcc_unreachable ();
7854     }
7855 }
7856
7857 /* Output the encoding of an attribute value.  */
7858
7859 static void
7860 output_value_format (dw_attr_ref a)
7861 {
7862   enum dwarf_form form = value_format (a);
7863
7864   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7865 }
7866
7867 /* Output the .debug_abbrev section which defines the DIE abbreviation
7868    table.  */
7869
7870 static void
7871 output_abbrev_section (void)
7872 {
7873   unsigned long abbrev_id;
7874
7875   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7876     {
7877       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7878       unsigned ix;
7879       dw_attr_ref a_attr;
7880
7881       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7882       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7883                                    dwarf_tag_name (abbrev->die_tag));
7884
7885       if (abbrev->die_child != NULL)
7886         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7887       else
7888         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7889
7890       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7891            ix++)
7892         {
7893           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7894                                        dwarf_attr_name (a_attr->dw_attr));
7895           output_value_format (a_attr);
7896         }
7897
7898       dw2_asm_output_data (1, 0, NULL);
7899       dw2_asm_output_data (1, 0, NULL);
7900     }
7901
7902   /* Terminate the table.  */
7903   dw2_asm_output_data (1, 0, NULL);
7904 }
7905
7906 /* Output a symbol we can use to refer to this DIE from another CU.  */
7907
7908 static inline void
7909 output_die_symbol (dw_die_ref die)
7910 {
7911   char *sym = die->die_symbol;
7912
7913   if (sym == 0)
7914     return;
7915
7916   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7917     /* We make these global, not weak; if the target doesn't support
7918        .linkonce, it doesn't support combining the sections, so debugging
7919        will break.  */
7920     targetm.asm_out.globalize_label (asm_out_file, sym);
7921
7922   ASM_OUTPUT_LABEL (asm_out_file, sym);
7923 }
7924
7925 /* Return a new location list, given the begin and end range, and the
7926    expression. gensym tells us whether to generate a new internal symbol for
7927    this location list node, which is done for the head of the list only.  */
7928
7929 static inline dw_loc_list_ref
7930 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7931               const char *section, unsigned int gensym)
7932 {
7933   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7934
7935   retlist->begin = begin;
7936   retlist->end = end;
7937   retlist->expr = expr;
7938   retlist->section = section;
7939   if (gensym)
7940     retlist->ll_symbol = gen_internal_sym ("LLST");
7941
7942   return retlist;
7943 }
7944
7945 /* Add a location description expression to a location list.  */
7946
7947 static inline void
7948 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7949                            const char *begin, const char *end,
7950                            const char *section)
7951 {
7952   dw_loc_list_ref *d;
7953
7954   /* Find the end of the chain.  */
7955   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7956     ;
7957
7958   /* Add a new location list node to the list.  */
7959   *d = new_loc_list (descr, begin, end, section, 0);
7960 }
7961
7962 /* Output the location list given to us.  */
7963
7964 static void
7965 output_loc_list (dw_loc_list_ref list_head)
7966 {
7967   dw_loc_list_ref curr = list_head;
7968
7969   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7970
7971   /* Walk the location list, and output each range + expression.  */
7972   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7973     {
7974       unsigned long size;
7975       /* Don't output an entry that starts and ends at the same address.  */
7976       if (strcmp (curr->begin, curr->end) == 0)
7977         continue;
7978       if (!have_multiple_function_sections)
7979         {
7980           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7981                                 "Location list begin address (%s)",
7982                                 list_head->ll_symbol);
7983           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7984                                 "Location list end address (%s)",
7985                                 list_head->ll_symbol);
7986         }
7987       else
7988         {
7989           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7990                                "Location list begin address (%s)",
7991                                list_head->ll_symbol);
7992           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7993                                "Location list end address (%s)",
7994                                list_head->ll_symbol);
7995         }
7996       size = size_of_locs (curr->expr);
7997
7998       /* Output the block length for this list of location operations.  */
7999       gcc_assert (size <= 0xffff);
8000       dw2_asm_output_data (2, size, "%s", "Location expression size");
8001
8002       output_loc_sequence (curr->expr);
8003     }
8004
8005   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8006                        "Location list terminator begin (%s)",
8007                        list_head->ll_symbol);
8008   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8009                        "Location list terminator end (%s)",
8010                        list_head->ll_symbol);
8011 }
8012
8013 /* Output the DIE and its attributes.  Called recursively to generate
8014    the definitions of each child DIE.  */
8015
8016 static void
8017 output_die (dw_die_ref die)
8018 {
8019   dw_attr_ref a;
8020   dw_die_ref c;
8021   unsigned long size;
8022   unsigned ix;
8023
8024   /* If someone in another CU might refer to us, set up a symbol for
8025      them to point to.  */
8026   if (die->die_symbol)
8027     output_die_symbol (die);
8028
8029   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8030                                (unsigned long)die->die_offset,
8031                                dwarf_tag_name (die->die_tag));
8032
8033   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8034     {
8035       const char *name = dwarf_attr_name (a->dw_attr);
8036
8037       switch (AT_class (a))
8038         {
8039         case dw_val_class_addr:
8040           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8041           break;
8042
8043         case dw_val_class_offset:
8044           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8045                                "%s", name);
8046           break;
8047
8048         case dw_val_class_range_list:
8049           {
8050             char *p = strchr (ranges_section_label, '\0');
8051
8052             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8053                      a->dw_attr_val.v.val_offset);
8054             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8055                                    debug_ranges_section, "%s", name);
8056             *p = '\0';
8057           }
8058           break;
8059
8060         case dw_val_class_loc:
8061           size = size_of_locs (AT_loc (a));
8062
8063           /* Output the block length for this list of location operations.  */
8064           dw2_asm_output_data (constant_size (size), size, "%s", name);
8065
8066           output_loc_sequence (AT_loc (a));
8067           break;
8068
8069         case dw_val_class_const:
8070           /* ??? It would be slightly more efficient to use a scheme like is
8071              used for unsigned constants below, but gdb 4.x does not sign
8072              extend.  Gdb 5.x does sign extend.  */
8073           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8074           break;
8075
8076         case dw_val_class_unsigned_const:
8077           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8078                                AT_unsigned (a), "%s", name);
8079           break;
8080
8081         case dw_val_class_long_long:
8082           {
8083             unsigned HOST_WIDE_INT first, second;
8084
8085             dw2_asm_output_data (1,
8086                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8087                                  "%s", name);
8088
8089             if (WORDS_BIG_ENDIAN)
8090               {
8091                 first = a->dw_attr_val.v.val_long_long.hi;
8092                 second = a->dw_attr_val.v.val_long_long.low;
8093               }
8094             else
8095               {
8096                 first = a->dw_attr_val.v.val_long_long.low;
8097                 second = a->dw_attr_val.v.val_long_long.hi;
8098               }
8099
8100             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8101                                  first, "long long constant");
8102             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8103                                  second, NULL);
8104           }
8105           break;
8106
8107         case dw_val_class_vec:
8108           {
8109             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8110             unsigned int len = a->dw_attr_val.v.val_vec.length;
8111             unsigned int i;
8112             unsigned char *p;
8113
8114             dw2_asm_output_data (constant_size (len * elt_size),
8115                                  len * elt_size, "%s", name);
8116             if (elt_size > sizeof (HOST_WIDE_INT))
8117               {
8118                 elt_size /= 2;
8119                 len *= 2;
8120               }
8121             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8122                  i < len;
8123                  i++, p += elt_size)
8124               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8125                                    "fp or vector constant word %u", i);
8126             break;
8127           }
8128
8129         case dw_val_class_flag:
8130           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8131           break;
8132
8133         case dw_val_class_loc_list:
8134           {
8135             char *sym = AT_loc_list (a)->ll_symbol;
8136
8137             gcc_assert (sym);
8138             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8139                                    "%s", name);
8140           }
8141           break;
8142
8143         case dw_val_class_die_ref:
8144           if (AT_ref_external (a))
8145             {
8146               char *sym = AT_ref (a)->die_symbol;
8147
8148               gcc_assert (sym);
8149               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8150                                      "%s", name);
8151             }
8152           else
8153             {
8154               gcc_assert (AT_ref (a)->die_offset);
8155               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8156                                    "%s", name);
8157             }
8158           break;
8159
8160         case dw_val_class_fde_ref:
8161           {
8162             char l1[20];
8163
8164             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8165                                          a->dw_attr_val.v.val_fde_index * 2);
8166             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8167                                    "%s", name);
8168           }
8169           break;
8170
8171         case dw_val_class_lbl_id:
8172           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8173           break;
8174
8175         case dw_val_class_lineptr:
8176           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8177                                  debug_line_section, "%s", name);
8178           break;
8179
8180         case dw_val_class_macptr:
8181           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8182                                  debug_macinfo_section, "%s", name);
8183           break;
8184
8185         case dw_val_class_str:
8186           if (AT_string_form (a) == DW_FORM_strp)
8187             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8188                                    a->dw_attr_val.v.val_str->label,
8189                                    debug_str_section,
8190                                    "%s: \"%s\"", name, AT_string (a));
8191           else
8192             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8193           break;
8194
8195         case dw_val_class_file:
8196           {
8197             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8198
8199             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8200                                  a->dw_attr_val.v.val_file->filename);
8201             break;
8202           }
8203
8204         default:
8205           gcc_unreachable ();
8206         }
8207     }
8208
8209   FOR_EACH_CHILD (die, c, output_die (c));
8210
8211   /* Add null byte to terminate sibling list.  */
8212   if (die->die_child != NULL)
8213     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8214                          (unsigned long) die->die_offset);
8215 }
8216
8217 /* Output the compilation unit that appears at the beginning of the
8218    .debug_info section, and precedes the DIE descriptions.  */
8219
8220 static void
8221 output_compilation_unit_header (void)
8222 {
8223   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8224     dw2_asm_output_data (4, 0xffffffff,
8225       "Initial length escape value indicating 64-bit DWARF extension");
8226   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8227                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8228                        "Length of Compilation Unit Info");
8229   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8230   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8231                          debug_abbrev_section,
8232                          "Offset Into Abbrev. Section");
8233   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8234 }
8235
8236 /* Output the compilation unit DIE and its children.  */
8237
8238 static void
8239 output_comp_unit (dw_die_ref die, int output_if_empty)
8240 {
8241   const char *secname;
8242   char *oldsym, *tmp;
8243
8244   /* Unless we are outputting main CU, we may throw away empty ones.  */
8245   if (!output_if_empty && die->die_child == NULL)
8246     return;
8247
8248   /* Even if there are no children of this DIE, we must output the information
8249      about the compilation unit.  Otherwise, on an empty translation unit, we
8250      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8251      will then complain when examining the file.  First mark all the DIEs in
8252      this CU so we know which get local refs.  */
8253   mark_dies (die);
8254
8255   build_abbrev_table (die);
8256
8257   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8258   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8259   calc_die_sizes (die);
8260
8261   oldsym = die->die_symbol;
8262   if (oldsym)
8263     {
8264       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8265
8266       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8267       secname = tmp;
8268       die->die_symbol = NULL;
8269       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8270     }
8271   else
8272     switch_to_section (debug_info_section);
8273
8274   /* Output debugging information.  */
8275   output_compilation_unit_header ();
8276   output_die (die);
8277
8278   /* Leave the marks on the main CU, so we can check them in
8279      output_pubnames.  */
8280   if (oldsym)
8281     {
8282       unmark_dies (die);
8283       die->die_symbol = oldsym;
8284     }
8285 }
8286
8287 /* Return the DWARF2/3 pubname associated with a decl.  */
8288
8289 static const char *
8290 dwarf2_name (tree decl, int scope)
8291 {
8292   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8293 }
8294
8295 /* Add a new entry to .debug_pubnames if appropriate.  */
8296
8297 static void
8298 add_pubname_string (const char *str, dw_die_ref die)
8299 {
8300   pubname_entry e;
8301
8302   e.die = die;
8303   e.name = xstrdup (str);
8304   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8305 }
8306
8307 static void
8308 add_pubname (tree decl, dw_die_ref die)
8309 {
8310
8311   if (TREE_PUBLIC (decl))
8312     add_pubname_string (dwarf2_name (decl, 1), die);
8313 }
8314
8315 /* Add a new entry to .debug_pubtypes if appropriate.  */
8316
8317 static void
8318 add_pubtype (tree decl, dw_die_ref die)
8319 {
8320   pubname_entry e;
8321
8322   e.name = NULL;
8323   if ((TREE_PUBLIC (decl)
8324        || die->die_parent == comp_unit_die)
8325       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8326     {
8327       e.die = die;
8328       if (TYPE_P (decl))
8329         {
8330           if (TYPE_NAME (decl))
8331             {
8332               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8333                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8334               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8335                        && DECL_NAME (TYPE_NAME (decl)))
8336                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8337               else
8338                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8339             }
8340         }
8341       else
8342         e.name = xstrdup (dwarf2_name (decl, 1));
8343
8344       /* If we don't have a name for the type, there's no point in adding
8345          it to the table.  */
8346       if (e.name && e.name[0] != '\0')
8347         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8348     }
8349 }
8350
8351 /* Output the public names table used to speed up access to externally
8352    visible names; or the public types table used to find type definitions.  */
8353
8354 static void
8355 output_pubnames (VEC (pubname_entry, gc) * names)
8356 {
8357   unsigned i;
8358   unsigned long pubnames_length = size_of_pubnames (names);
8359   pubname_ref pub;
8360
8361   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8362     dw2_asm_output_data (4, 0xffffffff,
8363       "Initial length escape value indicating 64-bit DWARF extension");
8364   if (names == pubname_table)
8365     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8366                          "Length of Public Names Info");
8367   else
8368     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8369                          "Length of Public Type Names Info");
8370   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8371   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8372                          debug_info_section,
8373                          "Offset of Compilation Unit Info");
8374   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8375                        "Compilation Unit Length");
8376
8377   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8378     {
8379       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8380       if (names == pubname_table)
8381         gcc_assert (pub->die->die_mark);
8382
8383       if (names != pubtype_table
8384           || pub->die->die_offset != 0
8385           || !flag_eliminate_unused_debug_types)
8386         {
8387           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8388                                "DIE offset");
8389
8390           dw2_asm_output_nstring (pub->name, -1, "external name");
8391         }
8392     }
8393
8394   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8395 }
8396
8397 /* Add a new entry to .debug_aranges if appropriate.  */
8398
8399 static void
8400 add_arange (tree decl, dw_die_ref die)
8401 {
8402   if (! DECL_SECTION_NAME (decl))
8403     return;
8404
8405   if (arange_table_in_use == arange_table_allocated)
8406     {
8407       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8408       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8409                                     arange_table_allocated);
8410       memset (arange_table + arange_table_in_use, 0,
8411               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8412     }
8413
8414   arange_table[arange_table_in_use++] = die;
8415 }
8416
8417 /* Output the information that goes into the .debug_aranges table.
8418    Namely, define the beginning and ending address range of the
8419    text section generated for this compilation unit.  */
8420
8421 static void
8422 output_aranges (void)
8423 {
8424   unsigned i;
8425   unsigned long aranges_length = size_of_aranges ();
8426
8427   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8428     dw2_asm_output_data (4, 0xffffffff,
8429       "Initial length escape value indicating 64-bit DWARF extension");
8430   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8431                        "Length of Address Ranges Info");
8432   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8433   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8434                          debug_info_section,
8435                          "Offset of Compilation Unit Info");
8436   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8437   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8438
8439   /* We need to align to twice the pointer size here.  */
8440   if (DWARF_ARANGES_PAD_SIZE)
8441     {
8442       /* Pad using a 2 byte words so that padding is correct for any
8443          pointer size.  */
8444       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8445                            2 * DWARF2_ADDR_SIZE);
8446       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8447         dw2_asm_output_data (2, 0, NULL);
8448     }
8449
8450   /* It is necessary not to output these entries if the sections were
8451      not used; if the sections were not used, the length will be 0 and
8452      the address may end up as 0 if the section is discarded by ld
8453      --gc-sections, leaving an invalid (0, 0) entry that can be
8454      confused with the terminator.  */
8455   if (text_section_used)
8456     {
8457       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8458       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8459                             text_section_label, "Length");
8460     }
8461   if (cold_text_section_used)
8462     {
8463       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8464                            "Address");
8465       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8466                             cold_text_section_label, "Length");
8467     }
8468
8469   for (i = 0; i < arange_table_in_use; i++)
8470     {
8471       dw_die_ref die = arange_table[i];
8472
8473       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8474       gcc_assert (die->die_mark);
8475
8476       if (die->die_tag == DW_TAG_subprogram)
8477         {
8478           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8479                                "Address");
8480           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8481                                 get_AT_low_pc (die), "Length");
8482         }
8483       else
8484         {
8485           /* A static variable; extract the symbol from DW_AT_location.
8486              Note that this code isn't currently hit, as we only emit
8487              aranges for functions (jason 9/23/99).  */
8488           dw_attr_ref a = get_AT (die, DW_AT_location);
8489           dw_loc_descr_ref loc;
8490
8491           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8492
8493           loc = AT_loc (a);
8494           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8495
8496           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8497                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8498           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8499                                get_AT_unsigned (die, DW_AT_byte_size),
8500                                "Length");
8501         }
8502     }
8503
8504   /* Output the terminator words.  */
8505   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8506   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8507 }
8508
8509 /* Add a new entry to .debug_ranges.  Return the offset at which it
8510    was placed.  */
8511
8512 static unsigned int
8513 add_ranges_num (int num)
8514 {
8515   unsigned int in_use = ranges_table_in_use;
8516
8517   if (in_use == ranges_table_allocated)
8518     {
8519       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8520       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8521                                     ranges_table_allocated);
8522       memset (ranges_table + ranges_table_in_use, 0,
8523               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8524     }
8525
8526   ranges_table[in_use].num = num;
8527   ranges_table_in_use = in_use + 1;
8528
8529   return in_use * 2 * DWARF2_ADDR_SIZE;
8530 }
8531
8532 /* Add a new entry to .debug_ranges corresponding to a block, or a
8533    range terminator if BLOCK is NULL.  */
8534
8535 static unsigned int
8536 add_ranges (const_tree block)
8537 {
8538   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8539 }
8540
8541 /* Add a new entry to .debug_ranges corresponding to a pair of
8542    labels.  */
8543
8544 static unsigned int
8545 add_ranges_by_labels (const char *begin, const char *end)
8546 {
8547   unsigned int in_use = ranges_by_label_in_use;
8548
8549   if (in_use == ranges_by_label_allocated)
8550     {
8551       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8552       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8553                                        ranges_by_label,
8554                                        ranges_by_label_allocated);
8555       memset (ranges_by_label + ranges_by_label_in_use, 0,
8556               RANGES_TABLE_INCREMENT
8557               * sizeof (struct dw_ranges_by_label_struct));
8558     }
8559
8560   ranges_by_label[in_use].begin = begin;
8561   ranges_by_label[in_use].end = end;
8562   ranges_by_label_in_use = in_use + 1;
8563
8564   return add_ranges_num (-(int)in_use - 1);
8565 }
8566
8567 static void
8568 output_ranges (void)
8569 {
8570   unsigned i;
8571   static const char *const start_fmt = "Offset 0x%x";
8572   const char *fmt = start_fmt;
8573
8574   for (i = 0; i < ranges_table_in_use; i++)
8575     {
8576       int block_num = ranges_table[i].num;
8577
8578       if (block_num > 0)
8579         {
8580           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8581           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8582
8583           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8584           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8585
8586           /* If all code is in the text section, then the compilation
8587              unit base address defaults to DW_AT_low_pc, which is the
8588              base of the text section.  */
8589           if (!have_multiple_function_sections)
8590             {
8591               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8592                                     text_section_label,
8593                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8594               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8595                                     text_section_label, NULL);
8596             }
8597
8598           /* Otherwise, the compilation unit base address is zero,
8599              which allows us to use absolute addresses, and not worry
8600              about whether the target supports cross-section
8601              arithmetic.  */
8602           else
8603             {
8604               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8605                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8606               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8607             }
8608
8609           fmt = NULL;
8610         }
8611
8612       /* Negative block_num stands for an index into ranges_by_label.  */
8613       else if (block_num < 0)
8614         {
8615           int lab_idx = - block_num - 1;
8616
8617           if (!have_multiple_function_sections)
8618             {
8619               gcc_unreachable ();
8620 #if 0
8621               /* If we ever use add_ranges_by_labels () for a single
8622                  function section, all we have to do is to take out
8623                  the #if 0 above.  */
8624               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8625                                     ranges_by_label[lab_idx].begin,
8626                                     text_section_label,
8627                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8628               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8629                                     ranges_by_label[lab_idx].end,
8630                                     text_section_label, NULL);
8631 #endif
8632             }
8633           else
8634             {
8635               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8636                                    ranges_by_label[lab_idx].begin,
8637                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8638               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8639                                    ranges_by_label[lab_idx].end,
8640                                    NULL);
8641             }
8642         }
8643       else
8644         {
8645           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8646           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8647           fmt = start_fmt;
8648         }
8649     }
8650 }
8651
8652 /* Data structure containing information about input files.  */
8653 struct file_info
8654 {
8655   const char *path;     /* Complete file name.  */
8656   const char *fname;    /* File name part.  */
8657   int length;           /* Length of entire string.  */
8658   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8659   int dir_idx;          /* Index in directory table.  */
8660 };
8661
8662 /* Data structure containing information about directories with source
8663    files.  */
8664 struct dir_info
8665 {
8666   const char *path;     /* Path including directory name.  */
8667   int length;           /* Path length.  */
8668   int prefix;           /* Index of directory entry which is a prefix.  */
8669   int count;            /* Number of files in this directory.  */
8670   int dir_idx;          /* Index of directory used as base.  */
8671 };
8672
8673 /* Callback function for file_info comparison.  We sort by looking at
8674    the directories in the path.  */
8675
8676 static int
8677 file_info_cmp (const void *p1, const void *p2)
8678 {
8679   const struct file_info *const s1 = (const struct file_info *) p1;
8680   const struct file_info *const s2 = (const struct file_info *) p2;
8681   const unsigned char *cp1;
8682   const unsigned char *cp2;
8683
8684   /* Take care of file names without directories.  We need to make sure that
8685      we return consistent values to qsort since some will get confused if
8686      we return the same value when identical operands are passed in opposite
8687      orders.  So if neither has a directory, return 0 and otherwise return
8688      1 or -1 depending on which one has the directory.  */
8689   if ((s1->path == s1->fname || s2->path == s2->fname))
8690     return (s2->path == s2->fname) - (s1->path == s1->fname);
8691
8692   cp1 = (const unsigned char *) s1->path;
8693   cp2 = (const unsigned char *) s2->path;
8694
8695   while (1)
8696     {
8697       ++cp1;
8698       ++cp2;
8699       /* Reached the end of the first path?  If so, handle like above.  */
8700       if ((cp1 == (const unsigned char *) s1->fname)
8701           || (cp2 == (const unsigned char *) s2->fname))
8702         return ((cp2 == (const unsigned char *) s2->fname)
8703                 - (cp1 == (const unsigned char *) s1->fname));
8704
8705       /* Character of current path component the same?  */
8706       else if (*cp1 != *cp2)
8707         return *cp1 - *cp2;
8708     }
8709 }
8710
8711 struct file_name_acquire_data
8712 {
8713   struct file_info *files;
8714   int used_files;
8715   int max_files;
8716 };
8717
8718 /* Traversal function for the hash table.  */
8719
8720 static int
8721 file_name_acquire (void ** slot, void *data)
8722 {
8723   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8724   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8725   struct file_info *fi;
8726   const char *f;
8727
8728   gcc_assert (fnad->max_files >= d->emitted_number);
8729
8730   if (! d->emitted_number)
8731     return 1;
8732
8733   gcc_assert (fnad->max_files != fnad->used_files);
8734
8735   fi = fnad->files + fnad->used_files++;
8736
8737   /* Skip all leading "./".  */
8738   f = d->filename;
8739   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8740     f += 2;
8741
8742   /* Create a new array entry.  */
8743   fi->path = f;
8744   fi->length = strlen (f);
8745   fi->file_idx = d;
8746
8747   /* Search for the file name part.  */
8748   f = strrchr (f, DIR_SEPARATOR);
8749 #if defined (DIR_SEPARATOR_2)
8750   {
8751     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8752
8753     if (g != NULL)
8754       {
8755         if (f == NULL || f < g)
8756           f = g;
8757       }
8758   }
8759 #endif
8760
8761   fi->fname = f == NULL ? fi->path : f + 1;
8762   return 1;
8763 }
8764
8765 /* Output the directory table and the file name table.  We try to minimize
8766    the total amount of memory needed.  A heuristic is used to avoid large
8767    slowdowns with many input files.  */
8768
8769 static void
8770 output_file_names (void)
8771 {
8772   struct file_name_acquire_data fnad;
8773   int numfiles;
8774   struct file_info *files;
8775   struct dir_info *dirs;
8776   int *saved;
8777   int *savehere;
8778   int *backmap;
8779   int ndirs;
8780   int idx_offset;
8781   int i;
8782   int idx;
8783
8784   if (!last_emitted_file)
8785     {
8786       dw2_asm_output_data (1, 0, "End directory table");
8787       dw2_asm_output_data (1, 0, "End file name table");
8788       return;
8789     }
8790
8791   numfiles = last_emitted_file->emitted_number;
8792
8793   /* Allocate the various arrays we need.  */
8794   files = XALLOCAVEC (struct file_info, numfiles);
8795   dirs = XALLOCAVEC (struct dir_info, numfiles);
8796
8797   fnad.files = files;
8798   fnad.used_files = 0;
8799   fnad.max_files = numfiles;
8800   htab_traverse (file_table, file_name_acquire, &fnad);
8801   gcc_assert (fnad.used_files == fnad.max_files);
8802
8803   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8804
8805   /* Find all the different directories used.  */
8806   dirs[0].path = files[0].path;
8807   dirs[0].length = files[0].fname - files[0].path;
8808   dirs[0].prefix = -1;
8809   dirs[0].count = 1;
8810   dirs[0].dir_idx = 0;
8811   files[0].dir_idx = 0;
8812   ndirs = 1;
8813
8814   for (i = 1; i < numfiles; i++)
8815     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8816         && memcmp (dirs[ndirs - 1].path, files[i].path,
8817                    dirs[ndirs - 1].length) == 0)
8818       {
8819         /* Same directory as last entry.  */
8820         files[i].dir_idx = ndirs - 1;
8821         ++dirs[ndirs - 1].count;
8822       }
8823     else
8824       {
8825         int j;
8826
8827         /* This is a new directory.  */
8828         dirs[ndirs].path = files[i].path;
8829         dirs[ndirs].length = files[i].fname - files[i].path;
8830         dirs[ndirs].count = 1;
8831         dirs[ndirs].dir_idx = ndirs;
8832         files[i].dir_idx = ndirs;
8833
8834         /* Search for a prefix.  */
8835         dirs[ndirs].prefix = -1;
8836         for (j = 0; j < ndirs; j++)
8837           if (dirs[j].length < dirs[ndirs].length
8838               && dirs[j].length > 1
8839               && (dirs[ndirs].prefix == -1
8840                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8841               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8842             dirs[ndirs].prefix = j;
8843
8844         ++ndirs;
8845       }
8846
8847   /* Now to the actual work.  We have to find a subset of the directories which
8848      allow expressing the file name using references to the directory table
8849      with the least amount of characters.  We do not do an exhaustive search
8850      where we would have to check out every combination of every single
8851      possible prefix.  Instead we use a heuristic which provides nearly optimal
8852      results in most cases and never is much off.  */
8853   saved = XALLOCAVEC (int, ndirs);
8854   savehere = XALLOCAVEC (int, ndirs);
8855
8856   memset (saved, '\0', ndirs * sizeof (saved[0]));
8857   for (i = 0; i < ndirs; i++)
8858     {
8859       int j;
8860       int total;
8861
8862       /* We can always save some space for the current directory.  But this
8863          does not mean it will be enough to justify adding the directory.  */
8864       savehere[i] = dirs[i].length;
8865       total = (savehere[i] - saved[i]) * dirs[i].count;
8866
8867       for (j = i + 1; j < ndirs; j++)
8868         {
8869           savehere[j] = 0;
8870           if (saved[j] < dirs[i].length)
8871             {
8872               /* Determine whether the dirs[i] path is a prefix of the
8873                  dirs[j] path.  */
8874               int k;
8875
8876               k = dirs[j].prefix;
8877               while (k != -1 && k != (int) i)
8878                 k = dirs[k].prefix;
8879
8880               if (k == (int) i)
8881                 {
8882                   /* Yes it is.  We can possibly save some memory by
8883                      writing the filenames in dirs[j] relative to
8884                      dirs[i].  */
8885                   savehere[j] = dirs[i].length;
8886                   total += (savehere[j] - saved[j]) * dirs[j].count;
8887                 }
8888             }
8889         }
8890
8891       /* Check whether we can save enough to justify adding the dirs[i]
8892          directory.  */
8893       if (total > dirs[i].length + 1)
8894         {
8895           /* It's worthwhile adding.  */
8896           for (j = i; j < ndirs; j++)
8897             if (savehere[j] > 0)
8898               {
8899                 /* Remember how much we saved for this directory so far.  */
8900                 saved[j] = savehere[j];
8901
8902                 /* Remember the prefix directory.  */
8903                 dirs[j].dir_idx = i;
8904               }
8905         }
8906     }
8907
8908   /* Emit the directory name table.  */
8909   idx = 1;
8910   idx_offset = dirs[0].length > 0 ? 1 : 0;
8911   for (i = 1 - idx_offset; i < ndirs; i++)
8912     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8913                             "Directory Entry: 0x%x", i + idx_offset);
8914
8915   dw2_asm_output_data (1, 0, "End directory table");
8916
8917   /* We have to emit them in the order of emitted_number since that's
8918      used in the debug info generation.  To do this efficiently we
8919      generate a back-mapping of the indices first.  */
8920   backmap = XALLOCAVEC (int, numfiles);
8921   for (i = 0; i < numfiles; i++)
8922     backmap[files[i].file_idx->emitted_number - 1] = i;
8923
8924   /* Now write all the file names.  */
8925   for (i = 0; i < numfiles; i++)
8926     {
8927       int file_idx = backmap[i];
8928       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8929
8930       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8931                               "File Entry: 0x%x", (unsigned) i + 1);
8932
8933       /* Include directory index.  */
8934       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8935
8936       /* Modification time.  */
8937       dw2_asm_output_data_uleb128 (0, NULL);
8938
8939       /* File length in bytes.  */
8940       dw2_asm_output_data_uleb128 (0, NULL);
8941     }
8942
8943   dw2_asm_output_data (1, 0, "End file name table");
8944 }
8945
8946
8947 /* Output the source line number correspondence information.  This
8948    information goes into the .debug_line section.  */
8949
8950 static void
8951 output_line_info (void)
8952 {
8953   char l1[20], l2[20], p1[20], p2[20];
8954   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8955   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8956   unsigned opc;
8957   unsigned n_op_args;
8958   unsigned long lt_index;
8959   unsigned long current_line;
8960   long line_offset;
8961   long line_delta;
8962   unsigned long current_file;
8963   unsigned long function;
8964
8965   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8966   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8967   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8968   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8969
8970   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8971     dw2_asm_output_data (4, 0xffffffff,
8972       "Initial length escape value indicating 64-bit DWARF extension");
8973   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8974                         "Length of Source Line Info");
8975   ASM_OUTPUT_LABEL (asm_out_file, l1);
8976
8977   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8978   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8979   ASM_OUTPUT_LABEL (asm_out_file, p1);
8980
8981   /* Define the architecture-dependent minimum instruction length (in
8982    bytes).  In this implementation of DWARF, this field is used for
8983    information purposes only.  Since GCC generates assembly language,
8984    we have no a priori knowledge of how many instruction bytes are
8985    generated for each source line, and therefore can use only the
8986    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8987    commands.  Accordingly, we fix this as `1', which is "correct
8988    enough" for all architectures, and don't let the target override.  */
8989   dw2_asm_output_data (1, 1,
8990                        "Minimum Instruction Length");
8991
8992   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8993                        "Default is_stmt_start flag");
8994   dw2_asm_output_data (1, DWARF_LINE_BASE,
8995                        "Line Base Value (Special Opcodes)");
8996   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8997                        "Line Range Value (Special Opcodes)");
8998   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8999                        "Special Opcode Base");
9000
9001   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9002     {
9003       switch (opc)
9004         {
9005         case DW_LNS_advance_pc:
9006         case DW_LNS_advance_line:
9007         case DW_LNS_set_file:
9008         case DW_LNS_set_column:
9009         case DW_LNS_fixed_advance_pc:
9010           n_op_args = 1;
9011           break;
9012         default:
9013           n_op_args = 0;
9014           break;
9015         }
9016
9017       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9018                            opc, n_op_args);
9019     }
9020
9021   /* Write out the information about the files we use.  */
9022   output_file_names ();
9023   ASM_OUTPUT_LABEL (asm_out_file, p2);
9024
9025   /* We used to set the address register to the first location in the text
9026      section here, but that didn't accomplish anything since we already
9027      have a line note for the opening brace of the first function.  */
9028
9029   /* Generate the line number to PC correspondence table, encoded as
9030      a series of state machine operations.  */
9031   current_file = 1;
9032   current_line = 1;
9033
9034   if (cfun && in_cold_section_p)
9035     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9036   else
9037     strcpy (prev_line_label, text_section_label);
9038   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9039     {
9040       dw_line_info_ref line_info = &line_info_table[lt_index];
9041
9042 #if 0
9043       /* Disable this optimization for now; GDB wants to see two line notes
9044          at the beginning of a function so it can find the end of the
9045          prologue.  */
9046
9047       /* Don't emit anything for redundant notes.  Just updating the
9048          address doesn't accomplish anything, because we already assume
9049          that anything after the last address is this line.  */
9050       if (line_info->dw_line_num == current_line
9051           && line_info->dw_file_num == current_file)
9052         continue;
9053 #endif
9054
9055       /* Emit debug info for the address of the current line.
9056
9057          Unfortunately, we have little choice here currently, and must always
9058          use the most general form.  GCC does not know the address delta
9059          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9060          attributes which will give an upper bound on the address range.  We
9061          could perhaps use length attributes to determine when it is safe to
9062          use DW_LNS_fixed_advance_pc.  */
9063
9064       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9065       if (0)
9066         {
9067           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9068           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9069                                "DW_LNS_fixed_advance_pc");
9070           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9071         }
9072       else
9073         {
9074           /* This can handle any delta.  This takes
9075              4+DWARF2_ADDR_SIZE bytes.  */
9076           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9077           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9078           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9079           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9080         }
9081
9082       strcpy (prev_line_label, line_label);
9083
9084       /* Emit debug info for the source file of the current line, if
9085          different from the previous line.  */
9086       if (line_info->dw_file_num != current_file)
9087         {
9088           current_file = line_info->dw_file_num;
9089           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9090           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9091         }
9092
9093       /* Emit debug info for the current line number, choosing the encoding
9094          that uses the least amount of space.  */
9095       if (line_info->dw_line_num != current_line)
9096         {
9097           line_offset = line_info->dw_line_num - current_line;
9098           line_delta = line_offset - DWARF_LINE_BASE;
9099           current_line = line_info->dw_line_num;
9100           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9101             /* This can handle deltas from -10 to 234, using the current
9102                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9103                takes 1 byte.  */
9104             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9105                                  "line %lu", current_line);
9106           else
9107             {
9108               /* This can handle any delta.  This takes at least 4 bytes,
9109                  depending on the value being encoded.  */
9110               dw2_asm_output_data (1, DW_LNS_advance_line,
9111                                    "advance to line %lu", current_line);
9112               dw2_asm_output_data_sleb128 (line_offset, NULL);
9113               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9114             }
9115         }
9116       else
9117         /* We still need to start a new row, so output a copy insn.  */
9118         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9119     }
9120
9121   /* Emit debug info for the address of the end of the function.  */
9122   if (0)
9123     {
9124       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9125                            "DW_LNS_fixed_advance_pc");
9126       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9127     }
9128   else
9129     {
9130       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9131       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9132       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9133       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9134     }
9135
9136   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9137   dw2_asm_output_data_uleb128 (1, NULL);
9138   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9139
9140   function = 0;
9141   current_file = 1;
9142   current_line = 1;
9143   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9144     {
9145       dw_separate_line_info_ref line_info
9146         = &separate_line_info_table[lt_index];
9147
9148 #if 0
9149       /* Don't emit anything for redundant notes.  */
9150       if (line_info->dw_line_num == current_line
9151           && line_info->dw_file_num == current_file
9152           && line_info->function == function)
9153         goto cont;
9154 #endif
9155
9156       /* Emit debug info for the address of the current line.  If this is
9157          a new function, or the first line of a function, then we need
9158          to handle it differently.  */
9159       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9160                                    lt_index);
9161       if (function != line_info->function)
9162         {
9163           function = line_info->function;
9164
9165           /* Set the address register to the first line in the function.  */
9166           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9167           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9168           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9169           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9170         }
9171       else
9172         {
9173           /* ??? See the DW_LNS_advance_pc comment above.  */
9174           if (0)
9175             {
9176               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9177                                    "DW_LNS_fixed_advance_pc");
9178               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9179             }
9180           else
9181             {
9182               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9183               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9184               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9185               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9186             }
9187         }
9188
9189       strcpy (prev_line_label, line_label);
9190
9191       /* Emit debug info for the source file of the current line, if
9192          different from the previous line.  */
9193       if (line_info->dw_file_num != current_file)
9194         {
9195           current_file = line_info->dw_file_num;
9196           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9197           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9198         }
9199
9200       /* Emit debug info for the current line number, choosing the encoding
9201          that uses the least amount of space.  */
9202       if (line_info->dw_line_num != current_line)
9203         {
9204           line_offset = line_info->dw_line_num - current_line;
9205           line_delta = line_offset - DWARF_LINE_BASE;
9206           current_line = line_info->dw_line_num;
9207           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9208             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9209                                  "line %lu", current_line);
9210           else
9211             {
9212               dw2_asm_output_data (1, DW_LNS_advance_line,
9213                                    "advance to line %lu", current_line);
9214               dw2_asm_output_data_sleb128 (line_offset, NULL);
9215               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9216             }
9217         }
9218       else
9219         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9220
9221 #if 0
9222     cont:
9223 #endif
9224
9225       lt_index++;
9226
9227       /* If we're done with a function, end its sequence.  */
9228       if (lt_index == separate_line_info_table_in_use
9229           || separate_line_info_table[lt_index].function != function)
9230         {
9231           current_file = 1;
9232           current_line = 1;
9233
9234           /* Emit debug info for the address of the end of the function.  */
9235           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9236           if (0)
9237             {
9238               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9239                                    "DW_LNS_fixed_advance_pc");
9240               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9241             }
9242           else
9243             {
9244               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9245               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9246               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9247               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9248             }
9249
9250           /* Output the marker for the end of this sequence.  */
9251           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9252           dw2_asm_output_data_uleb128 (1, NULL);
9253           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9254         }
9255     }
9256
9257   /* Output the marker for the end of the line number info.  */
9258   ASM_OUTPUT_LABEL (asm_out_file, l2);
9259 }
9260 \f
9261 /* Given a pointer to a tree node for some base type, return a pointer to
9262    a DIE that describes the given type.
9263
9264    This routine must only be called for GCC type nodes that correspond to
9265    Dwarf base (fundamental) types.  */
9266
9267 static dw_die_ref
9268 base_type_die (tree type)
9269 {
9270   dw_die_ref base_type_result;
9271   enum dwarf_type encoding;
9272
9273   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9274     return 0;
9275
9276   switch (TREE_CODE (type))
9277     {
9278     case INTEGER_TYPE:
9279       if (TYPE_STRING_FLAG (type))
9280         {
9281           if (TYPE_UNSIGNED (type))
9282             encoding = DW_ATE_unsigned_char;
9283           else
9284             encoding = DW_ATE_signed_char;
9285         }
9286       else if (TYPE_UNSIGNED (type))
9287         encoding = DW_ATE_unsigned;
9288       else
9289         encoding = DW_ATE_signed;
9290       break;
9291
9292     case REAL_TYPE:
9293       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9294         encoding = DW_ATE_decimal_float;
9295       else
9296         encoding = DW_ATE_float;
9297       break;
9298
9299     case FIXED_POINT_TYPE:
9300       if (TYPE_UNSIGNED (type))
9301         encoding = DW_ATE_unsigned_fixed;
9302       else
9303         encoding = DW_ATE_signed_fixed;
9304       break;
9305
9306       /* Dwarf2 doesn't know anything about complex ints, so use
9307          a user defined type for it.  */
9308     case COMPLEX_TYPE:
9309       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9310         encoding = DW_ATE_complex_float;
9311       else
9312         encoding = DW_ATE_lo_user;
9313       break;
9314
9315     case BOOLEAN_TYPE:
9316       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9317       encoding = DW_ATE_boolean;
9318       break;
9319
9320     default:
9321       /* No other TREE_CODEs are Dwarf fundamental types.  */
9322       gcc_unreachable ();
9323     }
9324
9325   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9326
9327   /* This probably indicates a bug.  */
9328   if (! TYPE_NAME (type))
9329     add_name_attribute (base_type_result, "__unknown__");
9330
9331   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9332                    int_size_in_bytes (type));
9333   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9334
9335   return base_type_result;
9336 }
9337
9338 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9339    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9340
9341 static inline int
9342 is_base_type (tree type)
9343 {
9344   switch (TREE_CODE (type))
9345     {
9346     case ERROR_MARK:
9347     case VOID_TYPE:
9348     case INTEGER_TYPE:
9349     case REAL_TYPE:
9350     case FIXED_POINT_TYPE:
9351     case COMPLEX_TYPE:
9352     case BOOLEAN_TYPE:
9353       return 1;
9354
9355     case ARRAY_TYPE:
9356     case RECORD_TYPE:
9357     case UNION_TYPE:
9358     case QUAL_UNION_TYPE:
9359     case ENUMERAL_TYPE:
9360     case FUNCTION_TYPE:
9361     case METHOD_TYPE:
9362     case POINTER_TYPE:
9363     case REFERENCE_TYPE:
9364     case OFFSET_TYPE:
9365     case LANG_TYPE:
9366     case VECTOR_TYPE:
9367       return 0;
9368
9369     default:
9370       gcc_unreachable ();
9371     }
9372
9373   return 0;
9374 }
9375
9376 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9377    node, return the size in bits for the type if it is a constant, or else
9378    return the alignment for the type if the type's size is not constant, or
9379    else return BITS_PER_WORD if the type actually turns out to be an
9380    ERROR_MARK node.  */
9381
9382 static inline unsigned HOST_WIDE_INT
9383 simple_type_size_in_bits (const_tree type)
9384 {
9385   if (TREE_CODE (type) == ERROR_MARK)
9386     return BITS_PER_WORD;
9387   else if (TYPE_SIZE (type) == NULL_TREE)
9388     return 0;
9389   else if (host_integerp (TYPE_SIZE (type), 1))
9390     return tree_low_cst (TYPE_SIZE (type), 1);
9391   else
9392     return TYPE_ALIGN (type);
9393 }
9394
9395 /* Return true if the debug information for the given type should be
9396    emitted as a subrange type.  */
9397
9398 static inline bool
9399 is_subrange_type (const_tree type)
9400 {
9401   tree subtype = TREE_TYPE (type);
9402
9403   /* Subrange types are identified by the fact that they are integer
9404      types, and that they have a subtype which is either an integer type
9405      or an enumeral type.  */
9406
9407   if (TREE_CODE (type) != INTEGER_TYPE
9408       || subtype == NULL_TREE)
9409     return false;
9410
9411   if (TREE_CODE (subtype) != INTEGER_TYPE
9412       && TREE_CODE (subtype) != ENUMERAL_TYPE
9413       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9414     return false;
9415
9416   if (TREE_CODE (type) == TREE_CODE (subtype)
9417       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9418       && TYPE_MIN_VALUE (type) != NULL
9419       && TYPE_MIN_VALUE (subtype) != NULL
9420       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9421       && TYPE_MAX_VALUE (type) != NULL
9422       && TYPE_MAX_VALUE (subtype) != NULL
9423       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9424     {
9425       /* The type and its subtype have the same representation.  If in
9426          addition the two types also have the same name, then the given
9427          type is not a subrange type, but rather a plain base type.  */
9428       /* FIXME: brobecker/2004-03-22:
9429          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9430          therefore be sufficient to check the TYPE_SIZE node pointers
9431          rather than checking the actual size.  Unfortunately, we have
9432          found some cases, such as in the Ada "integer" type, where
9433          this is not the case.  Until this problem is solved, we need to
9434          keep checking the actual size.  */
9435       tree type_name = TYPE_NAME (type);
9436       tree subtype_name = TYPE_NAME (subtype);
9437
9438       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9439         type_name = DECL_NAME (type_name);
9440
9441       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9442         subtype_name = DECL_NAME (subtype_name);
9443
9444       if (type_name == subtype_name)
9445         return false;
9446     }
9447
9448   return true;
9449 }
9450
9451 /*  Given a pointer to a tree node for a subrange type, return a pointer
9452     to a DIE that describes the given type.  */
9453
9454 static dw_die_ref
9455 subrange_type_die (tree type, dw_die_ref context_die)
9456 {
9457   dw_die_ref subrange_die;
9458   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9459
9460   if (context_die == NULL)
9461     context_die = comp_unit_die;
9462
9463   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9464
9465   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9466     {
9467       /* The size of the subrange type and its base type do not match,
9468          so we need to generate a size attribute for the subrange type.  */
9469       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9470     }
9471
9472   if (TYPE_MIN_VALUE (type) != NULL)
9473     add_bound_info (subrange_die, DW_AT_lower_bound,
9474                     TYPE_MIN_VALUE (type));
9475   if (TYPE_MAX_VALUE (type) != NULL)
9476     add_bound_info (subrange_die, DW_AT_upper_bound,
9477                     TYPE_MAX_VALUE (type));
9478
9479   return subrange_die;
9480 }
9481
9482 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9483    entry that chains various modifiers in front of the given type.  */
9484
9485 static dw_die_ref
9486 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9487                    dw_die_ref context_die)
9488 {
9489   enum tree_code code = TREE_CODE (type);
9490   dw_die_ref mod_type_die;
9491   dw_die_ref sub_die = NULL;
9492   tree item_type = NULL;
9493   tree qualified_type;
9494   tree name;
9495
9496   if (code == ERROR_MARK)
9497     return NULL;
9498
9499   /* See if we already have the appropriately qualified variant of
9500      this type.  */
9501   qualified_type
9502     = get_qualified_type (type,
9503                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9504                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9505
9506   /* If we do, then we can just use its DIE, if it exists.  */
9507   if (qualified_type)
9508     {
9509       mod_type_die = lookup_type_die (qualified_type);
9510       if (mod_type_die)
9511         return mod_type_die;
9512     }
9513
9514   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9515
9516   /* Handle C typedef types.  */
9517   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9518     {
9519       tree dtype = TREE_TYPE (name);
9520
9521       if (qualified_type == dtype)
9522         {
9523           /* For a named type, use the typedef.  */
9524           gen_type_die (qualified_type, context_die);
9525           return lookup_type_die (qualified_type);
9526         }
9527       else if (is_const_type < TYPE_READONLY (dtype)
9528                || is_volatile_type < TYPE_VOLATILE (dtype)
9529                || (is_const_type <= TYPE_READONLY (dtype)
9530                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9531                    && DECL_ORIGINAL_TYPE (name) != type))
9532         /* cv-unqualified version of named type.  Just use the unnamed
9533            type to which it refers.  */
9534         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9535                                   is_const_type, is_volatile_type,
9536                                   context_die);
9537       /* Else cv-qualified version of named type; fall through.  */
9538     }
9539
9540   if (is_const_type)
9541     {
9542       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9543       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9544     }
9545   else if (is_volatile_type)
9546     {
9547       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9548       sub_die = modified_type_die (type, 0, 0, context_die);
9549     }
9550   else if (code == POINTER_TYPE)
9551     {
9552       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9553       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9554                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9555       item_type = TREE_TYPE (type);
9556     }
9557   else if (code == REFERENCE_TYPE)
9558     {
9559       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9560       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9561                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9562       item_type = TREE_TYPE (type);
9563     }
9564   else if (is_subrange_type (type))
9565     {
9566       mod_type_die = subrange_type_die (type, context_die);
9567       item_type = TREE_TYPE (type);
9568     }
9569   else if (is_base_type (type))
9570     mod_type_die = base_type_die (type);
9571   else
9572     {
9573       gen_type_die (type, context_die);
9574
9575       /* We have to get the type_main_variant here (and pass that to the
9576          `lookup_type_die' routine) because the ..._TYPE node we have
9577          might simply be a *copy* of some original type node (where the
9578          copy was created to help us keep track of typedef names) and
9579          that copy might have a different TYPE_UID from the original
9580          ..._TYPE node.  */
9581       if (TREE_CODE (type) != VECTOR_TYPE)
9582         return lookup_type_die (type_main_variant (type));
9583       else
9584         /* Vectors have the debugging information in the type,
9585            not the main variant.  */
9586         return lookup_type_die (type);
9587     }
9588
9589   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9590      don't output a DW_TAG_typedef, since there isn't one in the
9591      user's program; just attach a DW_AT_name to the type.  */
9592   if (name
9593       && (TREE_CODE (name) != TYPE_DECL
9594           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9595     {
9596       if (TREE_CODE (name) == TYPE_DECL)
9597         /* Could just call add_name_and_src_coords_attributes here,
9598            but since this is a builtin type it doesn't have any
9599            useful source coordinates anyway.  */
9600         name = DECL_NAME (name);
9601       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9602     }
9603
9604   if (qualified_type)
9605     equate_type_number_to_die (qualified_type, mod_type_die);
9606
9607   if (item_type)
9608     /* We must do this after the equate_type_number_to_die call, in case
9609        this is a recursive type.  This ensures that the modified_type_die
9610        recursion will terminate even if the type is recursive.  Recursive
9611        types are possible in Ada.  */
9612     sub_die = modified_type_die (item_type,
9613                                  TYPE_READONLY (item_type),
9614                                  TYPE_VOLATILE (item_type),
9615                                  context_die);
9616
9617   if (sub_die != NULL)
9618     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9619
9620   return mod_type_die;
9621 }
9622
9623 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9624    an enumerated type.  */
9625
9626 static inline int
9627 type_is_enum (const_tree type)
9628 {
9629   return TREE_CODE (type) == ENUMERAL_TYPE;
9630 }
9631
9632 /* Return the DBX register number described by a given RTL node.  */
9633
9634 static unsigned int
9635 dbx_reg_number (const_rtx rtl)
9636 {
9637   unsigned regno = REGNO (rtl);
9638
9639   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9640
9641 #ifdef LEAF_REG_REMAP
9642   if (current_function_uses_only_leaf_regs)
9643     {
9644       int leaf_reg = LEAF_REG_REMAP (regno);
9645       if (leaf_reg != -1)
9646         regno = (unsigned) leaf_reg;
9647     }
9648 #endif
9649
9650   return DBX_REGISTER_NUMBER (regno);
9651 }
9652
9653 /* Optionally add a DW_OP_piece term to a location description expression.
9654    DW_OP_piece is only added if the location description expression already
9655    doesn't end with DW_OP_piece.  */
9656
9657 static void
9658 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9659 {
9660   dw_loc_descr_ref loc;
9661
9662   if (*list_head != NULL)
9663     {
9664       /* Find the end of the chain.  */
9665       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9666         ;
9667
9668       if (loc->dw_loc_opc != DW_OP_piece)
9669         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9670     }
9671 }
9672
9673 /* Return a location descriptor that designates a machine register or
9674    zero if there is none.  */
9675
9676 static dw_loc_descr_ref
9677 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9678 {
9679   rtx regs;
9680
9681   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9682     return 0;
9683
9684   regs = targetm.dwarf_register_span (rtl);
9685
9686   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9687     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9688   else
9689     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9690 }
9691
9692 /* Return a location descriptor that designates a machine register for
9693    a given hard register number.  */
9694
9695 static dw_loc_descr_ref
9696 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9697 {
9698   dw_loc_descr_ref reg_loc_descr;
9699
9700   if (regno <= 31)
9701     reg_loc_descr
9702       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9703   else
9704     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9705
9706   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9707     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9708
9709   return reg_loc_descr;
9710 }
9711
9712 /* Given an RTL of a register, return a location descriptor that
9713    designates a value that spans more than one register.  */
9714
9715 static dw_loc_descr_ref
9716 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9717                              enum var_init_status initialized)
9718 {
9719   int nregs, size, i;
9720   unsigned reg;
9721   dw_loc_descr_ref loc_result = NULL;
9722
9723   reg = REGNO (rtl);
9724 #ifdef LEAF_REG_REMAP
9725   if (current_function_uses_only_leaf_regs)
9726     {
9727       int leaf_reg = LEAF_REG_REMAP (reg);
9728       if (leaf_reg != -1)
9729         reg = (unsigned) leaf_reg;
9730     }
9731 #endif
9732   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9733   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9734
9735   /* Simple, contiguous registers.  */
9736   if (regs == NULL_RTX)
9737     {
9738       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9739
9740       loc_result = NULL;
9741       while (nregs--)
9742         {
9743           dw_loc_descr_ref t;
9744
9745           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9746                                       VAR_INIT_STATUS_INITIALIZED);
9747           add_loc_descr (&loc_result, t);
9748           add_loc_descr_op_piece (&loc_result, size);
9749           ++reg;
9750         }
9751       return loc_result;
9752     }
9753
9754   /* Now onto stupid register sets in non contiguous locations.  */
9755
9756   gcc_assert (GET_CODE (regs) == PARALLEL);
9757
9758   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9759   loc_result = NULL;
9760
9761   for (i = 0; i < XVECLEN (regs, 0); ++i)
9762     {
9763       dw_loc_descr_ref t;
9764
9765       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9766                                   VAR_INIT_STATUS_INITIALIZED);
9767       add_loc_descr (&loc_result, t);
9768       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9769       add_loc_descr_op_piece (&loc_result, size);
9770     }
9771
9772   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9773     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9774   return loc_result;
9775 }
9776
9777 #endif /* DWARF2_DEBUGGING_INFO */
9778
9779 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9780
9781 /* Return a location descriptor that designates a constant.  */
9782
9783 static dw_loc_descr_ref
9784 int_loc_descriptor (HOST_WIDE_INT i)
9785 {
9786   enum dwarf_location_atom op;
9787
9788   /* Pick the smallest representation of a constant, rather than just
9789      defaulting to the LEB encoding.  */
9790   if (i >= 0)
9791     {
9792       if (i <= 31)
9793         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9794       else if (i <= 0xff)
9795         op = DW_OP_const1u;
9796       else if (i <= 0xffff)
9797         op = DW_OP_const2u;
9798       else if (HOST_BITS_PER_WIDE_INT == 32
9799                || i <= 0xffffffff)
9800         op = DW_OP_const4u;
9801       else
9802         op = DW_OP_constu;
9803     }
9804   else
9805     {
9806       if (i >= -0x80)
9807         op = DW_OP_const1s;
9808       else if (i >= -0x8000)
9809         op = DW_OP_const2s;
9810       else if (HOST_BITS_PER_WIDE_INT == 32
9811                || i >= -0x80000000)
9812         op = DW_OP_const4s;
9813       else
9814         op = DW_OP_consts;
9815     }
9816
9817   return new_loc_descr (op, i, 0);
9818 }
9819 #endif
9820
9821 #ifdef DWARF2_DEBUGGING_INFO
9822
9823 /* Return a location descriptor that designates a base+offset location.  */
9824
9825 static dw_loc_descr_ref
9826 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9827                  enum var_init_status initialized)
9828 {
9829   unsigned int regno;
9830   dw_loc_descr_ref result;
9831   dw_fde_ref fde = current_fde ();
9832
9833   /* We only use "frame base" when we're sure we're talking about the
9834      post-prologue local stack frame.  We do this by *not* running
9835      register elimination until this point, and recognizing the special
9836      argument pointer and soft frame pointer rtx's.  */
9837   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9838     {
9839       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9840
9841       if (elim != reg)
9842         {
9843           if (GET_CODE (elim) == PLUS)
9844             {
9845               offset += INTVAL (XEXP (elim, 1));
9846               elim = XEXP (elim, 0);
9847             }
9848           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9849                        && (elim == hard_frame_pointer_rtx
9850                            || elim == stack_pointer_rtx))
9851                       || elim == (frame_pointer_needed
9852                                   ? hard_frame_pointer_rtx
9853                                   : stack_pointer_rtx));
9854
9855           /* If drap register is used to align stack, use frame
9856              pointer + offset to access stack variables.  If stack
9857              is aligned without drap, use stack pointer + offset to
9858              access stack variables.  */
9859           if (crtl->stack_realign_tried
9860               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9861               && reg == frame_pointer_rtx)
9862             {
9863               int base_reg
9864                 = DWARF_FRAME_REGNUM (cfa.indirect
9865                                       ? HARD_FRAME_POINTER_REGNUM
9866                                       : STACK_POINTER_REGNUM);
9867               return new_reg_loc_descr (base_reg, offset);
9868             }
9869
9870           offset += frame_pointer_fb_offset;
9871           return new_loc_descr (DW_OP_fbreg, offset, 0);
9872         }
9873     }
9874   else if (fde
9875            && fde->drap_reg != INVALID_REGNUM
9876            && (fde->drap_reg == REGNO (reg)
9877                || fde->vdrap_reg == REGNO (reg)))
9878     {
9879       /* Use cfa+offset to represent the location of arguments passed
9880          on stack when drap is used to align stack.  */
9881       return new_loc_descr (DW_OP_fbreg, offset, 0);
9882     }
9883
9884   regno = dbx_reg_number (reg);
9885   if (regno <= 31)
9886     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
9887                             offset, 0);
9888   else
9889     result = new_loc_descr (DW_OP_bregx, regno, offset);
9890
9891   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9892     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9893
9894   return result;
9895 }
9896
9897 /* Return true if this RTL expression describes a base+offset calculation.  */
9898
9899 static inline int
9900 is_based_loc (const_rtx rtl)
9901 {
9902   return (GET_CODE (rtl) == PLUS
9903           && ((REG_P (XEXP (rtl, 0))
9904                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9905                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9906 }
9907
9908 /* Return a descriptor that describes the concatenation of N locations
9909    used to form the address of a memory location.  */
9910
9911 static dw_loc_descr_ref
9912 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9913                             enum var_init_status initialized)
9914 {
9915   unsigned int i;
9916   dw_loc_descr_ref cc_loc_result = NULL;
9917   unsigned int n = XVECLEN (concatn, 0);
9918
9919   for (i = 0; i < n; ++i)
9920     {
9921       dw_loc_descr_ref ref;
9922       rtx x = XVECEXP (concatn, 0, i);
9923
9924       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9925       if (ref == NULL)
9926         return NULL;
9927
9928       add_loc_descr (&cc_loc_result, ref);
9929       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9930     }
9931
9932   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9933     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9934
9935   return cc_loc_result;
9936 }
9937
9938 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9939    failed.  */
9940
9941 static dw_loc_descr_ref
9942 tls_mem_loc_descriptor (rtx mem)
9943 {
9944   tree base;
9945   dw_loc_descr_ref loc_result;
9946
9947   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9948     return NULL;
9949
9950   base = get_base_address (MEM_EXPR (mem));
9951   if (base == NULL
9952       || TREE_CODE (base) != VAR_DECL
9953       || !DECL_THREAD_LOCAL_P (base))
9954     return NULL;
9955
9956   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9957   if (loc_result == NULL)
9958     return NULL;
9959
9960   if (INTVAL (MEM_OFFSET (mem)))
9961     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
9962
9963   return loc_result;
9964 }
9965
9966 /* The following routine converts the RTL for a variable or parameter
9967    (resident in memory) into an equivalent Dwarf representation of a
9968    mechanism for getting the address of that same variable onto the top of a
9969    hypothetical "address evaluation" stack.
9970
9971    When creating memory location descriptors, we are effectively transforming
9972    the RTL for a memory-resident object into its Dwarf postfix expression
9973    equivalent.  This routine recursively descends an RTL tree, turning
9974    it into Dwarf postfix code as it goes.
9975
9976    MODE is the mode of the memory reference, needed to handle some
9977    autoincrement addressing modes.
9978
9979    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9980    location list for RTL.
9981
9982    Return 0 if we can't represent the location.  */
9983
9984 static dw_loc_descr_ref
9985 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9986                     enum var_init_status initialized)
9987 {
9988   dw_loc_descr_ref mem_loc_result = NULL;
9989   enum dwarf_location_atom op;
9990
9991   /* Note that for a dynamically sized array, the location we will generate a
9992      description of here will be the lowest numbered location which is
9993      actually within the array.  That's *not* necessarily the same as the
9994      zeroth element of the array.  */
9995
9996   rtl = targetm.delegitimize_address (rtl);
9997
9998   switch (GET_CODE (rtl))
9999     {
10000     case POST_INC:
10001     case POST_DEC:
10002     case POST_MODIFY:
10003       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10004          just fall into the SUBREG code.  */
10005
10006       /* ... fall through ...  */
10007
10008     case SUBREG:
10009       /* The case of a subreg may arise when we have a local (register)
10010          variable or a formal (register) parameter which doesn't quite fill
10011          up an entire register.  For now, just assume that it is
10012          legitimate to make the Dwarf info refer to the whole register which
10013          contains the given subreg.  */
10014       rtl = XEXP (rtl, 0);
10015
10016       /* ... fall through ...  */
10017
10018     case REG:
10019       /* Whenever a register number forms a part of the description of the
10020          method for calculating the (dynamic) address of a memory resident
10021          object, DWARF rules require the register number be referred to as
10022          a "base register".  This distinction is not based in any way upon
10023          what category of register the hardware believes the given register
10024          belongs to.  This is strictly DWARF terminology we're dealing with
10025          here. Note that in cases where the location of a memory-resident
10026          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10027          OP_CONST (0)) the actual DWARF location descriptor that we generate
10028          may just be OP_BASEREG (basereg).  This may look deceptively like
10029          the object in question was allocated to a register (rather than in
10030          memory) so DWARF consumers need to be aware of the subtle
10031          distinction between OP_REG and OP_BASEREG.  */
10032       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10033         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10034       else if (stack_realign_drap
10035                && crtl->drap_reg
10036                && crtl->args.internal_arg_pointer == rtl
10037                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10038         {
10039           /* If RTL is internal_arg_pointer, which has been optimized
10040              out, use DRAP instead.  */
10041           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10042                                             VAR_INIT_STATUS_INITIALIZED);
10043         }
10044       break;
10045
10046     case MEM:
10047       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10048                                            VAR_INIT_STATUS_INITIALIZED);
10049       if (mem_loc_result == NULL)
10050         mem_loc_result = tls_mem_loc_descriptor (rtl);
10051       if (mem_loc_result != 0)
10052         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10053       break;
10054
10055     case LO_SUM:
10056          rtl = XEXP (rtl, 1);
10057
10058       /* ... fall through ...  */
10059
10060     case LABEL_REF:
10061       /* Some ports can transform a symbol ref into a label ref, because
10062          the symbol ref is too far away and has to be dumped into a constant
10063          pool.  */
10064     case CONST:
10065     case SYMBOL_REF:
10066       /* Alternatively, the symbol in the constant pool might be referenced
10067          by a different symbol.  */
10068       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10069         {
10070           bool marked;
10071           rtx tmp = get_pool_constant_mark (rtl, &marked);
10072
10073           if (GET_CODE (tmp) == SYMBOL_REF)
10074             {
10075               rtl = tmp;
10076               if (CONSTANT_POOL_ADDRESS_P (tmp))
10077                 get_pool_constant_mark (tmp, &marked);
10078               else
10079                 marked = true;
10080             }
10081
10082           /* If all references to this pool constant were optimized away,
10083              it was not output and thus we can't represent it.
10084              FIXME: might try to use DW_OP_const_value here, though
10085              DW_OP_piece complicates it.  */
10086           if (!marked)
10087             return 0;
10088         }
10089
10090       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10091       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10092       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10093       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10094       break;
10095
10096     case PRE_MODIFY:
10097       /* Extract the PLUS expression nested inside and fall into
10098          PLUS code below.  */
10099       rtl = XEXP (rtl, 1);
10100       goto plus;
10101
10102     case PRE_INC:
10103     case PRE_DEC:
10104       /* Turn these into a PLUS expression and fall into the PLUS code
10105          below.  */
10106       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10107                           GEN_INT (GET_CODE (rtl) == PRE_INC
10108                                    ? GET_MODE_UNIT_SIZE (mode)
10109                                    : -GET_MODE_UNIT_SIZE (mode)));
10110
10111       /* ... fall through ...  */
10112
10113     case PLUS:
10114     plus:
10115       if (is_based_loc (rtl))
10116         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10117                                           INTVAL (XEXP (rtl, 1)),
10118                                           VAR_INIT_STATUS_INITIALIZED);
10119       else
10120         {
10121           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10122                                                VAR_INIT_STATUS_INITIALIZED);
10123           if (mem_loc_result == 0)
10124             break;
10125
10126           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT)
10127             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10128           else
10129             {
10130               dw_loc_descr_ref mem_loc_result2
10131                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10132                                       VAR_INIT_STATUS_INITIALIZED);
10133               if (mem_loc_result2 == 0)
10134                 break;
10135               add_loc_descr (&mem_loc_result, mem_loc_result2);
10136               add_loc_descr (&mem_loc_result,
10137                              new_loc_descr (DW_OP_plus, 0, 0));
10138             }
10139         }
10140       break;
10141
10142     /* If a pseudo-reg is optimized away, it is possible for it to
10143        be replaced with a MEM containing a multiply or shift.  */
10144     case MULT:
10145       op = DW_OP_mul;
10146       goto do_binop;
10147
10148     case ASHIFT:
10149       op = DW_OP_shl;
10150       goto do_binop;
10151
10152     case ASHIFTRT:
10153       op = DW_OP_shra;
10154       goto do_binop;
10155
10156     case LSHIFTRT:
10157       op = DW_OP_shr;
10158       goto do_binop;
10159
10160     do_binop:
10161       {
10162         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10163                                                    VAR_INIT_STATUS_INITIALIZED);
10164         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10165                                                    VAR_INIT_STATUS_INITIALIZED);
10166
10167         if (op0 == 0 || op1 == 0)
10168           break;
10169
10170         mem_loc_result = op0;
10171         add_loc_descr (&mem_loc_result, op1);
10172         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10173         break;
10174       }
10175
10176     case CONST_INT:
10177       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10178       break;
10179
10180     case CONCATN:
10181       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10182                                                    VAR_INIT_STATUS_INITIALIZED);
10183       break;
10184
10185     case UNSPEC:
10186       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10187          can't express it in the debug info.  This can happen e.g. with some
10188          TLS UNSPECs.  */
10189       break;
10190
10191     default:
10192       gcc_unreachable ();
10193     }
10194
10195   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10196     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10197
10198   return mem_loc_result;
10199 }
10200
10201 /* Return a descriptor that describes the concatenation of two locations.
10202    This is typically a complex variable.  */
10203
10204 static dw_loc_descr_ref
10205 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10206 {
10207   dw_loc_descr_ref cc_loc_result = NULL;
10208   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10209   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10210
10211   if (x0_ref == 0 || x1_ref == 0)
10212     return 0;
10213
10214   cc_loc_result = x0_ref;
10215   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10216
10217   add_loc_descr (&cc_loc_result, x1_ref);
10218   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10219
10220   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10221     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10222
10223   return cc_loc_result;
10224 }
10225
10226 /* Return a descriptor that describes the concatenation of N
10227    locations.  */
10228
10229 static dw_loc_descr_ref
10230 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10231 {
10232   unsigned int i;
10233   dw_loc_descr_ref cc_loc_result = NULL;
10234   unsigned int n = XVECLEN (concatn, 0);
10235
10236   for (i = 0; i < n; ++i)
10237     {
10238       dw_loc_descr_ref ref;
10239       rtx x = XVECEXP (concatn, 0, i);
10240
10241       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10242       if (ref == NULL)
10243         return NULL;
10244
10245       add_loc_descr (&cc_loc_result, ref);
10246       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10247     }
10248
10249   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10250     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10251
10252   return cc_loc_result;
10253 }
10254
10255 /* Output a proper Dwarf location descriptor for a variable or parameter
10256    which is either allocated in a register or in a memory location.  For a
10257    register, we just generate an OP_REG and the register number.  For a
10258    memory location we provide a Dwarf postfix expression describing how to
10259    generate the (dynamic) address of the object onto the address stack.
10260
10261    If we don't know how to describe it, return 0.  */
10262
10263 static dw_loc_descr_ref
10264 loc_descriptor (rtx rtl, enum var_init_status initialized)
10265 {
10266   dw_loc_descr_ref loc_result = NULL;
10267
10268   switch (GET_CODE (rtl))
10269     {
10270     case SUBREG:
10271       /* The case of a subreg may arise when we have a local (register)
10272          variable or a formal (register) parameter which doesn't quite fill
10273          up an entire register.  For now, just assume that it is
10274          legitimate to make the Dwarf info refer to the whole register which
10275          contains the given subreg.  */
10276       rtl = SUBREG_REG (rtl);
10277
10278       /* ... fall through ...  */
10279
10280     case REG:
10281       loc_result = reg_loc_descriptor (rtl, initialized);
10282       break;
10283
10284     case MEM:
10285       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10286                                        initialized);
10287       if (loc_result == NULL)
10288         loc_result = tls_mem_loc_descriptor (rtl);
10289       break;
10290
10291     case CONCAT:
10292       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10293                                           initialized);
10294       break;
10295
10296     case CONCATN:
10297       loc_result = concatn_loc_descriptor (rtl, initialized);
10298       break;
10299
10300     case VAR_LOCATION:
10301       /* Single part.  */
10302       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10303         {
10304           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10305           break;
10306         }
10307
10308       rtl = XEXP (rtl, 1);
10309       /* FALLTHRU */
10310
10311     case PARALLEL:
10312       {
10313         rtvec par_elems = XVEC (rtl, 0);
10314         int num_elem = GET_NUM_ELEM (par_elems);
10315         enum machine_mode mode;
10316         int i;
10317
10318         /* Create the first one, so we have something to add to.  */
10319         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10320                                      initialized);
10321         if (loc_result == NULL)
10322           return NULL;
10323         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10324         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10325         for (i = 1; i < num_elem; i++)
10326           {
10327             dw_loc_descr_ref temp;
10328
10329             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10330                                    initialized);
10331             if (temp == NULL)
10332               return NULL;
10333             add_loc_descr (&loc_result, temp);
10334             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10335             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10336           }
10337       }
10338       break;
10339
10340     default:
10341       gcc_unreachable ();
10342     }
10343
10344   return loc_result;
10345 }
10346
10347 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10348    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10349    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10350    top-level invocation, and we require the address of LOC; is 0 if we require
10351    the value of LOC.  */
10352
10353 static dw_loc_descr_ref
10354 loc_descriptor_from_tree_1 (tree loc, int want_address)
10355 {
10356   dw_loc_descr_ref ret, ret1;
10357   int have_address = 0;
10358   enum dwarf_location_atom op;
10359
10360   /* ??? Most of the time we do not take proper care for sign/zero
10361      extending the values properly.  Hopefully this won't be a real
10362      problem...  */
10363
10364   switch (TREE_CODE (loc))
10365     {
10366     case ERROR_MARK:
10367       return 0;
10368
10369     case PLACEHOLDER_EXPR:
10370       /* This case involves extracting fields from an object to determine the
10371          position of other fields.  We don't try to encode this here.  The
10372          only user of this is Ada, which encodes the needed information using
10373          the names of types.  */
10374       return 0;
10375
10376     case CALL_EXPR:
10377       return 0;
10378
10379     case PREINCREMENT_EXPR:
10380     case PREDECREMENT_EXPR:
10381     case POSTINCREMENT_EXPR:
10382     case POSTDECREMENT_EXPR:
10383       /* There are no opcodes for these operations.  */
10384       return 0;
10385
10386     case ADDR_EXPR:
10387       /* If we already want an address, there's nothing we can do.  */
10388       if (want_address)
10389         return 0;
10390
10391       /* Otherwise, process the argument and look for the address.  */
10392       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10393
10394     case VAR_DECL:
10395       if (DECL_THREAD_LOCAL_P (loc))
10396         {
10397           rtx rtl;
10398           enum dwarf_location_atom first_op;
10399           enum dwarf_location_atom second_op;
10400
10401           if (targetm.have_tls)
10402             {
10403               /* If this is not defined, we have no way to emit the
10404                  data.  */
10405               if (!targetm.asm_out.output_dwarf_dtprel)
10406                 return 0;
10407
10408                /* The way DW_OP_GNU_push_tls_address is specified, we
10409                   can only look up addresses of objects in the current
10410                   module.  */
10411               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10412                 return 0;
10413               first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10414               second_op = DW_OP_GNU_push_tls_address;
10415             }
10416           else
10417             {
10418               if (!targetm.emutls.debug_form_tls_address)
10419                 return 0;
10420               loc = emutls_decl (loc);
10421               first_op = DW_OP_addr;
10422               second_op = DW_OP_form_tls_address;
10423             }
10424
10425           rtl = rtl_for_decl_location (loc);
10426           if (rtl == NULL_RTX)
10427             return 0;
10428
10429           if (!MEM_P (rtl))
10430             return 0;
10431           rtl = XEXP (rtl, 0);
10432           if (! CONSTANT_P (rtl))
10433             return 0;
10434
10435           ret = new_loc_descr (first_op, 0, 0);
10436           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10437           ret->dw_loc_oprnd1.v.val_addr = rtl;
10438
10439           ret1 = new_loc_descr (second_op, 0, 0);
10440           add_loc_descr (&ret, ret1);
10441
10442           have_address = 1;
10443           break;
10444         }
10445       /* FALLTHRU */
10446
10447     case PARM_DECL:
10448       if (DECL_HAS_VALUE_EXPR_P (loc))
10449         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10450                                            want_address);
10451       /* FALLTHRU */
10452
10453     case RESULT_DECL:
10454     case FUNCTION_DECL:
10455       {
10456         rtx rtl = rtl_for_decl_location (loc);
10457
10458         if (rtl == NULL_RTX)
10459           return 0;
10460         else if (GET_CODE (rtl) == CONST_INT)
10461           {
10462             HOST_WIDE_INT val = INTVAL (rtl);
10463             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10464               val &= GET_MODE_MASK (DECL_MODE (loc));
10465             ret = int_loc_descriptor (val);
10466           }
10467         else if (GET_CODE (rtl) == CONST_STRING)
10468           return 0;
10469         else if (CONSTANT_P (rtl))
10470           {
10471             ret = new_loc_descr (DW_OP_addr, 0, 0);
10472             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10473             ret->dw_loc_oprnd1.v.val_addr = rtl;
10474           }
10475         else
10476           {
10477             enum machine_mode mode;
10478
10479             /* Certain constructs can only be represented at top-level.  */
10480             if (want_address == 2)
10481               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10482
10483             mode = GET_MODE (rtl);
10484             if (MEM_P (rtl))
10485               {
10486                 rtl = XEXP (rtl, 0);
10487                 have_address = 1;
10488               }
10489             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10490           }
10491       }
10492       break;
10493
10494     case INDIRECT_REF:
10495       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10496       have_address = 1;
10497       break;
10498
10499     case COMPOUND_EXPR:
10500       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10501
10502     CASE_CONVERT:
10503     case VIEW_CONVERT_EXPR:
10504     case SAVE_EXPR:
10505     case MODIFY_EXPR:
10506       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10507
10508     case COMPONENT_REF:
10509     case BIT_FIELD_REF:
10510     case ARRAY_REF:
10511     case ARRAY_RANGE_REF:
10512       {
10513         tree obj, offset;
10514         HOST_WIDE_INT bitsize, bitpos, bytepos;
10515         enum machine_mode mode;
10516         int volatilep;
10517         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10518
10519         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10520                                    &unsignedp, &volatilep, false);
10521
10522         if (obj == loc)
10523           return 0;
10524
10525         ret = loc_descriptor_from_tree_1 (obj, 1);
10526         if (ret == 0
10527             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10528           return 0;
10529
10530         if (offset != NULL_TREE)
10531           {
10532             /* Variable offset.  */
10533             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10534             if (ret1 == 0)
10535               return 0;
10536             add_loc_descr (&ret, ret1);
10537             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10538           }
10539
10540         bytepos = bitpos / BITS_PER_UNIT;
10541         loc_descr_plus_const (&ret, bytepos);
10542
10543         have_address = 1;
10544         break;
10545       }
10546
10547     case INTEGER_CST:
10548       if (host_integerp (loc, 0))
10549         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10550       else
10551         return 0;
10552       break;
10553
10554     case CONSTRUCTOR:
10555       {
10556         /* Get an RTL for this, if something has been emitted.  */
10557         rtx rtl = lookup_constant_def (loc);
10558         enum machine_mode mode;
10559
10560         if (!rtl || !MEM_P (rtl))
10561           return 0;
10562         mode = GET_MODE (rtl);
10563         rtl = XEXP (rtl, 0);
10564         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10565         have_address = 1;
10566         break;
10567       }
10568
10569     case TRUTH_AND_EXPR:
10570     case TRUTH_ANDIF_EXPR:
10571     case BIT_AND_EXPR:
10572       op = DW_OP_and;
10573       goto do_binop;
10574
10575     case TRUTH_XOR_EXPR:
10576     case BIT_XOR_EXPR:
10577       op = DW_OP_xor;
10578       goto do_binop;
10579
10580     case TRUTH_OR_EXPR:
10581     case TRUTH_ORIF_EXPR:
10582     case BIT_IOR_EXPR:
10583       op = DW_OP_or;
10584       goto do_binop;
10585
10586     case FLOOR_DIV_EXPR:
10587     case CEIL_DIV_EXPR:
10588     case ROUND_DIV_EXPR:
10589     case TRUNC_DIV_EXPR:
10590       op = DW_OP_div;
10591       goto do_binop;
10592
10593     case MINUS_EXPR:
10594       op = DW_OP_minus;
10595       goto do_binop;
10596
10597     case FLOOR_MOD_EXPR:
10598     case CEIL_MOD_EXPR:
10599     case ROUND_MOD_EXPR:
10600     case TRUNC_MOD_EXPR:
10601       op = DW_OP_mod;
10602       goto do_binop;
10603
10604     case MULT_EXPR:
10605       op = DW_OP_mul;
10606       goto do_binop;
10607
10608     case LSHIFT_EXPR:
10609       op = DW_OP_shl;
10610       goto do_binop;
10611
10612     case RSHIFT_EXPR:
10613       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10614       goto do_binop;
10615
10616     case POINTER_PLUS_EXPR:
10617     case PLUS_EXPR:
10618       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10619           && host_integerp (TREE_OPERAND (loc, 1), 0))
10620         {
10621           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10622           if (ret == 0)
10623             return 0;
10624
10625           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
10626           break;
10627         }
10628
10629       op = DW_OP_plus;
10630       goto do_binop;
10631
10632     case LE_EXPR:
10633       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10634         return 0;
10635
10636       op = DW_OP_le;
10637       goto do_binop;
10638
10639     case GE_EXPR:
10640       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10641         return 0;
10642
10643       op = DW_OP_ge;
10644       goto do_binop;
10645
10646     case LT_EXPR:
10647       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10648         return 0;
10649
10650       op = DW_OP_lt;
10651       goto do_binop;
10652
10653     case GT_EXPR:
10654       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10655         return 0;
10656
10657       op = DW_OP_gt;
10658       goto do_binop;
10659
10660     case EQ_EXPR:
10661       op = DW_OP_eq;
10662       goto do_binop;
10663
10664     case NE_EXPR:
10665       op = DW_OP_ne;
10666       goto do_binop;
10667
10668     do_binop:
10669       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10670       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10671       if (ret == 0 || ret1 == 0)
10672         return 0;
10673
10674       add_loc_descr (&ret, ret1);
10675       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10676       break;
10677
10678     case TRUTH_NOT_EXPR:
10679     case BIT_NOT_EXPR:
10680       op = DW_OP_not;
10681       goto do_unop;
10682
10683     case ABS_EXPR:
10684       op = DW_OP_abs;
10685       goto do_unop;
10686
10687     case NEGATE_EXPR:
10688       op = DW_OP_neg;
10689       goto do_unop;
10690
10691     do_unop:
10692       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10693       if (ret == 0)
10694         return 0;
10695
10696       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10697       break;
10698
10699     case MIN_EXPR:
10700     case MAX_EXPR:
10701       {
10702         const enum tree_code code =
10703           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10704
10705         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10706                       build2 (code, integer_type_node,
10707                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10708                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10709       }
10710
10711       /* ... fall through ...  */
10712
10713     case COND_EXPR:
10714       {
10715         dw_loc_descr_ref lhs
10716           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10717         dw_loc_descr_ref rhs
10718           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10719         dw_loc_descr_ref bra_node, jump_node, tmp;
10720
10721         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10722         if (ret == 0 || lhs == 0 || rhs == 0)
10723           return 0;
10724
10725         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10726         add_loc_descr (&ret, bra_node);
10727
10728         add_loc_descr (&ret, rhs);
10729         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10730         add_loc_descr (&ret, jump_node);
10731
10732         add_loc_descr (&ret, lhs);
10733         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10734         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10735
10736         /* ??? Need a node to point the skip at.  Use a nop.  */
10737         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10738         add_loc_descr (&ret, tmp);
10739         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10740         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10741       }
10742       break;
10743
10744     case FIX_TRUNC_EXPR:
10745       return 0;
10746
10747     default:
10748       /* Leave front-end specific codes as simply unknown.  This comes
10749          up, for instance, with the C STMT_EXPR.  */
10750       if ((unsigned int) TREE_CODE (loc)
10751           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10752         return 0;
10753
10754 #ifdef ENABLE_CHECKING
10755       /* Otherwise this is a generic code; we should just lists all of
10756          these explicitly.  We forgot one.  */
10757       gcc_unreachable ();
10758 #else
10759       /* In a release build, we want to degrade gracefully: better to
10760          generate incomplete debugging information than to crash.  */
10761       return NULL;
10762 #endif
10763     }
10764
10765   /* Show if we can't fill the request for an address.  */
10766   if (want_address && !have_address)
10767     return 0;
10768
10769   /* If we've got an address and don't want one, dereference.  */
10770   if (!want_address && have_address && ret)
10771     {
10772       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10773
10774       if (size > DWARF2_ADDR_SIZE || size == -1)
10775         return 0;
10776       else if (size == DWARF2_ADDR_SIZE)
10777         op = DW_OP_deref;
10778       else
10779         op = DW_OP_deref_size;
10780
10781       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10782     }
10783
10784   return ret;
10785 }
10786
10787 static inline dw_loc_descr_ref
10788 loc_descriptor_from_tree (tree loc)
10789 {
10790   return loc_descriptor_from_tree_1 (loc, 2);
10791 }
10792
10793 /* Given a value, round it up to the lowest multiple of `boundary'
10794    which is not less than the value itself.  */
10795
10796 static inline HOST_WIDE_INT
10797 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10798 {
10799   return (((value + boundary - 1) / boundary) * boundary);
10800 }
10801
10802 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10803    pointer to the declared type for the relevant field variable, or return
10804    `integer_type_node' if the given node turns out to be an
10805    ERROR_MARK node.  */
10806
10807 static inline tree
10808 field_type (const_tree decl)
10809 {
10810   tree type;
10811
10812   if (TREE_CODE (decl) == ERROR_MARK)
10813     return integer_type_node;
10814
10815   type = DECL_BIT_FIELD_TYPE (decl);
10816   if (type == NULL_TREE)
10817     type = TREE_TYPE (decl);
10818
10819   return type;
10820 }
10821
10822 /* Given a pointer to a tree node, return the alignment in bits for
10823    it, or else return BITS_PER_WORD if the node actually turns out to
10824    be an ERROR_MARK node.  */
10825
10826 static inline unsigned
10827 simple_type_align_in_bits (const_tree type)
10828 {
10829   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10830 }
10831
10832 static inline unsigned
10833 simple_decl_align_in_bits (const_tree decl)
10834 {
10835   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10836 }
10837
10838 /* Return the result of rounding T up to ALIGN.  */
10839
10840 static inline HOST_WIDE_INT
10841 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10842 {
10843   /* We must be careful if T is negative because HOST_WIDE_INT can be
10844      either "above" or "below" unsigned int as per the C promotion
10845      rules, depending on the host, thus making the signedness of the
10846      direct multiplication and division unpredictable.  */
10847   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10848
10849   u += align - 1;
10850   u /= align;
10851   u *= align;
10852
10853   return (HOST_WIDE_INT) u;
10854 }
10855
10856 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10857    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10858    or return 0 if we are unable to determine what that offset is, either
10859    because the argument turns out to be a pointer to an ERROR_MARK node, or
10860    because the offset is actually variable.  (We can't handle the latter case
10861    just yet).  */
10862
10863 static HOST_WIDE_INT
10864 field_byte_offset (const_tree decl)
10865 {
10866   HOST_WIDE_INT object_offset_in_bits;
10867   HOST_WIDE_INT bitpos_int;
10868
10869   if (TREE_CODE (decl) == ERROR_MARK)
10870     return 0;
10871
10872   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10873
10874   /* We cannot yet cope with fields whose positions are variable, so
10875      for now, when we see such things, we simply return 0.  Someday, we may
10876      be able to handle such cases, but it will be damn difficult.  */
10877   if (! host_integerp (bit_position (decl), 0))
10878     return 0;
10879
10880   bitpos_int = int_bit_position (decl);
10881
10882 #ifdef PCC_BITFIELD_TYPE_MATTERS
10883   if (PCC_BITFIELD_TYPE_MATTERS)
10884     {
10885       tree type;
10886       tree field_size_tree;
10887       HOST_WIDE_INT deepest_bitpos;
10888       unsigned HOST_WIDE_INT field_size_in_bits;
10889       unsigned int type_align_in_bits;
10890       unsigned int decl_align_in_bits;
10891       unsigned HOST_WIDE_INT type_size_in_bits;
10892
10893       type = field_type (decl);
10894       type_size_in_bits = simple_type_size_in_bits (type);
10895       type_align_in_bits = simple_type_align_in_bits (type);
10896
10897       field_size_tree = DECL_SIZE (decl);
10898
10899       /* The size could be unspecified if there was an error, or for
10900          a flexible array member.  */
10901       if (!field_size_tree)
10902         field_size_tree = bitsize_zero_node;
10903
10904       /* If the size of the field is not constant, use the type size.  */
10905       if (host_integerp (field_size_tree, 1))
10906         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10907       else
10908         field_size_in_bits = type_size_in_bits;
10909
10910       decl_align_in_bits = simple_decl_align_in_bits (decl);
10911
10912       /* The GCC front-end doesn't make any attempt to keep track of the
10913          starting bit offset (relative to the start of the containing
10914          structure type) of the hypothetical "containing object" for a
10915          bit-field.  Thus, when computing the byte offset value for the
10916          start of the "containing object" of a bit-field, we must deduce
10917          this information on our own. This can be rather tricky to do in
10918          some cases.  For example, handling the following structure type
10919          definition when compiling for an i386/i486 target (which only
10920          aligns long long's to 32-bit boundaries) can be very tricky:
10921
10922          struct S { int field1; long long field2:31; };
10923
10924          Fortunately, there is a simple rule-of-thumb which can be used
10925          in such cases.  When compiling for an i386/i486, GCC will
10926          allocate 8 bytes for the structure shown above.  It decides to
10927          do this based upon one simple rule for bit-field allocation.
10928          GCC allocates each "containing object" for each bit-field at
10929          the first (i.e. lowest addressed) legitimate alignment boundary
10930          (based upon the required minimum alignment for the declared
10931          type of the field) which it can possibly use, subject to the
10932          condition that there is still enough available space remaining
10933          in the containing object (when allocated at the selected point)
10934          to fully accommodate all of the bits of the bit-field itself.
10935
10936          This simple rule makes it obvious why GCC allocates 8 bytes for
10937          each object of the structure type shown above.  When looking
10938          for a place to allocate the "containing object" for `field2',
10939          the compiler simply tries to allocate a 64-bit "containing
10940          object" at each successive 32-bit boundary (starting at zero)
10941          until it finds a place to allocate that 64- bit field such that
10942          at least 31 contiguous (and previously unallocated) bits remain
10943          within that selected 64 bit field.  (As it turns out, for the
10944          example above, the compiler finds it is OK to allocate the
10945          "containing object" 64-bit field at bit-offset zero within the
10946          structure type.)
10947
10948          Here we attempt to work backwards from the limited set of facts
10949          we're given, and we try to deduce from those facts, where GCC
10950          must have believed that the containing object started (within
10951          the structure type). The value we deduce is then used (by the
10952          callers of this routine) to generate DW_AT_location and
10953          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10954          the case of DW_AT_location, regular fields as well).  */
10955
10956       /* Figure out the bit-distance from the start of the structure to
10957          the "deepest" bit of the bit-field.  */
10958       deepest_bitpos = bitpos_int + field_size_in_bits;
10959
10960       /* This is the tricky part.  Use some fancy footwork to deduce
10961          where the lowest addressed bit of the containing object must
10962          be.  */
10963       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10964
10965       /* Round up to type_align by default.  This works best for
10966          bitfields.  */
10967       object_offset_in_bits
10968         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10969
10970       if (object_offset_in_bits > bitpos_int)
10971         {
10972           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10973
10974           /* Round up to decl_align instead.  */
10975           object_offset_in_bits
10976             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10977         }
10978     }
10979   else
10980 #endif
10981     object_offset_in_bits = bitpos_int;
10982
10983   return object_offset_in_bits / BITS_PER_UNIT;
10984 }
10985 \f
10986 /* The following routines define various Dwarf attributes and any data
10987    associated with them.  */
10988
10989 /* Add a location description attribute value to a DIE.
10990
10991    This emits location attributes suitable for whole variables and
10992    whole parameters.  Note that the location attributes for struct fields are
10993    generated by the routine `data_member_location_attribute' below.  */
10994
10995 static inline void
10996 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10997                              dw_loc_descr_ref descr)
10998 {
10999   if (descr != 0)
11000     add_AT_loc (die, attr_kind, descr);
11001 }
11002
11003 /* Attach the specialized form of location attribute used for data members of
11004    struct and union types.  In the special case of a FIELD_DECL node which
11005    represents a bit-field, the "offset" part of this special location
11006    descriptor must indicate the distance in bytes from the lowest-addressed
11007    byte of the containing struct or union type to the lowest-addressed byte of
11008    the "containing object" for the bit-field.  (See the `field_byte_offset'
11009    function above).
11010
11011    For any given bit-field, the "containing object" is a hypothetical object
11012    (of some integral or enum type) within which the given bit-field lives.  The
11013    type of this hypothetical "containing object" is always the same as the
11014    declared type of the individual bit-field itself (for GCC anyway... the
11015    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11016    bytes) of the hypothetical "containing object" which will be given in the
11017    DW_AT_byte_size attribute for this bit-field.  (See the
11018    `byte_size_attribute' function below.)  It is also used when calculating the
11019    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11020    function below.)  */
11021
11022 static void
11023 add_data_member_location_attribute (dw_die_ref die, tree decl)
11024 {
11025   HOST_WIDE_INT offset;
11026   dw_loc_descr_ref loc_descr = 0;
11027
11028   if (TREE_CODE (decl) == TREE_BINFO)
11029     {
11030       /* We're working on the TAG_inheritance for a base class.  */
11031       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11032         {
11033           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11034              aren't at a fixed offset from all (sub)objects of the same
11035              type.  We need to extract the appropriate offset from our
11036              vtable.  The following dwarf expression means
11037
11038                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11039
11040              This is specific to the V3 ABI, of course.  */
11041
11042           dw_loc_descr_ref tmp;
11043
11044           /* Make a copy of the object address.  */
11045           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11046           add_loc_descr (&loc_descr, tmp);
11047
11048           /* Extract the vtable address.  */
11049           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11050           add_loc_descr (&loc_descr, tmp);
11051
11052           /* Calculate the address of the offset.  */
11053           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11054           gcc_assert (offset < 0);
11055
11056           tmp = int_loc_descriptor (-offset);
11057           add_loc_descr (&loc_descr, tmp);
11058           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11059           add_loc_descr (&loc_descr, tmp);
11060
11061           /* Extract the offset.  */
11062           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11063           add_loc_descr (&loc_descr, tmp);
11064
11065           /* Add it to the object address.  */
11066           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11067           add_loc_descr (&loc_descr, tmp);
11068         }
11069       else
11070         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11071     }
11072   else
11073     offset = field_byte_offset (decl);
11074
11075   if (! loc_descr)
11076     {
11077       enum dwarf_location_atom op;
11078
11079       /* The DWARF2 standard says that we should assume that the structure
11080          address is already on the stack, so we can specify a structure field
11081          address by using DW_OP_plus_uconst.  */
11082
11083 #ifdef MIPS_DEBUGGING_INFO
11084       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11085          operator correctly.  It works only if we leave the offset on the
11086          stack.  */
11087       op = DW_OP_constu;
11088 #else
11089       op = DW_OP_plus_uconst;
11090 #endif
11091
11092       loc_descr = new_loc_descr (op, offset, 0);
11093     }
11094
11095   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11096 }
11097
11098 /* Writes integer values to dw_vec_const array.  */
11099
11100 static void
11101 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11102 {
11103   while (size != 0)
11104     {
11105       *dest++ = val & 0xff;
11106       val >>= 8;
11107       --size;
11108     }
11109 }
11110
11111 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11112
11113 static HOST_WIDE_INT
11114 extract_int (const unsigned char *src, unsigned int size)
11115 {
11116   HOST_WIDE_INT val = 0;
11117
11118   src += size;
11119   while (size != 0)
11120     {
11121       val <<= 8;
11122       val |= *--src & 0xff;
11123       --size;
11124     }
11125   return val;
11126 }
11127
11128 /* Writes floating point values to dw_vec_const array.  */
11129
11130 static void
11131 insert_float (const_rtx rtl, unsigned char *array)
11132 {
11133   REAL_VALUE_TYPE rv;
11134   long val[4];
11135   int i;
11136
11137   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11138   real_to_target (val, &rv, GET_MODE (rtl));
11139
11140   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11141   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11142     {
11143       insert_int (val[i], 4, array);
11144       array += 4;
11145     }
11146 }
11147
11148 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11149    does not have a "location" either in memory or in a register.  These
11150    things can arise in GNU C when a constant is passed as an actual parameter
11151    to an inlined function.  They can also arise in C++ where declared
11152    constants do not necessarily get memory "homes".  */
11153
11154 static void
11155 add_const_value_attribute (dw_die_ref die, rtx rtl)
11156 {
11157   switch (GET_CODE (rtl))
11158     {
11159     case CONST_INT:
11160       {
11161         HOST_WIDE_INT val = INTVAL (rtl);
11162
11163         if (val < 0)
11164           add_AT_int (die, DW_AT_const_value, val);
11165         else
11166           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11167       }
11168       break;
11169
11170     case CONST_DOUBLE:
11171       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11172          floating-point constant.  A CONST_DOUBLE is used whenever the
11173          constant requires more than one word in order to be adequately
11174          represented.  We output CONST_DOUBLEs as blocks.  */
11175       {
11176         enum machine_mode mode = GET_MODE (rtl);
11177
11178         if (SCALAR_FLOAT_MODE_P (mode))
11179           {
11180             unsigned int length = GET_MODE_SIZE (mode);
11181             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11182
11183             insert_float (rtl, array);
11184             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11185           }
11186         else
11187           {
11188             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11189             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11190
11191             add_AT_long_long (die, DW_AT_const_value,
11192                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11193           }
11194       }
11195       break;
11196
11197     case CONST_VECTOR:
11198       {
11199         enum machine_mode mode = GET_MODE (rtl);
11200         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11201         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11202         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11203         unsigned int i;
11204         unsigned char *p;
11205
11206         switch (GET_MODE_CLASS (mode))
11207           {
11208           case MODE_VECTOR_INT:
11209             for (i = 0, p = array; i < length; i++, p += elt_size)
11210               {
11211                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11212                 HOST_WIDE_INT lo, hi;
11213
11214                 switch (GET_CODE (elt))
11215                   {
11216                   case CONST_INT:
11217                     lo = INTVAL (elt);
11218                     hi = -(lo < 0);
11219                     break;
11220
11221                   case CONST_DOUBLE:
11222                     lo = CONST_DOUBLE_LOW (elt);
11223                     hi = CONST_DOUBLE_HIGH (elt);
11224                     break;
11225
11226                   default:
11227                     gcc_unreachable ();
11228                   }
11229
11230                 if (elt_size <= sizeof (HOST_WIDE_INT))
11231                   insert_int (lo, elt_size, p);
11232                 else
11233                   {
11234                     unsigned char *p0 = p;
11235                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11236
11237                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11238                     if (WORDS_BIG_ENDIAN)
11239                       {
11240                         p0 = p1;
11241                         p1 = p;
11242                       }
11243                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11244                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11245                   }
11246               }
11247             break;
11248
11249           case MODE_VECTOR_FLOAT:
11250             for (i = 0, p = array; i < length; i++, p += elt_size)
11251               {
11252                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11253                 insert_float (elt, p);
11254               }
11255             break;
11256
11257           default:
11258             gcc_unreachable ();
11259           }
11260
11261         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11262       }
11263       break;
11264
11265     case CONST_STRING:
11266       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11267       break;
11268
11269     case SYMBOL_REF:
11270     case LABEL_REF:
11271     case CONST:
11272       add_AT_addr (die, DW_AT_const_value, rtl);
11273       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11274       break;
11275
11276     case PLUS:
11277       /* In cases where an inlined instance of an inline function is passed
11278          the address of an `auto' variable (which is local to the caller) we
11279          can get a situation where the DECL_RTL of the artificial local
11280          variable (for the inlining) which acts as a stand-in for the
11281          corresponding formal parameter (of the inline function) will look
11282          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11283          exactly a compile-time constant expression, but it isn't the address
11284          of the (artificial) local variable either.  Rather, it represents the
11285          *value* which the artificial local variable always has during its
11286          lifetime.  We currently have no way to represent such quasi-constant
11287          values in Dwarf, so for now we just punt and generate nothing.  */
11288       break;
11289
11290     default:
11291       /* No other kinds of rtx should be possible here.  */
11292       gcc_unreachable ();
11293     }
11294
11295 }
11296
11297 /* Determine whether the evaluation of EXPR references any variables
11298    or functions which aren't otherwise used (and therefore may not be
11299    output).  */
11300 static tree
11301 reference_to_unused (tree * tp, int * walk_subtrees,
11302                      void * data ATTRIBUTE_UNUSED)
11303 {
11304   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11305     *walk_subtrees = 0;
11306
11307   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11308       && ! TREE_ASM_WRITTEN (*tp))
11309     return *tp;
11310   /* ???  The C++ FE emits debug information for using decls, so
11311      putting gcc_unreachable here falls over.  See PR31899.  For now
11312      be conservative.  */
11313   else if (!cgraph_global_info_ready
11314            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11315     return *tp;
11316   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11317     {
11318       struct varpool_node *node = varpool_node (*tp);
11319       if (!node->needed)
11320         return *tp;
11321     }
11322   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11323            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11324     {
11325       struct cgraph_node *node = cgraph_node (*tp);
11326       if (node->process || TREE_ASM_WRITTEN (*tp))
11327         return *tp;
11328     }
11329   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11330     return *tp;
11331
11332   return NULL_TREE;
11333 }
11334
11335 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11336    for use in a later add_const_value_attribute call.  */
11337
11338 static rtx
11339 rtl_for_decl_init (tree init, tree type)
11340 {
11341   rtx rtl = NULL_RTX;
11342
11343   /* If a variable is initialized with a string constant without embedded
11344      zeros, build CONST_STRING.  */
11345   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11346     {
11347       tree enttype = TREE_TYPE (type);
11348       tree domain = TYPE_DOMAIN (type);
11349       enum machine_mode mode = TYPE_MODE (enttype);
11350
11351       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11352           && domain
11353           && integer_zerop (TYPE_MIN_VALUE (domain))
11354           && compare_tree_int (TYPE_MAX_VALUE (domain),
11355                                TREE_STRING_LENGTH (init) - 1) == 0
11356           && ((size_t) TREE_STRING_LENGTH (init)
11357               == strlen (TREE_STRING_POINTER (init)) + 1))
11358         rtl = gen_rtx_CONST_STRING (VOIDmode,
11359                                     ggc_strdup (TREE_STRING_POINTER (init)));
11360     }
11361   /* Other aggregates, and complex values, could be represented using
11362      CONCAT: FIXME!  */
11363   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11364     ;
11365   /* Vectors only work if their mode is supported by the target.
11366      FIXME: generic vectors ought to work too.  */
11367   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11368     ;
11369   /* If the initializer is something that we know will expand into an
11370      immediate RTL constant, expand it now.  We must be careful not to
11371      reference variables which won't be output.  */
11372   else if (initializer_constant_valid_p (init, type)
11373            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11374     {
11375       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11376          possible.  */
11377       if (TREE_CODE (type) == VECTOR_TYPE)
11378         switch (TREE_CODE (init))
11379           {
11380           case VECTOR_CST:
11381             break;
11382           case CONSTRUCTOR:
11383             if (TREE_CONSTANT (init))
11384               {
11385                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11386                 bool constant_p = true;
11387                 tree value;
11388                 unsigned HOST_WIDE_INT ix;
11389
11390                 /* Even when ctor is constant, it might contain non-*_CST
11391                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11392                    belong into VECTOR_CST nodes.  */
11393                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11394                   if (!CONSTANT_CLASS_P (value))
11395                     {
11396                       constant_p = false;
11397                       break;
11398                     }
11399
11400                 if (constant_p)
11401                   {
11402                     init = build_vector_from_ctor (type, elts);
11403                     break;
11404                   }
11405               }
11406             /* FALLTHRU */
11407
11408           default:
11409             return NULL;
11410           }
11411
11412       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11413
11414       /* If expand_expr returns a MEM, it wasn't immediate.  */
11415       gcc_assert (!rtl || !MEM_P (rtl));
11416     }
11417
11418   return rtl;
11419 }
11420
11421 /* Generate RTL for the variable DECL to represent its location.  */
11422
11423 static rtx
11424 rtl_for_decl_location (tree decl)
11425 {
11426   rtx rtl;
11427
11428   /* Here we have to decide where we are going to say the parameter "lives"
11429      (as far as the debugger is concerned).  We only have a couple of
11430      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11431
11432      DECL_RTL normally indicates where the parameter lives during most of the
11433      activation of the function.  If optimization is enabled however, this
11434      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11435      that the parameter doesn't really live anywhere (as far as the code
11436      generation parts of GCC are concerned) during most of the function's
11437      activation.  That will happen (for example) if the parameter is never
11438      referenced within the function.
11439
11440      We could just generate a location descriptor here for all non-NULL
11441      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11442      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11443      where DECL_RTL is NULL or is a pseudo-reg.
11444
11445      Note however that we can only get away with using DECL_INCOMING_RTL as
11446      a backup substitute for DECL_RTL in certain limited cases.  In cases
11447      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11448      we can be sure that the parameter was passed using the same type as it is
11449      declared to have within the function, and that its DECL_INCOMING_RTL
11450      points us to a place where a value of that type is passed.
11451
11452      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11453      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11454      because in these cases DECL_INCOMING_RTL points us to a value of some
11455      type which is *different* from the type of the parameter itself.  Thus,
11456      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11457      such cases, the debugger would end up (for example) trying to fetch a
11458      `float' from a place which actually contains the first part of a
11459      `double'.  That would lead to really incorrect and confusing
11460      output at debug-time.
11461
11462      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11463      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11464      are a couple of exceptions however.  On little-endian machines we can
11465      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11466      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11467      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11468      when (on a little-endian machine) a non-prototyped function has a
11469      parameter declared to be of type `short' or `char'.  In such cases,
11470      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11471      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11472      passed `int' value.  If the debugger then uses that address to fetch
11473      a `short' or a `char' (on a little-endian machine) the result will be
11474      the correct data, so we allow for such exceptional cases below.
11475
11476      Note that our goal here is to describe the place where the given formal
11477      parameter lives during most of the function's activation (i.e. between the
11478      end of the prologue and the start of the epilogue).  We'll do that as best
11479      as we can. Note however that if the given formal parameter is modified
11480      sometime during the execution of the function, then a stack backtrace (at
11481      debug-time) will show the function as having been called with the *new*
11482      value rather than the value which was originally passed in.  This happens
11483      rarely enough that it is not a major problem, but it *is* a problem, and
11484      I'd like to fix it.
11485
11486      A future version of dwarf2out.c may generate two additional attributes for
11487      any given DW_TAG_formal_parameter DIE which will describe the "passed
11488      type" and the "passed location" for the given formal parameter in addition
11489      to the attributes we now generate to indicate the "declared type" and the
11490      "active location" for each parameter.  This additional set of attributes
11491      could be used by debuggers for stack backtraces. Separately, note that
11492      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11493      This happens (for example) for inlined-instances of inline function formal
11494      parameters which are never referenced.  This really shouldn't be
11495      happening.  All PARM_DECL nodes should get valid non-NULL
11496      DECL_INCOMING_RTL values.  FIXME.  */
11497
11498   /* Use DECL_RTL as the "location" unless we find something better.  */
11499   rtl = DECL_RTL_IF_SET (decl);
11500
11501   /* When generating abstract instances, ignore everything except
11502      constants, symbols living in memory, and symbols living in
11503      fixed registers.  */
11504   if (! reload_completed)
11505     {
11506       if (rtl
11507           && (CONSTANT_P (rtl)
11508               || (MEM_P (rtl)
11509                   && CONSTANT_P (XEXP (rtl, 0)))
11510               || (REG_P (rtl)
11511                   && TREE_CODE (decl) == VAR_DECL
11512                   && TREE_STATIC (decl))))
11513         {
11514           rtl = targetm.delegitimize_address (rtl);
11515           return rtl;
11516         }
11517       rtl = NULL_RTX;
11518     }
11519   else if (TREE_CODE (decl) == PARM_DECL)
11520     {
11521       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11522         {
11523           tree declared_type = TREE_TYPE (decl);
11524           tree passed_type = DECL_ARG_TYPE (decl);
11525           enum machine_mode dmode = TYPE_MODE (declared_type);
11526           enum machine_mode pmode = TYPE_MODE (passed_type);
11527
11528           /* This decl represents a formal parameter which was optimized out.
11529              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11530              all cases where (rtl == NULL_RTX) just below.  */
11531           if (dmode == pmode)
11532             rtl = DECL_INCOMING_RTL (decl);
11533           else if (SCALAR_INT_MODE_P (dmode)
11534                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11535                    && DECL_INCOMING_RTL (decl))
11536             {
11537               rtx inc = DECL_INCOMING_RTL (decl);
11538               if (REG_P (inc))
11539                 rtl = inc;
11540               else if (MEM_P (inc))
11541                 {
11542                   if (BYTES_BIG_ENDIAN)
11543                     rtl = adjust_address_nv (inc, dmode,
11544                                              GET_MODE_SIZE (pmode)
11545                                              - GET_MODE_SIZE (dmode));
11546                   else
11547                     rtl = inc;
11548                 }
11549             }
11550         }
11551
11552       /* If the parm was passed in registers, but lives on the stack, then
11553          make a big endian correction if the mode of the type of the
11554          parameter is not the same as the mode of the rtl.  */
11555       /* ??? This is the same series of checks that are made in dbxout.c before
11556          we reach the big endian correction code there.  It isn't clear if all
11557          of these checks are necessary here, but keeping them all is the safe
11558          thing to do.  */
11559       else if (MEM_P (rtl)
11560                && XEXP (rtl, 0) != const0_rtx
11561                && ! CONSTANT_P (XEXP (rtl, 0))
11562                /* Not passed in memory.  */
11563                && !MEM_P (DECL_INCOMING_RTL (decl))
11564                /* Not passed by invisible reference.  */
11565                && (!REG_P (XEXP (rtl, 0))
11566                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11567                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11568 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11569                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11570 #endif
11571                      )
11572                /* Big endian correction check.  */
11573                && BYTES_BIG_ENDIAN
11574                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11575                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11576                    < UNITS_PER_WORD))
11577         {
11578           int offset = (UNITS_PER_WORD
11579                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11580
11581           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11582                              plus_constant (XEXP (rtl, 0), offset));
11583         }
11584     }
11585   else if (TREE_CODE (decl) == VAR_DECL
11586            && rtl
11587            && MEM_P (rtl)
11588            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11589            && BYTES_BIG_ENDIAN)
11590     {
11591       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11592       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11593
11594       /* If a variable is declared "register" yet is smaller than
11595          a register, then if we store the variable to memory, it
11596          looks like we're storing a register-sized value, when in
11597          fact we are not.  We need to adjust the offset of the
11598          storage location to reflect the actual value's bytes,
11599          else gdb will not be able to display it.  */
11600       if (rsize > dsize)
11601         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11602                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11603     }
11604
11605   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11606      and will have been substituted directly into all expressions that use it.
11607      C does not have such a concept, but C++ and other languages do.  */
11608   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11609     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11610
11611   if (rtl)
11612     rtl = targetm.delegitimize_address (rtl);
11613
11614   /* If we don't look past the constant pool, we risk emitting a
11615      reference to a constant pool entry that isn't referenced from
11616      code, and thus is not emitted.  */
11617   if (rtl)
11618     rtl = avoid_constant_pool_reference (rtl);
11619
11620   return rtl;
11621 }
11622
11623 /* We need to figure out what section we should use as the base for the
11624    address ranges where a given location is valid.
11625    1. If this particular DECL has a section associated with it, use that.
11626    2. If this function has a section associated with it, use that.
11627    3. Otherwise, use the text section.
11628    XXX: If you split a variable across multiple sections, we won't notice.  */
11629
11630 static const char *
11631 secname_for_decl (const_tree decl)
11632 {
11633   const char *secname;
11634
11635   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11636     {
11637       tree sectree = DECL_SECTION_NAME (decl);
11638       secname = TREE_STRING_POINTER (sectree);
11639     }
11640   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11641     {
11642       tree sectree = DECL_SECTION_NAME (current_function_decl);
11643       secname = TREE_STRING_POINTER (sectree);
11644     }
11645   else if (cfun && in_cold_section_p)
11646     secname = crtl->subsections.cold_section_label;
11647   else
11648     secname = text_section_label;
11649
11650   return secname;
11651 }
11652
11653 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11654    returned.  If so, the decl for the COMMON block is returned, and the
11655    value is the offset into the common block for the symbol.  */
11656
11657 static tree
11658 fortran_common (tree decl, HOST_WIDE_INT *value)
11659 {
11660   tree val_expr, cvar;
11661   enum machine_mode mode;
11662   HOST_WIDE_INT bitsize, bitpos;
11663   tree offset;
11664   int volatilep = 0, unsignedp = 0;
11665
11666   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11667      it does not have a value (the offset into the common area), or if it
11668      is thread local (as opposed to global) then it isn't common, and shouldn't
11669      be handled as such.  */
11670   if (TREE_CODE (decl) != VAR_DECL
11671       || !TREE_PUBLIC (decl)
11672       || !TREE_STATIC (decl)
11673       || !DECL_HAS_VALUE_EXPR_P (decl)
11674       || !is_fortran ())
11675     return NULL_TREE;
11676
11677   val_expr = DECL_VALUE_EXPR (decl);
11678   if (TREE_CODE (val_expr) != COMPONENT_REF)
11679     return NULL_TREE;
11680
11681   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11682                               &mode, &unsignedp, &volatilep, true);
11683
11684   if (cvar == NULL_TREE
11685       || TREE_CODE (cvar) != VAR_DECL
11686       || DECL_ARTIFICIAL (cvar)
11687       || !TREE_PUBLIC (cvar))
11688     return NULL_TREE;
11689
11690   *value = 0;
11691   if (offset != NULL)
11692     {
11693       if (!host_integerp (offset, 0))
11694         return NULL_TREE;
11695       *value = tree_low_cst (offset, 0);
11696     }
11697   if (bitpos != 0)
11698     *value += bitpos / BITS_PER_UNIT;
11699
11700   return cvar;
11701 }
11702
11703 /* Dereference a location expression LOC if DECL is passed by invisible
11704    reference.  */
11705
11706 static dw_loc_descr_ref
11707 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11708 {
11709   HOST_WIDE_INT size;
11710   enum dwarf_location_atom op;
11711
11712   if (loc == NULL)
11713     return NULL;
11714
11715   if ((TREE_CODE (decl) != PARM_DECL
11716        && TREE_CODE (decl) != RESULT_DECL
11717        && TREE_CODE (decl) != VAR_DECL)
11718       || !DECL_BY_REFERENCE (decl))
11719     return loc;
11720
11721   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
11722      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
11723      location expression is considered to be address of a memory location,
11724      rather than the register itself.  */
11725   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
11726        || loc->dw_loc_opc == DW_OP_regx)
11727       && (loc->dw_loc_next == NULL
11728           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
11729               && loc->dw_loc_next->dw_loc_next == NULL)))
11730     {
11731       if (loc->dw_loc_opc == DW_OP_regx)
11732         {
11733           loc->dw_loc_opc = DW_OP_bregx;
11734           loc->dw_loc_oprnd2.v.val_int = 0;
11735         }
11736       else
11737         {
11738           loc->dw_loc_opc
11739             = (enum dwarf_location_atom)
11740               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
11741           loc->dw_loc_oprnd1.v.val_int = 0;
11742         }
11743       return loc;
11744     }
11745
11746   size = int_size_in_bytes (TREE_TYPE (decl));
11747   if (size > DWARF2_ADDR_SIZE || size == -1)
11748     return 0;
11749   else if (size == DWARF2_ADDR_SIZE)
11750     op = DW_OP_deref;
11751   else
11752     op = DW_OP_deref_size;
11753   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11754   return loc;
11755 }
11756
11757 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11758    data attribute for a variable or a parameter.  We generate the
11759    DW_AT_const_value attribute only in those cases where the given variable
11760    or parameter does not have a true "location" either in memory or in a
11761    register.  This can happen (for example) when a constant is passed as an
11762    actual argument in a call to an inline function.  (It's possible that
11763    these things can crop up in other ways also.)  Note that one type of
11764    constant value which can be passed into an inlined function is a constant
11765    pointer.  This can happen for example if an actual argument in an inlined
11766    function call evaluates to a compile-time constant address.  */
11767
11768 static void
11769 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11770                                        enum dwarf_attribute attr)
11771 {
11772   rtx rtl;
11773   dw_loc_descr_ref descr;
11774   var_loc_list *loc_list;
11775   struct var_loc_node *node;
11776   if (TREE_CODE (decl) == ERROR_MARK)
11777     return;
11778
11779   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11780               || TREE_CODE (decl) == RESULT_DECL);
11781
11782   /* See if we possibly have multiple locations for this variable.  */
11783   loc_list = lookup_decl_loc (decl);
11784
11785   /* If it truly has multiple locations, the first and last node will
11786      differ.  */
11787   if (loc_list && loc_list->first != loc_list->last)
11788     {
11789       const char *endname, *secname;
11790       dw_loc_list_ref list;
11791       rtx varloc;
11792       enum var_init_status initialized;
11793
11794       /* Now that we know what section we are using for a base,
11795          actually construct the list of locations.
11796          The first location information is what is passed to the
11797          function that creates the location list, and the remaining
11798          locations just get added on to that list.
11799          Note that we only know the start address for a location
11800          (IE location changes), so to build the range, we use
11801          the range [current location start, next location start].
11802          This means we have to special case the last node, and generate
11803          a range of [last location start, end of function label].  */
11804
11805       node = loc_list->first;
11806       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11807       secname = secname_for_decl (decl);
11808
11809       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11810         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11811       else
11812         initialized = VAR_INIT_STATUS_INITIALIZED;
11813
11814       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11815       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11816       node = node->next;
11817
11818       for (; node->next; node = node->next)
11819         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11820           {
11821             /* The variable has a location between NODE->LABEL and
11822                NODE->NEXT->LABEL.  */
11823             enum var_init_status initialized =
11824               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11825             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11826             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11827                                       decl);
11828             add_loc_descr_to_loc_list (&list, descr,
11829                                        node->label, node->next->label, secname);
11830           }
11831
11832       /* If the variable has a location at the last label
11833          it keeps its location until the end of function.  */
11834       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11835         {
11836           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11837           enum var_init_status initialized =
11838             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11839
11840           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11841           if (!current_function_decl)
11842             endname = text_end_label;
11843           else
11844             {
11845               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11846                                            current_function_funcdef_no);
11847               endname = ggc_strdup (label_id);
11848             }
11849           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11850                                     decl);
11851           add_loc_descr_to_loc_list (&list, descr,
11852                                      node->label, endname, secname);
11853         }
11854
11855       /* Finally, add the location list to the DIE, and we are done.  */
11856       add_AT_loc_list (die, attr, list);
11857       return;
11858     }
11859
11860   /* Try to get some constant RTL for this decl, and use that as the value of
11861      the location.  */
11862
11863   rtl = rtl_for_decl_location (decl);
11864   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11865     {
11866       add_const_value_attribute (die, rtl);
11867       return;
11868     }
11869
11870   /* If we have tried to generate the location otherwise, and it
11871      didn't work out (we wouldn't be here if we did), and we have a one entry
11872      location list, try generating a location from that.  */
11873   if (loc_list && loc_list->first)
11874     {
11875       enum var_init_status status;
11876       node = loc_list->first;
11877       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11878       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11879       if (descr)
11880         {
11881           descr = loc_by_reference (descr, decl);
11882           add_AT_location_description (die, attr, descr);
11883           return;
11884         }
11885     }
11886
11887   /* We couldn't get any rtl, so try directly generating the location
11888      description from the tree.  */
11889   descr = loc_descriptor_from_tree (decl);
11890   if (descr)
11891     {
11892       descr = loc_by_reference (descr, decl);
11893       add_AT_location_description (die, attr, descr);
11894       return;
11895     }
11896   /* None of that worked, so it must not really have a location;
11897      try adding a constant value attribute from the DECL_INITIAL.  */
11898   tree_add_const_value_attribute (die, decl);
11899 }
11900
11901 /* Add VARIABLE and DIE into deferred locations list.  */
11902
11903 static void
11904 defer_location (tree variable, dw_die_ref die)
11905 {
11906   deferred_locations entry;
11907   entry.variable = variable;
11908   entry.die = die;
11909   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
11910 }
11911
11912 /* Helper function for tree_add_const_value_attribute.  Natively encode
11913    initializer INIT into an array.  Return true if successful.  */
11914
11915 static bool
11916 native_encode_initializer (tree init, unsigned char *array, int size)
11917 {
11918   tree type;
11919
11920   if (init == NULL_TREE)
11921     return false;
11922
11923   STRIP_NOPS (init);
11924   switch (TREE_CODE (init))
11925     {
11926     case STRING_CST:
11927       type = TREE_TYPE (init);
11928       if (TREE_CODE (type) == ARRAY_TYPE)
11929         {
11930           tree enttype = TREE_TYPE (type);
11931           enum machine_mode mode = TYPE_MODE (enttype);
11932
11933           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11934             return false;
11935           if (int_size_in_bytes (type) != size)
11936             return false;
11937           if (size > TREE_STRING_LENGTH (init))
11938             {
11939               memcpy (array, TREE_STRING_POINTER (init),
11940                       TREE_STRING_LENGTH (init));
11941               memset (array + TREE_STRING_LENGTH (init),
11942                       '\0', size - TREE_STRING_LENGTH (init));
11943             }
11944           else
11945             memcpy (array, TREE_STRING_POINTER (init), size);
11946           return true;
11947         }
11948       return false;
11949     case CONSTRUCTOR:
11950       type = TREE_TYPE (init);
11951       if (int_size_in_bytes (type) != size)
11952         return false;
11953       if (TREE_CODE (type) == ARRAY_TYPE)
11954         {
11955           HOST_WIDE_INT min_index;
11956           unsigned HOST_WIDE_INT cnt;
11957           int curpos = 0, fieldsize;
11958           constructor_elt *ce;
11959
11960           if (TYPE_DOMAIN (type) == NULL_TREE
11961               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11962             return false;
11963
11964           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11965           if (fieldsize <= 0)
11966             return false;
11967
11968           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11969           memset (array, '\0', size);
11970           for (cnt = 0;
11971                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11972                cnt++)
11973             {
11974               tree val = ce->value;
11975               tree index = ce->index;
11976               int pos = curpos;
11977               if (index && TREE_CODE (index) == RANGE_EXPR)
11978                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11979                       * fieldsize;
11980               else if (index)
11981                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11982
11983               if (val)
11984                 {
11985                   STRIP_NOPS (val);
11986                   if (!native_encode_initializer (val, array + pos, fieldsize))
11987                     return false;
11988                 }
11989               curpos = pos + fieldsize;
11990               if (index && TREE_CODE (index) == RANGE_EXPR)
11991                 {
11992                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11993                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11994                   while (count > 0)
11995                     {
11996                       if (val)
11997                         memcpy (array + curpos, array + pos, fieldsize);
11998                       curpos += fieldsize;
11999                     }
12000                 }
12001               gcc_assert (curpos <= size);
12002             }
12003           return true;
12004         }
12005       else if (TREE_CODE (type) == RECORD_TYPE
12006                || TREE_CODE (type) == UNION_TYPE)
12007         {
12008           tree field = NULL_TREE;
12009           unsigned HOST_WIDE_INT cnt;
12010           constructor_elt *ce;
12011
12012           if (int_size_in_bytes (type) != size)
12013             return false;
12014
12015           if (TREE_CODE (type) == RECORD_TYPE)
12016             field = TYPE_FIELDS (type);
12017
12018           for (cnt = 0;
12019                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12020                cnt++, field = field ? TREE_CHAIN (field) : 0)
12021             {
12022               tree val = ce->value;
12023               int pos, fieldsize;
12024
12025               if (ce->index != 0)
12026                 field = ce->index;
12027
12028               if (val)
12029                 STRIP_NOPS (val);
12030
12031               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12032                 return false;
12033
12034               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12035                   && TYPE_DOMAIN (TREE_TYPE (field))
12036                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12037                 return false;
12038               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12039                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12040                 return false;
12041               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12042               pos = int_byte_position (field);
12043               gcc_assert (pos + fieldsize <= size);
12044               if (val
12045                   && !native_encode_initializer (val, array + pos, fieldsize))
12046                 return false;
12047             }
12048           return true;
12049         }
12050       return false;
12051     case VIEW_CONVERT_EXPR:
12052     case NON_LVALUE_EXPR:
12053       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12054     default:
12055       return native_encode_expr (init, array, size) == size;
12056     }
12057 }
12058
12059 /* If we don't have a copy of this variable in memory for some reason (such
12060    as a C++ member constant that doesn't have an out-of-line definition),
12061    we should tell the debugger about the constant value.  */
12062
12063 static void
12064 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12065 {
12066   tree init;
12067   tree type = TREE_TYPE (decl);
12068   rtx rtl;
12069
12070   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12071     return;
12072
12073   init = DECL_INITIAL (decl);
12074   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12075     /* OK */;
12076   else
12077     return;
12078
12079   rtl = rtl_for_decl_init (init, type);
12080   if (rtl)
12081     add_const_value_attribute (var_die, rtl);
12082   /* If the host and target are sane, try harder.  */
12083   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12084            && initializer_constant_valid_p (init, type))
12085     {
12086       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12087       if (size > 0 && (int) size == size)
12088         {
12089           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12090
12091           if (native_encode_initializer (init, array, size))
12092             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12093         }
12094     }
12095 }
12096
12097 /* Convert the CFI instructions for the current function into a
12098    location list.  This is used for DW_AT_frame_base when we targeting
12099    a dwarf2 consumer that does not support the dwarf3
12100    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12101    expressions.  */
12102
12103 static dw_loc_list_ref
12104 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12105 {
12106   dw_fde_ref fde;
12107   dw_loc_list_ref list, *list_tail;
12108   dw_cfi_ref cfi;
12109   dw_cfa_location last_cfa, next_cfa;
12110   const char *start_label, *last_label, *section;
12111
12112   fde = current_fde ();
12113   gcc_assert (fde != NULL);
12114
12115   section = secname_for_decl (current_function_decl);
12116   list_tail = &list;
12117   list = NULL;
12118
12119   next_cfa.reg = INVALID_REGNUM;
12120   next_cfa.offset = 0;
12121   next_cfa.indirect = 0;
12122   next_cfa.base_offset = 0;
12123
12124   start_label = fde->dw_fde_begin;
12125
12126   /* ??? Bald assumption that the CIE opcode list does not contain
12127      advance opcodes.  */
12128   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12129     lookup_cfa_1 (cfi, &next_cfa);
12130
12131   last_cfa = next_cfa;
12132   last_label = start_label;
12133
12134   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12135     switch (cfi->dw_cfi_opc)
12136       {
12137       case DW_CFA_set_loc:
12138       case DW_CFA_advance_loc1:
12139       case DW_CFA_advance_loc2:
12140       case DW_CFA_advance_loc4:
12141         if (!cfa_equal_p (&last_cfa, &next_cfa))
12142           {
12143             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12144                                        start_label, last_label, section,
12145                                        list == NULL);
12146
12147             list_tail = &(*list_tail)->dw_loc_next;
12148             last_cfa = next_cfa;
12149             start_label = last_label;
12150           }
12151         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12152         break;
12153
12154       case DW_CFA_advance_loc:
12155         /* The encoding is complex enough that we should never emit this.  */
12156       case DW_CFA_remember_state:
12157       case DW_CFA_restore_state:
12158         /* We don't handle these two in this function.  It would be possible
12159            if it were to be required.  */
12160         gcc_unreachable ();
12161
12162       default:
12163         lookup_cfa_1 (cfi, &next_cfa);
12164         break;
12165       }
12166
12167   if (!cfa_equal_p (&last_cfa, &next_cfa))
12168     {
12169       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12170                                  start_label, last_label, section,
12171                                  list == NULL);
12172       list_tail = &(*list_tail)->dw_loc_next;
12173       start_label = last_label;
12174     }
12175   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12176                              start_label, fde->dw_fde_end, section,
12177                              list == NULL);
12178
12179   return list;
12180 }
12181
12182 /* Compute a displacement from the "steady-state frame pointer" to the
12183    frame base (often the same as the CFA), and store it in
12184    frame_pointer_fb_offset.  OFFSET is added to the displacement
12185    before the latter is negated.  */
12186
12187 static void
12188 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12189 {
12190   rtx reg, elim;
12191
12192 #ifdef FRAME_POINTER_CFA_OFFSET
12193   reg = frame_pointer_rtx;
12194   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12195 #else
12196   reg = arg_pointer_rtx;
12197   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12198 #endif
12199
12200   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12201   if (GET_CODE (elim) == PLUS)
12202     {
12203       offset += INTVAL (XEXP (elim, 1));
12204       elim = XEXP (elim, 0);
12205     }
12206
12207   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12208                && (elim == hard_frame_pointer_rtx
12209                    || elim == stack_pointer_rtx))
12210               || elim == (frame_pointer_needed
12211                           ? hard_frame_pointer_rtx
12212                           : stack_pointer_rtx));
12213
12214   frame_pointer_fb_offset = -offset;
12215 }
12216
12217 /* Generate a DW_AT_name attribute given some string value to be included as
12218    the value of the attribute.  */
12219
12220 static void
12221 add_name_attribute (dw_die_ref die, const char *name_string)
12222 {
12223   if (name_string != NULL && *name_string != 0)
12224     {
12225       if (demangle_name_func)
12226         name_string = (*demangle_name_func) (name_string);
12227
12228       add_AT_string (die, DW_AT_name, name_string);
12229     }
12230 }
12231
12232 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12233
12234 static void
12235 add_comp_dir_attribute (dw_die_ref die)
12236 {
12237   const char *wd = get_src_pwd ();
12238   if (wd != NULL)
12239     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12240 }
12241
12242 /* Given a tree node describing an array bound (either lower or upper) output
12243    a representation for that bound.  */
12244
12245 static void
12246 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12247 {
12248   switch (TREE_CODE (bound))
12249     {
12250     case ERROR_MARK:
12251       return;
12252
12253     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12254     case INTEGER_CST:
12255       if (! host_integerp (bound, 0)
12256           || (bound_attr == DW_AT_lower_bound
12257               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12258                   || (is_fortran () && integer_onep (bound)))))
12259         /* Use the default.  */
12260         ;
12261       else
12262         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12263       break;
12264
12265     CASE_CONVERT:
12266     case VIEW_CONVERT_EXPR:
12267       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12268       break;
12269
12270     case SAVE_EXPR:
12271       break;
12272
12273     case VAR_DECL:
12274     case PARM_DECL:
12275     case RESULT_DECL:
12276       {
12277         dw_die_ref decl_die = lookup_decl_die (bound);
12278         dw_loc_descr_ref loc;
12279
12280         /* ??? Can this happen, or should the variable have been bound
12281            first?  Probably it can, since I imagine that we try to create
12282            the types of parameters in the order in which they exist in
12283            the list, and won't have created a forward reference to a
12284            later parameter.  */
12285         if (decl_die != NULL)
12286           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12287         else
12288           {
12289             loc = loc_descriptor_from_tree_1 (bound, 0);
12290             add_AT_location_description (subrange_die, bound_attr, loc);
12291           }
12292         break;
12293       }
12294
12295     default:
12296       {
12297         /* Otherwise try to create a stack operation procedure to
12298            evaluate the value of the array bound.  */
12299
12300         dw_die_ref ctx, decl_die;
12301         dw_loc_descr_ref loc;
12302
12303         loc = loc_descriptor_from_tree (bound);
12304         if (loc == NULL)
12305           break;
12306
12307         if (current_function_decl == 0)
12308           ctx = comp_unit_die;
12309         else
12310           ctx = lookup_decl_die (current_function_decl);
12311
12312         decl_die = new_die (DW_TAG_variable, ctx, bound);
12313         add_AT_flag (decl_die, DW_AT_artificial, 1);
12314         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12315         add_AT_loc (decl_die, DW_AT_location, loc);
12316
12317         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12318         break;
12319       }
12320     }
12321 }
12322
12323 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12324    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12325    Note that the block of subscript information for an array type also
12326    includes information about the element type of the given array type.  */
12327
12328 static void
12329 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12330 {
12331   unsigned dimension_number;
12332   tree lower, upper;
12333   dw_die_ref subrange_die;
12334
12335   for (dimension_number = 0;
12336        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12337        type = TREE_TYPE (type), dimension_number++)
12338     {
12339       tree domain = TYPE_DOMAIN (type);
12340
12341       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12342         break;
12343
12344       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12345          and (in GNU C only) variable bounds.  Handle all three forms
12346          here.  */
12347       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12348       if (domain)
12349         {
12350           /* We have an array type with specified bounds.  */
12351           lower = TYPE_MIN_VALUE (domain);
12352           upper = TYPE_MAX_VALUE (domain);
12353
12354           /* Define the index type.  */
12355           if (TREE_TYPE (domain))
12356             {
12357               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12358                  TREE_TYPE field.  We can't emit debug info for this
12359                  because it is an unnamed integral type.  */
12360               if (TREE_CODE (domain) == INTEGER_TYPE
12361                   && TYPE_NAME (domain) == NULL_TREE
12362                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12363                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12364                 ;
12365               else
12366                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12367                                     type_die);
12368             }
12369
12370           /* ??? If upper is NULL, the array has unspecified length,
12371              but it does have a lower bound.  This happens with Fortran
12372                dimension arr(N:*)
12373              Since the debugger is definitely going to need to know N
12374              to produce useful results, go ahead and output the lower
12375              bound solo, and hope the debugger can cope.  */
12376
12377           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12378           if (upper)
12379             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12380         }
12381
12382       /* Otherwise we have an array type with an unspecified length.  The
12383          DWARF-2 spec does not say how to handle this; let's just leave out the
12384          bounds.  */
12385     }
12386 }
12387
12388 static void
12389 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12390 {
12391   unsigned size;
12392
12393   switch (TREE_CODE (tree_node))
12394     {
12395     case ERROR_MARK:
12396       size = 0;
12397       break;
12398     case ENUMERAL_TYPE:
12399     case RECORD_TYPE:
12400     case UNION_TYPE:
12401     case QUAL_UNION_TYPE:
12402       size = int_size_in_bytes (tree_node);
12403       break;
12404     case FIELD_DECL:
12405       /* For a data member of a struct or union, the DW_AT_byte_size is
12406          generally given as the number of bytes normally allocated for an
12407          object of the *declared* type of the member itself.  This is true
12408          even for bit-fields.  */
12409       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12410       break;
12411     default:
12412       gcc_unreachable ();
12413     }
12414
12415   /* Note that `size' might be -1 when we get to this point.  If it is, that
12416      indicates that the byte size of the entity in question is variable.  We
12417      have no good way of expressing this fact in Dwarf at the present time,
12418      so just let the -1 pass on through.  */
12419   add_AT_unsigned (die, DW_AT_byte_size, size);
12420 }
12421
12422 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12423    which specifies the distance in bits from the highest order bit of the
12424    "containing object" for the bit-field to the highest order bit of the
12425    bit-field itself.
12426
12427    For any given bit-field, the "containing object" is a hypothetical object
12428    (of some integral or enum type) within which the given bit-field lives.  The
12429    type of this hypothetical "containing object" is always the same as the
12430    declared type of the individual bit-field itself.  The determination of the
12431    exact location of the "containing object" for a bit-field is rather
12432    complicated.  It's handled by the `field_byte_offset' function (above).
12433
12434    Note that it is the size (in bytes) of the hypothetical "containing object"
12435    which will be given in the DW_AT_byte_size attribute for this bit-field.
12436    (See `byte_size_attribute' above).  */
12437
12438 static inline void
12439 add_bit_offset_attribute (dw_die_ref die, tree decl)
12440 {
12441   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12442   tree type = DECL_BIT_FIELD_TYPE (decl);
12443   HOST_WIDE_INT bitpos_int;
12444   HOST_WIDE_INT highest_order_object_bit_offset;
12445   HOST_WIDE_INT highest_order_field_bit_offset;
12446   HOST_WIDE_INT unsigned bit_offset;
12447
12448   /* Must be a field and a bit field.  */
12449   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12450
12451   /* We can't yet handle bit-fields whose offsets are variable, so if we
12452      encounter such things, just return without generating any attribute
12453      whatsoever.  Likewise for variable or too large size.  */
12454   if (! host_integerp (bit_position (decl), 0)
12455       || ! host_integerp (DECL_SIZE (decl), 1))
12456     return;
12457
12458   bitpos_int = int_bit_position (decl);
12459
12460   /* Note that the bit offset is always the distance (in bits) from the
12461      highest-order bit of the "containing object" to the highest-order bit of
12462      the bit-field itself.  Since the "high-order end" of any object or field
12463      is different on big-endian and little-endian machines, the computation
12464      below must take account of these differences.  */
12465   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12466   highest_order_field_bit_offset = bitpos_int;
12467
12468   if (! BYTES_BIG_ENDIAN)
12469     {
12470       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12471       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12472     }
12473
12474   bit_offset
12475     = (! BYTES_BIG_ENDIAN
12476        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12477        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12478
12479   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12480 }
12481
12482 /* For a FIELD_DECL node which represents a bit field, output an attribute
12483    which specifies the length in bits of the given field.  */
12484
12485 static inline void
12486 add_bit_size_attribute (dw_die_ref die, tree decl)
12487 {
12488   /* Must be a field and a bit field.  */
12489   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12490               && DECL_BIT_FIELD_TYPE (decl));
12491
12492   if (host_integerp (DECL_SIZE (decl), 1))
12493     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12494 }
12495
12496 /* If the compiled language is ANSI C, then add a 'prototyped'
12497    attribute, if arg types are given for the parameters of a function.  */
12498
12499 static inline void
12500 add_prototyped_attribute (dw_die_ref die, tree func_type)
12501 {
12502   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12503       && TYPE_ARG_TYPES (func_type) != NULL)
12504     add_AT_flag (die, DW_AT_prototyped, 1);
12505 }
12506
12507 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12508    by looking in either the type declaration or object declaration
12509    equate table.  */
12510
12511 static inline dw_die_ref
12512 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12513 {
12514   dw_die_ref origin_die = NULL;
12515
12516   if (TREE_CODE (origin) != FUNCTION_DECL)
12517     {
12518       /* We may have gotten separated from the block for the inlined
12519          function, if we're in an exception handler or some such; make
12520          sure that the abstract function has been written out.
12521
12522          Doing this for nested functions is wrong, however; functions are
12523          distinct units, and our context might not even be inline.  */
12524       tree fn = origin;
12525
12526       if (TYPE_P (fn))
12527         fn = TYPE_STUB_DECL (fn);
12528
12529       fn = decl_function_context (fn);
12530       if (fn)
12531         dwarf2out_abstract_function (fn);
12532     }
12533
12534   if (DECL_P (origin))
12535     origin_die = lookup_decl_die (origin);
12536   else if (TYPE_P (origin))
12537     origin_die = lookup_type_die (origin);
12538
12539   /* XXX: Functions that are never lowered don't always have correct block
12540      trees (in the case of java, they simply have no block tree, in some other
12541      languages).  For these functions, there is nothing we can really do to
12542      output correct debug info for inlined functions in all cases.  Rather
12543      than die, we'll just produce deficient debug info now, in that we will
12544      have variables without a proper abstract origin.  In the future, when all
12545      functions are lowered, we should re-add a gcc_assert (origin_die)
12546      here.  */
12547
12548   if (origin_die)
12549     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12550   return origin_die;
12551 }
12552
12553 /* We do not currently support the pure_virtual attribute.  */
12554
12555 static inline void
12556 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12557 {
12558   if (DECL_VINDEX (func_decl))
12559     {
12560       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12561
12562       if (host_integerp (DECL_VINDEX (func_decl), 0))
12563         add_AT_loc (die, DW_AT_vtable_elem_location,
12564                     new_loc_descr (DW_OP_constu,
12565                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12566                                    0));
12567
12568       /* GNU extension: Record what type this method came from originally.  */
12569       if (debug_info_level > DINFO_LEVEL_TERSE)
12570         add_AT_die_ref (die, DW_AT_containing_type,
12571                         lookup_type_die (DECL_CONTEXT (func_decl)));
12572     }
12573 }
12574 \f
12575 /* Add source coordinate attributes for the given decl.  */
12576
12577 static void
12578 add_src_coords_attributes (dw_die_ref die, tree decl)
12579 {
12580   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12581
12582   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12583   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12584 }
12585
12586 /* Add a DW_AT_name attribute and source coordinate attribute for the
12587    given decl, but only if it actually has a name.  */
12588
12589 static void
12590 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12591 {
12592   tree decl_name;
12593
12594   decl_name = DECL_NAME (decl);
12595   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12596     {
12597       add_name_attribute (die, dwarf2_name (decl, 0));
12598       if (! DECL_ARTIFICIAL (decl))
12599         add_src_coords_attributes (die, decl);
12600
12601       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12602           && TREE_PUBLIC (decl)
12603           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12604           && !DECL_ABSTRACT (decl)
12605           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12606           && !is_fortran ())
12607         add_AT_string (die, DW_AT_MIPS_linkage_name,
12608                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12609     }
12610
12611 #ifdef VMS_DEBUGGING_INFO
12612   /* Get the function's name, as described by its RTL.  This may be different
12613      from the DECL_NAME name used in the source file.  */
12614   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12615     {
12616       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12617                    XEXP (DECL_RTL (decl), 0));
12618       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12619     }
12620 #endif
12621 }
12622
12623 /* Push a new declaration scope.  */
12624
12625 static void
12626 push_decl_scope (tree scope)
12627 {
12628   VEC_safe_push (tree, gc, decl_scope_table, scope);
12629 }
12630
12631 /* Pop a declaration scope.  */
12632
12633 static inline void
12634 pop_decl_scope (void)
12635 {
12636   VEC_pop (tree, decl_scope_table);
12637 }
12638
12639 /* Return the DIE for the scope that immediately contains this type.
12640    Non-named types get global scope.  Named types nested in other
12641    types get their containing scope if it's open, or global scope
12642    otherwise.  All other types (i.e. function-local named types) get
12643    the current active scope.  */
12644
12645 static dw_die_ref
12646 scope_die_for (tree t, dw_die_ref context_die)
12647 {
12648   dw_die_ref scope_die = NULL;
12649   tree containing_scope;
12650   int i;
12651
12652   /* Non-types always go in the current scope.  */
12653   gcc_assert (TYPE_P (t));
12654
12655   containing_scope = TYPE_CONTEXT (t);
12656
12657   /* Use the containing namespace if it was passed in (for a declaration).  */
12658   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12659     {
12660       if (context_die == lookup_decl_die (containing_scope))
12661         /* OK */;
12662       else
12663         containing_scope = NULL_TREE;
12664     }
12665
12666   /* Ignore function type "scopes" from the C frontend.  They mean that
12667      a tagged type is local to a parmlist of a function declarator, but
12668      that isn't useful to DWARF.  */
12669   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12670     containing_scope = NULL_TREE;
12671
12672   if (containing_scope == NULL_TREE)
12673     scope_die = comp_unit_die;
12674   else if (TYPE_P (containing_scope))
12675     {
12676       /* For types, we can just look up the appropriate DIE.  But
12677          first we check to see if we're in the middle of emitting it
12678          so we know where the new DIE should go.  */
12679       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12680         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12681           break;
12682
12683       if (i < 0)
12684         {
12685           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12686                       || TREE_ASM_WRITTEN (containing_scope));
12687
12688           /* If none of the current dies are suitable, we get file scope.  */
12689           scope_die = comp_unit_die;
12690         }
12691       else
12692         scope_die = lookup_type_die (containing_scope);
12693     }
12694   else
12695     scope_die = context_die;
12696
12697   return scope_die;
12698 }
12699
12700 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12701
12702 static inline int
12703 local_scope_p (dw_die_ref context_die)
12704 {
12705   for (; context_die; context_die = context_die->die_parent)
12706     if (context_die->die_tag == DW_TAG_inlined_subroutine
12707         || context_die->die_tag == DW_TAG_subprogram)
12708       return 1;
12709
12710   return 0;
12711 }
12712
12713 /* Returns nonzero if CONTEXT_DIE is a class.  */
12714
12715 static inline int
12716 class_scope_p (dw_die_ref context_die)
12717 {
12718   return (context_die
12719           && (context_die->die_tag == DW_TAG_structure_type
12720               || context_die->die_tag == DW_TAG_class_type
12721               || context_die->die_tag == DW_TAG_interface_type
12722               || context_die->die_tag == DW_TAG_union_type));
12723 }
12724
12725 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12726    whether or not to treat a DIE in this context as a declaration.  */
12727
12728 static inline int
12729 class_or_namespace_scope_p (dw_die_ref context_die)
12730 {
12731   return (class_scope_p (context_die)
12732           || (context_die && context_die->die_tag == DW_TAG_namespace));
12733 }
12734
12735 /* Many forms of DIEs require a "type description" attribute.  This
12736    routine locates the proper "type descriptor" die for the type given
12737    by 'type', and adds a DW_AT_type attribute below the given die.  */
12738
12739 static void
12740 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12741                     int decl_volatile, dw_die_ref context_die)
12742 {
12743   enum tree_code code  = TREE_CODE (type);
12744   dw_die_ref type_die  = NULL;
12745
12746   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12747      or fixed-point type, use the inner type.  This is because we have no
12748      support for unnamed types in base_type_die.  This can happen if this is
12749      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12750   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12751       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12752     type = TREE_TYPE (type), code = TREE_CODE (type);
12753
12754   if (code == ERROR_MARK
12755       /* Handle a special case.  For functions whose return type is void, we
12756          generate *no* type attribute.  (Note that no object may have type
12757          `void', so this only applies to function return types).  */
12758       || code == VOID_TYPE)
12759     return;
12760
12761   type_die = modified_type_die (type,
12762                                 decl_const || TYPE_READONLY (type),
12763                                 decl_volatile || TYPE_VOLATILE (type),
12764                                 context_die);
12765
12766   if (type_die != NULL)
12767     add_AT_die_ref (object_die, DW_AT_type, type_die);
12768 }
12769
12770 /* Given an object die, add the calling convention attribute for the
12771    function call type.  */
12772 static void
12773 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12774 {
12775   enum dwarf_calling_convention value = DW_CC_normal;
12776
12777   value = ((enum dwarf_calling_convention)
12778            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
12779
12780   /* DWARF doesn't provide a way to identify a program's source-level
12781      entry point.  DW_AT_calling_convention attributes are only meant
12782      to describe functions' calling conventions.  However, lacking a
12783      better way to signal the Fortran main program, we use this for the
12784      time being, following existing custom.  */
12785   if (is_fortran ()
12786       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12787     value = DW_CC_program;
12788
12789   /* Only add the attribute if the backend requests it, and
12790      is not DW_CC_normal.  */
12791   if (value && (value != DW_CC_normal))
12792     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12793 }
12794
12795 /* Given a tree pointer to a struct, class, union, or enum type node, return
12796    a pointer to the (string) tag name for the given type, or zero if the type
12797    was declared without a tag.  */
12798
12799 static const char *
12800 type_tag (const_tree type)
12801 {
12802   const char *name = 0;
12803
12804   if (TYPE_NAME (type) != 0)
12805     {
12806       tree t = 0;
12807
12808       /* Find the IDENTIFIER_NODE for the type name.  */
12809       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12810         t = TYPE_NAME (type);
12811
12812       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12813          a TYPE_DECL node, regardless of whether or not a `typedef' was
12814          involved.  */
12815       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12816                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12817         {
12818           /* We want to be extra verbose.  Don't call dwarf_name if
12819              DECL_NAME isn't set.  The default hook for decl_printable_name
12820              doesn't like that, and in this context it's correct to return
12821              0, instead of "<anonymous>" or the like.  */
12822           if (DECL_NAME (TYPE_NAME (type)))
12823             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12824         }
12825
12826       /* Now get the name as a string, or invent one.  */
12827       if (!name && t != 0)
12828         name = IDENTIFIER_POINTER (t);
12829     }
12830
12831   return (name == 0 || *name == '\0') ? 0 : name;
12832 }
12833
12834 /* Return the type associated with a data member, make a special check
12835    for bit field types.  */
12836
12837 static inline tree
12838 member_declared_type (const_tree member)
12839 {
12840   return (DECL_BIT_FIELD_TYPE (member)
12841           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12842 }
12843
12844 /* Get the decl's label, as described by its RTL. This may be different
12845    from the DECL_NAME name used in the source file.  */
12846
12847 #if 0
12848 static const char *
12849 decl_start_label (tree decl)
12850 {
12851   rtx x;
12852   const char *fnname;
12853
12854   x = DECL_RTL (decl);
12855   gcc_assert (MEM_P (x));
12856
12857   x = XEXP (x, 0);
12858   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12859
12860   fnname = XSTR (x, 0);
12861   return fnname;
12862 }
12863 #endif
12864 \f
12865 /* These routines generate the internal representation of the DIE's for
12866    the compilation unit.  Debugging information is collected by walking
12867    the declaration trees passed in from dwarf2out_decl().  */
12868
12869 static void
12870 gen_array_type_die (tree type, dw_die_ref context_die)
12871 {
12872   dw_die_ref scope_die = scope_die_for (type, context_die);
12873   dw_die_ref array_die;
12874
12875   /* GNU compilers represent multidimensional array types as sequences of one
12876      dimensional array types whose element types are themselves array types.
12877      We sometimes squish that down to a single array_type DIE with multiple
12878      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12879      say that we are allowed to do this kind of compression in C, because
12880      there is no difference between an array of arrays and a multidimensional
12881      array.  We don't do this for Ada to remain as close as possible to the
12882      actual representation, which is especially important against the language
12883      flexibilty wrt arrays of variable size.  */
12884
12885   bool collapse_nested_arrays = !is_ada ();
12886   tree element_type;
12887
12888   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12889      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12890   if (TYPE_STRING_FLAG (type)
12891       && TREE_CODE (type) == ARRAY_TYPE
12892       && is_fortran ()
12893       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12894     {
12895       HOST_WIDE_INT size;
12896
12897       array_die = new_die (DW_TAG_string_type, scope_die, type);
12898       add_name_attribute (array_die, type_tag (type));
12899       equate_type_number_to_die (type, array_die);
12900       size = int_size_in_bytes (type);
12901       if (size >= 0)
12902         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12903       else if (TYPE_DOMAIN (type) != NULL_TREE
12904                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12905                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12906         {
12907           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12908           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12909
12910           size = int_size_in_bytes (TREE_TYPE (szdecl));
12911           if (loc && size > 0)
12912             {
12913               add_AT_loc (array_die, DW_AT_string_length, loc);
12914               if (size != DWARF2_ADDR_SIZE)
12915                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12916             }
12917         }
12918       return;
12919     }
12920
12921   /* ??? The SGI dwarf reader fails for array of array of enum types
12922      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12923      array type comes before the outer array type.  We thus call gen_type_die
12924      before we new_die and must prevent nested array types collapsing for this
12925      target.  */
12926
12927 #ifdef MIPS_DEBUGGING_INFO
12928   gen_type_die (TREE_TYPE (type), context_die);
12929   collapse_nested_arrays = false;
12930 #endif
12931
12932   array_die = new_die (DW_TAG_array_type, scope_die, type);
12933   add_name_attribute (array_die, type_tag (type));
12934   equate_type_number_to_die (type, array_die);
12935
12936   if (TREE_CODE (type) == VECTOR_TYPE)
12937     {
12938       /* The frontend feeds us a representation for the vector as a struct
12939          containing an array.  Pull out the array type.  */
12940       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12941       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12942     }
12943
12944   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12945   if (is_fortran ()
12946       && TREE_CODE (type) == ARRAY_TYPE
12947       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12948       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12949     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12950
12951 #if 0
12952   /* We default the array ordering.  SDB will probably do
12953      the right things even if DW_AT_ordering is not present.  It's not even
12954      an issue until we start to get into multidimensional arrays anyway.  If
12955      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12956      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12957      and when we find out that we need to put these in, we will only do so
12958      for multidimensional arrays.  */
12959   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12960 #endif
12961
12962 #ifdef MIPS_DEBUGGING_INFO
12963   /* The SGI compilers handle arrays of unknown bound by setting
12964      AT_declaration and not emitting any subrange DIEs.  */
12965   if (! TYPE_DOMAIN (type))
12966     add_AT_flag (array_die, DW_AT_declaration, 1);
12967   else
12968 #endif
12969     add_subscript_info (array_die, type, collapse_nested_arrays);
12970
12971   /* Add representation of the type of the elements of this array type and
12972      emit the corresponding DIE if we haven't done it already.  */  
12973   element_type = TREE_TYPE (type);
12974   if (collapse_nested_arrays)
12975     while (TREE_CODE (element_type) == ARRAY_TYPE)
12976       {
12977         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12978           break;
12979         element_type = TREE_TYPE (element_type);
12980       }
12981
12982 #ifndef MIPS_DEBUGGING_INFO
12983   gen_type_die (element_type, context_die);
12984 #endif
12985
12986   add_type_attribute (array_die, element_type, 0, 0, context_die);
12987
12988   if (get_AT (array_die, DW_AT_name))
12989     add_pubtype (type, array_die);
12990 }
12991
12992 static dw_loc_descr_ref
12993 descr_info_loc (tree val, tree base_decl)
12994 {
12995   HOST_WIDE_INT size;
12996   dw_loc_descr_ref loc, loc2;
12997   enum dwarf_location_atom op;
12998
12999   if (val == base_decl)
13000     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13001
13002   switch (TREE_CODE (val))
13003     {
13004     CASE_CONVERT:
13005       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13006     case VAR_DECL:
13007       return loc_descriptor_from_tree_1 (val, 0);
13008     case INTEGER_CST:
13009       if (host_integerp (val, 0))
13010         return int_loc_descriptor (tree_low_cst (val, 0));
13011       break;
13012     case INDIRECT_REF:
13013       size = int_size_in_bytes (TREE_TYPE (val));
13014       if (size < 0)
13015         break;
13016       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13017       if (!loc)
13018         break;
13019       if (size == DWARF2_ADDR_SIZE)
13020         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13021       else
13022         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13023       return loc;
13024     case POINTER_PLUS_EXPR:
13025     case PLUS_EXPR:
13026       if (host_integerp (TREE_OPERAND (val, 1), 1)
13027           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13028              < 16384)
13029         {
13030           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13031           if (!loc)
13032             break;
13033           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13034         }
13035       else
13036         {
13037           op = DW_OP_plus;
13038         do_binop:
13039           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13040           if (!loc)
13041             break;
13042           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13043           if (!loc2)
13044             break;
13045           add_loc_descr (&loc, loc2);
13046           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13047         }
13048       return loc;
13049     case MINUS_EXPR:
13050       op = DW_OP_minus;
13051       goto do_binop;
13052     case MULT_EXPR:
13053       op = DW_OP_mul;
13054       goto do_binop;
13055     case EQ_EXPR:
13056       op = DW_OP_eq;
13057       goto do_binop;
13058     case NE_EXPR:
13059       op = DW_OP_ne;
13060       goto do_binop;
13061     default:
13062       break;
13063     }
13064   return NULL;
13065 }
13066
13067 static void
13068 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13069                       tree val, tree base_decl)
13070 {
13071   dw_loc_descr_ref loc;
13072
13073   if (host_integerp (val, 0))
13074     {
13075       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13076       return;
13077     }
13078
13079   loc = descr_info_loc (val, base_decl);
13080   if (!loc)
13081     return;
13082
13083   add_AT_loc (die, attr, loc);
13084 }
13085
13086 /* This routine generates DIE for array with hidden descriptor, details
13087    are filled into *info by a langhook.  */
13088
13089 static void
13090 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13091                           dw_die_ref context_die)
13092 {
13093   dw_die_ref scope_die = scope_die_for (type, context_die);
13094   dw_die_ref array_die;
13095   int dim;
13096
13097   array_die = new_die (DW_TAG_array_type, scope_die, type);
13098   add_name_attribute (array_die, type_tag (type));
13099   equate_type_number_to_die (type, array_die);
13100
13101   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13102   if (is_fortran ()
13103       && info->ndimensions >= 2)
13104     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13105
13106   if (info->data_location)
13107     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13108                           info->base_decl);
13109   if (info->associated)
13110     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13111                           info->base_decl);
13112   if (info->allocated)
13113     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13114                           info->base_decl);
13115
13116   for (dim = 0; dim < info->ndimensions; dim++)
13117     {
13118       dw_die_ref subrange_die
13119         = new_die (DW_TAG_subrange_type, array_die, NULL);
13120
13121       if (info->dimen[dim].lower_bound)
13122         {
13123           /* If it is the default value, omit it.  */
13124           if ((is_c_family () || is_java ())
13125               && integer_zerop (info->dimen[dim].lower_bound))
13126             ;
13127           else if (is_fortran ()
13128                    && integer_onep (info->dimen[dim].lower_bound))
13129             ;
13130           else
13131             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13132                                   info->dimen[dim].lower_bound,
13133                                   info->base_decl);
13134         }
13135       if (info->dimen[dim].upper_bound)
13136         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13137                               info->dimen[dim].upper_bound,
13138                               info->base_decl);
13139       if (info->dimen[dim].stride)
13140         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13141                               info->dimen[dim].stride,
13142                               info->base_decl);
13143     }
13144
13145   gen_type_die (info->element_type, context_die);
13146   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13147
13148   if (get_AT (array_die, DW_AT_name))
13149     add_pubtype (type, array_die);
13150 }
13151
13152 #if 0
13153 static void
13154 gen_entry_point_die (tree decl, dw_die_ref context_die)
13155 {
13156   tree origin = decl_ultimate_origin (decl);
13157   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13158
13159   if (origin != NULL)
13160     add_abstract_origin_attribute (decl_die, origin);
13161   else
13162     {
13163       add_name_and_src_coords_attributes (decl_die, decl);
13164       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13165                           0, 0, context_die);
13166     }
13167
13168   if (DECL_ABSTRACT (decl))
13169     equate_decl_number_to_die (decl, decl_die);
13170   else
13171     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13172 }
13173 #endif
13174
13175 /* Walk through the list of incomplete types again, trying once more to
13176    emit full debugging info for them.  */
13177
13178 static void
13179 retry_incomplete_types (void)
13180 {
13181   int i;
13182
13183   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13184     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13185 }
13186
13187 /* Determine what tag to use for a record type.  */
13188
13189 static enum dwarf_tag
13190 record_type_tag (tree type)
13191 {
13192   if (! lang_hooks.types.classify_record)
13193     return DW_TAG_structure_type;
13194
13195   switch (lang_hooks.types.classify_record (type))
13196     {
13197     case RECORD_IS_STRUCT:
13198       return DW_TAG_structure_type;
13199
13200     case RECORD_IS_CLASS:
13201       return DW_TAG_class_type;
13202
13203     case RECORD_IS_INTERFACE:
13204       return DW_TAG_interface_type;
13205
13206     default:
13207       gcc_unreachable ();
13208     }
13209 }
13210
13211 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13212    include all of the information about the enumeration values also. Each
13213    enumerated type name/value is listed as a child of the enumerated type
13214    DIE.  */
13215
13216 static dw_die_ref
13217 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13218 {
13219   dw_die_ref type_die = lookup_type_die (type);
13220
13221   if (type_die == NULL)
13222     {
13223       type_die = new_die (DW_TAG_enumeration_type,
13224                           scope_die_for (type, context_die), type);
13225       equate_type_number_to_die (type, type_die);
13226       add_name_attribute (type_die, type_tag (type));
13227     }
13228   else if (! TYPE_SIZE (type))
13229     return type_die;
13230   else
13231     remove_AT (type_die, DW_AT_declaration);
13232
13233   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13234      given enum type is incomplete, do not generate the DW_AT_byte_size
13235      attribute or the DW_AT_element_list attribute.  */
13236   if (TYPE_SIZE (type))
13237     {
13238       tree link;
13239
13240       TREE_ASM_WRITTEN (type) = 1;
13241       add_byte_size_attribute (type_die, type);
13242       if (TYPE_STUB_DECL (type) != NULL_TREE)
13243         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13244
13245       /* If the first reference to this type was as the return type of an
13246          inline function, then it may not have a parent.  Fix this now.  */
13247       if (type_die->die_parent == NULL)
13248         add_child_die (scope_die_for (type, context_die), type_die);
13249
13250       for (link = TYPE_VALUES (type);
13251            link != NULL; link = TREE_CHAIN (link))
13252         {
13253           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13254           tree value = TREE_VALUE (link);
13255
13256           add_name_attribute (enum_die,
13257                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13258
13259           if (TREE_CODE (value) == CONST_DECL)
13260             value = DECL_INITIAL (value);
13261
13262           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13263             /* DWARF2 does not provide a way of indicating whether or
13264                not enumeration constants are signed or unsigned.  GDB
13265                always assumes the values are signed, so we output all
13266                values as if they were signed.  That means that
13267                enumeration constants with very large unsigned values
13268                will appear to have negative values in the debugger.  */
13269             add_AT_int (enum_die, DW_AT_const_value,
13270                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13271         }
13272     }
13273   else
13274     add_AT_flag (type_die, DW_AT_declaration, 1);
13275
13276   if (get_AT (type_die, DW_AT_name))
13277     add_pubtype (type, type_die);
13278
13279   return type_die;
13280 }
13281
13282 /* Generate a DIE to represent either a real live formal parameter decl or to
13283    represent just the type of some formal parameter position in some function
13284    type.
13285
13286    Note that this routine is a bit unusual because its argument may be a
13287    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13288    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13289    node.  If it's the former then this function is being called to output a
13290    DIE to represent a formal parameter object (or some inlining thereof).  If
13291    it's the latter, then this function is only being called to output a
13292    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13293    argument type of some subprogram type.  */
13294
13295 static dw_die_ref
13296 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13297 {
13298   tree node_or_origin = node ? node : origin;
13299   dw_die_ref parm_die
13300     = new_die (DW_TAG_formal_parameter, context_die, node);
13301
13302   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13303     {
13304     case tcc_declaration:
13305       if (!origin)
13306         origin = decl_ultimate_origin (node);
13307       if (origin != NULL)
13308         add_abstract_origin_attribute (parm_die, origin);
13309       else
13310         {
13311           tree type = TREE_TYPE (node);
13312           add_name_and_src_coords_attributes (parm_die, node);
13313           if (DECL_BY_REFERENCE (node))
13314             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13315                                 context_die);
13316           else
13317             add_type_attribute (parm_die, type,
13318                                 TREE_READONLY (node),
13319                                 TREE_THIS_VOLATILE (node),
13320                                 context_die);
13321           if (DECL_ARTIFICIAL (node))
13322             add_AT_flag (parm_die, DW_AT_artificial, 1);
13323         }
13324
13325       if (node)
13326         equate_decl_number_to_die (node, parm_die);
13327       if (! DECL_ABSTRACT (node_or_origin))
13328         add_location_or_const_value_attribute (parm_die, node_or_origin,
13329                                                DW_AT_location);
13330
13331       break;
13332
13333     case tcc_type:
13334       /* We were called with some kind of a ..._TYPE node.  */
13335       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13336       break;
13337
13338     default:
13339       gcc_unreachable ();
13340     }
13341
13342   return parm_die;
13343 }
13344
13345 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13346    at the end of an (ANSI prototyped) formal parameters list.  */
13347
13348 static void
13349 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13350 {
13351   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13352 }
13353
13354 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13355    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13356    parameters as specified in some function type specification (except for
13357    those which appear as part of a function *definition*).  */
13358
13359 static void
13360 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13361 {
13362   tree link;
13363   tree formal_type = NULL;
13364   tree first_parm_type;
13365   tree arg;
13366
13367   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13368     {
13369       arg = DECL_ARGUMENTS (function_or_method_type);
13370       function_or_method_type = TREE_TYPE (function_or_method_type);
13371     }
13372   else
13373     arg = NULL_TREE;
13374
13375   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13376
13377   /* Make our first pass over the list of formal parameter types and output a
13378      DW_TAG_formal_parameter DIE for each one.  */
13379   for (link = first_parm_type; link; )
13380     {
13381       dw_die_ref parm_die;
13382
13383       formal_type = TREE_VALUE (link);
13384       if (formal_type == void_type_node)
13385         break;
13386
13387       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13388       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13389       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13390            && link == first_parm_type)
13391           || (arg && DECL_ARTIFICIAL (arg)))
13392         add_AT_flag (parm_die, DW_AT_artificial, 1);
13393
13394       link = TREE_CHAIN (link);
13395       if (arg)
13396         arg = TREE_CHAIN (arg);
13397     }
13398
13399   /* If this function type has an ellipsis, add a
13400      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13401   if (formal_type != void_type_node)
13402     gen_unspecified_parameters_die (function_or_method_type, context_die);
13403
13404   /* Make our second (and final) pass over the list of formal parameter types
13405      and output DIEs to represent those types (as necessary).  */
13406   for (link = TYPE_ARG_TYPES (function_or_method_type);
13407        link && TREE_VALUE (link);
13408        link = TREE_CHAIN (link))
13409     gen_type_die (TREE_VALUE (link), context_die);
13410 }
13411
13412 /* We want to generate the DIE for TYPE so that we can generate the
13413    die for MEMBER, which has been defined; we will need to refer back
13414    to the member declaration nested within TYPE.  If we're trying to
13415    generate minimal debug info for TYPE, processing TYPE won't do the
13416    trick; we need to attach the member declaration by hand.  */
13417
13418 static void
13419 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13420 {
13421   gen_type_die (type, context_die);
13422
13423   /* If we're trying to avoid duplicate debug info, we may not have
13424      emitted the member decl for this function.  Emit it now.  */
13425   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13426       && ! lookup_decl_die (member))
13427     {
13428       dw_die_ref type_die;
13429       gcc_assert (!decl_ultimate_origin (member));
13430
13431       push_decl_scope (type);
13432       type_die = lookup_type_die (type);
13433       if (TREE_CODE (member) == FUNCTION_DECL)
13434         gen_subprogram_die (member, type_die);
13435       else if (TREE_CODE (member) == FIELD_DECL)
13436         {
13437           /* Ignore the nameless fields that are used to skip bits but handle
13438              C++ anonymous unions and structs.  */
13439           if (DECL_NAME (member) != NULL_TREE
13440               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13441               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13442             {
13443               gen_type_die (member_declared_type (member), type_die);
13444               gen_field_die (member, type_die);
13445             }
13446         }
13447       else
13448         gen_variable_die (member, NULL_TREE, type_die);
13449
13450       pop_decl_scope ();
13451     }
13452 }
13453
13454 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13455    may later generate inlined and/or out-of-line instances of.  */
13456
13457 static void
13458 dwarf2out_abstract_function (tree decl)
13459 {
13460   dw_die_ref old_die;
13461   tree save_fn;
13462   tree context;
13463   int was_abstract = DECL_ABSTRACT (decl);
13464
13465   /* Make sure we have the actual abstract inline, not a clone.  */
13466   decl = DECL_ORIGIN (decl);
13467
13468   old_die = lookup_decl_die (decl);
13469   if (old_die && get_AT (old_die, DW_AT_inline))
13470     /* We've already generated the abstract instance.  */
13471     return;
13472
13473   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13474      we don't get confused by DECL_ABSTRACT.  */
13475   if (debug_info_level > DINFO_LEVEL_TERSE)
13476     {
13477       context = decl_class_context (decl);
13478       if (context)
13479         gen_type_die_for_member
13480           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13481     }
13482
13483   /* Pretend we've just finished compiling this function.  */
13484   save_fn = current_function_decl;
13485   current_function_decl = decl;
13486   push_cfun (DECL_STRUCT_FUNCTION (decl));
13487
13488   set_decl_abstract_flags (decl, 1);
13489   dwarf2out_decl (decl);
13490   if (! was_abstract)
13491     set_decl_abstract_flags (decl, 0);
13492
13493   current_function_decl = save_fn;
13494   pop_cfun ();
13495 }
13496
13497 /* Helper function of premark_used_types() which gets called through
13498    htab_traverse_resize().
13499
13500    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13501    marked as unused by prune_unused_types.  */
13502 static int
13503 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13504 {
13505   tree type;
13506   dw_die_ref die;
13507
13508   type = (tree) *slot;
13509   die = lookup_type_die (type);
13510   if (die != NULL)
13511     die->die_perennial_p = 1;
13512   return 1;
13513 }
13514
13515 /* Mark all members of used_types_hash as perennial.  */
13516 static void
13517 premark_used_types (void)
13518 {
13519   if (cfun && cfun->used_types_hash)
13520     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13521 }
13522
13523 /* Generate a DIE to represent a declared function (either file-scope or
13524    block-local).  */
13525
13526 static void
13527 gen_subprogram_die (tree decl, dw_die_ref context_die)
13528 {
13529   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13530   tree origin = decl_ultimate_origin (decl);
13531   dw_die_ref subr_die;
13532   tree fn_arg_types;
13533   tree outer_scope;
13534   dw_die_ref old_die = lookup_decl_die (decl);
13535   int declaration = (current_function_decl != decl
13536                      || class_or_namespace_scope_p (context_die));
13537
13538   premark_used_types ();
13539
13540   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13541      started to generate the abstract instance of an inline, decided to output
13542      its containing class, and proceeded to emit the declaration of the inline
13543      from the member list for the class.  If so, DECLARATION takes priority;
13544      we'll get back to the abstract instance when done with the class.  */
13545
13546   /* The class-scope declaration DIE must be the primary DIE.  */
13547   if (origin && declaration && class_or_namespace_scope_p (context_die))
13548     {
13549       origin = NULL;
13550       gcc_assert (!old_die);
13551     }
13552
13553   /* Now that the C++ front end lazily declares artificial member fns, we
13554      might need to retrofit the declaration into its class.  */
13555   if (!declaration && !origin && !old_die
13556       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13557       && !class_or_namespace_scope_p (context_die)
13558       && debug_info_level > DINFO_LEVEL_TERSE)
13559     old_die = force_decl_die (decl);
13560
13561   if (origin != NULL)
13562     {
13563       gcc_assert (!declaration || local_scope_p (context_die));
13564
13565       /* Fixup die_parent for the abstract instance of a nested
13566          inline function.  */
13567       if (old_die && old_die->die_parent == NULL)
13568         add_child_die (context_die, old_die);
13569
13570       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13571       add_abstract_origin_attribute (subr_die, origin);
13572     }
13573   else if (old_die)
13574     {
13575       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13576       struct dwarf_file_data * file_index = lookup_filename (s.file);
13577
13578       if (!get_AT_flag (old_die, DW_AT_declaration)
13579           /* We can have a normal definition following an inline one in the
13580              case of redefinition of GNU C extern inlines.
13581              It seems reasonable to use AT_specification in this case.  */
13582           && !get_AT (old_die, DW_AT_inline))
13583         {
13584           /* Detect and ignore this case, where we are trying to output
13585              something we have already output.  */
13586           return;
13587         }
13588
13589       /* If the definition comes from the same place as the declaration,
13590          maybe use the old DIE.  We always want the DIE for this function
13591          that has the *_pc attributes to be under comp_unit_die so the
13592          debugger can find it.  We also need to do this for abstract
13593          instances of inlines, since the spec requires the out-of-line copy
13594          to have the same parent.  For local class methods, this doesn't
13595          apply; we just use the old DIE.  */
13596       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13597           && (DECL_ARTIFICIAL (decl)
13598               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13599                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13600                       == (unsigned) s.line))))
13601         {
13602           subr_die = old_die;
13603
13604           /* Clear out the declaration attribute and the formal parameters.
13605              Do not remove all children, because it is possible that this
13606              declaration die was forced using force_decl_die(). In such
13607              cases die that forced declaration die (e.g. TAG_imported_module)
13608              is one of the children that we do not want to remove.  */
13609           remove_AT (subr_die, DW_AT_declaration);
13610           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13611         }
13612       else
13613         {
13614           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13615           add_AT_specification (subr_die, old_die);
13616           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13617             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13618           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13619             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13620         }
13621     }
13622   else
13623     {
13624       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13625
13626       if (TREE_PUBLIC (decl))
13627         add_AT_flag (subr_die, DW_AT_external, 1);
13628
13629       add_name_and_src_coords_attributes (subr_die, decl);
13630       if (debug_info_level > DINFO_LEVEL_TERSE)
13631         {
13632           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13633           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13634                               0, 0, context_die);
13635         }
13636
13637       add_pure_or_virtual_attribute (subr_die, decl);
13638       if (DECL_ARTIFICIAL (decl))
13639         add_AT_flag (subr_die, DW_AT_artificial, 1);
13640
13641       if (TREE_PROTECTED (decl))
13642         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13643       else if (TREE_PRIVATE (decl))
13644         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13645     }
13646
13647   if (declaration)
13648     {
13649       if (!old_die || !get_AT (old_die, DW_AT_inline))
13650         {
13651           add_AT_flag (subr_die, DW_AT_declaration, 1);
13652
13653           /* If this is an explicit function declaration then generate
13654              a DW_AT_explicit attribute.  */
13655           if (lang_hooks.decls.function_decl_explicit_p (decl))
13656             add_AT_flag (subr_die, DW_AT_explicit, 1);
13657
13658           /* The first time we see a member function, it is in the context of
13659              the class to which it belongs.  We make sure of this by emitting
13660              the class first.  The next time is the definition, which is
13661              handled above.  The two may come from the same source text.
13662
13663              Note that force_decl_die() forces function declaration die. It is
13664              later reused to represent definition.  */
13665           equate_decl_number_to_die (decl, subr_die);
13666         }
13667     }
13668   else if (DECL_ABSTRACT (decl))
13669     {
13670       if (DECL_DECLARED_INLINE_P (decl))
13671         {
13672           if (cgraph_function_possibly_inlined_p (decl))
13673             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13674           else
13675             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13676         }
13677       else
13678         {
13679           if (cgraph_function_possibly_inlined_p (decl))
13680             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13681           else
13682             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13683         }
13684
13685       if (DECL_DECLARED_INLINE_P (decl)
13686           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13687         add_AT_flag (subr_die, DW_AT_artificial, 1);
13688
13689       equate_decl_number_to_die (decl, subr_die);
13690     }
13691   else if (!DECL_EXTERNAL (decl))
13692     {
13693       HOST_WIDE_INT cfa_fb_offset;
13694
13695       if (!old_die || !get_AT (old_die, DW_AT_inline))
13696         equate_decl_number_to_die (decl, subr_die);
13697
13698       if (!flag_reorder_blocks_and_partition)
13699         {
13700           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13701                                        current_function_funcdef_no);
13702           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13703           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13704                                        current_function_funcdef_no);
13705           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13706
13707           add_pubname (decl, subr_die);
13708           add_arange (decl, subr_die);
13709         }
13710       else
13711         {  /* Do nothing for now; maybe need to duplicate die, one for
13712               hot section and one for cold section, then use the hot/cold
13713               section begin/end labels to generate the aranges...  */
13714           /*
13715             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13716             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13717             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13718             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13719
13720             add_pubname (decl, subr_die);
13721             add_arange (decl, subr_die);
13722             add_arange (decl, subr_die);
13723            */
13724         }
13725
13726 #ifdef MIPS_DEBUGGING_INFO
13727       /* Add a reference to the FDE for this routine.  */
13728       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13729 #endif
13730
13731       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13732
13733       /* We define the "frame base" as the function's CFA.  This is more
13734          convenient for several reasons: (1) It's stable across the prologue
13735          and epilogue, which makes it better than just a frame pointer,
13736          (2) With dwarf3, there exists a one-byte encoding that allows us
13737          to reference the .debug_frame data by proxy, but failing that,
13738          (3) We can at least reuse the code inspection and interpretation
13739          code that determines the CFA position at various points in the
13740          function.  */
13741       /* ??? Use some command-line or configury switch to enable the use
13742          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13743          consumers that understand it; fall back to "pure" dwarf2 and
13744          convert the CFA data into a location list.  */
13745       {
13746         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13747         if (list->dw_loc_next)
13748           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13749         else
13750           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13751       }
13752
13753       /* Compute a displacement from the "steady-state frame pointer" to
13754          the CFA.  The former is what all stack slots and argument slots
13755          will reference in the rtl; the later is what we've told the
13756          debugger about.  We'll need to adjust all frame_base references
13757          by this displacement.  */
13758       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13759
13760       if (cfun->static_chain_decl)
13761         add_AT_location_description (subr_die, DW_AT_static_link,
13762                  loc_descriptor_from_tree (cfun->static_chain_decl));
13763     }
13764
13765   /* Now output descriptions of the arguments for this function. This gets
13766      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13767      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13768      `...' at the end of the formal parameter list.  In order to find out if
13769      there was a trailing ellipsis or not, we must instead look at the type
13770      associated with the FUNCTION_DECL.  This will be a node of type
13771      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13772      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13773      an ellipsis at the end.  */
13774
13775   /* In the case where we are describing a mere function declaration, all we
13776      need to do here (and all we *can* do here) is to describe the *types* of
13777      its formal parameters.  */
13778   if (debug_info_level <= DINFO_LEVEL_TERSE)
13779     ;
13780   else if (declaration)
13781     gen_formal_types_die (decl, subr_die);
13782   else
13783     {
13784       /* Generate DIEs to represent all known formal parameters.  */
13785       tree arg_decls = DECL_ARGUMENTS (decl);
13786       tree parm;
13787
13788       /* When generating DIEs, generate the unspecified_parameters DIE
13789          instead if we come across the arg "__builtin_va_alist" */
13790       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13791         if (TREE_CODE (parm) == PARM_DECL)
13792           {
13793             if (DECL_NAME (parm)
13794                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13795                             "__builtin_va_alist"))
13796               gen_unspecified_parameters_die (parm, subr_die);
13797             else
13798               gen_decl_die (parm, NULL, subr_die);
13799           }
13800
13801       /* Decide whether we need an unspecified_parameters DIE at the end.
13802          There are 2 more cases to do this for: 1) the ansi ... declaration -
13803          this is detectable when the end of the arg list is not a
13804          void_type_node 2) an unprototyped function declaration (not a
13805          definition).  This just means that we have no info about the
13806          parameters at all.  */
13807       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13808       if (fn_arg_types != NULL)
13809         {
13810           /* This is the prototyped case, check for....  */
13811           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13812             gen_unspecified_parameters_die (decl, subr_die);
13813         }
13814       else if (DECL_INITIAL (decl) == NULL_TREE)
13815         gen_unspecified_parameters_die (decl, subr_die);
13816     }
13817
13818   /* Output Dwarf info for all of the stuff within the body of the function
13819      (if it has one - it may be just a declaration).  */
13820   outer_scope = DECL_INITIAL (decl);
13821
13822   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13823      a function.  This BLOCK actually represents the outermost binding contour
13824      for the function, i.e. the contour in which the function's formal
13825      parameters and labels get declared. Curiously, it appears that the front
13826      end doesn't actually put the PARM_DECL nodes for the current function onto
13827      the BLOCK_VARS list for this outer scope, but are strung off of the
13828      DECL_ARGUMENTS list for the function instead.
13829
13830      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13831      the LABEL_DECL nodes for the function however, and we output DWARF info
13832      for those in decls_for_scope.  Just within the `outer_scope' there will be
13833      a BLOCK node representing the function's outermost pair of curly braces,
13834      and any blocks used for the base and member initializers of a C++
13835      constructor function.  */
13836   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13837     {
13838       /* Emit a DW_TAG_variable DIE for a named return value.  */
13839       if (DECL_NAME (DECL_RESULT (decl)))
13840         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
13841
13842       current_function_has_inlines = 0;
13843       decls_for_scope (outer_scope, subr_die, 0);
13844
13845 #if 0 && defined (MIPS_DEBUGGING_INFO)
13846       if (current_function_has_inlines)
13847         {
13848           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13849           if (! comp_unit_has_inlines)
13850             {
13851               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13852               comp_unit_has_inlines = 1;
13853             }
13854         }
13855 #endif
13856     }
13857   /* Add the calling convention attribute if requested.  */
13858   add_calling_convention_attribute (subr_die, decl);
13859
13860 }
13861
13862 /* Returns a hash value for X (which really is a die_struct).  */
13863
13864 static hashval_t
13865 common_block_die_table_hash (const void *x)
13866 {
13867   const_dw_die_ref d = (const_dw_die_ref) x;
13868   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
13869 }
13870
13871 /* Return nonzero if decl_id and die_parent of die_struct X is the same
13872    as decl_id and die_parent of die_struct Y.  */
13873
13874 static int
13875 common_block_die_table_eq (const void *x, const void *y)
13876 {
13877   const_dw_die_ref d = (const_dw_die_ref) x;
13878   const_dw_die_ref e = (const_dw_die_ref) y;
13879   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
13880 }
13881
13882 /* Generate a DIE to represent a declared data object.
13883    Either DECL or ORIGIN must be non-null.  */
13884
13885 static void
13886 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
13887 {
13888   HOST_WIDE_INT off;
13889   tree com_decl;
13890   tree decl_or_origin = decl ? decl : origin;
13891   dw_die_ref var_die;
13892   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
13893   dw_die_ref origin_die;
13894   int declaration = (DECL_EXTERNAL (decl_or_origin)
13895                      /* If DECL is COMDAT and has not actually been
13896                         emitted, we cannot take its address; there
13897                         might end up being no definition anywhere in
13898                         the program.  For example, consider the C++
13899                         test case:
13900
13901                           template <class T>
13902                           struct S { static const int i = 7; };
13903
13904                           template <class T>
13905                           const int S<T>::i;
13906
13907                           int f() { return S<int>::i; }
13908
13909                         Here, S<int>::i is not DECL_EXTERNAL, but no
13910                         definition is required, so the compiler will
13911                         not emit a definition.  */
13912                      || (TREE_CODE (decl_or_origin) == VAR_DECL
13913                          && DECL_COMDAT (decl_or_origin)
13914                          && !TREE_ASM_WRITTEN (decl_or_origin))
13915                      || class_or_namespace_scope_p (context_die));
13916
13917   if (!origin)
13918     origin = decl_ultimate_origin (decl);
13919
13920   com_decl = fortran_common (decl_or_origin, &off);
13921
13922   /* Symbol in common gets emitted as a child of the common block, in the form
13923      of a data member.  */
13924   if (com_decl)
13925     {
13926       tree field;
13927       dw_die_ref com_die;
13928       dw_loc_descr_ref loc;
13929       die_node com_die_arg;
13930
13931       var_die = lookup_decl_die (decl_or_origin);
13932       if (var_die)
13933         {
13934           if (get_AT (var_die, DW_AT_location) == NULL)
13935             {
13936               loc = loc_descriptor_from_tree (com_decl);
13937               if (loc)
13938                 {
13939                   if (off)
13940                     {
13941                       /* Optimize the common case.  */
13942                       if (loc->dw_loc_opc == DW_OP_addr
13943                           && loc->dw_loc_next == NULL
13944                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
13945                              == SYMBOL_REF)
13946                         loc->dw_loc_oprnd1.v.val_addr
13947                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
13948                         else
13949                           loc_descr_plus_const (&loc, off);
13950                     }
13951                   add_AT_loc (var_die, DW_AT_location, loc);
13952                   remove_AT (var_die, DW_AT_declaration);
13953                 }
13954             }
13955           return;
13956         }
13957
13958       if (common_block_die_table == NULL)
13959         common_block_die_table
13960           = htab_create_ggc (10, common_block_die_table_hash,
13961                              common_block_die_table_eq, NULL);
13962
13963       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13964       com_die_arg.decl_id = DECL_UID (com_decl);
13965       com_die_arg.die_parent = context_die;
13966       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
13967       loc = loc_descriptor_from_tree (com_decl);
13968       if (com_die == NULL)
13969         {
13970           const char *cnam
13971             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13972           void **slot;
13973
13974           com_die = new_die (DW_TAG_common_block, context_die, decl);
13975           add_name_and_src_coords_attributes (com_die, com_decl);
13976           if (loc)
13977             {
13978               add_AT_loc (com_die, DW_AT_location, loc);
13979               /* Avoid sharing the same loc descriptor between
13980                  DW_TAG_common_block and DW_TAG_variable.  */
13981               loc = loc_descriptor_from_tree (com_decl);
13982             }
13983           else if (DECL_EXTERNAL (decl))
13984             add_AT_flag (com_die, DW_AT_declaration, 1);
13985           add_pubname_string (cnam, com_die); /* ??? needed? */
13986           com_die->decl_id = DECL_UID (com_decl);
13987           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
13988           *slot = (void *) com_die;
13989         }
13990       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13991         {
13992           add_AT_loc (com_die, DW_AT_location, loc);
13993           loc = loc_descriptor_from_tree (com_decl);
13994           remove_AT (com_die, DW_AT_declaration);
13995         }
13996       var_die = new_die (DW_TAG_variable, com_die, decl);
13997       add_name_and_src_coords_attributes (var_die, decl);
13998       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13999                           TREE_THIS_VOLATILE (decl), context_die);
14000       add_AT_flag (var_die, DW_AT_external, 1);
14001       if (loc)
14002         {
14003           if (off)
14004             {
14005               /* Optimize the common case.  */
14006               if (loc->dw_loc_opc == DW_OP_addr
14007                   && loc->dw_loc_next == NULL
14008                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14009                 loc->dw_loc_oprnd1.v.val_addr
14010                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14011               else
14012                 loc_descr_plus_const (&loc, off);
14013             }
14014           add_AT_loc (var_die, DW_AT_location, loc);
14015         }
14016       else if (DECL_EXTERNAL (decl))
14017         add_AT_flag (var_die, DW_AT_declaration, 1);
14018       equate_decl_number_to_die (decl, var_die);
14019       return;
14020     }
14021
14022   /* If the compiler emitted a definition for the DECL declaration
14023      and if we already emitted a DIE for it, don't emit a second
14024      DIE for it again.  */
14025   if (old_die
14026       && declaration
14027       && old_die->die_parent == context_die)
14028     return;
14029
14030   /* For static data members, the declaration in the class is supposed
14031      to have DW_TAG_member tag; the specification should still be
14032      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14033   if (declaration && class_scope_p (context_die))
14034     var_die = new_die (DW_TAG_member, context_die, decl);
14035   else
14036     var_die = new_die (DW_TAG_variable, context_die, decl);
14037
14038   origin_die = NULL;
14039   if (origin != NULL)
14040     origin_die = add_abstract_origin_attribute (var_die, origin);
14041
14042   /* Loop unrolling can create multiple blocks that refer to the same
14043      static variable, so we must test for the DW_AT_declaration flag.
14044
14045      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14046      copy decls and set the DECL_ABSTRACT flag on them instead of
14047      sharing them.
14048
14049      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14050
14051      ??? The declare_in_namespace support causes us to get two DIEs for one
14052      variable, both of which are declarations.  We want to avoid considering
14053      one to be a specification, so we must test that this DIE is not a
14054      declaration.  */
14055   else if (old_die && TREE_STATIC (decl) && ! declaration
14056            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14057     {
14058       /* This is a definition of a C++ class level static.  */
14059       add_AT_specification (var_die, old_die);
14060       if (DECL_NAME (decl))
14061         {
14062           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14063           struct dwarf_file_data * file_index = lookup_filename (s.file);
14064
14065           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14066             add_AT_file (var_die, DW_AT_decl_file, file_index);
14067
14068           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14069             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14070         }
14071     }
14072   else
14073     {
14074       tree type = TREE_TYPE (decl);
14075
14076       add_name_and_src_coords_attributes (var_die, decl);
14077       if ((TREE_CODE (decl) == PARM_DECL
14078            || TREE_CODE (decl) == RESULT_DECL
14079            || TREE_CODE (decl) == VAR_DECL)
14080           && DECL_BY_REFERENCE (decl))
14081         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14082       else
14083         add_type_attribute (var_die, type, TREE_READONLY (decl),
14084                             TREE_THIS_VOLATILE (decl), context_die);
14085
14086       if (TREE_PUBLIC (decl))
14087         add_AT_flag (var_die, DW_AT_external, 1);
14088
14089       if (DECL_ARTIFICIAL (decl))
14090         add_AT_flag (var_die, DW_AT_artificial, 1);
14091
14092       if (TREE_PROTECTED (decl))
14093         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14094       else if (TREE_PRIVATE (decl))
14095         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14096     }
14097
14098   if (declaration)
14099     add_AT_flag (var_die, DW_AT_declaration, 1);
14100
14101   if (decl && (DECL_ABSTRACT (decl) || declaration))
14102     equate_decl_number_to_die (decl, var_die);
14103
14104   if (! declaration
14105       && (! DECL_ABSTRACT (decl_or_origin)
14106           /* Local static vars are shared between all clones/inlines,
14107              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14108              already set.  */
14109           || (TREE_CODE (decl_or_origin) == VAR_DECL
14110               && TREE_STATIC (decl_or_origin)
14111               && DECL_RTL_SET_P (decl_or_origin)))
14112       /* When abstract origin already has DW_AT_location attribute, no need
14113          to add it again.  */
14114       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14115     {
14116       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14117           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14118         defer_location (decl_or_origin, var_die);
14119       else
14120         add_location_or_const_value_attribute (var_die,
14121                                                decl_or_origin,
14122                                                DW_AT_location);
14123       add_pubname (decl_or_origin, var_die);
14124     }
14125   else
14126     tree_add_const_value_attribute (var_die, decl_or_origin);
14127 }
14128
14129 /* Generate a DIE to represent a named constant.  */
14130
14131 static void
14132 gen_const_die (tree decl, dw_die_ref context_die)
14133 {
14134   dw_die_ref const_die;
14135   tree type = TREE_TYPE (decl);
14136
14137   const_die = new_die (DW_TAG_constant, context_die, decl);
14138   add_name_and_src_coords_attributes (const_die, decl);
14139   add_type_attribute (const_die, type, 1, 0, context_die);
14140   if (TREE_PUBLIC (decl))
14141     add_AT_flag (const_die, DW_AT_external, 1);
14142   if (DECL_ARTIFICIAL (decl))
14143     add_AT_flag (const_die, DW_AT_artificial, 1);
14144   tree_add_const_value_attribute (const_die, decl);
14145 }
14146
14147 /* Generate a DIE to represent a label identifier.  */
14148
14149 static void
14150 gen_label_die (tree decl, dw_die_ref context_die)
14151 {
14152   tree origin = decl_ultimate_origin (decl);
14153   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14154   rtx insn;
14155   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14156
14157   if (origin != NULL)
14158     add_abstract_origin_attribute (lbl_die, origin);
14159   else
14160     add_name_and_src_coords_attributes (lbl_die, decl);
14161
14162   if (DECL_ABSTRACT (decl))
14163     equate_decl_number_to_die (decl, lbl_die);
14164   else
14165     {
14166       insn = DECL_RTL_IF_SET (decl);
14167
14168       /* Deleted labels are programmer specified labels which have been
14169          eliminated because of various optimizations.  We still emit them
14170          here so that it is possible to put breakpoints on them.  */
14171       if (insn
14172           && (LABEL_P (insn)
14173               || ((NOTE_P (insn)
14174                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14175         {
14176           /* When optimization is enabled (via -O) some parts of the compiler
14177              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14178              represent source-level labels which were explicitly declared by
14179              the user.  This really shouldn't be happening though, so catch
14180              it if it ever does happen.  */
14181           gcc_assert (!INSN_DELETED_P (insn));
14182
14183           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14184           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14185         }
14186     }
14187 }
14188
14189 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14190    attributes to the DIE for a block STMT, to describe where the inlined
14191    function was called from.  This is similar to add_src_coords_attributes.  */
14192
14193 static inline void
14194 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14195 {
14196   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14197
14198   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14199   add_AT_unsigned (die, DW_AT_call_line, s.line);
14200 }
14201
14202
14203 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14204    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14205
14206 static inline void
14207 add_high_low_attributes (tree stmt, dw_die_ref die)
14208 {
14209   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14210
14211   if (BLOCK_FRAGMENT_CHAIN (stmt))
14212     {
14213       tree chain;
14214
14215       if (inlined_function_outer_scope_p (stmt))
14216         {
14217           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14218                                        BLOCK_NUMBER (stmt));
14219           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14220         }
14221
14222       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14223
14224       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14225       do
14226         {
14227           add_ranges (chain);
14228           chain = BLOCK_FRAGMENT_CHAIN (chain);
14229         }
14230       while (chain);
14231       add_ranges (NULL);
14232     }
14233   else
14234     {
14235       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14236                                    BLOCK_NUMBER (stmt));
14237       add_AT_lbl_id (die, DW_AT_low_pc, label);
14238       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14239                                    BLOCK_NUMBER (stmt));
14240       add_AT_lbl_id (die, DW_AT_high_pc, label);
14241     }
14242 }
14243
14244 /* Generate a DIE for a lexical block.  */
14245
14246 static void
14247 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14248 {
14249   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14250
14251   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14252     add_high_low_attributes (stmt, stmt_die);
14253
14254   decls_for_scope (stmt, stmt_die, depth);
14255 }
14256
14257 /* Generate a DIE for an inlined subprogram.  */
14258
14259 static void
14260 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14261 {
14262   tree decl = block_ultimate_origin (stmt);
14263
14264   /* Emit info for the abstract instance first, if we haven't yet.  We
14265      must emit this even if the block is abstract, otherwise when we
14266      emit the block below (or elsewhere), we may end up trying to emit
14267      a die whose origin die hasn't been emitted, and crashing.  */
14268   dwarf2out_abstract_function (decl);
14269
14270   if (! BLOCK_ABSTRACT (stmt))
14271     {
14272       dw_die_ref subr_die
14273         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14274
14275       add_abstract_origin_attribute (subr_die, decl);
14276       if (TREE_ASM_WRITTEN (stmt))
14277         add_high_low_attributes (stmt, subr_die);
14278       add_call_src_coords_attributes (stmt, subr_die);
14279
14280       decls_for_scope (stmt, subr_die, depth);
14281       current_function_has_inlines = 1;
14282     }
14283   else
14284     /* We may get here if we're the outer block of function A that was
14285        inlined into function B that was inlined into function C.  When
14286        generating debugging info for C, dwarf2out_abstract_function(B)
14287        would mark all inlined blocks as abstract, including this one.
14288        So, we wouldn't (and shouldn't) expect labels to be generated
14289        for this one.  Instead, just emit debugging info for
14290        declarations within the block.  This is particularly important
14291        in the case of initializers of arguments passed from B to us:
14292        if they're statement expressions containing declarations, we
14293        wouldn't generate dies for their abstract variables, and then,
14294        when generating dies for the real variables, we'd die (pun
14295        intended :-)  */
14296     gen_lexical_block_die (stmt, context_die, depth);
14297 }
14298
14299 /* Generate a DIE for a field in a record, or structure.  */
14300
14301 static void
14302 gen_field_die (tree decl, dw_die_ref context_die)
14303 {
14304   dw_die_ref decl_die;
14305
14306   if (TREE_TYPE (decl) == error_mark_node)
14307     return;
14308
14309   decl_die = new_die (DW_TAG_member, context_die, decl);
14310   add_name_and_src_coords_attributes (decl_die, decl);
14311   add_type_attribute (decl_die, member_declared_type (decl),
14312                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14313                       context_die);
14314
14315   if (DECL_BIT_FIELD_TYPE (decl))
14316     {
14317       add_byte_size_attribute (decl_die, decl);
14318       add_bit_size_attribute (decl_die, decl);
14319       add_bit_offset_attribute (decl_die, decl);
14320     }
14321
14322   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14323     add_data_member_location_attribute (decl_die, decl);
14324
14325   if (DECL_ARTIFICIAL (decl))
14326     add_AT_flag (decl_die, DW_AT_artificial, 1);
14327
14328   if (TREE_PROTECTED (decl))
14329     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14330   else if (TREE_PRIVATE (decl))
14331     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14332
14333   /* Equate decl number to die, so that we can look up this decl later on.  */
14334   equate_decl_number_to_die (decl, decl_die);
14335 }
14336
14337 #if 0
14338 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14339    Use modified_type_die instead.
14340    We keep this code here just in case these types of DIEs may be needed to
14341    represent certain things in other languages (e.g. Pascal) someday.  */
14342
14343 static void
14344 gen_pointer_type_die (tree type, dw_die_ref context_die)
14345 {
14346   dw_die_ref ptr_die
14347     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14348
14349   equate_type_number_to_die (type, ptr_die);
14350   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14351   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14352 }
14353
14354 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14355    Use modified_type_die instead.
14356    We keep this code here just in case these types of DIEs may be needed to
14357    represent certain things in other languages (e.g. Pascal) someday.  */
14358
14359 static void
14360 gen_reference_type_die (tree type, dw_die_ref context_die)
14361 {
14362   dw_die_ref ref_die
14363     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14364
14365   equate_type_number_to_die (type, ref_die);
14366   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14367   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14368 }
14369 #endif
14370
14371 /* Generate a DIE for a pointer to a member type.  */
14372
14373 static void
14374 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14375 {
14376   dw_die_ref ptr_die
14377     = new_die (DW_TAG_ptr_to_member_type,
14378                scope_die_for (type, context_die), type);
14379
14380   equate_type_number_to_die (type, ptr_die);
14381   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14382                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14383   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14384 }
14385
14386 /* Generate the DIE for the compilation unit.  */
14387
14388 static dw_die_ref
14389 gen_compile_unit_die (const char *filename)
14390 {
14391   dw_die_ref die;
14392   char producer[250];
14393   const char *language_string = lang_hooks.name;
14394   int language;
14395
14396   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14397
14398   if (filename)
14399     {
14400       add_name_attribute (die, filename);
14401       /* Don't add cwd for <built-in>.  */
14402       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14403         add_comp_dir_attribute (die);
14404     }
14405
14406   sprintf (producer, "%s %s", language_string, version_string);
14407
14408 #ifdef MIPS_DEBUGGING_INFO
14409   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14410      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14411      not appear in the producer string, the debugger reaches the conclusion
14412      that the object file is stripped and has no debugging information.
14413      To get the MIPS/SGI debugger to believe that there is debugging
14414      information in the object file, we add a -g to the producer string.  */
14415   if (debug_info_level > DINFO_LEVEL_TERSE)
14416     strcat (producer, " -g");
14417 #endif
14418
14419   add_AT_string (die, DW_AT_producer, producer);
14420
14421   if (strcmp (language_string, "GNU C++") == 0)
14422     language = DW_LANG_C_plus_plus;
14423   else if (strcmp (language_string, "GNU Ada") == 0)
14424     language = DW_LANG_Ada95;
14425   else if (strcmp (language_string, "GNU F77") == 0)
14426     language = DW_LANG_Fortran77;
14427   else if (strcmp (language_string, "GNU Fortran") == 0)
14428     language = DW_LANG_Fortran95;
14429   else if (strcmp (language_string, "GNU Pascal") == 0)
14430     language = DW_LANG_Pascal83;
14431   else if (strcmp (language_string, "GNU Java") == 0)
14432     language = DW_LANG_Java;
14433   else if (strcmp (language_string, "GNU Objective-C") == 0)
14434     language = DW_LANG_ObjC;
14435   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14436     language = DW_LANG_ObjC_plus_plus;
14437   else
14438     language = DW_LANG_C89;
14439
14440   add_AT_unsigned (die, DW_AT_language, language);
14441   return die;
14442 }
14443
14444 /* Generate the DIE for a base class.  */
14445
14446 static void
14447 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14448 {
14449   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14450
14451   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14452   add_data_member_location_attribute (die, binfo);
14453
14454   if (BINFO_VIRTUAL_P (binfo))
14455     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14456
14457   if (access == access_public_node)
14458     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14459   else if (access == access_protected_node)
14460     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14461 }
14462
14463 /* Generate a DIE for a class member.  */
14464
14465 static void
14466 gen_member_die (tree type, dw_die_ref context_die)
14467 {
14468   tree member;
14469   tree binfo = TYPE_BINFO (type);
14470   dw_die_ref child;
14471
14472   /* If this is not an incomplete type, output descriptions of each of its
14473      members. Note that as we output the DIEs necessary to represent the
14474      members of this record or union type, we will also be trying to output
14475      DIEs to represent the *types* of those members. However the `type'
14476      function (above) will specifically avoid generating type DIEs for member
14477      types *within* the list of member DIEs for this (containing) type except
14478      for those types (of members) which are explicitly marked as also being
14479      members of this (containing) type themselves.  The g++ front- end can
14480      force any given type to be treated as a member of some other (containing)
14481      type by setting the TYPE_CONTEXT of the given (member) type to point to
14482      the TREE node representing the appropriate (containing) type.  */
14483
14484   /* First output info about the base classes.  */
14485   if (binfo)
14486     {
14487       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14488       int i;
14489       tree base;
14490
14491       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14492         gen_inheritance_die (base,
14493                              (accesses ? VEC_index (tree, accesses, i)
14494                               : access_public_node), context_die);
14495     }
14496
14497   /* Now output info about the data members and type members.  */
14498   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14499     {
14500       /* If we thought we were generating minimal debug info for TYPE
14501          and then changed our minds, some of the member declarations
14502          may have already been defined.  Don't define them again, but
14503          do put them in the right order.  */
14504
14505       child = lookup_decl_die (member);
14506       if (child)
14507         splice_child_die (context_die, child);
14508       else
14509         gen_decl_die (member, NULL, context_die);
14510     }
14511
14512   /* Now output info about the function members (if any).  */
14513   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14514     {
14515       /* Don't include clones in the member list.  */
14516       if (DECL_ABSTRACT_ORIGIN (member))
14517         continue;
14518
14519       child = lookup_decl_die (member);
14520       if (child)
14521         splice_child_die (context_die, child);
14522       else
14523         gen_decl_die (member, NULL, context_die);
14524     }
14525 }
14526
14527 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14528    is set, we pretend that the type was never defined, so we only get the
14529    member DIEs needed by later specification DIEs.  */
14530
14531 static void
14532 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14533                                 enum debug_info_usage usage)
14534 {
14535   dw_die_ref type_die = lookup_type_die (type);
14536   dw_die_ref scope_die = 0;
14537   int nested = 0;
14538   int complete = (TYPE_SIZE (type)
14539                   && (! TYPE_STUB_DECL (type)
14540                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14541   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14542   complete = complete && should_emit_struct_debug (type, usage);
14543
14544   if (type_die && ! complete)
14545     return;
14546
14547   if (TYPE_CONTEXT (type) != NULL_TREE
14548       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14549           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14550     nested = 1;
14551
14552   scope_die = scope_die_for (type, context_die);
14553
14554   if (! type_die || (nested && scope_die == comp_unit_die))
14555     /* First occurrence of type or toplevel definition of nested class.  */
14556     {
14557       dw_die_ref old_die = type_die;
14558
14559       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14560                           ? record_type_tag (type) : DW_TAG_union_type,
14561                           scope_die, type);
14562       equate_type_number_to_die (type, type_die);
14563       if (old_die)
14564         add_AT_specification (type_die, old_die);
14565       else
14566         add_name_attribute (type_die, type_tag (type));
14567     }
14568   else
14569     remove_AT (type_die, DW_AT_declaration);
14570
14571   /* If this type has been completed, then give it a byte_size attribute and
14572      then give a list of members.  */
14573   if (complete && !ns_decl)
14574     {
14575       /* Prevent infinite recursion in cases where the type of some member of
14576          this type is expressed in terms of this type itself.  */
14577       TREE_ASM_WRITTEN (type) = 1;
14578       add_byte_size_attribute (type_die, type);
14579       if (TYPE_STUB_DECL (type) != NULL_TREE)
14580         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14581
14582       /* If the first reference to this type was as the return type of an
14583          inline function, then it may not have a parent.  Fix this now.  */
14584       if (type_die->die_parent == NULL)
14585         add_child_die (scope_die, type_die);
14586
14587       push_decl_scope (type);
14588       gen_member_die (type, type_die);
14589       pop_decl_scope ();
14590
14591       /* GNU extension: Record what type our vtable lives in.  */
14592       if (TYPE_VFIELD (type))
14593         {
14594           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14595
14596           gen_type_die (vtype, context_die);
14597           add_AT_die_ref (type_die, DW_AT_containing_type,
14598                           lookup_type_die (vtype));
14599         }
14600     }
14601   else
14602     {
14603       add_AT_flag (type_die, DW_AT_declaration, 1);
14604
14605       /* We don't need to do this for function-local types.  */
14606       if (TYPE_STUB_DECL (type)
14607           && ! decl_function_context (TYPE_STUB_DECL (type)))
14608         VEC_safe_push (tree, gc, incomplete_types, type);
14609     }
14610
14611   if (get_AT (type_die, DW_AT_name))
14612     add_pubtype (type, type_die);
14613 }
14614
14615 /* Generate a DIE for a subroutine _type_.  */
14616
14617 static void
14618 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14619 {
14620   tree return_type = TREE_TYPE (type);
14621   dw_die_ref subr_die
14622     = new_die (DW_TAG_subroutine_type,
14623                scope_die_for (type, context_die), type);
14624
14625   equate_type_number_to_die (type, subr_die);
14626   add_prototyped_attribute (subr_die, type);
14627   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14628   gen_formal_types_die (type, subr_die);
14629
14630   if (get_AT (subr_die, DW_AT_name))
14631     add_pubtype (type, subr_die);
14632 }
14633
14634 /* Generate a DIE for a type definition.  */
14635
14636 static void
14637 gen_typedef_die (tree decl, dw_die_ref context_die)
14638 {
14639   dw_die_ref type_die;
14640   tree origin;
14641
14642   if (TREE_ASM_WRITTEN (decl))
14643     return;
14644
14645   TREE_ASM_WRITTEN (decl) = 1;
14646   type_die = new_die (DW_TAG_typedef, context_die, decl);
14647   origin = decl_ultimate_origin (decl);
14648   if (origin != NULL)
14649     add_abstract_origin_attribute (type_die, origin);
14650   else
14651     {
14652       tree type;
14653
14654       add_name_and_src_coords_attributes (type_die, decl);
14655       if (DECL_ORIGINAL_TYPE (decl))
14656         {
14657           type = DECL_ORIGINAL_TYPE (decl);
14658
14659           gcc_assert (type != TREE_TYPE (decl));
14660           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14661         }
14662       else
14663         type = TREE_TYPE (decl);
14664
14665       add_type_attribute (type_die, type, TREE_READONLY (decl),
14666                           TREE_THIS_VOLATILE (decl), context_die);
14667     }
14668
14669   if (DECL_ABSTRACT (decl))
14670     equate_decl_number_to_die (decl, type_die);
14671
14672   if (get_AT (type_die, DW_AT_name))
14673     add_pubtype (decl, type_die);
14674 }
14675
14676 /* Generate a type description DIE.  */
14677
14678 static void
14679 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14680                                 enum debug_info_usage usage)
14681 {
14682   int need_pop;
14683   struct array_descr_info info;
14684
14685   if (type == NULL_TREE || type == error_mark_node)
14686     return;
14687
14688   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14689       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14690     {
14691       if (TREE_ASM_WRITTEN (type))
14692         return;
14693
14694       /* Prevent broken recursion; we can't hand off to the same type.  */
14695       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14696
14697       TREE_ASM_WRITTEN (type) = 1;
14698       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14699       return;
14700     }
14701
14702   /* If this is an array type with hidden descriptor, handle it first.  */
14703   if (!TREE_ASM_WRITTEN (type)
14704       && lang_hooks.types.get_array_descr_info
14705       && lang_hooks.types.get_array_descr_info (type, &info))
14706     {
14707       gen_descr_array_type_die (type, &info, context_die);
14708       TREE_ASM_WRITTEN (type) = 1;
14709       return;
14710     }
14711
14712   /* We are going to output a DIE to represent the unqualified version
14713      of this type (i.e. without any const or volatile qualifiers) so
14714      get the main variant (i.e. the unqualified version) of this type
14715      now.  (Vectors are special because the debugging info is in the
14716      cloned type itself).  */
14717   if (TREE_CODE (type) != VECTOR_TYPE)
14718     type = type_main_variant (type);
14719
14720   if (TREE_ASM_WRITTEN (type))
14721     return;
14722
14723   switch (TREE_CODE (type))
14724     {
14725     case ERROR_MARK:
14726       break;
14727
14728     case POINTER_TYPE:
14729     case REFERENCE_TYPE:
14730       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14731          ensures that the gen_type_die recursion will terminate even if the
14732          type is recursive.  Recursive types are possible in Ada.  */
14733       /* ??? We could perhaps do this for all types before the switch
14734          statement.  */
14735       TREE_ASM_WRITTEN (type) = 1;
14736
14737       /* For these types, all that is required is that we output a DIE (or a
14738          set of DIEs) to represent the "basis" type.  */
14739       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14740                                 DINFO_USAGE_IND_USE);
14741       break;
14742
14743     case OFFSET_TYPE:
14744       /* This code is used for C++ pointer-to-data-member types.
14745          Output a description of the relevant class type.  */
14746       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14747                                         DINFO_USAGE_IND_USE);
14748
14749       /* Output a description of the type of the object pointed to.  */
14750       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14751                                         DINFO_USAGE_IND_USE);
14752
14753       /* Now output a DIE to represent this pointer-to-data-member type
14754          itself.  */
14755       gen_ptr_to_mbr_type_die (type, context_die);
14756       break;
14757
14758     case FUNCTION_TYPE:
14759       /* Force out return type (in case it wasn't forced out already).  */
14760       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14761                                         DINFO_USAGE_DIR_USE);
14762       gen_subroutine_type_die (type, context_die);
14763       break;
14764
14765     case METHOD_TYPE:
14766       /* Force out return type (in case it wasn't forced out already).  */
14767       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14768                                         DINFO_USAGE_DIR_USE);
14769       gen_subroutine_type_die (type, context_die);
14770       break;
14771
14772     case ARRAY_TYPE:
14773       gen_array_type_die (type, context_die);
14774       break;
14775
14776     case VECTOR_TYPE:
14777       gen_array_type_die (type, context_die);
14778       break;
14779
14780     case ENUMERAL_TYPE:
14781     case RECORD_TYPE:
14782     case UNION_TYPE:
14783     case QUAL_UNION_TYPE:
14784       /* If this is a nested type whose containing class hasn't been written
14785          out yet, writing it out will cover this one, too.  This does not apply
14786          to instantiations of member class templates; they need to be added to
14787          the containing class as they are generated.  FIXME: This hurts the
14788          idea of combining type decls from multiple TUs, since we can't predict
14789          what set of template instantiations we'll get.  */
14790       if (TYPE_CONTEXT (type)
14791           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14792           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14793         {
14794           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14795
14796           if (TREE_ASM_WRITTEN (type))
14797             return;
14798
14799           /* If that failed, attach ourselves to the stub.  */
14800           push_decl_scope (TYPE_CONTEXT (type));
14801           context_die = lookup_type_die (TYPE_CONTEXT (type));
14802           need_pop = 1;
14803         }
14804       else
14805         {
14806           context_die = declare_in_namespace (type, context_die);
14807           need_pop = 0;
14808         }
14809
14810       if (TREE_CODE (type) == ENUMERAL_TYPE)
14811         {
14812           /* This might have been written out by the call to
14813              declare_in_namespace.  */
14814           if (!TREE_ASM_WRITTEN (type))
14815             gen_enumeration_type_die (type, context_die);
14816         }
14817       else
14818         gen_struct_or_union_type_die (type, context_die, usage);
14819
14820       if (need_pop)
14821         pop_decl_scope ();
14822
14823       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14824          it up if it is ever completed.  gen_*_type_die will set it for us
14825          when appropriate.  */
14826       return;
14827
14828     case VOID_TYPE:
14829     case INTEGER_TYPE:
14830     case REAL_TYPE:
14831     case FIXED_POINT_TYPE:
14832     case COMPLEX_TYPE:
14833     case BOOLEAN_TYPE:
14834       /* No DIEs needed for fundamental types.  */
14835       break;
14836
14837     case LANG_TYPE:
14838       /* No Dwarf representation currently defined.  */
14839       break;
14840
14841     default:
14842       gcc_unreachable ();
14843     }
14844
14845   TREE_ASM_WRITTEN (type) = 1;
14846 }
14847
14848 static void
14849 gen_type_die (tree type, dw_die_ref context_die)
14850 {
14851   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14852 }
14853
14854 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14855    things which are local to the given block.  */
14856
14857 static void
14858 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14859 {
14860   int must_output_die = 0;
14861   bool inlined_func;
14862
14863   /* Ignore blocks that are NULL.  */
14864   if (stmt == NULL_TREE)
14865     return;
14866
14867   inlined_func = inlined_function_outer_scope_p (stmt);
14868
14869   /* If the block is one fragment of a non-contiguous block, do not
14870      process the variables, since they will have been done by the
14871      origin block.  Do process subblocks.  */
14872   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14873     {
14874       tree sub;
14875
14876       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14877         gen_block_die (sub, context_die, depth + 1);
14878
14879       return;
14880     }
14881
14882   /* Determine if we need to output any Dwarf DIEs at all to represent this
14883      block.  */
14884   if (inlined_func)
14885     /* The outer scopes for inlinings *must* always be represented.  We
14886        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14887     must_output_die = 1;
14888   else
14889     {
14890       /* Determine if this block directly contains any "significant"
14891          local declarations which we will need to output DIEs for.  */
14892       if (debug_info_level > DINFO_LEVEL_TERSE)
14893         /* We are not in terse mode so *any* local declaration counts
14894            as being a "significant" one.  */
14895         must_output_die = ((BLOCK_VARS (stmt) != NULL
14896                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
14897                            && (TREE_USED (stmt)
14898                                || TREE_ASM_WRITTEN (stmt)
14899                                || BLOCK_ABSTRACT (stmt)));
14900       else if ((TREE_USED (stmt)
14901                 || TREE_ASM_WRITTEN (stmt)
14902                 || BLOCK_ABSTRACT (stmt))
14903                && !dwarf2out_ignore_block (stmt))
14904         must_output_die = 1;
14905     }
14906
14907   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14908      DIE for any block which contains no significant local declarations at
14909      all.  Rather, in such cases we just call `decls_for_scope' so that any
14910      needed Dwarf info for any sub-blocks will get properly generated. Note
14911      that in terse mode, our definition of what constitutes a "significant"
14912      local declaration gets restricted to include only inlined function
14913      instances and local (nested) function definitions.  */
14914   if (must_output_die)
14915     {
14916       if (inlined_func)
14917         gen_inlined_subroutine_die (stmt, context_die, depth);
14918       else
14919         gen_lexical_block_die (stmt, context_die, depth);
14920     }
14921   else
14922     decls_for_scope (stmt, context_die, depth);
14923 }
14924
14925 /* Process variable DECL (or variable with origin ORIGIN) within
14926    block STMT and add it to CONTEXT_DIE.  */
14927 static void
14928 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
14929 {
14930   dw_die_ref die;
14931   tree decl_or_origin = decl ? decl : origin;
14932   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
14933
14934   if (ultimate_origin)
14935     origin = ultimate_origin;
14936
14937   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
14938     die = lookup_decl_die (decl_or_origin);
14939   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
14940            && TYPE_DECL_IS_STUB (decl_or_origin))
14941     die = lookup_type_die (TREE_TYPE (decl_or_origin));
14942   else
14943     die = NULL;
14944
14945   if (die != NULL && die->die_parent == NULL)
14946     add_child_die (context_die, die);
14947   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
14948     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
14949                                          stmt, context_die);
14950   else
14951     gen_decl_die (decl, origin, context_die);
14952 }
14953
14954 /* Generate all of the decls declared within a given scope and (recursively)
14955    all of its sub-blocks.  */
14956
14957 static void
14958 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14959 {
14960   tree decl;
14961   unsigned int i;
14962   tree subblocks;
14963
14964   /* Ignore NULL blocks.  */
14965   if (stmt == NULL_TREE)
14966     return;
14967
14968   /* Output the DIEs to represent all of the data objects and typedefs
14969      declared directly within this block but not within any nested
14970      sub-blocks.  Also, nested function and tag DIEs have been
14971      generated with a parent of NULL; fix that up now.  */
14972   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14973     process_scope_var (stmt, decl, NULL_TREE, context_die);
14974   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
14975     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
14976                        context_die);
14977
14978   /* If we're at -g1, we're not interested in subblocks.  */
14979   if (debug_info_level <= DINFO_LEVEL_TERSE)
14980     return;
14981
14982   /* Output the DIEs to represent all sub-blocks (and the items declared
14983      therein) of this block.  */
14984   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14985        subblocks != NULL;
14986        subblocks = BLOCK_CHAIN (subblocks))
14987     gen_block_die (subblocks, context_die, depth + 1);
14988 }
14989
14990 /* Is this a typedef we can avoid emitting?  */
14991
14992 static inline int
14993 is_redundant_typedef (const_tree decl)
14994 {
14995   if (TYPE_DECL_IS_STUB (decl))
14996     return 1;
14997
14998   if (DECL_ARTIFICIAL (decl)
14999       && DECL_CONTEXT (decl)
15000       && is_tagged_type (DECL_CONTEXT (decl))
15001       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15002       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15003     /* Also ignore the artificial member typedef for the class name.  */
15004     return 1;
15005
15006   return 0;
15007 }
15008
15009 /* Returns the DIE for a context.  */
15010
15011 static inline dw_die_ref
15012 get_context_die (tree context)
15013 {
15014   if (context)
15015     {
15016       /* Find die that represents this context.  */
15017       if (TYPE_P (context))
15018         return force_type_die (context);
15019       else
15020         return force_decl_die (context);
15021     }
15022   return comp_unit_die;
15023 }
15024
15025 /* Returns the DIE for decl.  A DIE will always be returned.  */
15026
15027 static dw_die_ref
15028 force_decl_die (tree decl)
15029 {
15030   dw_die_ref decl_die;
15031   unsigned saved_external_flag;
15032   tree save_fn = NULL_TREE;
15033   decl_die = lookup_decl_die (decl);
15034   if (!decl_die)
15035     {
15036       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15037
15038       decl_die = lookup_decl_die (decl);
15039       if (decl_die)
15040         return decl_die;
15041
15042       switch (TREE_CODE (decl))
15043         {
15044         case FUNCTION_DECL:
15045           /* Clear current_function_decl, so that gen_subprogram_die thinks
15046              that this is a declaration. At this point, we just want to force
15047              declaration die.  */
15048           save_fn = current_function_decl;
15049           current_function_decl = NULL_TREE;
15050           gen_subprogram_die (decl, context_die);
15051           current_function_decl = save_fn;
15052           break;
15053
15054         case VAR_DECL:
15055           /* Set external flag to force declaration die. Restore it after
15056            gen_decl_die() call.  */
15057           saved_external_flag = DECL_EXTERNAL (decl);
15058           DECL_EXTERNAL (decl) = 1;
15059           gen_decl_die (decl, NULL, context_die);
15060           DECL_EXTERNAL (decl) = saved_external_flag;
15061           break;
15062
15063         case NAMESPACE_DECL:
15064           dwarf2out_decl (decl);
15065           break;
15066
15067         default:
15068           gcc_unreachable ();
15069         }
15070
15071       /* We should be able to find the DIE now.  */
15072       if (!decl_die)
15073         decl_die = lookup_decl_die (decl);
15074       gcc_assert (decl_die);
15075     }
15076
15077   return decl_die;
15078 }
15079
15080 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15081    always returned.  */
15082
15083 static dw_die_ref
15084 force_type_die (tree type)
15085 {
15086   dw_die_ref type_die;
15087
15088   type_die = lookup_type_die (type);
15089   if (!type_die)
15090     {
15091       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15092
15093       type_die = modified_type_die (type, TYPE_READONLY (type),
15094                                     TYPE_VOLATILE (type), context_die);
15095       gcc_assert (type_die);
15096     }
15097   return type_die;
15098 }
15099
15100 /* Force out any required namespaces to be able to output DECL,
15101    and return the new context_die for it, if it's changed.  */
15102
15103 static dw_die_ref
15104 setup_namespace_context (tree thing, dw_die_ref context_die)
15105 {
15106   tree context = (DECL_P (thing)
15107                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15108   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15109     /* Force out the namespace.  */
15110     context_die = force_decl_die (context);
15111
15112   return context_die;
15113 }
15114
15115 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15116    type) within its namespace, if appropriate.
15117
15118    For compatibility with older debuggers, namespace DIEs only contain
15119    declarations; all definitions are emitted at CU scope.  */
15120
15121 static dw_die_ref
15122 declare_in_namespace (tree thing, dw_die_ref context_die)
15123 {
15124   dw_die_ref ns_context;
15125
15126   if (debug_info_level <= DINFO_LEVEL_TERSE)
15127     return context_die;
15128
15129   /* If this decl is from an inlined function, then don't try to emit it in its
15130      namespace, as we will get confused.  It would have already been emitted
15131      when the abstract instance of the inline function was emitted anyways.  */
15132   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15133     return context_die;
15134
15135   ns_context = setup_namespace_context (thing, context_die);
15136
15137   if (ns_context != context_die)
15138     {
15139       if (is_fortran ())
15140         return ns_context;
15141       if (DECL_P (thing))
15142         gen_decl_die (thing, NULL, ns_context);
15143       else
15144         gen_type_die (thing, ns_context);
15145     }
15146   return context_die;
15147 }
15148
15149 /* Generate a DIE for a namespace or namespace alias.  */
15150
15151 static void
15152 gen_namespace_die (tree decl, dw_die_ref context_die)
15153 {
15154   dw_die_ref namespace_die;
15155
15156   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15157      they are an alias of.  */
15158   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15159     {
15160       /* Output a real namespace or module.  */
15161       context_die = setup_namespace_context (decl, comp_unit_die);
15162       namespace_die = new_die (is_fortran ()
15163                                ? DW_TAG_module : DW_TAG_namespace,
15164                                context_die, decl);
15165       /* For Fortran modules defined in different CU don't add src coords.  */
15166       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15167         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15168       else
15169         add_name_and_src_coords_attributes (namespace_die, decl);
15170       if (DECL_EXTERNAL (decl))
15171         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15172       equate_decl_number_to_die (decl, namespace_die);
15173     }
15174   else
15175     {
15176       /* Output a namespace alias.  */
15177
15178       /* Force out the namespace we are an alias of, if necessary.  */
15179       dw_die_ref origin_die
15180         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15181
15182       if (DECL_CONTEXT (decl) == NULL_TREE
15183           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15184         context_die = setup_namespace_context (decl, comp_unit_die);
15185       /* Now create the namespace alias DIE.  */
15186       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15187       add_name_and_src_coords_attributes (namespace_die, decl);
15188       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15189       equate_decl_number_to_die (decl, namespace_die);
15190     }
15191 }
15192
15193 /* Generate Dwarf debug information for a decl described by DECL.  */
15194
15195 static void
15196 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15197 {
15198   tree decl_or_origin = decl ? decl : origin;
15199   tree class_origin = NULL;
15200
15201   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15202     return;
15203
15204   switch (TREE_CODE (decl_or_origin))
15205     {
15206     case ERROR_MARK:
15207       break;
15208
15209     case CONST_DECL:
15210       if (!is_fortran ())
15211         {
15212           /* The individual enumerators of an enum type get output when we output
15213              the Dwarf representation of the relevant enum type itself.  */
15214           break;
15215         }
15216
15217       /* Emit its type.  */
15218       gen_type_die (TREE_TYPE (decl), context_die);
15219
15220       /* And its containing namespace.  */
15221       context_die = declare_in_namespace (decl, context_die);
15222
15223       gen_const_die (decl, context_die);
15224       break;
15225
15226     case FUNCTION_DECL:
15227       /* Don't output any DIEs to represent mere function declarations,
15228          unless they are class members or explicit block externs.  */
15229       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15230           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15231           && (current_function_decl == NULL_TREE
15232               || DECL_ARTIFICIAL (decl_or_origin)))
15233         break;
15234
15235 #if 0
15236       /* FIXME */
15237       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15238          on local redeclarations of global functions.  That seems broken.  */
15239       if (current_function_decl != decl)
15240         /* This is only a declaration.  */;
15241 #endif
15242
15243       /* If we're emitting a clone, emit info for the abstract instance.  */
15244       if (origin || DECL_ORIGIN (decl) != decl)
15245         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15246
15247       /* If we're emitting an out-of-line copy of an inline function,
15248          emit info for the abstract instance and set up to refer to it.  */
15249       else if (cgraph_function_possibly_inlined_p (decl)
15250                && ! DECL_ABSTRACT (decl)
15251                && ! class_or_namespace_scope_p (context_die)
15252                /* dwarf2out_abstract_function won't emit a die if this is just
15253                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15254                   that case, because that works only if we have a die.  */
15255                && DECL_INITIAL (decl) != NULL_TREE)
15256         {
15257           dwarf2out_abstract_function (decl);
15258           set_decl_origin_self (decl);
15259         }
15260
15261       /* Otherwise we're emitting the primary DIE for this decl.  */
15262       else if (debug_info_level > DINFO_LEVEL_TERSE)
15263         {
15264           /* Before we describe the FUNCTION_DECL itself, make sure that we
15265              have described its return type.  */
15266           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15267
15268           /* And its virtual context.  */
15269           if (DECL_VINDEX (decl) != NULL_TREE)
15270             gen_type_die (DECL_CONTEXT (decl), context_die);
15271
15272           /* And its containing type.  */
15273           if (!origin)
15274             origin = decl_class_context (decl);
15275           if (origin != NULL_TREE)
15276             gen_type_die_for_member (origin, decl, context_die);
15277
15278           /* And its containing namespace.  */
15279           context_die = declare_in_namespace (decl, context_die);
15280         }
15281
15282       /* Now output a DIE to represent the function itself.  */
15283       if (decl)
15284         gen_subprogram_die (decl, context_die);
15285       break;
15286
15287     case TYPE_DECL:
15288       /* If we are in terse mode, don't generate any DIEs to represent any
15289          actual typedefs.  */
15290       if (debug_info_level <= DINFO_LEVEL_TERSE)
15291         break;
15292
15293       /* In the special case of a TYPE_DECL node representing the declaration
15294          of some type tag, if the given TYPE_DECL is marked as having been
15295          instantiated from some other (original) TYPE_DECL node (e.g. one which
15296          was generated within the original definition of an inline function) we
15297          used to generate a special (abbreviated) DW_TAG_structure_type,
15298          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15299          should be actually referencing those DIEs, as variable DIEs with that
15300          type would be emitted already in the abstract origin, so it was always
15301          removed during unused type prunning.  Don't add anything in this
15302          case.  */
15303       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15304         break;
15305
15306       if (is_redundant_typedef (decl))
15307         gen_type_die (TREE_TYPE (decl), context_die);
15308       else
15309         /* Output a DIE to represent the typedef itself.  */
15310         gen_typedef_die (decl, context_die);
15311       break;
15312
15313     case LABEL_DECL:
15314       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15315         gen_label_die (decl, context_die);
15316       break;
15317
15318     case VAR_DECL:
15319     case RESULT_DECL:
15320       /* If we are in terse mode, don't generate any DIEs to represent any
15321          variable declarations or definitions.  */
15322       if (debug_info_level <= DINFO_LEVEL_TERSE)
15323         break;
15324
15325       /* Output any DIEs that are needed to specify the type of this data
15326          object.  */
15327       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15328            || TREE_CODE (decl_or_origin) == VAR_DECL)
15329           && DECL_BY_REFERENCE (decl_or_origin))
15330         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15331       else
15332         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15333
15334       /* And its containing type.  */
15335       class_origin = decl_class_context (decl_or_origin);
15336       if (class_origin != NULL_TREE)
15337         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15338
15339       /* And its containing namespace.  */
15340       context_die = declare_in_namespace (decl_or_origin, context_die);
15341
15342       /* Now output the DIE to represent the data object itself.  This gets
15343          complicated because of the possibility that the VAR_DECL really
15344          represents an inlined instance of a formal parameter for an inline
15345          function.  */
15346       if (!origin)
15347         origin = decl_ultimate_origin (decl);
15348       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15349         gen_formal_parameter_die (decl, origin, context_die);
15350       else
15351         gen_variable_die (decl, origin, context_die);
15352       break;
15353
15354     case FIELD_DECL:
15355       /* Ignore the nameless fields that are used to skip bits but handle C++
15356          anonymous unions and structs.  */
15357       if (DECL_NAME (decl) != NULL_TREE
15358           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15359           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15360         {
15361           gen_type_die (member_declared_type (decl), context_die);
15362           gen_field_die (decl, context_die);
15363         }
15364       break;
15365
15366     case PARM_DECL:
15367       if (DECL_BY_REFERENCE (decl_or_origin))
15368         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15369       else
15370         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15371       gen_formal_parameter_die (decl, origin, context_die);
15372       break;
15373
15374     case NAMESPACE_DECL:
15375     case IMPORTED_DECL:
15376       gen_namespace_die (decl, context_die);
15377       break;
15378
15379     default:
15380       /* Probably some frontend-internal decl.  Assume we don't care.  */
15381       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15382       break;
15383     }
15384 }
15385 \f
15386 /* Output debug information for global decl DECL.  Called from toplev.c after
15387    compilation proper has finished.  */
15388
15389 static void
15390 dwarf2out_global_decl (tree decl)
15391 {
15392   /* Output DWARF2 information for file-scope tentative data object
15393      declarations, file-scope (extern) function declarations (which
15394      had no corresponding body) and file-scope tagged type declarations
15395      and definitions which have not yet been forced out.  */
15396   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15397     dwarf2out_decl (decl);
15398 }
15399
15400 /* Output debug information for type decl DECL.  Called from toplev.c
15401    and from language front ends (to record built-in types).  */
15402 static void
15403 dwarf2out_type_decl (tree decl, int local)
15404 {
15405   if (!local)
15406     dwarf2out_decl (decl);
15407 }
15408
15409 /* Output debug information for imported module or decl DECL.
15410    NAME is non-NULL name in the lexical block if the decl has been renamed.
15411    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15412    that DECL belongs to.
15413    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15414 static void
15415 dwarf2out_imported_module_or_decl_1 (tree decl,
15416                                      tree name,
15417                                      tree lexical_block,
15418                                      dw_die_ref lexical_block_die)
15419 {
15420   expanded_location xloc;
15421   dw_die_ref imported_die = NULL;
15422   dw_die_ref at_import_die;
15423
15424   if (TREE_CODE (decl) == IMPORTED_DECL)
15425     {
15426       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15427       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15428       gcc_assert (decl);
15429     }
15430   else
15431     xloc = expand_location (input_location);
15432
15433   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15434     {
15435       if (is_base_type (TREE_TYPE (decl)))
15436         at_import_die = base_type_die (TREE_TYPE (decl));
15437       else
15438         at_import_die = force_type_die (TREE_TYPE (decl));
15439       /* For namespace N { typedef void T; } using N::T; base_type_die
15440          returns NULL, but DW_TAG_imported_declaration requires
15441          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15442       if (!at_import_die)
15443         {
15444           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15445           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15446           at_import_die = lookup_type_die (TREE_TYPE (decl));
15447           gcc_assert (at_import_die);
15448         }
15449     }
15450   else
15451     {
15452       at_import_die = lookup_decl_die (decl);
15453       if (!at_import_die)
15454         {
15455           /* If we're trying to avoid duplicate debug info, we may not have
15456              emitted the member decl for this field.  Emit it now.  */
15457           if (TREE_CODE (decl) == FIELD_DECL)
15458             {
15459               tree type = DECL_CONTEXT (decl);
15460
15461               if (TYPE_CONTEXT (type)
15462                   && TYPE_P (TYPE_CONTEXT (type))
15463                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15464                                                 DINFO_USAGE_DIR_USE))
15465                 return;
15466               gen_type_die_for_member (type, decl,
15467                                        get_context_die (TYPE_CONTEXT (type)));
15468             }
15469           at_import_die = force_decl_die (decl);
15470         }
15471     }
15472
15473   if (TREE_CODE (decl) == NAMESPACE_DECL)
15474     imported_die = new_die (DW_TAG_imported_module,
15475                             lexical_block_die,
15476                             lexical_block);
15477   else
15478     imported_die = new_die (DW_TAG_imported_declaration,
15479                             lexical_block_die,
15480                             lexical_block);
15481
15482   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15483   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15484   if (name)
15485     add_AT_string (imported_die, DW_AT_name,
15486                    IDENTIFIER_POINTER (name));
15487   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15488 }
15489
15490 /* Output debug information for imported module or decl DECL.
15491    NAME is non-NULL name in context if the decl has been renamed.
15492    CHILD is true if decl is one of the renamed decls as part of
15493    importing whole module.  */
15494
15495 static void
15496 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15497                                    bool child)
15498 {
15499   /* dw_die_ref at_import_die;  */
15500   dw_die_ref scope_die;
15501
15502   if (debug_info_level <= DINFO_LEVEL_TERSE)
15503     return;
15504
15505   gcc_assert (decl);
15506
15507   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15508      We need decl DIE for reference and scope die. First, get DIE for the decl
15509      itself.  */
15510
15511   /* Get the scope die for decl context. Use comp_unit_die for global module
15512      or decl. If die is not found for non globals, force new die.  */
15513   if (context
15514       && TYPE_P (context)
15515       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15516     return;
15517   scope_die = get_context_die (context);
15518
15519   if (child)
15520     {
15521       gcc_assert (scope_die->die_child);
15522       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15523       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15524       scope_die = scope_die->die_child;
15525     }
15526
15527   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15528   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15529
15530 }
15531
15532 /* Write the debugging output for DECL.  */
15533
15534 void
15535 dwarf2out_decl (tree decl)
15536 {
15537   dw_die_ref context_die = comp_unit_die;
15538
15539   switch (TREE_CODE (decl))
15540     {
15541     case ERROR_MARK:
15542       return;
15543
15544     case FUNCTION_DECL:
15545       /* What we would really like to do here is to filter out all mere
15546          file-scope declarations of file-scope functions which are never
15547          referenced later within this translation unit (and keep all of ones
15548          that *are* referenced later on) but we aren't clairvoyant, so we have
15549          no idea which functions will be referenced in the future (i.e. later
15550          on within the current translation unit). So here we just ignore all
15551          file-scope function declarations which are not also definitions.  If
15552          and when the debugger needs to know something about these functions,
15553          it will have to hunt around and find the DWARF information associated
15554          with the definition of the function.
15555
15556          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15557          nodes represent definitions and which ones represent mere
15558          declarations.  We have to check DECL_INITIAL instead. That's because
15559          the C front-end supports some weird semantics for "extern inline"
15560          function definitions.  These can get inlined within the current
15561          translation unit (and thus, we need to generate Dwarf info for their
15562          abstract instances so that the Dwarf info for the concrete inlined
15563          instances can have something to refer to) but the compiler never
15564          generates any out-of-lines instances of such things (despite the fact
15565          that they *are* definitions).
15566
15567          The important point is that the C front-end marks these "extern
15568          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15569          them anyway. Note that the C++ front-end also plays some similar games
15570          for inline function definitions appearing within include files which
15571          also contain `#pragma interface' pragmas.  */
15572       if (DECL_INITIAL (decl) == NULL_TREE)
15573         return;
15574
15575       /* If we're a nested function, initially use a parent of NULL; if we're
15576          a plain function, this will be fixed up in decls_for_scope.  If
15577          we're a method, it will be ignored, since we already have a DIE.  */
15578       if (decl_function_context (decl)
15579           /* But if we're in terse mode, we don't care about scope.  */
15580           && debug_info_level > DINFO_LEVEL_TERSE)
15581         context_die = NULL;
15582       break;
15583
15584     case VAR_DECL:
15585       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15586          declaration and if the declaration was never even referenced from
15587          within this entire compilation unit.  We suppress these DIEs in
15588          order to save space in the .debug section (by eliminating entries
15589          which are probably useless).  Note that we must not suppress
15590          block-local extern declarations (whether used or not) because that
15591          would screw-up the debugger's name lookup mechanism and cause it to
15592          miss things which really ought to be in scope at a given point.  */
15593       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15594         return;
15595
15596       /* For local statics lookup proper context die.  */
15597       if (TREE_STATIC (decl) && decl_function_context (decl))
15598         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15599
15600       /* If we are in terse mode, don't generate any DIEs to represent any
15601          variable declarations or definitions.  */
15602       if (debug_info_level <= DINFO_LEVEL_TERSE)
15603         return;
15604       break;
15605
15606     case CONST_DECL:
15607       if (debug_info_level <= DINFO_LEVEL_TERSE)
15608         return;
15609       if (!is_fortran ())
15610         return;
15611       if (TREE_STATIC (decl) && decl_function_context (decl))
15612         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15613       break;
15614
15615     case NAMESPACE_DECL:
15616     case IMPORTED_DECL:
15617       if (debug_info_level <= DINFO_LEVEL_TERSE)
15618         return;
15619       if (lookup_decl_die (decl) != NULL)
15620         return;
15621       break;
15622
15623     case TYPE_DECL:
15624       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15625       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15626         return;
15627
15628       /* Don't bother trying to generate any DIEs to represent any of the
15629          normal built-in types for the language we are compiling.  */
15630       if (DECL_IS_BUILTIN (decl))
15631         {
15632           /* OK, we need to generate one for `bool' so GDB knows what type
15633              comparisons have.  */
15634           if (is_cxx ()
15635               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15636               && ! DECL_IGNORED_P (decl))
15637             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15638
15639           return;
15640         }
15641
15642       /* If we are in terse mode, don't generate any DIEs for types.  */
15643       if (debug_info_level <= DINFO_LEVEL_TERSE)
15644         return;
15645
15646       /* If we're a function-scope tag, initially use a parent of NULL;
15647          this will be fixed up in decls_for_scope.  */
15648       if (decl_function_context (decl))
15649         context_die = NULL;
15650
15651       break;
15652
15653     default:
15654       return;
15655     }
15656
15657   gen_decl_die (decl, NULL, context_die);
15658 }
15659
15660 /* Output a marker (i.e. a label) for the beginning of the generated code for
15661    a lexical block.  */
15662
15663 static void
15664 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15665                        unsigned int blocknum)
15666 {
15667   switch_to_section (current_function_section ());
15668   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15669 }
15670
15671 /* Output a marker (i.e. a label) for the end of the generated code for a
15672    lexical block.  */
15673
15674 static void
15675 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15676 {
15677   switch_to_section (current_function_section ());
15678   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15679 }
15680
15681 /* Returns nonzero if it is appropriate not to emit any debugging
15682    information for BLOCK, because it doesn't contain any instructions.
15683
15684    Don't allow this for blocks with nested functions or local classes
15685    as we would end up with orphans, and in the presence of scheduling
15686    we may end up calling them anyway.  */
15687
15688 static bool
15689 dwarf2out_ignore_block (const_tree block)
15690 {
15691   tree decl;
15692   unsigned int i;
15693
15694   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15695     if (TREE_CODE (decl) == FUNCTION_DECL
15696         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15697       return 0;
15698   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15699     {
15700       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15701       if (TREE_CODE (decl) == FUNCTION_DECL
15702           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15703       return 0;
15704     }
15705
15706   return 1;
15707 }
15708
15709 /* Hash table routines for file_hash.  */
15710
15711 static int
15712 file_table_eq (const void *p1_p, const void *p2_p)
15713 {
15714   const struct dwarf_file_data *const p1 =
15715     (const struct dwarf_file_data *) p1_p;
15716   const char *const p2 = (const char *) p2_p;
15717   return strcmp (p1->filename, p2) == 0;
15718 }
15719
15720 static hashval_t
15721 file_table_hash (const void *p_p)
15722 {
15723   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15724   return htab_hash_string (p->filename);
15725 }
15726
15727 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15728    dwarf2out.c) and return its "index".  The index of each (known) filename is
15729    just a unique number which is associated with only that one filename.  We
15730    need such numbers for the sake of generating labels (in the .debug_sfnames
15731    section) and references to those files numbers (in the .debug_srcinfo
15732    and.debug_macinfo sections).  If the filename given as an argument is not
15733    found in our current list, add it to the list and assign it the next
15734    available unique index number.  In order to speed up searches, we remember
15735    the index of the filename was looked up last.  This handles the majority of
15736    all searches.  */
15737
15738 static struct dwarf_file_data *
15739 lookup_filename (const char *file_name)
15740 {
15741   void ** slot;
15742   struct dwarf_file_data * created;
15743
15744   /* Check to see if the file name that was searched on the previous
15745      call matches this file name.  If so, return the index.  */
15746   if (file_table_last_lookup
15747       && (file_name == file_table_last_lookup->filename
15748           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15749     return file_table_last_lookup;
15750
15751   /* Didn't match the previous lookup, search the table.  */
15752   slot = htab_find_slot_with_hash (file_table, file_name,
15753                                    htab_hash_string (file_name), INSERT);
15754   if (*slot)
15755     return (struct dwarf_file_data *) *slot;
15756
15757   created = GGC_NEW (struct dwarf_file_data);
15758   created->filename = file_name;
15759   created->emitted_number = 0;
15760   *slot = created;
15761   return created;
15762 }
15763
15764 /* If the assembler will construct the file table, then translate the compiler
15765    internal file table number into the assembler file table number, and emit
15766    a .file directive if we haven't already emitted one yet.  The file table
15767    numbers are different because we prune debug info for unused variables and
15768    types, which may include filenames.  */
15769
15770 static int
15771 maybe_emit_file (struct dwarf_file_data * fd)
15772 {
15773   if (! fd->emitted_number)
15774     {
15775       if (last_emitted_file)
15776         fd->emitted_number = last_emitted_file->emitted_number + 1;
15777       else
15778         fd->emitted_number = 1;
15779       last_emitted_file = fd;
15780
15781       if (DWARF2_ASM_LINE_DEBUG_INFO)
15782         {
15783           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15784           output_quoted_string (asm_out_file,
15785                                 remap_debug_filename (fd->filename));
15786           fputc ('\n', asm_out_file);
15787         }
15788     }
15789
15790   return fd->emitted_number;
15791 }
15792
15793 /* Replace DW_AT_name for the decl with name.  */
15794  
15795 static void
15796 dwarf2out_set_name (tree decl, tree name)
15797 {
15798   dw_die_ref die;
15799   dw_attr_ref attr;
15800
15801   die = TYPE_SYMTAB_DIE (decl);
15802   if (!die)
15803     return;
15804
15805   attr = get_AT (die, DW_AT_name);
15806   if (attr)
15807     {
15808       struct indirect_string_node *node;
15809
15810       node = find_AT_string (dwarf2_name (name, 0));
15811       /* replace the string.  */
15812       attr->dw_attr_val.v.val_str = node;
15813     }
15814
15815   else
15816     add_name_attribute (die, dwarf2_name (name, 0));
15817 }
15818 /* Called by the final INSN scan whenever we see a var location.  We
15819    use it to drop labels in the right places, and throw the location in
15820    our lookup table.  */
15821
15822 static void
15823 dwarf2out_var_location (rtx loc_note)
15824 {
15825   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15826   struct var_loc_node *newloc;
15827   rtx prev_insn;
15828   static rtx last_insn;
15829   static const char *last_label;
15830   tree decl;
15831
15832   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15833     return;
15834   prev_insn = PREV_INSN (loc_note);
15835
15836   newloc = GGC_CNEW (struct var_loc_node);
15837   /* If the insn we processed last time is the previous insn
15838      and it is also a var location note, use the label we emitted
15839      last time.  */
15840   if (last_insn != NULL_RTX
15841       && last_insn == prev_insn
15842       && NOTE_P (prev_insn)
15843       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15844     {
15845       newloc->label = last_label;
15846     }
15847   else
15848     {
15849       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15850       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15851       loclabel_num++;
15852       newloc->label = ggc_strdup (loclabel);
15853     }
15854   newloc->var_loc_note = loc_note;
15855   newloc->next = NULL;
15856
15857   if (cfun && in_cold_section_p)
15858     newloc->section_label = crtl->subsections.cold_section_label;
15859   else
15860     newloc->section_label = text_section_label;
15861
15862   last_insn = loc_note;
15863   last_label = newloc->label;
15864   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15865   add_var_loc_to_decl (decl, newloc);
15866 }
15867
15868 /* We need to reset the locations at the beginning of each
15869    function. We can't do this in the end_function hook, because the
15870    declarations that use the locations won't have been output when
15871    that hook is called.  Also compute have_multiple_function_sections here.  */
15872
15873 static void
15874 dwarf2out_begin_function (tree fun)
15875 {
15876   htab_empty (decl_loc_table);
15877
15878   if (function_section (fun) != text_section)
15879     have_multiple_function_sections = true;
15880
15881   dwarf2out_note_section_used ();
15882 }
15883
15884 /* Output a label to mark the beginning of a source code line entry
15885    and record information relating to this source line, in
15886    'line_info_table' for later output of the .debug_line section.  */
15887
15888 static void
15889 dwarf2out_source_line (unsigned int line, const char *filename)
15890 {
15891   if (debug_info_level >= DINFO_LEVEL_NORMAL
15892       && line != 0)
15893     {
15894       int file_num = maybe_emit_file (lookup_filename (filename));
15895
15896       switch_to_section (current_function_section ());
15897
15898       /* If requested, emit something human-readable.  */
15899       if (flag_debug_asm)
15900         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15901                  filename, line);
15902
15903       if (DWARF2_ASM_LINE_DEBUG_INFO)
15904         {
15905           /* Emit the .loc directive understood by GNU as.  */
15906           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15907
15908           /* Indicate that line number info exists.  */
15909           line_info_table_in_use++;
15910         }
15911       else if (function_section (current_function_decl) != text_section)
15912         {
15913           dw_separate_line_info_ref line_info;
15914           targetm.asm_out.internal_label (asm_out_file,
15915                                           SEPARATE_LINE_CODE_LABEL,
15916                                           separate_line_info_table_in_use);
15917
15918           /* Expand the line info table if necessary.  */
15919           if (separate_line_info_table_in_use
15920               == separate_line_info_table_allocated)
15921             {
15922               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15923               separate_line_info_table
15924                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15925                                  separate_line_info_table,
15926                                  separate_line_info_table_allocated);
15927               memset (separate_line_info_table
15928                        + separate_line_info_table_in_use,
15929                       0,
15930                       (LINE_INFO_TABLE_INCREMENT
15931                        * sizeof (dw_separate_line_info_entry)));
15932             }
15933
15934           /* Add the new entry at the end of the line_info_table.  */
15935           line_info
15936             = &separate_line_info_table[separate_line_info_table_in_use++];
15937           line_info->dw_file_num = file_num;
15938           line_info->dw_line_num = line;
15939           line_info->function = current_function_funcdef_no;
15940         }
15941       else
15942         {
15943           dw_line_info_ref line_info;
15944
15945           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15946                                      line_info_table_in_use);
15947
15948           /* Expand the line info table if necessary.  */
15949           if (line_info_table_in_use == line_info_table_allocated)
15950             {
15951               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15952               line_info_table
15953                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15954                                  line_info_table_allocated);
15955               memset (line_info_table + line_info_table_in_use, 0,
15956                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15957             }
15958
15959           /* Add the new entry at the end of the line_info_table.  */
15960           line_info = &line_info_table[line_info_table_in_use++];
15961           line_info->dw_file_num = file_num;
15962           line_info->dw_line_num = line;
15963         }
15964     }
15965 }
15966
15967 /* Record the beginning of a new source file.  */
15968
15969 static void
15970 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15971 {
15972   if (flag_eliminate_dwarf2_dups)
15973     {
15974       /* Record the beginning of the file for break_out_includes.  */
15975       dw_die_ref bincl_die;
15976
15977       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15978       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15979     }
15980
15981   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15982     {
15983       int file_num = maybe_emit_file (lookup_filename (filename));
15984
15985       switch_to_section (debug_macinfo_section);
15986       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15987       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15988                                    lineno);
15989
15990       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15991     }
15992 }
15993
15994 /* Record the end of a source file.  */
15995
15996 static void
15997 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15998 {
15999   if (flag_eliminate_dwarf2_dups)
16000     /* Record the end of the file for break_out_includes.  */
16001     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16002
16003   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16004     {
16005       switch_to_section (debug_macinfo_section);
16006       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16007     }
16008 }
16009
16010 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16011    the tail part of the directive line, i.e. the part which is past the
16012    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16013
16014 static void
16015 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16016                   const char *buffer ATTRIBUTE_UNUSED)
16017 {
16018   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16019     {
16020       switch_to_section (debug_macinfo_section);
16021       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16022       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16023       dw2_asm_output_nstring (buffer, -1, "The macro");
16024     }
16025 }
16026
16027 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16028    the tail part of the directive line, i.e. the part which is past the
16029    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16030
16031 static void
16032 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16033                  const char *buffer ATTRIBUTE_UNUSED)
16034 {
16035   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16036     {
16037       switch_to_section (debug_macinfo_section);
16038       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16039       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16040       dw2_asm_output_nstring (buffer, -1, "The macro");
16041     }
16042 }
16043
16044 /* Set up for Dwarf output at the start of compilation.  */
16045
16046 static void
16047 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16048 {
16049   /* Allocate the file_table.  */
16050   file_table = htab_create_ggc (50, file_table_hash,
16051                                 file_table_eq, NULL);
16052
16053   /* Allocate the decl_die_table.  */
16054   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16055                                     decl_die_table_eq, NULL);
16056
16057   /* Allocate the decl_loc_table.  */
16058   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16059                                     decl_loc_table_eq, NULL);
16060
16061   /* Allocate the initial hunk of the decl_scope_table.  */
16062   decl_scope_table = VEC_alloc (tree, gc, 256);
16063
16064   /* Allocate the initial hunk of the abbrev_die_table.  */
16065   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16066   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16067   /* Zero-th entry is allocated, but unused.  */
16068   abbrev_die_table_in_use = 1;
16069
16070   /* Allocate the initial hunk of the line_info_table.  */
16071   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16072   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16073
16074   /* Zero-th entry is allocated, but unused.  */
16075   line_info_table_in_use = 1;
16076
16077   /* Allocate the pubtypes and pubnames vectors.  */
16078   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16079   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16080
16081   /* Generate the initial DIE for the .debug section.  Note that the (string)
16082      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16083      will (typically) be a relative pathname and that this pathname should be
16084      taken as being relative to the directory from which the compiler was
16085      invoked when the given (base) source file was compiled.  We will fill
16086      in this value in dwarf2out_finish.  */
16087   comp_unit_die = gen_compile_unit_die (NULL);
16088
16089   incomplete_types = VEC_alloc (tree, gc, 64);
16090
16091   used_rtx_array = VEC_alloc (rtx, gc, 32);
16092
16093   debug_info_section = get_section (DEBUG_INFO_SECTION,
16094                                     SECTION_DEBUG, NULL);
16095   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16096                                       SECTION_DEBUG, NULL);
16097   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16098                                        SECTION_DEBUG, NULL);
16099   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16100                                        SECTION_DEBUG, NULL);
16101   debug_line_section = get_section (DEBUG_LINE_SECTION,
16102                                     SECTION_DEBUG, NULL);
16103   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16104                                    SECTION_DEBUG, NULL);
16105   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16106                                         SECTION_DEBUG, NULL);
16107 #ifdef DEBUG_PUBTYPES_SECTION
16108   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16109                                         SECTION_DEBUG, NULL);
16110 #endif
16111   debug_str_section = get_section (DEBUG_STR_SECTION,
16112                                    DEBUG_STR_SECTION_FLAGS, NULL);
16113   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16114                                       SECTION_DEBUG, NULL);
16115   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16116                                      SECTION_DEBUG, NULL);
16117
16118   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16119   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16120                                DEBUG_ABBREV_SECTION_LABEL, 0);
16121   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16122   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16123                                COLD_TEXT_SECTION_LABEL, 0);
16124   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16125
16126   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16127                                DEBUG_INFO_SECTION_LABEL, 0);
16128   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16129                                DEBUG_LINE_SECTION_LABEL, 0);
16130   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16131                                DEBUG_RANGES_SECTION_LABEL, 0);
16132   switch_to_section (debug_abbrev_section);
16133   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16134   switch_to_section (debug_info_section);
16135   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16136   switch_to_section (debug_line_section);
16137   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16138
16139   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16140     {
16141       switch_to_section (debug_macinfo_section);
16142       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16143                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16144       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16145     }
16146
16147   switch_to_section (text_section);
16148   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16149   if (flag_reorder_blocks_and_partition)
16150     {
16151       cold_text_section = unlikely_text_section ();
16152       switch_to_section (cold_text_section);
16153       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16154     }
16155 }
16156
16157 /* A helper function for dwarf2out_finish called through
16158    ht_forall.  Emit one queued .debug_str string.  */
16159
16160 static int
16161 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16162 {
16163   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16164
16165   if (node->form == DW_FORM_strp)
16166     {
16167       switch_to_section (debug_str_section);
16168       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16169       assemble_string (node->str, strlen (node->str) + 1);
16170     }
16171
16172   return 1;
16173 }
16174
16175 #if ENABLE_ASSERT_CHECKING
16176 /* Verify that all marks are clear.  */
16177
16178 static void
16179 verify_marks_clear (dw_die_ref die)
16180 {
16181   dw_die_ref c;
16182
16183   gcc_assert (! die->die_mark);
16184   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16185 }
16186 #endif /* ENABLE_ASSERT_CHECKING */
16187
16188 /* Clear the marks for a die and its children.
16189    Be cool if the mark isn't set.  */
16190
16191 static void
16192 prune_unmark_dies (dw_die_ref die)
16193 {
16194   dw_die_ref c;
16195
16196   if (die->die_mark)
16197     die->die_mark = 0;
16198   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16199 }
16200
16201 /* Given DIE that we're marking as used, find any other dies
16202    it references as attributes and mark them as used.  */
16203
16204 static void
16205 prune_unused_types_walk_attribs (dw_die_ref die)
16206 {
16207   dw_attr_ref a;
16208   unsigned ix;
16209
16210   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16211     {
16212       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16213         {
16214           /* A reference to another DIE.
16215              Make sure that it will get emitted.  */
16216           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16217         }
16218       /* Set the string's refcount to 0 so that prune_unused_types_mark
16219          accounts properly for it.  */
16220       if (AT_class (a) == dw_val_class_str)
16221         a->dw_attr_val.v.val_str->refcount = 0;
16222     }
16223 }
16224
16225
16226 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16227    to DIE's children.  */
16228
16229 static void
16230 prune_unused_types_mark (dw_die_ref die, int dokids)
16231 {
16232   dw_die_ref c;
16233
16234   if (die->die_mark == 0)
16235     {
16236       /* We haven't done this node yet.  Mark it as used.  */
16237       die->die_mark = 1;
16238
16239       /* We also have to mark its parents as used.
16240          (But we don't want to mark our parents' kids due to this.)  */
16241       if (die->die_parent)
16242         prune_unused_types_mark (die->die_parent, 0);
16243
16244       /* Mark any referenced nodes.  */
16245       prune_unused_types_walk_attribs (die);
16246
16247       /* If this node is a specification,
16248          also mark the definition, if it exists.  */
16249       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16250         prune_unused_types_mark (die->die_definition, 1);
16251     }
16252
16253   if (dokids && die->die_mark != 2)
16254     {
16255       /* We need to walk the children, but haven't done so yet.
16256          Remember that we've walked the kids.  */
16257       die->die_mark = 2;
16258
16259       /* If this is an array type, we need to make sure our
16260          kids get marked, even if they're types.  */
16261       if (die->die_tag == DW_TAG_array_type)
16262         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16263       else
16264         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16265     }
16266 }
16267
16268 /* For local classes, look if any static member functions were emitted
16269    and if so, mark them.  */
16270
16271 static void
16272 prune_unused_types_walk_local_classes (dw_die_ref die)
16273 {
16274   dw_die_ref c;
16275
16276   if (die->die_mark == 2)
16277     return;
16278
16279   switch (die->die_tag)
16280     {
16281     case DW_TAG_structure_type:
16282     case DW_TAG_union_type:
16283     case DW_TAG_class_type:
16284       break;
16285
16286     case DW_TAG_subprogram:
16287       if (!get_AT_flag (die, DW_AT_declaration)
16288           || die->die_definition != NULL)
16289         prune_unused_types_mark (die, 1);
16290       return;
16291
16292     default:
16293       return;
16294     }
16295
16296   /* Mark children.  */
16297   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16298 }
16299
16300 /* Walk the tree DIE and mark types that we actually use.  */
16301
16302 static void
16303 prune_unused_types_walk (dw_die_ref die)
16304 {
16305   dw_die_ref c;
16306
16307   /* Don't do anything if this node is already marked and
16308      children have been marked as well.  */
16309   if (die->die_mark == 2)
16310     return;
16311
16312   switch (die->die_tag)
16313     {
16314     case DW_TAG_structure_type:
16315     case DW_TAG_union_type:
16316     case DW_TAG_class_type:
16317       if (die->die_perennial_p)
16318         break;
16319
16320       for (c = die->die_parent; c; c = c->die_parent)
16321         if (c->die_tag == DW_TAG_subprogram)
16322           break;
16323
16324       /* Finding used static member functions inside of classes
16325          is needed just for local classes, because for other classes
16326          static member function DIEs with DW_AT_specification
16327          are emitted outside of the DW_TAG_*_type.  If we ever change
16328          it, we'd need to call this even for non-local classes.  */
16329       if (c)
16330         prune_unused_types_walk_local_classes (die);
16331
16332       /* It's a type node --- don't mark it.  */
16333       return;
16334
16335     case DW_TAG_const_type:
16336     case DW_TAG_packed_type:
16337     case DW_TAG_pointer_type:
16338     case DW_TAG_reference_type:
16339     case DW_TAG_volatile_type:
16340     case DW_TAG_typedef:
16341     case DW_TAG_array_type:
16342     case DW_TAG_interface_type:
16343     case DW_TAG_friend:
16344     case DW_TAG_variant_part:
16345     case DW_TAG_enumeration_type:
16346     case DW_TAG_subroutine_type:
16347     case DW_TAG_string_type:
16348     case DW_TAG_set_type:
16349     case DW_TAG_subrange_type:
16350     case DW_TAG_ptr_to_member_type:
16351     case DW_TAG_file_type:
16352       if (die->die_perennial_p)
16353         break;
16354
16355       /* It's a type node --- don't mark it.  */
16356       return;
16357
16358     default:
16359       /* Mark everything else.  */
16360       break;
16361   }
16362
16363   if (die->die_mark == 0)
16364     {
16365       die->die_mark = 1;
16366
16367       /* Now, mark any dies referenced from here.  */
16368       prune_unused_types_walk_attribs (die);
16369     }
16370
16371   die->die_mark = 2;
16372
16373   /* Mark children.  */
16374   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16375 }
16376
16377 /* Increment the string counts on strings referred to from DIE's
16378    attributes.  */
16379
16380 static void
16381 prune_unused_types_update_strings (dw_die_ref die)
16382 {
16383   dw_attr_ref a;
16384   unsigned ix;
16385
16386   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16387     if (AT_class (a) == dw_val_class_str)
16388       {
16389         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16390         s->refcount++;
16391         /* Avoid unnecessarily putting strings that are used less than
16392            twice in the hash table.  */
16393         if (s->refcount
16394             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16395           {
16396             void ** slot;
16397             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16398                                              htab_hash_string (s->str),
16399                                              INSERT);
16400             gcc_assert (*slot == NULL);
16401             *slot = s;
16402           }
16403       }
16404 }
16405
16406 /* Remove from the tree DIE any dies that aren't marked.  */
16407
16408 static void
16409 prune_unused_types_prune (dw_die_ref die)
16410 {
16411   dw_die_ref c;
16412
16413   gcc_assert (die->die_mark);
16414   prune_unused_types_update_strings (die);
16415
16416   if (! die->die_child)
16417     return;
16418
16419   c = die->die_child;
16420   do {
16421     dw_die_ref prev = c;
16422     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16423       if (c == die->die_child)
16424         {
16425           /* No marked children between 'prev' and the end of the list.  */
16426           if (prev == c)
16427             /* No marked children at all.  */
16428             die->die_child = NULL;
16429           else
16430             {
16431               prev->die_sib = c->die_sib;
16432               die->die_child = prev;
16433             }
16434           return;
16435         }
16436
16437     if (c != prev->die_sib)
16438       prev->die_sib = c;
16439     prune_unused_types_prune (c);
16440   } while (c != die->die_child);
16441 }
16442
16443
16444 /* Remove dies representing declarations that we never use.  */
16445
16446 static void
16447 prune_unused_types (void)
16448 {
16449   unsigned int i;
16450   limbo_die_node *node;
16451   pubname_ref pub;
16452
16453 #if ENABLE_ASSERT_CHECKING
16454   /* All the marks should already be clear.  */
16455   verify_marks_clear (comp_unit_die);
16456   for (node = limbo_die_list; node; node = node->next)
16457     verify_marks_clear (node->die);
16458 #endif /* ENABLE_ASSERT_CHECKING */
16459
16460   /* Set the mark on nodes that are actually used.  */
16461   prune_unused_types_walk (comp_unit_die);
16462   for (node = limbo_die_list; node; node = node->next)
16463     prune_unused_types_walk (node->die);
16464
16465   /* Also set the mark on nodes referenced from the
16466      pubname_table or arange_table.  */
16467   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16468     prune_unused_types_mark (pub->die, 1);
16469   for (i = 0; i < arange_table_in_use; i++)
16470     prune_unused_types_mark (arange_table[i], 1);
16471
16472   /* Get rid of nodes that aren't marked; and update the string counts.  */
16473   if (debug_str_hash)
16474     htab_empty (debug_str_hash);
16475   prune_unused_types_prune (comp_unit_die);
16476   for (node = limbo_die_list; node; node = node->next)
16477     prune_unused_types_prune (node->die);
16478
16479   /* Leave the marks clear.  */
16480   prune_unmark_dies (comp_unit_die);
16481   for (node = limbo_die_list; node; node = node->next)
16482     prune_unmark_dies (node->die);
16483 }
16484
16485 /* Set the parameter to true if there are any relative pathnames in
16486    the file table.  */
16487 static int
16488 file_table_relative_p (void ** slot, void *param)
16489 {
16490   bool *p = (bool *) param;
16491   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16492   if (!IS_ABSOLUTE_PATH (d->filename))
16493     {
16494       *p = true;
16495       return 0;
16496     }
16497   return 1;
16498 }
16499
16500 /* Output stuff that dwarf requires at the end of every file,
16501    and generate the DWARF-2 debugging info.  */
16502
16503 static void
16504 dwarf2out_finish (const char *filename)
16505 {
16506   limbo_die_node *node, *next_node;
16507   dw_die_ref die = 0;
16508   unsigned int i;
16509
16510   /* Add the name for the main input file now.  We delayed this from
16511      dwarf2out_init to avoid complications with PCH.  */
16512   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16513   if (!IS_ABSOLUTE_PATH (filename))
16514     add_comp_dir_attribute (comp_unit_die);
16515   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16516     {
16517       bool p = false;
16518       htab_traverse (file_table, file_table_relative_p, &p);
16519       if (p)
16520         add_comp_dir_attribute (comp_unit_die);
16521     }
16522
16523   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16524     {
16525       add_location_or_const_value_attribute (
16526         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16527         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16528         DW_AT_location);
16529     }
16530
16531   /* Traverse the limbo die list, and add parent/child links.  The only
16532      dies without parents that should be here are concrete instances of
16533      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16534      For concrete instances, we can get the parent die from the abstract
16535      instance.  */
16536   for (node = limbo_die_list; node; node = next_node)
16537     {
16538       next_node = node->next;
16539       die = node->die;
16540
16541       if (die->die_parent == NULL)
16542         {
16543           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16544
16545           if (origin)
16546             add_child_die (origin->die_parent, die);
16547           else if (die == comp_unit_die)
16548             ;
16549           else if (errorcount > 0 || sorrycount > 0)
16550             /* It's OK to be confused by errors in the input.  */
16551             add_child_die (comp_unit_die, die);
16552           else
16553             {
16554               /* In certain situations, the lexical block containing a
16555                  nested function can be optimized away, which results
16556                  in the nested function die being orphaned.  Likewise
16557                  with the return type of that nested function.  Force
16558                  this to be a child of the containing function.
16559
16560                  It may happen that even the containing function got fully
16561                  inlined and optimized out.  In that case we are lost and
16562                  assign the empty child.  This should not be big issue as
16563                  the function is likely unreachable too.  */
16564               tree context = NULL_TREE;
16565
16566               gcc_assert (node->created_for);
16567
16568               if (DECL_P (node->created_for))
16569                 context = DECL_CONTEXT (node->created_for);
16570               else if (TYPE_P (node->created_for))
16571                 context = TYPE_CONTEXT (node->created_for);
16572
16573               gcc_assert (context
16574                           && (TREE_CODE (context) == FUNCTION_DECL
16575                               || TREE_CODE (context) == NAMESPACE_DECL));
16576
16577               origin = lookup_decl_die (context);
16578               if (origin)
16579                 add_child_die (origin, die);
16580               else
16581                 add_child_die (comp_unit_die, die);
16582             }
16583         }
16584     }
16585
16586   limbo_die_list = NULL;
16587
16588   /* Walk through the list of incomplete types again, trying once more to
16589      emit full debugging info for them.  */
16590   retry_incomplete_types ();
16591
16592   if (flag_eliminate_unused_debug_types)
16593     prune_unused_types ();
16594
16595   /* Generate separate CUs for each of the include files we've seen.
16596      They will go into limbo_die_list.  */
16597   if (flag_eliminate_dwarf2_dups)
16598     break_out_includes (comp_unit_die);
16599
16600   /* Traverse the DIE's and add add sibling attributes to those DIE's
16601      that have children.  */
16602   add_sibling_attributes (comp_unit_die);
16603   for (node = limbo_die_list; node; node = node->next)
16604     add_sibling_attributes (node->die);
16605
16606   /* Output a terminator label for the .text section.  */
16607   switch_to_section (text_section);
16608   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16609   if (flag_reorder_blocks_and_partition)
16610     {
16611       switch_to_section (unlikely_text_section ());
16612       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16613     }
16614
16615   /* We can only use the low/high_pc attributes if all of the code was
16616      in .text.  */
16617   if (!have_multiple_function_sections)
16618     {
16619       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16620       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16621     }
16622
16623   else
16624     {
16625       unsigned fde_idx = 0;
16626
16627       /* We need to give .debug_loc and .debug_ranges an appropriate
16628          "base address".  Use zero so that these addresses become
16629          absolute.  Historically, we've emitted the unexpected
16630          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16631          Emit both to give time for other tools to adapt.  */
16632       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16633       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16634
16635       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16636                          add_ranges_by_labels (text_section_label,
16637                                                text_end_label));
16638       if (flag_reorder_blocks_and_partition)
16639         add_ranges_by_labels (cold_text_section_label,
16640                               cold_end_label);
16641
16642       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16643         {
16644           dw_fde_ref fde = &fde_table[fde_idx];
16645
16646           if (fde->dw_fde_switched_sections)
16647             {
16648               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16649                                     fde->dw_fde_hot_section_end_label);
16650               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16651                                     fde->dw_fde_unlikely_section_end_label);
16652             }
16653           else
16654             add_ranges_by_labels (fde->dw_fde_begin,
16655                                   fde->dw_fde_end);
16656         }
16657
16658       add_ranges (NULL);
16659     }
16660
16661   /* Output location list section if necessary.  */
16662   if (have_location_lists)
16663     {
16664       /* Output the location lists info.  */
16665       switch_to_section (debug_loc_section);
16666       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16667                                    DEBUG_LOC_SECTION_LABEL, 0);
16668       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16669       output_location_lists (die);
16670     }
16671
16672   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16673     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16674                     debug_line_section_label);
16675
16676   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16677     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16678
16679   /* Output all of the compilation units.  We put the main one last so that
16680      the offsets are available to output_pubnames.  */
16681   for (node = limbo_die_list; node; node = node->next)
16682     output_comp_unit (node->die, 0);
16683
16684   /* Output the main compilation unit if non-empty or if .debug_macinfo
16685      has been emitted.  */
16686   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16687
16688   /* Output the abbreviation table.  */
16689   switch_to_section (debug_abbrev_section);
16690   output_abbrev_section ();
16691
16692   /* Output public names table if necessary.  */
16693   if (!VEC_empty (pubname_entry, pubname_table))
16694     {
16695       switch_to_section (debug_pubnames_section);
16696       output_pubnames (pubname_table);
16697     }
16698
16699 #ifdef DEBUG_PUBTYPES_SECTION
16700   /* Output public types table if necessary.  */
16701   if (!VEC_empty (pubname_entry, pubtype_table))
16702     {
16703       switch_to_section (debug_pubtypes_section);
16704       output_pubnames (pubtype_table);
16705     }
16706 #endif
16707
16708   /* Output the address range information.  We only put functions in the arange
16709      table, so don't write it out if we don't have any.  */
16710   if (fde_table_in_use)
16711     {
16712       switch_to_section (debug_aranges_section);
16713       output_aranges ();
16714     }
16715
16716   /* Output ranges section if necessary.  */
16717   if (ranges_table_in_use)
16718     {
16719       switch_to_section (debug_ranges_section);
16720       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16721       output_ranges ();
16722     }
16723
16724   /* Output the source line correspondence table.  We must do this
16725      even if there is no line information.  Otherwise, on an empty
16726      translation unit, we will generate a present, but empty,
16727      .debug_info section.  IRIX 6.5 `nm' will then complain when
16728      examining the file.  This is done late so that any filenames
16729      used by the debug_info section are marked as 'used'.  */
16730   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16731     {
16732       switch_to_section (debug_line_section);
16733       output_line_info ();
16734     }
16735
16736   /* Have to end the macro section.  */
16737   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16738     {
16739       switch_to_section (debug_macinfo_section);
16740       dw2_asm_output_data (1, 0, "End compilation unit");
16741     }
16742
16743   /* If we emitted any DW_FORM_strp form attribute, output the string
16744      table too.  */
16745   if (debug_str_hash)
16746     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16747 }
16748 #else
16749
16750 /* This should never be used, but its address is needed for comparisons.  */
16751 const struct gcc_debug_hooks dwarf2_debug_hooks;
16752
16753 #endif /* DWARF2_DEBUGGING_INFO */
16754
16755 #include "gt-dwarf2out.h"