OSDN Git Service

f954d6e2ea29aa032216565241fc6671004b3f39
[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 "elf/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 *, int, bool);
95
96 static rtx last_var_location_insn;
97 #endif
98
99 #ifndef DWARF2_FRAME_INFO
100 # ifdef DWARF2_DEBUGGING_INFO
101 #  define DWARF2_FRAME_INFO \
102   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
103 # else
104 #  define DWARF2_FRAME_INFO 0
105 # endif
106 #endif
107
108 /* Map register numbers held in the call frame info that gcc has
109    collected using DWARF_FRAME_REGNUM to those that should be output in
110    .debug_frame and .eh_frame.  */
111 #ifndef DWARF2_FRAME_REG_OUT
112 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
113 #endif
114
115 /* Save the result of dwarf2out_do_frame across PCH.  */
116 static GTY(()) bool saved_do_cfi_asm = 0;
117
118 /* Decide whether we want to emit frame unwind information for the current
119    translation unit.  */
120
121 int
122 dwarf2out_do_frame (void)
123 {
124   /* We want to emit correct CFA location expressions or lists, so we
125      have to return true if we're going to output debug info, even if
126      we're not going to output frame or unwind info.  */
127   return (write_symbols == DWARF2_DEBUG
128           || write_symbols == VMS_AND_DWARF2_DEBUG
129           || DWARF2_FRAME_INFO || saved_do_cfi_asm
130 #ifdef DWARF2_UNWIND_INFO
131           || (DWARF2_UNWIND_INFO
132               && (flag_unwind_tables
133                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
134 #endif
135           );
136 }
137
138 /* Decide whether to emit frame unwind via assembler directives.  */
139
140 int
141 dwarf2out_do_cfi_asm (void)
142 {
143   int enc;
144
145 #ifdef MIPS_DEBUGGING_INFO
146   return false;
147 #endif
148   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
149     return false;
150   if (saved_do_cfi_asm || !eh_personality_libfunc)
151     return true;
152   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
153     return false;
154
155   /* Make sure the personality encoding is one the assembler can support.
156      In particular, aligned addresses can't be handled.  */
157   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
158   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
159     return false;
160   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
161   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
162     return false;
163
164   saved_do_cfi_asm = true;
165   return true;
166 }
167
168 /* The size of the target's pointer type.  */
169 #ifndef PTR_SIZE
170 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
171 #endif
172
173 /* Array of RTXes referenced by the debugging information, which therefore
174    must be kept around forever.  */
175 static GTY(()) VEC(rtx,gc) *used_rtx_array;
176
177 /* A pointer to the base of a list of incomplete types which might be
178    completed at some later time.  incomplete_types_list needs to be a
179    VEC(tree,gc) because we want to tell the garbage collector about
180    it.  */
181 static GTY(()) VEC(tree,gc) *incomplete_types;
182
183 /* A pointer to the base of a table of references to declaration
184    scopes.  This table is a display which tracks the nesting
185    of declaration scopes at the current scope and containing
186    scopes.  This table is used to find the proper place to
187    define type declaration DIE's.  */
188 static GTY(()) VEC(tree,gc) *decl_scope_table;
189
190 /* Pointers to various DWARF2 sections.  */
191 static GTY(()) section *debug_info_section;
192 static GTY(()) section *debug_abbrev_section;
193 static GTY(()) section *debug_aranges_section;
194 static GTY(()) section *debug_macinfo_section;
195 static GTY(()) section *debug_line_section;
196 static GTY(()) section *debug_loc_section;
197 static GTY(()) section *debug_pubnames_section;
198 static GTY(()) section *debug_pubtypes_section;
199 static GTY(()) section *debug_str_section;
200 static GTY(()) section *debug_ranges_section;
201 static GTY(()) section *debug_frame_section;
202
203 /* How to start an assembler comment.  */
204 #ifndef ASM_COMMENT_START
205 #define ASM_COMMENT_START ";#"
206 #endif
207
208 typedef struct dw_cfi_struct *dw_cfi_ref;
209 typedef struct dw_fde_struct *dw_fde_ref;
210 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
211
212 /* Call frames are described using a sequence of Call Frame
213    Information instructions.  The register number, offset
214    and address fields are provided as possible operands;
215    their use is selected by the opcode field.  */
216
217 enum dw_cfi_oprnd_type {
218   dw_cfi_oprnd_unused,
219   dw_cfi_oprnd_reg_num,
220   dw_cfi_oprnd_offset,
221   dw_cfi_oprnd_addr,
222   dw_cfi_oprnd_loc
223 };
224
225 typedef union GTY(()) dw_cfi_oprnd_struct {
226   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
227   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
228   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
229   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
230 }
231 dw_cfi_oprnd;
232
233 typedef struct GTY(()) dw_cfi_struct {
234   dw_cfi_ref dw_cfi_next;
235   enum dwarf_call_frame_info dw_cfi_opc;
236   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
237     dw_cfi_oprnd1;
238   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
239     dw_cfi_oprnd2;
240 }
241 dw_cfi_node;
242
243 /* This is how we define the location of the CFA. We use to handle it
244    as REG + OFFSET all the time,  but now it can be more complex.
245    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
246    Instead of passing around REG and OFFSET, we pass a copy
247    of this structure.  */
248 typedef struct GTY(()) cfa_loc {
249   HOST_WIDE_INT offset;
250   HOST_WIDE_INT base_offset;
251   unsigned int reg;
252   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
253   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
254 } dw_cfa_location;
255
256 /* All call frame descriptions (FDE's) in the GCC generated DWARF
257    refer to a single Common Information Entry (CIE), defined at
258    the beginning of the .debug_frame section.  This use of a single
259    CIE obviates the need to keep track of multiple CIE's
260    in the DWARF generation routines below.  */
261
262 typedef struct GTY(()) dw_fde_struct {
263   tree decl;
264   const char *dw_fde_begin;
265   const char *dw_fde_current_label;
266   const char *dw_fde_end;
267   const char *dw_fde_hot_section_label;
268   const char *dw_fde_hot_section_end_label;
269   const char *dw_fde_unlikely_section_label;
270   const char *dw_fde_unlikely_section_end_label;
271   bool dw_fde_switched_sections;
272   dw_cfi_ref dw_fde_cfi;
273   unsigned funcdef_number;
274   HOST_WIDE_INT stack_realignment;
275   /* Dynamic realign argument pointer register.  */
276   unsigned int drap_reg;
277   /* Virtual dynamic realign argument pointer register.  */
278   unsigned int vdrap_reg;
279   unsigned all_throwers_are_sibcalls : 1;
280   unsigned nothrow : 1;
281   unsigned uses_eh_lsda : 1;
282   /* Whether we did stack realign in this call frame.  */
283   unsigned stack_realign : 1;
284   /* Whether dynamic realign argument pointer register has been saved.  */
285   unsigned drap_reg_saved: 1;
286 }
287 dw_fde_node;
288
289 /* Maximum size (in bytes) of an artificially generated label.  */
290 #define MAX_ARTIFICIAL_LABEL_BYTES      30
291
292 /* The size of addresses as they appear in the Dwarf 2 data.
293    Some architectures use word addresses to refer to code locations,
294    but Dwarf 2 info always uses byte addresses.  On such machines,
295    Dwarf 2 addresses need to be larger than the architecture's
296    pointers.  */
297 #ifndef DWARF2_ADDR_SIZE
298 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
299 #endif
300
301 /* The size in bytes of a DWARF field indicating an offset or length
302    relative to a debug info section, specified to be 4 bytes in the
303    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
304    as PTR_SIZE.  */
305
306 #ifndef DWARF_OFFSET_SIZE
307 #define DWARF_OFFSET_SIZE 4
308 #endif
309
310 /* According to the (draft) DWARF 3 specification, the initial length
311    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
312    bytes are 0xffffffff, followed by the length stored in the next 8
313    bytes.
314
315    However, the SGI/MIPS ABI uses an initial length which is equal to
316    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
317
318 #ifndef DWARF_INITIAL_LENGTH_SIZE
319 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
320 #endif
321
322 /* Round SIZE up to the nearest BOUNDARY.  */
323 #define DWARF_ROUND(SIZE,BOUNDARY) \
324   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
325
326 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
327 #ifndef DWARF_CIE_DATA_ALIGNMENT
328 #ifdef STACK_GROWS_DOWNWARD
329 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
330 #else
331 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
332 #endif
333 #endif
334
335 /* CIE identifier.  */
336 #if HOST_BITS_PER_WIDE_INT >= 64
337 #define DWARF_CIE_ID \
338   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
339 #else
340 #define DWARF_CIE_ID DW_CIE_ID
341 #endif
342
343 /* A pointer to the base of a table that contains frame description
344    information for each routine.  */
345 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
346
347 /* Number of elements currently allocated for fde_table.  */
348 static GTY(()) unsigned fde_table_allocated;
349
350 /* Number of elements in fde_table currently in use.  */
351 static GTY(()) unsigned fde_table_in_use;
352
353 /* Size (in elements) of increments by which we may expand the
354    fde_table.  */
355 #define FDE_TABLE_INCREMENT 256
356
357 /* Get the current fde_table entry we should use.  */
358
359 static inline dw_fde_ref
360 current_fde (void)
361 {
362   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
363 }
364
365 /* A list of call frame insns for the CIE.  */
366 static GTY(()) dw_cfi_ref cie_cfi_head;
367
368 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
369 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
370    attribute that accelerates the lookup of the FDE associated
371    with the subprogram.  This variable holds the table index of the FDE
372    associated with the current function (body) definition.  */
373 static unsigned current_funcdef_fde;
374 #endif
375
376 struct GTY(()) indirect_string_node {
377   const char *str;
378   unsigned int refcount;
379   enum dwarf_form form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
415                                           HOST_WIDE_INT);
416 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
417 static void output_cfi_directive (dw_cfi_ref);
418 static void output_call_frame_info (int);
419 static void dwarf2out_note_section_used (void);
420 static void dwarf2out_stack_adjust (rtx, bool);
421 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
422 static void flush_queued_reg_saves (void);
423 static bool clobbers_queued_reg_save (const_rtx);
424 static void dwarf2out_frame_debug_expr (rtx, const char *);
425
426 /* Support for complex CFA locations.  */
427 static void output_cfa_loc (dw_cfi_ref);
428 static void output_cfa_loc_raw (dw_cfi_ref);
429 static void get_cfa_from_loc_descr (dw_cfa_location *,
430                                     struct dw_loc_descr_struct *);
431 static struct dw_loc_descr_struct *build_cfa_loc
432   (dw_cfa_location *, HOST_WIDE_INT);
433 static struct dw_loc_descr_struct *build_cfa_aligned_loc
434   (HOST_WIDE_INT, HOST_WIDE_INT);
435 static void def_cfa_1 (const char *, dw_cfa_location *);
436
437 /* How to start an assembler comment.  */
438 #ifndef ASM_COMMENT_START
439 #define ASM_COMMENT_START ";#"
440 #endif
441
442 /* Data and reference forms for relocatable data.  */
443 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
444 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
445
446 #ifndef DEBUG_FRAME_SECTION
447 #define DEBUG_FRAME_SECTION     ".debug_frame"
448 #endif
449
450 #ifndef FUNC_BEGIN_LABEL
451 #define FUNC_BEGIN_LABEL        "LFB"
452 #endif
453
454 #ifndef FUNC_END_LABEL
455 #define FUNC_END_LABEL          "LFE"
456 #endif
457
458 #ifndef FRAME_BEGIN_LABEL
459 #define FRAME_BEGIN_LABEL       "Lframe"
460 #endif
461 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
462 #define CIE_END_LABEL           "LECIE"
463 #define FDE_LABEL               "LSFDE"
464 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
465 #define FDE_END_LABEL           "LEFDE"
466 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
467 #define LINE_NUMBER_END_LABEL   "LELT"
468 #define LN_PROLOG_AS_LABEL      "LASLTP"
469 #define LN_PROLOG_END_LABEL     "LELTP"
470 #define DIE_LABEL_PREFIX        "DW"
471
472 /* The DWARF 2 CFA column which tracks the return address.  Normally this
473    is the column for PC, or the first column after all of the hard
474    registers.  */
475 #ifndef DWARF_FRAME_RETURN_COLUMN
476 #ifdef PC_REGNUM
477 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
478 #else
479 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
480 #endif
481 #endif
482
483 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
484    default, we just provide columns for all registers.  */
485 #ifndef DWARF_FRAME_REGNUM
486 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
487 #endif
488 \f
489 /* Hook used by __throw.  */
490
491 rtx
492 expand_builtin_dwarf_sp_column (void)
493 {
494   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
495   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
496 }
497
498 /* Return a pointer to a copy of the section string name S with all
499    attributes stripped off, and an asterisk prepended (for assemble_name).  */
500
501 static inline char *
502 stripattributes (const char *s)
503 {
504   char *stripped = XNEWVEC (char, strlen (s) + 2);
505   char *p = stripped;
506
507   *p++ = '*';
508
509   while (*s && *s != ',')
510     *p++ = *s++;
511
512   *p = '\0';
513   return stripped;
514 }
515
516 /* MEM is a memory reference for the register size table, each element of
517    which has mode MODE.  Initialize column C as a return address column.  */
518
519 static void
520 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
521 {
522   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
523   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
524   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525 }
526
527 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
528
529 static inline HOST_WIDE_INT
530 div_data_align (HOST_WIDE_INT off)
531 {
532   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
533   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
534   return r;
535 }
536
537 /* Return true if we need a signed version of a given opcode
538    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
539
540 static inline bool
541 need_data_align_sf_opcode (HOST_WIDE_INT off)
542 {
543   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
544 }
545
546 /* Generate code to initialize the register size table.  */
547
548 void
549 expand_builtin_init_dwarf_reg_sizes (tree address)
550 {
551   unsigned int i;
552   enum machine_mode mode = TYPE_MODE (char_type_node);
553   rtx addr = expand_normal (address);
554   rtx mem = gen_rtx_MEM (BLKmode, addr);
555   bool wrote_return_column = false;
556
557   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
558     {
559       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
560
561       if (rnum < DWARF_FRAME_REGISTERS)
562         {
563           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
564           enum machine_mode save_mode = reg_raw_mode[i];
565           HOST_WIDE_INT size;
566
567           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
568             save_mode = choose_hard_reg_mode (i, 1, true);
569           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
570             {
571               if (save_mode == VOIDmode)
572                 continue;
573               wrote_return_column = true;
574             }
575           size = GET_MODE_SIZE (save_mode);
576           if (offset < 0)
577             continue;
578
579           emit_move_insn (adjust_address (mem, mode, offset),
580                           gen_int_mode (size, mode));
581         }
582     }
583
584   if (!wrote_return_column)
585     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
586
587 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
588   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
589 #endif
590
591   targetm.init_dwarf_reg_sizes_extra (address);
592 }
593
594 /* Convert a DWARF call frame info. operation to its string name */
595
596 static const char *
597 dwarf_cfi_name (unsigned int cfi_opc)
598 {
599   switch (cfi_opc)
600     {
601     case DW_CFA_advance_loc:
602       return "DW_CFA_advance_loc";
603     case DW_CFA_offset:
604       return "DW_CFA_offset";
605     case DW_CFA_restore:
606       return "DW_CFA_restore";
607     case DW_CFA_nop:
608       return "DW_CFA_nop";
609     case DW_CFA_set_loc:
610       return "DW_CFA_set_loc";
611     case DW_CFA_advance_loc1:
612       return "DW_CFA_advance_loc1";
613     case DW_CFA_advance_loc2:
614       return "DW_CFA_advance_loc2";
615     case DW_CFA_advance_loc4:
616       return "DW_CFA_advance_loc4";
617     case DW_CFA_offset_extended:
618       return "DW_CFA_offset_extended";
619     case DW_CFA_restore_extended:
620       return "DW_CFA_restore_extended";
621     case DW_CFA_undefined:
622       return "DW_CFA_undefined";
623     case DW_CFA_same_value:
624       return "DW_CFA_same_value";
625     case DW_CFA_register:
626       return "DW_CFA_register";
627     case DW_CFA_remember_state:
628       return "DW_CFA_remember_state";
629     case DW_CFA_restore_state:
630       return "DW_CFA_restore_state";
631     case DW_CFA_def_cfa:
632       return "DW_CFA_def_cfa";
633     case DW_CFA_def_cfa_register:
634       return "DW_CFA_def_cfa_register";
635     case DW_CFA_def_cfa_offset:
636       return "DW_CFA_def_cfa_offset";
637
638     /* DWARF 3 */
639     case DW_CFA_def_cfa_expression:
640       return "DW_CFA_def_cfa_expression";
641     case DW_CFA_expression:
642       return "DW_CFA_expression";
643     case DW_CFA_offset_extended_sf:
644       return "DW_CFA_offset_extended_sf";
645     case DW_CFA_def_cfa_sf:
646       return "DW_CFA_def_cfa_sf";
647     case DW_CFA_def_cfa_offset_sf:
648       return "DW_CFA_def_cfa_offset_sf";
649
650     /* SGI/MIPS specific */
651     case DW_CFA_MIPS_advance_loc8:
652       return "DW_CFA_MIPS_advance_loc8";
653
654     /* GNU extensions */
655     case DW_CFA_GNU_window_save:
656       return "DW_CFA_GNU_window_save";
657     case DW_CFA_GNU_args_size:
658       return "DW_CFA_GNU_args_size";
659     case DW_CFA_GNU_negative_offset_extended:
660       return "DW_CFA_GNU_negative_offset_extended";
661
662     default:
663       return "DW_CFA_<unknown>";
664     }
665 }
666
667 /* Return a pointer to a newly allocated Call Frame Instruction.  */
668
669 static inline dw_cfi_ref
670 new_cfi (void)
671 {
672   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
673
674   cfi->dw_cfi_next = NULL;
675   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
676   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
677
678   return cfi;
679 }
680
681 /* Add a Call Frame Instruction to list of instructions.  */
682
683 static inline void
684 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
685 {
686   dw_cfi_ref *p;
687   dw_fde_ref fde = current_fde ();
688
689   /* When DRAP is used, CFA is defined with an expression.  Redefine
690      CFA may lead to a different CFA value.   */
691   /* ??? Of course, this heuristic fails when we're annotating epilogues,
692      because of course we'll always want to redefine the CFA back to the
693      stack pointer on the way out.  Where should we move this check?  */
694   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
695     switch (cfi->dw_cfi_opc)
696       {
697         case DW_CFA_def_cfa_register:
698         case DW_CFA_def_cfa_offset:
699         case DW_CFA_def_cfa_offset_sf:
700         case DW_CFA_def_cfa:
701         case DW_CFA_def_cfa_sf:
702           gcc_unreachable ();
703
704         default:
705           break;
706       }
707
708   /* Find the end of the chain.  */
709   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
710     ;
711
712   *p = cfi;
713 }
714
715 /* Generate a new label for the CFI info to refer to.  FORCE is true
716    if a label needs to be output even when using .cfi_* directives.  */
717
718 char *
719 dwarf2out_cfi_label (bool force)
720 {
721   static char label[20];
722
723   if (!force && dwarf2out_do_cfi_asm ())
724     {
725       /* In this case, we will be emitting the asm directive instead of
726          the label, so just return a placeholder to keep the rest of the
727          interfaces happy.  */
728       strcpy (label, "<do not output>");
729     }
730   else
731     {
732       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
733       ASM_OUTPUT_LABEL (asm_out_file, label);
734     }
735
736   return label;
737 }
738
739 /* True if remember_state should be emitted before following CFI directive.  */
740 static bool emit_cfa_remember;
741
742 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
743    or to the CIE if LABEL is NULL.  */
744
745 static void
746 add_fde_cfi (const char *label, dw_cfi_ref cfi)
747 {
748   dw_cfi_ref *list_head;
749
750   if (emit_cfa_remember)
751     {
752       dw_cfi_ref cfi_remember;
753
754       /* Emit the state save.  */
755       emit_cfa_remember = false;
756       cfi_remember = new_cfi (); 
757       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
758       add_fde_cfi (label, cfi_remember);
759     }
760
761   list_head = &cie_cfi_head;
762
763   if (dwarf2out_do_cfi_asm ())
764     {
765       if (label)
766         {
767           dw_fde_ref fde = current_fde ();
768
769           gcc_assert (fde != NULL);
770
771           /* We still have to add the cfi to the list so that lookup_cfa
772              works later on.  When -g2 and above we even need to force
773              emitting of CFI labels and add to list a DW_CFA_set_loc for
774              convert_cfa_to_fb_loc_list purposes.  If we're generating
775              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
776              convert_cfa_to_fb_loc_list.  */
777           if (dwarf_version == 2
778               && debug_info_level > DINFO_LEVEL_TERSE
779               && (write_symbols == DWARF2_DEBUG
780                   || write_symbols == VMS_AND_DWARF2_DEBUG))
781             {
782               switch (cfi->dw_cfi_opc)
783                 {
784                 case DW_CFA_def_cfa_offset:
785                 case DW_CFA_def_cfa_offset_sf:
786                 case DW_CFA_def_cfa_register:
787                 case DW_CFA_def_cfa:
788                 case DW_CFA_def_cfa_sf:
789                 case DW_CFA_def_cfa_expression:
790                 case DW_CFA_restore_state:
791                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
792                     label = dwarf2out_cfi_label (true);
793
794                   if (fde->dw_fde_current_label == NULL
795                       || strcmp (label, fde->dw_fde_current_label) != 0)
796                     {
797                       dw_cfi_ref xcfi;
798
799                       label = xstrdup (label);
800
801                       /* Set the location counter to the new label.  */
802                       xcfi = new_cfi ();
803                       /* It doesn't metter whether DW_CFA_set_loc
804                          or DW_CFA_advance_loc4 is added here, those aren't
805                          emitted into assembly, only looked up by
806                          convert_cfa_to_fb_loc_list.  */
807                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
808                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
809                       add_cfi (&fde->dw_fde_cfi, xcfi);
810                       fde->dw_fde_current_label = label;
811                     }
812                   break;
813                 default:
814                   break;
815                 }
816             }
817
818           output_cfi_directive (cfi);
819
820           list_head = &fde->dw_fde_cfi;
821         }
822       /* ??? If this is a CFI for the CIE, we don't emit.  This
823          assumes that the standard CIE contents that the assembler
824          uses matches the standard CIE contents that the compiler
825          uses.  This is probably a bad assumption.  I'm not quite
826          sure how to address this for now.  */
827     }
828   else if (label)
829     {
830       dw_fde_ref fde = current_fde ();
831
832       gcc_assert (fde != NULL);
833
834       if (*label == 0)
835         label = dwarf2out_cfi_label (false);
836
837       if (fde->dw_fde_current_label == NULL
838           || strcmp (label, fde->dw_fde_current_label) != 0)
839         {
840           dw_cfi_ref xcfi;
841
842           label = xstrdup (label);
843
844           /* Set the location counter to the new label.  */
845           xcfi = new_cfi ();
846           /* If we have a current label, advance from there, otherwise
847              set the location directly using set_loc.  */
848           xcfi->dw_cfi_opc = fde->dw_fde_current_label
849                              ? DW_CFA_advance_loc4
850                              : DW_CFA_set_loc;
851           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
852           add_cfi (&fde->dw_fde_cfi, xcfi);
853
854           fde->dw_fde_current_label = label;
855         }
856
857       list_head = &fde->dw_fde_cfi;
858     }
859
860   add_cfi (list_head, cfi);
861 }
862
863 /* Subroutine of lookup_cfa.  */
864
865 static void
866 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
867 {
868   switch (cfi->dw_cfi_opc)
869     {
870     case DW_CFA_def_cfa_offset:
871     case DW_CFA_def_cfa_offset_sf:
872       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
873       break;
874     case DW_CFA_def_cfa_register:
875       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
876       break;
877     case DW_CFA_def_cfa:
878     case DW_CFA_def_cfa_sf:
879       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
880       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
881       break;
882     case DW_CFA_def_cfa_expression:
883       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
884       break;
885
886     case DW_CFA_remember_state:
887       gcc_assert (!remember->in_use);
888       *remember = *loc;
889       remember->in_use = 1;
890       break;
891     case DW_CFA_restore_state:
892       gcc_assert (remember->in_use);
893       *loc = *remember;
894       remember->in_use = 0;
895       break;
896
897     default:
898       break;
899     }
900 }
901
902 /* Find the previous value for the CFA.  */
903
904 static void
905 lookup_cfa (dw_cfa_location *loc)
906 {
907   dw_cfi_ref cfi;
908   dw_fde_ref fde;
909   dw_cfa_location remember;
910
911   memset (loc, 0, sizeof (*loc));
912   loc->reg = INVALID_REGNUM;
913   remember = *loc;
914
915   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
916     lookup_cfa_1 (cfi, loc, &remember);
917
918   fde = current_fde ();
919   if (fde)
920     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
921       lookup_cfa_1 (cfi, loc, &remember);
922 }
923
924 /* The current rule for calculating the DWARF2 canonical frame address.  */
925 static dw_cfa_location cfa;
926
927 /* The register used for saving registers to the stack, and its offset
928    from the CFA.  */
929 static dw_cfa_location cfa_store;
930
931 /* The current save location around an epilogue.  */
932 static dw_cfa_location cfa_remember;
933
934 /* The running total of the size of arguments pushed onto the stack.  */
935 static HOST_WIDE_INT args_size;
936
937 /* The last args_size we actually output.  */
938 static HOST_WIDE_INT old_args_size;
939
940 /* Entry point to update the canonical frame address (CFA).
941    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
942    calculated from REG+OFFSET.  */
943
944 void
945 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
946 {
947   dw_cfa_location loc;
948   loc.indirect = 0;
949   loc.base_offset = 0;
950   loc.reg = reg;
951   loc.offset = offset;
952   def_cfa_1 (label, &loc);
953 }
954
955 /* Determine if two dw_cfa_location structures define the same data.  */
956
957 static bool
958 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
959 {
960   return (loc1->reg == loc2->reg
961           && loc1->offset == loc2->offset
962           && loc1->indirect == loc2->indirect
963           && (loc1->indirect == 0
964               || loc1->base_offset == loc2->base_offset));
965 }
966
967 /* This routine does the actual work.  The CFA is now calculated from
968    the dw_cfa_location structure.  */
969
970 static void
971 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
972 {
973   dw_cfi_ref cfi;
974   dw_cfa_location old_cfa, loc;
975
976   cfa = *loc_p;
977   loc = *loc_p;
978
979   if (cfa_store.reg == loc.reg && loc.indirect == 0)
980     cfa_store.offset = loc.offset;
981
982   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
983   lookup_cfa (&old_cfa);
984
985   /* If nothing changed, no need to issue any call frame instructions.  */
986   if (cfa_equal_p (&loc, &old_cfa))
987     return;
988
989   cfi = new_cfi ();
990
991   if (loc.reg == old_cfa.reg && !loc.indirect)
992     {
993       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
994          the CFA register did not change but the offset did.  The data 
995          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
996          in the assembler via the .cfi_def_cfa_offset directive.  */
997       if (need_data_align_sf_opcode (loc.offset))
998         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
999       else
1000         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1001       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1002     }
1003
1004 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1005   else if (loc.offset == old_cfa.offset
1006            && old_cfa.reg != INVALID_REGNUM
1007            && !loc.indirect)
1008     {
1009       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1010          indicating the CFA register has changed to <register> but the
1011          offset has not changed.  */
1012       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1013       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1014     }
1015 #endif
1016
1017   else if (loc.indirect == 0)
1018     {
1019       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1020          indicating the CFA register has changed to <register> with
1021          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1022          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1023          directive.  */
1024       if (need_data_align_sf_opcode (loc.offset))
1025         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1026       else
1027         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1028       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1029       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1030     }
1031   else
1032     {
1033       /* Construct a DW_CFA_def_cfa_expression instruction to
1034          calculate the CFA using a full location expression since no
1035          register-offset pair is available.  */
1036       struct dw_loc_descr_struct *loc_list;
1037
1038       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1039       loc_list = build_cfa_loc (&loc, 0);
1040       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1041     }
1042
1043   add_fde_cfi (label, cfi);
1044 }
1045
1046 /* Add the CFI for saving a register.  REG is the CFA column number.
1047    LABEL is passed to add_fde_cfi.
1048    If SREG is -1, the register is saved at OFFSET from the CFA;
1049    otherwise it is saved in SREG.  */
1050
1051 static void
1052 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1053 {
1054   dw_cfi_ref cfi = new_cfi ();
1055   dw_fde_ref fde = current_fde ();
1056
1057   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1058
1059   /* When stack is aligned, store REG using DW_CFA_expression with
1060      FP.  */
1061   if (fde
1062       && fde->stack_realign
1063       && sreg == INVALID_REGNUM)
1064     {
1065       cfi->dw_cfi_opc = DW_CFA_expression;
1066       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1067       cfi->dw_cfi_oprnd1.dw_cfi_loc
1068         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1069     }
1070   else if (sreg == INVALID_REGNUM)
1071     {
1072       if (need_data_align_sf_opcode (offset))
1073         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1074       else if (reg & ~0x3f)
1075         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1076       else
1077         cfi->dw_cfi_opc = DW_CFA_offset;
1078       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1079     }
1080   else if (sreg == reg)
1081     cfi->dw_cfi_opc = DW_CFA_same_value;
1082   else
1083     {
1084       cfi->dw_cfi_opc = DW_CFA_register;
1085       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1086     }
1087
1088   add_fde_cfi (label, cfi);
1089 }
1090
1091 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1092    This CFI tells the unwinder that it needs to restore the window registers
1093    from the previous frame's window save area.
1094
1095    ??? Perhaps we should note in the CIE where windows are saved (instead of
1096    assuming 0(cfa)) and what registers are in the window.  */
1097
1098 void
1099 dwarf2out_window_save (const char *label)
1100 {
1101   dw_cfi_ref cfi = new_cfi ();
1102
1103   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1104   add_fde_cfi (label, cfi);
1105 }
1106
1107 /* Add a CFI to update the running total of the size of arguments
1108    pushed onto the stack.  */
1109
1110 void
1111 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1112 {
1113   dw_cfi_ref cfi;
1114
1115   if (size == old_args_size)
1116     return;
1117
1118   old_args_size = size;
1119
1120   cfi = new_cfi ();
1121   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1122   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1123   add_fde_cfi (label, cfi);
1124 }
1125
1126 /* Entry point for saving a register to the stack.  REG is the GCC register
1127    number.  LABEL and OFFSET are passed to reg_save.  */
1128
1129 void
1130 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1131 {
1132   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1133 }
1134
1135 /* Entry point for saving the return address in the stack.
1136    LABEL and OFFSET are passed to reg_save.  */
1137
1138 void
1139 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1140 {
1141   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1142 }
1143
1144 /* Entry point for saving the return address in a register.
1145    LABEL and SREG are passed to reg_save.  */
1146
1147 void
1148 dwarf2out_return_reg (const char *label, unsigned int sreg)
1149 {
1150   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1151 }
1152
1153 #ifdef DWARF2_UNWIND_INFO
1154 /* Record the initial position of the return address.  RTL is
1155    INCOMING_RETURN_ADDR_RTX.  */
1156
1157 static void
1158 initial_return_save (rtx rtl)
1159 {
1160   unsigned int reg = INVALID_REGNUM;
1161   HOST_WIDE_INT offset = 0;
1162
1163   switch (GET_CODE (rtl))
1164     {
1165     case REG:
1166       /* RA is in a register.  */
1167       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1168       break;
1169
1170     case MEM:
1171       /* RA is on the stack.  */
1172       rtl = XEXP (rtl, 0);
1173       switch (GET_CODE (rtl))
1174         {
1175         case REG:
1176           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1177           offset = 0;
1178           break;
1179
1180         case PLUS:
1181           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1182           offset = INTVAL (XEXP (rtl, 1));
1183           break;
1184
1185         case MINUS:
1186           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1187           offset = -INTVAL (XEXP (rtl, 1));
1188           break;
1189
1190         default:
1191           gcc_unreachable ();
1192         }
1193
1194       break;
1195
1196     case PLUS:
1197       /* The return address is at some offset from any value we can
1198          actually load.  For instance, on the SPARC it is in %i7+8. Just
1199          ignore the offset for now; it doesn't matter for unwinding frames.  */
1200       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1201       initial_return_save (XEXP (rtl, 0));
1202       return;
1203
1204     default:
1205       gcc_unreachable ();
1206     }
1207
1208   if (reg != DWARF_FRAME_RETURN_COLUMN)
1209     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1210 }
1211 #endif
1212
1213 /* Given a SET, calculate the amount of stack adjustment it
1214    contains.  */
1215
1216 static HOST_WIDE_INT
1217 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1218                      HOST_WIDE_INT cur_offset)
1219 {
1220   const_rtx src = SET_SRC (pattern);
1221   const_rtx dest = SET_DEST (pattern);
1222   HOST_WIDE_INT offset = 0;
1223   enum rtx_code code;
1224
1225   if (dest == stack_pointer_rtx)
1226     {
1227       code = GET_CODE (src);
1228
1229       /* Assume (set (reg sp) (reg whatever)) sets args_size
1230          level to 0.  */
1231       if (code == REG && src != stack_pointer_rtx)
1232         {
1233           offset = -cur_args_size;
1234 #ifndef STACK_GROWS_DOWNWARD
1235           offset = -offset;
1236 #endif
1237           return offset - cur_offset;
1238         }
1239
1240       if (! (code == PLUS || code == MINUS)
1241           || XEXP (src, 0) != stack_pointer_rtx
1242           || !CONST_INT_P (XEXP (src, 1)))
1243         return 0;
1244
1245       /* (set (reg sp) (plus (reg sp) (const_int))) */
1246       offset = INTVAL (XEXP (src, 1));
1247       if (code == PLUS)
1248         offset = -offset;
1249       return offset;
1250     }
1251
1252   if (MEM_P (src) && !MEM_P (dest))
1253     dest = src;
1254   if (MEM_P (dest))
1255     {
1256       /* (set (mem (pre_dec (reg sp))) (foo)) */
1257       src = XEXP (dest, 0);
1258       code = GET_CODE (src);
1259
1260       switch (code)
1261         {
1262         case PRE_MODIFY:
1263         case POST_MODIFY:
1264           if (XEXP (src, 0) == stack_pointer_rtx)
1265             {
1266               rtx val = XEXP (XEXP (src, 1), 1);
1267               /* We handle only adjustments by constant amount.  */
1268               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1269                           && CONST_INT_P (val));
1270               offset = -INTVAL (val);
1271               break;
1272             }
1273           return 0;
1274
1275         case PRE_DEC:
1276         case POST_DEC:
1277           if (XEXP (src, 0) == stack_pointer_rtx)
1278             {
1279               offset = GET_MODE_SIZE (GET_MODE (dest));
1280               break;
1281             }
1282           return 0;
1283
1284         case PRE_INC:
1285         case POST_INC:
1286           if (XEXP (src, 0) == stack_pointer_rtx)
1287             {
1288               offset = -GET_MODE_SIZE (GET_MODE (dest));
1289               break;
1290             }
1291           return 0;
1292
1293         default:
1294           return 0;
1295         }
1296     }
1297   else
1298     return 0;
1299
1300   return offset;
1301 }
1302
1303 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1304    indexed by INSN_UID.  */
1305
1306 static HOST_WIDE_INT *barrier_args_size;
1307
1308 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1309
1310 static HOST_WIDE_INT
1311 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1312                              VEC (rtx, heap) **next)
1313 {
1314   HOST_WIDE_INT offset = 0;
1315   int i;
1316
1317   if (! RTX_FRAME_RELATED_P (insn))
1318     {
1319       if (prologue_epilogue_contains (insn))
1320         /* Nothing */;
1321       else if (GET_CODE (PATTERN (insn)) == SET)
1322         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1323       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1324                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1325         {
1326           /* There may be stack adjustments inside compound insns.  Search
1327              for them.  */
1328           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1329             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1330               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1331                                              cur_args_size, offset);
1332         }
1333     }
1334   else
1335     {
1336       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1337
1338       if (expr)
1339         {
1340           expr = XEXP (expr, 0);
1341           if (GET_CODE (expr) == PARALLEL
1342               || GET_CODE (expr) == SEQUENCE)
1343             for (i = 1; i < XVECLEN (expr, 0); i++)
1344               {
1345                 rtx elem = XVECEXP (expr, 0, i);
1346
1347                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1348                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1349               }
1350         }
1351     }
1352
1353 #ifndef STACK_GROWS_DOWNWARD
1354   offset = -offset;
1355 #endif
1356
1357   cur_args_size += offset;
1358   if (cur_args_size < 0)
1359     cur_args_size = 0;
1360
1361   if (JUMP_P (insn))
1362     {
1363       rtx dest = JUMP_LABEL (insn);
1364
1365       if (dest)
1366         {
1367           if (barrier_args_size [INSN_UID (dest)] < 0)
1368             {
1369               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1370               VEC_safe_push (rtx, heap, *next, dest);
1371             }
1372         }
1373     }
1374
1375   return cur_args_size;
1376 }
1377
1378 /* Walk the whole function and compute args_size on BARRIERs.  */
1379
1380 static void
1381 compute_barrier_args_size (void)
1382 {
1383   int max_uid = get_max_uid (), i;
1384   rtx insn;
1385   VEC (rtx, heap) *worklist, *next, *tmp;
1386
1387   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1388   for (i = 0; i < max_uid; i++)
1389     barrier_args_size[i] = -1;
1390
1391   worklist = VEC_alloc (rtx, heap, 20);
1392   next = VEC_alloc (rtx, heap, 20);
1393   insn = get_insns ();
1394   barrier_args_size[INSN_UID (insn)] = 0;
1395   VEC_quick_push (rtx, worklist, insn);
1396   for (;;)
1397     {
1398       while (!VEC_empty (rtx, worklist))
1399         {
1400           rtx prev, body, first_insn;
1401           HOST_WIDE_INT cur_args_size;
1402
1403           first_insn = insn = VEC_pop (rtx, worklist);
1404           cur_args_size = barrier_args_size[INSN_UID (insn)];
1405           prev = prev_nonnote_insn (insn);
1406           if (prev && BARRIER_P (prev))
1407             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1408
1409           for (; insn; insn = NEXT_INSN (insn))
1410             {
1411               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1412                 continue;
1413               if (BARRIER_P (insn))
1414                 break;
1415
1416               if (LABEL_P (insn))
1417                 {
1418                   if (insn == first_insn)
1419                     continue;
1420                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1421                     {
1422                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1423                       continue;
1424                     }
1425                   else
1426                     {
1427                       /* The insns starting with this label have been
1428                          already scanned or are in the worklist.  */
1429                       break;
1430                     }
1431                 }
1432
1433               body = PATTERN (insn);
1434               if (GET_CODE (body) == SEQUENCE)
1435                 {
1436                   HOST_WIDE_INT dest_args_size = cur_args_size;
1437                   for (i = 1; i < XVECLEN (body, 0); i++)
1438                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1439                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1440                       dest_args_size
1441                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1442                                                        dest_args_size, &next);
1443                     else
1444                       cur_args_size
1445                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1446                                                        cur_args_size, &next);
1447
1448                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1449                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1450                                                  dest_args_size, &next);
1451                   else
1452                     cur_args_size
1453                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1454                                                      cur_args_size, &next);
1455                 }
1456               else
1457                 cur_args_size
1458                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1459             }
1460         }
1461
1462       if (VEC_empty (rtx, next))
1463         break;
1464
1465       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1466       tmp = next;
1467       next = worklist;
1468       worklist = tmp;
1469       VEC_truncate (rtx, next, 0);
1470     }
1471
1472   VEC_free (rtx, heap, worklist);
1473   VEC_free (rtx, heap, next);
1474 }
1475
1476
1477 /* Check INSN to see if it looks like a push or a stack adjustment, and
1478    make a note of it if it does.  EH uses this information to find out how
1479    much extra space it needs to pop off the stack.  */
1480
1481 static void
1482 dwarf2out_stack_adjust (rtx insn, bool after_p)
1483 {
1484   HOST_WIDE_INT offset;
1485   const char *label;
1486   int i;
1487
1488   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1489      with this function.  Proper support would require all frame-related
1490      insns to be marked, and to be able to handle saving state around
1491      epilogues textually in the middle of the function.  */
1492   if (prologue_epilogue_contains (insn))
1493     return;
1494
1495   /* If INSN is an instruction from target of an annulled branch, the
1496      effects are for the target only and so current argument size
1497      shouldn't change at all.  */
1498   if (final_sequence
1499       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1500       && INSN_FROM_TARGET_P (insn))
1501     return;
1502
1503   /* If only calls can throw, and we have a frame pointer,
1504      save up adjustments until we see the CALL_INSN.  */
1505   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1506     {
1507       if (CALL_P (insn) && !after_p)
1508         {
1509           /* Extract the size of the args from the CALL rtx itself.  */
1510           insn = PATTERN (insn);
1511           if (GET_CODE (insn) == PARALLEL)
1512             insn = XVECEXP (insn, 0, 0);
1513           if (GET_CODE (insn) == SET)
1514             insn = SET_SRC (insn);
1515           gcc_assert (GET_CODE (insn) == CALL);
1516           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1517         }
1518       return;
1519     }
1520
1521   if (CALL_P (insn) && !after_p)
1522     {
1523       if (!flag_asynchronous_unwind_tables)
1524         dwarf2out_args_size ("", args_size);
1525       return;
1526     }
1527   else if (BARRIER_P (insn))
1528     {
1529       /* Don't call compute_barrier_args_size () if the only
1530          BARRIER is at the end of function.  */
1531       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1532         compute_barrier_args_size ();
1533       if (barrier_args_size == NULL)
1534         offset = 0;
1535       else
1536         {
1537           offset = barrier_args_size[INSN_UID (insn)];
1538           if (offset < 0)
1539             offset = 0;
1540         }
1541
1542       offset -= args_size;
1543 #ifndef STACK_GROWS_DOWNWARD
1544       offset = -offset;
1545 #endif
1546     }
1547   else if (GET_CODE (PATTERN (insn)) == SET)
1548     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1549   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1550            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1551     {
1552       /* There may be stack adjustments inside compound insns.  Search
1553          for them.  */
1554       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1555         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1556           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1557                                          args_size, offset);
1558     }
1559   else
1560     return;
1561
1562   if (offset == 0)
1563     return;
1564
1565   label = dwarf2out_cfi_label (false);
1566   dwarf2out_args_size_adjust (offset, label);
1567 }
1568
1569 /* Adjust args_size based on stack adjustment OFFSET.  */
1570
1571 static void
1572 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1573 {
1574   if (cfa.reg == STACK_POINTER_REGNUM)
1575     cfa.offset += offset;
1576
1577   if (cfa_store.reg == STACK_POINTER_REGNUM)
1578     cfa_store.offset += offset;
1579
1580 #ifndef STACK_GROWS_DOWNWARD
1581   offset = -offset;
1582 #endif
1583
1584   args_size += offset;
1585   if (args_size < 0)
1586     args_size = 0;
1587
1588   def_cfa_1 (label, &cfa);
1589   if (flag_asynchronous_unwind_tables)
1590     dwarf2out_args_size (label, args_size);
1591 }
1592
1593 #endif
1594
1595 /* We delay emitting a register save until either (a) we reach the end
1596    of the prologue or (b) the register is clobbered.  This clusters
1597    register saves so that there are fewer pc advances.  */
1598
1599 struct GTY(()) queued_reg_save {
1600   struct queued_reg_save *next;
1601   rtx reg;
1602   HOST_WIDE_INT cfa_offset;
1603   rtx saved_reg;
1604 };
1605
1606 static GTY(()) struct queued_reg_save *queued_reg_saves;
1607
1608 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1609 struct GTY(()) reg_saved_in_data {
1610   rtx orig_reg;
1611   rtx saved_in_reg;
1612 };
1613
1614 /* A list of registers saved in other registers.
1615    The list intentionally has a small maximum capacity of 4; if your
1616    port needs more than that, you might consider implementing a
1617    more efficient data structure.  */
1618 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1619 static GTY(()) size_t num_regs_saved_in_regs;
1620
1621 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1622 static const char *last_reg_save_label;
1623
1624 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1625    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1626
1627 static void
1628 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1629 {
1630   struct queued_reg_save *q;
1631
1632   /* Duplicates waste space, but it's also necessary to remove them
1633      for correctness, since the queue gets output in reverse
1634      order.  */
1635   for (q = queued_reg_saves; q != NULL; q = q->next)
1636     if (REGNO (q->reg) == REGNO (reg))
1637       break;
1638
1639   if (q == NULL)
1640     {
1641       q = GGC_NEW (struct queued_reg_save);
1642       q->next = queued_reg_saves;
1643       queued_reg_saves = q;
1644     }
1645
1646   q->reg = reg;
1647   q->cfa_offset = offset;
1648   q->saved_reg = sreg;
1649
1650   last_reg_save_label = label;
1651 }
1652
1653 /* Output all the entries in QUEUED_REG_SAVES.  */
1654
1655 static void
1656 flush_queued_reg_saves (void)
1657 {
1658   struct queued_reg_save *q;
1659
1660   for (q = queued_reg_saves; q; q = q->next)
1661     {
1662       size_t i;
1663       unsigned int reg, sreg;
1664
1665       for (i = 0; i < num_regs_saved_in_regs; i++)
1666         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1667           break;
1668       if (q->saved_reg && i == num_regs_saved_in_regs)
1669         {
1670           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1671           num_regs_saved_in_regs++;
1672         }
1673       if (i != num_regs_saved_in_regs)
1674         {
1675           regs_saved_in_regs[i].orig_reg = q->reg;
1676           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1677         }
1678
1679       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1680       if (q->saved_reg)
1681         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1682       else
1683         sreg = INVALID_REGNUM;
1684       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1685     }
1686
1687   queued_reg_saves = NULL;
1688   last_reg_save_label = NULL;
1689 }
1690
1691 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1692    location for?  Or, does it clobber a register which we've previously
1693    said that some other register is saved in, and for which we now
1694    have a new location for?  */
1695
1696 static bool
1697 clobbers_queued_reg_save (const_rtx insn)
1698 {
1699   struct queued_reg_save *q;
1700
1701   for (q = queued_reg_saves; q; q = q->next)
1702     {
1703       size_t i;
1704       if (modified_in_p (q->reg, insn))
1705         return true;
1706       for (i = 0; i < num_regs_saved_in_regs; i++)
1707         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1708             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1709           return true;
1710     }
1711
1712   return false;
1713 }
1714
1715 /* Entry point for saving the first register into the second.  */
1716
1717 void
1718 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1719 {
1720   size_t i;
1721   unsigned int regno, sregno;
1722
1723   for (i = 0; i < num_regs_saved_in_regs; i++)
1724     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1725       break;
1726   if (i == num_regs_saved_in_regs)
1727     {
1728       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1729       num_regs_saved_in_regs++;
1730     }
1731   regs_saved_in_regs[i].orig_reg = reg;
1732   regs_saved_in_regs[i].saved_in_reg = sreg;
1733
1734   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1735   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1736   reg_save (label, regno, sregno, 0);
1737 }
1738
1739 /* What register, if any, is currently saved in REG?  */
1740
1741 static rtx
1742 reg_saved_in (rtx reg)
1743 {
1744   unsigned int regn = REGNO (reg);
1745   size_t i;
1746   struct queued_reg_save *q;
1747
1748   for (q = queued_reg_saves; q; q = q->next)
1749     if (q->saved_reg && regn == REGNO (q->saved_reg))
1750       return q->reg;
1751
1752   for (i = 0; i < num_regs_saved_in_regs; i++)
1753     if (regs_saved_in_regs[i].saved_in_reg
1754         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1755       return regs_saved_in_regs[i].orig_reg;
1756
1757   return NULL_RTX;
1758 }
1759
1760
1761 /* A temporary register holding an integral value used in adjusting SP
1762    or setting up the store_reg.  The "offset" field holds the integer
1763    value, not an offset.  */
1764 static dw_cfa_location cfa_temp;
1765
1766 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1767
1768 static void
1769 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1770 {
1771   memset (&cfa, 0, sizeof (cfa));
1772
1773   switch (GET_CODE (pat))
1774     {
1775     case PLUS:
1776       cfa.reg = REGNO (XEXP (pat, 0));
1777       cfa.offset = INTVAL (XEXP (pat, 1));
1778       break;
1779
1780     case REG:
1781       cfa.reg = REGNO (pat);
1782       break;
1783
1784     default:
1785       /* Recurse and define an expression.  */
1786       gcc_unreachable ();
1787     }
1788
1789   def_cfa_1 (label, &cfa);
1790 }
1791
1792 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1793
1794 static void
1795 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1796 {
1797   rtx src, dest;
1798
1799   gcc_assert (GET_CODE (pat) == SET);
1800   dest = XEXP (pat, 0);
1801   src = XEXP (pat, 1);
1802
1803   switch (GET_CODE (src))
1804     {
1805     case PLUS:
1806       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1807       cfa.offset -= INTVAL (XEXP (src, 1));
1808       break;
1809
1810     case REG:
1811         break;
1812
1813     default:
1814         gcc_unreachable ();
1815     }
1816
1817   cfa.reg = REGNO (dest);
1818   gcc_assert (cfa.indirect == 0);
1819
1820   def_cfa_1 (label, &cfa);
1821 }
1822
1823 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1824
1825 static void
1826 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1827 {
1828   HOST_WIDE_INT offset;
1829   rtx src, addr, span;
1830
1831   src = XEXP (set, 1);
1832   addr = XEXP (set, 0);
1833   gcc_assert (MEM_P (addr));
1834   addr = XEXP (addr, 0);
1835   
1836   /* As documented, only consider extremely simple addresses.  */
1837   switch (GET_CODE (addr))
1838     {
1839     case REG:
1840       gcc_assert (REGNO (addr) == cfa.reg);
1841       offset = -cfa.offset;
1842       break;
1843     case PLUS:
1844       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1845       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1846       break;
1847     default:
1848       gcc_unreachable ();
1849     }
1850
1851   span = targetm.dwarf_register_span (src);
1852
1853   /* ??? We'd like to use queue_reg_save, but we need to come up with
1854      a different flushing heuristic for epilogues.  */
1855   if (!span)
1856     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1857   else
1858     {
1859       /* We have a PARALLEL describing where the contents of SRC live.
1860          Queue register saves for each piece of the PARALLEL.  */
1861       int par_index;
1862       int limit;
1863       HOST_WIDE_INT span_offset = offset;
1864
1865       gcc_assert (GET_CODE (span) == PARALLEL);
1866
1867       limit = XVECLEN (span, 0);
1868       for (par_index = 0; par_index < limit; par_index++)
1869         {
1870           rtx elem = XVECEXP (span, 0, par_index);
1871
1872           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1873                     INVALID_REGNUM, span_offset);
1874           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1875         }
1876     }
1877 }
1878
1879 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1880
1881 static void
1882 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1883 {
1884   rtx src, dest;
1885   unsigned sregno, dregno;
1886
1887   src = XEXP (set, 1);
1888   dest = XEXP (set, 0);
1889
1890   if (src == pc_rtx)
1891     sregno = DWARF_FRAME_RETURN_COLUMN;
1892   else
1893     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1894
1895   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1896
1897   /* ??? We'd like to use queue_reg_save, but we need to come up with
1898      a different flushing heuristic for epilogues.  */
1899   reg_save (label, sregno, dregno, 0);
1900 }
1901
1902 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1903
1904 static void
1905 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1906 {
1907   dw_cfi_ref cfi = new_cfi ();
1908   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1909
1910   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1911   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1912
1913   add_fde_cfi (label, cfi);
1914 }
1915
1916 /* Record call frame debugging information for an expression EXPR,
1917    which either sets SP or FP (adjusting how we calculate the frame
1918    address) or saves a register to the stack or another register.
1919    LABEL indicates the address of EXPR.
1920
1921    This function encodes a state machine mapping rtxes to actions on
1922    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1923    users need not read the source code.
1924
1925   The High-Level Picture
1926
1927   Changes in the register we use to calculate the CFA: Currently we
1928   assume that if you copy the CFA register into another register, we
1929   should take the other one as the new CFA register; this seems to
1930   work pretty well.  If it's wrong for some target, it's simple
1931   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1932
1933   Changes in the register we use for saving registers to the stack:
1934   This is usually SP, but not always.  Again, we deduce that if you
1935   copy SP into another register (and SP is not the CFA register),
1936   then the new register is the one we will be using for register
1937   saves.  This also seems to work.
1938
1939   Register saves: There's not much guesswork about this one; if
1940   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1941   register save, and the register used to calculate the destination
1942   had better be the one we think we're using for this purpose.
1943   It's also assumed that a copy from a call-saved register to another
1944   register is saving that register if RTX_FRAME_RELATED_P is set on
1945   that instruction.  If the copy is from a call-saved register to
1946   the *same* register, that means that the register is now the same
1947   value as in the caller.
1948
1949   Except: If the register being saved is the CFA register, and the
1950   offset is nonzero, we are saving the CFA, so we assume we have to
1951   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1952   the intent is to save the value of SP from the previous frame.
1953
1954   In addition, if a register has previously been saved to a different
1955   register,
1956
1957   Invariants / Summaries of Rules
1958
1959   cfa          current rule for calculating the CFA.  It usually
1960                consists of a register and an offset.
1961   cfa_store    register used by prologue code to save things to the stack
1962                cfa_store.offset is the offset from the value of
1963                cfa_store.reg to the actual CFA
1964   cfa_temp     register holding an integral value.  cfa_temp.offset
1965                stores the value, which will be used to adjust the
1966                stack pointer.  cfa_temp is also used like cfa_store,
1967                to track stores to the stack via fp or a temp reg.
1968
1969   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1970                with cfa.reg as the first operand changes the cfa.reg and its
1971                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1972                cfa_temp.offset.
1973
1974   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1975                expression yielding a constant.  This sets cfa_temp.reg
1976                and cfa_temp.offset.
1977
1978   Rule 5:      Create a new register cfa_store used to save items to the
1979                stack.
1980
1981   Rules 10-14: Save a register to the stack.  Define offset as the
1982                difference of the original location and cfa_store's
1983                location (or cfa_temp's location if cfa_temp is used).
1984
1985   Rules 16-20: If AND operation happens on sp in prologue, we assume
1986                stack is realigned.  We will use a group of DW_OP_XXX
1987                expressions to represent the location of the stored
1988                register instead of CFA+offset.
1989
1990   The Rules
1991
1992   "{a,b}" indicates a choice of a xor b.
1993   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1994
1995   Rule 1:
1996   (set <reg1> <reg2>:cfa.reg)
1997   effects: cfa.reg = <reg1>
1998            cfa.offset unchanged
1999            cfa_temp.reg = <reg1>
2000            cfa_temp.offset = cfa.offset
2001
2002   Rule 2:
2003   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2004                               {<const_int>,<reg>:cfa_temp.reg}))
2005   effects: cfa.reg = sp if fp used
2006            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2007            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2008              if cfa_store.reg==sp
2009
2010   Rule 3:
2011   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2012   effects: cfa.reg = fp
2013            cfa_offset += +/- <const_int>
2014
2015   Rule 4:
2016   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2017   constraints: <reg1> != fp
2018                <reg1> != sp
2019   effects: cfa.reg = <reg1>
2020            cfa_temp.reg = <reg1>
2021            cfa_temp.offset = cfa.offset
2022
2023   Rule 5:
2024   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2025   constraints: <reg1> != fp
2026                <reg1> != sp
2027   effects: cfa_store.reg = <reg1>
2028            cfa_store.offset = cfa.offset - cfa_temp.offset
2029
2030   Rule 6:
2031   (set <reg> <const_int>)
2032   effects: cfa_temp.reg = <reg>
2033            cfa_temp.offset = <const_int>
2034
2035   Rule 7:
2036   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2037   effects: cfa_temp.reg = <reg1>
2038            cfa_temp.offset |= <const_int>
2039
2040   Rule 8:
2041   (set <reg> (high <exp>))
2042   effects: none
2043
2044   Rule 9:
2045   (set <reg> (lo_sum <exp> <const_int>))
2046   effects: cfa_temp.reg = <reg>
2047            cfa_temp.offset = <const_int>
2048
2049   Rule 10:
2050   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2051   effects: cfa_store.offset -= <const_int>
2052            cfa.offset = cfa_store.offset if cfa.reg == sp
2053            cfa.reg = sp
2054            cfa.base_offset = -cfa_store.offset
2055
2056   Rule 11:
2057   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2058   effects: cfa_store.offset += -/+ mode_size(mem)
2059            cfa.offset = cfa_store.offset if cfa.reg == sp
2060            cfa.reg = sp
2061            cfa.base_offset = -cfa_store.offset
2062
2063   Rule 12:
2064   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2065
2066        <reg2>)
2067   effects: cfa.reg = <reg1>
2068            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2069
2070   Rule 13:
2071   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2072   effects: cfa.reg = <reg1>
2073            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2074
2075   Rule 14:
2076   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2077   effects: cfa.reg = <reg1>
2078            cfa.base_offset = -cfa_temp.offset
2079            cfa_temp.offset -= mode_size(mem)
2080
2081   Rule 15:
2082   (set <reg> {unspec, unspec_volatile})
2083   effects: target-dependent
2084
2085   Rule 16:
2086   (set sp (and: sp <const_int>))
2087   constraints: cfa_store.reg == sp
2088   effects: current_fde.stack_realign = 1
2089            cfa_store.offset = 0
2090            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2091
2092   Rule 17:
2093   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2094   effects: cfa_store.offset += -/+ mode_size(mem)
2095
2096   Rule 18:
2097   (set (mem ({pre_inc, pre_dec} sp)) fp)
2098   constraints: fde->stack_realign == 1
2099   effects: cfa_store.offset = 0
2100            cfa.reg != HARD_FRAME_POINTER_REGNUM
2101
2102   Rule 19:
2103   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2104   constraints: fde->stack_realign == 1
2105                && cfa.offset == 0
2106                && cfa.indirect == 0
2107                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2108   effects: Use DW_CFA_def_cfa_expression to define cfa
2109            cfa.reg == fde->drap_reg
2110
2111   Rule 20:
2112   (set reg fde->drap_reg)
2113   constraints: fde->vdrap_reg == INVALID_REGNUM
2114   effects: fde->vdrap_reg = reg.
2115   (set mem fde->drap_reg)
2116   constraints: fde->drap_reg_saved == 1
2117   effects: none.  */
2118
2119 static void
2120 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2121 {
2122   rtx src, dest, span;
2123   HOST_WIDE_INT offset;
2124   dw_fde_ref fde;
2125
2126   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2127      the PARALLEL independently. The first element is always processed if
2128      it is a SET. This is for backward compatibility.   Other elements
2129      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2130      flag is set in them.  */
2131   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2132     {
2133       int par_index;
2134       int limit = XVECLEN (expr, 0);
2135       rtx elem;
2136
2137       /* PARALLELs have strict read-modify-write semantics, so we
2138          ought to evaluate every rvalue before changing any lvalue.
2139          It's cumbersome to do that in general, but there's an
2140          easy approximation that is enough for all current users:
2141          handle register saves before register assignments.  */
2142       if (GET_CODE (expr) == PARALLEL)
2143         for (par_index = 0; par_index < limit; par_index++)
2144           {
2145             elem = XVECEXP (expr, 0, par_index);
2146             if (GET_CODE (elem) == SET
2147                 && MEM_P (SET_DEST (elem))
2148                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2149               dwarf2out_frame_debug_expr (elem, label);
2150           }
2151
2152       for (par_index = 0; par_index < limit; par_index++)
2153         {
2154           elem = XVECEXP (expr, 0, par_index);
2155           if (GET_CODE (elem) == SET
2156               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2157               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2158             dwarf2out_frame_debug_expr (elem, label);
2159           else if (GET_CODE (elem) == SET
2160                    && par_index != 0
2161                    && !RTX_FRAME_RELATED_P (elem))
2162             {
2163               /* Stack adjustment combining might combine some post-prologue
2164                  stack adjustment into a prologue stack adjustment.  */
2165               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2166
2167               if (offset != 0)
2168                 dwarf2out_args_size_adjust (offset, label);
2169             }
2170         }
2171       return;
2172     }
2173
2174   gcc_assert (GET_CODE (expr) == SET);
2175
2176   src = SET_SRC (expr);
2177   dest = SET_DEST (expr);
2178
2179   if (REG_P (src))
2180     {
2181       rtx rsi = reg_saved_in (src);
2182       if (rsi)
2183         src = rsi;
2184     }
2185
2186   fde = current_fde ();
2187
2188   if (REG_P (src)
2189       && fde
2190       && fde->drap_reg == REGNO (src)
2191       && (fde->drap_reg_saved
2192           || REG_P (dest)))
2193     {
2194       /* Rule 20 */
2195       /* If we are saving dynamic realign argument pointer to a
2196          register, the destination is virtual dynamic realign
2197          argument pointer.  It may be used to access argument.  */
2198       if (REG_P (dest))
2199         {
2200           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2201           fde->vdrap_reg = REGNO (dest);
2202         }
2203       return;
2204     }
2205
2206   switch (GET_CODE (dest))
2207     {
2208     case REG:
2209       switch (GET_CODE (src))
2210         {
2211           /* Setting FP from SP.  */
2212         case REG:
2213           if (cfa.reg == (unsigned) REGNO (src))
2214             {
2215               /* Rule 1 */
2216               /* Update the CFA rule wrt SP or FP.  Make sure src is
2217                  relative to the current CFA register.
2218
2219                  We used to require that dest be either SP or FP, but the
2220                  ARM copies SP to a temporary register, and from there to
2221                  FP.  So we just rely on the backends to only set
2222                  RTX_FRAME_RELATED_P on appropriate insns.  */
2223               cfa.reg = REGNO (dest);
2224               cfa_temp.reg = cfa.reg;
2225               cfa_temp.offset = cfa.offset;
2226             }
2227           else
2228             {
2229               /* Saving a register in a register.  */
2230               gcc_assert (!fixed_regs [REGNO (dest)]
2231                           /* For the SPARC and its register window.  */
2232                           || (DWARF_FRAME_REGNUM (REGNO (src))
2233                               == DWARF_FRAME_RETURN_COLUMN));
2234
2235               /* After stack is aligned, we can only save SP in FP
2236                  if drap register is used.  In this case, we have
2237                  to restore stack pointer with the CFA value and we
2238                  don't generate this DWARF information.  */
2239               if (fde
2240                   && fde->stack_realign
2241                   && REGNO (src) == STACK_POINTER_REGNUM)
2242                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2243                             && fde->drap_reg != INVALID_REGNUM
2244                             && cfa.reg != REGNO (src));
2245               else
2246                 queue_reg_save (label, src, dest, 0);
2247             }
2248           break;
2249
2250         case PLUS:
2251         case MINUS:
2252         case LO_SUM:
2253           if (dest == stack_pointer_rtx)
2254             {
2255               /* Rule 2 */
2256               /* Adjusting SP.  */
2257               switch (GET_CODE (XEXP (src, 1)))
2258                 {
2259                 case CONST_INT:
2260                   offset = INTVAL (XEXP (src, 1));
2261                   break;
2262                 case REG:
2263                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2264                               == cfa_temp.reg);
2265                   offset = cfa_temp.offset;
2266                   break;
2267                 default:
2268                   gcc_unreachable ();
2269                 }
2270
2271               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2272                 {
2273                   /* Restoring SP from FP in the epilogue.  */
2274                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2275                   cfa.reg = STACK_POINTER_REGNUM;
2276                 }
2277               else if (GET_CODE (src) == LO_SUM)
2278                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2279                 ;
2280               else
2281                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2282
2283               if (GET_CODE (src) != MINUS)
2284                 offset = -offset;
2285               if (cfa.reg == STACK_POINTER_REGNUM)
2286                 cfa.offset += offset;
2287               if (cfa_store.reg == STACK_POINTER_REGNUM)
2288                 cfa_store.offset += offset;
2289             }
2290           else if (dest == hard_frame_pointer_rtx)
2291             {
2292               /* Rule 3 */
2293               /* Either setting the FP from an offset of the SP,
2294                  or adjusting the FP */
2295               gcc_assert (frame_pointer_needed);
2296
2297               gcc_assert (REG_P (XEXP (src, 0))
2298                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2299                           && CONST_INT_P (XEXP (src, 1)));
2300               offset = INTVAL (XEXP (src, 1));
2301               if (GET_CODE (src) != MINUS)
2302                 offset = -offset;
2303               cfa.offset += offset;
2304               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2305             }
2306           else
2307             {
2308               gcc_assert (GET_CODE (src) != MINUS);
2309
2310               /* Rule 4 */
2311               if (REG_P (XEXP (src, 0))
2312                   && REGNO (XEXP (src, 0)) == cfa.reg
2313                   && CONST_INT_P (XEXP (src, 1)))
2314                 {
2315                   /* Setting a temporary CFA register that will be copied
2316                      into the FP later on.  */
2317                   offset = - INTVAL (XEXP (src, 1));
2318                   cfa.offset += offset;
2319                   cfa.reg = REGNO (dest);
2320                   /* Or used to save regs to the stack.  */
2321                   cfa_temp.reg = cfa.reg;
2322                   cfa_temp.offset = cfa.offset;
2323                 }
2324
2325               /* Rule 5 */
2326               else if (REG_P (XEXP (src, 0))
2327                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2328                        && XEXP (src, 1) == stack_pointer_rtx)
2329                 {
2330                   /* Setting a scratch register that we will use instead
2331                      of SP for saving registers to the stack.  */
2332                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2333                   cfa_store.reg = REGNO (dest);
2334                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2335                 }
2336
2337               /* Rule 9 */
2338               else if (GET_CODE (src) == LO_SUM
2339                        && CONST_INT_P (XEXP (src, 1)))
2340                 {
2341                   cfa_temp.reg = REGNO (dest);
2342                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2343                 }
2344               else
2345                 gcc_unreachable ();
2346             }
2347           break;
2348
2349           /* Rule 6 */
2350         case CONST_INT:
2351           cfa_temp.reg = REGNO (dest);
2352           cfa_temp.offset = INTVAL (src);
2353           break;
2354
2355           /* Rule 7 */
2356         case IOR:
2357           gcc_assert (REG_P (XEXP (src, 0))
2358                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2359                       && CONST_INT_P (XEXP (src, 1)));
2360
2361           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2362             cfa_temp.reg = REGNO (dest);
2363           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2364           break;
2365
2366           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2367              which will fill in all of the bits.  */
2368           /* Rule 8 */
2369         case HIGH:
2370           break;
2371
2372           /* Rule 15 */
2373         case UNSPEC:
2374         case UNSPEC_VOLATILE:
2375           gcc_assert (targetm.dwarf_handle_frame_unspec);
2376           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2377           return;
2378
2379           /* Rule 16 */
2380         case AND:
2381           /* If this AND operation happens on stack pointer in prologue,
2382              we assume the stack is realigned and we extract the
2383              alignment.  */
2384           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2385             {
2386               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2387               fde->stack_realign = 1;
2388               fde->stack_realignment = INTVAL (XEXP (src, 1));
2389               cfa_store.offset = 0;
2390
2391               if (cfa.reg != STACK_POINTER_REGNUM
2392                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2393                 fde->drap_reg = cfa.reg;
2394             }
2395           return;
2396
2397         default:
2398           gcc_unreachable ();
2399         }
2400
2401       def_cfa_1 (label, &cfa);
2402       break;
2403
2404     case MEM:
2405
2406       /* Saving a register to the stack.  Make sure dest is relative to the
2407          CFA register.  */
2408       switch (GET_CODE (XEXP (dest, 0)))
2409         {
2410           /* Rule 10 */
2411           /* With a push.  */
2412         case PRE_MODIFY:
2413           /* We can't handle variable size modifications.  */
2414           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2415                       == CONST_INT);
2416           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2417
2418           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2419                       && cfa_store.reg == STACK_POINTER_REGNUM);
2420
2421           cfa_store.offset += offset;
2422           if (cfa.reg == STACK_POINTER_REGNUM)
2423             cfa.offset = cfa_store.offset;
2424
2425           offset = -cfa_store.offset;
2426           break;
2427
2428           /* Rule 11 */
2429         case PRE_INC:
2430         case PRE_DEC:
2431           offset = GET_MODE_SIZE (GET_MODE (dest));
2432           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2433             offset = -offset;
2434
2435           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2436                        == STACK_POINTER_REGNUM)
2437                       && cfa_store.reg == STACK_POINTER_REGNUM);
2438
2439           cfa_store.offset += offset;
2440
2441           /* Rule 18: If stack is aligned, we will use FP as a
2442              reference to represent the address of the stored
2443              regiser.  */
2444           if (fde
2445               && fde->stack_realign
2446               && src == hard_frame_pointer_rtx)
2447             {
2448               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2449               cfa_store.offset = 0;
2450             }
2451
2452           if (cfa.reg == STACK_POINTER_REGNUM)
2453             cfa.offset = cfa_store.offset;
2454
2455           offset = -cfa_store.offset;
2456           break;
2457
2458           /* Rule 12 */
2459           /* With an offset.  */
2460         case PLUS:
2461         case MINUS:
2462         case LO_SUM:
2463           {
2464             int regno;
2465
2466             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2467                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2468             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2469             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2470               offset = -offset;
2471
2472             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2473
2474             if (cfa_store.reg == (unsigned) regno)
2475               offset -= cfa_store.offset;
2476             else
2477               {
2478                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2479                 offset -= cfa_temp.offset;
2480               }
2481           }
2482           break;
2483
2484           /* Rule 13 */
2485           /* Without an offset.  */
2486         case REG:
2487           {
2488             int regno = REGNO (XEXP (dest, 0));
2489
2490             if (cfa_store.reg == (unsigned) regno)
2491               offset = -cfa_store.offset;
2492             else
2493               {
2494                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2495                 offset = -cfa_temp.offset;
2496               }
2497           }
2498           break;
2499
2500           /* Rule 14 */
2501         case POST_INC:
2502           gcc_assert (cfa_temp.reg
2503                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2504           offset = -cfa_temp.offset;
2505           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2506           break;
2507
2508         default:
2509           gcc_unreachable ();
2510         }
2511
2512         /* Rule 17 */
2513         /* If the source operand of this MEM operation is not a
2514            register, basically the source is return address.  Here
2515            we only care how much stack grew and we don't save it.  */
2516       if (!REG_P (src))
2517         break;
2518
2519       if (REGNO (src) != STACK_POINTER_REGNUM
2520           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2521           && (unsigned) REGNO (src) == cfa.reg)
2522         {
2523           /* We're storing the current CFA reg into the stack.  */
2524
2525           if (cfa.offset == 0)
2526             {
2527               /* Rule 19 */
2528               /* If stack is aligned, putting CFA reg into stack means
2529                  we can no longer use reg + offset to represent CFA.
2530                  Here we use DW_CFA_def_cfa_expression instead.  The
2531                  result of this expression equals to the original CFA
2532                  value.  */
2533               if (fde
2534                   && fde->stack_realign
2535                   && cfa.indirect == 0
2536                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2537                 {
2538                   dw_cfa_location cfa_exp;
2539
2540                   gcc_assert (fde->drap_reg == cfa.reg);
2541
2542                   cfa_exp.indirect = 1;
2543                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2544                   cfa_exp.base_offset = offset;
2545                   cfa_exp.offset = 0;
2546
2547                   fde->drap_reg_saved = 1;
2548
2549                   def_cfa_1 (label, &cfa_exp);
2550                   break;
2551                 }
2552
2553               /* If the source register is exactly the CFA, assume
2554                  we're saving SP like any other register; this happens
2555                  on the ARM.  */
2556               def_cfa_1 (label, &cfa);
2557               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2558               break;
2559             }
2560           else
2561             {
2562               /* Otherwise, we'll need to look in the stack to
2563                  calculate the CFA.  */
2564               rtx x = XEXP (dest, 0);
2565
2566               if (!REG_P (x))
2567                 x = XEXP (x, 0);
2568               gcc_assert (REG_P (x));
2569
2570               cfa.reg = REGNO (x);
2571               cfa.base_offset = offset;
2572               cfa.indirect = 1;
2573               def_cfa_1 (label, &cfa);
2574               break;
2575             }
2576         }
2577
2578       def_cfa_1 (label, &cfa);
2579       {
2580         span = targetm.dwarf_register_span (src);
2581
2582         if (!span)
2583           queue_reg_save (label, src, NULL_RTX, offset);
2584         else
2585           {
2586             /* We have a PARALLEL describing where the contents of SRC
2587                live.  Queue register saves for each piece of the
2588                PARALLEL.  */
2589             int par_index;
2590             int limit;
2591             HOST_WIDE_INT span_offset = offset;
2592
2593             gcc_assert (GET_CODE (span) == PARALLEL);
2594
2595             limit = XVECLEN (span, 0);
2596             for (par_index = 0; par_index < limit; par_index++)
2597               {
2598                 rtx elem = XVECEXP (span, 0, par_index);
2599
2600                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2601                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2602               }
2603           }
2604       }
2605       break;
2606
2607     default:
2608       gcc_unreachable ();
2609     }
2610 }
2611
2612 /* Record call frame debugging information for INSN, which either
2613    sets SP or FP (adjusting how we calculate the frame address) or saves a
2614    register to the stack.  If INSN is NULL_RTX, initialize our state.
2615
2616    If AFTER_P is false, we're being called before the insn is emitted,
2617    otherwise after.  Call instructions get invoked twice.  */
2618
2619 void
2620 dwarf2out_frame_debug (rtx insn, bool after_p)
2621 {
2622   const char *label;
2623   rtx note, n;
2624   bool handled_one = false;
2625
2626   if (insn == NULL_RTX)
2627     {
2628       size_t i;
2629
2630       /* Flush any queued register saves.  */
2631       flush_queued_reg_saves ();
2632
2633       /* Set up state for generating call frame debug info.  */
2634       lookup_cfa (&cfa);
2635       gcc_assert (cfa.reg
2636                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2637
2638       cfa.reg = STACK_POINTER_REGNUM;
2639       cfa_store = cfa;
2640       cfa_temp.reg = -1;
2641       cfa_temp.offset = 0;
2642
2643       for (i = 0; i < num_regs_saved_in_regs; i++)
2644         {
2645           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2646           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2647         }
2648       num_regs_saved_in_regs = 0;
2649
2650       if (barrier_args_size)
2651         {
2652           XDELETEVEC (barrier_args_size);
2653           barrier_args_size = NULL;
2654         }
2655       return;
2656     }
2657
2658   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2659     flush_queued_reg_saves ();
2660
2661   if (! RTX_FRAME_RELATED_P (insn))
2662     {
2663       if (!ACCUMULATE_OUTGOING_ARGS)
2664         dwarf2out_stack_adjust (insn, after_p);
2665       return;
2666     }
2667
2668   label = dwarf2out_cfi_label (false);
2669
2670   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2671     switch (REG_NOTE_KIND (note))
2672       {
2673       case REG_FRAME_RELATED_EXPR:
2674         insn = XEXP (note, 0);
2675         goto found;
2676
2677       case REG_CFA_DEF_CFA:
2678         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2679         handled_one = true;
2680         break;
2681
2682       case REG_CFA_ADJUST_CFA:
2683         n = XEXP (note, 0);
2684         if (n == NULL)
2685           {
2686             n = PATTERN (insn);
2687             if (GET_CODE (n) == PARALLEL)
2688               n = XVECEXP (n, 0, 0);
2689           }
2690         dwarf2out_frame_debug_adjust_cfa (n, label);
2691         handled_one = true;
2692         break;
2693
2694       case REG_CFA_OFFSET:
2695         n = XEXP (note, 0);
2696         if (n == NULL)
2697           n = single_set (insn);
2698         dwarf2out_frame_debug_cfa_offset (n, label);
2699         handled_one = true;
2700         break;
2701
2702       case REG_CFA_REGISTER:
2703         n = XEXP (note, 0);
2704         if (n == NULL)
2705           {
2706             n = PATTERN (insn);
2707             if (GET_CODE (n) == PARALLEL)
2708               n = XVECEXP (n, 0, 0);
2709           }
2710         dwarf2out_frame_debug_cfa_register (n, label);
2711         handled_one = true;
2712         break;
2713
2714       case REG_CFA_RESTORE:
2715         n = XEXP (note, 0);
2716         if (n == NULL)
2717           {
2718             n = PATTERN (insn);
2719             if (GET_CODE (n) == PARALLEL)
2720               n = XVECEXP (n, 0, 0);
2721             n = XEXP (n, 0);
2722           }
2723         dwarf2out_frame_debug_cfa_restore (n, label);
2724         handled_one = true;
2725         break;
2726
2727       default:
2728         break;
2729       }
2730   if (handled_one)
2731     return;
2732
2733   insn = PATTERN (insn);
2734  found:
2735   dwarf2out_frame_debug_expr (insn, label);
2736 }
2737
2738 /* Determine if we need to save and restore CFI information around this
2739    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2740    we do need to save/restore, then emit the save now, and insert a
2741    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2742
2743 void
2744 dwarf2out_begin_epilogue (rtx insn)
2745 {
2746   bool saw_frp = false;
2747   rtx i;
2748
2749   /* Scan forward to the return insn, noticing if there are possible
2750      frame related insns.  */
2751   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2752     {
2753       if (!INSN_P (i))
2754         continue;
2755
2756       /* Look for both regular and sibcalls to end the block.  */
2757       if (returnjump_p (i))
2758         break;
2759       if (CALL_P (i) && SIBLING_CALL_P (i))
2760         break;
2761
2762       if (RTX_FRAME_RELATED_P (i))
2763         saw_frp = true;
2764     }
2765
2766   /* If the port doesn't emit epilogue unwind info, we don't need a
2767      save/restore pair.  */
2768   if (!saw_frp)
2769     return;
2770
2771   /* Otherwise, search forward to see if the return insn was the last
2772      basic block of the function.  If so, we don't need save/restore.  */
2773   gcc_assert (i != NULL);
2774   i = next_real_insn (i);
2775   if (i == NULL)
2776     return;
2777
2778   /* Insert the restore before that next real insn in the stream, and before
2779      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2780      properly nested.  This should be after any label or alignment.  This
2781      will be pushed into the CFI stream by the function below.  */
2782   while (1)
2783     {
2784       rtx p = PREV_INSN (i);
2785       if (!NOTE_P (p))
2786         break;
2787       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2788         break;
2789       i = p;
2790     }
2791   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2792
2793   emit_cfa_remember = true;
2794
2795   /* And emulate the state save.  */
2796   gcc_assert (!cfa_remember.in_use);
2797   cfa_remember = cfa;
2798   cfa_remember.in_use = 1;
2799 }
2800
2801 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2802
2803 void
2804 dwarf2out_frame_debug_restore_state (void)
2805 {
2806   dw_cfi_ref cfi = new_cfi (); 
2807   const char *label = dwarf2out_cfi_label (false);
2808
2809   cfi->dw_cfi_opc = DW_CFA_restore_state;
2810   add_fde_cfi (label, cfi);
2811
2812   gcc_assert (cfa_remember.in_use);
2813   cfa = cfa_remember;
2814   cfa_remember.in_use = 0;
2815 }
2816
2817 #endif
2818
2819 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2820 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2821  (enum dwarf_call_frame_info cfi);
2822
2823 static enum dw_cfi_oprnd_type
2824 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2825 {
2826   switch (cfi)
2827     {
2828     case DW_CFA_nop:
2829     case DW_CFA_GNU_window_save:
2830     case DW_CFA_remember_state:
2831     case DW_CFA_restore_state:
2832       return dw_cfi_oprnd_unused;
2833
2834     case DW_CFA_set_loc:
2835     case DW_CFA_advance_loc1:
2836     case DW_CFA_advance_loc2:
2837     case DW_CFA_advance_loc4:
2838     case DW_CFA_MIPS_advance_loc8:
2839       return dw_cfi_oprnd_addr;
2840
2841     case DW_CFA_offset:
2842     case DW_CFA_offset_extended:
2843     case DW_CFA_def_cfa:
2844     case DW_CFA_offset_extended_sf:
2845     case DW_CFA_def_cfa_sf:
2846     case DW_CFA_restore:
2847     case DW_CFA_restore_extended:
2848     case DW_CFA_undefined:
2849     case DW_CFA_same_value:
2850     case DW_CFA_def_cfa_register:
2851     case DW_CFA_register:
2852       return dw_cfi_oprnd_reg_num;
2853
2854     case DW_CFA_def_cfa_offset:
2855     case DW_CFA_GNU_args_size:
2856     case DW_CFA_def_cfa_offset_sf:
2857       return dw_cfi_oprnd_offset;
2858
2859     case DW_CFA_def_cfa_expression:
2860     case DW_CFA_expression:
2861       return dw_cfi_oprnd_loc;
2862
2863     default:
2864       gcc_unreachable ();
2865     }
2866 }
2867
2868 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2869 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2870  (enum dwarf_call_frame_info cfi);
2871
2872 static enum dw_cfi_oprnd_type
2873 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2874 {
2875   switch (cfi)
2876     {
2877     case DW_CFA_def_cfa:
2878     case DW_CFA_def_cfa_sf:
2879     case DW_CFA_offset:
2880     case DW_CFA_offset_extended_sf:
2881     case DW_CFA_offset_extended:
2882       return dw_cfi_oprnd_offset;
2883
2884     case DW_CFA_register:
2885       return dw_cfi_oprnd_reg_num;
2886
2887     default:
2888       return dw_cfi_oprnd_unused;
2889     }
2890 }
2891
2892 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2893
2894 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2895    switch to the data section instead, and write out a synthetic label
2896    for collect2.  */
2897
2898 static void
2899 switch_to_eh_frame_section (void)
2900 {
2901   tree label;
2902
2903 #ifdef EH_FRAME_SECTION_NAME
2904   if (eh_frame_section == 0)
2905     {
2906       int flags;
2907
2908       if (EH_TABLES_CAN_BE_READ_ONLY)
2909         {
2910           int fde_encoding;
2911           int per_encoding;
2912           int lsda_encoding;
2913
2914           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2915                                                        /*global=*/0);
2916           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2917                                                        /*global=*/1);
2918           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2919                                                         /*global=*/0);
2920           flags = ((! flag_pic
2921                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2922                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2923                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2924                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2925                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2926                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2927                    ? 0 : SECTION_WRITE);
2928         }
2929       else
2930         flags = SECTION_WRITE;
2931       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2932     }
2933 #endif
2934
2935   if (eh_frame_section)
2936     switch_to_section (eh_frame_section);
2937   else
2938     {
2939       /* We have no special eh_frame section.  Put the information in
2940          the data section and emit special labels to guide collect2.  */
2941       switch_to_section (data_section);
2942       label = get_file_function_name ("F");
2943       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2944       targetm.asm_out.globalize_label (asm_out_file,
2945                                        IDENTIFIER_POINTER (label));
2946       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2947     }
2948 }
2949
2950 /* Output a Call Frame Information opcode and its operand(s).  */
2951
2952 static void
2953 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2954 {
2955   unsigned long r;
2956   HOST_WIDE_INT off;
2957
2958   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2959     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2960                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2961                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2962                          ((unsigned HOST_WIDE_INT)
2963                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2964   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2965     {
2966       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2967       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2968                            "DW_CFA_offset, column 0x%lx", r);
2969       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2970       dw2_asm_output_data_uleb128 (off, NULL);
2971     }
2972   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2973     {
2974       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2975       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2976                            "DW_CFA_restore, column 0x%lx", r);
2977     }
2978   else
2979     {
2980       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2981                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2982
2983       switch (cfi->dw_cfi_opc)
2984         {
2985         case DW_CFA_set_loc:
2986           if (for_eh)
2987             dw2_asm_output_encoded_addr_rtx (
2988                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2989                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2990                 false, NULL);
2991           else
2992             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2993                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2994           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2995           break;
2996
2997         case DW_CFA_advance_loc1:
2998           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2999                                 fde->dw_fde_current_label, NULL);
3000           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3001           break;
3002
3003         case DW_CFA_advance_loc2:
3004           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3005                                 fde->dw_fde_current_label, NULL);
3006           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3007           break;
3008
3009         case DW_CFA_advance_loc4:
3010           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3011                                 fde->dw_fde_current_label, NULL);
3012           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3013           break;
3014
3015         case DW_CFA_MIPS_advance_loc8:
3016           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3017                                 fde->dw_fde_current_label, NULL);
3018           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3019           break;
3020
3021         case DW_CFA_offset_extended:
3022           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3023           dw2_asm_output_data_uleb128 (r, NULL);
3024           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3025           dw2_asm_output_data_uleb128 (off, NULL);
3026           break;
3027
3028         case DW_CFA_def_cfa:
3029           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3030           dw2_asm_output_data_uleb128 (r, NULL);
3031           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3032           break;
3033
3034         case DW_CFA_offset_extended_sf:
3035           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3036           dw2_asm_output_data_uleb128 (r, NULL);
3037           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3038           dw2_asm_output_data_sleb128 (off, NULL);
3039           break;
3040
3041         case DW_CFA_def_cfa_sf:
3042           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3043           dw2_asm_output_data_uleb128 (r, NULL);
3044           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3045           dw2_asm_output_data_sleb128 (off, NULL);
3046           break;
3047
3048         case DW_CFA_restore_extended:
3049         case DW_CFA_undefined:
3050         case DW_CFA_same_value:
3051         case DW_CFA_def_cfa_register:
3052           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3053           dw2_asm_output_data_uleb128 (r, NULL);
3054           break;
3055
3056         case DW_CFA_register:
3057           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3058           dw2_asm_output_data_uleb128 (r, NULL);
3059           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3060           dw2_asm_output_data_uleb128 (r, NULL);
3061           break;
3062
3063         case DW_CFA_def_cfa_offset:
3064         case DW_CFA_GNU_args_size:
3065           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3066           break;
3067
3068         case DW_CFA_def_cfa_offset_sf:
3069           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3070           dw2_asm_output_data_sleb128 (off, NULL);
3071           break;
3072
3073         case DW_CFA_GNU_window_save:
3074           break;
3075
3076         case DW_CFA_def_cfa_expression:
3077         case DW_CFA_expression:
3078           output_cfa_loc (cfi);
3079           break;
3080
3081         case DW_CFA_GNU_negative_offset_extended:
3082           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3083           gcc_unreachable ();
3084
3085         default:
3086           break;
3087         }
3088     }
3089 }
3090
3091 /* Similar, but do it via assembler directives instead.  */
3092
3093 static void
3094 output_cfi_directive (dw_cfi_ref cfi)
3095 {
3096   unsigned long r, r2;
3097
3098   switch (cfi->dw_cfi_opc)
3099     {
3100     case DW_CFA_advance_loc:
3101     case DW_CFA_advance_loc1:
3102     case DW_CFA_advance_loc2:
3103     case DW_CFA_advance_loc4:
3104     case DW_CFA_MIPS_advance_loc8:
3105     case DW_CFA_set_loc:
3106       /* Should only be created by add_fde_cfi in a code path not
3107          followed when emitting via directives.  The assembler is
3108          going to take care of this for us.  */
3109       gcc_unreachable ();
3110
3111     case DW_CFA_offset:
3112     case DW_CFA_offset_extended:
3113     case DW_CFA_offset_extended_sf:
3114       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3115       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3116                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3117       break;
3118
3119     case DW_CFA_restore:
3120     case DW_CFA_restore_extended:
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3122       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3123       break;
3124
3125     case DW_CFA_undefined:
3126       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3127       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3128       break;
3129
3130     case DW_CFA_same_value:
3131       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3132       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3133       break;
3134
3135     case DW_CFA_def_cfa:
3136     case DW_CFA_def_cfa_sf:
3137       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3138       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3139                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3140       break;
3141
3142     case DW_CFA_def_cfa_register:
3143       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3144       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3145       break;
3146
3147     case DW_CFA_register:
3148       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3149       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3150       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3151       break;
3152
3153     case DW_CFA_def_cfa_offset:
3154     case DW_CFA_def_cfa_offset_sf:
3155       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3156                HOST_WIDE_INT_PRINT_DEC"\n",
3157                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3158       break;
3159
3160     case DW_CFA_remember_state:
3161       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3162       break;
3163     case DW_CFA_restore_state:
3164       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3165       break;
3166
3167     case DW_CFA_GNU_args_size:
3168       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3169       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3170       if (flag_debug_asm)
3171         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3172                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3173       fputc ('\n', asm_out_file);
3174       break;
3175
3176     case DW_CFA_GNU_window_save:
3177       fprintf (asm_out_file, "\t.cfi_window_save\n");
3178       break;
3179
3180     case DW_CFA_def_cfa_expression:
3181     case DW_CFA_expression:
3182       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3183       output_cfa_loc_raw (cfi);
3184       fputc ('\n', asm_out_file);
3185       break;
3186
3187     default:
3188       gcc_unreachable ();
3189     }
3190 }
3191
3192 /* Output the call frame information used to record information
3193    that relates to calculating the frame pointer, and records the
3194    location of saved registers.  */
3195
3196 static void
3197 output_call_frame_info (int for_eh)
3198 {
3199   unsigned int i;
3200   dw_fde_ref fde;
3201   dw_cfi_ref cfi;
3202   char l1[20], l2[20], section_start_label[20];
3203   bool any_lsda_needed = false;
3204   char augmentation[6];
3205   int augmentation_size;
3206   int fde_encoding = DW_EH_PE_absptr;
3207   int per_encoding = DW_EH_PE_absptr;
3208   int lsda_encoding = DW_EH_PE_absptr;
3209   int return_reg;
3210   int dw_cie_version;
3211
3212   /* Don't emit a CIE if there won't be any FDEs.  */
3213   if (fde_table_in_use == 0)
3214     return;
3215
3216   /* Nothing to do if the assembler's doing it all.  */
3217   if (dwarf2out_do_cfi_asm ())
3218     return;
3219
3220   /* If we make FDEs linkonce, we may have to emit an empty label for
3221      an FDE that wouldn't otherwise be emitted.  We want to avoid
3222      having an FDE kept around when the function it refers to is
3223      discarded.  Example where this matters: a primary function
3224      template in C++ requires EH information, but an explicit
3225      specialization doesn't.  */
3226   if (TARGET_USES_WEAK_UNWIND_INFO
3227       && ! flag_asynchronous_unwind_tables
3228       && flag_exceptions
3229       && for_eh)
3230     for (i = 0; i < fde_table_in_use; i++)
3231       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3232           && !fde_table[i].uses_eh_lsda
3233           && ! DECL_WEAK (fde_table[i].decl))
3234         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3235                                       for_eh, /* empty */ 1);
3236
3237   /* If we don't have any functions we'll want to unwind out of, don't
3238      emit any EH unwind information.  Note that if exceptions aren't
3239      enabled, we won't have collected nothrow information, and if we
3240      asked for asynchronous tables, we always want this info.  */
3241   if (for_eh)
3242     {
3243       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3244
3245       for (i = 0; i < fde_table_in_use; i++)
3246         if (fde_table[i].uses_eh_lsda)
3247           any_eh_needed = any_lsda_needed = true;
3248         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3249           any_eh_needed = true;
3250         else if (! fde_table[i].nothrow
3251                  && ! fde_table[i].all_throwers_are_sibcalls)
3252           any_eh_needed = true;
3253
3254       if (! any_eh_needed)
3255         return;
3256     }
3257
3258   /* We're going to be generating comments, so turn on app.  */
3259   if (flag_debug_asm)
3260     app_enable ();
3261
3262   if (for_eh)
3263     switch_to_eh_frame_section ();
3264   else
3265     {
3266       if (!debug_frame_section)
3267         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3268                                            SECTION_DEBUG, NULL);
3269       switch_to_section (debug_frame_section);
3270     }
3271
3272   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3273   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3274
3275   /* Output the CIE.  */
3276   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3277   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3278   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3279     dw2_asm_output_data (4, 0xffffffff,
3280       "Initial length escape value indicating 64-bit DWARF extension");
3281   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3282                         "Length of Common Information Entry");
3283   ASM_OUTPUT_LABEL (asm_out_file, l1);
3284
3285   /* Now that the CIE pointer is PC-relative for EH,
3286      use 0 to identify the CIE.  */
3287   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3288                        (for_eh ? 0 : DWARF_CIE_ID),
3289                        "CIE Identifier Tag");
3290
3291   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3292      use CIE version 1, unless that would produce incorrect results
3293      due to overflowing the return register column.  */
3294   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3295   dw_cie_version = 1;
3296   if (return_reg >= 256 || dwarf_version > 2)
3297     dw_cie_version = 3;
3298   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3299
3300   augmentation[0] = 0;
3301   augmentation_size = 0;
3302   if (for_eh)
3303     {
3304       char *p;
3305
3306       /* Augmentation:
3307          z      Indicates that a uleb128 is present to size the
3308                 augmentation section.
3309          L      Indicates the encoding (and thus presence) of
3310                 an LSDA pointer in the FDE augmentation.
3311          R      Indicates a non-default pointer encoding for
3312                 FDE code pointers.
3313          P      Indicates the presence of an encoding + language
3314                 personality routine in the CIE augmentation.  */
3315
3316       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3317       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3318       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3319
3320       p = augmentation + 1;
3321       if (eh_personality_libfunc)
3322         {
3323           *p++ = 'P';
3324           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3325           assemble_external_libcall (eh_personality_libfunc);
3326         }
3327       if (any_lsda_needed)
3328         {
3329           *p++ = 'L';
3330           augmentation_size += 1;
3331         }
3332       if (fde_encoding != DW_EH_PE_absptr)
3333         {
3334           *p++ = 'R';
3335           augmentation_size += 1;
3336         }
3337       if (p > augmentation + 1)
3338         {
3339           augmentation[0] = 'z';
3340           *p = '\0';
3341         }
3342
3343       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3344       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3345         {
3346           int offset = (  4             /* Length */
3347                         + 4             /* CIE Id */
3348                         + 1             /* CIE version */
3349                         + strlen (augmentation) + 1     /* Augmentation */
3350                         + size_of_uleb128 (1)           /* Code alignment */
3351                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3352                         + 1             /* RA column */
3353                         + 1             /* Augmentation size */
3354                         + 1             /* Personality encoding */ );
3355           int pad = -offset & (PTR_SIZE - 1);
3356
3357           augmentation_size += pad;
3358
3359           /* Augmentations should be small, so there's scarce need to
3360              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3361           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3362         }
3363     }
3364
3365   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3366   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3367   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3368                                "CIE Data Alignment Factor");
3369
3370   if (dw_cie_version == 1)
3371     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3372   else
3373     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3374
3375   if (augmentation[0])
3376     {
3377       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3378       if (eh_personality_libfunc)
3379         {
3380           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3381                                eh_data_format_name (per_encoding));
3382           dw2_asm_output_encoded_addr_rtx (per_encoding,
3383                                            eh_personality_libfunc,
3384                                            true, NULL);
3385         }
3386
3387       if (any_lsda_needed)
3388         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3389                              eh_data_format_name (lsda_encoding));
3390
3391       if (fde_encoding != DW_EH_PE_absptr)
3392         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3393                              eh_data_format_name (fde_encoding));
3394     }
3395
3396   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3397     output_cfi (cfi, NULL, for_eh);
3398
3399   /* Pad the CIE out to an address sized boundary.  */
3400   ASM_OUTPUT_ALIGN (asm_out_file,
3401                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3402   ASM_OUTPUT_LABEL (asm_out_file, l2);
3403
3404   /* Loop through all of the FDE's.  */
3405   for (i = 0; i < fde_table_in_use; i++)
3406     {
3407       fde = &fde_table[i];
3408
3409       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3410       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3411           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3412           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3413           && !fde->uses_eh_lsda)
3414         continue;
3415
3416       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3417       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3418       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3419       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3420       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3421         dw2_asm_output_data (4, 0xffffffff,
3422                              "Initial length escape value indicating 64-bit DWARF extension");
3423       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3424                             "FDE Length");
3425       ASM_OUTPUT_LABEL (asm_out_file, l1);
3426
3427       if (for_eh)
3428         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3429       else
3430         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3431                                debug_frame_section, "FDE CIE offset");
3432
3433       if (for_eh)
3434         {
3435           if (fde->dw_fde_switched_sections)
3436             {
3437               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3438                                       fde->dw_fde_unlikely_section_label);
3439               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3440                                       fde->dw_fde_hot_section_label);
3441               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3442               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3443               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3444                                                "FDE initial location");
3445               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3446                                     fde->dw_fde_hot_section_end_label,
3447                                     fde->dw_fde_hot_section_label,
3448                                     "FDE address range");
3449               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3450                                                "FDE initial location");
3451               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3452                                     fde->dw_fde_unlikely_section_end_label,
3453                                     fde->dw_fde_unlikely_section_label,
3454                                     "FDE address range");
3455             }
3456           else
3457             {
3458               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3459               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3460               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3461                                                sym_ref,
3462                                                false,
3463                                                "FDE initial location");
3464               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3465                                     fde->dw_fde_end, fde->dw_fde_begin,
3466                                     "FDE address range");
3467             }
3468         }
3469       else
3470         {
3471           if (fde->dw_fde_switched_sections)
3472             {
3473               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3474                                    fde->dw_fde_hot_section_label,
3475                                    "FDE initial location");
3476               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3477                                     fde->dw_fde_hot_section_end_label,
3478                                     fde->dw_fde_hot_section_label,
3479                                     "FDE address range");
3480               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3481                                    fde->dw_fde_unlikely_section_label,
3482                                    "FDE initial location");
3483               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3484                                     fde->dw_fde_unlikely_section_end_label,
3485                                     fde->dw_fde_unlikely_section_label,
3486                                     "FDE address range");
3487             }
3488           else
3489             {
3490               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3491                                    "FDE initial location");
3492               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3493                                     fde->dw_fde_end, fde->dw_fde_begin,
3494                                     "FDE address range");
3495             }
3496         }
3497
3498       if (augmentation[0])
3499         {
3500           if (any_lsda_needed)
3501             {
3502               int size = size_of_encoded_value (lsda_encoding);
3503
3504               if (lsda_encoding == DW_EH_PE_aligned)
3505                 {
3506                   int offset = (  4             /* Length */
3507                                 + 4             /* CIE offset */
3508                                 + 2 * size_of_encoded_value (fde_encoding)
3509                                 + 1             /* Augmentation size */ );
3510                   int pad = -offset & (PTR_SIZE - 1);
3511
3512                   size += pad;
3513                   gcc_assert (size_of_uleb128 (size) == 1);
3514                 }
3515
3516               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3517
3518               if (fde->uses_eh_lsda)
3519                 {
3520                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3521                                                fde->funcdef_number);
3522                   dw2_asm_output_encoded_addr_rtx (
3523                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3524                         false, "Language Specific Data Area");
3525                 }
3526               else
3527                 {
3528                   if (lsda_encoding == DW_EH_PE_aligned)
3529                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3530                   dw2_asm_output_data
3531                     (size_of_encoded_value (lsda_encoding), 0,
3532                      "Language Specific Data Area (none)");
3533                 }
3534             }
3535           else
3536             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3537         }
3538
3539       /* Loop through the Call Frame Instructions associated with
3540          this FDE.  */
3541       fde->dw_fde_current_label = fde->dw_fde_begin;
3542       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3543         output_cfi (cfi, fde, for_eh);
3544
3545       /* Pad the FDE out to an address sized boundary.  */
3546       ASM_OUTPUT_ALIGN (asm_out_file,
3547                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3548       ASM_OUTPUT_LABEL (asm_out_file, l2);
3549     }
3550
3551   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3552     dw2_asm_output_data (4, 0, "End of Table");
3553 #ifdef MIPS_DEBUGGING_INFO
3554   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3555      get a value of 0.  Putting .align 0 after the label fixes it.  */
3556   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3557 #endif
3558
3559   /* Turn off app to make assembly quicker.  */
3560   if (flag_debug_asm)
3561     app_disable ();
3562 }
3563
3564 /* Output a marker (i.e. a label) for the beginning of a function, before
3565    the prologue.  */
3566
3567 void
3568 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3569                           const char *file ATTRIBUTE_UNUSED)
3570 {
3571   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3572   char * dup_label;
3573   dw_fde_ref fde;
3574
3575   current_function_func_begin_label = NULL;
3576
3577 #ifdef TARGET_UNWIND_INFO
3578   /* ??? current_function_func_begin_label is also used by except.c
3579      for call-site information.  We must emit this label if it might
3580      be used.  */
3581   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3582       && ! dwarf2out_do_frame ())
3583     return;
3584 #else
3585   if (! dwarf2out_do_frame ())
3586     return;
3587 #endif
3588
3589   switch_to_section (function_section (current_function_decl));
3590   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3591                                current_function_funcdef_no);
3592   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3593                           current_function_funcdef_no);
3594   dup_label = xstrdup (label);
3595   current_function_func_begin_label = dup_label;
3596
3597 #ifdef TARGET_UNWIND_INFO
3598   /* We can elide the fde allocation if we're not emitting debug info.  */
3599   if (! dwarf2out_do_frame ())
3600     return;
3601 #endif
3602
3603   /* Expand the fde table if necessary.  */
3604   if (fde_table_in_use == fde_table_allocated)
3605     {
3606       fde_table_allocated += FDE_TABLE_INCREMENT;
3607       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3608       memset (fde_table + fde_table_in_use, 0,
3609               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3610     }
3611
3612   /* Record the FDE associated with this function.  */
3613   current_funcdef_fde = fde_table_in_use;
3614
3615   /* Add the new FDE at the end of the fde_table.  */
3616   fde = &fde_table[fde_table_in_use++];
3617   fde->decl = current_function_decl;
3618   fde->dw_fde_begin = dup_label;
3619   fde->dw_fde_current_label = dup_label;
3620   fde->dw_fde_hot_section_label = NULL;
3621   fde->dw_fde_hot_section_end_label = NULL;
3622   fde->dw_fde_unlikely_section_label = NULL;
3623   fde->dw_fde_unlikely_section_end_label = NULL;
3624   fde->dw_fde_switched_sections = false;
3625   fde->dw_fde_end = NULL;
3626   fde->dw_fde_cfi = NULL;
3627   fde->funcdef_number = current_function_funcdef_no;
3628   fde->nothrow = crtl->nothrow;
3629   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3630   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3631   fde->drap_reg = INVALID_REGNUM;
3632   fde->vdrap_reg = INVALID_REGNUM;
3633
3634   args_size = old_args_size = 0;
3635
3636   /* We only want to output line number information for the genuine dwarf2
3637      prologue case, not the eh frame case.  */
3638 #ifdef DWARF2_DEBUGGING_INFO
3639   if (file)
3640     dwarf2out_source_line (line, file, 0, true);
3641 #endif
3642
3643   if (dwarf2out_do_cfi_asm ())
3644     {
3645       int enc;
3646       rtx ref;
3647
3648       fprintf (asm_out_file, "\t.cfi_startproc\n");
3649
3650       if (eh_personality_libfunc)
3651         {
3652           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3653           ref = eh_personality_libfunc;
3654
3655           /* ??? The GAS support isn't entirely consistent.  We have to
3656              handle indirect support ourselves, but PC-relative is done
3657              in the assembler.  Further, the assembler can't handle any
3658              of the weirder relocation types.  */
3659           if (enc & DW_EH_PE_indirect)
3660             ref = dw2_force_const_mem (ref, true);
3661
3662           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3663           output_addr_const (asm_out_file, ref);
3664           fputc ('\n', asm_out_file);
3665         }
3666
3667       if (crtl->uses_eh_lsda)
3668         {
3669           char lab[20];
3670
3671           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3672           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3673                                        current_function_funcdef_no);
3674           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3675           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3676
3677           if (enc & DW_EH_PE_indirect)
3678             ref = dw2_force_const_mem (ref, true);
3679
3680           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3681           output_addr_const (asm_out_file, ref);
3682           fputc ('\n', asm_out_file);
3683         }
3684     }
3685 }
3686
3687 /* Output a marker (i.e. a label) for the absolute end of the generated code
3688    for a function definition.  This gets called *after* the epilogue code has
3689    been generated.  */
3690
3691 void
3692 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3693                         const char *file ATTRIBUTE_UNUSED)
3694 {
3695   dw_fde_ref fde;
3696   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3697
3698 #ifdef DWARF2_DEBUGGING_INFO
3699   last_var_location_insn = NULL_RTX;
3700 #endif
3701
3702   if (dwarf2out_do_cfi_asm ())
3703     fprintf (asm_out_file, "\t.cfi_endproc\n");
3704
3705   /* Output a label to mark the endpoint of the code generated for this
3706      function.  */
3707   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3708                                current_function_funcdef_no);
3709   ASM_OUTPUT_LABEL (asm_out_file, label);
3710   fde = current_fde ();
3711   gcc_assert (fde != NULL);
3712   fde->dw_fde_end = xstrdup (label);
3713 }
3714
3715 void
3716 dwarf2out_frame_init (void)
3717 {
3718   /* Allocate the initial hunk of the fde_table.  */
3719   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3720   fde_table_allocated = FDE_TABLE_INCREMENT;
3721   fde_table_in_use = 0;
3722
3723   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3724      sake of lookup_cfa.  */
3725
3726   /* On entry, the Canonical Frame Address is at SP.  */
3727   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3728
3729 #ifdef DWARF2_UNWIND_INFO
3730   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3731     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3732 #endif
3733 }
3734
3735 void
3736 dwarf2out_frame_finish (void)
3737 {
3738   /* Output call frame information.  */
3739   if (DWARF2_FRAME_INFO)
3740     output_call_frame_info (0);
3741
3742 #ifndef TARGET_UNWIND_INFO
3743   /* Output another copy for the unwinder.  */
3744   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3745     output_call_frame_info (1);
3746 #endif
3747 }
3748
3749 /* Note that the current function section is being used for code.  */
3750
3751 static void
3752 dwarf2out_note_section_used (void)
3753 {
3754   section *sec = current_function_section ();
3755   if (sec == text_section)
3756     text_section_used = true;
3757   else if (sec == cold_text_section)
3758     cold_text_section_used = true;
3759 }
3760
3761 void
3762 dwarf2out_switch_text_section (void)
3763 {
3764   dw_fde_ref fde = current_fde ();
3765
3766   gcc_assert (cfun && fde);
3767
3768   fde->dw_fde_switched_sections = true;
3769   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3770   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3771   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3772   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3773   have_multiple_function_sections = true;
3774
3775   /* Reset the current label on switching text sections, so that we
3776      don't attempt to advance_loc4 between labels in different sections.  */
3777   fde->dw_fde_current_label = NULL;
3778
3779   /* There is no need to mark used sections when not debugging.  */
3780   if (cold_text_section != NULL)
3781     dwarf2out_note_section_used ();
3782 }
3783 #endif
3784 \f
3785 /* And now, the subset of the debugging information support code necessary
3786    for emitting location expressions.  */
3787
3788 /* Data about a single source file.  */
3789 struct GTY(()) dwarf_file_data {
3790   const char * filename;
3791   int emitted_number;
3792 };
3793
3794 typedef struct dw_val_struct *dw_val_ref;
3795 typedef struct die_struct *dw_die_ref;
3796 typedef const struct die_struct *const_dw_die_ref;
3797 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3798 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3799
3800 typedef struct GTY(()) deferred_locations_struct
3801 {
3802   tree variable;
3803   dw_die_ref die;
3804 } deferred_locations;
3805
3806 DEF_VEC_O(deferred_locations);
3807 DEF_VEC_ALLOC_O(deferred_locations,gc);
3808
3809 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3810
3811 /* Each DIE may have a series of attribute/value pairs.  Values
3812    can take on several forms.  The forms that are used in this
3813    implementation are listed below.  */
3814
3815 enum dw_val_class
3816 {
3817   dw_val_class_addr,
3818   dw_val_class_offset,
3819   dw_val_class_loc,
3820   dw_val_class_loc_list,
3821   dw_val_class_range_list,
3822   dw_val_class_const,
3823   dw_val_class_unsigned_const,
3824   dw_val_class_long_long,
3825   dw_val_class_vec,
3826   dw_val_class_flag,
3827   dw_val_class_die_ref,
3828   dw_val_class_fde_ref,
3829   dw_val_class_lbl_id,
3830   dw_val_class_lineptr,
3831   dw_val_class_str,
3832   dw_val_class_macptr,
3833   dw_val_class_file
3834 };
3835
3836 /* Describe a double word constant value.  */
3837 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3838
3839 typedef struct GTY(()) dw_long_long_struct {
3840   unsigned long hi;
3841   unsigned long low;
3842 }
3843 dw_long_long_const;
3844
3845 /* Describe a floating point constant value, or a vector constant value.  */
3846
3847 typedef struct GTY(()) dw_vec_struct {
3848   unsigned char * GTY((length ("%h.length"))) array;
3849   unsigned length;
3850   unsigned elt_size;
3851 }
3852 dw_vec_const;
3853
3854 /* The dw_val_node describes an attribute's value, as it is
3855    represented internally.  */
3856
3857 typedef struct GTY(()) dw_val_struct {
3858   enum dw_val_class val_class;
3859   union dw_val_struct_union
3860     {
3861       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3862       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3863       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3864       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3865       HOST_WIDE_INT GTY ((default)) val_int;
3866       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3867       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3868       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3869       struct dw_val_die_union
3870         {
3871           dw_die_ref die;
3872           int external;
3873         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3874       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3875       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3876       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3877       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3878       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3879     }
3880   GTY ((desc ("%1.val_class"))) v;
3881 }
3882 dw_val_node;
3883
3884 /* Locations in memory are described using a sequence of stack machine
3885    operations.  */
3886
3887 typedef struct GTY(()) dw_loc_descr_struct {
3888   dw_loc_descr_ref dw_loc_next;
3889   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
3890   /* Used to distinguish DW_OP_addr with a direct symbol relocation
3891      from DW_OP_addr with a dtp-relative symbol relocation.  */
3892   unsigned int dtprel : 1;
3893   int dw_loc_addr;
3894   dw_val_node dw_loc_oprnd1;
3895   dw_val_node dw_loc_oprnd2;
3896 }
3897 dw_loc_descr_node;
3898
3899 /* Location lists are ranges + location descriptions for that range,
3900    so you can track variables that are in different places over
3901    their entire life.  */
3902 typedef struct GTY(()) dw_loc_list_struct {
3903   dw_loc_list_ref dw_loc_next;
3904   const char *begin; /* Label for begin address of range */
3905   const char *end;  /* Label for end address of range */
3906   char *ll_symbol; /* Label for beginning of location list.
3907                       Only on head of list */
3908   const char *section; /* Section this loclist is relative to */
3909   dw_loc_descr_ref expr;
3910 } dw_loc_list_node;
3911
3912 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3913
3914 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3915
3916 /* Convert a DWARF stack opcode into its string name.  */
3917
3918 static const char *
3919 dwarf_stack_op_name (unsigned int op)
3920 {
3921   switch (op)
3922     {
3923     case DW_OP_addr:
3924       return "DW_OP_addr";
3925     case DW_OP_deref:
3926       return "DW_OP_deref";
3927     case DW_OP_const1u:
3928       return "DW_OP_const1u";
3929     case DW_OP_const1s:
3930       return "DW_OP_const1s";
3931     case DW_OP_const2u:
3932       return "DW_OP_const2u";
3933     case DW_OP_const2s:
3934       return "DW_OP_const2s";
3935     case DW_OP_const4u:
3936       return "DW_OP_const4u";
3937     case DW_OP_const4s:
3938       return "DW_OP_const4s";
3939     case DW_OP_const8u:
3940       return "DW_OP_const8u";
3941     case DW_OP_const8s:
3942       return "DW_OP_const8s";
3943     case DW_OP_constu:
3944       return "DW_OP_constu";
3945     case DW_OP_consts:
3946       return "DW_OP_consts";
3947     case DW_OP_dup:
3948       return "DW_OP_dup";
3949     case DW_OP_drop:
3950       return "DW_OP_drop";
3951     case DW_OP_over:
3952       return "DW_OP_over";
3953     case DW_OP_pick:
3954       return "DW_OP_pick";
3955     case DW_OP_swap:
3956       return "DW_OP_swap";
3957     case DW_OP_rot:
3958       return "DW_OP_rot";
3959     case DW_OP_xderef:
3960       return "DW_OP_xderef";
3961     case DW_OP_abs:
3962       return "DW_OP_abs";
3963     case DW_OP_and:
3964       return "DW_OP_and";
3965     case DW_OP_div:
3966       return "DW_OP_div";
3967     case DW_OP_minus:
3968       return "DW_OP_minus";
3969     case DW_OP_mod:
3970       return "DW_OP_mod";
3971     case DW_OP_mul:
3972       return "DW_OP_mul";
3973     case DW_OP_neg:
3974       return "DW_OP_neg";
3975     case DW_OP_not:
3976       return "DW_OP_not";
3977     case DW_OP_or:
3978       return "DW_OP_or";
3979     case DW_OP_plus:
3980       return "DW_OP_plus";
3981     case DW_OP_plus_uconst:
3982       return "DW_OP_plus_uconst";
3983     case DW_OP_shl:
3984       return "DW_OP_shl";
3985     case DW_OP_shr:
3986       return "DW_OP_shr";
3987     case DW_OP_shra:
3988       return "DW_OP_shra";
3989     case DW_OP_xor:
3990       return "DW_OP_xor";
3991     case DW_OP_bra:
3992       return "DW_OP_bra";
3993     case DW_OP_eq:
3994       return "DW_OP_eq";
3995     case DW_OP_ge:
3996       return "DW_OP_ge";
3997     case DW_OP_gt:
3998       return "DW_OP_gt";
3999     case DW_OP_le:
4000       return "DW_OP_le";
4001     case DW_OP_lt:
4002       return "DW_OP_lt";
4003     case DW_OP_ne:
4004       return "DW_OP_ne";
4005     case DW_OP_skip:
4006       return "DW_OP_skip";
4007     case DW_OP_lit0:
4008       return "DW_OP_lit0";
4009     case DW_OP_lit1:
4010       return "DW_OP_lit1";
4011     case DW_OP_lit2:
4012       return "DW_OP_lit2";
4013     case DW_OP_lit3:
4014       return "DW_OP_lit3";
4015     case DW_OP_lit4:
4016       return "DW_OP_lit4";
4017     case DW_OP_lit5:
4018       return "DW_OP_lit5";
4019     case DW_OP_lit6:
4020       return "DW_OP_lit6";
4021     case DW_OP_lit7:
4022       return "DW_OP_lit7";
4023     case DW_OP_lit8:
4024       return "DW_OP_lit8";
4025     case DW_OP_lit9:
4026       return "DW_OP_lit9";
4027     case DW_OP_lit10:
4028       return "DW_OP_lit10";
4029     case DW_OP_lit11:
4030       return "DW_OP_lit11";
4031     case DW_OP_lit12:
4032       return "DW_OP_lit12";
4033     case DW_OP_lit13:
4034       return "DW_OP_lit13";
4035     case DW_OP_lit14:
4036       return "DW_OP_lit14";
4037     case DW_OP_lit15:
4038       return "DW_OP_lit15";
4039     case DW_OP_lit16:
4040       return "DW_OP_lit16";
4041     case DW_OP_lit17:
4042       return "DW_OP_lit17";
4043     case DW_OP_lit18:
4044       return "DW_OP_lit18";
4045     case DW_OP_lit19:
4046       return "DW_OP_lit19";
4047     case DW_OP_lit20:
4048       return "DW_OP_lit20";
4049     case DW_OP_lit21:
4050       return "DW_OP_lit21";
4051     case DW_OP_lit22:
4052       return "DW_OP_lit22";
4053     case DW_OP_lit23:
4054       return "DW_OP_lit23";
4055     case DW_OP_lit24:
4056       return "DW_OP_lit24";
4057     case DW_OP_lit25:
4058       return "DW_OP_lit25";
4059     case DW_OP_lit26:
4060       return "DW_OP_lit26";
4061     case DW_OP_lit27:
4062       return "DW_OP_lit27";
4063     case DW_OP_lit28:
4064       return "DW_OP_lit28";
4065     case DW_OP_lit29:
4066       return "DW_OP_lit29";
4067     case DW_OP_lit30:
4068       return "DW_OP_lit30";
4069     case DW_OP_lit31:
4070       return "DW_OP_lit31";
4071     case DW_OP_reg0:
4072       return "DW_OP_reg0";
4073     case DW_OP_reg1:
4074       return "DW_OP_reg1";
4075     case DW_OP_reg2:
4076       return "DW_OP_reg2";
4077     case DW_OP_reg3:
4078       return "DW_OP_reg3";
4079     case DW_OP_reg4:
4080       return "DW_OP_reg4";
4081     case DW_OP_reg5:
4082       return "DW_OP_reg5";
4083     case DW_OP_reg6:
4084       return "DW_OP_reg6";
4085     case DW_OP_reg7:
4086       return "DW_OP_reg7";
4087     case DW_OP_reg8:
4088       return "DW_OP_reg8";
4089     case DW_OP_reg9:
4090       return "DW_OP_reg9";
4091     case DW_OP_reg10:
4092       return "DW_OP_reg10";
4093     case DW_OP_reg11:
4094       return "DW_OP_reg11";
4095     case DW_OP_reg12:
4096       return "DW_OP_reg12";
4097     case DW_OP_reg13:
4098       return "DW_OP_reg13";
4099     case DW_OP_reg14:
4100       return "DW_OP_reg14";
4101     case DW_OP_reg15:
4102       return "DW_OP_reg15";
4103     case DW_OP_reg16:
4104       return "DW_OP_reg16";
4105     case DW_OP_reg17:
4106       return "DW_OP_reg17";
4107     case DW_OP_reg18:
4108       return "DW_OP_reg18";
4109     case DW_OP_reg19:
4110       return "DW_OP_reg19";
4111     case DW_OP_reg20:
4112       return "DW_OP_reg20";
4113     case DW_OP_reg21:
4114       return "DW_OP_reg21";
4115     case DW_OP_reg22:
4116       return "DW_OP_reg22";
4117     case DW_OP_reg23:
4118       return "DW_OP_reg23";
4119     case DW_OP_reg24:
4120       return "DW_OP_reg24";
4121     case DW_OP_reg25:
4122       return "DW_OP_reg25";
4123     case DW_OP_reg26:
4124       return "DW_OP_reg26";
4125     case DW_OP_reg27:
4126       return "DW_OP_reg27";
4127     case DW_OP_reg28:
4128       return "DW_OP_reg28";
4129     case DW_OP_reg29:
4130       return "DW_OP_reg29";
4131     case DW_OP_reg30:
4132       return "DW_OP_reg30";
4133     case DW_OP_reg31:
4134       return "DW_OP_reg31";
4135     case DW_OP_breg0:
4136       return "DW_OP_breg0";
4137     case DW_OP_breg1:
4138       return "DW_OP_breg1";
4139     case DW_OP_breg2:
4140       return "DW_OP_breg2";
4141     case DW_OP_breg3:
4142       return "DW_OP_breg3";
4143     case DW_OP_breg4:
4144       return "DW_OP_breg4";
4145     case DW_OP_breg5:
4146       return "DW_OP_breg5";
4147     case DW_OP_breg6:
4148       return "DW_OP_breg6";
4149     case DW_OP_breg7:
4150       return "DW_OP_breg7";
4151     case DW_OP_breg8:
4152       return "DW_OP_breg8";
4153     case DW_OP_breg9:
4154       return "DW_OP_breg9";
4155     case DW_OP_breg10:
4156       return "DW_OP_breg10";
4157     case DW_OP_breg11:
4158       return "DW_OP_breg11";
4159     case DW_OP_breg12:
4160       return "DW_OP_breg12";
4161     case DW_OP_breg13:
4162       return "DW_OP_breg13";
4163     case DW_OP_breg14:
4164       return "DW_OP_breg14";
4165     case DW_OP_breg15:
4166       return "DW_OP_breg15";
4167     case DW_OP_breg16:
4168       return "DW_OP_breg16";
4169     case DW_OP_breg17:
4170       return "DW_OP_breg17";
4171     case DW_OP_breg18:
4172       return "DW_OP_breg18";
4173     case DW_OP_breg19:
4174       return "DW_OP_breg19";
4175     case DW_OP_breg20:
4176       return "DW_OP_breg20";
4177     case DW_OP_breg21:
4178       return "DW_OP_breg21";
4179     case DW_OP_breg22:
4180       return "DW_OP_breg22";
4181     case DW_OP_breg23:
4182       return "DW_OP_breg23";
4183     case DW_OP_breg24:
4184       return "DW_OP_breg24";
4185     case DW_OP_breg25:
4186       return "DW_OP_breg25";
4187     case DW_OP_breg26:
4188       return "DW_OP_breg26";
4189     case DW_OP_breg27:
4190       return "DW_OP_breg27";
4191     case DW_OP_breg28:
4192       return "DW_OP_breg28";
4193     case DW_OP_breg29:
4194       return "DW_OP_breg29";
4195     case DW_OP_breg30:
4196       return "DW_OP_breg30";
4197     case DW_OP_breg31:
4198       return "DW_OP_breg31";
4199     case DW_OP_regx:
4200       return "DW_OP_regx";
4201     case DW_OP_fbreg:
4202       return "DW_OP_fbreg";
4203     case DW_OP_bregx:
4204       return "DW_OP_bregx";
4205     case DW_OP_piece:
4206       return "DW_OP_piece";
4207     case DW_OP_deref_size:
4208       return "DW_OP_deref_size";
4209     case DW_OP_xderef_size:
4210       return "DW_OP_xderef_size";
4211     case DW_OP_nop:
4212       return "DW_OP_nop";
4213
4214     case DW_OP_push_object_address:
4215       return "DW_OP_push_object_address";
4216     case DW_OP_call2:
4217       return "DW_OP_call2";
4218     case DW_OP_call4:
4219       return "DW_OP_call4";
4220     case DW_OP_call_ref:
4221       return "DW_OP_call_ref";
4222     case DW_OP_form_tls_address:
4223       return "DW_OP_form_tls_address";
4224     case DW_OP_call_frame_cfa:
4225       return "DW_OP_call_frame_cfa";
4226     case DW_OP_bit_piece:
4227       return "DW_OP_bit_piece";
4228
4229     case DW_OP_GNU_push_tls_address:
4230       return "DW_OP_GNU_push_tls_address";
4231     case DW_OP_GNU_uninit:
4232       return "DW_OP_GNU_uninit";
4233     case DW_OP_GNU_encoded_addr:
4234       return "DW_OP_GNU_encoded_addr";
4235
4236     default:
4237       return "OP_<unknown>";
4238     }
4239 }
4240
4241 /* Return a pointer to a newly allocated location description.  Location
4242    descriptions are simple expression terms that can be strung
4243    together to form more complicated location (address) descriptions.  */
4244
4245 static inline dw_loc_descr_ref
4246 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4247                unsigned HOST_WIDE_INT oprnd2)
4248 {
4249   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4250
4251   descr->dw_loc_opc = op;
4252   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4253   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4254   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4255   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4256
4257   return descr;
4258 }
4259
4260 /* Return a pointer to a newly allocated location description for
4261    REG and OFFSET.  */
4262
4263 static inline dw_loc_descr_ref
4264 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4265 {
4266   if (reg <= 31)
4267     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4268                           offset, 0);
4269   else
4270     return new_loc_descr (DW_OP_bregx, reg, offset);
4271 }
4272
4273 /* Add a location description term to a location description expression.  */
4274
4275 static inline void
4276 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4277 {
4278   dw_loc_descr_ref *d;
4279
4280   /* Find the end of the chain.  */
4281   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4282     ;
4283
4284   *d = descr;
4285 }
4286
4287 /* Add a constant OFFSET to a location expression.  */
4288
4289 static void
4290 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4291 {
4292   dw_loc_descr_ref loc;
4293   HOST_WIDE_INT *p;
4294
4295   gcc_assert (*list_head != NULL);
4296
4297   if (!offset)
4298     return;
4299
4300   /* Find the end of the chain.  */
4301   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4302     ;
4303
4304   p = NULL;
4305   if (loc->dw_loc_opc == DW_OP_fbreg
4306       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4307     p = &loc->dw_loc_oprnd1.v.val_int;
4308   else if (loc->dw_loc_opc == DW_OP_bregx)
4309     p = &loc->dw_loc_oprnd2.v.val_int;
4310
4311   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4312      offset.  Don't optimize if an signed integer overflow would happen.  */
4313   if (p != NULL
4314       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4315           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4316     *p += offset;
4317
4318   else if (offset > 0)
4319     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4320
4321   else
4322     {
4323       loc->dw_loc_next = int_loc_descriptor (offset);
4324       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4325     }
4326 }
4327
4328 /* Return the size of a location descriptor.  */
4329
4330 static unsigned long
4331 size_of_loc_descr (dw_loc_descr_ref loc)
4332 {
4333   unsigned long size = 1;
4334
4335   switch (loc->dw_loc_opc)
4336     {
4337     case DW_OP_addr:
4338       size += DWARF2_ADDR_SIZE;
4339       break;
4340     case DW_OP_const1u:
4341     case DW_OP_const1s:
4342       size += 1;
4343       break;
4344     case DW_OP_const2u:
4345     case DW_OP_const2s:
4346       size += 2;
4347       break;
4348     case DW_OP_const4u:
4349     case DW_OP_const4s:
4350       size += 4;
4351       break;
4352     case DW_OP_const8u:
4353     case DW_OP_const8s:
4354       size += 8;
4355       break;
4356     case DW_OP_constu:
4357       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4358       break;
4359     case DW_OP_consts:
4360       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4361       break;
4362     case DW_OP_pick:
4363       size += 1;
4364       break;
4365     case DW_OP_plus_uconst:
4366       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4367       break;
4368     case DW_OP_skip:
4369     case DW_OP_bra:
4370       size += 2;
4371       break;
4372     case DW_OP_breg0:
4373     case DW_OP_breg1:
4374     case DW_OP_breg2:
4375     case DW_OP_breg3:
4376     case DW_OP_breg4:
4377     case DW_OP_breg5:
4378     case DW_OP_breg6:
4379     case DW_OP_breg7:
4380     case DW_OP_breg8:
4381     case DW_OP_breg9:
4382     case DW_OP_breg10:
4383     case DW_OP_breg11:
4384     case DW_OP_breg12:
4385     case DW_OP_breg13:
4386     case DW_OP_breg14:
4387     case DW_OP_breg15:
4388     case DW_OP_breg16:
4389     case DW_OP_breg17:
4390     case DW_OP_breg18:
4391     case DW_OP_breg19:
4392     case DW_OP_breg20:
4393     case DW_OP_breg21:
4394     case DW_OP_breg22:
4395     case DW_OP_breg23:
4396     case DW_OP_breg24:
4397     case DW_OP_breg25:
4398     case DW_OP_breg26:
4399     case DW_OP_breg27:
4400     case DW_OP_breg28:
4401     case DW_OP_breg29:
4402     case DW_OP_breg30:
4403     case DW_OP_breg31:
4404       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4405       break;
4406     case DW_OP_regx:
4407       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4408       break;
4409     case DW_OP_fbreg:
4410       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4411       break;
4412     case DW_OP_bregx:
4413       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4414       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4415       break;
4416     case DW_OP_piece:
4417       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4418       break;
4419     case DW_OP_deref_size:
4420     case DW_OP_xderef_size:
4421       size += 1;
4422       break;
4423     case DW_OP_call2:
4424       size += 2;
4425       break;
4426     case DW_OP_call4:
4427       size += 4;
4428       break;
4429     case DW_OP_call_ref:
4430       size += DWARF2_ADDR_SIZE;
4431       break;
4432     default:
4433       break;
4434     }
4435
4436   return size;
4437 }
4438
4439 /* Return the size of a series of location descriptors.  */
4440
4441 static unsigned long
4442 size_of_locs (dw_loc_descr_ref loc)
4443 {
4444   dw_loc_descr_ref l;
4445   unsigned long size;
4446
4447   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4448      field, to avoid writing to a PCH file.  */
4449   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4450     {
4451       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4452         break;
4453       size += size_of_loc_descr (l);
4454     }
4455   if (! l)
4456     return size;
4457
4458   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4459     {
4460       l->dw_loc_addr = size;
4461       size += size_of_loc_descr (l);
4462     }
4463
4464   return size;
4465 }
4466
4467 /* Output location description stack opcode's operands (if any).  */
4468
4469 static void
4470 output_loc_operands (dw_loc_descr_ref loc)
4471 {
4472   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4473   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4474
4475   switch (loc->dw_loc_opc)
4476     {
4477 #ifdef DWARF2_DEBUGGING_INFO
4478     case DW_OP_const2u:
4479     case DW_OP_const2s:
4480       dw2_asm_output_data (2, val1->v.val_int, NULL);
4481       break;
4482     case DW_OP_const4u:
4483     case DW_OP_const4s:
4484       dw2_asm_output_data (4, val1->v.val_int, NULL);
4485       break;
4486     case DW_OP_const8u:
4487     case DW_OP_const8s:
4488       gcc_assert (HOST_BITS_PER_LONG >= 64);
4489       dw2_asm_output_data (8, val1->v.val_int, NULL);
4490       break;
4491     case DW_OP_skip:
4492     case DW_OP_bra:
4493       {
4494         int offset;
4495
4496         gcc_assert (val1->val_class == dw_val_class_loc);
4497         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4498
4499         dw2_asm_output_data (2, offset, NULL);
4500       }
4501       break;
4502 #else
4503     case DW_OP_const2u:
4504     case DW_OP_const2s:
4505     case DW_OP_const4u:
4506     case DW_OP_const4s:
4507     case DW_OP_const8u:
4508     case DW_OP_const8s:
4509     case DW_OP_skip:
4510     case DW_OP_bra:
4511       /* We currently don't make any attempt to make sure these are
4512          aligned properly like we do for the main unwind info, so
4513          don't support emitting things larger than a byte if we're
4514          only doing unwinding.  */
4515       gcc_unreachable ();
4516 #endif
4517     case DW_OP_const1u:
4518     case DW_OP_const1s:
4519       dw2_asm_output_data (1, val1->v.val_int, NULL);
4520       break;
4521     case DW_OP_constu:
4522       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4523       break;
4524     case DW_OP_consts:
4525       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4526       break;
4527     case DW_OP_pick:
4528       dw2_asm_output_data (1, val1->v.val_int, NULL);
4529       break;
4530     case DW_OP_plus_uconst:
4531       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4532       break;
4533     case DW_OP_breg0:
4534     case DW_OP_breg1:
4535     case DW_OP_breg2:
4536     case DW_OP_breg3:
4537     case DW_OP_breg4:
4538     case DW_OP_breg5:
4539     case DW_OP_breg6:
4540     case DW_OP_breg7:
4541     case DW_OP_breg8:
4542     case DW_OP_breg9:
4543     case DW_OP_breg10:
4544     case DW_OP_breg11:
4545     case DW_OP_breg12:
4546     case DW_OP_breg13:
4547     case DW_OP_breg14:
4548     case DW_OP_breg15:
4549     case DW_OP_breg16:
4550     case DW_OP_breg17:
4551     case DW_OP_breg18:
4552     case DW_OP_breg19:
4553     case DW_OP_breg20:
4554     case DW_OP_breg21:
4555     case DW_OP_breg22:
4556     case DW_OP_breg23:
4557     case DW_OP_breg24:
4558     case DW_OP_breg25:
4559     case DW_OP_breg26:
4560     case DW_OP_breg27:
4561     case DW_OP_breg28:
4562     case DW_OP_breg29:
4563     case DW_OP_breg30:
4564     case DW_OP_breg31:
4565       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4566       break;
4567     case DW_OP_regx:
4568       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4569       break;
4570     case DW_OP_fbreg:
4571       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4572       break;
4573     case DW_OP_bregx:
4574       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4575       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4576       break;
4577     case DW_OP_piece:
4578       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4579       break;
4580     case DW_OP_deref_size:
4581     case DW_OP_xderef_size:
4582       dw2_asm_output_data (1, val1->v.val_int, NULL);
4583       break;
4584
4585     case DW_OP_addr:
4586       if (loc->dtprel)
4587         {
4588           if (targetm.asm_out.output_dwarf_dtprel)
4589             {
4590               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4591                                                    DWARF2_ADDR_SIZE,
4592                                                    val1->v.val_addr);
4593               fputc ('\n', asm_out_file);
4594             }
4595           else
4596             gcc_unreachable ();
4597         }
4598       else
4599         {
4600 #ifdef DWARF2_DEBUGGING_INFO
4601           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4602 #else
4603           gcc_unreachable ();
4604 #endif
4605         }
4606       break;
4607
4608     default:
4609       /* Other codes have no operands.  */
4610       break;
4611     }
4612 }
4613
4614 /* Output a sequence of location operations.  */
4615
4616 static void
4617 output_loc_sequence (dw_loc_descr_ref loc)
4618 {
4619   for (; loc != NULL; loc = loc->dw_loc_next)
4620     {
4621       /* Output the opcode.  */
4622       dw2_asm_output_data (1, loc->dw_loc_opc,
4623                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4624
4625       /* Output the operand(s) (if any).  */
4626       output_loc_operands (loc);
4627     }
4628 }
4629
4630 /* Output location description stack opcode's operands (if any).
4631    The output is single bytes on a line, suitable for .cfi_escape.  */
4632
4633 static void
4634 output_loc_operands_raw (dw_loc_descr_ref loc)
4635 {
4636   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4637   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4638
4639   switch (loc->dw_loc_opc)
4640     {
4641     case DW_OP_addr:
4642       /* We cannot output addresses in .cfi_escape, only bytes.  */
4643       gcc_unreachable ();
4644
4645     case DW_OP_const1u:
4646     case DW_OP_const1s:
4647     case DW_OP_pick:
4648     case DW_OP_deref_size:
4649     case DW_OP_xderef_size:
4650       fputc (',', asm_out_file);
4651       dw2_asm_output_data_raw (1, val1->v.val_int);
4652       break;
4653
4654     case DW_OP_const2u:
4655     case DW_OP_const2s:
4656       fputc (',', asm_out_file);
4657       dw2_asm_output_data_raw (2, val1->v.val_int);
4658       break;
4659
4660     case DW_OP_const4u:
4661     case DW_OP_const4s:
4662       fputc (',', asm_out_file);
4663       dw2_asm_output_data_raw (4, val1->v.val_int);
4664       break;
4665
4666     case DW_OP_const8u:
4667     case DW_OP_const8s:
4668       gcc_assert (HOST_BITS_PER_LONG >= 64);
4669       fputc (',', asm_out_file);
4670       dw2_asm_output_data_raw (8, val1->v.val_int);
4671       break;
4672
4673     case DW_OP_skip:
4674     case DW_OP_bra:
4675       {
4676         int offset;
4677
4678         gcc_assert (val1->val_class == dw_val_class_loc);
4679         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4680
4681         fputc (',', asm_out_file);
4682         dw2_asm_output_data_raw (2, offset);
4683       }
4684       break;
4685
4686     case DW_OP_constu:
4687     case DW_OP_plus_uconst:
4688     case DW_OP_regx:
4689     case DW_OP_piece:
4690       fputc (',', asm_out_file);
4691       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4692       break;
4693
4694     case DW_OP_consts:
4695     case DW_OP_breg0:
4696     case DW_OP_breg1:
4697     case DW_OP_breg2:
4698     case DW_OP_breg3:
4699     case DW_OP_breg4:
4700     case DW_OP_breg5:
4701     case DW_OP_breg6:
4702     case DW_OP_breg7:
4703     case DW_OP_breg8:
4704     case DW_OP_breg9:
4705     case DW_OP_breg10:
4706     case DW_OP_breg11:
4707     case DW_OP_breg12:
4708     case DW_OP_breg13:
4709     case DW_OP_breg14:
4710     case DW_OP_breg15:
4711     case DW_OP_breg16:
4712     case DW_OP_breg17:
4713     case DW_OP_breg18:
4714     case DW_OP_breg19:
4715     case DW_OP_breg20:
4716     case DW_OP_breg21:
4717     case DW_OP_breg22:
4718     case DW_OP_breg23:
4719     case DW_OP_breg24:
4720     case DW_OP_breg25:
4721     case DW_OP_breg26:
4722     case DW_OP_breg27:
4723     case DW_OP_breg28:
4724     case DW_OP_breg29:
4725     case DW_OP_breg30:
4726     case DW_OP_breg31:
4727     case DW_OP_fbreg:
4728       fputc (',', asm_out_file);
4729       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4730       break;
4731
4732     case DW_OP_bregx:
4733       fputc (',', asm_out_file);
4734       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4735       fputc (',', asm_out_file);
4736       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4737       break;
4738
4739     default:
4740       /* Other codes have no operands.  */
4741       break;
4742     }
4743 }
4744
4745 static void
4746 output_loc_sequence_raw (dw_loc_descr_ref loc)
4747 {
4748   while (1)
4749     {
4750       /* Output the opcode.  */
4751       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4752       output_loc_operands_raw (loc);
4753
4754       if (!loc->dw_loc_next)
4755         break;
4756       loc = loc->dw_loc_next;
4757
4758       fputc (',', asm_out_file);
4759     }
4760 }
4761
4762 /* This routine will generate the correct assembly data for a location
4763    description based on a cfi entry with a complex address.  */
4764
4765 static void
4766 output_cfa_loc (dw_cfi_ref cfi)
4767 {
4768   dw_loc_descr_ref loc;
4769   unsigned long size;
4770
4771   if (cfi->dw_cfi_opc == DW_CFA_expression)
4772     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4773
4774   /* Output the size of the block.  */
4775   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4776   size = size_of_locs (loc);
4777   dw2_asm_output_data_uleb128 (size, NULL);
4778
4779   /* Now output the operations themselves.  */
4780   output_loc_sequence (loc);
4781 }
4782
4783 /* Similar, but used for .cfi_escape.  */
4784
4785 static void
4786 output_cfa_loc_raw (dw_cfi_ref cfi)
4787 {
4788   dw_loc_descr_ref loc;
4789   unsigned long size;
4790
4791   if (cfi->dw_cfi_opc == DW_CFA_expression)
4792     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4793
4794   /* Output the size of the block.  */
4795   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4796   size = size_of_locs (loc);
4797   dw2_asm_output_data_uleb128_raw (size);
4798   fputc (',', asm_out_file);
4799
4800   /* Now output the operations themselves.  */
4801   output_loc_sequence_raw (loc);
4802 }
4803
4804 /* This function builds a dwarf location descriptor sequence from a
4805    dw_cfa_location, adding the given OFFSET to the result of the
4806    expression.  */
4807
4808 static struct dw_loc_descr_struct *
4809 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4810 {
4811   struct dw_loc_descr_struct *head, *tmp;
4812
4813   offset += cfa->offset;
4814
4815   if (cfa->indirect)
4816     {
4817       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4818       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4819       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4820       add_loc_descr (&head, tmp);
4821       if (offset != 0)
4822         {
4823           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4824           add_loc_descr (&head, tmp);
4825         }
4826     }
4827   else
4828     head = new_reg_loc_descr (cfa->reg, offset);
4829
4830   return head;
4831 }
4832
4833 /* This function builds a dwarf location descriptor sequence for
4834    the address at OFFSET from the CFA when stack is aligned to
4835    ALIGNMENT byte.  */
4836
4837 static struct dw_loc_descr_struct *
4838 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4839 {
4840   struct dw_loc_descr_struct *head;
4841   unsigned int dwarf_fp
4842     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4843
4844  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4845   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4846     {
4847       head = new_reg_loc_descr (dwarf_fp, 0);
4848       add_loc_descr (&head, int_loc_descriptor (alignment));
4849       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4850       loc_descr_plus_const (&head, offset);
4851     }
4852   else
4853     head = new_reg_loc_descr (dwarf_fp, offset);
4854   return head;
4855 }
4856
4857 /* This function fills in aa dw_cfa_location structure from a dwarf location
4858    descriptor sequence.  */
4859
4860 static void
4861 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4862 {
4863   struct dw_loc_descr_struct *ptr;
4864   cfa->offset = 0;
4865   cfa->base_offset = 0;
4866   cfa->indirect = 0;
4867   cfa->reg = -1;
4868
4869   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4870     {
4871       enum dwarf_location_atom op = ptr->dw_loc_opc;
4872
4873       switch (op)
4874         {
4875         case DW_OP_reg0:
4876         case DW_OP_reg1:
4877         case DW_OP_reg2:
4878         case DW_OP_reg3:
4879         case DW_OP_reg4:
4880         case DW_OP_reg5:
4881         case DW_OP_reg6:
4882         case DW_OP_reg7:
4883         case DW_OP_reg8:
4884         case DW_OP_reg9:
4885         case DW_OP_reg10:
4886         case DW_OP_reg11:
4887         case DW_OP_reg12:
4888         case DW_OP_reg13:
4889         case DW_OP_reg14:
4890         case DW_OP_reg15:
4891         case DW_OP_reg16:
4892         case DW_OP_reg17:
4893         case DW_OP_reg18:
4894         case DW_OP_reg19:
4895         case DW_OP_reg20:
4896         case DW_OP_reg21:
4897         case DW_OP_reg22:
4898         case DW_OP_reg23:
4899         case DW_OP_reg24:
4900         case DW_OP_reg25:
4901         case DW_OP_reg26:
4902         case DW_OP_reg27:
4903         case DW_OP_reg28:
4904         case DW_OP_reg29:
4905         case DW_OP_reg30:
4906         case DW_OP_reg31:
4907           cfa->reg = op - DW_OP_reg0;
4908           break;
4909         case DW_OP_regx:
4910           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4911           break;
4912         case DW_OP_breg0:
4913         case DW_OP_breg1:
4914         case DW_OP_breg2:
4915         case DW_OP_breg3:
4916         case DW_OP_breg4:
4917         case DW_OP_breg5:
4918         case DW_OP_breg6:
4919         case DW_OP_breg7:
4920         case DW_OP_breg8:
4921         case DW_OP_breg9:
4922         case DW_OP_breg10:
4923         case DW_OP_breg11:
4924         case DW_OP_breg12:
4925         case DW_OP_breg13:
4926         case DW_OP_breg14:
4927         case DW_OP_breg15:
4928         case DW_OP_breg16:
4929         case DW_OP_breg17:
4930         case DW_OP_breg18:
4931         case DW_OP_breg19:
4932         case DW_OP_breg20:
4933         case DW_OP_breg21:
4934         case DW_OP_breg22:
4935         case DW_OP_breg23:
4936         case DW_OP_breg24:
4937         case DW_OP_breg25:
4938         case DW_OP_breg26:
4939         case DW_OP_breg27:
4940         case DW_OP_breg28:
4941         case DW_OP_breg29:
4942         case DW_OP_breg30:
4943         case DW_OP_breg31:
4944           cfa->reg = op - DW_OP_breg0;
4945           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4946           break;
4947         case DW_OP_bregx:
4948           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4949           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4950           break;
4951         case DW_OP_deref:
4952           cfa->indirect = 1;
4953           break;
4954         case DW_OP_plus_uconst:
4955           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4956           break;
4957         default:
4958           internal_error ("DW_LOC_OP %s not implemented",
4959                           dwarf_stack_op_name (ptr->dw_loc_opc));
4960         }
4961     }
4962 }
4963 #endif /* .debug_frame support */
4964 \f
4965 /* And now, the support for symbolic debugging information.  */
4966 #ifdef DWARF2_DEBUGGING_INFO
4967
4968 /* .debug_str support.  */
4969 static int output_indirect_string (void **, void *);
4970
4971 static void dwarf2out_init (const char *);
4972 static void dwarf2out_finish (const char *);
4973 static void dwarf2out_define (unsigned int, const char *);
4974 static void dwarf2out_undef (unsigned int, const char *);
4975 static void dwarf2out_start_source_file (unsigned, const char *);
4976 static void dwarf2out_end_source_file (unsigned);
4977 static void dwarf2out_begin_block (unsigned, unsigned);
4978 static void dwarf2out_end_block (unsigned, unsigned);
4979 static bool dwarf2out_ignore_block (const_tree);
4980 static void dwarf2out_global_decl (tree);
4981 static void dwarf2out_type_decl (tree, int);
4982 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4983 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4984                                                  dw_die_ref);
4985 static void dwarf2out_abstract_function (tree);
4986 static void dwarf2out_var_location (rtx);
4987 static void dwarf2out_begin_function (tree);
4988 static void dwarf2out_set_name (tree, tree);
4989
4990 /* The debug hooks structure.  */
4991
4992 const struct gcc_debug_hooks dwarf2_debug_hooks =
4993 {
4994   dwarf2out_init,
4995   dwarf2out_finish,
4996   dwarf2out_define,
4997   dwarf2out_undef,
4998   dwarf2out_start_source_file,
4999   dwarf2out_end_source_file,
5000   dwarf2out_begin_block,
5001   dwarf2out_end_block,
5002   dwarf2out_ignore_block,
5003   dwarf2out_source_line,
5004   dwarf2out_begin_prologue,
5005   debug_nothing_int_charstar,   /* end_prologue */
5006   dwarf2out_end_epilogue,
5007   dwarf2out_begin_function,
5008   debug_nothing_int,            /* end_function */
5009   dwarf2out_decl,               /* function_decl */
5010   dwarf2out_global_decl,
5011   dwarf2out_type_decl,          /* type_decl */
5012   dwarf2out_imported_module_or_decl,
5013   debug_nothing_tree,           /* deferred_inline_function */
5014   /* The DWARF 2 backend tries to reduce debugging bloat by not
5015      emitting the abstract description of inline functions until
5016      something tries to reference them.  */
5017   dwarf2out_abstract_function,  /* outlining_inline_function */
5018   debug_nothing_rtx,            /* label */
5019   debug_nothing_int,            /* handle_pch */
5020   dwarf2out_var_location,
5021   dwarf2out_switch_text_section,
5022   dwarf2out_set_name,
5023   1                             /* start_end_main_source_file */
5024 };
5025 #endif
5026 \f
5027 /* NOTE: In the comments in this file, many references are made to
5028    "Debugging Information Entries".  This term is abbreviated as `DIE'
5029    throughout the remainder of this file.  */
5030
5031 /* An internal representation of the DWARF output is built, and then
5032    walked to generate the DWARF debugging info.  The walk of the internal
5033    representation is done after the entire program has been compiled.
5034    The types below are used to describe the internal representation.  */
5035
5036 /* Various DIE's use offsets relative to the beginning of the
5037    .debug_info section to refer to each other.  */
5038
5039 typedef long int dw_offset;
5040
5041 /* Define typedefs here to avoid circular dependencies.  */
5042
5043 typedef struct dw_attr_struct *dw_attr_ref;
5044 typedef struct dw_line_info_struct *dw_line_info_ref;
5045 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5046 typedef struct pubname_struct *pubname_ref;
5047 typedef struct dw_ranges_struct *dw_ranges_ref;
5048 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5049
5050 /* Each entry in the line_info_table maintains the file and
5051    line number associated with the label generated for that
5052    entry.  The label gives the PC value associated with
5053    the line number entry.  */
5054
5055 typedef struct GTY(()) dw_line_info_struct {
5056   unsigned long dw_file_num;
5057   unsigned long dw_line_num;
5058 }
5059 dw_line_info_entry;
5060
5061 /* Line information for functions in separate sections; each one gets its
5062    own sequence.  */
5063 typedef struct GTY(()) dw_separate_line_info_struct {
5064   unsigned long dw_file_num;
5065   unsigned long dw_line_num;
5066   unsigned long function;
5067 }
5068 dw_separate_line_info_entry;
5069
5070 /* Each DIE attribute has a field specifying the attribute kind,
5071    a link to the next attribute in the chain, and an attribute value.
5072    Attributes are typically linked below the DIE they modify.  */
5073
5074 typedef struct GTY(()) dw_attr_struct {
5075   enum dwarf_attribute dw_attr;
5076   dw_val_node dw_attr_val;
5077 }
5078 dw_attr_node;
5079
5080 DEF_VEC_O(dw_attr_node);
5081 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5082
5083 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5084    The children of each node form a circular list linked by
5085    die_sib.  die_child points to the node *before* the "first" child node.  */
5086
5087 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5088   enum dwarf_tag die_tag;
5089   char *die_symbol;
5090   VEC(dw_attr_node,gc) * die_attr;
5091   dw_die_ref die_parent;
5092   dw_die_ref die_child;
5093   dw_die_ref die_sib;
5094   dw_die_ref die_definition; /* ref from a specification to its definition */
5095   dw_offset die_offset;
5096   unsigned long die_abbrev;
5097   int die_mark;
5098   /* Die is used and must not be pruned as unused.  */
5099   int die_perennial_p;
5100   unsigned int decl_id;
5101 }
5102 die_node;
5103
5104 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5105 #define FOR_EACH_CHILD(die, c, expr) do {       \
5106   c = die->die_child;                           \
5107   if (c) do {                                   \
5108     c = c->die_sib;                             \
5109     expr;                                       \
5110   } while (c != die->die_child);                \
5111 } while (0)
5112
5113 /* The pubname structure */
5114
5115 typedef struct GTY(()) pubname_struct {
5116   dw_die_ref die;
5117   const char *name;
5118 }
5119 pubname_entry;
5120
5121 DEF_VEC_O(pubname_entry);
5122 DEF_VEC_ALLOC_O(pubname_entry, gc);
5123
5124 struct GTY(()) dw_ranges_struct {
5125   /* If this is positive, it's a block number, otherwise it's a
5126      bitwise-negated index into dw_ranges_by_label.  */
5127   int num;
5128 };
5129
5130 struct GTY(()) dw_ranges_by_label_struct {
5131   const char *begin;
5132   const char *end;
5133 };
5134
5135 /* The limbo die list structure.  */
5136 typedef struct GTY(()) limbo_die_struct {
5137   dw_die_ref die;
5138   tree created_for;
5139   struct limbo_die_struct *next;
5140 }
5141 limbo_die_node;
5142
5143 /* How to start an assembler comment.  */
5144 #ifndef ASM_COMMENT_START
5145 #define ASM_COMMENT_START ";#"
5146 #endif
5147
5148 /* Define a macro which returns nonzero for a TYPE_DECL which was
5149    implicitly generated for a tagged type.
5150
5151    Note that unlike the gcc front end (which generates a NULL named
5152    TYPE_DECL node for each complete tagged type, each array type, and
5153    each function type node created) the g++ front end generates a
5154    _named_ TYPE_DECL node for each tagged type node created.
5155    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5156    generate a DW_TAG_typedef DIE for them.  */
5157
5158 #define TYPE_DECL_IS_STUB(decl)                         \
5159   (DECL_NAME (decl) == NULL_TREE                        \
5160    || (DECL_ARTIFICIAL (decl)                           \
5161        && is_tagged_type (TREE_TYPE (decl))             \
5162        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5163            /* This is necessary for stub decls that     \
5164               appear in nested inline functions.  */    \
5165            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5166                && (decl_ultimate_origin (decl)          \
5167                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5168
5169 /* Information concerning the compilation unit's programming
5170    language, and compiler version.  */
5171
5172 /* Fixed size portion of the DWARF compilation unit header.  */
5173 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5174   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5175
5176 /* Fixed size portion of public names info.  */
5177 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5178
5179 /* Fixed size portion of the address range info.  */
5180 #define DWARF_ARANGES_HEADER_SIZE                                       \
5181   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5182                 DWARF2_ADDR_SIZE * 2)                                   \
5183    - DWARF_INITIAL_LENGTH_SIZE)
5184
5185 /* Size of padding portion in the address range info.  It must be
5186    aligned to twice the pointer size.  */
5187 #define DWARF_ARANGES_PAD_SIZE \
5188   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5189                 DWARF2_ADDR_SIZE * 2)                              \
5190    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5191
5192 /* Use assembler line directives if available.  */
5193 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5194 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5195 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5196 #else
5197 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5198 #endif
5199 #endif
5200
5201 /* Minimum line offset in a special line info. opcode.
5202    This value was chosen to give a reasonable range of values.  */
5203 #define DWARF_LINE_BASE  -10
5204
5205 /* First special line opcode - leave room for the standard opcodes.  */
5206 #define DWARF_LINE_OPCODE_BASE  10
5207
5208 /* Range of line offsets in a special line info. opcode.  */
5209 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5210
5211 /* Flag that indicates the initial value of the is_stmt_start flag.
5212    In the present implementation, we do not mark any lines as
5213    the beginning of a source statement, because that information
5214    is not made available by the GCC front-end.  */
5215 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5216
5217 #ifdef DWARF2_DEBUGGING_INFO
5218 /* This location is used by calc_die_sizes() to keep track
5219    the offset of each DIE within the .debug_info section.  */
5220 static unsigned long next_die_offset;
5221 #endif
5222
5223 /* Record the root of the DIE's built for the current compilation unit.  */
5224 static GTY(()) dw_die_ref comp_unit_die;
5225
5226 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5227 static GTY(()) limbo_die_node *limbo_die_list;
5228
5229 /* A list of DIEs for which we may have to generate
5230    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5231    set.  */
5232 static GTY(()) limbo_die_node *deferred_asm_name;
5233
5234 /* Filenames referenced by this compilation unit.  */
5235 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5236
5237 /* A hash table of references to DIE's that describe declarations.
5238    The key is a DECL_UID() which is a unique number identifying each decl.  */
5239 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5240
5241 /* A hash table of references to DIE's that describe COMMON blocks.
5242    The key is DECL_UID() ^ die_parent.  */
5243 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5244
5245 /* Node of the variable location list.  */
5246 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5247   rtx GTY (()) var_loc_note;
5248   const char * GTY (()) label;
5249   const char * GTY (()) section_label;
5250   struct var_loc_node * GTY (()) next;
5251 };
5252
5253 /* Variable location list.  */
5254 struct GTY (()) var_loc_list_def {
5255   struct var_loc_node * GTY (()) first;
5256
5257   /* Do not mark the last element of the chained list because
5258      it is marked through the chain.  */
5259   struct var_loc_node * GTY ((skip ("%h"))) last;
5260
5261   /* DECL_UID of the variable decl.  */
5262   unsigned int decl_id;
5263 };
5264 typedef struct var_loc_list_def var_loc_list;
5265
5266
5267 /* Table of decl location linked lists.  */
5268 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5269
5270 /* A pointer to the base of a list of references to DIE's that
5271    are uniquely identified by their tag, presence/absence of
5272    children DIE's, and list of attribute/value pairs.  */
5273 static GTY((length ("abbrev_die_table_allocated")))
5274   dw_die_ref *abbrev_die_table;
5275
5276 /* Number of elements currently allocated for abbrev_die_table.  */
5277 static GTY(()) unsigned abbrev_die_table_allocated;
5278
5279 /* Number of elements in type_die_table currently in use.  */
5280 static GTY(()) unsigned abbrev_die_table_in_use;
5281
5282 /* Size (in elements) of increments by which we may expand the
5283    abbrev_die_table.  */
5284 #define ABBREV_DIE_TABLE_INCREMENT 256
5285
5286 /* A pointer to the base of a table that contains line information
5287    for each source code line in .text in the compilation unit.  */
5288 static GTY((length ("line_info_table_allocated")))
5289      dw_line_info_ref line_info_table;
5290
5291 /* Number of elements currently allocated for line_info_table.  */
5292 static GTY(()) unsigned line_info_table_allocated;
5293
5294 /* Number of elements in line_info_table currently in use.  */
5295 static GTY(()) unsigned line_info_table_in_use;
5296
5297 /* A pointer to the base of a table that contains line information
5298    for each source code line outside of .text in the compilation unit.  */
5299 static GTY ((length ("separate_line_info_table_allocated")))
5300      dw_separate_line_info_ref separate_line_info_table;
5301
5302 /* Number of elements currently allocated for separate_line_info_table.  */
5303 static GTY(()) unsigned separate_line_info_table_allocated;
5304
5305 /* Number of elements in separate_line_info_table currently in use.  */
5306 static GTY(()) unsigned separate_line_info_table_in_use;
5307
5308 /* Size (in elements) of increments by which we may expand the
5309    line_info_table.  */
5310 #define LINE_INFO_TABLE_INCREMENT 1024
5311
5312 /* A pointer to the base of a table that contains a list of publicly
5313    accessible names.  */
5314 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5315
5316 /* A pointer to the base of a table that contains a list of publicly
5317    accessible types.  */
5318 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5319
5320 /* Array of dies for which we should generate .debug_arange info.  */
5321 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5322
5323 /* Number of elements currently allocated for arange_table.  */
5324 static GTY(()) unsigned arange_table_allocated;
5325
5326 /* Number of elements in arange_table currently in use.  */
5327 static GTY(()) unsigned arange_table_in_use;
5328
5329 /* Size (in elements) of increments by which we may expand the
5330    arange_table.  */
5331 #define ARANGE_TABLE_INCREMENT 64
5332
5333 /* Array of dies for which we should generate .debug_ranges info.  */
5334 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5335
5336 /* Number of elements currently allocated for ranges_table.  */
5337 static GTY(()) unsigned ranges_table_allocated;
5338
5339 /* Number of elements in ranges_table currently in use.  */
5340 static GTY(()) unsigned ranges_table_in_use;
5341
5342 /* Array of pairs of labels referenced in ranges_table.  */
5343 static GTY ((length ("ranges_by_label_allocated")))
5344      dw_ranges_by_label_ref ranges_by_label;
5345
5346 /* Number of elements currently allocated for ranges_by_label.  */
5347 static GTY(()) unsigned ranges_by_label_allocated;
5348
5349 /* Number of elements in ranges_by_label currently in use.  */
5350 static GTY(()) unsigned ranges_by_label_in_use;
5351
5352 /* Size (in elements) of increments by which we may expand the
5353    ranges_table.  */
5354 #define RANGES_TABLE_INCREMENT 64
5355
5356 /* Whether we have location lists that need outputting */
5357 static GTY(()) bool have_location_lists;
5358
5359 /* Unique label counter.  */
5360 static GTY(()) unsigned int loclabel_num;
5361
5362 #ifdef DWARF2_DEBUGGING_INFO
5363 /* Record whether the function being analyzed contains inlined functions.  */
5364 static int current_function_has_inlines;
5365 #endif
5366 #if 0 && defined (MIPS_DEBUGGING_INFO)
5367 static int comp_unit_has_inlines;
5368 #endif
5369
5370 /* The last file entry emitted by maybe_emit_file().  */
5371 static GTY(()) struct dwarf_file_data * last_emitted_file;
5372
5373 /* Number of internal labels generated by gen_internal_sym().  */
5374 static GTY(()) int label_num;
5375
5376 /* Cached result of previous call to lookup_filename.  */
5377 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5378
5379 #ifdef DWARF2_DEBUGGING_INFO
5380
5381 /* Offset from the "steady-state frame pointer" to the frame base,
5382    within the current function.  */
5383 static HOST_WIDE_INT frame_pointer_fb_offset;
5384
5385 /* Forward declarations for functions defined in this file.  */
5386
5387 static int is_pseudo_reg (const_rtx);
5388 static tree type_main_variant (tree);
5389 static int is_tagged_type (const_tree);
5390 static const char *dwarf_tag_name (unsigned);
5391 static const char *dwarf_attr_name (unsigned);
5392 static const char *dwarf_form_name (unsigned);
5393 static tree decl_ultimate_origin (const_tree);
5394 static tree decl_class_context (tree);
5395 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5396 static inline enum dw_val_class AT_class (dw_attr_ref);
5397 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5398 static inline unsigned AT_flag (dw_attr_ref);
5399 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5400 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5401 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5402 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5403 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5404                               unsigned long);
5405 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5406                                unsigned int, unsigned char *);
5407 static hashval_t debug_str_do_hash (const void *);
5408 static int debug_str_eq (const void *, const void *);
5409 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5410 static inline const char *AT_string (dw_attr_ref);
5411 static enum dwarf_form AT_string_form (dw_attr_ref);
5412 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5413 static void add_AT_specification (dw_die_ref, dw_die_ref);
5414 static inline dw_die_ref AT_ref (dw_attr_ref);
5415 static inline int AT_ref_external (dw_attr_ref);
5416 static inline void set_AT_ref_external (dw_attr_ref, int);
5417 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5418 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5419 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5420 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5421                              dw_loc_list_ref);
5422 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5423 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5424 static inline rtx AT_addr (dw_attr_ref);
5425 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5426 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5427 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5428 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5429                            unsigned HOST_WIDE_INT);
5430 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5431                                unsigned long);
5432 static inline const char *AT_lbl (dw_attr_ref);
5433 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5434 static const char *get_AT_low_pc (dw_die_ref);
5435 static const char *get_AT_hi_pc (dw_die_ref);
5436 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5437 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5438 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5439 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5440 static bool is_c_family (void);
5441 static bool is_cxx (void);
5442 static bool is_java (void);
5443 static bool is_fortran (void);
5444 static bool is_ada (void);
5445 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5446 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5447 static void add_child_die (dw_die_ref, dw_die_ref);
5448 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5449 static dw_die_ref lookup_type_die (tree);
5450 static void equate_type_number_to_die (tree, dw_die_ref);
5451 static hashval_t decl_die_table_hash (const void *);
5452 static int decl_die_table_eq (const void *, const void *);
5453 static dw_die_ref lookup_decl_die (tree);
5454 static hashval_t common_block_die_table_hash (const void *);
5455 static int common_block_die_table_eq (const void *, const void *);
5456 static hashval_t decl_loc_table_hash (const void *);
5457 static int decl_loc_table_eq (const void *, const void *);
5458 static var_loc_list *lookup_decl_loc (const_tree);
5459 static void equate_decl_number_to_die (tree, dw_die_ref);
5460 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5461 static void print_spaces (FILE *);
5462 static void print_die (dw_die_ref, FILE *);
5463 static void print_dwarf_line_table (FILE *);
5464 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5465 static dw_die_ref pop_compile_unit (dw_die_ref);
5466 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5467 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5468 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5469 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5470 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5471 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5472 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5473 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5474 static void compute_section_prefix (dw_die_ref);
5475 static int is_type_die (dw_die_ref);
5476 static int is_comdat_die (dw_die_ref);
5477 static int is_symbol_die (dw_die_ref);
5478 static void assign_symbol_names (dw_die_ref);
5479 static void break_out_includes (dw_die_ref);
5480 static hashval_t htab_cu_hash (const void *);
5481 static int htab_cu_eq (const void *, const void *);
5482 static void htab_cu_del (void *);
5483 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5484 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5485 static void add_sibling_attributes (dw_die_ref);
5486 static void build_abbrev_table (dw_die_ref);
5487 static void output_location_lists (dw_die_ref);
5488 static int constant_size (unsigned HOST_WIDE_INT);
5489 static unsigned long size_of_die (dw_die_ref);
5490 static void calc_die_sizes (dw_die_ref);
5491 static void mark_dies (dw_die_ref);
5492 static void unmark_dies (dw_die_ref);
5493 static void unmark_all_dies (dw_die_ref);
5494 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5495 static unsigned long size_of_aranges (void);
5496 static enum dwarf_form value_format (dw_attr_ref);
5497 static void output_value_format (dw_attr_ref);
5498 static void output_abbrev_section (void);
5499 static void output_die_symbol (dw_die_ref);
5500 static void output_die (dw_die_ref);
5501 static void output_compilation_unit_header (void);
5502 static void output_comp_unit (dw_die_ref, int);
5503 static const char *dwarf2_name (tree, int);
5504 static void add_pubname (tree, dw_die_ref);
5505 static void add_pubname_string (const char *, dw_die_ref);
5506 static void add_pubtype (tree, dw_die_ref);
5507 static void output_pubnames (VEC (pubname_entry,gc) *);
5508 static void add_arange (tree, dw_die_ref);
5509 static void output_aranges (void);
5510 static unsigned int add_ranges_num (int);
5511 static unsigned int add_ranges (const_tree);
5512 static unsigned int add_ranges_by_labels (const char *, const char *);
5513 static void output_ranges (void);
5514 static void output_line_info (void);
5515 static void output_file_names (void);
5516 static dw_die_ref base_type_die (tree);
5517 static int is_base_type (tree);
5518 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5519 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5520 static int type_is_enum (const_tree);
5521 static unsigned int dbx_reg_number (const_rtx);
5522 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5523 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5524 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5525                                                 enum var_init_status);
5526 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5527                                                      enum var_init_status);
5528 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5529                                          enum var_init_status);
5530 static int is_based_loc (const_rtx);
5531 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5532                                             enum var_init_status);
5533 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5534                                                enum var_init_status);
5535 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5536 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5537 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5538 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5539 static tree field_type (const_tree);
5540 static unsigned int simple_type_align_in_bits (const_tree);
5541 static unsigned int simple_decl_align_in_bits (const_tree);
5542 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5543 static HOST_WIDE_INT field_byte_offset (const_tree);
5544 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5545                                          dw_loc_descr_ref);
5546 static void add_data_member_location_attribute (dw_die_ref, tree);
5547 static void add_const_value_attribute (dw_die_ref, rtx);
5548 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5549 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5550 static void insert_float (const_rtx, unsigned char *);
5551 static rtx rtl_for_decl_location (tree);
5552 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5553                                                    enum dwarf_attribute);
5554 static void tree_add_const_value_attribute (dw_die_ref, tree);
5555 static void add_name_attribute (dw_die_ref, const char *);
5556 static void add_comp_dir_attribute (dw_die_ref);
5557 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5558 static void add_subscript_info (dw_die_ref, tree, bool);
5559 static void add_byte_size_attribute (dw_die_ref, tree);
5560 static void add_bit_offset_attribute (dw_die_ref, tree);
5561 static void add_bit_size_attribute (dw_die_ref, tree);
5562 static void add_prototyped_attribute (dw_die_ref, tree);
5563 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5564 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5565 static void add_src_coords_attributes (dw_die_ref, tree);
5566 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5567 static void push_decl_scope (tree);
5568 static void pop_decl_scope (void);
5569 static dw_die_ref scope_die_for (tree, dw_die_ref);
5570 static inline int local_scope_p (dw_die_ref);
5571 static inline int class_scope_p (dw_die_ref);
5572 static inline int class_or_namespace_scope_p (dw_die_ref);
5573 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5574 static void add_calling_convention_attribute (dw_die_ref, tree);
5575 static const char *type_tag (const_tree);
5576 static tree member_declared_type (const_tree);
5577 #if 0
5578 static const char *decl_start_label (tree);
5579 #endif
5580 static void gen_array_type_die (tree, dw_die_ref);
5581 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5582 #if 0
5583 static void gen_entry_point_die (tree, dw_die_ref);
5584 #endif
5585 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5586 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5587 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5588 static void gen_formal_types_die (tree, dw_die_ref);
5589 static void gen_subprogram_die (tree, dw_die_ref);
5590 static void gen_variable_die (tree, tree, dw_die_ref);
5591 static void gen_const_die (tree, dw_die_ref);
5592 static void gen_label_die (tree, dw_die_ref);
5593 static void gen_lexical_block_die (tree, dw_die_ref, int);
5594 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5595 static void gen_field_die (tree, dw_die_ref);
5596 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5597 static dw_die_ref gen_compile_unit_die (const char *);
5598 static void gen_inheritance_die (tree, tree, dw_die_ref);
5599 static void gen_member_die (tree, dw_die_ref);
5600 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5601                                                 enum debug_info_usage);
5602 static void gen_subroutine_type_die (tree, dw_die_ref);
5603 static void gen_typedef_die (tree, dw_die_ref);
5604 static void gen_type_die (tree, dw_die_ref);
5605 static void gen_block_die (tree, dw_die_ref, int);
5606 static void decls_for_scope (tree, dw_die_ref, int);
5607 static int is_redundant_typedef (const_tree);
5608 static void gen_namespace_die (tree, dw_die_ref);
5609 static void gen_decl_die (tree, tree, dw_die_ref);
5610 static dw_die_ref force_decl_die (tree);
5611 static dw_die_ref force_type_die (tree);
5612 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5613 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5614 static struct dwarf_file_data * lookup_filename (const char *);
5615 static void retry_incomplete_types (void);
5616 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5617 static void splice_child_die (dw_die_ref, dw_die_ref);
5618 static int file_info_cmp (const void *, const void *);
5619 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5620                                      const char *, const char *, unsigned);
5621 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5622                                        const char *, const char *,
5623                                        const char *);
5624 static void output_loc_list (dw_loc_list_ref);
5625 static char *gen_internal_sym (const char *);
5626
5627 static void prune_unmark_dies (dw_die_ref);
5628 static void prune_unused_types_mark (dw_die_ref, int);
5629 static void prune_unused_types_walk (dw_die_ref);
5630 static void prune_unused_types_walk_attribs (dw_die_ref);
5631 static void prune_unused_types_prune (dw_die_ref);
5632 static void prune_unused_types (void);
5633 static int maybe_emit_file (struct dwarf_file_data *fd);
5634
5635 /* Section names used to hold DWARF debugging information.  */
5636 #ifndef DEBUG_INFO_SECTION
5637 #define DEBUG_INFO_SECTION      ".debug_info"
5638 #endif
5639 #ifndef DEBUG_ABBREV_SECTION
5640 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5641 #endif
5642 #ifndef DEBUG_ARANGES_SECTION
5643 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5644 #endif
5645 #ifndef DEBUG_MACINFO_SECTION
5646 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5647 #endif
5648 #ifndef DEBUG_LINE_SECTION
5649 #define DEBUG_LINE_SECTION      ".debug_line"
5650 #endif
5651 #ifndef DEBUG_LOC_SECTION
5652 #define DEBUG_LOC_SECTION       ".debug_loc"
5653 #endif
5654 #ifndef DEBUG_PUBNAMES_SECTION
5655 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5656 #endif
5657 #ifndef DEBUG_PUBTYPES_SECTION
5658 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
5659 #endif
5660 #ifndef DEBUG_STR_SECTION
5661 #define DEBUG_STR_SECTION       ".debug_str"
5662 #endif
5663 #ifndef DEBUG_RANGES_SECTION
5664 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5665 #endif
5666
5667 /* Standard ELF section names for compiled code and data.  */
5668 #ifndef TEXT_SECTION_NAME
5669 #define TEXT_SECTION_NAME       ".text"
5670 #endif
5671
5672 /* Section flags for .debug_str section.  */
5673 #define DEBUG_STR_SECTION_FLAGS \
5674   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5675    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5676    : SECTION_DEBUG)
5677
5678 /* Labels we insert at beginning sections we can reference instead of
5679    the section names themselves.  */
5680
5681 #ifndef TEXT_SECTION_LABEL
5682 #define TEXT_SECTION_LABEL              "Ltext"
5683 #endif
5684 #ifndef COLD_TEXT_SECTION_LABEL
5685 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5686 #endif
5687 #ifndef DEBUG_LINE_SECTION_LABEL
5688 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5689 #endif
5690 #ifndef DEBUG_INFO_SECTION_LABEL
5691 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5692 #endif
5693 #ifndef DEBUG_ABBREV_SECTION_LABEL
5694 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5695 #endif
5696 #ifndef DEBUG_LOC_SECTION_LABEL
5697 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5698 #endif
5699 #ifndef DEBUG_RANGES_SECTION_LABEL
5700 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5701 #endif
5702 #ifndef DEBUG_MACINFO_SECTION_LABEL
5703 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5704 #endif
5705
5706 /* Definitions of defaults for formats and names of various special
5707    (artificial) labels which may be generated within this file (when the -g
5708    options is used and DWARF2_DEBUGGING_INFO is in effect.
5709    If necessary, these may be overridden from within the tm.h file, but
5710    typically, overriding these defaults is unnecessary.  */
5711
5712 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5713 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5714 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5715 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5716 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5717 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5718 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5719 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5720 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5721 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5722
5723 #ifndef TEXT_END_LABEL
5724 #define TEXT_END_LABEL          "Letext"
5725 #endif
5726 #ifndef COLD_END_LABEL
5727 #define COLD_END_LABEL          "Letext_cold"
5728 #endif
5729 #ifndef BLOCK_BEGIN_LABEL
5730 #define BLOCK_BEGIN_LABEL       "LBB"
5731 #endif
5732 #ifndef BLOCK_END_LABEL
5733 #define BLOCK_END_LABEL         "LBE"
5734 #endif
5735 #ifndef LINE_CODE_LABEL
5736 #define LINE_CODE_LABEL         "LM"
5737 #endif
5738 #ifndef SEPARATE_LINE_CODE_LABEL
5739 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5740 #endif
5741
5742 \f
5743 /* We allow a language front-end to designate a function that is to be
5744    called to "demangle" any name before it is put into a DIE.  */
5745
5746 static const char *(*demangle_name_func) (const char *);
5747
5748 void
5749 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5750 {
5751   demangle_name_func = func;
5752 }
5753
5754 /* Test if rtl node points to a pseudo register.  */
5755
5756 static inline int
5757 is_pseudo_reg (const_rtx rtl)
5758 {
5759   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5760           || (GET_CODE (rtl) == SUBREG
5761               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5762 }
5763
5764 /* Return a reference to a type, with its const and volatile qualifiers
5765    removed.  */
5766
5767 static inline tree
5768 type_main_variant (tree type)
5769 {
5770   type = TYPE_MAIN_VARIANT (type);
5771
5772   /* ??? There really should be only one main variant among any group of
5773      variants of a given type (and all of the MAIN_VARIANT values for all
5774      members of the group should point to that one type) but sometimes the C
5775      front-end messes this up for array types, so we work around that bug
5776      here.  */
5777   if (TREE_CODE (type) == ARRAY_TYPE)
5778     while (type != TYPE_MAIN_VARIANT (type))
5779       type = TYPE_MAIN_VARIANT (type);
5780
5781   return type;
5782 }
5783
5784 /* Return nonzero if the given type node represents a tagged type.  */
5785
5786 static inline int
5787 is_tagged_type (const_tree type)
5788 {
5789   enum tree_code code = TREE_CODE (type);
5790
5791   return (code == RECORD_TYPE || code == UNION_TYPE
5792           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5793 }
5794
5795 /* Convert a DIE tag into its string name.  */
5796
5797 static const char *
5798 dwarf_tag_name (unsigned int tag)
5799 {
5800   switch (tag)
5801     {
5802     case DW_TAG_padding:
5803       return "DW_TAG_padding";
5804     case DW_TAG_array_type:
5805       return "DW_TAG_array_type";
5806     case DW_TAG_class_type:
5807       return "DW_TAG_class_type";
5808     case DW_TAG_entry_point:
5809       return "DW_TAG_entry_point";
5810     case DW_TAG_enumeration_type:
5811       return "DW_TAG_enumeration_type";
5812     case DW_TAG_formal_parameter:
5813       return "DW_TAG_formal_parameter";
5814     case DW_TAG_imported_declaration:
5815       return "DW_TAG_imported_declaration";
5816     case DW_TAG_label:
5817       return "DW_TAG_label";
5818     case DW_TAG_lexical_block:
5819       return "DW_TAG_lexical_block";
5820     case DW_TAG_member:
5821       return "DW_TAG_member";
5822     case DW_TAG_pointer_type:
5823       return "DW_TAG_pointer_type";
5824     case DW_TAG_reference_type:
5825       return "DW_TAG_reference_type";
5826     case DW_TAG_compile_unit:
5827       return "DW_TAG_compile_unit";
5828     case DW_TAG_string_type:
5829       return "DW_TAG_string_type";
5830     case DW_TAG_structure_type:
5831       return "DW_TAG_structure_type";
5832     case DW_TAG_subroutine_type:
5833       return "DW_TAG_subroutine_type";
5834     case DW_TAG_typedef:
5835       return "DW_TAG_typedef";
5836     case DW_TAG_union_type:
5837       return "DW_TAG_union_type";
5838     case DW_TAG_unspecified_parameters:
5839       return "DW_TAG_unspecified_parameters";
5840     case DW_TAG_variant:
5841       return "DW_TAG_variant";
5842     case DW_TAG_common_block:
5843       return "DW_TAG_common_block";
5844     case DW_TAG_common_inclusion:
5845       return "DW_TAG_common_inclusion";
5846     case DW_TAG_inheritance:
5847       return "DW_TAG_inheritance";
5848     case DW_TAG_inlined_subroutine:
5849       return "DW_TAG_inlined_subroutine";
5850     case DW_TAG_module:
5851       return "DW_TAG_module";
5852     case DW_TAG_ptr_to_member_type:
5853       return "DW_TAG_ptr_to_member_type";
5854     case DW_TAG_set_type:
5855       return "DW_TAG_set_type";
5856     case DW_TAG_subrange_type:
5857       return "DW_TAG_subrange_type";
5858     case DW_TAG_with_stmt:
5859       return "DW_TAG_with_stmt";
5860     case DW_TAG_access_declaration:
5861       return "DW_TAG_access_declaration";
5862     case DW_TAG_base_type:
5863       return "DW_TAG_base_type";
5864     case DW_TAG_catch_block:
5865       return "DW_TAG_catch_block";
5866     case DW_TAG_const_type:
5867       return "DW_TAG_const_type";
5868     case DW_TAG_constant:
5869       return "DW_TAG_constant";
5870     case DW_TAG_enumerator:
5871       return "DW_TAG_enumerator";
5872     case DW_TAG_file_type:
5873       return "DW_TAG_file_type";
5874     case DW_TAG_friend:
5875       return "DW_TAG_friend";
5876     case DW_TAG_namelist:
5877       return "DW_TAG_namelist";
5878     case DW_TAG_namelist_item:
5879       return "DW_TAG_namelist_item";
5880     case DW_TAG_packed_type:
5881       return "DW_TAG_packed_type";
5882     case DW_TAG_subprogram:
5883       return "DW_TAG_subprogram";
5884     case DW_TAG_template_type_param:
5885       return "DW_TAG_template_type_param";
5886     case DW_TAG_template_value_param:
5887       return "DW_TAG_template_value_param";
5888     case DW_TAG_thrown_type:
5889       return "DW_TAG_thrown_type";
5890     case DW_TAG_try_block:
5891       return "DW_TAG_try_block";
5892     case DW_TAG_variant_part:
5893       return "DW_TAG_variant_part";
5894     case DW_TAG_variable:
5895       return "DW_TAG_variable";
5896     case DW_TAG_volatile_type:
5897       return "DW_TAG_volatile_type";
5898     case DW_TAG_dwarf_procedure:
5899       return "DW_TAG_dwarf_procedure";
5900     case DW_TAG_restrict_type:
5901       return "DW_TAG_restrict_type";
5902     case DW_TAG_interface_type:
5903       return "DW_TAG_interface_type";
5904     case DW_TAG_namespace:
5905       return "DW_TAG_namespace";
5906     case DW_TAG_imported_module:
5907       return "DW_TAG_imported_module";
5908     case DW_TAG_unspecified_type:
5909       return "DW_TAG_unspecified_type";
5910     case DW_TAG_partial_unit:
5911       return "DW_TAG_partial_unit";
5912     case DW_TAG_imported_unit:
5913       return "DW_TAG_imported_unit";
5914     case DW_TAG_condition:
5915       return "DW_TAG_condition";
5916     case DW_TAG_shared_type:
5917       return "DW_TAG_shared_type";
5918     case DW_TAG_MIPS_loop:
5919       return "DW_TAG_MIPS_loop";
5920     case DW_TAG_format_label:
5921       return "DW_TAG_format_label";
5922     case DW_TAG_function_template:
5923       return "DW_TAG_function_template";
5924     case DW_TAG_class_template:
5925       return "DW_TAG_class_template";
5926     case DW_TAG_GNU_BINCL:
5927       return "DW_TAG_GNU_BINCL";
5928     case DW_TAG_GNU_EINCL:
5929       return "DW_TAG_GNU_EINCL";
5930     default:
5931       return "DW_TAG_<unknown>";
5932     }
5933 }
5934
5935 /* Convert a DWARF attribute code into its string name.  */
5936
5937 static const char *
5938 dwarf_attr_name (unsigned int attr)
5939 {
5940   switch (attr)
5941     {
5942     case DW_AT_sibling:
5943       return "DW_AT_sibling";
5944     case DW_AT_location:
5945       return "DW_AT_location";
5946     case DW_AT_name:
5947       return "DW_AT_name";
5948     case DW_AT_ordering:
5949       return "DW_AT_ordering";
5950     case DW_AT_subscr_data:
5951       return "DW_AT_subscr_data";
5952     case DW_AT_byte_size:
5953       return "DW_AT_byte_size";
5954     case DW_AT_bit_offset:
5955       return "DW_AT_bit_offset";
5956     case DW_AT_bit_size:
5957       return "DW_AT_bit_size";
5958     case DW_AT_element_list:
5959       return "DW_AT_element_list";
5960     case DW_AT_stmt_list:
5961       return "DW_AT_stmt_list";
5962     case DW_AT_low_pc:
5963       return "DW_AT_low_pc";
5964     case DW_AT_high_pc:
5965       return "DW_AT_high_pc";
5966     case DW_AT_language:
5967       return "DW_AT_language";
5968     case DW_AT_member:
5969       return "DW_AT_member";
5970     case DW_AT_discr:
5971       return "DW_AT_discr";
5972     case DW_AT_discr_value:
5973       return "DW_AT_discr_value";
5974     case DW_AT_visibility:
5975       return "DW_AT_visibility";
5976     case DW_AT_import:
5977       return "DW_AT_import";
5978     case DW_AT_string_length:
5979       return "DW_AT_string_length";
5980     case DW_AT_common_reference:
5981       return "DW_AT_common_reference";
5982     case DW_AT_comp_dir:
5983       return "DW_AT_comp_dir";
5984     case DW_AT_const_value:
5985       return "DW_AT_const_value";
5986     case DW_AT_containing_type:
5987       return "DW_AT_containing_type";
5988     case DW_AT_default_value:
5989       return "DW_AT_default_value";
5990     case DW_AT_inline:
5991       return "DW_AT_inline";
5992     case DW_AT_is_optional:
5993       return "DW_AT_is_optional";
5994     case DW_AT_lower_bound:
5995       return "DW_AT_lower_bound";
5996     case DW_AT_producer:
5997       return "DW_AT_producer";
5998     case DW_AT_prototyped:
5999       return "DW_AT_prototyped";
6000     case DW_AT_return_addr:
6001       return "DW_AT_return_addr";
6002     case DW_AT_start_scope:
6003       return "DW_AT_start_scope";
6004     case DW_AT_bit_stride:
6005       return "DW_AT_bit_stride";
6006     case DW_AT_upper_bound:
6007       return "DW_AT_upper_bound";
6008     case DW_AT_abstract_origin:
6009       return "DW_AT_abstract_origin";
6010     case DW_AT_accessibility:
6011       return "DW_AT_accessibility";
6012     case DW_AT_address_class:
6013       return "DW_AT_address_class";
6014     case DW_AT_artificial:
6015       return "DW_AT_artificial";
6016     case DW_AT_base_types:
6017       return "DW_AT_base_types";
6018     case DW_AT_calling_convention:
6019       return "DW_AT_calling_convention";
6020     case DW_AT_count:
6021       return "DW_AT_count";
6022     case DW_AT_data_member_location:
6023       return "DW_AT_data_member_location";
6024     case DW_AT_decl_column:
6025       return "DW_AT_decl_column";
6026     case DW_AT_decl_file:
6027       return "DW_AT_decl_file";
6028     case DW_AT_decl_line:
6029       return "DW_AT_decl_line";
6030     case DW_AT_declaration:
6031       return "DW_AT_declaration";
6032     case DW_AT_discr_list:
6033       return "DW_AT_discr_list";
6034     case DW_AT_encoding:
6035       return "DW_AT_encoding";
6036     case DW_AT_external:
6037       return "DW_AT_external";
6038     case DW_AT_explicit:
6039       return "DW_AT_explicit";
6040     case DW_AT_frame_base:
6041       return "DW_AT_frame_base";
6042     case DW_AT_friend:
6043       return "DW_AT_friend";
6044     case DW_AT_identifier_case:
6045       return "DW_AT_identifier_case";
6046     case DW_AT_macro_info:
6047       return "DW_AT_macro_info";
6048     case DW_AT_namelist_items:
6049       return "DW_AT_namelist_items";
6050     case DW_AT_priority:
6051       return "DW_AT_priority";
6052     case DW_AT_segment:
6053       return "DW_AT_segment";
6054     case DW_AT_specification:
6055       return "DW_AT_specification";
6056     case DW_AT_static_link:
6057       return "DW_AT_static_link";
6058     case DW_AT_type:
6059       return "DW_AT_type";
6060     case DW_AT_use_location:
6061       return "DW_AT_use_location";
6062     case DW_AT_variable_parameter:
6063       return "DW_AT_variable_parameter";
6064     case DW_AT_virtuality:
6065       return "DW_AT_virtuality";
6066     case DW_AT_vtable_elem_location:
6067       return "DW_AT_vtable_elem_location";
6068
6069     case DW_AT_allocated:
6070       return "DW_AT_allocated";
6071     case DW_AT_associated:
6072       return "DW_AT_associated";
6073     case DW_AT_data_location:
6074       return "DW_AT_data_location";
6075     case DW_AT_byte_stride:
6076       return "DW_AT_byte_stride";
6077     case DW_AT_entry_pc:
6078       return "DW_AT_entry_pc";
6079     case DW_AT_use_UTF8:
6080       return "DW_AT_use_UTF8";
6081     case DW_AT_extension:
6082       return "DW_AT_extension";
6083     case DW_AT_ranges:
6084       return "DW_AT_ranges";
6085     case DW_AT_trampoline:
6086       return "DW_AT_trampoline";
6087     case DW_AT_call_column:
6088       return "DW_AT_call_column";
6089     case DW_AT_call_file:
6090       return "DW_AT_call_file";
6091     case DW_AT_call_line:
6092       return "DW_AT_call_line";
6093
6094     case DW_AT_MIPS_fde:
6095       return "DW_AT_MIPS_fde";
6096     case DW_AT_MIPS_loop_begin:
6097       return "DW_AT_MIPS_loop_begin";
6098     case DW_AT_MIPS_tail_loop_begin:
6099       return "DW_AT_MIPS_tail_loop_begin";
6100     case DW_AT_MIPS_epilog_begin:
6101       return "DW_AT_MIPS_epilog_begin";
6102     case DW_AT_MIPS_loop_unroll_factor:
6103       return "DW_AT_MIPS_loop_unroll_factor";
6104     case DW_AT_MIPS_software_pipeline_depth:
6105       return "DW_AT_MIPS_software_pipeline_depth";
6106     case DW_AT_MIPS_linkage_name:
6107       return "DW_AT_MIPS_linkage_name";
6108     case DW_AT_MIPS_stride:
6109       return "DW_AT_MIPS_stride";
6110     case DW_AT_MIPS_abstract_name:
6111       return "DW_AT_MIPS_abstract_name";
6112     case DW_AT_MIPS_clone_origin:
6113       return "DW_AT_MIPS_clone_origin";
6114     case DW_AT_MIPS_has_inlines:
6115       return "DW_AT_MIPS_has_inlines";
6116
6117     case DW_AT_sf_names:
6118       return "DW_AT_sf_names";
6119     case DW_AT_src_info:
6120       return "DW_AT_src_info";
6121     case DW_AT_mac_info:
6122       return "DW_AT_mac_info";
6123     case DW_AT_src_coords:
6124       return "DW_AT_src_coords";
6125     case DW_AT_body_begin:
6126       return "DW_AT_body_begin";
6127     case DW_AT_body_end:
6128       return "DW_AT_body_end";
6129     case DW_AT_GNU_vector:
6130       return "DW_AT_GNU_vector";
6131
6132     case DW_AT_VMS_rtnbeg_pd_address:
6133       return "DW_AT_VMS_rtnbeg_pd_address";
6134
6135     default:
6136       return "DW_AT_<unknown>";
6137     }
6138 }
6139
6140 /* Convert a DWARF value form code into its string name.  */
6141
6142 static const char *
6143 dwarf_form_name (unsigned int form)
6144 {
6145   switch (form)
6146     {
6147     case DW_FORM_addr:
6148       return "DW_FORM_addr";
6149     case DW_FORM_block2:
6150       return "DW_FORM_block2";
6151     case DW_FORM_block4:
6152       return "DW_FORM_block4";
6153     case DW_FORM_data2:
6154       return "DW_FORM_data2";
6155     case DW_FORM_data4:
6156       return "DW_FORM_data4";
6157     case DW_FORM_data8:
6158       return "DW_FORM_data8";
6159     case DW_FORM_string:
6160       return "DW_FORM_string";
6161     case DW_FORM_block:
6162       return "DW_FORM_block";
6163     case DW_FORM_block1:
6164       return "DW_FORM_block1";
6165     case DW_FORM_data1:
6166       return "DW_FORM_data1";
6167     case DW_FORM_flag:
6168       return "DW_FORM_flag";
6169     case DW_FORM_sdata:
6170       return "DW_FORM_sdata";
6171     case DW_FORM_strp:
6172       return "DW_FORM_strp";
6173     case DW_FORM_udata:
6174       return "DW_FORM_udata";
6175     case DW_FORM_ref_addr:
6176       return "DW_FORM_ref_addr";
6177     case DW_FORM_ref1:
6178       return "DW_FORM_ref1";
6179     case DW_FORM_ref2:
6180       return "DW_FORM_ref2";
6181     case DW_FORM_ref4:
6182       return "DW_FORM_ref4";
6183     case DW_FORM_ref8:
6184       return "DW_FORM_ref8";
6185     case DW_FORM_ref_udata:
6186       return "DW_FORM_ref_udata";
6187     case DW_FORM_indirect:
6188       return "DW_FORM_indirect";
6189     default:
6190       return "DW_FORM_<unknown>";
6191     }
6192 }
6193 \f
6194 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6195    instance of an inlined instance of a decl which is local to an inline
6196    function, so we have to trace all of the way back through the origin chain
6197    to find out what sort of node actually served as the original seed for the
6198    given block.  */
6199
6200 static tree
6201 decl_ultimate_origin (const_tree decl)
6202 {
6203   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6204     return NULL_TREE;
6205
6206   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6207      nodes in the function to point to themselves; ignore that if
6208      we're trying to output the abstract instance of this function.  */
6209   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6210     return NULL_TREE;
6211
6212   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6213      most distant ancestor, this should never happen.  */
6214   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6215
6216   return DECL_ABSTRACT_ORIGIN (decl);
6217 }
6218
6219 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6220    of a virtual function may refer to a base class, so we check the 'this'
6221    parameter.  */
6222
6223 static tree
6224 decl_class_context (tree decl)
6225 {
6226   tree context = NULL_TREE;
6227
6228   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6229     context = DECL_CONTEXT (decl);
6230   else
6231     context = TYPE_MAIN_VARIANT
6232       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6233
6234   if (context && !TYPE_P (context))
6235     context = NULL_TREE;
6236
6237   return context;
6238 }
6239 \f
6240 /* Add an attribute/value pair to a DIE.  */
6241
6242 static inline void
6243 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6244 {
6245   /* Maybe this should be an assert?  */
6246   if (die == NULL)
6247     return;
6248
6249   if (die->die_attr == NULL)
6250     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6251   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6252 }
6253
6254 static inline enum dw_val_class
6255 AT_class (dw_attr_ref a)
6256 {
6257   return a->dw_attr_val.val_class;
6258 }
6259
6260 /* Add a flag value attribute to a DIE.  */
6261
6262 static inline void
6263 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6264 {
6265   dw_attr_node attr;
6266
6267   attr.dw_attr = attr_kind;
6268   attr.dw_attr_val.val_class = dw_val_class_flag;
6269   attr.dw_attr_val.v.val_flag = flag;
6270   add_dwarf_attr (die, &attr);
6271 }
6272
6273 static inline unsigned
6274 AT_flag (dw_attr_ref a)
6275 {
6276   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6277   return a->dw_attr_val.v.val_flag;
6278 }
6279
6280 /* Add a signed integer attribute value to a DIE.  */
6281
6282 static inline void
6283 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6284 {
6285   dw_attr_node attr;
6286
6287   attr.dw_attr = attr_kind;
6288   attr.dw_attr_val.val_class = dw_val_class_const;
6289   attr.dw_attr_val.v.val_int = int_val;
6290   add_dwarf_attr (die, &attr);
6291 }
6292
6293 static inline HOST_WIDE_INT
6294 AT_int (dw_attr_ref a)
6295 {
6296   gcc_assert (a && AT_class (a) == dw_val_class_const);
6297   return a->dw_attr_val.v.val_int;
6298 }
6299
6300 /* Add an unsigned integer attribute value to a DIE.  */
6301
6302 static inline void
6303 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6304                  unsigned HOST_WIDE_INT unsigned_val)
6305 {
6306   dw_attr_node attr;
6307
6308   attr.dw_attr = attr_kind;
6309   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6310   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6311   add_dwarf_attr (die, &attr);
6312 }
6313
6314 static inline unsigned HOST_WIDE_INT
6315 AT_unsigned (dw_attr_ref a)
6316 {
6317   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6318   return a->dw_attr_val.v.val_unsigned;
6319 }
6320
6321 /* Add an unsigned double integer attribute value to a DIE.  */
6322
6323 static inline void
6324 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6325                   long unsigned int val_hi, long unsigned int val_low)
6326 {
6327   dw_attr_node attr;
6328
6329   attr.dw_attr = attr_kind;
6330   attr.dw_attr_val.val_class = dw_val_class_long_long;
6331   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6332   attr.dw_attr_val.v.val_long_long.low = val_low;
6333   add_dwarf_attr (die, &attr);
6334 }
6335
6336 /* Add a floating point attribute value to a DIE and return it.  */
6337
6338 static inline void
6339 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6340             unsigned int length, unsigned int elt_size, unsigned char *array)
6341 {
6342   dw_attr_node attr;
6343
6344   attr.dw_attr = attr_kind;
6345   attr.dw_attr_val.val_class = dw_val_class_vec;
6346   attr.dw_attr_val.v.val_vec.length = length;
6347   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6348   attr.dw_attr_val.v.val_vec.array = array;
6349   add_dwarf_attr (die, &attr);
6350 }
6351
6352 /* Hash and equality functions for debug_str_hash.  */
6353
6354 static hashval_t
6355 debug_str_do_hash (const void *x)
6356 {
6357   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6358 }
6359
6360 static int
6361 debug_str_eq (const void *x1, const void *x2)
6362 {
6363   return strcmp ((((const struct indirect_string_node *)x1)->str),
6364                  (const char *)x2) == 0;
6365 }
6366
6367 static struct indirect_string_node *
6368 find_AT_string (const char *str)
6369 {
6370   struct indirect_string_node *node;
6371   void **slot;
6372
6373   if (! debug_str_hash)
6374     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6375                                       debug_str_eq, NULL);
6376
6377   slot = htab_find_slot_with_hash (debug_str_hash, str,
6378                                    htab_hash_string (str), INSERT);
6379   if (*slot == NULL)
6380     {
6381       node = (struct indirect_string_node *)
6382                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6383       node->str = ggc_strdup (str);
6384       *slot = node;
6385     }
6386   else
6387     node = (struct indirect_string_node *) *slot;
6388
6389   node->refcount++;
6390   return node;
6391 }
6392
6393 /* Add a string attribute value to a DIE.  */
6394
6395 static inline void
6396 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6397 {
6398   dw_attr_node attr;
6399   struct indirect_string_node *node;
6400
6401   node = find_AT_string (str);
6402
6403   attr.dw_attr = attr_kind;
6404   attr.dw_attr_val.val_class = dw_val_class_str;
6405   attr.dw_attr_val.v.val_str = node;
6406   add_dwarf_attr (die, &attr);
6407 }
6408
6409 static inline const char *
6410 AT_string (dw_attr_ref a)
6411 {
6412   gcc_assert (a && AT_class (a) == dw_val_class_str);
6413   return a->dw_attr_val.v.val_str->str;
6414 }
6415
6416 /* Find out whether a string should be output inline in DIE
6417    or out-of-line in .debug_str section.  */
6418
6419 static enum dwarf_form
6420 AT_string_form (dw_attr_ref a)
6421 {
6422   struct indirect_string_node *node;
6423   unsigned int len;
6424   char label[32];
6425
6426   gcc_assert (a && AT_class (a) == dw_val_class_str);
6427
6428   node = a->dw_attr_val.v.val_str;
6429   if (node->form)
6430     return node->form;
6431
6432   len = strlen (node->str) + 1;
6433
6434   /* If the string is shorter or equal to the size of the reference, it is
6435      always better to put it inline.  */
6436   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6437     return node->form = DW_FORM_string;
6438
6439   /* If we cannot expect the linker to merge strings in .debug_str
6440      section, only put it into .debug_str if it is worth even in this
6441      single module.  */
6442   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6443       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6444     return node->form = DW_FORM_string;
6445
6446   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6447   ++dw2_string_counter;
6448   node->label = xstrdup (label);
6449
6450   return node->form = DW_FORM_strp;
6451 }
6452
6453 /* Add a DIE reference attribute value to a DIE.  */
6454
6455 static inline void
6456 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6457 {
6458   dw_attr_node attr;
6459
6460   attr.dw_attr = attr_kind;
6461   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6462   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6463   attr.dw_attr_val.v.val_die_ref.external = 0;
6464   add_dwarf_attr (die, &attr);
6465 }
6466
6467 /* Add an AT_specification attribute to a DIE, and also make the back
6468    pointer from the specification to the definition.  */
6469
6470 static inline void
6471 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6472 {
6473   add_AT_die_ref (die, DW_AT_specification, targ_die);
6474   gcc_assert (!targ_die->die_definition);
6475   targ_die->die_definition = die;
6476 }
6477
6478 static inline dw_die_ref
6479 AT_ref (dw_attr_ref a)
6480 {
6481   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6482   return a->dw_attr_val.v.val_die_ref.die;
6483 }
6484
6485 static inline int
6486 AT_ref_external (dw_attr_ref a)
6487 {
6488   if (a && AT_class (a) == dw_val_class_die_ref)
6489     return a->dw_attr_val.v.val_die_ref.external;
6490
6491   return 0;
6492 }
6493
6494 static inline void
6495 set_AT_ref_external (dw_attr_ref a, int i)
6496 {
6497   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6498   a->dw_attr_val.v.val_die_ref.external = i;
6499 }
6500
6501 /* Add an FDE reference attribute value to a DIE.  */
6502
6503 static inline void
6504 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6505 {
6506   dw_attr_node attr;
6507
6508   attr.dw_attr = attr_kind;
6509   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6510   attr.dw_attr_val.v.val_fde_index = targ_fde;
6511   add_dwarf_attr (die, &attr);
6512 }
6513
6514 /* Add a location description attribute value to a DIE.  */
6515
6516 static inline void
6517 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6518 {
6519   dw_attr_node attr;
6520
6521   attr.dw_attr = attr_kind;
6522   attr.dw_attr_val.val_class = dw_val_class_loc;
6523   attr.dw_attr_val.v.val_loc = loc;
6524   add_dwarf_attr (die, &attr);
6525 }
6526
6527 static inline dw_loc_descr_ref
6528 AT_loc (dw_attr_ref a)
6529 {
6530   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6531   return a->dw_attr_val.v.val_loc;
6532 }
6533
6534 static inline void
6535 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6536 {
6537   dw_attr_node attr;
6538
6539   attr.dw_attr = attr_kind;
6540   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6541   attr.dw_attr_val.v.val_loc_list = loc_list;
6542   add_dwarf_attr (die, &attr);
6543   have_location_lists = true;
6544 }
6545
6546 static inline dw_loc_list_ref
6547 AT_loc_list (dw_attr_ref a)
6548 {
6549   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6550   return a->dw_attr_val.v.val_loc_list;
6551 }
6552
6553 /* Add an address constant attribute value to a DIE.  */
6554
6555 static inline void
6556 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6557 {
6558   dw_attr_node attr;
6559
6560   attr.dw_attr = attr_kind;
6561   attr.dw_attr_val.val_class = dw_val_class_addr;
6562   attr.dw_attr_val.v.val_addr = addr;
6563   add_dwarf_attr (die, &attr);
6564 }
6565
6566 /* Get the RTX from to an address DIE attribute.  */
6567
6568 static inline rtx
6569 AT_addr (dw_attr_ref a)
6570 {
6571   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6572   return a->dw_attr_val.v.val_addr;
6573 }
6574
6575 /* Add a file attribute value to a DIE.  */
6576
6577 static inline void
6578 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6579              struct dwarf_file_data *fd)
6580 {
6581   dw_attr_node attr;
6582
6583   attr.dw_attr = attr_kind;
6584   attr.dw_attr_val.val_class = dw_val_class_file;
6585   attr.dw_attr_val.v.val_file = fd;
6586   add_dwarf_attr (die, &attr);
6587 }
6588
6589 /* Get the dwarf_file_data from a file DIE attribute.  */
6590
6591 static inline struct dwarf_file_data *
6592 AT_file (dw_attr_ref a)
6593 {
6594   gcc_assert (a && AT_class (a) == dw_val_class_file);
6595   return a->dw_attr_val.v.val_file;
6596 }
6597
6598 /* Add a label identifier attribute value to a DIE.  */
6599
6600 static inline void
6601 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6602 {
6603   dw_attr_node attr;
6604
6605   attr.dw_attr = attr_kind;
6606   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6607   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6608   add_dwarf_attr (die, &attr);
6609 }
6610
6611 /* Add a section offset attribute value to a DIE, an offset into the
6612    debug_line section.  */
6613
6614 static inline void
6615 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6616                 const char *label)
6617 {
6618   dw_attr_node attr;
6619
6620   attr.dw_attr = attr_kind;
6621   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6622   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6623   add_dwarf_attr (die, &attr);
6624 }
6625
6626 /* Add a section offset attribute value to a DIE, an offset into the
6627    debug_macinfo section.  */
6628
6629 static inline void
6630 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6631                const char *label)
6632 {
6633   dw_attr_node attr;
6634
6635   attr.dw_attr = attr_kind;
6636   attr.dw_attr_val.val_class = dw_val_class_macptr;
6637   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6638   add_dwarf_attr (die, &attr);
6639 }
6640
6641 /* Add an offset attribute value to a DIE.  */
6642
6643 static inline void
6644 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6645                unsigned HOST_WIDE_INT offset)
6646 {
6647   dw_attr_node attr;
6648
6649   attr.dw_attr = attr_kind;
6650   attr.dw_attr_val.val_class = dw_val_class_offset;
6651   attr.dw_attr_val.v.val_offset = offset;
6652   add_dwarf_attr (die, &attr);
6653 }
6654
6655 /* Add an range_list attribute value to a DIE.  */
6656
6657 static void
6658 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6659                    long unsigned int offset)
6660 {
6661   dw_attr_node attr;
6662
6663   attr.dw_attr = attr_kind;
6664   attr.dw_attr_val.val_class = dw_val_class_range_list;
6665   attr.dw_attr_val.v.val_offset = offset;
6666   add_dwarf_attr (die, &attr);
6667 }
6668
6669 static inline const char *
6670 AT_lbl (dw_attr_ref a)
6671 {
6672   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6673                     || AT_class (a) == dw_val_class_lineptr
6674                     || AT_class (a) == dw_val_class_macptr));
6675   return a->dw_attr_val.v.val_lbl_id;
6676 }
6677
6678 /* Get the attribute of type attr_kind.  */
6679
6680 static dw_attr_ref
6681 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6682 {
6683   dw_attr_ref a;
6684   unsigned ix;
6685   dw_die_ref spec = NULL;
6686
6687   if (! die)
6688     return NULL;
6689
6690   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6691     if (a->dw_attr == attr_kind)
6692       return a;
6693     else if (a->dw_attr == DW_AT_specification
6694              || a->dw_attr == DW_AT_abstract_origin)
6695       spec = AT_ref (a);
6696
6697   if (spec)
6698     return get_AT (spec, attr_kind);
6699
6700   return NULL;
6701 }
6702
6703 /* Return the "low pc" attribute value, typically associated with a subprogram
6704    DIE.  Return null if the "low pc" attribute is either not present, or if it
6705    cannot be represented as an assembler label identifier.  */
6706
6707 static inline const char *
6708 get_AT_low_pc (dw_die_ref die)
6709 {
6710   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6711
6712   return a ? AT_lbl (a) : NULL;
6713 }
6714
6715 /* Return the "high pc" attribute value, typically associated with a subprogram
6716    DIE.  Return null if the "high pc" attribute is either not present, or if it
6717    cannot be represented as an assembler label identifier.  */
6718
6719 static inline const char *
6720 get_AT_hi_pc (dw_die_ref die)
6721 {
6722   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6723
6724   return a ? AT_lbl (a) : NULL;
6725 }
6726
6727 /* Return the value of the string attribute designated by ATTR_KIND, or
6728    NULL if it is not present.  */
6729
6730 static inline const char *
6731 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6732 {
6733   dw_attr_ref a = get_AT (die, attr_kind);
6734
6735   return a ? AT_string (a) : NULL;
6736 }
6737
6738 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6739    if it is not present.  */
6740
6741 static inline int
6742 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6743 {
6744   dw_attr_ref a = get_AT (die, attr_kind);
6745
6746   return a ? AT_flag (a) : 0;
6747 }
6748
6749 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6750    if it is not present.  */
6751
6752 static inline unsigned
6753 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6754 {
6755   dw_attr_ref a = get_AT (die, attr_kind);
6756
6757   return a ? AT_unsigned (a) : 0;
6758 }
6759
6760 static inline dw_die_ref
6761 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6762 {
6763   dw_attr_ref a = get_AT (die, attr_kind);
6764
6765   return a ? AT_ref (a) : NULL;
6766 }
6767
6768 static inline struct dwarf_file_data *
6769 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6770 {
6771   dw_attr_ref a = get_AT (die, attr_kind);
6772
6773   return a ? AT_file (a) : NULL;
6774 }
6775
6776 /* Return TRUE if the language is C or C++.  */
6777
6778 static inline bool
6779 is_c_family (void)
6780 {
6781   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6782
6783   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6784           || lang == DW_LANG_C99
6785           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6786 }
6787
6788 /* Return TRUE if the language is C++.  */
6789
6790 static inline bool
6791 is_cxx (void)
6792 {
6793   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6794
6795   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6796 }
6797
6798 /* Return TRUE if the language is Fortran.  */
6799
6800 static inline bool
6801 is_fortran (void)
6802 {
6803   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6804
6805   return (lang == DW_LANG_Fortran77
6806           || lang == DW_LANG_Fortran90
6807           || lang == DW_LANG_Fortran95);
6808 }
6809
6810 /* Return TRUE if the language is Java.  */
6811
6812 static inline bool
6813 is_java (void)
6814 {
6815   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6816
6817   return lang == DW_LANG_Java;
6818 }
6819
6820 /* Return TRUE if the language is Ada.  */
6821
6822 static inline bool
6823 is_ada (void)
6824 {
6825   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6826
6827   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6828 }
6829
6830 /* Remove the specified attribute if present.  */
6831
6832 static void
6833 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6834 {
6835   dw_attr_ref a;
6836   unsigned ix;
6837
6838   if (! die)
6839     return;
6840
6841   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6842     if (a->dw_attr == attr_kind)
6843       {
6844         if (AT_class (a) == dw_val_class_str)
6845           if (a->dw_attr_val.v.val_str->refcount)
6846             a->dw_attr_val.v.val_str->refcount--;
6847
6848         /* VEC_ordered_remove should help reduce the number of abbrevs
6849            that are needed.  */
6850         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6851         return;
6852       }
6853 }
6854
6855 /* Remove CHILD from its parent.  PREV must have the property that
6856    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6857
6858 static void
6859 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6860 {
6861   gcc_assert (child->die_parent == prev->die_parent);
6862   gcc_assert (prev->die_sib == child);
6863   if (prev == child)
6864     {
6865       gcc_assert (child->die_parent->die_child == child);
6866       prev = NULL;
6867     }
6868   else
6869     prev->die_sib = child->die_sib;
6870   if (child->die_parent->die_child == child)
6871     child->die_parent->die_child = prev;
6872 }
6873
6874 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6875    matches TAG.  */
6876
6877 static void
6878 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6879 {
6880   dw_die_ref c;
6881
6882   c = die->die_child;
6883   if (c) do {
6884     dw_die_ref prev = c;
6885     c = c->die_sib;
6886     while (c->die_tag == tag)
6887       {
6888         remove_child_with_prev (c, prev);
6889         /* Might have removed every child.  */
6890         if (c == c->die_sib)
6891           return;
6892         c = c->die_sib;
6893       }
6894   } while (c != die->die_child);
6895 }
6896
6897 /* Add a CHILD_DIE as the last child of DIE.  */
6898
6899 static void
6900 add_child_die (dw_die_ref die, dw_die_ref child_die)
6901 {
6902   /* FIXME this should probably be an assert.  */
6903   if (! die || ! child_die)
6904     return;
6905   gcc_assert (die != child_die);
6906
6907   child_die->die_parent = die;
6908   if (die->die_child)
6909     {
6910       child_die->die_sib = die->die_child->die_sib;
6911       die->die_child->die_sib = child_die;
6912     }
6913   else
6914     child_die->die_sib = child_die;
6915   die->die_child = child_die;
6916 }
6917
6918 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6919    is the specification, to the end of PARENT's list of children.
6920    This is done by removing and re-adding it.  */
6921
6922 static void
6923 splice_child_die (dw_die_ref parent, dw_die_ref child)
6924 {
6925   dw_die_ref p;
6926
6927   /* We want the declaration DIE from inside the class, not the
6928      specification DIE at toplevel.  */
6929   if (child->die_parent != parent)
6930     {
6931       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6932
6933       if (tmp)
6934         child = tmp;
6935     }
6936
6937   gcc_assert (child->die_parent == parent
6938               || (child->die_parent
6939                   == get_AT_ref (parent, DW_AT_specification)));
6940
6941   for (p = child->die_parent->die_child; ; p = p->die_sib)
6942     if (p->die_sib == child)
6943       {
6944         remove_child_with_prev (child, p);
6945         break;
6946       }
6947
6948   add_child_die (parent, child);
6949 }
6950
6951 /* Return a pointer to a newly created DIE node.  */
6952
6953 static inline dw_die_ref
6954 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6955 {
6956   dw_die_ref die = GGC_CNEW (die_node);
6957
6958   die->die_tag = tag_value;
6959
6960   if (parent_die != NULL)
6961     add_child_die (parent_die, die);
6962   else
6963     {
6964       limbo_die_node *limbo_node;
6965
6966       limbo_node = GGC_CNEW (limbo_die_node);
6967       limbo_node->die = die;
6968       limbo_node->created_for = t;
6969       limbo_node->next = limbo_die_list;
6970       limbo_die_list = limbo_node;
6971     }
6972
6973   return die;
6974 }
6975
6976 /* Return the DIE associated with the given type specifier.  */
6977
6978 static inline dw_die_ref
6979 lookup_type_die (tree type)
6980 {
6981   return TYPE_SYMTAB_DIE (type);
6982 }
6983
6984 /* Equate a DIE to a given type specifier.  */
6985
6986 static inline void
6987 equate_type_number_to_die (tree type, dw_die_ref type_die)
6988 {
6989   TYPE_SYMTAB_DIE (type) = type_die;
6990 }
6991
6992 /* Returns a hash value for X (which really is a die_struct).  */
6993
6994 static hashval_t
6995 decl_die_table_hash (const void *x)
6996 {
6997   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6998 }
6999
7000 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7001
7002 static int
7003 decl_die_table_eq (const void *x, const void *y)
7004 {
7005   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7006 }
7007
7008 /* Return the DIE associated with a given declaration.  */
7009
7010 static inline dw_die_ref
7011 lookup_decl_die (tree decl)
7012 {
7013   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7014 }
7015
7016 /* Returns a hash value for X (which really is a var_loc_list).  */
7017
7018 static hashval_t
7019 decl_loc_table_hash (const void *x)
7020 {
7021   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7022 }
7023
7024 /* Return nonzero if decl_id of var_loc_list X is the same as
7025    UID of decl *Y.  */
7026
7027 static int
7028 decl_loc_table_eq (const void *x, const void *y)
7029 {
7030   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7031 }
7032
7033 /* Return the var_loc list associated with a given declaration.  */
7034
7035 static inline var_loc_list *
7036 lookup_decl_loc (const_tree decl)
7037 {
7038   return (var_loc_list *)
7039     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7040 }
7041
7042 /* Equate a DIE to a particular declaration.  */
7043
7044 static void
7045 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7046 {
7047   unsigned int decl_id = DECL_UID (decl);
7048   void **slot;
7049
7050   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7051   *slot = decl_die;
7052   decl_die->decl_id = decl_id;
7053 }
7054
7055 /* Add a variable location node to the linked list for DECL.  */
7056
7057 static void
7058 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7059 {
7060   unsigned int decl_id = DECL_UID (decl);
7061   var_loc_list *temp;
7062   void **slot;
7063
7064   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7065   if (*slot == NULL)
7066     {
7067       temp = GGC_CNEW (var_loc_list);
7068       temp->decl_id = decl_id;
7069       *slot = temp;
7070     }
7071   else
7072     temp = (var_loc_list *) *slot;
7073
7074   if (temp->last)
7075     {
7076       /* If the current location is the same as the end of the list,
7077          and either both or neither of the locations is uninitialized,
7078          we have nothing to do.  */
7079       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7080                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7081           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7082                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7083               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7084                    == VAR_INIT_STATUS_UNINITIALIZED)
7085                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7086                       == VAR_INIT_STATUS_UNINITIALIZED))))
7087         {
7088           /* Add LOC to the end of list and update LAST.  */
7089           temp->last->next = loc;
7090           temp->last = loc;
7091         }
7092     }
7093   /* Do not add empty location to the beginning of the list.  */
7094   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7095     {
7096       temp->first = loc;
7097       temp->last = loc;
7098     }
7099 }
7100 \f
7101 /* Keep track of the number of spaces used to indent the
7102    output of the debugging routines that print the structure of
7103    the DIE internal representation.  */
7104 static int print_indent;
7105
7106 /* Indent the line the number of spaces given by print_indent.  */
7107
7108 static inline void
7109 print_spaces (FILE *outfile)
7110 {
7111   fprintf (outfile, "%*s", print_indent, "");
7112 }
7113
7114 /* Print the information associated with a given DIE, and its children.
7115    This routine is a debugging aid only.  */
7116
7117 static void
7118 print_die (dw_die_ref die, FILE *outfile)
7119 {
7120   dw_attr_ref a;
7121   dw_die_ref c;
7122   unsigned ix;
7123
7124   print_spaces (outfile);
7125   fprintf (outfile, "DIE %4ld: %s\n",
7126            die->die_offset, dwarf_tag_name (die->die_tag));
7127   print_spaces (outfile);
7128   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7129   fprintf (outfile, " offset: %ld\n", die->die_offset);
7130
7131   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7132     {
7133       print_spaces (outfile);
7134       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7135
7136       switch (AT_class (a))
7137         {
7138         case dw_val_class_addr:
7139           fprintf (outfile, "address");
7140           break;
7141         case dw_val_class_offset:
7142           fprintf (outfile, "offset");
7143           break;
7144         case dw_val_class_loc:
7145           fprintf (outfile, "location descriptor");
7146           break;
7147         case dw_val_class_loc_list:
7148           fprintf (outfile, "location list -> label:%s",
7149                    AT_loc_list (a)->ll_symbol);
7150           break;
7151         case dw_val_class_range_list:
7152           fprintf (outfile, "range list");
7153           break;
7154         case dw_val_class_const:
7155           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7156           break;
7157         case dw_val_class_unsigned_const:
7158           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7159           break;
7160         case dw_val_class_long_long:
7161           fprintf (outfile, "constant (%lu,%lu)",
7162                    a->dw_attr_val.v.val_long_long.hi,
7163                    a->dw_attr_val.v.val_long_long.low);
7164           break;
7165         case dw_val_class_vec:
7166           fprintf (outfile, "floating-point or vector constant");
7167           break;
7168         case dw_val_class_flag:
7169           fprintf (outfile, "%u", AT_flag (a));
7170           break;
7171         case dw_val_class_die_ref:
7172           if (AT_ref (a) != NULL)
7173             {
7174               if (AT_ref (a)->die_symbol)
7175                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7176               else
7177                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7178             }
7179           else
7180             fprintf (outfile, "die -> <null>");
7181           break;
7182         case dw_val_class_lbl_id:
7183         case dw_val_class_lineptr:
7184         case dw_val_class_macptr:
7185           fprintf (outfile, "label: %s", AT_lbl (a));
7186           break;
7187         case dw_val_class_str:
7188           if (AT_string (a) != NULL)
7189             fprintf (outfile, "\"%s\"", AT_string (a));
7190           else
7191             fprintf (outfile, "<null>");
7192           break;
7193         case dw_val_class_file:
7194           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7195                    AT_file (a)->emitted_number);
7196           break;
7197         default:
7198           break;
7199         }
7200
7201       fprintf (outfile, "\n");
7202     }
7203
7204   if (die->die_child != NULL)
7205     {
7206       print_indent += 4;
7207       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7208       print_indent -= 4;
7209     }
7210   if (print_indent == 0)
7211     fprintf (outfile, "\n");
7212 }
7213
7214 /* Print the contents of the source code line number correspondence table.
7215    This routine is a debugging aid only.  */
7216
7217 static void
7218 print_dwarf_line_table (FILE *outfile)
7219 {
7220   unsigned i;
7221   dw_line_info_ref line_info;
7222
7223   fprintf (outfile, "\n\nDWARF source line information\n");
7224   for (i = 1; i < line_info_table_in_use; i++)
7225     {
7226       line_info = &line_info_table[i];
7227       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7228                line_info->dw_file_num,
7229                line_info->dw_line_num);
7230     }
7231
7232   fprintf (outfile, "\n\n");
7233 }
7234
7235 /* Print the information collected for a given DIE.  */
7236
7237 void
7238 debug_dwarf_die (dw_die_ref die)
7239 {
7240   print_die (die, stderr);
7241 }
7242
7243 /* Print all DWARF information collected for the compilation unit.
7244    This routine is a debugging aid only.  */
7245
7246 void
7247 debug_dwarf (void)
7248 {
7249   print_indent = 0;
7250   print_die (comp_unit_die, stderr);
7251   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7252     print_dwarf_line_table (stderr);
7253 }
7254 \f
7255 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7256    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7257    DIE that marks the start of the DIEs for this include file.  */
7258
7259 static dw_die_ref
7260 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7261 {
7262   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7263   dw_die_ref new_unit = gen_compile_unit_die (filename);
7264
7265   new_unit->die_sib = old_unit;
7266   return new_unit;
7267 }
7268
7269 /* Close an include-file CU and reopen the enclosing one.  */
7270
7271 static dw_die_ref
7272 pop_compile_unit (dw_die_ref old_unit)
7273 {
7274   dw_die_ref new_unit = old_unit->die_sib;
7275
7276   old_unit->die_sib = NULL;
7277   return new_unit;
7278 }
7279
7280 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7281 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7282
7283 /* Calculate the checksum of a location expression.  */
7284
7285 static inline void
7286 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7287 {
7288   int tem;
7289
7290   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7291   CHECKSUM (tem);
7292   CHECKSUM (loc->dw_loc_oprnd1);
7293   CHECKSUM (loc->dw_loc_oprnd2);
7294 }
7295
7296 /* Calculate the checksum of an attribute.  */
7297
7298 static void
7299 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7300 {
7301   dw_loc_descr_ref loc;
7302   rtx r;
7303
7304   CHECKSUM (at->dw_attr);
7305
7306   /* We don't care that this was compiled with a different compiler
7307      snapshot; if the output is the same, that's what matters.  */
7308   if (at->dw_attr == DW_AT_producer)
7309     return;
7310
7311   switch (AT_class (at))
7312     {
7313     case dw_val_class_const:
7314       CHECKSUM (at->dw_attr_val.v.val_int);
7315       break;
7316     case dw_val_class_unsigned_const:
7317       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7318       break;
7319     case dw_val_class_long_long:
7320       CHECKSUM (at->dw_attr_val.v.val_long_long);
7321       break;
7322     case dw_val_class_vec:
7323       CHECKSUM (at->dw_attr_val.v.val_vec);
7324       break;
7325     case dw_val_class_flag:
7326       CHECKSUM (at->dw_attr_val.v.val_flag);
7327       break;
7328     case dw_val_class_str:
7329       CHECKSUM_STRING (AT_string (at));
7330       break;
7331
7332     case dw_val_class_addr:
7333       r = AT_addr (at);
7334       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7335       CHECKSUM_STRING (XSTR (r, 0));
7336       break;
7337
7338     case dw_val_class_offset:
7339       CHECKSUM (at->dw_attr_val.v.val_offset);
7340       break;
7341
7342     case dw_val_class_loc:
7343       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7344         loc_checksum (loc, ctx);
7345       break;
7346
7347     case dw_val_class_die_ref:
7348       die_checksum (AT_ref (at), ctx, mark);
7349       break;
7350
7351     case dw_val_class_fde_ref:
7352     case dw_val_class_lbl_id:
7353     case dw_val_class_lineptr:
7354     case dw_val_class_macptr:
7355       break;
7356
7357     case dw_val_class_file:
7358       CHECKSUM_STRING (AT_file (at)->filename);
7359       break;
7360
7361     default:
7362       break;
7363     }
7364 }
7365
7366 /* Calculate the checksum of a DIE.  */
7367
7368 static void
7369 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7370 {
7371   dw_die_ref c;
7372   dw_attr_ref a;
7373   unsigned ix;
7374
7375   /* To avoid infinite recursion.  */
7376   if (die->die_mark)
7377     {
7378       CHECKSUM (die->die_mark);
7379       return;
7380     }
7381   die->die_mark = ++(*mark);
7382
7383   CHECKSUM (die->die_tag);
7384
7385   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7386     attr_checksum (a, ctx, mark);
7387
7388   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7389 }
7390
7391 #undef CHECKSUM
7392 #undef CHECKSUM_STRING
7393
7394 /* Do the location expressions look same?  */
7395 static inline int
7396 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7397 {
7398   return loc1->dw_loc_opc == loc2->dw_loc_opc
7399          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7400          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7401 }
7402
7403 /* Do the values look the same?  */
7404 static int
7405 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7406 {
7407   dw_loc_descr_ref loc1, loc2;
7408   rtx r1, r2;
7409
7410   if (v1->val_class != v2->val_class)
7411     return 0;
7412
7413   switch (v1->val_class)
7414     {
7415     case dw_val_class_const:
7416       return v1->v.val_int == v2->v.val_int;
7417     case dw_val_class_unsigned_const:
7418       return v1->v.val_unsigned == v2->v.val_unsigned;
7419     case dw_val_class_long_long:
7420       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7421              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7422     case dw_val_class_vec:
7423       if (v1->v.val_vec.length != v2->v.val_vec.length
7424           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7425         return 0;
7426       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7427                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7428         return 0;
7429       return 1;
7430     case dw_val_class_flag:
7431       return v1->v.val_flag == v2->v.val_flag;
7432     case dw_val_class_str:
7433       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7434
7435     case dw_val_class_addr:
7436       r1 = v1->v.val_addr;
7437       r2 = v2->v.val_addr;
7438       if (GET_CODE (r1) != GET_CODE (r2))
7439         return 0;
7440       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7441       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7442
7443     case dw_val_class_offset:
7444       return v1->v.val_offset == v2->v.val_offset;
7445
7446     case dw_val_class_loc:
7447       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7448            loc1 && loc2;
7449            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7450         if (!same_loc_p (loc1, loc2, mark))
7451           return 0;
7452       return !loc1 && !loc2;
7453
7454     case dw_val_class_die_ref:
7455       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7456
7457     case dw_val_class_fde_ref:
7458     case dw_val_class_lbl_id:
7459     case dw_val_class_lineptr:
7460     case dw_val_class_macptr:
7461       return 1;
7462
7463     case dw_val_class_file:
7464       return v1->v.val_file == v2->v.val_file;
7465
7466     default:
7467       return 1;
7468     }
7469 }
7470
7471 /* Do the attributes look the same?  */
7472
7473 static int
7474 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7475 {
7476   if (at1->dw_attr != at2->dw_attr)
7477     return 0;
7478
7479   /* We don't care that this was compiled with a different compiler
7480      snapshot; if the output is the same, that's what matters. */
7481   if (at1->dw_attr == DW_AT_producer)
7482     return 1;
7483
7484   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7485 }
7486
7487 /* Do the dies look the same?  */
7488
7489 static int
7490 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7491 {
7492   dw_die_ref c1, c2;
7493   dw_attr_ref a1;
7494   unsigned ix;
7495
7496   /* To avoid infinite recursion.  */
7497   if (die1->die_mark)
7498     return die1->die_mark == die2->die_mark;
7499   die1->die_mark = die2->die_mark = ++(*mark);
7500
7501   if (die1->die_tag != die2->die_tag)
7502     return 0;
7503
7504   if (VEC_length (dw_attr_node, die1->die_attr)
7505       != VEC_length (dw_attr_node, die2->die_attr))
7506     return 0;
7507
7508   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7509     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7510       return 0;
7511
7512   c1 = die1->die_child;
7513   c2 = die2->die_child;
7514   if (! c1)
7515     {
7516       if (c2)
7517         return 0;
7518     }
7519   else
7520     for (;;)
7521       {
7522         if (!same_die_p (c1, c2, mark))
7523           return 0;
7524         c1 = c1->die_sib;
7525         c2 = c2->die_sib;
7526         if (c1 == die1->die_child)
7527           {
7528             if (c2 == die2->die_child)
7529               break;
7530             else
7531               return 0;
7532           }
7533     }
7534
7535   return 1;
7536 }
7537
7538 /* Do the dies look the same?  Wrapper around same_die_p.  */
7539
7540 static int
7541 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7542 {
7543   int mark = 0;
7544   int ret = same_die_p (die1, die2, &mark);
7545
7546   unmark_all_dies (die1);
7547   unmark_all_dies (die2);
7548
7549   return ret;
7550 }
7551
7552 /* The prefix to attach to symbols on DIEs in the current comdat debug
7553    info section.  */
7554 static char *comdat_symbol_id;
7555
7556 /* The index of the current symbol within the current comdat CU.  */
7557 static unsigned int comdat_symbol_number;
7558
7559 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7560    children, and set comdat_symbol_id accordingly.  */
7561
7562 static void
7563 compute_section_prefix (dw_die_ref unit_die)
7564 {
7565   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7566   const char *base = die_name ? lbasename (die_name) : "anonymous";
7567   char *name = XALLOCAVEC (char, strlen (base) + 64);
7568   char *p;
7569   int i, mark;
7570   unsigned char checksum[16];
7571   struct md5_ctx ctx;
7572
7573   /* Compute the checksum of the DIE, then append part of it as hex digits to
7574      the name filename of the unit.  */
7575
7576   md5_init_ctx (&ctx);
7577   mark = 0;
7578   die_checksum (unit_die, &ctx, &mark);
7579   unmark_all_dies (unit_die);
7580   md5_finish_ctx (&ctx, checksum);
7581
7582   sprintf (name, "%s.", base);
7583   clean_symbol_name (name);
7584
7585   p = name + strlen (name);
7586   for (i = 0; i < 4; i++)
7587     {
7588       sprintf (p, "%.2x", checksum[i]);
7589       p += 2;
7590     }
7591
7592   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7593   comdat_symbol_number = 0;
7594 }
7595
7596 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7597
7598 static int
7599 is_type_die (dw_die_ref die)
7600 {
7601   switch (die->die_tag)
7602     {
7603     case DW_TAG_array_type:
7604     case DW_TAG_class_type:
7605     case DW_TAG_interface_type:
7606     case DW_TAG_enumeration_type:
7607     case DW_TAG_pointer_type:
7608     case DW_TAG_reference_type:
7609     case DW_TAG_string_type:
7610     case DW_TAG_structure_type:
7611     case DW_TAG_subroutine_type:
7612     case DW_TAG_union_type:
7613     case DW_TAG_ptr_to_member_type:
7614     case DW_TAG_set_type:
7615     case DW_TAG_subrange_type:
7616     case DW_TAG_base_type:
7617     case DW_TAG_const_type:
7618     case DW_TAG_file_type:
7619     case DW_TAG_packed_type:
7620     case DW_TAG_volatile_type:
7621     case DW_TAG_typedef:
7622       return 1;
7623     default:
7624       return 0;
7625     }
7626 }
7627
7628 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7629    Basically, we want to choose the bits that are likely to be shared between
7630    compilations (types) and leave out the bits that are specific to individual
7631    compilations (functions).  */
7632
7633 static int
7634 is_comdat_die (dw_die_ref c)
7635 {
7636   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7637      we do for stabs.  The advantage is a greater likelihood of sharing between
7638      objects that don't include headers in the same order (and therefore would
7639      put the base types in a different comdat).  jason 8/28/00 */
7640
7641   if (c->die_tag == DW_TAG_base_type)
7642     return 0;
7643
7644   if (c->die_tag == DW_TAG_pointer_type
7645       || c->die_tag == DW_TAG_reference_type
7646       || c->die_tag == DW_TAG_const_type
7647       || c->die_tag == DW_TAG_volatile_type)
7648     {
7649       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7650
7651       return t ? is_comdat_die (t) : 0;
7652     }
7653
7654   return is_type_die (c);
7655 }
7656
7657 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7658    compilation unit.  */
7659
7660 static int
7661 is_symbol_die (dw_die_ref c)
7662 {
7663   return (is_type_die (c)
7664           || (get_AT (c, DW_AT_declaration)
7665               && !get_AT (c, DW_AT_specification))
7666           || c->die_tag == DW_TAG_namespace
7667           || c->die_tag == DW_TAG_module);
7668 }
7669
7670 static char *
7671 gen_internal_sym (const char *prefix)
7672 {
7673   char buf[256];
7674
7675   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7676   return xstrdup (buf);
7677 }
7678
7679 /* Assign symbols to all worthy DIEs under DIE.  */
7680
7681 static void
7682 assign_symbol_names (dw_die_ref die)
7683 {
7684   dw_die_ref c;
7685
7686   if (is_symbol_die (die))
7687     {
7688       if (comdat_symbol_id)
7689         {
7690           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7691
7692           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7693                    comdat_symbol_id, comdat_symbol_number++);
7694           die->die_symbol = xstrdup (p);
7695         }
7696       else
7697         die->die_symbol = gen_internal_sym ("LDIE");
7698     }
7699
7700   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7701 }
7702
7703 struct cu_hash_table_entry
7704 {
7705   dw_die_ref cu;
7706   unsigned min_comdat_num, max_comdat_num;
7707   struct cu_hash_table_entry *next;
7708 };
7709
7710 /* Routines to manipulate hash table of CUs.  */
7711 static hashval_t
7712 htab_cu_hash (const void *of)
7713 {
7714   const struct cu_hash_table_entry *const entry =
7715     (const struct cu_hash_table_entry *) of;
7716
7717   return htab_hash_string (entry->cu->die_symbol);
7718 }
7719
7720 static int
7721 htab_cu_eq (const void *of1, const void *of2)
7722 {
7723   const struct cu_hash_table_entry *const entry1 =
7724     (const struct cu_hash_table_entry *) of1;
7725   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7726
7727   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7728 }
7729
7730 static void
7731 htab_cu_del (void *what)
7732 {
7733   struct cu_hash_table_entry *next,
7734     *entry = (struct cu_hash_table_entry *) what;
7735
7736   while (entry)
7737     {
7738       next = entry->next;
7739       free (entry);
7740       entry = next;
7741     }
7742 }
7743
7744 /* Check whether we have already seen this CU and set up SYM_NUM
7745    accordingly.  */
7746 static int
7747 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7748 {
7749   struct cu_hash_table_entry dummy;
7750   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7751
7752   dummy.max_comdat_num = 0;
7753
7754   slot = (struct cu_hash_table_entry **)
7755     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7756         INSERT);
7757   entry = *slot;
7758
7759   for (; entry; last = entry, entry = entry->next)
7760     {
7761       if (same_die_p_wrap (cu, entry->cu))
7762         break;
7763     }
7764
7765   if (entry)
7766     {
7767       *sym_num = entry->min_comdat_num;
7768       return 1;
7769     }
7770
7771   entry = XCNEW (struct cu_hash_table_entry);
7772   entry->cu = cu;
7773   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7774   entry->next = *slot;
7775   *slot = entry;
7776
7777   return 0;
7778 }
7779
7780 /* Record SYM_NUM to record of CU in HTABLE.  */
7781 static void
7782 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7783 {
7784   struct cu_hash_table_entry **slot, *entry;
7785
7786   slot = (struct cu_hash_table_entry **)
7787     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7788         NO_INSERT);
7789   entry = *slot;
7790
7791   entry->max_comdat_num = sym_num;
7792 }
7793
7794 /* Traverse the DIE (which is always comp_unit_die), and set up
7795    additional compilation units for each of the include files we see
7796    bracketed by BINCL/EINCL.  */
7797
7798 static void
7799 break_out_includes (dw_die_ref die)
7800 {
7801   dw_die_ref c;
7802   dw_die_ref unit = NULL;
7803   limbo_die_node *node, **pnode;
7804   htab_t cu_hash_table;
7805
7806   c = die->die_child;
7807   if (c) do {
7808     dw_die_ref prev = c;
7809     c = c->die_sib;
7810     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7811            || (unit && is_comdat_die (c)))
7812       {
7813         dw_die_ref next = c->die_sib;
7814
7815         /* This DIE is for a secondary CU; remove it from the main one.  */
7816         remove_child_with_prev (c, prev);
7817
7818         if (c->die_tag == DW_TAG_GNU_BINCL)
7819           unit = push_new_compile_unit (unit, c);
7820         else if (c->die_tag == DW_TAG_GNU_EINCL)
7821           unit = pop_compile_unit (unit);
7822         else
7823           add_child_die (unit, c);
7824         c = next;
7825         if (c == die->die_child)
7826           break;
7827       }
7828   } while (c != die->die_child);
7829
7830 #if 0
7831   /* We can only use this in debugging, since the frontend doesn't check
7832      to make sure that we leave every include file we enter.  */
7833   gcc_assert (!unit);
7834 #endif
7835
7836   assign_symbol_names (die);
7837   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7838   for (node = limbo_die_list, pnode = &limbo_die_list;
7839        node;
7840        node = node->next)
7841     {
7842       int is_dupl;
7843
7844       compute_section_prefix (node->die);
7845       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7846                         &comdat_symbol_number);
7847       assign_symbol_names (node->die);
7848       if (is_dupl)
7849         *pnode = node->next;
7850       else
7851         {
7852           pnode = &node->next;
7853           record_comdat_symbol_number (node->die, cu_hash_table,
7854                 comdat_symbol_number);
7855         }
7856     }
7857   htab_delete (cu_hash_table);
7858 }
7859
7860 /* Traverse the DIE and add a sibling attribute if it may have the
7861    effect of speeding up access to siblings.  To save some space,
7862    avoid generating sibling attributes for DIE's without children.  */
7863
7864 static void
7865 add_sibling_attributes (dw_die_ref die)
7866 {
7867   dw_die_ref c;
7868
7869   if (! die->die_child)
7870     return;
7871
7872   if (die->die_parent && die != die->die_parent->die_child)
7873     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7874
7875   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7876 }
7877
7878 /* Output all location lists for the DIE and its children.  */
7879
7880 static void
7881 output_location_lists (dw_die_ref die)
7882 {
7883   dw_die_ref c;
7884   dw_attr_ref a;
7885   unsigned ix;
7886
7887   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7888     if (AT_class (a) == dw_val_class_loc_list)
7889       output_loc_list (AT_loc_list (a));
7890
7891   FOR_EACH_CHILD (die, c, output_location_lists (c));
7892 }
7893
7894 /* The format of each DIE (and its attribute value pairs) is encoded in an
7895    abbreviation table.  This routine builds the abbreviation table and assigns
7896    a unique abbreviation id for each abbreviation entry.  The children of each
7897    die are visited recursively.  */
7898
7899 static void
7900 build_abbrev_table (dw_die_ref die)
7901 {
7902   unsigned long abbrev_id;
7903   unsigned int n_alloc;
7904   dw_die_ref c;
7905   dw_attr_ref a;
7906   unsigned ix;
7907
7908   /* Scan the DIE references, and mark as external any that refer to
7909      DIEs from other CUs (i.e. those which are not marked).  */
7910   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7911     if (AT_class (a) == dw_val_class_die_ref
7912         && AT_ref (a)->die_mark == 0)
7913       {
7914         gcc_assert (AT_ref (a)->die_symbol);
7915         set_AT_ref_external (a, 1);
7916       }
7917
7918   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7919     {
7920       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7921       dw_attr_ref die_a, abbrev_a;
7922       unsigned ix;
7923       bool ok = true;
7924
7925       if (abbrev->die_tag != die->die_tag)
7926         continue;
7927       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7928         continue;
7929
7930       if (VEC_length (dw_attr_node, abbrev->die_attr)
7931           != VEC_length (dw_attr_node, die->die_attr))
7932         continue;
7933
7934       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7935         {
7936           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7937           if ((abbrev_a->dw_attr != die_a->dw_attr)
7938               || (value_format (abbrev_a) != value_format (die_a)))
7939             {
7940               ok = false;
7941               break;
7942             }
7943         }
7944       if (ok)
7945         break;
7946     }
7947
7948   if (abbrev_id >= abbrev_die_table_in_use)
7949     {
7950       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7951         {
7952           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7953           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7954                                             n_alloc);
7955
7956           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7957                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7958           abbrev_die_table_allocated = n_alloc;
7959         }
7960
7961       ++abbrev_die_table_in_use;
7962       abbrev_die_table[abbrev_id] = die;
7963     }
7964
7965   die->die_abbrev = abbrev_id;
7966   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7967 }
7968 \f
7969 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7970
7971 static int
7972 constant_size (unsigned HOST_WIDE_INT value)
7973 {
7974   int log;
7975
7976   if (value == 0)
7977     log = 0;
7978   else
7979     log = floor_log2 (value);
7980
7981   log = log / 8;
7982   log = 1 << (floor_log2 (log) + 1);
7983
7984   return log;
7985 }
7986
7987 /* Return the size of a DIE as it is represented in the
7988    .debug_info section.  */
7989
7990 static unsigned long
7991 size_of_die (dw_die_ref die)
7992 {
7993   unsigned long size = 0;
7994   dw_attr_ref a;
7995   unsigned ix;
7996
7997   size += size_of_uleb128 (die->die_abbrev);
7998   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7999     {
8000       switch (AT_class (a))
8001         {
8002         case dw_val_class_addr:
8003           size += DWARF2_ADDR_SIZE;
8004           break;
8005         case dw_val_class_offset:
8006           size += DWARF_OFFSET_SIZE;
8007           break;
8008         case dw_val_class_loc:
8009           {
8010             unsigned long lsize = size_of_locs (AT_loc (a));
8011
8012             /* Block length.  */
8013             size += constant_size (lsize);
8014             size += lsize;
8015           }
8016           break;
8017         case dw_val_class_loc_list:
8018           size += DWARF_OFFSET_SIZE;
8019           break;
8020         case dw_val_class_range_list:
8021           size += DWARF_OFFSET_SIZE;
8022           break;
8023         case dw_val_class_const:
8024           size += size_of_sleb128 (AT_int (a));
8025           break;
8026         case dw_val_class_unsigned_const:
8027           size += constant_size (AT_unsigned (a));
8028           break;
8029         case dw_val_class_long_long:
8030           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
8031           break;
8032         case dw_val_class_vec:
8033           size += constant_size (a->dw_attr_val.v.val_vec.length
8034                                  * a->dw_attr_val.v.val_vec.elt_size)
8035                   + a->dw_attr_val.v.val_vec.length
8036                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8037           break;
8038         case dw_val_class_flag:
8039           size += 1;
8040           break;
8041         case dw_val_class_die_ref:
8042           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8043              whereas in DWARF3 it's always sized as an offset.  */
8044           if (AT_ref_external (a) && dwarf_version == 2)
8045             size += DWARF2_ADDR_SIZE;
8046           else
8047             size += DWARF_OFFSET_SIZE;
8048           break;
8049         case dw_val_class_fde_ref:
8050           size += DWARF_OFFSET_SIZE;
8051           break;
8052         case dw_val_class_lbl_id:
8053           size += DWARF2_ADDR_SIZE;
8054           break;
8055         case dw_val_class_lineptr:
8056         case dw_val_class_macptr:
8057           size += DWARF_OFFSET_SIZE;
8058           break;
8059         case dw_val_class_str:
8060           if (AT_string_form (a) == DW_FORM_strp)
8061             size += DWARF_OFFSET_SIZE;
8062           else
8063             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8064           break;
8065         case dw_val_class_file:
8066           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8067           break;
8068         default:
8069           gcc_unreachable ();
8070         }
8071     }
8072
8073   return size;
8074 }
8075
8076 /* Size the debugging information associated with a given DIE.  Visits the
8077    DIE's children recursively.  Updates the global variable next_die_offset, on
8078    each time through.  Uses the current value of next_die_offset to update the
8079    die_offset field in each DIE.  */
8080
8081 static void
8082 calc_die_sizes (dw_die_ref die)
8083 {
8084   dw_die_ref c;
8085
8086   die->die_offset = next_die_offset;
8087   next_die_offset += size_of_die (die);
8088
8089   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8090
8091   if (die->die_child != NULL)
8092     /* Count the null byte used to terminate sibling lists.  */
8093     next_die_offset += 1;
8094 }
8095
8096 /* Set the marks for a die and its children.  We do this so
8097    that we know whether or not a reference needs to use FORM_ref_addr; only
8098    DIEs in the same CU will be marked.  We used to clear out the offset
8099    and use that as the flag, but ran into ordering problems.  */
8100
8101 static void
8102 mark_dies (dw_die_ref die)
8103 {
8104   dw_die_ref c;
8105
8106   gcc_assert (!die->die_mark);
8107
8108   die->die_mark = 1;
8109   FOR_EACH_CHILD (die, c, mark_dies (c));
8110 }
8111
8112 /* Clear the marks for a die and its children.  */
8113
8114 static void
8115 unmark_dies (dw_die_ref die)
8116 {
8117   dw_die_ref c;
8118
8119   gcc_assert (die->die_mark);
8120
8121   die->die_mark = 0;
8122   FOR_EACH_CHILD (die, c, unmark_dies (c));
8123 }
8124
8125 /* Clear the marks for a die, its children and referred dies.  */
8126
8127 static void
8128 unmark_all_dies (dw_die_ref die)
8129 {
8130   dw_die_ref c;
8131   dw_attr_ref a;
8132   unsigned ix;
8133
8134   if (!die->die_mark)
8135     return;
8136   die->die_mark = 0;
8137
8138   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8139
8140   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8141     if (AT_class (a) == dw_val_class_die_ref)
8142       unmark_all_dies (AT_ref (a));
8143 }
8144
8145 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8146    generated for the compilation unit.  */
8147
8148 static unsigned long
8149 size_of_pubnames (VEC (pubname_entry, gc) * names)
8150 {
8151   unsigned long size;
8152   unsigned i;
8153   pubname_ref p;
8154
8155   size = DWARF_PUBNAMES_HEADER_SIZE;
8156   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8157     if (names != pubtype_table
8158         || p->die->die_offset != 0
8159         || !flag_eliminate_unused_debug_types)
8160       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8161
8162   size += DWARF_OFFSET_SIZE;
8163   return size;
8164 }
8165
8166 /* Return the size of the information in the .debug_aranges section.  */
8167
8168 static unsigned long
8169 size_of_aranges (void)
8170 {
8171   unsigned long size;
8172
8173   size = DWARF_ARANGES_HEADER_SIZE;
8174
8175   /* Count the address/length pair for this compilation unit.  */
8176   if (text_section_used)
8177     size += 2 * DWARF2_ADDR_SIZE;
8178   if (cold_text_section_used)
8179     size += 2 * DWARF2_ADDR_SIZE;
8180   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8181
8182   /* Count the two zero words used to terminated the address range table.  */
8183   size += 2 * DWARF2_ADDR_SIZE;
8184   return size;
8185 }
8186 \f
8187 /* Select the encoding of an attribute value.  */
8188
8189 static enum dwarf_form
8190 value_format (dw_attr_ref a)
8191 {
8192   switch (a->dw_attr_val.val_class)
8193     {
8194     case dw_val_class_addr:
8195       return DW_FORM_addr;
8196     case dw_val_class_range_list:
8197     case dw_val_class_offset:
8198     case dw_val_class_loc_list:
8199       switch (DWARF_OFFSET_SIZE)
8200         {
8201         case 4:
8202           return DW_FORM_data4;
8203         case 8:
8204           return DW_FORM_data8;
8205         default:
8206           gcc_unreachable ();
8207         }
8208     case dw_val_class_loc:
8209       switch (constant_size (size_of_locs (AT_loc (a))))
8210         {
8211         case 1:
8212           return DW_FORM_block1;
8213         case 2:
8214           return DW_FORM_block2;
8215         default:
8216           gcc_unreachable ();
8217         }
8218     case dw_val_class_const:
8219       return DW_FORM_sdata;
8220     case dw_val_class_unsigned_const:
8221       switch (constant_size (AT_unsigned (a)))
8222         {
8223         case 1:
8224           return DW_FORM_data1;
8225         case 2:
8226           return DW_FORM_data2;
8227         case 4:
8228           return DW_FORM_data4;
8229         case 8:
8230           return DW_FORM_data8;
8231         default:
8232           gcc_unreachable ();
8233         }
8234     case dw_val_class_long_long:
8235       return DW_FORM_block1;
8236     case dw_val_class_vec:
8237       switch (constant_size (a->dw_attr_val.v.val_vec.length
8238                              * a->dw_attr_val.v.val_vec.elt_size))
8239         {
8240         case 1:
8241           return DW_FORM_block1;
8242         case 2:
8243           return DW_FORM_block2;
8244         case 4:
8245           return DW_FORM_block4;
8246         default:
8247           gcc_unreachable ();
8248         }
8249     case dw_val_class_flag:
8250       return DW_FORM_flag;
8251     case dw_val_class_die_ref:
8252       if (AT_ref_external (a))
8253         return DW_FORM_ref_addr;
8254       else
8255         return DW_FORM_ref;
8256     case dw_val_class_fde_ref:
8257       return DW_FORM_data;
8258     case dw_val_class_lbl_id:
8259       return DW_FORM_addr;
8260     case dw_val_class_lineptr:
8261     case dw_val_class_macptr:
8262       return DW_FORM_data;
8263     case dw_val_class_str:
8264       return AT_string_form (a);
8265     case dw_val_class_file:
8266       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8267         {
8268         case 1:
8269           return DW_FORM_data1;
8270         case 2:
8271           return DW_FORM_data2;
8272         case 4:
8273           return DW_FORM_data4;
8274         default:
8275           gcc_unreachable ();
8276         }
8277
8278     default:
8279       gcc_unreachable ();
8280     }
8281 }
8282
8283 /* Output the encoding of an attribute value.  */
8284
8285 static void
8286 output_value_format (dw_attr_ref a)
8287 {
8288   enum dwarf_form form = value_format (a);
8289
8290   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8291 }
8292
8293 /* Output the .debug_abbrev section which defines the DIE abbreviation
8294    table.  */
8295
8296 static void
8297 output_abbrev_section (void)
8298 {
8299   unsigned long abbrev_id;
8300
8301   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8302     {
8303       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8304       unsigned ix;
8305       dw_attr_ref a_attr;
8306
8307       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8308       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8309                                    dwarf_tag_name (abbrev->die_tag));
8310
8311       if (abbrev->die_child != NULL)
8312         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8313       else
8314         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8315
8316       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8317            ix++)
8318         {
8319           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8320                                        dwarf_attr_name (a_attr->dw_attr));
8321           output_value_format (a_attr);
8322         }
8323
8324       dw2_asm_output_data (1, 0, NULL);
8325       dw2_asm_output_data (1, 0, NULL);
8326     }
8327
8328   /* Terminate the table.  */
8329   dw2_asm_output_data (1, 0, NULL);
8330 }
8331
8332 /* Output a symbol we can use to refer to this DIE from another CU.  */
8333
8334 static inline void
8335 output_die_symbol (dw_die_ref die)
8336 {
8337   char *sym = die->die_symbol;
8338
8339   if (sym == 0)
8340     return;
8341
8342   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8343     /* We make these global, not weak; if the target doesn't support
8344        .linkonce, it doesn't support combining the sections, so debugging
8345        will break.  */
8346     targetm.asm_out.globalize_label (asm_out_file, sym);
8347
8348   ASM_OUTPUT_LABEL (asm_out_file, sym);
8349 }
8350
8351 /* Return a new location list, given the begin and end range, and the
8352    expression. gensym tells us whether to generate a new internal symbol for
8353    this location list node, which is done for the head of the list only.  */
8354
8355 static inline dw_loc_list_ref
8356 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8357               const char *section, unsigned int gensym)
8358 {
8359   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8360
8361   retlist->begin = begin;
8362   retlist->end = end;
8363   retlist->expr = expr;
8364   retlist->section = section;
8365   if (gensym)
8366     retlist->ll_symbol = gen_internal_sym ("LLST");
8367
8368   return retlist;
8369 }
8370
8371 /* Add a location description expression to a location list.  */
8372
8373 static inline void
8374 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8375                            const char *begin, const char *end,
8376                            const char *section)
8377 {
8378   dw_loc_list_ref *d;
8379
8380   /* Find the end of the chain.  */
8381   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8382     ;
8383
8384   /* Add a new location list node to the list.  */
8385   *d = new_loc_list (descr, begin, end, section, 0);
8386 }
8387
8388 /* Output the location list given to us.  */
8389
8390 static void
8391 output_loc_list (dw_loc_list_ref list_head)
8392 {
8393   dw_loc_list_ref curr = list_head;
8394
8395   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8396
8397   /* Walk the location list, and output each range + expression.  */
8398   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8399     {
8400       unsigned long size;
8401       /* Don't output an entry that starts and ends at the same address.  */
8402       if (strcmp (curr->begin, curr->end) == 0)
8403         continue;
8404       if (!have_multiple_function_sections)
8405         {
8406           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8407                                 "Location list begin address (%s)",
8408                                 list_head->ll_symbol);
8409           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8410                                 "Location list end address (%s)",
8411                                 list_head->ll_symbol);
8412         }
8413       else
8414         {
8415           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8416                                "Location list begin address (%s)",
8417                                list_head->ll_symbol);
8418           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8419                                "Location list end address (%s)",
8420                                list_head->ll_symbol);
8421         }
8422       size = size_of_locs (curr->expr);
8423
8424       /* Output the block length for this list of location operations.  */
8425       gcc_assert (size <= 0xffff);
8426       dw2_asm_output_data (2, size, "%s", "Location expression size");
8427
8428       output_loc_sequence (curr->expr);
8429     }
8430
8431   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8432                        "Location list terminator begin (%s)",
8433                        list_head->ll_symbol);
8434   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8435                        "Location list terminator end (%s)",
8436                        list_head->ll_symbol);
8437 }
8438
8439 /* Output the DIE and its attributes.  Called recursively to generate
8440    the definitions of each child DIE.  */
8441
8442 static void
8443 output_die (dw_die_ref die)
8444 {
8445   dw_attr_ref a;
8446   dw_die_ref c;
8447   unsigned long size;
8448   unsigned ix;
8449
8450   /* If someone in another CU might refer to us, set up a symbol for
8451      them to point to.  */
8452   if (die->die_symbol)
8453     output_die_symbol (die);
8454
8455   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8456                                (unsigned long)die->die_offset,
8457                                dwarf_tag_name (die->die_tag));
8458
8459   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8460     {
8461       const char *name = dwarf_attr_name (a->dw_attr);
8462
8463       switch (AT_class (a))
8464         {
8465         case dw_val_class_addr:
8466           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8467           break;
8468
8469         case dw_val_class_offset:
8470           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8471                                "%s", name);
8472           break;
8473
8474         case dw_val_class_range_list:
8475           {
8476             char *p = strchr (ranges_section_label, '\0');
8477
8478             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8479                      a->dw_attr_val.v.val_offset);
8480             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8481                                    debug_ranges_section, "%s", name);
8482             *p = '\0';
8483           }
8484           break;
8485
8486         case dw_val_class_loc:
8487           size = size_of_locs (AT_loc (a));
8488
8489           /* Output the block length for this list of location operations.  */
8490           dw2_asm_output_data (constant_size (size), size, "%s", name);
8491
8492           output_loc_sequence (AT_loc (a));
8493           break;
8494
8495         case dw_val_class_const:
8496           /* ??? It would be slightly more efficient to use a scheme like is
8497              used for unsigned constants below, but gdb 4.x does not sign
8498              extend.  Gdb 5.x does sign extend.  */
8499           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8500           break;
8501
8502         case dw_val_class_unsigned_const:
8503           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8504                                AT_unsigned (a), "%s", name);
8505           break;
8506
8507         case dw_val_class_long_long:
8508           {
8509             unsigned HOST_WIDE_INT first, second;
8510
8511             dw2_asm_output_data (1,
8512                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8513                                  "%s", name);
8514
8515             if (WORDS_BIG_ENDIAN)
8516               {
8517                 first = a->dw_attr_val.v.val_long_long.hi;
8518                 second = a->dw_attr_val.v.val_long_long.low;
8519               }
8520             else
8521               {
8522                 first = a->dw_attr_val.v.val_long_long.low;
8523                 second = a->dw_attr_val.v.val_long_long.hi;
8524               }
8525
8526             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8527                                  first, "long long constant");
8528             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8529                                  second, NULL);
8530           }
8531           break;
8532
8533         case dw_val_class_vec:
8534           {
8535             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8536             unsigned int len = a->dw_attr_val.v.val_vec.length;
8537             unsigned int i;
8538             unsigned char *p;
8539
8540             dw2_asm_output_data (constant_size (len * elt_size),
8541                                  len * elt_size, "%s", name);
8542             if (elt_size > sizeof (HOST_WIDE_INT))
8543               {
8544                 elt_size /= 2;
8545                 len *= 2;
8546               }
8547             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8548                  i < len;
8549                  i++, p += elt_size)
8550               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8551                                    "fp or vector constant word %u", i);
8552             break;
8553           }
8554
8555         case dw_val_class_flag:
8556           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8557           break;
8558
8559         case dw_val_class_loc_list:
8560           {
8561             char *sym = AT_loc_list (a)->ll_symbol;
8562
8563             gcc_assert (sym);
8564             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8565                                    "%s", name);
8566           }
8567           break;
8568
8569         case dw_val_class_die_ref:
8570           if (AT_ref_external (a))
8571             {
8572               char *sym = AT_ref (a)->die_symbol;
8573               int size;
8574
8575               gcc_assert (sym);
8576
8577               /* In DWARF2, DW_FORM_ref_addr is sized by target address
8578                  length, whereas in DWARF3 it's always sized as an offset.  */
8579               if (dwarf_version == 2)
8580                 size = DWARF2_ADDR_SIZE;
8581               else
8582                 size = DWARF_OFFSET_SIZE;
8583               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
8584             }
8585           else
8586             {
8587               gcc_assert (AT_ref (a)->die_offset);
8588               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8589                                    "%s", name);
8590             }
8591           break;
8592
8593         case dw_val_class_fde_ref:
8594           {
8595             char l1[20];
8596
8597             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8598                                          a->dw_attr_val.v.val_fde_index * 2);
8599             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8600                                    "%s", name);
8601           }
8602           break;
8603
8604         case dw_val_class_lbl_id:
8605           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8606           break;
8607
8608         case dw_val_class_lineptr:
8609           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8610                                  debug_line_section, "%s", name);
8611           break;
8612
8613         case dw_val_class_macptr:
8614           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8615                                  debug_macinfo_section, "%s", name);
8616           break;
8617
8618         case dw_val_class_str:
8619           if (AT_string_form (a) == DW_FORM_strp)
8620             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8621                                    a->dw_attr_val.v.val_str->label,
8622                                    debug_str_section,
8623                                    "%s: \"%s\"", name, AT_string (a));
8624           else
8625             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8626           break;
8627
8628         case dw_val_class_file:
8629           {
8630             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8631
8632             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8633                                  a->dw_attr_val.v.val_file->filename);
8634             break;
8635           }
8636
8637         default:
8638           gcc_unreachable ();
8639         }
8640     }
8641
8642   FOR_EACH_CHILD (die, c, output_die (c));
8643
8644   /* Add null byte to terminate sibling list.  */
8645   if (die->die_child != NULL)
8646     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8647                          (unsigned long) die->die_offset);
8648 }
8649
8650 /* Output the compilation unit that appears at the beginning of the
8651    .debug_info section, and precedes the DIE descriptions.  */
8652
8653 static void
8654 output_compilation_unit_header (void)
8655 {
8656   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8657     dw2_asm_output_data (4, 0xffffffff,
8658       "Initial length escape value indicating 64-bit DWARF extension");
8659   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8660                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8661                        "Length of Compilation Unit Info");
8662   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8663   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8664                          debug_abbrev_section,
8665                          "Offset Into Abbrev. Section");
8666   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8667 }
8668
8669 /* Output the compilation unit DIE and its children.  */
8670
8671 static void
8672 output_comp_unit (dw_die_ref die, int output_if_empty)
8673 {
8674   const char *secname;
8675   char *oldsym, *tmp;
8676
8677   /* Unless we are outputting main CU, we may throw away empty ones.  */
8678   if (!output_if_empty && die->die_child == NULL)
8679     return;
8680
8681   /* Even if there are no children of this DIE, we must output the information
8682      about the compilation unit.  Otherwise, on an empty translation unit, we
8683      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8684      will then complain when examining the file.  First mark all the DIEs in
8685      this CU so we know which get local refs.  */
8686   mark_dies (die);
8687
8688   build_abbrev_table (die);
8689
8690   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8691   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8692   calc_die_sizes (die);
8693
8694   oldsym = die->die_symbol;
8695   if (oldsym)
8696     {
8697       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8698
8699       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8700       secname = tmp;
8701       die->die_symbol = NULL;
8702       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8703     }
8704   else
8705     switch_to_section (debug_info_section);
8706
8707   /* Output debugging information.  */
8708   output_compilation_unit_header ();
8709   output_die (die);
8710
8711   /* Leave the marks on the main CU, so we can check them in
8712      output_pubnames.  */
8713   if (oldsym)
8714     {
8715       unmark_dies (die);
8716       die->die_symbol = oldsym;
8717     }
8718 }
8719
8720 /* Return the DWARF2/3 pubname associated with a decl.  */
8721
8722 static const char *
8723 dwarf2_name (tree decl, int scope)
8724 {
8725   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8726 }
8727
8728 /* Add a new entry to .debug_pubnames if appropriate.  */
8729
8730 static void
8731 add_pubname_string (const char *str, dw_die_ref die)
8732 {
8733   pubname_entry e;
8734
8735   e.die = die;
8736   e.name = xstrdup (str);
8737   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8738 }
8739
8740 static void
8741 add_pubname (tree decl, dw_die_ref die)
8742 {
8743   if (TREE_PUBLIC (decl))
8744     add_pubname_string (dwarf2_name (decl, 1), die);
8745 }
8746
8747 /* Add a new entry to .debug_pubtypes if appropriate.  */
8748
8749 static void
8750 add_pubtype (tree decl, dw_die_ref die)
8751 {
8752   pubname_entry e;
8753
8754   e.name = NULL;
8755   if ((TREE_PUBLIC (decl)
8756        || die->die_parent == comp_unit_die)
8757       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8758     {
8759       e.die = die;
8760       if (TYPE_P (decl))
8761         {
8762           if (TYPE_NAME (decl))
8763             {
8764               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8765                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8766               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8767                        && DECL_NAME (TYPE_NAME (decl)))
8768                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8769               else
8770                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8771             }
8772         }
8773       else
8774         e.name = xstrdup (dwarf2_name (decl, 1));
8775
8776       /* If we don't have a name for the type, there's no point in adding
8777          it to the table.  */
8778       if (e.name && e.name[0] != '\0')
8779         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8780     }
8781 }
8782
8783 /* Output the public names table used to speed up access to externally
8784    visible names; or the public types table used to find type definitions.  */
8785
8786 static void
8787 output_pubnames (VEC (pubname_entry, gc) * names)
8788 {
8789   unsigned i;
8790   unsigned long pubnames_length = size_of_pubnames (names);
8791   pubname_ref pub;
8792
8793   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8794     dw2_asm_output_data (4, 0xffffffff,
8795       "Initial length escape value indicating 64-bit DWARF extension");
8796   if (names == pubname_table)
8797     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8798                          "Length of Public Names Info");
8799   else
8800     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8801                          "Length of Public Type Names Info");
8802   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8803   dw2_asm_output_data (2, 2, "DWARF Version");
8804   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8805                          debug_info_section,
8806                          "Offset of Compilation Unit Info");
8807   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8808                        "Compilation Unit Length");
8809
8810   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8811     {
8812       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8813       if (names == pubname_table)
8814         gcc_assert (pub->die->die_mark);
8815
8816       if (names != pubtype_table
8817           || pub->die->die_offset != 0
8818           || !flag_eliminate_unused_debug_types)
8819         {
8820           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8821                                "DIE offset");
8822
8823           dw2_asm_output_nstring (pub->name, -1, "external name");
8824         }
8825     }
8826
8827   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8828 }
8829
8830 /* Add a new entry to .debug_aranges if appropriate.  */
8831
8832 static void
8833 add_arange (tree decl, dw_die_ref die)
8834 {
8835   if (! DECL_SECTION_NAME (decl))
8836     return;
8837
8838   if (arange_table_in_use == arange_table_allocated)
8839     {
8840       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8841       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8842                                     arange_table_allocated);
8843       memset (arange_table + arange_table_in_use, 0,
8844               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8845     }
8846
8847   arange_table[arange_table_in_use++] = die;
8848 }
8849
8850 /* Output the information that goes into the .debug_aranges table.
8851    Namely, define the beginning and ending address range of the
8852    text section generated for this compilation unit.  */
8853
8854 static void
8855 output_aranges (void)
8856 {
8857   unsigned i;
8858   unsigned long aranges_length = size_of_aranges ();
8859
8860   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8861     dw2_asm_output_data (4, 0xffffffff,
8862       "Initial length escape value indicating 64-bit DWARF extension");
8863   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8864                        "Length of Address Ranges Info");
8865   /* Version number for aranges is still 2, even in DWARF3.  */
8866   dw2_asm_output_data (2, 2, "DWARF Version");
8867   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8868                          debug_info_section,
8869                          "Offset of Compilation Unit Info");
8870   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8871   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8872
8873   /* We need to align to twice the pointer size here.  */
8874   if (DWARF_ARANGES_PAD_SIZE)
8875     {
8876       /* Pad using a 2 byte words so that padding is correct for any
8877          pointer size.  */
8878       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8879                            2 * DWARF2_ADDR_SIZE);
8880       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8881         dw2_asm_output_data (2, 0, NULL);
8882     }
8883
8884   /* It is necessary not to output these entries if the sections were
8885      not used; if the sections were not used, the length will be 0 and
8886      the address may end up as 0 if the section is discarded by ld
8887      --gc-sections, leaving an invalid (0, 0) entry that can be
8888      confused with the terminator.  */
8889   if (text_section_used)
8890     {
8891       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8892       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8893                             text_section_label, "Length");
8894     }
8895   if (cold_text_section_used)
8896     {
8897       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8898                            "Address");
8899       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8900                             cold_text_section_label, "Length");
8901     }
8902
8903   for (i = 0; i < arange_table_in_use; i++)
8904     {
8905       dw_die_ref die = arange_table[i];
8906
8907       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8908       gcc_assert (die->die_mark);
8909
8910       if (die->die_tag == DW_TAG_subprogram)
8911         {
8912           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8913                                "Address");
8914           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8915                                 get_AT_low_pc (die), "Length");
8916         }
8917       else
8918         {
8919           /* A static variable; extract the symbol from DW_AT_location.
8920              Note that this code isn't currently hit, as we only emit
8921              aranges for functions (jason 9/23/99).  */
8922           dw_attr_ref a = get_AT (die, DW_AT_location);
8923           dw_loc_descr_ref loc;
8924
8925           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8926
8927           loc = AT_loc (a);
8928           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8929
8930           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8931                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8932           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8933                                get_AT_unsigned (die, DW_AT_byte_size),
8934                                "Length");
8935         }
8936     }
8937
8938   /* Output the terminator words.  */
8939   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8940   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8941 }
8942
8943 /* Add a new entry to .debug_ranges.  Return the offset at which it
8944    was placed.  */
8945
8946 static unsigned int
8947 add_ranges_num (int num)
8948 {
8949   unsigned int in_use = ranges_table_in_use;
8950
8951   if (in_use == ranges_table_allocated)
8952     {
8953       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8954       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8955                                     ranges_table_allocated);
8956       memset (ranges_table + ranges_table_in_use, 0,
8957               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8958     }
8959
8960   ranges_table[in_use].num = num;
8961   ranges_table_in_use = in_use + 1;
8962
8963   return in_use * 2 * DWARF2_ADDR_SIZE;
8964 }
8965
8966 /* Add a new entry to .debug_ranges corresponding to a block, or a
8967    range terminator if BLOCK is NULL.  */
8968
8969 static unsigned int
8970 add_ranges (const_tree block)
8971 {
8972   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8973 }
8974
8975 /* Add a new entry to .debug_ranges corresponding to a pair of
8976    labels.  */
8977
8978 static unsigned int
8979 add_ranges_by_labels (const char *begin, const char *end)
8980 {
8981   unsigned int in_use = ranges_by_label_in_use;
8982
8983   if (in_use == ranges_by_label_allocated)
8984     {
8985       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8986       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8987                                        ranges_by_label,
8988                                        ranges_by_label_allocated);
8989       memset (ranges_by_label + ranges_by_label_in_use, 0,
8990               RANGES_TABLE_INCREMENT
8991               * sizeof (struct dw_ranges_by_label_struct));
8992     }
8993
8994   ranges_by_label[in_use].begin = begin;
8995   ranges_by_label[in_use].end = end;
8996   ranges_by_label_in_use = in_use + 1;
8997
8998   return add_ranges_num (-(int)in_use - 1);
8999 }
9000
9001 static void
9002 output_ranges (void)
9003 {
9004   unsigned i;
9005   static const char *const start_fmt = "Offset 0x%x";
9006   const char *fmt = start_fmt;
9007
9008   for (i = 0; i < ranges_table_in_use; i++)
9009     {
9010       int block_num = ranges_table[i].num;
9011
9012       if (block_num > 0)
9013         {
9014           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9015           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9016
9017           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9018           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9019
9020           /* If all code is in the text section, then the compilation
9021              unit base address defaults to DW_AT_low_pc, which is the
9022              base of the text section.  */
9023           if (!have_multiple_function_sections)
9024             {
9025               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9026                                     text_section_label,
9027                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9028               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9029                                     text_section_label, NULL);
9030             }
9031
9032           /* Otherwise, the compilation unit base address is zero,
9033              which allows us to use absolute addresses, and not worry
9034              about whether the target supports cross-section
9035              arithmetic.  */
9036           else
9037             {
9038               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9039                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9040               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9041             }
9042
9043           fmt = NULL;
9044         }
9045
9046       /* Negative block_num stands for an index into ranges_by_label.  */
9047       else if (block_num < 0)
9048         {
9049           int lab_idx = - block_num - 1;
9050
9051           if (!have_multiple_function_sections)
9052             {
9053               gcc_unreachable ();
9054 #if 0
9055               /* If we ever use add_ranges_by_labels () for a single
9056                  function section, all we have to do is to take out
9057                  the #if 0 above.  */
9058               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9059                                     ranges_by_label[lab_idx].begin,
9060                                     text_section_label,
9061                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9062               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9063                                     ranges_by_label[lab_idx].end,
9064                                     text_section_label, NULL);
9065 #endif
9066             }
9067           else
9068             {
9069               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9070                                    ranges_by_label[lab_idx].begin,
9071                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9072               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9073                                    ranges_by_label[lab_idx].end,
9074                                    NULL);
9075             }
9076         }
9077       else
9078         {
9079           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9080           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9081           fmt = start_fmt;
9082         }
9083     }
9084 }
9085
9086 /* Data structure containing information about input files.  */
9087 struct file_info
9088 {
9089   const char *path;     /* Complete file name.  */
9090   const char *fname;    /* File name part.  */
9091   int length;           /* Length of entire string.  */
9092   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9093   int dir_idx;          /* Index in directory table.  */
9094 };
9095
9096 /* Data structure containing information about directories with source
9097    files.  */
9098 struct dir_info
9099 {
9100   const char *path;     /* Path including directory name.  */
9101   int length;           /* Path length.  */
9102   int prefix;           /* Index of directory entry which is a prefix.  */
9103   int count;            /* Number of files in this directory.  */
9104   int dir_idx;          /* Index of directory used as base.  */
9105 };
9106
9107 /* Callback function for file_info comparison.  We sort by looking at
9108    the directories in the path.  */
9109
9110 static int
9111 file_info_cmp (const void *p1, const void *p2)
9112 {
9113   const struct file_info *const s1 = (const struct file_info *) p1;
9114   const struct file_info *const s2 = (const struct file_info *) p2;
9115   const unsigned char *cp1;
9116   const unsigned char *cp2;
9117
9118   /* Take care of file names without directories.  We need to make sure that
9119      we return consistent values to qsort since some will get confused if
9120      we return the same value when identical operands are passed in opposite
9121      orders.  So if neither has a directory, return 0 and otherwise return
9122      1 or -1 depending on which one has the directory.  */
9123   if ((s1->path == s1->fname || s2->path == s2->fname))
9124     return (s2->path == s2->fname) - (s1->path == s1->fname);
9125
9126   cp1 = (const unsigned char *) s1->path;
9127   cp2 = (const unsigned char *) s2->path;
9128
9129   while (1)
9130     {
9131       ++cp1;
9132       ++cp2;
9133       /* Reached the end of the first path?  If so, handle like above.  */
9134       if ((cp1 == (const unsigned char *) s1->fname)
9135           || (cp2 == (const unsigned char *) s2->fname))
9136         return ((cp2 == (const unsigned char *) s2->fname)
9137                 - (cp1 == (const unsigned char *) s1->fname));
9138
9139       /* Character of current path component the same?  */
9140       else if (*cp1 != *cp2)
9141         return *cp1 - *cp2;
9142     }
9143 }
9144
9145 struct file_name_acquire_data
9146 {
9147   struct file_info *files;
9148   int used_files;
9149   int max_files;
9150 };
9151
9152 /* Traversal function for the hash table.  */
9153
9154 static int
9155 file_name_acquire (void ** slot, void *data)
9156 {
9157   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9158   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9159   struct file_info *fi;
9160   const char *f;
9161
9162   gcc_assert (fnad->max_files >= d->emitted_number);
9163
9164   if (! d->emitted_number)
9165     return 1;
9166
9167   gcc_assert (fnad->max_files != fnad->used_files);
9168
9169   fi = fnad->files + fnad->used_files++;
9170
9171   /* Skip all leading "./".  */
9172   f = d->filename;
9173   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9174     f += 2;
9175
9176   /* Create a new array entry.  */
9177   fi->path = f;
9178   fi->length = strlen (f);
9179   fi->file_idx = d;
9180
9181   /* Search for the file name part.  */
9182   f = strrchr (f, DIR_SEPARATOR);
9183 #if defined (DIR_SEPARATOR_2)
9184   {
9185     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9186
9187     if (g != NULL)
9188       {
9189         if (f == NULL || f < g)
9190           f = g;
9191       }
9192   }
9193 #endif
9194
9195   fi->fname = f == NULL ? fi->path : f + 1;
9196   return 1;
9197 }
9198
9199 /* Output the directory table and the file name table.  We try to minimize
9200    the total amount of memory needed.  A heuristic is used to avoid large
9201    slowdowns with many input files.  */
9202
9203 static void
9204 output_file_names (void)
9205 {
9206   struct file_name_acquire_data fnad;
9207   int numfiles;
9208   struct file_info *files;
9209   struct dir_info *dirs;
9210   int *saved;
9211   int *savehere;
9212   int *backmap;
9213   int ndirs;
9214   int idx_offset;
9215   int i;
9216   int idx;
9217
9218   if (!last_emitted_file)
9219     {
9220       dw2_asm_output_data (1, 0, "End directory table");
9221       dw2_asm_output_data (1, 0, "End file name table");
9222       return;
9223     }
9224
9225   numfiles = last_emitted_file->emitted_number;
9226
9227   /* Allocate the various arrays we need.  */
9228   files = XALLOCAVEC (struct file_info, numfiles);
9229   dirs = XALLOCAVEC (struct dir_info, numfiles);
9230
9231   fnad.files = files;
9232   fnad.used_files = 0;
9233   fnad.max_files = numfiles;
9234   htab_traverse (file_table, file_name_acquire, &fnad);
9235   gcc_assert (fnad.used_files == fnad.max_files);
9236
9237   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9238
9239   /* Find all the different directories used.  */
9240   dirs[0].path = files[0].path;
9241   dirs[0].length = files[0].fname - files[0].path;
9242   dirs[0].prefix = -1;
9243   dirs[0].count = 1;
9244   dirs[0].dir_idx = 0;
9245   files[0].dir_idx = 0;
9246   ndirs = 1;
9247
9248   for (i = 1; i < numfiles; i++)
9249     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9250         && memcmp (dirs[ndirs - 1].path, files[i].path,
9251                    dirs[ndirs - 1].length) == 0)
9252       {
9253         /* Same directory as last entry.  */
9254         files[i].dir_idx = ndirs - 1;
9255         ++dirs[ndirs - 1].count;
9256       }
9257     else
9258       {
9259         int j;
9260
9261         /* This is a new directory.  */
9262         dirs[ndirs].path = files[i].path;
9263         dirs[ndirs].length = files[i].fname - files[i].path;
9264         dirs[ndirs].count = 1;
9265         dirs[ndirs].dir_idx = ndirs;
9266         files[i].dir_idx = ndirs;
9267
9268         /* Search for a prefix.  */
9269         dirs[ndirs].prefix = -1;
9270         for (j = 0; j < ndirs; j++)
9271           if (dirs[j].length < dirs[ndirs].length
9272               && dirs[j].length > 1
9273               && (dirs[ndirs].prefix == -1
9274                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9275               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9276             dirs[ndirs].prefix = j;
9277
9278         ++ndirs;
9279       }
9280
9281   /* Now to the actual work.  We have to find a subset of the directories which
9282      allow expressing the file name using references to the directory table
9283      with the least amount of characters.  We do not do an exhaustive search
9284      where we would have to check out every combination of every single
9285      possible prefix.  Instead we use a heuristic which provides nearly optimal
9286      results in most cases and never is much off.  */
9287   saved = XALLOCAVEC (int, ndirs);
9288   savehere = XALLOCAVEC (int, ndirs);
9289
9290   memset (saved, '\0', ndirs * sizeof (saved[0]));
9291   for (i = 0; i < ndirs; i++)
9292     {
9293       int j;
9294       int total;
9295
9296       /* We can always save some space for the current directory.  But this
9297          does not mean it will be enough to justify adding the directory.  */
9298       savehere[i] = dirs[i].length;
9299       total = (savehere[i] - saved[i]) * dirs[i].count;
9300
9301       for (j = i + 1; j < ndirs; j++)
9302         {
9303           savehere[j] = 0;
9304           if (saved[j] < dirs[i].length)
9305             {
9306               /* Determine whether the dirs[i] path is a prefix of the
9307                  dirs[j] path.  */
9308               int k;
9309
9310               k = dirs[j].prefix;
9311               while (k != -1 && k != (int) i)
9312                 k = dirs[k].prefix;
9313
9314               if (k == (int) i)
9315                 {
9316                   /* Yes it is.  We can possibly save some memory by
9317                      writing the filenames in dirs[j] relative to
9318                      dirs[i].  */
9319                   savehere[j] = dirs[i].length;
9320                   total += (savehere[j] - saved[j]) * dirs[j].count;
9321                 }
9322             }
9323         }
9324
9325       /* Check whether we can save enough to justify adding the dirs[i]
9326          directory.  */
9327       if (total > dirs[i].length + 1)
9328         {
9329           /* It's worthwhile adding.  */
9330           for (j = i; j < ndirs; j++)
9331             if (savehere[j] > 0)
9332               {
9333                 /* Remember how much we saved for this directory so far.  */
9334                 saved[j] = savehere[j];
9335
9336                 /* Remember the prefix directory.  */
9337                 dirs[j].dir_idx = i;
9338               }
9339         }
9340     }
9341
9342   /* Emit the directory name table.  */
9343   idx = 1;
9344   idx_offset = dirs[0].length > 0 ? 1 : 0;
9345   for (i = 1 - idx_offset; i < ndirs; i++)
9346     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9347                             "Directory Entry: 0x%x", i + idx_offset);
9348
9349   dw2_asm_output_data (1, 0, "End directory table");
9350
9351   /* We have to emit them in the order of emitted_number since that's
9352      used in the debug info generation.  To do this efficiently we
9353      generate a back-mapping of the indices first.  */
9354   backmap = XALLOCAVEC (int, numfiles);
9355   for (i = 0; i < numfiles; i++)
9356     backmap[files[i].file_idx->emitted_number - 1] = i;
9357
9358   /* Now write all the file names.  */
9359   for (i = 0; i < numfiles; i++)
9360     {
9361       int file_idx = backmap[i];
9362       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9363
9364       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9365                               "File Entry: 0x%x", (unsigned) i + 1);
9366
9367       /* Include directory index.  */
9368       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9369
9370       /* Modification time.  */
9371       dw2_asm_output_data_uleb128 (0, NULL);
9372
9373       /* File length in bytes.  */
9374       dw2_asm_output_data_uleb128 (0, NULL);
9375     }
9376
9377   dw2_asm_output_data (1, 0, "End file name table");
9378 }
9379
9380
9381 /* Output the source line number correspondence information.  This
9382    information goes into the .debug_line section.  */
9383
9384 static void
9385 output_line_info (void)
9386 {
9387   char l1[20], l2[20], p1[20], p2[20];
9388   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9389   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9390   unsigned opc;
9391   unsigned n_op_args;
9392   unsigned long lt_index;
9393   unsigned long current_line;
9394   long line_offset;
9395   long line_delta;
9396   unsigned long current_file;
9397   unsigned long function;
9398
9399   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9400   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9401   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9402   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9403
9404   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9405     dw2_asm_output_data (4, 0xffffffff,
9406       "Initial length escape value indicating 64-bit DWARF extension");
9407   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9408                         "Length of Source Line Info");
9409   ASM_OUTPUT_LABEL (asm_out_file, l1);
9410
9411   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9412   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9413   ASM_OUTPUT_LABEL (asm_out_file, p1);
9414
9415   /* Define the architecture-dependent minimum instruction length (in
9416    bytes).  In this implementation of DWARF, this field is used for
9417    information purposes only.  Since GCC generates assembly language,
9418    we have no a priori knowledge of how many instruction bytes are
9419    generated for each source line, and therefore can use only the
9420    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9421    commands.  Accordingly, we fix this as `1', which is "correct
9422    enough" for all architectures, and don't let the target override.  */
9423   dw2_asm_output_data (1, 1,
9424                        "Minimum Instruction Length");
9425
9426   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9427                        "Default is_stmt_start flag");
9428   dw2_asm_output_data (1, DWARF_LINE_BASE,
9429                        "Line Base Value (Special Opcodes)");
9430   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9431                        "Line Range Value (Special Opcodes)");
9432   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9433                        "Special Opcode Base");
9434
9435   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9436     {
9437       switch (opc)
9438         {
9439         case DW_LNS_advance_pc:
9440         case DW_LNS_advance_line:
9441         case DW_LNS_set_file:
9442         case DW_LNS_set_column:
9443         case DW_LNS_fixed_advance_pc:
9444           n_op_args = 1;
9445           break;
9446         default:
9447           n_op_args = 0;
9448           break;
9449         }
9450
9451       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9452                            opc, n_op_args);
9453     }
9454
9455   /* Write out the information about the files we use.  */
9456   output_file_names ();
9457   ASM_OUTPUT_LABEL (asm_out_file, p2);
9458
9459   /* We used to set the address register to the first location in the text
9460      section here, but that didn't accomplish anything since we already
9461      have a line note for the opening brace of the first function.  */
9462
9463   /* Generate the line number to PC correspondence table, encoded as
9464      a series of state machine operations.  */
9465   current_file = 1;
9466   current_line = 1;
9467
9468   if (cfun && in_cold_section_p)
9469     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9470   else
9471     strcpy (prev_line_label, text_section_label);
9472   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9473     {
9474       dw_line_info_ref line_info = &line_info_table[lt_index];
9475
9476 #if 0
9477       /* Disable this optimization for now; GDB wants to see two line notes
9478          at the beginning of a function so it can find the end of the
9479          prologue.  */
9480
9481       /* Don't emit anything for redundant notes.  Just updating the
9482          address doesn't accomplish anything, because we already assume
9483          that anything after the last address is this line.  */
9484       if (line_info->dw_line_num == current_line
9485           && line_info->dw_file_num == current_file)
9486         continue;
9487 #endif
9488
9489       /* Emit debug info for the address of the current line.
9490
9491          Unfortunately, we have little choice here currently, and must always
9492          use the most general form.  GCC does not know the address delta
9493          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9494          attributes which will give an upper bound on the address range.  We
9495          could perhaps use length attributes to determine when it is safe to
9496          use DW_LNS_fixed_advance_pc.  */
9497
9498       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9499       if (0)
9500         {
9501           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9502           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9503                                "DW_LNS_fixed_advance_pc");
9504           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9505         }
9506       else
9507         {
9508           /* This can handle any delta.  This takes
9509              4+DWARF2_ADDR_SIZE bytes.  */
9510           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9511           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9512           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9513           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9514         }
9515
9516       strcpy (prev_line_label, line_label);
9517
9518       /* Emit debug info for the source file of the current line, if
9519          different from the previous line.  */
9520       if (line_info->dw_file_num != current_file)
9521         {
9522           current_file = line_info->dw_file_num;
9523           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9524           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9525         }
9526
9527       /* Emit debug info for the current line number, choosing the encoding
9528          that uses the least amount of space.  */
9529       if (line_info->dw_line_num != current_line)
9530         {
9531           line_offset = line_info->dw_line_num - current_line;
9532           line_delta = line_offset - DWARF_LINE_BASE;
9533           current_line = line_info->dw_line_num;
9534           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9535             /* This can handle deltas from -10 to 234, using the current
9536                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9537                takes 1 byte.  */
9538             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9539                                  "line %lu", current_line);
9540           else
9541             {
9542               /* This can handle any delta.  This takes at least 4 bytes,
9543                  depending on the value being encoded.  */
9544               dw2_asm_output_data (1, DW_LNS_advance_line,
9545                                    "advance to line %lu", current_line);
9546               dw2_asm_output_data_sleb128 (line_offset, NULL);
9547               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9548             }
9549         }
9550       else
9551         /* We still need to start a new row, so output a copy insn.  */
9552         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9553     }
9554
9555   /* Emit debug info for the address of the end of the function.  */
9556   if (0)
9557     {
9558       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9559                            "DW_LNS_fixed_advance_pc");
9560       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9561     }
9562   else
9563     {
9564       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9565       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9566       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9567       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9568     }
9569
9570   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9571   dw2_asm_output_data_uleb128 (1, NULL);
9572   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9573
9574   function = 0;
9575   current_file = 1;
9576   current_line = 1;
9577   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9578     {
9579       dw_separate_line_info_ref line_info
9580         = &separate_line_info_table[lt_index];
9581
9582 #if 0
9583       /* Don't emit anything for redundant notes.  */
9584       if (line_info->dw_line_num == current_line
9585           && line_info->dw_file_num == current_file
9586           && line_info->function == function)
9587         goto cont;
9588 #endif
9589
9590       /* Emit debug info for the address of the current line.  If this is
9591          a new function, or the first line of a function, then we need
9592          to handle it differently.  */
9593       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9594                                    lt_index);
9595       if (function != line_info->function)
9596         {
9597           function = line_info->function;
9598
9599           /* Set the address register to the first line in the function.  */
9600           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9601           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9602           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9603           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9604         }
9605       else
9606         {
9607           /* ??? See the DW_LNS_advance_pc comment above.  */
9608           if (0)
9609             {
9610               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9611                                    "DW_LNS_fixed_advance_pc");
9612               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9613             }
9614           else
9615             {
9616               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9617               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9618               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9619               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9620             }
9621         }
9622
9623       strcpy (prev_line_label, line_label);
9624
9625       /* Emit debug info for the source file of the current line, if
9626          different from the previous line.  */
9627       if (line_info->dw_file_num != current_file)
9628         {
9629           current_file = line_info->dw_file_num;
9630           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9631           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9632         }
9633
9634       /* Emit debug info for the current line number, choosing the encoding
9635          that uses the least amount of space.  */
9636       if (line_info->dw_line_num != current_line)
9637         {
9638           line_offset = line_info->dw_line_num - current_line;
9639           line_delta = line_offset - DWARF_LINE_BASE;
9640           current_line = line_info->dw_line_num;
9641           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9642             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9643                                  "line %lu", current_line);
9644           else
9645             {
9646               dw2_asm_output_data (1, DW_LNS_advance_line,
9647                                    "advance to line %lu", current_line);
9648               dw2_asm_output_data_sleb128 (line_offset, NULL);
9649               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9650             }
9651         }
9652       else
9653         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9654
9655 #if 0
9656     cont:
9657 #endif
9658
9659       lt_index++;
9660
9661       /* If we're done with a function, end its sequence.  */
9662       if (lt_index == separate_line_info_table_in_use
9663           || separate_line_info_table[lt_index].function != function)
9664         {
9665           current_file = 1;
9666           current_line = 1;
9667
9668           /* Emit debug info for the address of the end of the function.  */
9669           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9670           if (0)
9671             {
9672               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9673                                    "DW_LNS_fixed_advance_pc");
9674               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9675             }
9676           else
9677             {
9678               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9679               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9680               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9681               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9682             }
9683
9684           /* Output the marker for the end of this sequence.  */
9685           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9686           dw2_asm_output_data_uleb128 (1, NULL);
9687           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9688         }
9689     }
9690
9691   /* Output the marker for the end of the line number info.  */
9692   ASM_OUTPUT_LABEL (asm_out_file, l2);
9693 }
9694 \f
9695 /* Given a pointer to a tree node for some base type, return a pointer to
9696    a DIE that describes the given type.
9697
9698    This routine must only be called for GCC type nodes that correspond to
9699    Dwarf base (fundamental) types.  */
9700
9701 static dw_die_ref
9702 base_type_die (tree type)
9703 {
9704   dw_die_ref base_type_result;
9705   enum dwarf_type encoding;
9706
9707   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9708     return 0;
9709
9710   /* If this is a subtype that should not be emitted as a subrange type,
9711      use the base type.  See subrange_type_for_debug_p.  */
9712   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9713     type = TREE_TYPE (type);
9714
9715   switch (TREE_CODE (type))
9716     {
9717     case INTEGER_TYPE:
9718       if (TYPE_STRING_FLAG (type))
9719         {
9720           if (TYPE_UNSIGNED (type))
9721             encoding = DW_ATE_unsigned_char;
9722           else
9723             encoding = DW_ATE_signed_char;
9724         }
9725       else if (TYPE_UNSIGNED (type))
9726         encoding = DW_ATE_unsigned;
9727       else
9728         encoding = DW_ATE_signed;
9729       break;
9730
9731     case REAL_TYPE:
9732       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9733         encoding = DW_ATE_decimal_float;
9734       else
9735         encoding = DW_ATE_float;
9736       break;
9737
9738     case FIXED_POINT_TYPE:
9739       if (TYPE_UNSIGNED (type))
9740         encoding = DW_ATE_unsigned_fixed;
9741       else
9742         encoding = DW_ATE_signed_fixed;
9743       break;
9744
9745       /* Dwarf2 doesn't know anything about complex ints, so use
9746          a user defined type for it.  */
9747     case COMPLEX_TYPE:
9748       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9749         encoding = DW_ATE_complex_float;
9750       else
9751         encoding = DW_ATE_lo_user;
9752       break;
9753
9754     case BOOLEAN_TYPE:
9755       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9756       encoding = DW_ATE_boolean;
9757       break;
9758
9759     default:
9760       /* No other TREE_CODEs are Dwarf fundamental types.  */
9761       gcc_unreachable ();
9762     }
9763
9764   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9765
9766   /* This probably indicates a bug.  */
9767   if (! TYPE_NAME (type))
9768     add_name_attribute (base_type_result, "__unknown__");
9769
9770   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9771                    int_size_in_bytes (type));
9772   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9773
9774   return base_type_result;
9775 }
9776
9777 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9778    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9779
9780 static inline int
9781 is_base_type (tree type)
9782 {
9783   switch (TREE_CODE (type))
9784     {
9785     case ERROR_MARK:
9786     case VOID_TYPE:
9787     case INTEGER_TYPE:
9788     case REAL_TYPE:
9789     case FIXED_POINT_TYPE:
9790     case COMPLEX_TYPE:
9791     case BOOLEAN_TYPE:
9792       return 1;
9793
9794     case ARRAY_TYPE:
9795     case RECORD_TYPE:
9796     case UNION_TYPE:
9797     case QUAL_UNION_TYPE:
9798     case ENUMERAL_TYPE:
9799     case FUNCTION_TYPE:
9800     case METHOD_TYPE:
9801     case POINTER_TYPE:
9802     case REFERENCE_TYPE:
9803     case OFFSET_TYPE:
9804     case LANG_TYPE:
9805     case VECTOR_TYPE:
9806       return 0;
9807
9808     default:
9809       gcc_unreachable ();
9810     }
9811
9812   return 0;
9813 }
9814
9815 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9816    node, return the size in bits for the type if it is a constant, or else
9817    return the alignment for the type if the type's size is not constant, or
9818    else return BITS_PER_WORD if the type actually turns out to be an
9819    ERROR_MARK node.  */
9820
9821 static inline unsigned HOST_WIDE_INT
9822 simple_type_size_in_bits (const_tree type)
9823 {
9824   if (TREE_CODE (type) == ERROR_MARK)
9825     return BITS_PER_WORD;
9826   else if (TYPE_SIZE (type) == NULL_TREE)
9827     return 0;
9828   else if (host_integerp (TYPE_SIZE (type), 1))
9829     return tree_low_cst (TYPE_SIZE (type), 1);
9830   else
9831     return TYPE_ALIGN (type);
9832 }
9833
9834 /*  Given a pointer to a tree node for a subrange type, return a pointer
9835     to a DIE that describes the given type.  */
9836
9837 static dw_die_ref
9838 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9839 {
9840   dw_die_ref subrange_die;
9841   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9842
9843   if (context_die == NULL)
9844     context_die = comp_unit_die;
9845
9846   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9847
9848   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9849     {
9850       /* The size of the subrange type and its base type do not match,
9851          so we need to generate a size attribute for the subrange type.  */
9852       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9853     }
9854
9855   if (low)
9856     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9857   if (high)
9858     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9859
9860   return subrange_die;
9861 }
9862
9863 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9864    entry that chains various modifiers in front of the given type.  */
9865
9866 static dw_die_ref
9867 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9868                    dw_die_ref context_die)
9869 {
9870   enum tree_code code = TREE_CODE (type);
9871   dw_die_ref mod_type_die;
9872   dw_die_ref sub_die = NULL;
9873   tree item_type = NULL;
9874   tree qualified_type;
9875   tree name, low, high;
9876
9877   if (code == ERROR_MARK)
9878     return NULL;
9879
9880   /* See if we already have the appropriately qualified variant of
9881      this type.  */
9882   qualified_type
9883     = get_qualified_type (type,
9884                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9885                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9886
9887   /* If we do, then we can just use its DIE, if it exists.  */
9888   if (qualified_type)
9889     {
9890       mod_type_die = lookup_type_die (qualified_type);
9891       if (mod_type_die)
9892         return mod_type_die;
9893     }
9894
9895   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9896
9897   /* Handle C typedef types.  */
9898   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9899     {
9900       tree dtype = TREE_TYPE (name);
9901
9902       if (qualified_type == dtype)
9903         {
9904           /* For a named type, use the typedef.  */
9905           gen_type_die (qualified_type, context_die);
9906           return lookup_type_die (qualified_type);
9907         }
9908       else if (is_const_type < TYPE_READONLY (dtype)
9909                || is_volatile_type < TYPE_VOLATILE (dtype)
9910                || (is_const_type <= TYPE_READONLY (dtype)
9911                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9912                    && DECL_ORIGINAL_TYPE (name) != type))
9913         /* cv-unqualified version of named type.  Just use the unnamed
9914            type to which it refers.  */
9915         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9916                                   is_const_type, is_volatile_type,
9917                                   context_die);
9918       /* Else cv-qualified version of named type; fall through.  */
9919     }
9920
9921   if (is_const_type)
9922     {
9923       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9924       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9925     }
9926   else if (is_volatile_type)
9927     {
9928       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9929       sub_die = modified_type_die (type, 0, 0, context_die);
9930     }
9931   else if (code == POINTER_TYPE)
9932     {
9933       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9934       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9935                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9936       item_type = TREE_TYPE (type);
9937     }
9938   else if (code == REFERENCE_TYPE)
9939     {
9940       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9941       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9942                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9943       item_type = TREE_TYPE (type);
9944     }
9945   else if (code == INTEGER_TYPE
9946            && TREE_TYPE (type) != NULL_TREE
9947            && subrange_type_for_debug_p (type, &low, &high))
9948     {
9949       mod_type_die = subrange_type_die (type, low, high, context_die);
9950       item_type = TREE_TYPE (type);
9951     }
9952   else if (is_base_type (type))
9953     mod_type_die = base_type_die (type);
9954   else
9955     {
9956       gen_type_die (type, context_die);
9957
9958       /* We have to get the type_main_variant here (and pass that to the
9959          `lookup_type_die' routine) because the ..._TYPE node we have
9960          might simply be a *copy* of some original type node (where the
9961          copy was created to help us keep track of typedef names) and
9962          that copy might have a different TYPE_UID from the original
9963          ..._TYPE node.  */
9964       if (TREE_CODE (type) != VECTOR_TYPE)
9965         return lookup_type_die (type_main_variant (type));
9966       else
9967         /* Vectors have the debugging information in the type,
9968            not the main variant.  */
9969         return lookup_type_die (type);
9970     }
9971
9972   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9973      don't output a DW_TAG_typedef, since there isn't one in the
9974      user's program; just attach a DW_AT_name to the type.  */
9975   if (name
9976       && (TREE_CODE (name) != TYPE_DECL
9977           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9978     {
9979       if (TREE_CODE (name) == TYPE_DECL)
9980         /* Could just call add_name_and_src_coords_attributes here,
9981            but since this is a builtin type it doesn't have any
9982            useful source coordinates anyway.  */
9983         name = DECL_NAME (name);
9984       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9985     }
9986
9987   if (qualified_type)
9988     equate_type_number_to_die (qualified_type, mod_type_die);
9989
9990   if (item_type)
9991     /* We must do this after the equate_type_number_to_die call, in case
9992        this is a recursive type.  This ensures that the modified_type_die
9993        recursion will terminate even if the type is recursive.  Recursive
9994        types are possible in Ada.  */
9995     sub_die = modified_type_die (item_type,
9996                                  TYPE_READONLY (item_type),
9997                                  TYPE_VOLATILE (item_type),
9998                                  context_die);
9999
10000   if (sub_die != NULL)
10001     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10002
10003   return mod_type_die;
10004 }
10005
10006 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10007    an enumerated type.  */
10008
10009 static inline int
10010 type_is_enum (const_tree type)
10011 {
10012   return TREE_CODE (type) == ENUMERAL_TYPE;
10013 }
10014
10015 /* Return the DBX register number described by a given RTL node.  */
10016
10017 static unsigned int
10018 dbx_reg_number (const_rtx rtl)
10019 {
10020   unsigned regno = REGNO (rtl);
10021
10022   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10023
10024 #ifdef LEAF_REG_REMAP
10025   if (current_function_uses_only_leaf_regs)
10026     {
10027       int leaf_reg = LEAF_REG_REMAP (regno);
10028       if (leaf_reg != -1)
10029         regno = (unsigned) leaf_reg;
10030     }
10031 #endif
10032
10033   return DBX_REGISTER_NUMBER (regno);
10034 }
10035
10036 /* Optionally add a DW_OP_piece term to a location description expression.
10037    DW_OP_piece is only added if the location description expression already
10038    doesn't end with DW_OP_piece.  */
10039
10040 static void
10041 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10042 {
10043   dw_loc_descr_ref loc;
10044
10045   if (*list_head != NULL)
10046     {
10047       /* Find the end of the chain.  */
10048       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10049         ;
10050
10051       if (loc->dw_loc_opc != DW_OP_piece)
10052         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10053     }
10054 }
10055
10056 /* Return a location descriptor that designates a machine register or
10057    zero if there is none.  */
10058
10059 static dw_loc_descr_ref
10060 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10061 {
10062   rtx regs;
10063
10064   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10065     return 0;
10066
10067   regs = targetm.dwarf_register_span (rtl);
10068
10069   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10070     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10071   else
10072     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10073 }
10074
10075 /* Return a location descriptor that designates a machine register for
10076    a given hard register number.  */
10077
10078 static dw_loc_descr_ref
10079 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10080 {
10081   dw_loc_descr_ref reg_loc_descr;
10082
10083   if (regno <= 31)
10084     reg_loc_descr
10085       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10086   else
10087     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10088
10089   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10090     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10091
10092   return reg_loc_descr;
10093 }
10094
10095 /* Given an RTL of a register, return a location descriptor that
10096    designates a value that spans more than one register.  */
10097
10098 static dw_loc_descr_ref
10099 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10100                              enum var_init_status initialized)
10101 {
10102   int nregs, size, i;
10103   unsigned reg;
10104   dw_loc_descr_ref loc_result = NULL;
10105
10106   reg = REGNO (rtl);
10107 #ifdef LEAF_REG_REMAP
10108   if (current_function_uses_only_leaf_regs)
10109     {
10110       int leaf_reg = LEAF_REG_REMAP (reg);
10111       if (leaf_reg != -1)
10112         reg = (unsigned) leaf_reg;
10113     }
10114 #endif
10115   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10116   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10117
10118   /* Simple, contiguous registers.  */
10119   if (regs == NULL_RTX)
10120     {
10121       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10122
10123       loc_result = NULL;
10124       while (nregs--)
10125         {
10126           dw_loc_descr_ref t;
10127
10128           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10129                                       VAR_INIT_STATUS_INITIALIZED);
10130           add_loc_descr (&loc_result, t);
10131           add_loc_descr_op_piece (&loc_result, size);
10132           ++reg;
10133         }
10134       return loc_result;
10135     }
10136
10137   /* Now onto stupid register sets in non contiguous locations.  */
10138
10139   gcc_assert (GET_CODE (regs) == PARALLEL);
10140
10141   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10142   loc_result = NULL;
10143
10144   for (i = 0; i < XVECLEN (regs, 0); ++i)
10145     {
10146       dw_loc_descr_ref t;
10147
10148       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10149                                   VAR_INIT_STATUS_INITIALIZED);
10150       add_loc_descr (&loc_result, t);
10151       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10152       add_loc_descr_op_piece (&loc_result, size);
10153     }
10154
10155   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10156     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10157   return loc_result;
10158 }
10159
10160 #endif /* DWARF2_DEBUGGING_INFO */
10161
10162 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10163
10164 /* Return a location descriptor that designates a constant.  */
10165
10166 static dw_loc_descr_ref
10167 int_loc_descriptor (HOST_WIDE_INT i)
10168 {
10169   enum dwarf_location_atom op;
10170
10171   /* Pick the smallest representation of a constant, rather than just
10172      defaulting to the LEB encoding.  */
10173   if (i >= 0)
10174     {
10175       if (i <= 31)
10176         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10177       else if (i <= 0xff)
10178         op = DW_OP_const1u;
10179       else if (i <= 0xffff)
10180         op = DW_OP_const2u;
10181       else if (HOST_BITS_PER_WIDE_INT == 32
10182                || i <= 0xffffffff)
10183         op = DW_OP_const4u;
10184       else
10185         op = DW_OP_constu;
10186     }
10187   else
10188     {
10189       if (i >= -0x80)
10190         op = DW_OP_const1s;
10191       else if (i >= -0x8000)
10192         op = DW_OP_const2s;
10193       else if (HOST_BITS_PER_WIDE_INT == 32
10194                || i >= -0x80000000)
10195         op = DW_OP_const4s;
10196       else
10197         op = DW_OP_consts;
10198     }
10199
10200   return new_loc_descr (op, i, 0);
10201 }
10202 #endif
10203
10204 #ifdef DWARF2_DEBUGGING_INFO
10205
10206 /* Return a location descriptor that designates a base+offset location.  */
10207
10208 static dw_loc_descr_ref
10209 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10210                  enum var_init_status initialized)
10211 {
10212   unsigned int regno;
10213   dw_loc_descr_ref result;
10214   dw_fde_ref fde = current_fde ();
10215
10216   /* We only use "frame base" when we're sure we're talking about the
10217      post-prologue local stack frame.  We do this by *not* running
10218      register elimination until this point, and recognizing the special
10219      argument pointer and soft frame pointer rtx's.  */
10220   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10221     {
10222       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10223
10224       if (elim != reg)
10225         {
10226           if (GET_CODE (elim) == PLUS)
10227             {
10228               offset += INTVAL (XEXP (elim, 1));
10229               elim = XEXP (elim, 0);
10230             }
10231           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10232                        && (elim == hard_frame_pointer_rtx
10233                            || elim == stack_pointer_rtx))
10234                       || elim == (frame_pointer_needed
10235                                   ? hard_frame_pointer_rtx
10236                                   : stack_pointer_rtx));
10237
10238           /* If drap register is used to align stack, use frame
10239              pointer + offset to access stack variables.  If stack
10240              is aligned without drap, use stack pointer + offset to
10241              access stack variables.  */
10242           if (crtl->stack_realign_tried
10243               && cfa.reg == HARD_FRAME_POINTER_REGNUM
10244               && reg == frame_pointer_rtx)
10245             {
10246               int base_reg
10247                 = DWARF_FRAME_REGNUM (cfa.indirect
10248                                       ? HARD_FRAME_POINTER_REGNUM
10249                                       : STACK_POINTER_REGNUM);
10250               return new_reg_loc_descr (base_reg, offset);
10251             }
10252
10253           offset += frame_pointer_fb_offset;
10254           return new_loc_descr (DW_OP_fbreg, offset, 0);
10255         }
10256     }
10257   else if (fde
10258            && fde->drap_reg != INVALID_REGNUM
10259            && (fde->drap_reg == REGNO (reg)
10260                || fde->vdrap_reg == REGNO (reg)))
10261     {
10262       /* Use cfa+offset to represent the location of arguments passed
10263          on stack when drap is used to align stack.  */
10264       return new_loc_descr (DW_OP_fbreg, offset, 0);
10265     }
10266
10267   regno = dbx_reg_number (reg);
10268   if (regno <= 31)
10269     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10270                             offset, 0);
10271   else
10272     result = new_loc_descr (DW_OP_bregx, regno, offset);
10273
10274   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10275     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10276
10277   return result;
10278 }
10279
10280 /* Return true if this RTL expression describes a base+offset calculation.  */
10281
10282 static inline int
10283 is_based_loc (const_rtx rtl)
10284 {
10285   return (GET_CODE (rtl) == PLUS
10286           && ((REG_P (XEXP (rtl, 0))
10287                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10288                && CONST_INT_P (XEXP (rtl, 1)))));
10289 }
10290
10291 /* Return a descriptor that describes the concatenation of N locations
10292    used to form the address of a memory location.  */
10293
10294 static dw_loc_descr_ref
10295 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10296                             enum var_init_status initialized)
10297 {
10298   unsigned int i;
10299   dw_loc_descr_ref cc_loc_result = NULL;
10300   unsigned int n = XVECLEN (concatn, 0);
10301
10302   for (i = 0; i < n; ++i)
10303     {
10304       dw_loc_descr_ref ref;
10305       rtx x = XVECEXP (concatn, 0, i);
10306
10307       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10308       if (ref == NULL)
10309         return NULL;
10310
10311       add_loc_descr (&cc_loc_result, ref);
10312       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10313     }
10314
10315   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10316     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10317
10318   return cc_loc_result;
10319 }
10320
10321 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10322    failed.  */
10323
10324 static dw_loc_descr_ref
10325 tls_mem_loc_descriptor (rtx mem)
10326 {
10327   tree base;
10328   dw_loc_descr_ref loc_result;
10329
10330   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10331     return NULL;
10332
10333   base = get_base_address (MEM_EXPR (mem));
10334   if (base == NULL
10335       || TREE_CODE (base) != VAR_DECL
10336       || !DECL_THREAD_LOCAL_P (base))
10337     return NULL;
10338
10339   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10340   if (loc_result == NULL)
10341     return NULL;
10342
10343   if (INTVAL (MEM_OFFSET (mem)))
10344     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10345
10346   return loc_result;
10347 }
10348
10349 /* The following routine converts the RTL for a variable or parameter
10350    (resident in memory) into an equivalent Dwarf representation of a
10351    mechanism for getting the address of that same variable onto the top of a
10352    hypothetical "address evaluation" stack.
10353
10354    When creating memory location descriptors, we are effectively transforming
10355    the RTL for a memory-resident object into its Dwarf postfix expression
10356    equivalent.  This routine recursively descends an RTL tree, turning
10357    it into Dwarf postfix code as it goes.
10358
10359    MODE is the mode of the memory reference, needed to handle some
10360    autoincrement addressing modes.
10361
10362    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10363    location list for RTL.
10364
10365    Return 0 if we can't represent the location.  */
10366
10367 static dw_loc_descr_ref
10368 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10369                     enum var_init_status initialized)
10370 {
10371   dw_loc_descr_ref mem_loc_result = NULL;
10372   enum dwarf_location_atom op;
10373
10374   /* Note that for a dynamically sized array, the location we will generate a
10375      description of here will be the lowest numbered location which is
10376      actually within the array.  That's *not* necessarily the same as the
10377      zeroth element of the array.  */
10378
10379   rtl = targetm.delegitimize_address (rtl);
10380
10381   switch (GET_CODE (rtl))
10382     {
10383     case POST_INC:
10384     case POST_DEC:
10385     case POST_MODIFY:
10386       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10387          just fall into the SUBREG code.  */
10388
10389       /* ... fall through ...  */
10390
10391     case SUBREG:
10392       /* The case of a subreg may arise when we have a local (register)
10393          variable or a formal (register) parameter which doesn't quite fill
10394          up an entire register.  For now, just assume that it is
10395          legitimate to make the Dwarf info refer to the whole register which
10396          contains the given subreg.  */
10397       rtl = XEXP (rtl, 0);
10398
10399       /* ... fall through ...  */
10400
10401     case REG:
10402       /* Whenever a register number forms a part of the description of the
10403          method for calculating the (dynamic) address of a memory resident
10404          object, DWARF rules require the register number be referred to as
10405          a "base register".  This distinction is not based in any way upon
10406          what category of register the hardware believes the given register
10407          belongs to.  This is strictly DWARF terminology we're dealing with
10408          here. Note that in cases where the location of a memory-resident
10409          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10410          OP_CONST (0)) the actual DWARF location descriptor that we generate
10411          may just be OP_BASEREG (basereg).  This may look deceptively like
10412          the object in question was allocated to a register (rather than in
10413          memory) so DWARF consumers need to be aware of the subtle
10414          distinction between OP_REG and OP_BASEREG.  */
10415       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10416         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10417       else if (stack_realign_drap
10418                && crtl->drap_reg
10419                && crtl->args.internal_arg_pointer == rtl
10420                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10421         {
10422           /* If RTL is internal_arg_pointer, which has been optimized
10423              out, use DRAP instead.  */
10424           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10425                                             VAR_INIT_STATUS_INITIALIZED);
10426         }
10427       break;
10428
10429     case MEM:
10430       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10431                                            VAR_INIT_STATUS_INITIALIZED);
10432       if (mem_loc_result == NULL)
10433         mem_loc_result = tls_mem_loc_descriptor (rtl);
10434       if (mem_loc_result != 0)
10435         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10436       break;
10437
10438     case LO_SUM:
10439          rtl = XEXP (rtl, 1);
10440
10441       /* ... fall through ...  */
10442
10443     case LABEL_REF:
10444       /* Some ports can transform a symbol ref into a label ref, because
10445          the symbol ref is too far away and has to be dumped into a constant
10446          pool.  */
10447     case CONST:
10448     case SYMBOL_REF:
10449       /* Alternatively, the symbol in the constant pool might be referenced
10450          by a different symbol.  */
10451       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10452         {
10453           bool marked;
10454           rtx tmp = get_pool_constant_mark (rtl, &marked);
10455
10456           if (GET_CODE (tmp) == SYMBOL_REF)
10457             {
10458               rtl = tmp;
10459               if (CONSTANT_POOL_ADDRESS_P (tmp))
10460                 get_pool_constant_mark (tmp, &marked);
10461               else
10462                 marked = true;
10463             }
10464
10465           /* If all references to this pool constant were optimized away,
10466              it was not output and thus we can't represent it.
10467              FIXME: might try to use DW_OP_const_value here, though
10468              DW_OP_piece complicates it.  */
10469           if (!marked)
10470             return 0;
10471         }
10472
10473       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10474       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10475       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10476       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10477       break;
10478
10479     case PRE_MODIFY:
10480       /* Extract the PLUS expression nested inside and fall into
10481          PLUS code below.  */
10482       rtl = XEXP (rtl, 1);
10483       goto plus;
10484
10485     case PRE_INC:
10486     case PRE_DEC:
10487       /* Turn these into a PLUS expression and fall into the PLUS code
10488          below.  */
10489       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10490                           GEN_INT (GET_CODE (rtl) == PRE_INC
10491                                    ? GET_MODE_UNIT_SIZE (mode)
10492                                    : -GET_MODE_UNIT_SIZE (mode)));
10493
10494       /* ... fall through ...  */
10495
10496     case PLUS:
10497     plus:
10498       if (is_based_loc (rtl))
10499         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10500                                           INTVAL (XEXP (rtl, 1)),
10501                                           VAR_INIT_STATUS_INITIALIZED);
10502       else
10503         {
10504           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10505                                                VAR_INIT_STATUS_INITIALIZED);
10506           if (mem_loc_result == 0)
10507             break;
10508
10509           if (CONST_INT_P (XEXP (rtl, 1)))
10510             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10511           else
10512             {
10513               dw_loc_descr_ref mem_loc_result2
10514                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10515                                       VAR_INIT_STATUS_INITIALIZED);
10516               if (mem_loc_result2 == 0)
10517                 break;
10518               add_loc_descr (&mem_loc_result, mem_loc_result2);
10519               add_loc_descr (&mem_loc_result,
10520                              new_loc_descr (DW_OP_plus, 0, 0));
10521             }
10522         }
10523       break;
10524
10525     /* If a pseudo-reg is optimized away, it is possible for it to
10526        be replaced with a MEM containing a multiply or shift.  */
10527     case MULT:
10528       op = DW_OP_mul;
10529       goto do_binop;
10530
10531     case ASHIFT:
10532       op = DW_OP_shl;
10533       goto do_binop;
10534
10535     case ASHIFTRT:
10536       op = DW_OP_shra;
10537       goto do_binop;
10538
10539     case LSHIFTRT:
10540       op = DW_OP_shr;
10541       goto do_binop;
10542
10543     do_binop:
10544       {
10545         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10546                                                    VAR_INIT_STATUS_INITIALIZED);
10547         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10548                                                    VAR_INIT_STATUS_INITIALIZED);
10549
10550         if (op0 == 0 || op1 == 0)
10551           break;
10552
10553         mem_loc_result = op0;
10554         add_loc_descr (&mem_loc_result, op1);
10555         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10556         break;
10557       }
10558
10559     case CONST_INT:
10560       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10561       break;
10562
10563     case CONCATN:
10564       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10565                                                    VAR_INIT_STATUS_INITIALIZED);
10566       break;
10567
10568     case UNSPEC:
10569       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10570          can't express it in the debug info.  This can happen e.g. with some
10571          TLS UNSPECs.  */
10572       break;
10573
10574     default:
10575       gcc_unreachable ();
10576     }
10577
10578   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10579     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10580
10581   return mem_loc_result;
10582 }
10583
10584 /* Return a descriptor that describes the concatenation of two locations.
10585    This is typically a complex variable.  */
10586
10587 static dw_loc_descr_ref
10588 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10589 {
10590   dw_loc_descr_ref cc_loc_result = NULL;
10591   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10592   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10593
10594   if (x0_ref == 0 || x1_ref == 0)
10595     return 0;
10596
10597   cc_loc_result = x0_ref;
10598   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10599
10600   add_loc_descr (&cc_loc_result, x1_ref);
10601   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10602
10603   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10604     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10605
10606   return cc_loc_result;
10607 }
10608
10609 /* Return a descriptor that describes the concatenation of N
10610    locations.  */
10611
10612 static dw_loc_descr_ref
10613 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10614 {
10615   unsigned int i;
10616   dw_loc_descr_ref cc_loc_result = NULL;
10617   unsigned int n = XVECLEN (concatn, 0);
10618
10619   for (i = 0; i < n; ++i)
10620     {
10621       dw_loc_descr_ref ref;
10622       rtx x = XVECEXP (concatn, 0, i);
10623
10624       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10625       if (ref == NULL)
10626         return NULL;
10627
10628       add_loc_descr (&cc_loc_result, ref);
10629       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10630     }
10631
10632   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10633     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10634
10635   return cc_loc_result;
10636 }
10637
10638 /* Output a proper Dwarf location descriptor for a variable or parameter
10639    which is either allocated in a register or in a memory location.  For a
10640    register, we just generate an OP_REG and the register number.  For a
10641    memory location we provide a Dwarf postfix expression describing how to
10642    generate the (dynamic) address of the object onto the address stack.
10643
10644    If we don't know how to describe it, return 0.  */
10645
10646 static dw_loc_descr_ref
10647 loc_descriptor (rtx rtl, enum var_init_status initialized)
10648 {
10649   dw_loc_descr_ref loc_result = NULL;
10650
10651   switch (GET_CODE (rtl))
10652     {
10653     case SUBREG:
10654       /* The case of a subreg may arise when we have a local (register)
10655          variable or a formal (register) parameter which doesn't quite fill
10656          up an entire register.  For now, just assume that it is
10657          legitimate to make the Dwarf info refer to the whole register which
10658          contains the given subreg.  */
10659       rtl = SUBREG_REG (rtl);
10660
10661       /* ... fall through ...  */
10662
10663     case REG:
10664       loc_result = reg_loc_descriptor (rtl, initialized);
10665       break;
10666
10667     case MEM:
10668       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10669                                        initialized);
10670       if (loc_result == NULL)
10671         loc_result = tls_mem_loc_descriptor (rtl);
10672       break;
10673
10674     case CONCAT:
10675       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10676                                           initialized);
10677       break;
10678
10679     case CONCATN:
10680       loc_result = concatn_loc_descriptor (rtl, initialized);
10681       break;
10682
10683     case VAR_LOCATION:
10684       /* Single part.  */
10685       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10686         {
10687           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10688           break;
10689         }
10690
10691       rtl = XEXP (rtl, 1);
10692       /* FALLTHRU */
10693
10694     case PARALLEL:
10695       {
10696         rtvec par_elems = XVEC (rtl, 0);
10697         int num_elem = GET_NUM_ELEM (par_elems);
10698         enum machine_mode mode;
10699         int i;
10700
10701         /* Create the first one, so we have something to add to.  */
10702         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10703                                      initialized);
10704         if (loc_result == NULL)
10705           return NULL;
10706         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10707         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10708         for (i = 1; i < num_elem; i++)
10709           {
10710             dw_loc_descr_ref temp;
10711
10712             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10713                                    initialized);
10714             if (temp == NULL)
10715               return NULL;
10716             add_loc_descr (&loc_result, temp);
10717             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10718             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10719           }
10720       }
10721       break;
10722
10723     default:
10724       gcc_unreachable ();
10725     }
10726
10727   return loc_result;
10728 }
10729
10730 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10731    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10732    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10733    top-level invocation, and we require the address of LOC; is 0 if we require
10734    the value of LOC.  */
10735
10736 static dw_loc_descr_ref
10737 loc_descriptor_from_tree_1 (tree loc, int want_address)
10738 {
10739   dw_loc_descr_ref ret, ret1;
10740   int have_address = 0;
10741   enum dwarf_location_atom op;
10742
10743   /* ??? Most of the time we do not take proper care for sign/zero
10744      extending the values properly.  Hopefully this won't be a real
10745      problem...  */
10746
10747   switch (TREE_CODE (loc))
10748     {
10749     case ERROR_MARK:
10750       return 0;
10751
10752     case PLACEHOLDER_EXPR:
10753       /* This case involves extracting fields from an object to determine the
10754          position of other fields.  We don't try to encode this here.  The
10755          only user of this is Ada, which encodes the needed information using
10756          the names of types.  */
10757       return 0;
10758
10759     case CALL_EXPR:
10760       return 0;
10761
10762     case PREINCREMENT_EXPR:
10763     case PREDECREMENT_EXPR:
10764     case POSTINCREMENT_EXPR:
10765     case POSTDECREMENT_EXPR:
10766       /* There are no opcodes for these operations.  */
10767       return 0;
10768
10769     case ADDR_EXPR:
10770       /* If we already want an address, there's nothing we can do.  */
10771       if (want_address)
10772         return 0;
10773
10774       /* Otherwise, process the argument and look for the address.  */
10775       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10776
10777     case VAR_DECL:
10778       if (DECL_THREAD_LOCAL_P (loc))
10779         {
10780           rtx rtl;
10781           enum dwarf_location_atom first_op;
10782           enum dwarf_location_atom second_op;
10783           bool dtprel = false;
10784
10785           if (targetm.have_tls)
10786             {
10787               /* If this is not defined, we have no way to emit the
10788                  data.  */
10789               if (!targetm.asm_out.output_dwarf_dtprel)
10790                 return 0;
10791
10792                /* The way DW_OP_GNU_push_tls_address is specified, we
10793                   can only look up addresses of objects in the current
10794                   module.  */
10795               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10796                 return 0;
10797               first_op = DW_OP_addr;
10798               dtprel = true;
10799               second_op = DW_OP_GNU_push_tls_address;
10800             }
10801           else
10802             {
10803               if (!targetm.emutls.debug_form_tls_address)
10804                 return 0;
10805               loc = emutls_decl (loc);
10806               first_op = DW_OP_addr;
10807               second_op = DW_OP_form_tls_address;
10808             }
10809
10810           rtl = rtl_for_decl_location (loc);
10811           if (rtl == NULL_RTX)
10812             return 0;
10813
10814           if (!MEM_P (rtl))
10815             return 0;
10816           rtl = XEXP (rtl, 0);
10817           if (! CONSTANT_P (rtl))
10818             return 0;
10819
10820           ret = new_loc_descr (first_op, 0, 0);
10821           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10822           ret->dw_loc_oprnd1.v.val_addr = rtl;
10823           ret->dtprel = dtprel;
10824
10825           ret1 = new_loc_descr (second_op, 0, 0);
10826           add_loc_descr (&ret, ret1);
10827
10828           have_address = 1;
10829           break;
10830         }
10831       /* FALLTHRU */
10832
10833     case PARM_DECL:
10834       if (DECL_HAS_VALUE_EXPR_P (loc))
10835         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10836                                            want_address);
10837       /* FALLTHRU */
10838
10839     case RESULT_DECL:
10840     case FUNCTION_DECL:
10841       {
10842         rtx rtl = rtl_for_decl_location (loc);
10843
10844         if (rtl == NULL_RTX)
10845           return 0;
10846         else if (CONST_INT_P (rtl))
10847           {
10848             HOST_WIDE_INT val = INTVAL (rtl);
10849             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10850               val &= GET_MODE_MASK (DECL_MODE (loc));
10851             ret = int_loc_descriptor (val);
10852           }
10853         else if (GET_CODE (rtl) == CONST_STRING)
10854           return 0;
10855         else if (CONSTANT_P (rtl))
10856           {
10857             ret = new_loc_descr (DW_OP_addr, 0, 0);
10858             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10859             ret->dw_loc_oprnd1.v.val_addr = rtl;
10860           }
10861         else
10862           {
10863             enum machine_mode mode;
10864
10865             /* Certain constructs can only be represented at top-level.  */
10866             if (want_address == 2)
10867               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10868
10869             mode = GET_MODE (rtl);
10870             if (MEM_P (rtl))
10871               {
10872                 rtl = XEXP (rtl, 0);
10873                 have_address = 1;
10874               }
10875             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10876           }
10877       }
10878       break;
10879
10880     case INDIRECT_REF:
10881       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10882       have_address = 1;
10883       break;
10884
10885     case COMPOUND_EXPR:
10886       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10887
10888     CASE_CONVERT:
10889     case VIEW_CONVERT_EXPR:
10890     case SAVE_EXPR:
10891     case MODIFY_EXPR:
10892       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10893
10894     case COMPONENT_REF:
10895     case BIT_FIELD_REF:
10896     case ARRAY_REF:
10897     case ARRAY_RANGE_REF:
10898       {
10899         tree obj, offset;
10900         HOST_WIDE_INT bitsize, bitpos, bytepos;
10901         enum machine_mode mode;
10902         int volatilep;
10903         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10904
10905         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10906                                    &unsignedp, &volatilep, false);
10907
10908         if (obj == loc)
10909           return 0;
10910
10911         ret = loc_descriptor_from_tree_1 (obj, 1);
10912         if (ret == 0
10913             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10914           return 0;
10915
10916         if (offset != NULL_TREE)
10917           {
10918             /* Variable offset.  */
10919             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10920             if (ret1 == 0)
10921               return 0;
10922             add_loc_descr (&ret, ret1);
10923             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10924           }
10925
10926         bytepos = bitpos / BITS_PER_UNIT;
10927         loc_descr_plus_const (&ret, bytepos);
10928
10929         have_address = 1;
10930         break;
10931       }
10932
10933     case INTEGER_CST:
10934       if (host_integerp (loc, 0))
10935         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10936       else
10937         return 0;
10938       break;
10939
10940     case CONSTRUCTOR:
10941       {
10942         /* Get an RTL for this, if something has been emitted.  */
10943         rtx rtl = lookup_constant_def (loc);
10944         enum machine_mode mode;
10945
10946         if (!rtl || !MEM_P (rtl))
10947           return 0;
10948         mode = GET_MODE (rtl);
10949         rtl = XEXP (rtl, 0);
10950         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10951         have_address = 1;
10952         break;
10953       }
10954
10955     case TRUTH_AND_EXPR:
10956     case TRUTH_ANDIF_EXPR:
10957     case BIT_AND_EXPR:
10958       op = DW_OP_and;
10959       goto do_binop;
10960
10961     case TRUTH_XOR_EXPR:
10962     case BIT_XOR_EXPR:
10963       op = DW_OP_xor;
10964       goto do_binop;
10965
10966     case TRUTH_OR_EXPR:
10967     case TRUTH_ORIF_EXPR:
10968     case BIT_IOR_EXPR:
10969       op = DW_OP_or;
10970       goto do_binop;
10971
10972     case FLOOR_DIV_EXPR:
10973     case CEIL_DIV_EXPR:
10974     case ROUND_DIV_EXPR:
10975     case TRUNC_DIV_EXPR:
10976       op = DW_OP_div;
10977       goto do_binop;
10978
10979     case MINUS_EXPR:
10980       op = DW_OP_minus;
10981       goto do_binop;
10982
10983     case FLOOR_MOD_EXPR:
10984     case CEIL_MOD_EXPR:
10985     case ROUND_MOD_EXPR:
10986     case TRUNC_MOD_EXPR:
10987       op = DW_OP_mod;
10988       goto do_binop;
10989
10990     case MULT_EXPR:
10991       op = DW_OP_mul;
10992       goto do_binop;
10993
10994     case LSHIFT_EXPR:
10995       op = DW_OP_shl;
10996       goto do_binop;
10997
10998     case RSHIFT_EXPR:
10999       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
11000       goto do_binop;
11001
11002     case POINTER_PLUS_EXPR:
11003     case PLUS_EXPR:
11004       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
11005           && host_integerp (TREE_OPERAND (loc, 1), 0))
11006         {
11007           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11008           if (ret == 0)
11009             return 0;
11010
11011           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
11012           break;
11013         }
11014
11015       op = DW_OP_plus;
11016       goto do_binop;
11017
11018     case LE_EXPR:
11019       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11020         return 0;
11021
11022       op = DW_OP_le;
11023       goto do_binop;
11024
11025     case GE_EXPR:
11026       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11027         return 0;
11028
11029       op = DW_OP_ge;
11030       goto do_binop;
11031
11032     case LT_EXPR:
11033       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11034         return 0;
11035
11036       op = DW_OP_lt;
11037       goto do_binop;
11038
11039     case GT_EXPR:
11040       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
11041         return 0;
11042
11043       op = DW_OP_gt;
11044       goto do_binop;
11045
11046     case EQ_EXPR:
11047       op = DW_OP_eq;
11048       goto do_binop;
11049
11050     case NE_EXPR:
11051       op = DW_OP_ne;
11052       goto do_binop;
11053
11054     do_binop:
11055       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11056       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11057       if (ret == 0 || ret1 == 0)
11058         return 0;
11059
11060       add_loc_descr (&ret, ret1);
11061       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11062       break;
11063
11064     case TRUTH_NOT_EXPR:
11065     case BIT_NOT_EXPR:
11066       op = DW_OP_not;
11067       goto do_unop;
11068
11069     case ABS_EXPR:
11070       op = DW_OP_abs;
11071       goto do_unop;
11072
11073     case NEGATE_EXPR:
11074       op = DW_OP_neg;
11075       goto do_unop;
11076
11077     do_unop:
11078       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11079       if (ret == 0)
11080         return 0;
11081
11082       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11083       break;
11084
11085     case MIN_EXPR:
11086     case MAX_EXPR:
11087       {
11088         const enum tree_code code =
11089           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11090
11091         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11092                       build2 (code, integer_type_node,
11093                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11094                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11095       }
11096
11097       /* ... fall through ...  */
11098
11099     case COND_EXPR:
11100       {
11101         dw_loc_descr_ref lhs
11102           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11103         dw_loc_descr_ref rhs
11104           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11105         dw_loc_descr_ref bra_node, jump_node, tmp;
11106
11107         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11108         if (ret == 0 || lhs == 0 || rhs == 0)
11109           return 0;
11110
11111         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11112         add_loc_descr (&ret, bra_node);
11113
11114         add_loc_descr (&ret, rhs);
11115         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11116         add_loc_descr (&ret, jump_node);
11117
11118         add_loc_descr (&ret, lhs);
11119         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11120         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11121
11122         /* ??? Need a node to point the skip at.  Use a nop.  */
11123         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11124         add_loc_descr (&ret, tmp);
11125         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11126         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11127       }
11128       break;
11129
11130     case FIX_TRUNC_EXPR:
11131       return 0;
11132
11133     default:
11134       /* Leave front-end specific codes as simply unknown.  This comes
11135          up, for instance, with the C STMT_EXPR.  */
11136       if ((unsigned int) TREE_CODE (loc)
11137           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11138         return 0;
11139
11140 #ifdef ENABLE_CHECKING
11141       /* Otherwise this is a generic code; we should just lists all of
11142          these explicitly.  We forgot one.  */
11143       gcc_unreachable ();
11144 #else
11145       /* In a release build, we want to degrade gracefully: better to
11146          generate incomplete debugging information than to crash.  */
11147       return NULL;
11148 #endif
11149     }
11150
11151   /* Show if we can't fill the request for an address.  */
11152   if (want_address && !have_address)
11153     return 0;
11154
11155   /* If we've got an address and don't want one, dereference.  */
11156   if (!want_address && have_address && ret)
11157     {
11158       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11159
11160       if (size > DWARF2_ADDR_SIZE || size == -1)
11161         return 0;
11162       else if (size == DWARF2_ADDR_SIZE)
11163         op = DW_OP_deref;
11164       else
11165         op = DW_OP_deref_size;
11166
11167       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11168     }
11169
11170   return ret;
11171 }
11172
11173 static inline dw_loc_descr_ref
11174 loc_descriptor_from_tree (tree loc)
11175 {
11176   return loc_descriptor_from_tree_1 (loc, 2);
11177 }
11178
11179 /* Given a value, round it up to the lowest multiple of `boundary'
11180    which is not less than the value itself.  */
11181
11182 static inline HOST_WIDE_INT
11183 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11184 {
11185   return (((value + boundary - 1) / boundary) * boundary);
11186 }
11187
11188 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11189    pointer to the declared type for the relevant field variable, or return
11190    `integer_type_node' if the given node turns out to be an
11191    ERROR_MARK node.  */
11192
11193 static inline tree
11194 field_type (const_tree decl)
11195 {
11196   tree type;
11197
11198   if (TREE_CODE (decl) == ERROR_MARK)
11199     return integer_type_node;
11200
11201   type = DECL_BIT_FIELD_TYPE (decl);
11202   if (type == NULL_TREE)
11203     type = TREE_TYPE (decl);
11204
11205   return type;
11206 }
11207
11208 /* Given a pointer to a tree node, return the alignment in bits for
11209    it, or else return BITS_PER_WORD if the node actually turns out to
11210    be an ERROR_MARK node.  */
11211
11212 static inline unsigned
11213 simple_type_align_in_bits (const_tree type)
11214 {
11215   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11216 }
11217
11218 static inline unsigned
11219 simple_decl_align_in_bits (const_tree decl)
11220 {
11221   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11222 }
11223
11224 /* Return the result of rounding T up to ALIGN.  */
11225
11226 static inline HOST_WIDE_INT
11227 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11228 {
11229   /* We must be careful if T is negative because HOST_WIDE_INT can be
11230      either "above" or "below" unsigned int as per the C promotion
11231      rules, depending on the host, thus making the signedness of the
11232      direct multiplication and division unpredictable.  */
11233   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11234
11235   u += align - 1;
11236   u /= align;
11237   u *= align;
11238
11239   return (HOST_WIDE_INT) u;
11240 }
11241
11242 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11243    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11244    or return 0 if we are unable to determine what that offset is, either
11245    because the argument turns out to be a pointer to an ERROR_MARK node, or
11246    because the offset is actually variable.  (We can't handle the latter case
11247    just yet).  */
11248
11249 static HOST_WIDE_INT
11250 field_byte_offset (const_tree decl)
11251 {
11252   HOST_WIDE_INT object_offset_in_bits;
11253   HOST_WIDE_INT bitpos_int;
11254
11255   if (TREE_CODE (decl) == ERROR_MARK)
11256     return 0;
11257
11258   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11259
11260   /* We cannot yet cope with fields whose positions are variable, so
11261      for now, when we see such things, we simply return 0.  Someday, we may
11262      be able to handle such cases, but it will be damn difficult.  */
11263   if (! host_integerp (bit_position (decl), 0))
11264     return 0;
11265
11266   bitpos_int = int_bit_position (decl);
11267
11268 #ifdef PCC_BITFIELD_TYPE_MATTERS
11269   if (PCC_BITFIELD_TYPE_MATTERS)
11270     {
11271       tree type;
11272       tree field_size_tree;
11273       HOST_WIDE_INT deepest_bitpos;
11274       unsigned HOST_WIDE_INT field_size_in_bits;
11275       unsigned int type_align_in_bits;
11276       unsigned int decl_align_in_bits;
11277       unsigned HOST_WIDE_INT type_size_in_bits;
11278
11279       type = field_type (decl);
11280       type_size_in_bits = simple_type_size_in_bits (type);
11281       type_align_in_bits = simple_type_align_in_bits (type);
11282
11283       field_size_tree = DECL_SIZE (decl);
11284
11285       /* The size could be unspecified if there was an error, or for
11286          a flexible array member.  */
11287       if (!field_size_tree)
11288         field_size_tree = bitsize_zero_node;
11289
11290       /* If the size of the field is not constant, use the type size.  */
11291       if (host_integerp (field_size_tree, 1))
11292         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11293       else
11294         field_size_in_bits = type_size_in_bits;
11295
11296       decl_align_in_bits = simple_decl_align_in_bits (decl);
11297
11298       /* The GCC front-end doesn't make any attempt to keep track of the
11299          starting bit offset (relative to the start of the containing
11300          structure type) of the hypothetical "containing object" for a
11301          bit-field.  Thus, when computing the byte offset value for the
11302          start of the "containing object" of a bit-field, we must deduce
11303          this information on our own. This can be rather tricky to do in
11304          some cases.  For example, handling the following structure type
11305          definition when compiling for an i386/i486 target (which only
11306          aligns long long's to 32-bit boundaries) can be very tricky:
11307
11308          struct S { int field1; long long field2:31; };
11309
11310          Fortunately, there is a simple rule-of-thumb which can be used
11311          in such cases.  When compiling for an i386/i486, GCC will
11312          allocate 8 bytes for the structure shown above.  It decides to
11313          do this based upon one simple rule for bit-field allocation.
11314          GCC allocates each "containing object" for each bit-field at
11315          the first (i.e. lowest addressed) legitimate alignment boundary
11316          (based upon the required minimum alignment for the declared
11317          type of the field) which it can possibly use, subject to the
11318          condition that there is still enough available space remaining
11319          in the containing object (when allocated at the selected point)
11320          to fully accommodate all of the bits of the bit-field itself.
11321
11322          This simple rule makes it obvious why GCC allocates 8 bytes for
11323          each object of the structure type shown above.  When looking
11324          for a place to allocate the "containing object" for `field2',
11325          the compiler simply tries to allocate a 64-bit "containing
11326          object" at each successive 32-bit boundary (starting at zero)
11327          until it finds a place to allocate that 64- bit field such that
11328          at least 31 contiguous (and previously unallocated) bits remain
11329          within that selected 64 bit field.  (As it turns out, for the
11330          example above, the compiler finds it is OK to allocate the
11331          "containing object" 64-bit field at bit-offset zero within the
11332          structure type.)
11333
11334          Here we attempt to work backwards from the limited set of facts
11335          we're given, and we try to deduce from those facts, where GCC
11336          must have believed that the containing object started (within
11337          the structure type). The value we deduce is then used (by the
11338          callers of this routine) to generate DW_AT_location and
11339          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11340          the case of DW_AT_location, regular fields as well).  */
11341
11342       /* Figure out the bit-distance from the start of the structure to
11343          the "deepest" bit of the bit-field.  */
11344       deepest_bitpos = bitpos_int + field_size_in_bits;
11345
11346       /* This is the tricky part.  Use some fancy footwork to deduce
11347          where the lowest addressed bit of the containing object must
11348          be.  */
11349       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11350
11351       /* Round up to type_align by default.  This works best for
11352          bitfields.  */
11353       object_offset_in_bits
11354         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11355
11356       if (object_offset_in_bits > bitpos_int)
11357         {
11358           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11359
11360           /* Round up to decl_align instead.  */
11361           object_offset_in_bits
11362             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11363         }
11364     }
11365   else
11366 #endif
11367     object_offset_in_bits = bitpos_int;
11368
11369   return object_offset_in_bits / BITS_PER_UNIT;
11370 }
11371 \f
11372 /* The following routines define various Dwarf attributes and any data
11373    associated with them.  */
11374
11375 /* Add a location description attribute value to a DIE.
11376
11377    This emits location attributes suitable for whole variables and
11378    whole parameters.  Note that the location attributes for struct fields are
11379    generated by the routine `data_member_location_attribute' below.  */
11380
11381 static inline void
11382 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11383                              dw_loc_descr_ref descr)
11384 {
11385   if (descr != 0)
11386     add_AT_loc (die, attr_kind, descr);
11387 }
11388
11389 /* Attach the specialized form of location attribute used for data members of
11390    struct and union types.  In the special case of a FIELD_DECL node which
11391    represents a bit-field, the "offset" part of this special location
11392    descriptor must indicate the distance in bytes from the lowest-addressed
11393    byte of the containing struct or union type to the lowest-addressed byte of
11394    the "containing object" for the bit-field.  (See the `field_byte_offset'
11395    function above).
11396
11397    For any given bit-field, the "containing object" is a hypothetical object
11398    (of some integral or enum type) within which the given bit-field lives.  The
11399    type of this hypothetical "containing object" is always the same as the
11400    declared type of the individual bit-field itself (for GCC anyway... the
11401    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11402    bytes) of the hypothetical "containing object" which will be given in the
11403    DW_AT_byte_size attribute for this bit-field.  (See the
11404    `byte_size_attribute' function below.)  It is also used when calculating the
11405    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11406    function below.)  */
11407
11408 static void
11409 add_data_member_location_attribute (dw_die_ref die, tree decl)
11410 {
11411   HOST_WIDE_INT offset;
11412   dw_loc_descr_ref loc_descr = 0;
11413
11414   if (TREE_CODE (decl) == TREE_BINFO)
11415     {
11416       /* We're working on the TAG_inheritance for a base class.  */
11417       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11418         {
11419           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11420              aren't at a fixed offset from all (sub)objects of the same
11421              type.  We need to extract the appropriate offset from our
11422              vtable.  The following dwarf expression means
11423
11424                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11425
11426              This is specific to the V3 ABI, of course.  */
11427
11428           dw_loc_descr_ref tmp;
11429
11430           /* Make a copy of the object address.  */
11431           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11432           add_loc_descr (&loc_descr, tmp);
11433
11434           /* Extract the vtable address.  */
11435           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11436           add_loc_descr (&loc_descr, tmp);
11437
11438           /* Calculate the address of the offset.  */
11439           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11440           gcc_assert (offset < 0);
11441
11442           tmp = int_loc_descriptor (-offset);
11443           add_loc_descr (&loc_descr, tmp);
11444           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11445           add_loc_descr (&loc_descr, tmp);
11446
11447           /* Extract the offset.  */
11448           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11449           add_loc_descr (&loc_descr, tmp);
11450
11451           /* Add it to the object address.  */
11452           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11453           add_loc_descr (&loc_descr, tmp);
11454         }
11455       else
11456         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11457     }
11458   else
11459     offset = field_byte_offset (decl);
11460
11461   if (! loc_descr)
11462     {
11463       enum dwarf_location_atom op;
11464
11465       /* The DWARF2 standard says that we should assume that the structure
11466          address is already on the stack, so we can specify a structure field
11467          address by using DW_OP_plus_uconst.  */
11468
11469 #ifdef MIPS_DEBUGGING_INFO
11470       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11471          operator correctly.  It works only if we leave the offset on the
11472          stack.  */
11473       op = DW_OP_constu;
11474 #else
11475       op = DW_OP_plus_uconst;
11476 #endif
11477
11478       loc_descr = new_loc_descr (op, offset, 0);
11479     }
11480
11481   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11482 }
11483
11484 /* Writes integer values to dw_vec_const array.  */
11485
11486 static void
11487 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11488 {
11489   while (size != 0)
11490     {
11491       *dest++ = val & 0xff;
11492       val >>= 8;
11493       --size;
11494     }
11495 }
11496
11497 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11498
11499 static HOST_WIDE_INT
11500 extract_int (const unsigned char *src, unsigned int size)
11501 {
11502   HOST_WIDE_INT val = 0;
11503
11504   src += size;
11505   while (size != 0)
11506     {
11507       val <<= 8;
11508       val |= *--src & 0xff;
11509       --size;
11510     }
11511   return val;
11512 }
11513
11514 /* Writes floating point values to dw_vec_const array.  */
11515
11516 static void
11517 insert_float (const_rtx rtl, unsigned char *array)
11518 {
11519   REAL_VALUE_TYPE rv;
11520   long val[4];
11521   int i;
11522
11523   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11524   real_to_target (val, &rv, GET_MODE (rtl));
11525
11526   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11527   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11528     {
11529       insert_int (val[i], 4, array);
11530       array += 4;
11531     }
11532 }
11533
11534 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11535    does not have a "location" either in memory or in a register.  These
11536    things can arise in GNU C when a constant is passed as an actual parameter
11537    to an inlined function.  They can also arise in C++ where declared
11538    constants do not necessarily get memory "homes".  */
11539
11540 static void
11541 add_const_value_attribute (dw_die_ref die, rtx rtl)
11542 {
11543   switch (GET_CODE (rtl))
11544     {
11545     case CONST_INT:
11546       {
11547         HOST_WIDE_INT val = INTVAL (rtl);
11548
11549         if (val < 0)
11550           add_AT_int (die, DW_AT_const_value, val);
11551         else
11552           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11553       }
11554       break;
11555
11556     case CONST_DOUBLE:
11557       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11558          floating-point constant.  A CONST_DOUBLE is used whenever the
11559          constant requires more than one word in order to be adequately
11560          represented.  We output CONST_DOUBLEs as blocks.  */
11561       {
11562         enum machine_mode mode = GET_MODE (rtl);
11563
11564         if (SCALAR_FLOAT_MODE_P (mode))
11565           {
11566             unsigned int length = GET_MODE_SIZE (mode);
11567             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11568
11569             insert_float (rtl, array);
11570             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11571           }
11572         else
11573           {
11574             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11575             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11576
11577             add_AT_long_long (die, DW_AT_const_value,
11578                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11579           }
11580       }
11581       break;
11582
11583     case CONST_VECTOR:
11584       {
11585         enum machine_mode mode = GET_MODE (rtl);
11586         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11587         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11588         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11589         unsigned int i;
11590         unsigned char *p;
11591
11592         switch (GET_MODE_CLASS (mode))
11593           {
11594           case MODE_VECTOR_INT:
11595             for (i = 0, p = array; i < length; i++, p += elt_size)
11596               {
11597                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11598                 HOST_WIDE_INT lo, hi;
11599
11600                 switch (GET_CODE (elt))
11601                   {
11602                   case CONST_INT:
11603                     lo = INTVAL (elt);
11604                     hi = -(lo < 0);
11605                     break;
11606
11607                   case CONST_DOUBLE:
11608                     lo = CONST_DOUBLE_LOW (elt);
11609                     hi = CONST_DOUBLE_HIGH (elt);
11610                     break;
11611
11612                   default:
11613                     gcc_unreachable ();
11614                   }
11615
11616                 if (elt_size <= sizeof (HOST_WIDE_INT))
11617                   insert_int (lo, elt_size, p);
11618                 else
11619                   {
11620                     unsigned char *p0 = p;
11621                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11622
11623                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11624                     if (WORDS_BIG_ENDIAN)
11625                       {
11626                         p0 = p1;
11627                         p1 = p;
11628                       }
11629                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11630                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11631                   }
11632               }
11633             break;
11634
11635           case MODE_VECTOR_FLOAT:
11636             for (i = 0, p = array; i < length; i++, p += elt_size)
11637               {
11638                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11639                 insert_float (elt, p);
11640               }
11641             break;
11642
11643           default:
11644             gcc_unreachable ();
11645           }
11646
11647         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11648       }
11649       break;
11650
11651     case CONST_STRING:
11652       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11653       break;
11654
11655     case SYMBOL_REF:
11656     case LABEL_REF:
11657     case CONST:
11658       add_AT_addr (die, DW_AT_const_value, rtl);
11659       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11660       break;
11661
11662     case PLUS:
11663       /* In cases where an inlined instance of an inline function is passed
11664          the address of an `auto' variable (which is local to the caller) we
11665          can get a situation where the DECL_RTL of the artificial local
11666          variable (for the inlining) which acts as a stand-in for the
11667          corresponding formal parameter (of the inline function) will look
11668          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11669          exactly a compile-time constant expression, but it isn't the address
11670          of the (artificial) local variable either.  Rather, it represents the
11671          *value* which the artificial local variable always has during its
11672          lifetime.  We currently have no way to represent such quasi-constant
11673          values in Dwarf, so for now we just punt and generate nothing.  */
11674       break;
11675
11676     default:
11677       /* No other kinds of rtx should be possible here.  */
11678       gcc_unreachable ();
11679     }
11680
11681 }
11682
11683 /* Determine whether the evaluation of EXPR references any variables
11684    or functions which aren't otherwise used (and therefore may not be
11685    output).  */
11686 static tree
11687 reference_to_unused (tree * tp, int * walk_subtrees,
11688                      void * data ATTRIBUTE_UNUSED)
11689 {
11690   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11691     *walk_subtrees = 0;
11692
11693   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11694       && ! TREE_ASM_WRITTEN (*tp))
11695     return *tp;
11696   /* ???  The C++ FE emits debug information for using decls, so
11697      putting gcc_unreachable here falls over.  See PR31899.  For now
11698      be conservative.  */
11699   else if (!cgraph_global_info_ready
11700            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11701     return *tp;
11702   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11703     {
11704       struct varpool_node *node = varpool_node (*tp);
11705       if (!node->needed)
11706         return *tp;
11707     }
11708   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11709            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11710     {
11711       struct cgraph_node *node = cgraph_node (*tp);
11712       if (node->process || TREE_ASM_WRITTEN (*tp))
11713         return *tp;
11714     }
11715   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11716     return *tp;
11717
11718   return NULL_TREE;
11719 }
11720
11721 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11722    for use in a later add_const_value_attribute call.  */
11723
11724 static rtx
11725 rtl_for_decl_init (tree init, tree type)
11726 {
11727   rtx rtl = NULL_RTX;
11728
11729   /* If a variable is initialized with a string constant without embedded
11730      zeros, build CONST_STRING.  */
11731   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11732     {
11733       tree enttype = TREE_TYPE (type);
11734       tree domain = TYPE_DOMAIN (type);
11735       enum machine_mode mode = TYPE_MODE (enttype);
11736
11737       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11738           && domain
11739           && integer_zerop (TYPE_MIN_VALUE (domain))
11740           && compare_tree_int (TYPE_MAX_VALUE (domain),
11741                                TREE_STRING_LENGTH (init) - 1) == 0
11742           && ((size_t) TREE_STRING_LENGTH (init)
11743               == strlen (TREE_STRING_POINTER (init)) + 1))
11744         rtl = gen_rtx_CONST_STRING (VOIDmode,
11745                                     ggc_strdup (TREE_STRING_POINTER (init)));
11746     }
11747   /* Other aggregates, and complex values, could be represented using
11748      CONCAT: FIXME!  */
11749   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11750     ;
11751   /* Vectors only work if their mode is supported by the target.
11752      FIXME: generic vectors ought to work too.  */
11753   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11754     ;
11755   /* If the initializer is something that we know will expand into an
11756      immediate RTL constant, expand it now.  We must be careful not to
11757      reference variables which won't be output.  */
11758   else if (initializer_constant_valid_p (init, type)
11759            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11760     {
11761       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11762          possible.  */
11763       if (TREE_CODE (type) == VECTOR_TYPE)
11764         switch (TREE_CODE (init))
11765           {
11766           case VECTOR_CST:
11767             break;
11768           case CONSTRUCTOR:
11769             if (TREE_CONSTANT (init))
11770               {
11771                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11772                 bool constant_p = true;
11773                 tree value;
11774                 unsigned HOST_WIDE_INT ix;
11775
11776                 /* Even when ctor is constant, it might contain non-*_CST
11777                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11778                    belong into VECTOR_CST nodes.  */
11779                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11780                   if (!CONSTANT_CLASS_P (value))
11781                     {
11782                       constant_p = false;
11783                       break;
11784                     }
11785
11786                 if (constant_p)
11787                   {
11788                     init = build_vector_from_ctor (type, elts);
11789                     break;
11790                   }
11791               }
11792             /* FALLTHRU */
11793
11794           default:
11795             return NULL;
11796           }
11797
11798       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11799
11800       /* If expand_expr returns a MEM, it wasn't immediate.  */
11801       gcc_assert (!rtl || !MEM_P (rtl));
11802     }
11803
11804   return rtl;
11805 }
11806
11807 /* Generate RTL for the variable DECL to represent its location.  */
11808
11809 static rtx
11810 rtl_for_decl_location (tree decl)
11811 {
11812   rtx rtl;
11813
11814   /* Here we have to decide where we are going to say the parameter "lives"
11815      (as far as the debugger is concerned).  We only have a couple of
11816      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11817
11818      DECL_RTL normally indicates where the parameter lives during most of the
11819      activation of the function.  If optimization is enabled however, this
11820      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11821      that the parameter doesn't really live anywhere (as far as the code
11822      generation parts of GCC are concerned) during most of the function's
11823      activation.  That will happen (for example) if the parameter is never
11824      referenced within the function.
11825
11826      We could just generate a location descriptor here for all non-NULL
11827      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11828      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11829      where DECL_RTL is NULL or is a pseudo-reg.
11830
11831      Note however that we can only get away with using DECL_INCOMING_RTL as
11832      a backup substitute for DECL_RTL in certain limited cases.  In cases
11833      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11834      we can be sure that the parameter was passed using the same type as it is
11835      declared to have within the function, and that its DECL_INCOMING_RTL
11836      points us to a place where a value of that type is passed.
11837
11838      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11839      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11840      because in these cases DECL_INCOMING_RTL points us to a value of some
11841      type which is *different* from the type of the parameter itself.  Thus,
11842      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11843      such cases, the debugger would end up (for example) trying to fetch a
11844      `float' from a place which actually contains the first part of a
11845      `double'.  That would lead to really incorrect and confusing
11846      output at debug-time.
11847
11848      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11849      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11850      are a couple of exceptions however.  On little-endian machines we can
11851      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11852      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11853      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11854      when (on a little-endian machine) a non-prototyped function has a
11855      parameter declared to be of type `short' or `char'.  In such cases,
11856      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11857      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11858      passed `int' value.  If the debugger then uses that address to fetch
11859      a `short' or a `char' (on a little-endian machine) the result will be
11860      the correct data, so we allow for such exceptional cases below.
11861
11862      Note that our goal here is to describe the place where the given formal
11863      parameter lives during most of the function's activation (i.e. between the
11864      end of the prologue and the start of the epilogue).  We'll do that as best
11865      as we can. Note however that if the given formal parameter is modified
11866      sometime during the execution of the function, then a stack backtrace (at
11867      debug-time) will show the function as having been called with the *new*
11868      value rather than the value which was originally passed in.  This happens
11869      rarely enough that it is not a major problem, but it *is* a problem, and
11870      I'd like to fix it.
11871
11872      A future version of dwarf2out.c may generate two additional attributes for
11873      any given DW_TAG_formal_parameter DIE which will describe the "passed
11874      type" and the "passed location" for the given formal parameter in addition
11875      to the attributes we now generate to indicate the "declared type" and the
11876      "active location" for each parameter.  This additional set of attributes
11877      could be used by debuggers for stack backtraces. Separately, note that
11878      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11879      This happens (for example) for inlined-instances of inline function formal
11880      parameters which are never referenced.  This really shouldn't be
11881      happening.  All PARM_DECL nodes should get valid non-NULL
11882      DECL_INCOMING_RTL values.  FIXME.  */
11883
11884   /* Use DECL_RTL as the "location" unless we find something better.  */
11885   rtl = DECL_RTL_IF_SET (decl);
11886
11887   /* When generating abstract instances, ignore everything except
11888      constants, symbols living in memory, and symbols living in
11889      fixed registers.  */
11890   if (! reload_completed)
11891     {
11892       if (rtl
11893           && (CONSTANT_P (rtl)
11894               || (MEM_P (rtl)
11895                   && CONSTANT_P (XEXP (rtl, 0)))
11896               || (REG_P (rtl)
11897                   && TREE_CODE (decl) == VAR_DECL
11898                   && TREE_STATIC (decl))))
11899         {
11900           rtl = targetm.delegitimize_address (rtl);
11901           return rtl;
11902         }
11903       rtl = NULL_RTX;
11904     }
11905   else if (TREE_CODE (decl) == PARM_DECL)
11906     {
11907       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11908         {
11909           tree declared_type = TREE_TYPE (decl);
11910           tree passed_type = DECL_ARG_TYPE (decl);
11911           enum machine_mode dmode = TYPE_MODE (declared_type);
11912           enum machine_mode pmode = TYPE_MODE (passed_type);
11913
11914           /* This decl represents a formal parameter which was optimized out.
11915              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11916              all cases where (rtl == NULL_RTX) just below.  */
11917           if (dmode == pmode)
11918             rtl = DECL_INCOMING_RTL (decl);
11919           else if (SCALAR_INT_MODE_P (dmode)
11920                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11921                    && DECL_INCOMING_RTL (decl))
11922             {
11923               rtx inc = DECL_INCOMING_RTL (decl);
11924               if (REG_P (inc))
11925                 rtl = inc;
11926               else if (MEM_P (inc))
11927                 {
11928                   if (BYTES_BIG_ENDIAN)
11929                     rtl = adjust_address_nv (inc, dmode,
11930                                              GET_MODE_SIZE (pmode)
11931                                              - GET_MODE_SIZE (dmode));
11932                   else
11933                     rtl = inc;
11934                 }
11935             }
11936         }
11937
11938       /* If the parm was passed in registers, but lives on the stack, then
11939          make a big endian correction if the mode of the type of the
11940          parameter is not the same as the mode of the rtl.  */
11941       /* ??? This is the same series of checks that are made in dbxout.c before
11942          we reach the big endian correction code there.  It isn't clear if all
11943          of these checks are necessary here, but keeping them all is the safe
11944          thing to do.  */
11945       else if (MEM_P (rtl)
11946                && XEXP (rtl, 0) != const0_rtx
11947                && ! CONSTANT_P (XEXP (rtl, 0))
11948                /* Not passed in memory.  */
11949                && !MEM_P (DECL_INCOMING_RTL (decl))
11950                /* Not passed by invisible reference.  */
11951                && (!REG_P (XEXP (rtl, 0))
11952                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11953                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11954 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11955                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11956 #endif
11957                      )
11958                /* Big endian correction check.  */
11959                && BYTES_BIG_ENDIAN
11960                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11961                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11962                    < UNITS_PER_WORD))
11963         {
11964           int offset = (UNITS_PER_WORD
11965                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11966
11967           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11968                              plus_constant (XEXP (rtl, 0), offset));
11969         }
11970     }
11971   else if (TREE_CODE (decl) == VAR_DECL
11972            && rtl
11973            && MEM_P (rtl)
11974            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11975            && BYTES_BIG_ENDIAN)
11976     {
11977       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11978       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11979
11980       /* If a variable is declared "register" yet is smaller than
11981          a register, then if we store the variable to memory, it
11982          looks like we're storing a register-sized value, when in
11983          fact we are not.  We need to adjust the offset of the
11984          storage location to reflect the actual value's bytes,
11985          else gdb will not be able to display it.  */
11986       if (rsize > dsize)
11987         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11988                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11989     }
11990
11991   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11992      and will have been substituted directly into all expressions that use it.
11993      C does not have such a concept, but C++ and other languages do.  */
11994   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11995     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11996
11997   if (rtl)
11998     rtl = targetm.delegitimize_address (rtl);
11999
12000   /* If we don't look past the constant pool, we risk emitting a
12001      reference to a constant pool entry that isn't referenced from
12002      code, and thus is not emitted.  */
12003   if (rtl)
12004     rtl = avoid_constant_pool_reference (rtl);
12005
12006   return rtl;
12007 }
12008
12009 /* We need to figure out what section we should use as the base for the
12010    address ranges where a given location is valid.
12011    1. If this particular DECL has a section associated with it, use that.
12012    2. If this function has a section associated with it, use that.
12013    3. Otherwise, use the text section.
12014    XXX: If you split a variable across multiple sections, we won't notice.  */
12015
12016 static const char *
12017 secname_for_decl (const_tree decl)
12018 {
12019   const char *secname;
12020
12021   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12022     {
12023       tree sectree = DECL_SECTION_NAME (decl);
12024       secname = TREE_STRING_POINTER (sectree);
12025     }
12026   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12027     {
12028       tree sectree = DECL_SECTION_NAME (current_function_decl);
12029       secname = TREE_STRING_POINTER (sectree);
12030     }
12031   else if (cfun && in_cold_section_p)
12032     secname = crtl->subsections.cold_section_label;
12033   else
12034     secname = text_section_label;
12035
12036   return secname;
12037 }
12038
12039 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
12040    returned.  If so, the decl for the COMMON block is returned, and the
12041    value is the offset into the common block for the symbol.  */
12042
12043 static tree
12044 fortran_common (tree decl, HOST_WIDE_INT *value)
12045 {
12046   tree val_expr, cvar;
12047   enum machine_mode mode;
12048   HOST_WIDE_INT bitsize, bitpos;
12049   tree offset;
12050   int volatilep = 0, unsignedp = 0;
12051
12052   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
12053      it does not have a value (the offset into the common area), or if it
12054      is thread local (as opposed to global) then it isn't common, and shouldn't
12055      be handled as such.  */
12056   if (TREE_CODE (decl) != VAR_DECL
12057       || !TREE_PUBLIC (decl)
12058       || !TREE_STATIC (decl)
12059       || !DECL_HAS_VALUE_EXPR_P (decl)
12060       || !is_fortran ())
12061     return NULL_TREE;
12062
12063   val_expr = DECL_VALUE_EXPR (decl);
12064   if (TREE_CODE (val_expr) != COMPONENT_REF)
12065     return NULL_TREE;
12066
12067   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12068                               &mode, &unsignedp, &volatilep, true);
12069
12070   if (cvar == NULL_TREE
12071       || TREE_CODE (cvar) != VAR_DECL
12072       || DECL_ARTIFICIAL (cvar)
12073       || !TREE_PUBLIC (cvar))
12074     return NULL_TREE;
12075
12076   *value = 0;
12077   if (offset != NULL)
12078     {
12079       if (!host_integerp (offset, 0))
12080         return NULL_TREE;
12081       *value = tree_low_cst (offset, 0);
12082     }
12083   if (bitpos != 0)
12084     *value += bitpos / BITS_PER_UNIT;
12085
12086   return cvar;
12087 }
12088
12089 /* Dereference a location expression LOC if DECL is passed by invisible
12090    reference.  */
12091
12092 static dw_loc_descr_ref
12093 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12094 {
12095   HOST_WIDE_INT size;
12096   enum dwarf_location_atom op;
12097
12098   if (loc == NULL)
12099     return NULL;
12100
12101   if ((TREE_CODE (decl) != PARM_DECL
12102        && TREE_CODE (decl) != RESULT_DECL
12103        && TREE_CODE (decl) != VAR_DECL)
12104       || !DECL_BY_REFERENCE (decl))
12105     return loc;
12106
12107   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12108      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12109      location expression is considered to be address of a memory location,
12110      rather than the register itself.  */
12111   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12112        || loc->dw_loc_opc == DW_OP_regx)
12113       && (loc->dw_loc_next == NULL
12114           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12115               && loc->dw_loc_next->dw_loc_next == NULL)))
12116     {
12117       if (loc->dw_loc_opc == DW_OP_regx)
12118         {
12119           loc->dw_loc_opc = DW_OP_bregx;
12120           loc->dw_loc_oprnd2.v.val_int = 0;
12121         }
12122       else
12123         {
12124           loc->dw_loc_opc
12125             = (enum dwarf_location_atom)
12126               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12127           loc->dw_loc_oprnd1.v.val_int = 0;
12128         }
12129       return loc;
12130     }
12131
12132   size = int_size_in_bytes (TREE_TYPE (decl));
12133   if (size > DWARF2_ADDR_SIZE || size == -1)
12134     return 0;
12135   else if (size == DWARF2_ADDR_SIZE)
12136     op = DW_OP_deref;
12137   else
12138     op = DW_OP_deref_size;
12139   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12140   return loc;
12141 }
12142
12143 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12144    data attribute for a variable or a parameter.  We generate the
12145    DW_AT_const_value attribute only in those cases where the given variable
12146    or parameter does not have a true "location" either in memory or in a
12147    register.  This can happen (for example) when a constant is passed as an
12148    actual argument in a call to an inline function.  (It's possible that
12149    these things can crop up in other ways also.)  Note that one type of
12150    constant value which can be passed into an inlined function is a constant
12151    pointer.  This can happen for example if an actual argument in an inlined
12152    function call evaluates to a compile-time constant address.  */
12153
12154 static void
12155 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12156                                        enum dwarf_attribute attr)
12157 {
12158   rtx rtl;
12159   dw_loc_descr_ref descr;
12160   var_loc_list *loc_list;
12161   struct var_loc_node *node;
12162   if (TREE_CODE (decl) == ERROR_MARK)
12163     return;
12164
12165   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12166               || TREE_CODE (decl) == RESULT_DECL);
12167
12168   /* See if we possibly have multiple locations for this variable.  */
12169   loc_list = lookup_decl_loc (decl);
12170
12171   /* If it truly has multiple locations, the first and last node will
12172      differ.  */
12173   if (loc_list && loc_list->first != loc_list->last)
12174     {
12175       const char *endname, *secname;
12176       dw_loc_list_ref list;
12177       rtx varloc;
12178       enum var_init_status initialized;
12179
12180       /* Now that we know what section we are using for a base,
12181          actually construct the list of locations.
12182          The first location information is what is passed to the
12183          function that creates the location list, and the remaining
12184          locations just get added on to that list.
12185          Note that we only know the start address for a location
12186          (IE location changes), so to build the range, we use
12187          the range [current location start, next location start].
12188          This means we have to special case the last node, and generate
12189          a range of [last location start, end of function label].  */
12190
12191       node = loc_list->first;
12192       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12193       secname = secname_for_decl (decl);
12194
12195       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12196         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12197       else
12198         initialized = VAR_INIT_STATUS_INITIALIZED;
12199
12200       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12201       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12202       node = node->next;
12203
12204       for (; node->next; node = node->next)
12205         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12206           {
12207             /* The variable has a location between NODE->LABEL and
12208                NODE->NEXT->LABEL.  */
12209             enum var_init_status initialized =
12210               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12211             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12212             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12213                                       decl);
12214             add_loc_descr_to_loc_list (&list, descr,
12215                                        node->label, node->next->label, secname);
12216           }
12217
12218       /* If the variable has a location at the last label
12219          it keeps its location until the end of function.  */
12220       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12221         {
12222           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12223           enum var_init_status initialized =
12224             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12225
12226           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12227           if (!current_function_decl)
12228             endname = text_end_label;
12229           else
12230             {
12231               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12232                                            current_function_funcdef_no);
12233               endname = ggc_strdup (label_id);
12234             }
12235           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12236                                     decl);
12237           add_loc_descr_to_loc_list (&list, descr,
12238                                      node->label, endname, secname);
12239         }
12240
12241       /* Finally, add the location list to the DIE, and we are done.  */
12242       add_AT_loc_list (die, attr, list);
12243       return;
12244     }
12245
12246   /* Try to get some constant RTL for this decl, and use that as the value of
12247      the location.  */
12248
12249   rtl = rtl_for_decl_location (decl);
12250   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12251     {
12252       add_const_value_attribute (die, rtl);
12253       return;
12254     }
12255
12256   /* If we have tried to generate the location otherwise, and it
12257      didn't work out (we wouldn't be here if we did), and we have a one entry
12258      location list, try generating a location from that.  */
12259   if (loc_list && loc_list->first)
12260     {
12261       enum var_init_status status;
12262       node = loc_list->first;
12263       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12264       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12265       if (descr)
12266         {
12267           descr = loc_by_reference (descr, decl);
12268           add_AT_location_description (die, attr, descr);
12269           return;
12270         }
12271     }
12272
12273   /* We couldn't get any rtl, so try directly generating the location
12274      description from the tree.  */
12275   descr = loc_descriptor_from_tree (decl);
12276   if (descr)
12277     {
12278       descr = loc_by_reference (descr, decl);
12279       add_AT_location_description (die, attr, descr);
12280       return;
12281     }
12282   /* None of that worked, so it must not really have a location;
12283      try adding a constant value attribute from the DECL_INITIAL.  */
12284   tree_add_const_value_attribute (die, decl);
12285 }
12286
12287 /* Add VARIABLE and DIE into deferred locations list.  */
12288
12289 static void
12290 defer_location (tree variable, dw_die_ref die)
12291 {
12292   deferred_locations entry;
12293   entry.variable = variable;
12294   entry.die = die;
12295   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12296 }
12297
12298 /* Helper function for tree_add_const_value_attribute.  Natively encode
12299    initializer INIT into an array.  Return true if successful.  */
12300
12301 static bool
12302 native_encode_initializer (tree init, unsigned char *array, int size)
12303 {
12304   tree type;
12305
12306   if (init == NULL_TREE)
12307     return false;
12308
12309   STRIP_NOPS (init);
12310   switch (TREE_CODE (init))
12311     {
12312     case STRING_CST:
12313       type = TREE_TYPE (init);
12314       if (TREE_CODE (type) == ARRAY_TYPE)
12315         {
12316           tree enttype = TREE_TYPE (type);
12317           enum machine_mode mode = TYPE_MODE (enttype);
12318
12319           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12320             return false;
12321           if (int_size_in_bytes (type) != size)
12322             return false;
12323           if (size > TREE_STRING_LENGTH (init))
12324             {
12325               memcpy (array, TREE_STRING_POINTER (init),
12326                       TREE_STRING_LENGTH (init));
12327               memset (array + TREE_STRING_LENGTH (init),
12328                       '\0', size - TREE_STRING_LENGTH (init));
12329             }
12330           else
12331             memcpy (array, TREE_STRING_POINTER (init), size);
12332           return true;
12333         }
12334       return false;
12335     case CONSTRUCTOR:
12336       type = TREE_TYPE (init);
12337       if (int_size_in_bytes (type) != size)
12338         return false;
12339       if (TREE_CODE (type) == ARRAY_TYPE)
12340         {
12341           HOST_WIDE_INT min_index;
12342           unsigned HOST_WIDE_INT cnt;
12343           int curpos = 0, fieldsize;
12344           constructor_elt *ce;
12345
12346           if (TYPE_DOMAIN (type) == NULL_TREE
12347               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12348             return false;
12349
12350           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12351           if (fieldsize <= 0)
12352             return false;
12353
12354           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12355           memset (array, '\0', size);
12356           for (cnt = 0;
12357                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12358                cnt++)
12359             {
12360               tree val = ce->value;
12361               tree index = ce->index;
12362               int pos = curpos;
12363               if (index && TREE_CODE (index) == RANGE_EXPR)
12364                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12365                       * fieldsize;
12366               else if (index)
12367                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12368
12369               if (val)
12370                 {
12371                   STRIP_NOPS (val);
12372                   if (!native_encode_initializer (val, array + pos, fieldsize))
12373                     return false;
12374                 }
12375               curpos = pos + fieldsize;
12376               if (index && TREE_CODE (index) == RANGE_EXPR)
12377                 {
12378                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12379                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12380                   while (count > 0)
12381                     {
12382                       if (val)
12383                         memcpy (array + curpos, array + pos, fieldsize);
12384                       curpos += fieldsize;
12385                     }
12386                 }
12387               gcc_assert (curpos <= size);
12388             }
12389           return true;
12390         }
12391       else if (TREE_CODE (type) == RECORD_TYPE
12392                || TREE_CODE (type) == UNION_TYPE)
12393         {
12394           tree field = NULL_TREE;
12395           unsigned HOST_WIDE_INT cnt;
12396           constructor_elt *ce;
12397
12398           if (int_size_in_bytes (type) != size)
12399             return false;
12400
12401           if (TREE_CODE (type) == RECORD_TYPE)
12402             field = TYPE_FIELDS (type);
12403
12404           for (cnt = 0;
12405                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12406                cnt++, field = field ? TREE_CHAIN (field) : 0)
12407             {
12408               tree val = ce->value;
12409               int pos, fieldsize;
12410
12411               if (ce->index != 0)
12412                 field = ce->index;
12413
12414               if (val)
12415                 STRIP_NOPS (val);
12416
12417               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12418                 return false;
12419
12420               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12421                   && TYPE_DOMAIN (TREE_TYPE (field))
12422                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12423                 return false;
12424               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12425                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12426                 return false;
12427               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12428               pos = int_byte_position (field);
12429               gcc_assert (pos + fieldsize <= size);
12430               if (val
12431                   && !native_encode_initializer (val, array + pos, fieldsize))
12432                 return false;
12433             }
12434           return true;
12435         }
12436       return false;
12437     case VIEW_CONVERT_EXPR:
12438     case NON_LVALUE_EXPR:
12439       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12440     default:
12441       return native_encode_expr (init, array, size) == size;
12442     }
12443 }
12444
12445 /* If we don't have a copy of this variable in memory for some reason (such
12446    as a C++ member constant that doesn't have an out-of-line definition),
12447    we should tell the debugger about the constant value.  */
12448
12449 static void
12450 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12451 {
12452   tree init;
12453   tree type = TREE_TYPE (decl);
12454   rtx rtl;
12455
12456   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12457     return;
12458
12459   init = DECL_INITIAL (decl);
12460   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12461     /* OK */;
12462   else
12463     return;
12464
12465   rtl = rtl_for_decl_init (init, type);
12466   if (rtl)
12467     add_const_value_attribute (var_die, rtl);
12468   /* If the host and target are sane, try harder.  */
12469   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12470            && initializer_constant_valid_p (init, type))
12471     {
12472       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12473       if (size > 0 && (int) size == size)
12474         {
12475           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12476
12477           if (native_encode_initializer (init, array, size))
12478             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12479         }
12480     }
12481 }
12482
12483 /* Convert the CFI instructions for the current function into a
12484    location list.  This is used for DW_AT_frame_base when we targeting
12485    a dwarf2 consumer that does not support the dwarf3
12486    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12487    expressions.  */
12488
12489 static dw_loc_list_ref
12490 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12491 {
12492   dw_fde_ref fde;
12493   dw_loc_list_ref list, *list_tail;
12494   dw_cfi_ref cfi;
12495   dw_cfa_location last_cfa, next_cfa;
12496   const char *start_label, *last_label, *section;
12497   dw_cfa_location remember;
12498
12499   fde = current_fde ();
12500   gcc_assert (fde != NULL);
12501
12502   section = secname_for_decl (current_function_decl);
12503   list_tail = &list;
12504   list = NULL;
12505
12506   memset (&next_cfa, 0, sizeof (next_cfa));
12507   next_cfa.reg = INVALID_REGNUM;
12508   remember = next_cfa;
12509
12510   start_label = fde->dw_fde_begin;
12511
12512   /* ??? Bald assumption that the CIE opcode list does not contain
12513      advance opcodes.  */
12514   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12515     lookup_cfa_1 (cfi, &next_cfa, &remember);
12516
12517   last_cfa = next_cfa;
12518   last_label = start_label;
12519
12520   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12521     switch (cfi->dw_cfi_opc)
12522       {
12523       case DW_CFA_set_loc:
12524       case DW_CFA_advance_loc1:
12525       case DW_CFA_advance_loc2:
12526       case DW_CFA_advance_loc4:
12527         if (!cfa_equal_p (&last_cfa, &next_cfa))
12528           {
12529             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12530                                        start_label, last_label, section,
12531                                        list == NULL);
12532
12533             list_tail = &(*list_tail)->dw_loc_next;
12534             last_cfa = next_cfa;
12535             start_label = last_label;
12536           }
12537         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12538         break;
12539
12540       case DW_CFA_advance_loc:
12541         /* The encoding is complex enough that we should never emit this.  */
12542         gcc_unreachable ();
12543
12544       default:
12545         lookup_cfa_1 (cfi, &next_cfa, &remember);
12546         break;
12547       }
12548
12549   if (!cfa_equal_p (&last_cfa, &next_cfa))
12550     {
12551       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12552                                  start_label, last_label, section,
12553                                  list == NULL);
12554       list_tail = &(*list_tail)->dw_loc_next;
12555       start_label = last_label;
12556     }
12557   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12558                              start_label, fde->dw_fde_end, section,
12559                              list == NULL);
12560
12561   return list;
12562 }
12563
12564 /* Compute a displacement from the "steady-state frame pointer" to the
12565    frame base (often the same as the CFA), and store it in
12566    frame_pointer_fb_offset.  OFFSET is added to the displacement
12567    before the latter is negated.  */
12568
12569 static void
12570 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12571 {
12572   rtx reg, elim;
12573
12574 #ifdef FRAME_POINTER_CFA_OFFSET
12575   reg = frame_pointer_rtx;
12576   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12577 #else
12578   reg = arg_pointer_rtx;
12579   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12580 #endif
12581
12582   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12583   if (GET_CODE (elim) == PLUS)
12584     {
12585       offset += INTVAL (XEXP (elim, 1));
12586       elim = XEXP (elim, 0);
12587     }
12588
12589   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12590                && (elim == hard_frame_pointer_rtx
12591                    || elim == stack_pointer_rtx))
12592               || elim == (frame_pointer_needed
12593                           ? hard_frame_pointer_rtx
12594                           : stack_pointer_rtx));
12595
12596   frame_pointer_fb_offset = -offset;
12597 }
12598
12599 /* Generate a DW_AT_name attribute given some string value to be included as
12600    the value of the attribute.  */
12601
12602 static void
12603 add_name_attribute (dw_die_ref die, const char *name_string)
12604 {
12605   if (name_string != NULL && *name_string != 0)
12606     {
12607       if (demangle_name_func)
12608         name_string = (*demangle_name_func) (name_string);
12609
12610       add_AT_string (die, DW_AT_name, name_string);
12611     }
12612 }
12613
12614 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12615
12616 static void
12617 add_comp_dir_attribute (dw_die_ref die)
12618 {
12619   const char *wd = get_src_pwd ();
12620   if (wd != NULL)
12621     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12622 }
12623
12624 /* Given a tree node describing an array bound (either lower or upper) output
12625    a representation for that bound.  */
12626
12627 static void
12628 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12629 {
12630   switch (TREE_CODE (bound))
12631     {
12632     case ERROR_MARK:
12633       return;
12634
12635     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12636     case INTEGER_CST:
12637       if (! host_integerp (bound, 0)
12638           || (bound_attr == DW_AT_lower_bound
12639               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12640                   || (is_fortran () && integer_onep (bound)))))
12641         /* Use the default.  */
12642         ;
12643       else
12644         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12645       break;
12646
12647     CASE_CONVERT:
12648     case VIEW_CONVERT_EXPR:
12649       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12650       break;
12651
12652     case SAVE_EXPR:
12653       break;
12654
12655     case VAR_DECL:
12656     case PARM_DECL:
12657     case RESULT_DECL:
12658       {
12659         dw_die_ref decl_die = lookup_decl_die (bound);
12660         dw_loc_descr_ref loc;
12661
12662         /* ??? Can this happen, or should the variable have been bound
12663            first?  Probably it can, since I imagine that we try to create
12664            the types of parameters in the order in which they exist in
12665            the list, and won't have created a forward reference to a
12666            later parameter.  */
12667         if (decl_die != NULL)
12668           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12669         else
12670           {
12671             loc = loc_descriptor_from_tree_1 (bound, 0);
12672             add_AT_location_description (subrange_die, bound_attr, loc);
12673           }
12674         break;
12675       }
12676
12677     default:
12678       {
12679         /* Otherwise try to create a stack operation procedure to
12680            evaluate the value of the array bound.  */
12681
12682         dw_die_ref ctx, decl_die;
12683         dw_loc_descr_ref loc;
12684
12685         loc = loc_descriptor_from_tree (bound);
12686         if (loc == NULL)
12687           break;
12688
12689         if (current_function_decl == 0)
12690           ctx = comp_unit_die;
12691         else
12692           ctx = lookup_decl_die (current_function_decl);
12693
12694         decl_die = new_die (DW_TAG_variable, ctx, bound);
12695         add_AT_flag (decl_die, DW_AT_artificial, 1);
12696         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12697         add_AT_loc (decl_die, DW_AT_location, loc);
12698
12699         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12700         break;
12701       }
12702     }
12703 }
12704
12705 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12706    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12707    Note that the block of subscript information for an array type also
12708    includes information about the element type of the given array type.  */
12709
12710 static void
12711 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12712 {
12713   unsigned dimension_number;
12714   tree lower, upper;
12715   dw_die_ref subrange_die;
12716
12717   for (dimension_number = 0;
12718        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12719        type = TREE_TYPE (type), dimension_number++)
12720     {
12721       tree domain = TYPE_DOMAIN (type);
12722
12723       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12724         break;
12725
12726       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12727          and (in GNU C only) variable bounds.  Handle all three forms
12728          here.  */
12729       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12730       if (domain)
12731         {
12732           /* We have an array type with specified bounds.  */
12733           lower = TYPE_MIN_VALUE (domain);
12734           upper = TYPE_MAX_VALUE (domain);
12735
12736           /* Define the index type.  */
12737           if (TREE_TYPE (domain))
12738             {
12739               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12740                  TREE_TYPE field.  We can't emit debug info for this
12741                  because it is an unnamed integral type.  */
12742               if (TREE_CODE (domain) == INTEGER_TYPE
12743                   && TYPE_NAME (domain) == NULL_TREE
12744                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12745                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12746                 ;
12747               else
12748                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12749                                     type_die);
12750             }
12751
12752           /* ??? If upper is NULL, the array has unspecified length,
12753              but it does have a lower bound.  This happens with Fortran
12754                dimension arr(N:*)
12755              Since the debugger is definitely going to need to know N
12756              to produce useful results, go ahead and output the lower
12757              bound solo, and hope the debugger can cope.  */
12758
12759           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12760           if (upper)
12761             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12762         }
12763
12764       /* Otherwise we have an array type with an unspecified length.  The
12765          DWARF-2 spec does not say how to handle this; let's just leave out the
12766          bounds.  */
12767     }
12768 }
12769
12770 static void
12771 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12772 {
12773   unsigned size;
12774
12775   switch (TREE_CODE (tree_node))
12776     {
12777     case ERROR_MARK:
12778       size = 0;
12779       break;
12780     case ENUMERAL_TYPE:
12781     case RECORD_TYPE:
12782     case UNION_TYPE:
12783     case QUAL_UNION_TYPE:
12784       size = int_size_in_bytes (tree_node);
12785       break;
12786     case FIELD_DECL:
12787       /* For a data member of a struct or union, the DW_AT_byte_size is
12788          generally given as the number of bytes normally allocated for an
12789          object of the *declared* type of the member itself.  This is true
12790          even for bit-fields.  */
12791       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12792       break;
12793     default:
12794       gcc_unreachable ();
12795     }
12796
12797   /* Note that `size' might be -1 when we get to this point.  If it is, that
12798      indicates that the byte size of the entity in question is variable.  We
12799      have no good way of expressing this fact in Dwarf at the present time,
12800      so just let the -1 pass on through.  */
12801   add_AT_unsigned (die, DW_AT_byte_size, size);
12802 }
12803
12804 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12805    which specifies the distance in bits from the highest order bit of the
12806    "containing object" for the bit-field to the highest order bit of the
12807    bit-field itself.
12808
12809    For any given bit-field, the "containing object" is a hypothetical object
12810    (of some integral or enum type) within which the given bit-field lives.  The
12811    type of this hypothetical "containing object" is always the same as the
12812    declared type of the individual bit-field itself.  The determination of the
12813    exact location of the "containing object" for a bit-field is rather
12814    complicated.  It's handled by the `field_byte_offset' function (above).
12815
12816    Note that it is the size (in bytes) of the hypothetical "containing object"
12817    which will be given in the DW_AT_byte_size attribute for this bit-field.
12818    (See `byte_size_attribute' above).  */
12819
12820 static inline void
12821 add_bit_offset_attribute (dw_die_ref die, tree decl)
12822 {
12823   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12824   tree type = DECL_BIT_FIELD_TYPE (decl);
12825   HOST_WIDE_INT bitpos_int;
12826   HOST_WIDE_INT highest_order_object_bit_offset;
12827   HOST_WIDE_INT highest_order_field_bit_offset;
12828   HOST_WIDE_INT unsigned bit_offset;
12829
12830   /* Must be a field and a bit field.  */
12831   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12832
12833   /* We can't yet handle bit-fields whose offsets are variable, so if we
12834      encounter such things, just return without generating any attribute
12835      whatsoever.  Likewise for variable or too large size.  */
12836   if (! host_integerp (bit_position (decl), 0)
12837       || ! host_integerp (DECL_SIZE (decl), 1))
12838     return;
12839
12840   bitpos_int = int_bit_position (decl);
12841
12842   /* Note that the bit offset is always the distance (in bits) from the
12843      highest-order bit of the "containing object" to the highest-order bit of
12844      the bit-field itself.  Since the "high-order end" of any object or field
12845      is different on big-endian and little-endian machines, the computation
12846      below must take account of these differences.  */
12847   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12848   highest_order_field_bit_offset = bitpos_int;
12849
12850   if (! BYTES_BIG_ENDIAN)
12851     {
12852       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12853       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12854     }
12855
12856   bit_offset
12857     = (! BYTES_BIG_ENDIAN
12858        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12859        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12860
12861   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12862 }
12863
12864 /* For a FIELD_DECL node which represents a bit field, output an attribute
12865    which specifies the length in bits of the given field.  */
12866
12867 static inline void
12868 add_bit_size_attribute (dw_die_ref die, tree decl)
12869 {
12870   /* Must be a field and a bit field.  */
12871   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12872               && DECL_BIT_FIELD_TYPE (decl));
12873
12874   if (host_integerp (DECL_SIZE (decl), 1))
12875     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12876 }
12877
12878 /* If the compiled language is ANSI C, then add a 'prototyped'
12879    attribute, if arg types are given for the parameters of a function.  */
12880
12881 static inline void
12882 add_prototyped_attribute (dw_die_ref die, tree func_type)
12883 {
12884   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12885       && TYPE_ARG_TYPES (func_type) != NULL)
12886     add_AT_flag (die, DW_AT_prototyped, 1);
12887 }
12888
12889 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12890    by looking in either the type declaration or object declaration
12891    equate table.  */
12892
12893 static inline dw_die_ref
12894 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12895 {
12896   dw_die_ref origin_die = NULL;
12897
12898   if (TREE_CODE (origin) != FUNCTION_DECL)
12899     {
12900       /* We may have gotten separated from the block for the inlined
12901          function, if we're in an exception handler or some such; make
12902          sure that the abstract function has been written out.
12903
12904          Doing this for nested functions is wrong, however; functions are
12905          distinct units, and our context might not even be inline.  */
12906       tree fn = origin;
12907
12908       if (TYPE_P (fn))
12909         fn = TYPE_STUB_DECL (fn);
12910
12911       fn = decl_function_context (fn);
12912       if (fn)
12913         dwarf2out_abstract_function (fn);
12914     }
12915
12916   if (DECL_P (origin))
12917     origin_die = lookup_decl_die (origin);
12918   else if (TYPE_P (origin))
12919     origin_die = lookup_type_die (origin);
12920
12921   /* XXX: Functions that are never lowered don't always have correct block
12922      trees (in the case of java, they simply have no block tree, in some other
12923      languages).  For these functions, there is nothing we can really do to
12924      output correct debug info for inlined functions in all cases.  Rather
12925      than die, we'll just produce deficient debug info now, in that we will
12926      have variables without a proper abstract origin.  In the future, when all
12927      functions are lowered, we should re-add a gcc_assert (origin_die)
12928      here.  */
12929
12930   if (origin_die)
12931     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12932   return origin_die;
12933 }
12934
12935 /* We do not currently support the pure_virtual attribute.  */
12936
12937 static inline void
12938 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12939 {
12940   if (DECL_VINDEX (func_decl))
12941     {
12942       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12943
12944       if (host_integerp (DECL_VINDEX (func_decl), 0))
12945         add_AT_loc (die, DW_AT_vtable_elem_location,
12946                     new_loc_descr (DW_OP_constu,
12947                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12948                                    0));
12949
12950       /* GNU extension: Record what type this method came from originally.  */
12951       if (debug_info_level > DINFO_LEVEL_TERSE)
12952         add_AT_die_ref (die, DW_AT_containing_type,
12953                         lookup_type_die (DECL_CONTEXT (func_decl)));
12954     }
12955 }
12956 \f
12957 /* Add source coordinate attributes for the given decl.  */
12958
12959 static void
12960 add_src_coords_attributes (dw_die_ref die, tree decl)
12961 {
12962   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12963
12964   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12965   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12966 }
12967
12968 /* Add a DW_AT_name attribute and source coordinate attribute for the
12969    given decl, but only if it actually has a name.  */
12970
12971 static void
12972 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12973 {
12974   tree decl_name;
12975
12976   decl_name = DECL_NAME (decl);
12977   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12978     {
12979       add_name_attribute (die, dwarf2_name (decl, 0));
12980       if (! DECL_ARTIFICIAL (decl))
12981         add_src_coords_attributes (die, decl);
12982
12983       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12984           && TREE_PUBLIC (decl)
12985           && !DECL_ABSTRACT (decl)
12986           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12987           && !is_fortran ())
12988         {
12989           /* Defer until we have an assembler name set.  */
12990           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
12991             {
12992               limbo_die_node *asm_name;
12993
12994               asm_name = GGC_CNEW (limbo_die_node);
12995               asm_name->die = die;
12996               asm_name->created_for = decl;
12997               asm_name->next = deferred_asm_name;
12998               deferred_asm_name = asm_name;
12999             }
13000           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
13001             add_AT_string (die, DW_AT_MIPS_linkage_name,
13002                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
13003         }
13004     }
13005
13006 #ifdef VMS_DEBUGGING_INFO
13007   /* Get the function's name, as described by its RTL.  This may be different
13008      from the DECL_NAME name used in the source file.  */
13009   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
13010     {
13011       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
13012                    XEXP (DECL_RTL (decl), 0));
13013       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
13014     }
13015 #endif
13016 }
13017
13018 /* Push a new declaration scope.  */
13019
13020 static void
13021 push_decl_scope (tree scope)
13022 {
13023   VEC_safe_push (tree, gc, decl_scope_table, scope);
13024 }
13025
13026 /* Pop a declaration scope.  */
13027
13028 static inline void
13029 pop_decl_scope (void)
13030 {
13031   VEC_pop (tree, decl_scope_table);
13032 }
13033
13034 /* Return the DIE for the scope that immediately contains this type.
13035    Non-named types get global scope.  Named types nested in other
13036    types get their containing scope if it's open, or global scope
13037    otherwise.  All other types (i.e. function-local named types) get
13038    the current active scope.  */
13039
13040 static dw_die_ref
13041 scope_die_for (tree t, dw_die_ref context_die)
13042 {
13043   dw_die_ref scope_die = NULL;
13044   tree containing_scope;
13045   int i;
13046
13047   /* Non-types always go in the current scope.  */
13048   gcc_assert (TYPE_P (t));
13049
13050   containing_scope = TYPE_CONTEXT (t);
13051
13052   /* Use the containing namespace if it was passed in (for a declaration).  */
13053   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
13054     {
13055       if (context_die == lookup_decl_die (containing_scope))
13056         /* OK */;
13057       else
13058         containing_scope = NULL_TREE;
13059     }
13060
13061   /* Ignore function type "scopes" from the C frontend.  They mean that
13062      a tagged type is local to a parmlist of a function declarator, but
13063      that isn't useful to DWARF.  */
13064   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13065     containing_scope = NULL_TREE;
13066
13067   if (containing_scope == NULL_TREE)
13068     scope_die = comp_unit_die;
13069   else if (TYPE_P (containing_scope))
13070     {
13071       /* For types, we can just look up the appropriate DIE.  But
13072          first we check to see if we're in the middle of emitting it
13073          so we know where the new DIE should go.  */
13074       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13075         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13076           break;
13077
13078       if (i < 0)
13079         {
13080           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13081                       || TREE_ASM_WRITTEN (containing_scope));
13082
13083           /* If none of the current dies are suitable, we get file scope.  */
13084           scope_die = comp_unit_die;
13085         }
13086       else
13087         scope_die = lookup_type_die (containing_scope);
13088     }
13089   else
13090     scope_die = context_die;
13091
13092   return scope_die;
13093 }
13094
13095 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13096
13097 static inline int
13098 local_scope_p (dw_die_ref context_die)
13099 {
13100   for (; context_die; context_die = context_die->die_parent)
13101     if (context_die->die_tag == DW_TAG_inlined_subroutine
13102         || context_die->die_tag == DW_TAG_subprogram)
13103       return 1;
13104
13105   return 0;
13106 }
13107
13108 /* Returns nonzero if CONTEXT_DIE is a class.  */
13109
13110 static inline int
13111 class_scope_p (dw_die_ref context_die)
13112 {
13113   return (context_die
13114           && (context_die->die_tag == DW_TAG_structure_type
13115               || context_die->die_tag == DW_TAG_class_type
13116               || context_die->die_tag == DW_TAG_interface_type
13117               || context_die->die_tag == DW_TAG_union_type));
13118 }
13119
13120 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13121    whether or not to treat a DIE in this context as a declaration.  */
13122
13123 static inline int
13124 class_or_namespace_scope_p (dw_die_ref context_die)
13125 {
13126   return (class_scope_p (context_die)
13127           || (context_die && context_die->die_tag == DW_TAG_namespace));
13128 }
13129
13130 /* Many forms of DIEs require a "type description" attribute.  This
13131    routine locates the proper "type descriptor" die for the type given
13132    by 'type', and adds a DW_AT_type attribute below the given die.  */
13133
13134 static void
13135 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13136                     int decl_volatile, dw_die_ref context_die)
13137 {
13138   enum tree_code code  = TREE_CODE (type);
13139   dw_die_ref type_die  = NULL;
13140
13141   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13142      or fixed-point type, use the inner type.  This is because we have no
13143      support for unnamed types in base_type_die.  This can happen if this is
13144      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13145   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13146       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13147     type = TREE_TYPE (type), code = TREE_CODE (type);
13148
13149   if (code == ERROR_MARK
13150       /* Handle a special case.  For functions whose return type is void, we
13151          generate *no* type attribute.  (Note that no object may have type
13152          `void', so this only applies to function return types).  */
13153       || code == VOID_TYPE)
13154     return;
13155
13156   type_die = modified_type_die (type,
13157                                 decl_const || TYPE_READONLY (type),
13158                                 decl_volatile || TYPE_VOLATILE (type),
13159                                 context_die);
13160
13161   if (type_die != NULL)
13162     add_AT_die_ref (object_die, DW_AT_type, type_die);
13163 }
13164
13165 /* Given an object die, add the calling convention attribute for the
13166    function call type.  */
13167 static void
13168 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13169 {
13170   enum dwarf_calling_convention value = DW_CC_normal;
13171
13172   value = ((enum dwarf_calling_convention)
13173            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13174
13175   /* DWARF doesn't provide a way to identify a program's source-level
13176      entry point.  DW_AT_calling_convention attributes are only meant
13177      to describe functions' calling conventions.  However, lacking a
13178      better way to signal the Fortran main program, we use this for the
13179      time being, following existing custom.  */
13180   if (is_fortran ()
13181       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13182     value = DW_CC_program;
13183
13184   /* Only add the attribute if the backend requests it, and
13185      is not DW_CC_normal.  */
13186   if (value && (value != DW_CC_normal))
13187     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13188 }
13189
13190 /* Given a tree pointer to a struct, class, union, or enum type node, return
13191    a pointer to the (string) tag name for the given type, or zero if the type
13192    was declared without a tag.  */
13193
13194 static const char *
13195 type_tag (const_tree type)
13196 {
13197   const char *name = 0;
13198
13199   if (TYPE_NAME (type) != 0)
13200     {
13201       tree t = 0;
13202
13203       /* Find the IDENTIFIER_NODE for the type name.  */
13204       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13205         t = TYPE_NAME (type);
13206
13207       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13208          a TYPE_DECL node, regardless of whether or not a `typedef' was
13209          involved.  */
13210       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13211                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13212         {
13213           /* We want to be extra verbose.  Don't call dwarf_name if
13214              DECL_NAME isn't set.  The default hook for decl_printable_name
13215              doesn't like that, and in this context it's correct to return
13216              0, instead of "<anonymous>" or the like.  */
13217           if (DECL_NAME (TYPE_NAME (type)))
13218             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13219         }
13220
13221       /* Now get the name as a string, or invent one.  */
13222       if (!name && t != 0)
13223         name = IDENTIFIER_POINTER (t);
13224     }
13225
13226   return (name == 0 || *name == '\0') ? 0 : name;
13227 }
13228
13229 /* Return the type associated with a data member, make a special check
13230    for bit field types.  */
13231
13232 static inline tree
13233 member_declared_type (const_tree member)
13234 {
13235   return (DECL_BIT_FIELD_TYPE (member)
13236           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13237 }
13238
13239 /* Get the decl's label, as described by its RTL. This may be different
13240    from the DECL_NAME name used in the source file.  */
13241
13242 #if 0
13243 static const char *
13244 decl_start_label (tree decl)
13245 {
13246   rtx x;
13247   const char *fnname;
13248
13249   x = DECL_RTL (decl);
13250   gcc_assert (MEM_P (x));
13251
13252   x = XEXP (x, 0);
13253   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13254
13255   fnname = XSTR (x, 0);
13256   return fnname;
13257 }
13258 #endif
13259 \f
13260 /* These routines generate the internal representation of the DIE's for
13261    the compilation unit.  Debugging information is collected by walking
13262    the declaration trees passed in from dwarf2out_decl().  */
13263
13264 static void
13265 gen_array_type_die (tree type, dw_die_ref context_die)
13266 {
13267   dw_die_ref scope_die = scope_die_for (type, context_die);
13268   dw_die_ref array_die;
13269
13270   /* GNU compilers represent multidimensional array types as sequences of one
13271      dimensional array types whose element types are themselves array types.
13272      We sometimes squish that down to a single array_type DIE with multiple
13273      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13274      say that we are allowed to do this kind of compression in C, because
13275      there is no difference between an array of arrays and a multidimensional
13276      array.  We don't do this for Ada to remain as close as possible to the
13277      actual representation, which is especially important against the language
13278      flexibilty wrt arrays of variable size.  */
13279
13280   bool collapse_nested_arrays = !is_ada ();
13281   tree element_type;
13282
13283   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13284      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13285   if (TYPE_STRING_FLAG (type)
13286       && TREE_CODE (type) == ARRAY_TYPE
13287       && is_fortran ()
13288       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13289     {
13290       HOST_WIDE_INT size;
13291
13292       array_die = new_die (DW_TAG_string_type, scope_die, type);
13293       add_name_attribute (array_die, type_tag (type));
13294       equate_type_number_to_die (type, array_die);
13295       size = int_size_in_bytes (type);
13296       if (size >= 0)
13297         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13298       else if (TYPE_DOMAIN (type) != NULL_TREE
13299                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13300                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13301         {
13302           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13303           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13304
13305           size = int_size_in_bytes (TREE_TYPE (szdecl));
13306           if (loc && size > 0)
13307             {
13308               add_AT_loc (array_die, DW_AT_string_length, loc);
13309               if (size != DWARF2_ADDR_SIZE)
13310                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13311             }
13312         }
13313       return;
13314     }
13315
13316   /* ??? The SGI dwarf reader fails for array of array of enum types
13317      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13318      array type comes before the outer array type.  We thus call gen_type_die
13319      before we new_die and must prevent nested array types collapsing for this
13320      target.  */
13321
13322 #ifdef MIPS_DEBUGGING_INFO
13323   gen_type_die (TREE_TYPE (type), context_die);
13324   collapse_nested_arrays = false;
13325 #endif
13326
13327   array_die = new_die (DW_TAG_array_type, scope_die, type);
13328   add_name_attribute (array_die, type_tag (type));
13329   equate_type_number_to_die (type, array_die);
13330
13331   if (TREE_CODE (type) == VECTOR_TYPE)
13332     {
13333       /* The frontend feeds us a representation for the vector as a struct
13334          containing an array.  Pull out the array type.  */
13335       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13336       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13337     }
13338
13339   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13340   if (is_fortran ()
13341       && TREE_CODE (type) == ARRAY_TYPE
13342       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13343       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13344     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13345
13346 #if 0
13347   /* We default the array ordering.  SDB will probably do
13348      the right things even if DW_AT_ordering is not present.  It's not even
13349      an issue until we start to get into multidimensional arrays anyway.  If
13350      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13351      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13352      and when we find out that we need to put these in, we will only do so
13353      for multidimensional arrays.  */
13354   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13355 #endif
13356
13357 #ifdef MIPS_DEBUGGING_INFO
13358   /* The SGI compilers handle arrays of unknown bound by setting
13359      AT_declaration and not emitting any subrange DIEs.  */
13360   if (! TYPE_DOMAIN (type))
13361     add_AT_flag (array_die, DW_AT_declaration, 1);
13362   else
13363 #endif
13364     add_subscript_info (array_die, type, collapse_nested_arrays);
13365
13366   /* Add representation of the type of the elements of this array type and
13367      emit the corresponding DIE if we haven't done it already.  */  
13368   element_type = TREE_TYPE (type);
13369   if (collapse_nested_arrays)
13370     while (TREE_CODE (element_type) == ARRAY_TYPE)
13371       {
13372         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13373           break;
13374         element_type = TREE_TYPE (element_type);
13375       }
13376
13377 #ifndef MIPS_DEBUGGING_INFO
13378   gen_type_die (element_type, context_die);
13379 #endif
13380
13381   add_type_attribute (array_die, element_type, 0, 0, context_die);
13382
13383   if (get_AT (array_die, DW_AT_name))
13384     add_pubtype (type, array_die);
13385 }
13386
13387 static dw_loc_descr_ref
13388 descr_info_loc (tree val, tree base_decl)
13389 {
13390   HOST_WIDE_INT size;
13391   dw_loc_descr_ref loc, loc2;
13392   enum dwarf_location_atom op;
13393
13394   if (val == base_decl)
13395     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13396
13397   switch (TREE_CODE (val))
13398     {
13399     CASE_CONVERT:
13400       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13401     case VAR_DECL:
13402       return loc_descriptor_from_tree_1 (val, 0);
13403     case INTEGER_CST:
13404       if (host_integerp (val, 0))
13405         return int_loc_descriptor (tree_low_cst (val, 0));
13406       break;
13407     case INDIRECT_REF:
13408       size = int_size_in_bytes (TREE_TYPE (val));
13409       if (size < 0)
13410         break;
13411       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13412       if (!loc)
13413         break;
13414       if (size == DWARF2_ADDR_SIZE)
13415         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13416       else
13417         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13418       return loc;
13419     case POINTER_PLUS_EXPR:
13420     case PLUS_EXPR:
13421       if (host_integerp (TREE_OPERAND (val, 1), 1)
13422           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13423              < 16384)
13424         {
13425           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13426           if (!loc)
13427             break;
13428           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13429         }
13430       else
13431         {
13432           op = DW_OP_plus;
13433         do_binop:
13434           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13435           if (!loc)
13436             break;
13437           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13438           if (!loc2)
13439             break;
13440           add_loc_descr (&loc, loc2);
13441           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13442         }
13443       return loc;
13444     case MINUS_EXPR:
13445       op = DW_OP_minus;
13446       goto do_binop;
13447     case MULT_EXPR:
13448       op = DW_OP_mul;
13449       goto do_binop;
13450     case EQ_EXPR:
13451       op = DW_OP_eq;
13452       goto do_binop;
13453     case NE_EXPR:
13454       op = DW_OP_ne;
13455       goto do_binop;
13456     default:
13457       break;
13458     }
13459   return NULL;
13460 }
13461
13462 static void
13463 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13464                       tree val, tree base_decl)
13465 {
13466   dw_loc_descr_ref loc;
13467
13468   if (host_integerp (val, 0))
13469     {
13470       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13471       return;
13472     }
13473
13474   loc = descr_info_loc (val, base_decl);
13475   if (!loc)
13476     return;
13477
13478   add_AT_loc (die, attr, loc);
13479 }
13480
13481 /* This routine generates DIE for array with hidden descriptor, details
13482    are filled into *info by a langhook.  */
13483
13484 static void
13485 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13486                           dw_die_ref context_die)
13487 {
13488   dw_die_ref scope_die = scope_die_for (type, context_die);
13489   dw_die_ref array_die;
13490   int dim;
13491
13492   array_die = new_die (DW_TAG_array_type, scope_die, type);
13493   add_name_attribute (array_die, type_tag (type));
13494   equate_type_number_to_die (type, array_die);
13495
13496   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13497   if (is_fortran ()
13498       && info->ndimensions >= 2)
13499     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13500
13501   if (info->data_location)
13502     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13503                           info->base_decl);
13504   if (info->associated)
13505     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13506                           info->base_decl);
13507   if (info->allocated)
13508     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13509                           info->base_decl);
13510
13511   for (dim = 0; dim < info->ndimensions; dim++)
13512     {
13513       dw_die_ref subrange_die
13514         = new_die (DW_TAG_subrange_type, array_die, NULL);
13515
13516       if (info->dimen[dim].lower_bound)
13517         {
13518           /* If it is the default value, omit it.  */
13519           if ((is_c_family () || is_java ())
13520               && integer_zerop (info->dimen[dim].lower_bound))
13521             ;
13522           else if (is_fortran ()
13523                    && integer_onep (info->dimen[dim].lower_bound))
13524             ;
13525           else
13526             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13527                                   info->dimen[dim].lower_bound,
13528                                   info->base_decl);
13529         }
13530       if (info->dimen[dim].upper_bound)
13531         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13532                               info->dimen[dim].upper_bound,
13533                               info->base_decl);
13534       if (info->dimen[dim].stride)
13535         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13536                               info->dimen[dim].stride,
13537                               info->base_decl);
13538     }
13539
13540   gen_type_die (info->element_type, context_die);
13541   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13542
13543   if (get_AT (array_die, DW_AT_name))
13544     add_pubtype (type, array_die);
13545 }
13546
13547 #if 0
13548 static void
13549 gen_entry_point_die (tree decl, dw_die_ref context_die)
13550 {
13551   tree origin = decl_ultimate_origin (decl);
13552   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13553
13554   if (origin != NULL)
13555     add_abstract_origin_attribute (decl_die, origin);
13556   else
13557     {
13558       add_name_and_src_coords_attributes (decl_die, decl);
13559       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13560                           0, 0, context_die);
13561     }
13562
13563   if (DECL_ABSTRACT (decl))
13564     equate_decl_number_to_die (decl, decl_die);
13565   else
13566     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13567 }
13568 #endif
13569
13570 /* Walk through the list of incomplete types again, trying once more to
13571    emit full debugging info for them.  */
13572
13573 static void
13574 retry_incomplete_types (void)
13575 {
13576   int i;
13577
13578   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13579     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13580 }
13581
13582 /* Determine what tag to use for a record type.  */
13583
13584 static enum dwarf_tag
13585 record_type_tag (tree type)
13586 {
13587   if (! lang_hooks.types.classify_record)
13588     return DW_TAG_structure_type;
13589
13590   switch (lang_hooks.types.classify_record (type))
13591     {
13592     case RECORD_IS_STRUCT:
13593       return DW_TAG_structure_type;
13594
13595     case RECORD_IS_CLASS:
13596       return DW_TAG_class_type;
13597
13598     case RECORD_IS_INTERFACE:
13599       return DW_TAG_interface_type;
13600
13601     default:
13602       gcc_unreachable ();
13603     }
13604 }
13605
13606 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13607    include all of the information about the enumeration values also. Each
13608    enumerated type name/value is listed as a child of the enumerated type
13609    DIE.  */
13610
13611 static dw_die_ref
13612 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13613 {
13614   dw_die_ref type_die = lookup_type_die (type);
13615
13616   if (type_die == NULL)
13617     {
13618       type_die = new_die (DW_TAG_enumeration_type,
13619                           scope_die_for (type, context_die), type);
13620       equate_type_number_to_die (type, type_die);
13621       add_name_attribute (type_die, type_tag (type));
13622     }
13623   else if (! TYPE_SIZE (type))
13624     return type_die;
13625   else
13626     remove_AT (type_die, DW_AT_declaration);
13627
13628   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13629      given enum type is incomplete, do not generate the DW_AT_byte_size
13630      attribute or the DW_AT_element_list attribute.  */
13631   if (TYPE_SIZE (type))
13632     {
13633       tree link;
13634
13635       TREE_ASM_WRITTEN (type) = 1;
13636       add_byte_size_attribute (type_die, type);
13637       if (TYPE_STUB_DECL (type) != NULL_TREE)
13638         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13639
13640       /* If the first reference to this type was as the return type of an
13641          inline function, then it may not have a parent.  Fix this now.  */
13642       if (type_die->die_parent == NULL)
13643         add_child_die (scope_die_for (type, context_die), type_die);
13644
13645       for (link = TYPE_VALUES (type);
13646            link != NULL; link = TREE_CHAIN (link))
13647         {
13648           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13649           tree value = TREE_VALUE (link);
13650
13651           add_name_attribute (enum_die,
13652                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13653
13654           if (TREE_CODE (value) == CONST_DECL)
13655             value = DECL_INITIAL (value);
13656
13657           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13658             /* DWARF2 does not provide a way of indicating whether or
13659                not enumeration constants are signed or unsigned.  GDB
13660                always assumes the values are signed, so we output all
13661                values as if they were signed.  That means that
13662                enumeration constants with very large unsigned values
13663                will appear to have negative values in the debugger.  */
13664             add_AT_int (enum_die, DW_AT_const_value,
13665                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13666         }
13667     }
13668   else
13669     add_AT_flag (type_die, DW_AT_declaration, 1);
13670
13671   if (get_AT (type_die, DW_AT_name))
13672     add_pubtype (type, type_die);
13673
13674   return type_die;
13675 }
13676
13677 /* Generate a DIE to represent either a real live formal parameter decl or to
13678    represent just the type of some formal parameter position in some function
13679    type.
13680
13681    Note that this routine is a bit unusual because its argument may be a
13682    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13683    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13684    node.  If it's the former then this function is being called to output a
13685    DIE to represent a formal parameter object (or some inlining thereof).  If
13686    it's the latter, then this function is only being called to output a
13687    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13688    argument type of some subprogram type.  */
13689
13690 static dw_die_ref
13691 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13692 {
13693   tree node_or_origin = node ? node : origin;
13694   dw_die_ref parm_die
13695     = new_die (DW_TAG_formal_parameter, context_die, node);
13696
13697   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13698     {
13699     case tcc_declaration:
13700       if (!origin)
13701         origin = decl_ultimate_origin (node);
13702       if (origin != NULL)
13703         add_abstract_origin_attribute (parm_die, origin);
13704       else
13705         {
13706           tree type = TREE_TYPE (node);
13707           add_name_and_src_coords_attributes (parm_die, node);
13708           if (DECL_BY_REFERENCE (node))
13709             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13710                                 context_die);
13711           else
13712             add_type_attribute (parm_die, type,
13713                                 TREE_READONLY (node),
13714                                 TREE_THIS_VOLATILE (node),
13715                                 context_die);
13716           if (DECL_ARTIFICIAL (node))
13717             add_AT_flag (parm_die, DW_AT_artificial, 1);
13718         }
13719
13720       if (node)
13721         equate_decl_number_to_die (node, parm_die);
13722       if (! DECL_ABSTRACT (node_or_origin))
13723         add_location_or_const_value_attribute (parm_die, node_or_origin,
13724                                                DW_AT_location);
13725
13726       break;
13727
13728     case tcc_type:
13729       /* We were called with some kind of a ..._TYPE node.  */
13730       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13731       break;
13732
13733     default:
13734       gcc_unreachable ();
13735     }
13736
13737   return parm_die;
13738 }
13739
13740 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13741    at the end of an (ANSI prototyped) formal parameters list.  */
13742
13743 static void
13744 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13745 {
13746   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13747 }
13748
13749 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13750    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13751    parameters as specified in some function type specification (except for
13752    those which appear as part of a function *definition*).  */
13753
13754 static void
13755 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13756 {
13757   tree link;
13758   tree formal_type = NULL;
13759   tree first_parm_type;
13760   tree arg;
13761
13762   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13763     {
13764       arg = DECL_ARGUMENTS (function_or_method_type);
13765       function_or_method_type = TREE_TYPE (function_or_method_type);
13766     }
13767   else
13768     arg = NULL_TREE;
13769
13770   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13771
13772   /* Make our first pass over the list of formal parameter types and output a
13773      DW_TAG_formal_parameter DIE for each one.  */
13774   for (link = first_parm_type; link; )
13775     {
13776       dw_die_ref parm_die;
13777
13778       formal_type = TREE_VALUE (link);
13779       if (formal_type == void_type_node)
13780         break;
13781
13782       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13783       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13784       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13785            && link == first_parm_type)
13786           || (arg && DECL_ARTIFICIAL (arg)))
13787         add_AT_flag (parm_die, DW_AT_artificial, 1);
13788
13789       link = TREE_CHAIN (link);
13790       if (arg)
13791         arg = TREE_CHAIN (arg);
13792     }
13793
13794   /* If this function type has an ellipsis, add a
13795      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13796   if (formal_type != void_type_node)
13797     gen_unspecified_parameters_die (function_or_method_type, context_die);
13798
13799   /* Make our second (and final) pass over the list of formal parameter types
13800      and output DIEs to represent those types (as necessary).  */
13801   for (link = TYPE_ARG_TYPES (function_or_method_type);
13802        link && TREE_VALUE (link);
13803        link = TREE_CHAIN (link))
13804     gen_type_die (TREE_VALUE (link), context_die);
13805 }
13806
13807 /* We want to generate the DIE for TYPE so that we can generate the
13808    die for MEMBER, which has been defined; we will need to refer back
13809    to the member declaration nested within TYPE.  If we're trying to
13810    generate minimal debug info for TYPE, processing TYPE won't do the
13811    trick; we need to attach the member declaration by hand.  */
13812
13813 static void
13814 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13815 {
13816   gen_type_die (type, context_die);
13817
13818   /* If we're trying to avoid duplicate debug info, we may not have
13819      emitted the member decl for this function.  Emit it now.  */
13820   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13821       && ! lookup_decl_die (member))
13822     {
13823       dw_die_ref type_die;
13824       gcc_assert (!decl_ultimate_origin (member));
13825
13826       push_decl_scope (type);
13827       type_die = lookup_type_die (type);
13828       if (TREE_CODE (member) == FUNCTION_DECL)
13829         gen_subprogram_die (member, type_die);
13830       else if (TREE_CODE (member) == FIELD_DECL)
13831         {
13832           /* Ignore the nameless fields that are used to skip bits but handle
13833              C++ anonymous unions and structs.  */
13834           if (DECL_NAME (member) != NULL_TREE
13835               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13836               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13837             {
13838               gen_type_die (member_declared_type (member), type_die);
13839               gen_field_die (member, type_die);
13840             }
13841         }
13842       else
13843         gen_variable_die (member, NULL_TREE, type_die);
13844
13845       pop_decl_scope ();
13846     }
13847 }
13848
13849 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13850    may later generate inlined and/or out-of-line instances of.  */
13851
13852 static void
13853 dwarf2out_abstract_function (tree decl)
13854 {
13855   dw_die_ref old_die;
13856   tree save_fn;
13857   tree context;
13858   int was_abstract = DECL_ABSTRACT (decl);
13859
13860   /* Make sure we have the actual abstract inline, not a clone.  */
13861   decl = DECL_ORIGIN (decl);
13862   htab_empty (decl_loc_table);
13863
13864   old_die = lookup_decl_die (decl);
13865   if (old_die && get_AT (old_die, DW_AT_inline))
13866     /* We've already generated the abstract instance.  */
13867     return;
13868
13869   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13870      we don't get confused by DECL_ABSTRACT.  */
13871   if (debug_info_level > DINFO_LEVEL_TERSE)
13872     {
13873       context = decl_class_context (decl);
13874       if (context)
13875         gen_type_die_for_member
13876           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13877     }
13878
13879   /* Pretend we've just finished compiling this function.  */
13880   save_fn = current_function_decl;
13881   current_function_decl = decl;
13882   push_cfun (DECL_STRUCT_FUNCTION (decl));
13883
13884   set_decl_abstract_flags (decl, 1);
13885   dwarf2out_decl (decl);
13886   if (! was_abstract)
13887     set_decl_abstract_flags (decl, 0);
13888
13889   current_function_decl = save_fn;
13890   pop_cfun ();
13891 }
13892
13893 /* Helper function of premark_used_types() which gets called through
13894    htab_traverse_resize().
13895
13896    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13897    marked as unused by prune_unused_types.  */
13898 static int
13899 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13900 {
13901   tree type;
13902   dw_die_ref die;
13903
13904   type = (tree) *slot;
13905   die = lookup_type_die (type);
13906   if (die != NULL)
13907     die->die_perennial_p = 1;
13908   return 1;
13909 }
13910
13911 /* Mark all members of used_types_hash as perennial.  */
13912 static void
13913 premark_used_types (void)
13914 {
13915   if (cfun && cfun->used_types_hash)
13916     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13917 }
13918
13919 /* Generate a DIE to represent a declared function (either file-scope or
13920    block-local).  */
13921
13922 static void
13923 gen_subprogram_die (tree decl, dw_die_ref context_die)
13924 {
13925   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13926   tree origin = decl_ultimate_origin (decl);
13927   dw_die_ref subr_die;
13928   tree fn_arg_types;
13929   tree outer_scope;
13930   dw_die_ref old_die = lookup_decl_die (decl);
13931   int declaration = (current_function_decl != decl
13932                      || class_or_namespace_scope_p (context_die));
13933
13934   premark_used_types ();
13935
13936   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13937      started to generate the abstract instance of an inline, decided to output
13938      its containing class, and proceeded to emit the declaration of the inline
13939      from the member list for the class.  If so, DECLARATION takes priority;
13940      we'll get back to the abstract instance when done with the class.  */
13941
13942   /* The class-scope declaration DIE must be the primary DIE.  */
13943   if (origin && declaration && class_or_namespace_scope_p (context_die))
13944     {
13945       origin = NULL;
13946       gcc_assert (!old_die);
13947     }
13948
13949   /* Now that the C++ front end lazily declares artificial member fns, we
13950      might need to retrofit the declaration into its class.  */
13951   if (!declaration && !origin && !old_die
13952       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13953       && !class_or_namespace_scope_p (context_die)
13954       && debug_info_level > DINFO_LEVEL_TERSE)
13955     old_die = force_decl_die (decl);
13956
13957   if (origin != NULL)
13958     {
13959       gcc_assert (!declaration || local_scope_p (context_die));
13960
13961       /* Fixup die_parent for the abstract instance of a nested
13962          inline function.  */
13963       if (old_die && old_die->die_parent == NULL)
13964         add_child_die (context_die, old_die);
13965
13966       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13967       add_abstract_origin_attribute (subr_die, origin);
13968     }
13969   else if (old_die)
13970     {
13971       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13972       struct dwarf_file_data * file_index = lookup_filename (s.file);
13973
13974       if (!get_AT_flag (old_die, DW_AT_declaration)
13975           /* We can have a normal definition following an inline one in the
13976              case of redefinition of GNU C extern inlines.
13977              It seems reasonable to use AT_specification in this case.  */
13978           && !get_AT (old_die, DW_AT_inline))
13979         {
13980           /* Detect and ignore this case, where we are trying to output
13981              something we have already output.  */
13982           return;
13983         }
13984
13985       /* If the definition comes from the same place as the declaration,
13986          maybe use the old DIE.  We always want the DIE for this function
13987          that has the *_pc attributes to be under comp_unit_die so the
13988          debugger can find it.  We also need to do this for abstract
13989          instances of inlines, since the spec requires the out-of-line copy
13990          to have the same parent.  For local class methods, this doesn't
13991          apply; we just use the old DIE.  */
13992       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13993           && (DECL_ARTIFICIAL (decl)
13994               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13995                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13996                       == (unsigned) s.line))))
13997         {
13998           subr_die = old_die;
13999
14000           /* Clear out the declaration attribute and the formal parameters.
14001              Do not remove all children, because it is possible that this
14002              declaration die was forced using force_decl_die(). In such
14003              cases die that forced declaration die (e.g. TAG_imported_module)
14004              is one of the children that we do not want to remove.  */
14005           remove_AT (subr_die, DW_AT_declaration);
14006           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
14007         }
14008       else
14009         {
14010           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14011           add_AT_specification (subr_die, old_die);
14012           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14013             add_AT_file (subr_die, DW_AT_decl_file, file_index);
14014           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14015             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
14016         }
14017     }
14018   else
14019     {
14020       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
14021
14022       if (TREE_PUBLIC (decl))
14023         add_AT_flag (subr_die, DW_AT_external, 1);
14024
14025       add_name_and_src_coords_attributes (subr_die, decl);
14026       if (debug_info_level > DINFO_LEVEL_TERSE)
14027         {
14028           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
14029           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
14030                               0, 0, context_die);
14031         }
14032
14033       add_pure_or_virtual_attribute (subr_die, decl);
14034       if (DECL_ARTIFICIAL (decl))
14035         add_AT_flag (subr_die, DW_AT_artificial, 1);
14036
14037       if (TREE_PROTECTED (decl))
14038         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
14039       else if (TREE_PRIVATE (decl))
14040         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
14041     }
14042
14043   if (declaration)
14044     {
14045       if (!old_die || !get_AT (old_die, DW_AT_inline))
14046         {
14047           add_AT_flag (subr_die, DW_AT_declaration, 1);
14048
14049           /* If this is an explicit function declaration then generate
14050              a DW_AT_explicit attribute.  */
14051           if (lang_hooks.decls.function_decl_explicit_p (decl))
14052             add_AT_flag (subr_die, DW_AT_explicit, 1);
14053
14054           /* The first time we see a member function, it is in the context of
14055              the class to which it belongs.  We make sure of this by emitting
14056              the class first.  The next time is the definition, which is
14057              handled above.  The two may come from the same source text.
14058
14059              Note that force_decl_die() forces function declaration die. It is
14060              later reused to represent definition.  */
14061           equate_decl_number_to_die (decl, subr_die);
14062         }
14063     }
14064   else if (DECL_ABSTRACT (decl))
14065     {
14066       if (DECL_DECLARED_INLINE_P (decl))
14067         {
14068           if (cgraph_function_possibly_inlined_p (decl))
14069             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14070           else
14071             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14072         }
14073       else
14074         {
14075           if (cgraph_function_possibly_inlined_p (decl))
14076             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14077           else
14078             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14079         }
14080
14081       if (DECL_DECLARED_INLINE_P (decl)
14082           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14083         add_AT_flag (subr_die, DW_AT_artificial, 1);
14084
14085       equate_decl_number_to_die (decl, subr_die);
14086     }
14087   else if (!DECL_EXTERNAL (decl))
14088     {
14089       HOST_WIDE_INT cfa_fb_offset;
14090
14091       if (!old_die || !get_AT (old_die, DW_AT_inline))
14092         equate_decl_number_to_die (decl, subr_die);
14093
14094       if (!flag_reorder_blocks_and_partition)
14095         {
14096           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14097                                        current_function_funcdef_no);
14098           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14099           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14100                                        current_function_funcdef_no);
14101           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14102
14103           add_pubname (decl, subr_die);
14104           add_arange (decl, subr_die);
14105         }
14106       else
14107         {  /* Do nothing for now; maybe need to duplicate die, one for
14108               hot section and one for cold section, then use the hot/cold
14109               section begin/end labels to generate the aranges...  */
14110           /*
14111             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14112             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14113             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14114             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14115
14116             add_pubname (decl, subr_die);
14117             add_arange (decl, subr_die);
14118             add_arange (decl, subr_die);
14119            */
14120         }
14121
14122 #ifdef MIPS_DEBUGGING_INFO
14123       /* Add a reference to the FDE for this routine.  */
14124       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14125 #endif
14126
14127       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14128
14129       /* We define the "frame base" as the function's CFA.  This is more
14130          convenient for several reasons: (1) It's stable across the prologue
14131          and epilogue, which makes it better than just a frame pointer,
14132          (2) With dwarf3, there exists a one-byte encoding that allows us
14133          to reference the .debug_frame data by proxy, but failing that,
14134          (3) We can at least reuse the code inspection and interpretation
14135          code that determines the CFA position at various points in the
14136          function.  */
14137       if (dwarf_version >= 3)
14138         {
14139           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
14140           add_AT_loc (subr_die, DW_AT_frame_base, op);
14141         }
14142       else
14143         {
14144           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14145           if (list->dw_loc_next)
14146             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14147           else
14148             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14149         }
14150
14151       /* Compute a displacement from the "steady-state frame pointer" to
14152          the CFA.  The former is what all stack slots and argument slots
14153          will reference in the rtl; the later is what we've told the
14154          debugger about.  We'll need to adjust all frame_base references
14155          by this displacement.  */
14156       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14157
14158       if (cfun->static_chain_decl)
14159         add_AT_location_description (subr_die, DW_AT_static_link,
14160                  loc_descriptor_from_tree (cfun->static_chain_decl));
14161     }
14162
14163   /* Now output descriptions of the arguments for this function. This gets
14164      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14165      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14166      `...' at the end of the formal parameter list.  In order to find out if
14167      there was a trailing ellipsis or not, we must instead look at the type
14168      associated with the FUNCTION_DECL.  This will be a node of type
14169      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14170      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14171      an ellipsis at the end.  */
14172
14173   /* In the case where we are describing a mere function declaration, all we
14174      need to do here (and all we *can* do here) is to describe the *types* of
14175      its formal parameters.  */
14176   if (debug_info_level <= DINFO_LEVEL_TERSE)
14177     ;
14178   else if (declaration)
14179     gen_formal_types_die (decl, subr_die);
14180   else
14181     {
14182       /* Generate DIEs to represent all known formal parameters.  */
14183       tree arg_decls = DECL_ARGUMENTS (decl);
14184       tree parm;
14185
14186       /* When generating DIEs, generate the unspecified_parameters DIE
14187          instead if we come across the arg "__builtin_va_alist" */
14188       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14189         if (TREE_CODE (parm) == PARM_DECL)
14190           {
14191             if (DECL_NAME (parm)
14192                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14193                             "__builtin_va_alist"))
14194               gen_unspecified_parameters_die (parm, subr_die);
14195             else
14196               gen_decl_die (parm, NULL, subr_die);
14197           }
14198
14199       /* Decide whether we need an unspecified_parameters DIE at the end.
14200          There are 2 more cases to do this for: 1) the ansi ... declaration -
14201          this is detectable when the end of the arg list is not a
14202          void_type_node 2) an unprototyped function declaration (not a
14203          definition).  This just means that we have no info about the
14204          parameters at all.  */
14205       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14206       if (fn_arg_types != NULL)
14207         {
14208           /* This is the prototyped case, check for....  */
14209           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14210             gen_unspecified_parameters_die (decl, subr_die);
14211         }
14212       else if (DECL_INITIAL (decl) == NULL_TREE)
14213         gen_unspecified_parameters_die (decl, subr_die);
14214     }
14215
14216   /* Output Dwarf info for all of the stuff within the body of the function
14217      (if it has one - it may be just a declaration).  */
14218   outer_scope = DECL_INITIAL (decl);
14219
14220   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14221      a function.  This BLOCK actually represents the outermost binding contour
14222      for the function, i.e. the contour in which the function's formal
14223      parameters and labels get declared. Curiously, it appears that the front
14224      end doesn't actually put the PARM_DECL nodes for the current function onto
14225      the BLOCK_VARS list for this outer scope, but are strung off of the
14226      DECL_ARGUMENTS list for the function instead.
14227
14228      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14229      the LABEL_DECL nodes for the function however, and we output DWARF info
14230      for those in decls_for_scope.  Just within the `outer_scope' there will be
14231      a BLOCK node representing the function's outermost pair of curly braces,
14232      and any blocks used for the base and member initializers of a C++
14233      constructor function.  */
14234   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14235     {
14236       /* Emit a DW_TAG_variable DIE for a named return value.  */
14237       if (DECL_NAME (DECL_RESULT (decl)))
14238         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14239
14240       current_function_has_inlines = 0;
14241       decls_for_scope (outer_scope, subr_die, 0);
14242
14243 #if 0 && defined (MIPS_DEBUGGING_INFO)
14244       if (current_function_has_inlines)
14245         {
14246           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14247           if (! comp_unit_has_inlines)
14248             {
14249               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14250               comp_unit_has_inlines = 1;
14251             }
14252         }
14253 #endif
14254     }
14255   /* Add the calling convention attribute if requested.  */
14256   add_calling_convention_attribute (subr_die, decl);
14257
14258 }
14259
14260 /* Returns a hash value for X (which really is a die_struct).  */
14261
14262 static hashval_t
14263 common_block_die_table_hash (const void *x)
14264 {
14265   const_dw_die_ref d = (const_dw_die_ref) x;
14266   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14267 }
14268
14269 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14270    as decl_id and die_parent of die_struct Y.  */
14271
14272 static int
14273 common_block_die_table_eq (const void *x, const void *y)
14274 {
14275   const_dw_die_ref d = (const_dw_die_ref) x;
14276   const_dw_die_ref e = (const_dw_die_ref) y;
14277   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14278 }
14279
14280 /* Generate a DIE to represent a declared data object.
14281    Either DECL or ORIGIN must be non-null.  */
14282
14283 static void
14284 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14285 {
14286   HOST_WIDE_INT off;
14287   tree com_decl;
14288   tree decl_or_origin = decl ? decl : origin;
14289   dw_die_ref var_die;
14290   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14291   dw_die_ref origin_die;
14292   int declaration = (DECL_EXTERNAL (decl_or_origin)
14293                      /* If DECL is COMDAT and has not actually been
14294                         emitted, we cannot take its address; there
14295                         might end up being no definition anywhere in
14296                         the program.  For example, consider the C++
14297                         test case:
14298
14299                           template <class T>
14300                           struct S { static const int i = 7; };
14301
14302                           template <class T>
14303                           const int S<T>::i;
14304
14305                           int f() { return S<int>::i; }
14306
14307                         Here, S<int>::i is not DECL_EXTERNAL, but no
14308                         definition is required, so the compiler will
14309                         not emit a definition.  */
14310                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14311                          && DECL_COMDAT (decl_or_origin)
14312                          && !TREE_ASM_WRITTEN (decl_or_origin))
14313                      || class_or_namespace_scope_p (context_die));
14314
14315   if (!origin)
14316     origin = decl_ultimate_origin (decl);
14317
14318   com_decl = fortran_common (decl_or_origin, &off);
14319
14320   /* Symbol in common gets emitted as a child of the common block, in the form
14321      of a data member.  */
14322   if (com_decl)
14323     {
14324       tree field;
14325       dw_die_ref com_die;
14326       dw_loc_descr_ref loc;
14327       die_node com_die_arg;
14328
14329       var_die = lookup_decl_die (decl_or_origin);
14330       if (var_die)
14331         {
14332           if (get_AT (var_die, DW_AT_location) == NULL)
14333             {
14334               loc = loc_descriptor_from_tree (com_decl);
14335               if (loc)
14336                 {
14337                   if (off)
14338                     {
14339                       /* Optimize the common case.  */
14340                       if (loc->dw_loc_opc == DW_OP_addr
14341                           && loc->dw_loc_next == NULL
14342                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14343                              == SYMBOL_REF)
14344                         loc->dw_loc_oprnd1.v.val_addr
14345                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14346                         else
14347                           loc_descr_plus_const (&loc, off);
14348                     }
14349                   add_AT_loc (var_die, DW_AT_location, loc);
14350                   remove_AT (var_die, DW_AT_declaration);
14351                 }
14352             }
14353           return;
14354         }
14355
14356       if (common_block_die_table == NULL)
14357         common_block_die_table
14358           = htab_create_ggc (10, common_block_die_table_hash,
14359                              common_block_die_table_eq, NULL);
14360
14361       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14362       com_die_arg.decl_id = DECL_UID (com_decl);
14363       com_die_arg.die_parent = context_die;
14364       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14365       loc = loc_descriptor_from_tree (com_decl);
14366       if (com_die == NULL)
14367         {
14368           const char *cnam
14369             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14370           void **slot;
14371
14372           com_die = new_die (DW_TAG_common_block, context_die, decl);
14373           add_name_and_src_coords_attributes (com_die, com_decl);
14374           if (loc)
14375             {
14376               add_AT_loc (com_die, DW_AT_location, loc);
14377               /* Avoid sharing the same loc descriptor between
14378                  DW_TAG_common_block and DW_TAG_variable.  */
14379               loc = loc_descriptor_from_tree (com_decl);
14380             }
14381           else if (DECL_EXTERNAL (decl))
14382             add_AT_flag (com_die, DW_AT_declaration, 1);
14383           add_pubname_string (cnam, com_die); /* ??? needed? */
14384           com_die->decl_id = DECL_UID (com_decl);
14385           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14386           *slot = (void *) com_die;
14387         }
14388       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14389         {
14390           add_AT_loc (com_die, DW_AT_location, loc);
14391           loc = loc_descriptor_from_tree (com_decl);
14392           remove_AT (com_die, DW_AT_declaration);
14393         }
14394       var_die = new_die (DW_TAG_variable, com_die, decl);
14395       add_name_and_src_coords_attributes (var_die, decl);
14396       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14397                           TREE_THIS_VOLATILE (decl), context_die);
14398       add_AT_flag (var_die, DW_AT_external, 1);
14399       if (loc)
14400         {
14401           if (off)
14402             {
14403               /* Optimize the common case.  */
14404               if (loc->dw_loc_opc == DW_OP_addr
14405                   && loc->dw_loc_next == NULL
14406                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14407                 loc->dw_loc_oprnd1.v.val_addr
14408                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14409               else
14410                 loc_descr_plus_const (&loc, off);
14411             }
14412           add_AT_loc (var_die, DW_AT_location, loc);
14413         }
14414       else if (DECL_EXTERNAL (decl))
14415         add_AT_flag (var_die, DW_AT_declaration, 1);
14416       equate_decl_number_to_die (decl, var_die);
14417       return;
14418     }
14419
14420   /* If the compiler emitted a definition for the DECL declaration
14421      and if we already emitted a DIE for it, don't emit a second
14422      DIE for it again.  */
14423   if (old_die
14424       && declaration
14425       && old_die->die_parent == context_die)
14426     return;
14427
14428   /* For static data members, the declaration in the class is supposed
14429      to have DW_TAG_member tag; the specification should still be
14430      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14431   if (declaration && class_scope_p (context_die))
14432     var_die = new_die (DW_TAG_member, context_die, decl);
14433   else
14434     var_die = new_die (DW_TAG_variable, context_die, decl);
14435
14436   origin_die = NULL;
14437   if (origin != NULL)
14438     origin_die = add_abstract_origin_attribute (var_die, origin);
14439
14440   /* Loop unrolling can create multiple blocks that refer to the same
14441      static variable, so we must test for the DW_AT_declaration flag.
14442
14443      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14444      copy decls and set the DECL_ABSTRACT flag on them instead of
14445      sharing them.
14446
14447      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14448
14449      ??? The declare_in_namespace support causes us to get two DIEs for one
14450      variable, both of which are declarations.  We want to avoid considering
14451      one to be a specification, so we must test that this DIE is not a
14452      declaration.  */
14453   else if (old_die && TREE_STATIC (decl) && ! declaration
14454            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14455     {
14456       /* This is a definition of a C++ class level static.  */
14457       add_AT_specification (var_die, old_die);
14458       if (DECL_NAME (decl))
14459         {
14460           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14461           struct dwarf_file_data * file_index = lookup_filename (s.file);
14462
14463           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14464             add_AT_file (var_die, DW_AT_decl_file, file_index);
14465
14466           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14467             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14468         }
14469     }
14470   else
14471     {
14472       tree type = TREE_TYPE (decl);
14473
14474       add_name_and_src_coords_attributes (var_die, decl);
14475       if ((TREE_CODE (decl) == PARM_DECL
14476            || TREE_CODE (decl) == RESULT_DECL
14477            || TREE_CODE (decl) == VAR_DECL)
14478           && DECL_BY_REFERENCE (decl))
14479         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14480       else
14481         add_type_attribute (var_die, type, TREE_READONLY (decl),
14482                             TREE_THIS_VOLATILE (decl), context_die);
14483
14484       if (TREE_PUBLIC (decl))
14485         add_AT_flag (var_die, DW_AT_external, 1);
14486
14487       if (DECL_ARTIFICIAL (decl))
14488         add_AT_flag (var_die, DW_AT_artificial, 1);
14489
14490       if (TREE_PROTECTED (decl))
14491         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14492       else if (TREE_PRIVATE (decl))
14493         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14494     }
14495
14496   if (declaration)
14497     add_AT_flag (var_die, DW_AT_declaration, 1);
14498
14499   if (decl && (DECL_ABSTRACT (decl) || declaration))
14500     equate_decl_number_to_die (decl, var_die);
14501
14502   if (! declaration
14503       && (! DECL_ABSTRACT (decl_or_origin)
14504           /* Local static vars are shared between all clones/inlines,
14505              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14506              already set.  */
14507           || (TREE_CODE (decl_or_origin) == VAR_DECL
14508               && TREE_STATIC (decl_or_origin)
14509               && DECL_RTL_SET_P (decl_or_origin)))
14510       /* When abstract origin already has DW_AT_location attribute, no need
14511          to add it again.  */
14512       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14513     {
14514       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14515           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14516         defer_location (decl_or_origin, var_die);
14517       else
14518         add_location_or_const_value_attribute (var_die,
14519                                                decl_or_origin,
14520                                                DW_AT_location);
14521       add_pubname (decl_or_origin, var_die);
14522     }
14523   else
14524     tree_add_const_value_attribute (var_die, decl_or_origin);
14525 }
14526
14527 /* Generate a DIE to represent a named constant.  */
14528
14529 static void
14530 gen_const_die (tree decl, dw_die_ref context_die)
14531 {
14532   dw_die_ref const_die;
14533   tree type = TREE_TYPE (decl);
14534
14535   const_die = new_die (DW_TAG_constant, context_die, decl);
14536   add_name_and_src_coords_attributes (const_die, decl);
14537   add_type_attribute (const_die, type, 1, 0, context_die);
14538   if (TREE_PUBLIC (decl))
14539     add_AT_flag (const_die, DW_AT_external, 1);
14540   if (DECL_ARTIFICIAL (decl))
14541     add_AT_flag (const_die, DW_AT_artificial, 1);
14542   tree_add_const_value_attribute (const_die, decl);
14543 }
14544
14545 /* Generate a DIE to represent a label identifier.  */
14546
14547 static void
14548 gen_label_die (tree decl, dw_die_ref context_die)
14549 {
14550   tree origin = decl_ultimate_origin (decl);
14551   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14552   rtx insn;
14553   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14554
14555   if (origin != NULL)
14556     add_abstract_origin_attribute (lbl_die, origin);
14557   else
14558     add_name_and_src_coords_attributes (lbl_die, decl);
14559
14560   if (DECL_ABSTRACT (decl))
14561     equate_decl_number_to_die (decl, lbl_die);
14562   else
14563     {
14564       insn = DECL_RTL_IF_SET (decl);
14565
14566       /* Deleted labels are programmer specified labels which have been
14567          eliminated because of various optimizations.  We still emit them
14568          here so that it is possible to put breakpoints on them.  */
14569       if (insn
14570           && (LABEL_P (insn)
14571               || ((NOTE_P (insn)
14572                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14573         {
14574           /* When optimization is enabled (via -O) some parts of the compiler
14575              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14576              represent source-level labels which were explicitly declared by
14577              the user.  This really shouldn't be happening though, so catch
14578              it if it ever does happen.  */
14579           gcc_assert (!INSN_DELETED_P (insn));
14580
14581           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14582           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14583         }
14584     }
14585 }
14586
14587 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14588    attributes to the DIE for a block STMT, to describe where the inlined
14589    function was called from.  This is similar to add_src_coords_attributes.  */
14590
14591 static inline void
14592 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14593 {
14594   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14595
14596   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14597   add_AT_unsigned (die, DW_AT_call_line, s.line);
14598 }
14599
14600
14601 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14602    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14603
14604 static inline void
14605 add_high_low_attributes (tree stmt, dw_die_ref die)
14606 {
14607   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14608
14609   if (BLOCK_FRAGMENT_CHAIN (stmt))
14610     {
14611       tree chain;
14612
14613       if (inlined_function_outer_scope_p (stmt))
14614         {
14615           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14616                                        BLOCK_NUMBER (stmt));
14617           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14618         }
14619
14620       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14621
14622       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14623       do
14624         {
14625           add_ranges (chain);
14626           chain = BLOCK_FRAGMENT_CHAIN (chain);
14627         }
14628       while (chain);
14629       add_ranges (NULL);
14630     }
14631   else
14632     {
14633       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14634                                    BLOCK_NUMBER (stmt));
14635       add_AT_lbl_id (die, DW_AT_low_pc, label);
14636       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14637                                    BLOCK_NUMBER (stmt));
14638       add_AT_lbl_id (die, DW_AT_high_pc, label);
14639     }
14640 }
14641
14642 /* Generate a DIE for a lexical block.  */
14643
14644 static void
14645 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14646 {
14647   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14648
14649   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14650     add_high_low_attributes (stmt, stmt_die);
14651
14652   decls_for_scope (stmt, stmt_die, depth);
14653 }
14654
14655 /* Generate a DIE for an inlined subprogram.  */
14656
14657 static void
14658 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14659 {
14660   tree decl = block_ultimate_origin (stmt);
14661
14662   /* Emit info for the abstract instance first, if we haven't yet.  We
14663      must emit this even if the block is abstract, otherwise when we
14664      emit the block below (or elsewhere), we may end up trying to emit
14665      a die whose origin die hasn't been emitted, and crashing.  */
14666   dwarf2out_abstract_function (decl);
14667
14668   if (! BLOCK_ABSTRACT (stmt))
14669     {
14670       dw_die_ref subr_die
14671         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14672
14673       add_abstract_origin_attribute (subr_die, decl);
14674       if (TREE_ASM_WRITTEN (stmt))
14675         add_high_low_attributes (stmt, subr_die);
14676       add_call_src_coords_attributes (stmt, subr_die);
14677
14678       decls_for_scope (stmt, subr_die, depth);
14679       current_function_has_inlines = 1;
14680     }
14681   else
14682     /* We may get here if we're the outer block of function A that was
14683        inlined into function B that was inlined into function C.  When
14684        generating debugging info for C, dwarf2out_abstract_function(B)
14685        would mark all inlined blocks as abstract, including this one.
14686        So, we wouldn't (and shouldn't) expect labels to be generated
14687        for this one.  Instead, just emit debugging info for
14688        declarations within the block.  This is particularly important
14689        in the case of initializers of arguments passed from B to us:
14690        if they're statement expressions containing declarations, we
14691        wouldn't generate dies for their abstract variables, and then,
14692        when generating dies for the real variables, we'd die (pun
14693        intended :-)  */
14694     gen_lexical_block_die (stmt, context_die, depth);
14695 }
14696
14697 /* Generate a DIE for a field in a record, or structure.  */
14698
14699 static void
14700 gen_field_die (tree decl, dw_die_ref context_die)
14701 {
14702   dw_die_ref decl_die;
14703
14704   if (TREE_TYPE (decl) == error_mark_node)
14705     return;
14706
14707   decl_die = new_die (DW_TAG_member, context_die, decl);
14708   add_name_and_src_coords_attributes (decl_die, decl);
14709   add_type_attribute (decl_die, member_declared_type (decl),
14710                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14711                       context_die);
14712
14713   if (DECL_BIT_FIELD_TYPE (decl))
14714     {
14715       add_byte_size_attribute (decl_die, decl);
14716       add_bit_size_attribute (decl_die, decl);
14717       add_bit_offset_attribute (decl_die, decl);
14718     }
14719
14720   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14721     add_data_member_location_attribute (decl_die, decl);
14722
14723   if (DECL_ARTIFICIAL (decl))
14724     add_AT_flag (decl_die, DW_AT_artificial, 1);
14725
14726   if (TREE_PROTECTED (decl))
14727     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14728   else if (TREE_PRIVATE (decl))
14729     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14730
14731   /* Equate decl number to die, so that we can look up this decl later on.  */
14732   equate_decl_number_to_die (decl, decl_die);
14733 }
14734
14735 #if 0
14736 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14737    Use modified_type_die instead.
14738    We keep this code here just in case these types of DIEs may be needed to
14739    represent certain things in other languages (e.g. Pascal) someday.  */
14740
14741 static void
14742 gen_pointer_type_die (tree type, dw_die_ref context_die)
14743 {
14744   dw_die_ref ptr_die
14745     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14746
14747   equate_type_number_to_die (type, ptr_die);
14748   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14749   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14750 }
14751
14752 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14753    Use modified_type_die instead.
14754    We keep this code here just in case these types of DIEs may be needed to
14755    represent certain things in other languages (e.g. Pascal) someday.  */
14756
14757 static void
14758 gen_reference_type_die (tree type, dw_die_ref context_die)
14759 {
14760   dw_die_ref ref_die
14761     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14762
14763   equate_type_number_to_die (type, ref_die);
14764   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14765   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14766 }
14767 #endif
14768
14769 /* Generate a DIE for a pointer to a member type.  */
14770
14771 static void
14772 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14773 {
14774   dw_die_ref ptr_die
14775     = new_die (DW_TAG_ptr_to_member_type,
14776                scope_die_for (type, context_die), type);
14777
14778   equate_type_number_to_die (type, ptr_die);
14779   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14780                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14781   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14782 }
14783
14784 /* Generate the DIE for the compilation unit.  */
14785
14786 static dw_die_ref
14787 gen_compile_unit_die (const char *filename)
14788 {
14789   dw_die_ref die;
14790   char producer[250];
14791   const char *language_string = lang_hooks.name;
14792   int language;
14793
14794   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14795
14796   if (filename)
14797     {
14798       add_name_attribute (die, filename);
14799       /* Don't add cwd for <built-in>.  */
14800       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14801         add_comp_dir_attribute (die);
14802     }
14803
14804   sprintf (producer, "%s %s", language_string, version_string);
14805
14806 #ifdef MIPS_DEBUGGING_INFO
14807   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14808      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14809      not appear in the producer string, the debugger reaches the conclusion
14810      that the object file is stripped and has no debugging information.
14811      To get the MIPS/SGI debugger to believe that there is debugging
14812      information in the object file, we add a -g to the producer string.  */
14813   if (debug_info_level > DINFO_LEVEL_TERSE)
14814     strcat (producer, " -g");
14815 #endif
14816
14817   add_AT_string (die, DW_AT_producer, producer);
14818
14819   if (strcmp (language_string, "GNU C++") == 0)
14820     language = DW_LANG_C_plus_plus;
14821   else if (strcmp (language_string, "GNU Ada") == 0)
14822     language = DW_LANG_Ada95;
14823   else if (strcmp (language_string, "GNU F77") == 0)
14824     language = DW_LANG_Fortran77;
14825   else if (strcmp (language_string, "GNU Fortran") == 0)
14826     language = DW_LANG_Fortran95;
14827   else if (strcmp (language_string, "GNU Pascal") == 0)
14828     language = DW_LANG_Pascal83;
14829   else if (strcmp (language_string, "GNU Java") == 0)
14830     language = DW_LANG_Java;
14831   else if (strcmp (language_string, "GNU Objective-C") == 0)
14832     language = DW_LANG_ObjC;
14833   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14834     language = DW_LANG_ObjC_plus_plus;
14835   else
14836     language = DW_LANG_C89;
14837
14838   add_AT_unsigned (die, DW_AT_language, language);
14839   return die;
14840 }
14841
14842 /* Generate the DIE for a base class.  */
14843
14844 static void
14845 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14846 {
14847   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14848
14849   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14850   add_data_member_location_attribute (die, binfo);
14851
14852   if (BINFO_VIRTUAL_P (binfo))
14853     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14854
14855   if (access == access_public_node)
14856     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14857   else if (access == access_protected_node)
14858     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14859 }
14860
14861 /* Generate a DIE for a class member.  */
14862
14863 static void
14864 gen_member_die (tree type, dw_die_ref context_die)
14865 {
14866   tree member;
14867   tree binfo = TYPE_BINFO (type);
14868   dw_die_ref child;
14869
14870   /* If this is not an incomplete type, output descriptions of each of its
14871      members. Note that as we output the DIEs necessary to represent the
14872      members of this record or union type, we will also be trying to output
14873      DIEs to represent the *types* of those members. However the `type'
14874      function (above) will specifically avoid generating type DIEs for member
14875      types *within* the list of member DIEs for this (containing) type except
14876      for those types (of members) which are explicitly marked as also being
14877      members of this (containing) type themselves.  The g++ front- end can
14878      force any given type to be treated as a member of some other (containing)
14879      type by setting the TYPE_CONTEXT of the given (member) type to point to
14880      the TREE node representing the appropriate (containing) type.  */
14881
14882   /* First output info about the base classes.  */
14883   if (binfo)
14884     {
14885       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14886       int i;
14887       tree base;
14888
14889       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14890         gen_inheritance_die (base,
14891                              (accesses ? VEC_index (tree, accesses, i)
14892                               : access_public_node), context_die);
14893     }
14894
14895   /* Now output info about the data members and type members.  */
14896   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14897     {
14898       /* If we thought we were generating minimal debug info for TYPE
14899          and then changed our minds, some of the member declarations
14900          may have already been defined.  Don't define them again, but
14901          do put them in the right order.  */
14902
14903       child = lookup_decl_die (member);
14904       if (child)
14905         splice_child_die (context_die, child);
14906       else
14907         gen_decl_die (member, NULL, context_die);
14908     }
14909
14910   /* Now output info about the function members (if any).  */
14911   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14912     {
14913       /* Don't include clones in the member list.  */
14914       if (DECL_ABSTRACT_ORIGIN (member))
14915         continue;
14916
14917       child = lookup_decl_die (member);
14918       if (child)
14919         splice_child_die (context_die, child);
14920       else
14921         gen_decl_die (member, NULL, context_die);
14922     }
14923 }
14924
14925 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14926    is set, we pretend that the type was never defined, so we only get the
14927    member DIEs needed by later specification DIEs.  */
14928
14929 static void
14930 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14931                                 enum debug_info_usage usage)
14932 {
14933   dw_die_ref type_die = lookup_type_die (type);
14934   dw_die_ref scope_die = 0;
14935   int nested = 0;
14936   int complete = (TYPE_SIZE (type)
14937                   && (! TYPE_STUB_DECL (type)
14938                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14939   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14940   complete = complete && should_emit_struct_debug (type, usage);
14941
14942   if (type_die && ! complete)
14943     return;
14944
14945   if (TYPE_CONTEXT (type) != NULL_TREE
14946       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14947           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14948     nested = 1;
14949
14950   scope_die = scope_die_for (type, context_die);
14951
14952   if (! type_die || (nested && scope_die == comp_unit_die))
14953     /* First occurrence of type or toplevel definition of nested class.  */
14954     {
14955       dw_die_ref old_die = type_die;
14956
14957       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14958                           ? record_type_tag (type) : DW_TAG_union_type,
14959                           scope_die, type);
14960       equate_type_number_to_die (type, type_die);
14961       if (old_die)
14962         add_AT_specification (type_die, old_die);
14963       else
14964         add_name_attribute (type_die, type_tag (type));
14965     }
14966   else
14967     remove_AT (type_die, DW_AT_declaration);
14968
14969   /* If this type has been completed, then give it a byte_size attribute and
14970      then give a list of members.  */
14971   if (complete && !ns_decl)
14972     {
14973       /* Prevent infinite recursion in cases where the type of some member of
14974          this type is expressed in terms of this type itself.  */
14975       TREE_ASM_WRITTEN (type) = 1;
14976       add_byte_size_attribute (type_die, type);
14977       if (TYPE_STUB_DECL (type) != NULL_TREE)
14978         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14979
14980       /* If the first reference to this type was as the return type of an
14981          inline function, then it may not have a parent.  Fix this now.  */
14982       if (type_die->die_parent == NULL)
14983         add_child_die (scope_die, type_die);
14984
14985       push_decl_scope (type);
14986       gen_member_die (type, type_die);
14987       pop_decl_scope ();
14988
14989       /* GNU extension: Record what type our vtable lives in.  */
14990       if (TYPE_VFIELD (type))
14991         {
14992           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14993
14994           gen_type_die (vtype, context_die);
14995           add_AT_die_ref (type_die, DW_AT_containing_type,
14996                           lookup_type_die (vtype));
14997         }
14998     }
14999   else
15000     {
15001       add_AT_flag (type_die, DW_AT_declaration, 1);
15002
15003       /* We don't need to do this for function-local types.  */
15004       if (TYPE_STUB_DECL (type)
15005           && ! decl_function_context (TYPE_STUB_DECL (type)))
15006         VEC_safe_push (tree, gc, incomplete_types, type);
15007     }
15008
15009   if (get_AT (type_die, DW_AT_name))
15010     add_pubtype (type, type_die);
15011 }
15012
15013 /* Generate a DIE for a subroutine _type_.  */
15014
15015 static void
15016 gen_subroutine_type_die (tree type, dw_die_ref context_die)
15017 {
15018   tree return_type = TREE_TYPE (type);
15019   dw_die_ref subr_die
15020     = new_die (DW_TAG_subroutine_type,
15021                scope_die_for (type, context_die), type);
15022
15023   equate_type_number_to_die (type, subr_die);
15024   add_prototyped_attribute (subr_die, type);
15025   add_type_attribute (subr_die, return_type, 0, 0, context_die);
15026   gen_formal_types_die (type, subr_die);
15027
15028   if (get_AT (subr_die, DW_AT_name))
15029     add_pubtype (type, subr_die);
15030 }
15031
15032 /* Generate a DIE for a type definition.  */
15033
15034 static void
15035 gen_typedef_die (tree decl, dw_die_ref context_die)
15036 {
15037   dw_die_ref type_die;
15038   tree origin;
15039
15040   if (TREE_ASM_WRITTEN (decl))
15041     return;
15042
15043   TREE_ASM_WRITTEN (decl) = 1;
15044   type_die = new_die (DW_TAG_typedef, context_die, decl);
15045   origin = decl_ultimate_origin (decl);
15046   if (origin != NULL)
15047     add_abstract_origin_attribute (type_die, origin);
15048   else
15049     {
15050       tree type;
15051
15052       add_name_and_src_coords_attributes (type_die, decl);
15053       if (DECL_ORIGINAL_TYPE (decl))
15054         {
15055           type = DECL_ORIGINAL_TYPE (decl);
15056
15057           gcc_assert (type != TREE_TYPE (decl));
15058           equate_type_number_to_die (TREE_TYPE (decl), type_die);
15059         }
15060       else
15061         type = TREE_TYPE (decl);
15062
15063       add_type_attribute (type_die, type, TREE_READONLY (decl),
15064                           TREE_THIS_VOLATILE (decl), context_die);
15065     }
15066
15067   if (DECL_ABSTRACT (decl))
15068     equate_decl_number_to_die (decl, type_die);
15069
15070   if (get_AT (type_die, DW_AT_name))
15071     add_pubtype (decl, type_die);
15072 }
15073
15074 /* Generate a type description DIE.  */
15075
15076 static void
15077 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15078                                 enum debug_info_usage usage)
15079 {
15080   int need_pop;
15081   struct array_descr_info info;
15082
15083   if (type == NULL_TREE || type == error_mark_node)
15084     return;
15085
15086   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15087       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15088     {
15089       if (TREE_ASM_WRITTEN (type))
15090         return;
15091
15092       /* Prevent broken recursion; we can't hand off to the same type.  */
15093       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15094
15095       /* Use the DIE of the containing namespace as the parent DIE of
15096          the type description DIE we want to generate.  */
15097       if (DECL_CONTEXT (TYPE_NAME (type))
15098           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15099         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15100
15101       TREE_ASM_WRITTEN (type) = 1;
15102       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15103       return;
15104     }
15105
15106   /* If this is an array type with hidden descriptor, handle it first.  */
15107   if (!TREE_ASM_WRITTEN (type)
15108       && lang_hooks.types.get_array_descr_info
15109       && lang_hooks.types.get_array_descr_info (type, &info))
15110     {
15111       gen_descr_array_type_die (type, &info, context_die);
15112       TREE_ASM_WRITTEN (type) = 1;
15113       return;
15114     }
15115
15116   /* We are going to output a DIE to represent the unqualified version
15117      of this type (i.e. without any const or volatile qualifiers) so
15118      get the main variant (i.e. the unqualified version) of this type
15119      now.  (Vectors are special because the debugging info is in the
15120      cloned type itself).  */
15121   if (TREE_CODE (type) != VECTOR_TYPE)
15122     type = type_main_variant (type);
15123
15124   if (TREE_ASM_WRITTEN (type))
15125     return;
15126
15127   switch (TREE_CODE (type))
15128     {
15129     case ERROR_MARK:
15130       break;
15131
15132     case POINTER_TYPE:
15133     case REFERENCE_TYPE:
15134       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15135          ensures that the gen_type_die recursion will terminate even if the
15136          type is recursive.  Recursive types are possible in Ada.  */
15137       /* ??? We could perhaps do this for all types before the switch
15138          statement.  */
15139       TREE_ASM_WRITTEN (type) = 1;
15140
15141       /* For these types, all that is required is that we output a DIE (or a
15142          set of DIEs) to represent the "basis" type.  */
15143       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15144                                 DINFO_USAGE_IND_USE);
15145       break;
15146
15147     case OFFSET_TYPE:
15148       /* This code is used for C++ pointer-to-data-member types.
15149          Output a description of the relevant class type.  */
15150       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15151                                         DINFO_USAGE_IND_USE);
15152
15153       /* Output a description of the type of the object pointed to.  */
15154       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15155                                         DINFO_USAGE_IND_USE);
15156
15157       /* Now output a DIE to represent this pointer-to-data-member type
15158          itself.  */
15159       gen_ptr_to_mbr_type_die (type, context_die);
15160       break;
15161
15162     case FUNCTION_TYPE:
15163       /* Force out return type (in case it wasn't forced out already).  */
15164       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15165                                         DINFO_USAGE_DIR_USE);
15166       gen_subroutine_type_die (type, context_die);
15167       break;
15168
15169     case METHOD_TYPE:
15170       /* Force out return type (in case it wasn't forced out already).  */
15171       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15172                                         DINFO_USAGE_DIR_USE);
15173       gen_subroutine_type_die (type, context_die);
15174       break;
15175
15176     case ARRAY_TYPE:
15177       gen_array_type_die (type, context_die);
15178       break;
15179
15180     case VECTOR_TYPE:
15181       gen_array_type_die (type, context_die);
15182       break;
15183
15184     case ENUMERAL_TYPE:
15185     case RECORD_TYPE:
15186     case UNION_TYPE:
15187     case QUAL_UNION_TYPE:
15188       /* If this is a nested type whose containing class hasn't been written
15189          out yet, writing it out will cover this one, too.  This does not apply
15190          to instantiations of member class templates; they need to be added to
15191          the containing class as they are generated.  FIXME: This hurts the
15192          idea of combining type decls from multiple TUs, since we can't predict
15193          what set of template instantiations we'll get.  */
15194       if (TYPE_CONTEXT (type)
15195           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15196           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15197         {
15198           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15199
15200           if (TREE_ASM_WRITTEN (type))
15201             return;
15202
15203           /* If that failed, attach ourselves to the stub.  */
15204           push_decl_scope (TYPE_CONTEXT (type));
15205           context_die = lookup_type_die (TYPE_CONTEXT (type));
15206           need_pop = 1;
15207         }
15208       else
15209         {
15210           context_die = declare_in_namespace (type, context_die);
15211           need_pop = 0;
15212         }
15213
15214       if (TREE_CODE (type) == ENUMERAL_TYPE)
15215         {
15216           /* This might have been written out by the call to
15217              declare_in_namespace.  */
15218           if (!TREE_ASM_WRITTEN (type))
15219             gen_enumeration_type_die (type, context_die);
15220         }
15221       else
15222         gen_struct_or_union_type_die (type, context_die, usage);
15223
15224       if (need_pop)
15225         pop_decl_scope ();
15226
15227       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15228          it up if it is ever completed.  gen_*_type_die will set it for us
15229          when appropriate.  */
15230       return;
15231
15232     case VOID_TYPE:
15233     case INTEGER_TYPE:
15234     case REAL_TYPE:
15235     case FIXED_POINT_TYPE:
15236     case COMPLEX_TYPE:
15237     case BOOLEAN_TYPE:
15238       /* No DIEs needed for fundamental types.  */
15239       break;
15240
15241     case LANG_TYPE:
15242       /* No Dwarf representation currently defined.  */
15243       break;
15244
15245     default:
15246       gcc_unreachable ();
15247     }
15248
15249   TREE_ASM_WRITTEN (type) = 1;
15250 }
15251
15252 static void
15253 gen_type_die (tree type, dw_die_ref context_die)
15254 {
15255   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15256 }
15257
15258 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15259    things which are local to the given block.  */
15260
15261 static void
15262 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15263 {
15264   int must_output_die = 0;
15265   bool inlined_func;
15266
15267   /* Ignore blocks that are NULL.  */
15268   if (stmt == NULL_TREE)
15269     return;
15270
15271   inlined_func = inlined_function_outer_scope_p (stmt);
15272
15273   /* If the block is one fragment of a non-contiguous block, do not
15274      process the variables, since they will have been done by the
15275      origin block.  Do process subblocks.  */
15276   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15277     {
15278       tree sub;
15279
15280       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15281         gen_block_die (sub, context_die, depth + 1);
15282
15283       return;
15284     }
15285
15286   /* Determine if we need to output any Dwarf DIEs at all to represent this
15287      block.  */
15288   if (inlined_func)
15289     /* The outer scopes for inlinings *must* always be represented.  We
15290        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15291     must_output_die = 1;
15292   else
15293     {
15294       /* Determine if this block directly contains any "significant"
15295          local declarations which we will need to output DIEs for.  */
15296       if (debug_info_level > DINFO_LEVEL_TERSE)
15297         /* We are not in terse mode so *any* local declaration counts
15298            as being a "significant" one.  */
15299         must_output_die = ((BLOCK_VARS (stmt) != NULL
15300                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15301                            && (TREE_USED (stmt)
15302                                || TREE_ASM_WRITTEN (stmt)
15303                                || BLOCK_ABSTRACT (stmt)));
15304       else if ((TREE_USED (stmt)
15305                 || TREE_ASM_WRITTEN (stmt)
15306                 || BLOCK_ABSTRACT (stmt))
15307                && !dwarf2out_ignore_block (stmt))
15308         must_output_die = 1;
15309     }
15310
15311   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15312      DIE for any block which contains no significant local declarations at
15313      all.  Rather, in such cases we just call `decls_for_scope' so that any
15314      needed Dwarf info for any sub-blocks will get properly generated. Note
15315      that in terse mode, our definition of what constitutes a "significant"
15316      local declaration gets restricted to include only inlined function
15317      instances and local (nested) function definitions.  */
15318   if (must_output_die)
15319     {
15320       if (inlined_func)
15321         gen_inlined_subroutine_die (stmt, context_die, depth);
15322       else
15323         gen_lexical_block_die (stmt, context_die, depth);
15324     }
15325   else
15326     decls_for_scope (stmt, context_die, depth);
15327 }
15328
15329 /* Process variable DECL (or variable with origin ORIGIN) within
15330    block STMT and add it to CONTEXT_DIE.  */
15331 static void
15332 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15333 {
15334   dw_die_ref die;
15335   tree decl_or_origin = decl ? decl : origin;
15336   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15337
15338   if (ultimate_origin)
15339     origin = ultimate_origin;
15340
15341   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15342     die = lookup_decl_die (decl_or_origin);
15343   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15344            && TYPE_DECL_IS_STUB (decl_or_origin))
15345     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15346   else
15347     die = NULL;
15348
15349   if (die != NULL && die->die_parent == NULL)
15350     add_child_die (context_die, die);
15351   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15352     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15353                                          stmt, context_die);
15354   else
15355     gen_decl_die (decl, origin, context_die);
15356 }
15357
15358 /* Generate all of the decls declared within a given scope and (recursively)
15359    all of its sub-blocks.  */
15360
15361 static void
15362 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15363 {
15364   tree decl;
15365   unsigned int i;
15366   tree subblocks;
15367
15368   /* Ignore NULL blocks.  */
15369   if (stmt == NULL_TREE)
15370     return;
15371
15372   /* Output the DIEs to represent all of the data objects and typedefs
15373      declared directly within this block but not within any nested
15374      sub-blocks.  Also, nested function and tag DIEs have been
15375      generated with a parent of NULL; fix that up now.  */
15376   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15377     process_scope_var (stmt, decl, NULL_TREE, context_die);
15378   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15379     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15380                        context_die);
15381
15382   /* If we're at -g1, we're not interested in subblocks.  */
15383   if (debug_info_level <= DINFO_LEVEL_TERSE)
15384     return;
15385
15386   /* Output the DIEs to represent all sub-blocks (and the items declared
15387      therein) of this block.  */
15388   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15389        subblocks != NULL;
15390        subblocks = BLOCK_CHAIN (subblocks))
15391     gen_block_die (subblocks, context_die, depth + 1);
15392 }
15393
15394 /* Is this a typedef we can avoid emitting?  */
15395
15396 static inline int
15397 is_redundant_typedef (const_tree decl)
15398 {
15399   if (TYPE_DECL_IS_STUB (decl))
15400     return 1;
15401
15402   if (DECL_ARTIFICIAL (decl)
15403       && DECL_CONTEXT (decl)
15404       && is_tagged_type (DECL_CONTEXT (decl))
15405       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15406       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15407     /* Also ignore the artificial member typedef for the class name.  */
15408     return 1;
15409
15410   return 0;
15411 }
15412
15413 /* Returns the DIE for a context.  */
15414
15415 static inline dw_die_ref
15416 get_context_die (tree context)
15417 {
15418   if (context)
15419     {
15420       /* Find die that represents this context.  */
15421       if (TYPE_P (context))
15422         return force_type_die (context);
15423       else
15424         return force_decl_die (context);
15425     }
15426   return comp_unit_die;
15427 }
15428
15429 /* Returns the DIE for decl.  A DIE will always be returned.  */
15430
15431 static dw_die_ref
15432 force_decl_die (tree decl)
15433 {
15434   dw_die_ref decl_die;
15435   unsigned saved_external_flag;
15436   tree save_fn = NULL_TREE;
15437   decl_die = lookup_decl_die (decl);
15438   if (!decl_die)
15439     {
15440       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15441
15442       decl_die = lookup_decl_die (decl);
15443       if (decl_die)
15444         return decl_die;
15445
15446       switch (TREE_CODE (decl))
15447         {
15448         case FUNCTION_DECL:
15449           /* Clear current_function_decl, so that gen_subprogram_die thinks
15450              that this is a declaration. At this point, we just want to force
15451              declaration die.  */
15452           save_fn = current_function_decl;
15453           current_function_decl = NULL_TREE;
15454           gen_subprogram_die (decl, context_die);
15455           current_function_decl = save_fn;
15456           break;
15457
15458         case VAR_DECL:
15459           /* Set external flag to force declaration die. Restore it after
15460            gen_decl_die() call.  */
15461           saved_external_flag = DECL_EXTERNAL (decl);
15462           DECL_EXTERNAL (decl) = 1;
15463           gen_decl_die (decl, NULL, context_die);
15464           DECL_EXTERNAL (decl) = saved_external_flag;
15465           break;
15466
15467         case NAMESPACE_DECL:
15468           dwarf2out_decl (decl);
15469           break;
15470
15471         default:
15472           gcc_unreachable ();
15473         }
15474
15475       /* We should be able to find the DIE now.  */
15476       if (!decl_die)
15477         decl_die = lookup_decl_die (decl);
15478       gcc_assert (decl_die);
15479     }
15480
15481   return decl_die;
15482 }
15483
15484 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15485    always returned.  */
15486
15487 static dw_die_ref
15488 force_type_die (tree type)
15489 {
15490   dw_die_ref type_die;
15491
15492   type_die = lookup_type_die (type);
15493   if (!type_die)
15494     {
15495       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15496
15497       type_die = modified_type_die (type, TYPE_READONLY (type),
15498                                     TYPE_VOLATILE (type), context_die);
15499       gcc_assert (type_die);
15500     }
15501   return type_die;
15502 }
15503
15504 /* Force out any required namespaces to be able to output DECL,
15505    and return the new context_die for it, if it's changed.  */
15506
15507 static dw_die_ref
15508 setup_namespace_context (tree thing, dw_die_ref context_die)
15509 {
15510   tree context = (DECL_P (thing)
15511                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15512   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15513     /* Force out the namespace.  */
15514     context_die = force_decl_die (context);
15515
15516   return context_die;
15517 }
15518
15519 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15520    type) within its namespace, if appropriate.
15521
15522    For compatibility with older debuggers, namespace DIEs only contain
15523    declarations; all definitions are emitted at CU scope.  */
15524
15525 static dw_die_ref
15526 declare_in_namespace (tree thing, dw_die_ref context_die)
15527 {
15528   dw_die_ref ns_context;
15529
15530   if (debug_info_level <= DINFO_LEVEL_TERSE)
15531     return context_die;
15532
15533   /* If this decl is from an inlined function, then don't try to emit it in its
15534      namespace, as we will get confused.  It would have already been emitted
15535      when the abstract instance of the inline function was emitted anyways.  */
15536   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15537     return context_die;
15538
15539   ns_context = setup_namespace_context (thing, context_die);
15540
15541   if (ns_context != context_die)
15542     {
15543       if (is_fortran ())
15544         return ns_context;
15545       if (DECL_P (thing))
15546         gen_decl_die (thing, NULL, ns_context);
15547       else
15548         gen_type_die (thing, ns_context);
15549     }
15550   return context_die;
15551 }
15552
15553 /* Generate a DIE for a namespace or namespace alias.  */
15554
15555 static void
15556 gen_namespace_die (tree decl, dw_die_ref context_die)
15557 {
15558   dw_die_ref namespace_die;
15559
15560   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15561      they are an alias of.  */
15562   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15563     {
15564       /* Output a real namespace or module.  */
15565       context_die = setup_namespace_context (decl, comp_unit_die);
15566       namespace_die = new_die (is_fortran ()
15567                                ? DW_TAG_module : DW_TAG_namespace,
15568                                context_die, decl);
15569       /* For Fortran modules defined in different CU don't add src coords.  */
15570       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15571         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15572       else
15573         add_name_and_src_coords_attributes (namespace_die, decl);
15574       if (DECL_EXTERNAL (decl))
15575         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15576       equate_decl_number_to_die (decl, namespace_die);
15577     }
15578   else
15579     {
15580       /* Output a namespace alias.  */
15581
15582       /* Force out the namespace we are an alias of, if necessary.  */
15583       dw_die_ref origin_die
15584         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15585
15586       if (DECL_CONTEXT (decl) == NULL_TREE
15587           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15588         context_die = setup_namespace_context (decl, comp_unit_die);
15589       /* Now create the namespace alias DIE.  */
15590       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15591       add_name_and_src_coords_attributes (namespace_die, decl);
15592       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15593       equate_decl_number_to_die (decl, namespace_die);
15594     }
15595 }
15596
15597 /* Generate Dwarf debug information for a decl described by DECL.  */
15598
15599 static void
15600 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15601 {
15602   tree decl_or_origin = decl ? decl : origin;
15603   tree class_origin = NULL;
15604
15605   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15606     return;
15607
15608   switch (TREE_CODE (decl_or_origin))
15609     {
15610     case ERROR_MARK:
15611       break;
15612
15613     case CONST_DECL:
15614       if (!is_fortran ())
15615         {
15616           /* The individual enumerators of an enum type get output when we output
15617              the Dwarf representation of the relevant enum type itself.  */
15618           break;
15619         }
15620
15621       /* Emit its type.  */
15622       gen_type_die (TREE_TYPE (decl), context_die);
15623
15624       /* And its containing namespace.  */
15625       context_die = declare_in_namespace (decl, context_die);
15626
15627       gen_const_die (decl, context_die);
15628       break;
15629
15630     case FUNCTION_DECL:
15631       /* Don't output any DIEs to represent mere function declarations,
15632          unless they are class members or explicit block externs.  */
15633       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15634           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15635           && (current_function_decl == NULL_TREE
15636               || DECL_ARTIFICIAL (decl_or_origin)))
15637         break;
15638
15639 #if 0
15640       /* FIXME */
15641       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15642          on local redeclarations of global functions.  That seems broken.  */
15643       if (current_function_decl != decl)
15644         /* This is only a declaration.  */;
15645 #endif
15646
15647       /* If we're emitting a clone, emit info for the abstract instance.  */
15648       if (origin || DECL_ORIGIN (decl) != decl)
15649         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15650
15651       /* If we're emitting an out-of-line copy of an inline function,
15652          emit info for the abstract instance and set up to refer to it.  */
15653       else if (cgraph_function_possibly_inlined_p (decl)
15654                && ! DECL_ABSTRACT (decl)
15655                && ! class_or_namespace_scope_p (context_die)
15656                /* dwarf2out_abstract_function won't emit a die if this is just
15657                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15658                   that case, because that works only if we have a die.  */
15659                && DECL_INITIAL (decl) != NULL_TREE)
15660         {
15661           dwarf2out_abstract_function (decl);
15662           set_decl_origin_self (decl);
15663         }
15664
15665       /* Otherwise we're emitting the primary DIE for this decl.  */
15666       else if (debug_info_level > DINFO_LEVEL_TERSE)
15667         {
15668           /* Before we describe the FUNCTION_DECL itself, make sure that we
15669              have described its return type.  */
15670           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15671
15672           /* And its virtual context.  */
15673           if (DECL_VINDEX (decl) != NULL_TREE)
15674             gen_type_die (DECL_CONTEXT (decl), context_die);
15675
15676           /* And its containing type.  */
15677           if (!origin)
15678             origin = decl_class_context (decl);
15679           if (origin != NULL_TREE)
15680             gen_type_die_for_member (origin, decl, context_die);
15681
15682           /* And its containing namespace.  */
15683           context_die = declare_in_namespace (decl, context_die);
15684         }
15685
15686       /* Now output a DIE to represent the function itself.  */
15687       if (decl)
15688         gen_subprogram_die (decl, context_die);
15689       break;
15690
15691     case TYPE_DECL:
15692       /* If we are in terse mode, don't generate any DIEs to represent any
15693          actual typedefs.  */
15694       if (debug_info_level <= DINFO_LEVEL_TERSE)
15695         break;
15696
15697       /* In the special case of a TYPE_DECL node representing the declaration
15698          of some type tag, if the given TYPE_DECL is marked as having been
15699          instantiated from some other (original) TYPE_DECL node (e.g. one which
15700          was generated within the original definition of an inline function) we
15701          used to generate a special (abbreviated) DW_TAG_structure_type,
15702          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15703          should be actually referencing those DIEs, as variable DIEs with that
15704          type would be emitted already in the abstract origin, so it was always
15705          removed during unused type prunning.  Don't add anything in this
15706          case.  */
15707       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15708         break;
15709
15710       if (is_redundant_typedef (decl))
15711         gen_type_die (TREE_TYPE (decl), context_die);
15712       else
15713         /* Output a DIE to represent the typedef itself.  */
15714         gen_typedef_die (decl, context_die);
15715       break;
15716
15717     case LABEL_DECL:
15718       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15719         gen_label_die (decl, context_die);
15720       break;
15721
15722     case VAR_DECL:
15723     case RESULT_DECL:
15724       /* If we are in terse mode, don't generate any DIEs to represent any
15725          variable declarations or definitions.  */
15726       if (debug_info_level <= DINFO_LEVEL_TERSE)
15727         break;
15728
15729       /* Output any DIEs that are needed to specify the type of this data
15730          object.  */
15731       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15732            || TREE_CODE (decl_or_origin) == VAR_DECL)
15733           && DECL_BY_REFERENCE (decl_or_origin))
15734         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15735       else
15736         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15737
15738       /* And its containing type.  */
15739       class_origin = decl_class_context (decl_or_origin);
15740       if (class_origin != NULL_TREE)
15741         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15742
15743       /* And its containing namespace.  */
15744       context_die = declare_in_namespace (decl_or_origin, context_die);
15745
15746       /* Now output the DIE to represent the data object itself.  This gets
15747          complicated because of the possibility that the VAR_DECL really
15748          represents an inlined instance of a formal parameter for an inline
15749          function.  */
15750       if (!origin)
15751         origin = decl_ultimate_origin (decl);
15752       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15753         gen_formal_parameter_die (decl, origin, context_die);
15754       else
15755         gen_variable_die (decl, origin, context_die);
15756       break;
15757
15758     case FIELD_DECL:
15759       /* Ignore the nameless fields that are used to skip bits but handle C++
15760          anonymous unions and structs.  */
15761       if (DECL_NAME (decl) != NULL_TREE
15762           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15763           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15764         {
15765           gen_type_die (member_declared_type (decl), context_die);
15766           gen_field_die (decl, context_die);
15767         }
15768       break;
15769
15770     case PARM_DECL:
15771       if (DECL_BY_REFERENCE (decl_or_origin))
15772         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15773       else
15774         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15775       gen_formal_parameter_die (decl, origin, context_die);
15776       break;
15777
15778     case NAMESPACE_DECL:
15779     case IMPORTED_DECL:
15780       gen_namespace_die (decl, context_die);
15781       break;
15782
15783     default:
15784       /* Probably some frontend-internal decl.  Assume we don't care.  */
15785       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15786       break;
15787     }
15788 }
15789 \f
15790 /* Output debug information for global decl DECL.  Called from toplev.c after
15791    compilation proper has finished.  */
15792
15793 static void
15794 dwarf2out_global_decl (tree decl)
15795 {
15796   /* Output DWARF2 information for file-scope tentative data object
15797      declarations, file-scope (extern) function declarations (which
15798      had no corresponding body) and file-scope tagged type declarations
15799      and definitions which have not yet been forced out.  */
15800   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15801     dwarf2out_decl (decl);
15802 }
15803
15804 /* Output debug information for type decl DECL.  Called from toplev.c
15805    and from language front ends (to record built-in types).  */
15806 static void
15807 dwarf2out_type_decl (tree decl, int local)
15808 {
15809   if (!local)
15810     dwarf2out_decl (decl);
15811 }
15812
15813 /* Output debug information for imported module or decl DECL.
15814    NAME is non-NULL name in the lexical block if the decl has been renamed.
15815    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15816    that DECL belongs to.
15817    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15818 static void
15819 dwarf2out_imported_module_or_decl_1 (tree decl,
15820                                      tree name,
15821                                      tree lexical_block,
15822                                      dw_die_ref lexical_block_die)
15823 {
15824   expanded_location xloc;
15825   dw_die_ref imported_die = NULL;
15826   dw_die_ref at_import_die;
15827
15828   if (TREE_CODE (decl) == IMPORTED_DECL)
15829     {
15830       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15831       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15832       gcc_assert (decl);
15833     }
15834   else
15835     xloc = expand_location (input_location);
15836
15837   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15838     {
15839       if (is_base_type (TREE_TYPE (decl)))
15840         at_import_die = base_type_die (TREE_TYPE (decl));
15841       else
15842         at_import_die = force_type_die (TREE_TYPE (decl));
15843       /* For namespace N { typedef void T; } using N::T; base_type_die
15844          returns NULL, but DW_TAG_imported_declaration requires
15845          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15846       if (!at_import_die)
15847         {
15848           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15849           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15850           at_import_die = lookup_type_die (TREE_TYPE (decl));
15851           gcc_assert (at_import_die);
15852         }
15853     }
15854   else
15855     {
15856       at_import_die = lookup_decl_die (decl);
15857       if (!at_import_die)
15858         {
15859           /* If we're trying to avoid duplicate debug info, we may not have
15860              emitted the member decl for this field.  Emit it now.  */
15861           if (TREE_CODE (decl) == FIELD_DECL)
15862             {
15863               tree type = DECL_CONTEXT (decl);
15864
15865               if (TYPE_CONTEXT (type)
15866                   && TYPE_P (TYPE_CONTEXT (type))
15867                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15868                                                 DINFO_USAGE_DIR_USE))
15869                 return;
15870               gen_type_die_for_member (type, decl,
15871                                        get_context_die (TYPE_CONTEXT (type)));
15872             }
15873           at_import_die = force_decl_die (decl);
15874         }
15875     }
15876
15877   if (TREE_CODE (decl) == NAMESPACE_DECL)
15878     imported_die = new_die (DW_TAG_imported_module,
15879                             lexical_block_die,
15880                             lexical_block);
15881   else
15882     imported_die = new_die (DW_TAG_imported_declaration,
15883                             lexical_block_die,
15884                             lexical_block);
15885
15886   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15887   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15888   if (name)
15889     add_AT_string (imported_die, DW_AT_name,
15890                    IDENTIFIER_POINTER (name));
15891   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15892 }
15893
15894 /* Output debug information for imported module or decl DECL.
15895    NAME is non-NULL name in context if the decl has been renamed.
15896    CHILD is true if decl is one of the renamed decls as part of
15897    importing whole module.  */
15898
15899 static void
15900 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15901                                    bool child)
15902 {
15903   /* dw_die_ref at_import_die;  */
15904   dw_die_ref scope_die;
15905
15906   if (debug_info_level <= DINFO_LEVEL_TERSE)
15907     return;
15908
15909   gcc_assert (decl);
15910
15911   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15912      We need decl DIE for reference and scope die. First, get DIE for the decl
15913      itself.  */
15914
15915   /* Get the scope die for decl context. Use comp_unit_die for global module
15916      or decl. If die is not found for non globals, force new die.  */
15917   if (context
15918       && TYPE_P (context)
15919       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15920     return;
15921   scope_die = get_context_die (context);
15922
15923   if (child)
15924     {
15925       gcc_assert (scope_die->die_child);
15926       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15927       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15928       scope_die = scope_die->die_child;
15929     }
15930
15931   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15932   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15933
15934 }
15935
15936 /* Write the debugging output for DECL.  */
15937
15938 void
15939 dwarf2out_decl (tree decl)
15940 {
15941   dw_die_ref context_die = comp_unit_die;
15942
15943   switch (TREE_CODE (decl))
15944     {
15945     case ERROR_MARK:
15946       return;
15947
15948     case FUNCTION_DECL:
15949       /* What we would really like to do here is to filter out all mere
15950          file-scope declarations of file-scope functions which are never
15951          referenced later within this translation unit (and keep all of ones
15952          that *are* referenced later on) but we aren't clairvoyant, so we have
15953          no idea which functions will be referenced in the future (i.e. later
15954          on within the current translation unit). So here we just ignore all
15955          file-scope function declarations which are not also definitions.  If
15956          and when the debugger needs to know something about these functions,
15957          it will have to hunt around and find the DWARF information associated
15958          with the definition of the function.
15959
15960          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15961          nodes represent definitions and which ones represent mere
15962          declarations.  We have to check DECL_INITIAL instead. That's because
15963          the C front-end supports some weird semantics for "extern inline"
15964          function definitions.  These can get inlined within the current
15965          translation unit (and thus, we need to generate Dwarf info for their
15966          abstract instances so that the Dwarf info for the concrete inlined
15967          instances can have something to refer to) but the compiler never
15968          generates any out-of-lines instances of such things (despite the fact
15969          that they *are* definitions).
15970
15971          The important point is that the C front-end marks these "extern
15972          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15973          them anyway. Note that the C++ front-end also plays some similar games
15974          for inline function definitions appearing within include files which
15975          also contain `#pragma interface' pragmas.  */
15976       if (DECL_INITIAL (decl) == NULL_TREE)
15977         return;
15978
15979       /* If we're a nested function, initially use a parent of NULL; if we're
15980          a plain function, this will be fixed up in decls_for_scope.  If
15981          we're a method, it will be ignored, since we already have a DIE.  */
15982       if (decl_function_context (decl)
15983           /* But if we're in terse mode, we don't care about scope.  */
15984           && debug_info_level > DINFO_LEVEL_TERSE)
15985         context_die = NULL;
15986       break;
15987
15988     case VAR_DECL:
15989       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15990          declaration and if the declaration was never even referenced from
15991          within this entire compilation unit.  We suppress these DIEs in
15992          order to save space in the .debug section (by eliminating entries
15993          which are probably useless).  Note that we must not suppress
15994          block-local extern declarations (whether used or not) because that
15995          would screw-up the debugger's name lookup mechanism and cause it to
15996          miss things which really ought to be in scope at a given point.  */
15997       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15998         return;
15999
16000       /* For local statics lookup proper context die.  */
16001       if (TREE_STATIC (decl) && decl_function_context (decl))
16002         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16003
16004       /* If we are in terse mode, don't generate any DIEs to represent any
16005          variable declarations or definitions.  */
16006       if (debug_info_level <= DINFO_LEVEL_TERSE)
16007         return;
16008       break;
16009
16010     case CONST_DECL:
16011       if (debug_info_level <= DINFO_LEVEL_TERSE)
16012         return;
16013       if (!is_fortran ())
16014         return;
16015       if (TREE_STATIC (decl) && decl_function_context (decl))
16016         context_die = lookup_decl_die (DECL_CONTEXT (decl));
16017       break;
16018
16019     case NAMESPACE_DECL:
16020     case IMPORTED_DECL:
16021       if (debug_info_level <= DINFO_LEVEL_TERSE)
16022         return;
16023       if (lookup_decl_die (decl) != NULL)
16024         return;
16025       break;
16026
16027     case TYPE_DECL:
16028       /* Don't emit stubs for types unless they are needed by other DIEs.  */
16029       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
16030         return;
16031
16032       /* Don't bother trying to generate any DIEs to represent any of the
16033          normal built-in types for the language we are compiling.  */
16034       if (DECL_IS_BUILTIN (decl))
16035         {
16036           /* OK, we need to generate one for `bool' so GDB knows what type
16037              comparisons have.  */
16038           if (is_cxx ()
16039               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
16040               && ! DECL_IGNORED_P (decl))
16041             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
16042
16043           return;
16044         }
16045
16046       /* If we are in terse mode, don't generate any DIEs for types.  */
16047       if (debug_info_level <= DINFO_LEVEL_TERSE)
16048         return;
16049
16050       /* If we're a function-scope tag, initially use a parent of NULL;
16051          this will be fixed up in decls_for_scope.  */
16052       if (decl_function_context (decl))
16053         context_die = NULL;
16054
16055       break;
16056
16057     default:
16058       return;
16059     }
16060
16061   gen_decl_die (decl, NULL, context_die);
16062 }
16063
16064 /* Output a marker (i.e. a label) for the beginning of the generated code for
16065    a lexical block.  */
16066
16067 static void
16068 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16069                        unsigned int blocknum)
16070 {
16071   switch_to_section (current_function_section ());
16072   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16073 }
16074
16075 /* Output a marker (i.e. a label) for the end of the generated code for a
16076    lexical block.  */
16077
16078 static void
16079 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16080 {
16081   switch_to_section (current_function_section ());
16082   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16083 }
16084
16085 /* Returns nonzero if it is appropriate not to emit any debugging
16086    information for BLOCK, because it doesn't contain any instructions.
16087
16088    Don't allow this for blocks with nested functions or local classes
16089    as we would end up with orphans, and in the presence of scheduling
16090    we may end up calling them anyway.  */
16091
16092 static bool
16093 dwarf2out_ignore_block (const_tree block)
16094 {
16095   tree decl;
16096   unsigned int i;
16097
16098   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16099     if (TREE_CODE (decl) == FUNCTION_DECL
16100         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16101       return 0;
16102   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16103     {
16104       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16105       if (TREE_CODE (decl) == FUNCTION_DECL
16106           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16107       return 0;
16108     }
16109
16110   return 1;
16111 }
16112
16113 /* Hash table routines for file_hash.  */
16114
16115 static int
16116 file_table_eq (const void *p1_p, const void *p2_p)
16117 {
16118   const struct dwarf_file_data *const p1 =
16119     (const struct dwarf_file_data *) p1_p;
16120   const char *const p2 = (const char *) p2_p;
16121   return strcmp (p1->filename, p2) == 0;
16122 }
16123
16124 static hashval_t
16125 file_table_hash (const void *p_p)
16126 {
16127   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16128   return htab_hash_string (p->filename);
16129 }
16130
16131 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16132    dwarf2out.c) and return its "index".  The index of each (known) filename is
16133    just a unique number which is associated with only that one filename.  We
16134    need such numbers for the sake of generating labels (in the .debug_sfnames
16135    section) and references to those files numbers (in the .debug_srcinfo
16136    and.debug_macinfo sections).  If the filename given as an argument is not
16137    found in our current list, add it to the list and assign it the next
16138    available unique index number.  In order to speed up searches, we remember
16139    the index of the filename was looked up last.  This handles the majority of
16140    all searches.  */
16141
16142 static struct dwarf_file_data *
16143 lookup_filename (const char *file_name)
16144 {
16145   void ** slot;
16146   struct dwarf_file_data * created;
16147
16148   /* Check to see if the file name that was searched on the previous
16149      call matches this file name.  If so, return the index.  */
16150   if (file_table_last_lookup
16151       && (file_name == file_table_last_lookup->filename
16152           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16153     return file_table_last_lookup;
16154
16155   /* Didn't match the previous lookup, search the table.  */
16156   slot = htab_find_slot_with_hash (file_table, file_name,
16157                                    htab_hash_string (file_name), INSERT);
16158   if (*slot)
16159     return (struct dwarf_file_data *) *slot;
16160
16161   created = GGC_NEW (struct dwarf_file_data);
16162   created->filename = file_name;
16163   created->emitted_number = 0;
16164   *slot = created;
16165   return created;
16166 }
16167
16168 /* If the assembler will construct the file table, then translate the compiler
16169    internal file table number into the assembler file table number, and emit
16170    a .file directive if we haven't already emitted one yet.  The file table
16171    numbers are different because we prune debug info for unused variables and
16172    types, which may include filenames.  */
16173
16174 static int
16175 maybe_emit_file (struct dwarf_file_data * fd)
16176 {
16177   if (! fd->emitted_number)
16178     {
16179       if (last_emitted_file)
16180         fd->emitted_number = last_emitted_file->emitted_number + 1;
16181       else
16182         fd->emitted_number = 1;
16183       last_emitted_file = fd;
16184
16185       if (DWARF2_ASM_LINE_DEBUG_INFO)
16186         {
16187           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16188           output_quoted_string (asm_out_file,
16189                                 remap_debug_filename (fd->filename));
16190           fputc ('\n', asm_out_file);
16191         }
16192     }
16193
16194   return fd->emitted_number;
16195 }
16196
16197 /* Replace DW_AT_name for the decl with name.  */
16198  
16199 static void
16200 dwarf2out_set_name (tree decl, tree name)
16201 {
16202   dw_die_ref die;
16203   dw_attr_ref attr;
16204
16205   die = TYPE_SYMTAB_DIE (decl);
16206   if (!die)
16207     return;
16208
16209   attr = get_AT (die, DW_AT_name);
16210   if (attr)
16211     {
16212       struct indirect_string_node *node;
16213
16214       node = find_AT_string (dwarf2_name (name, 0));
16215       /* replace the string.  */
16216       attr->dw_attr_val.v.val_str = node;
16217     }
16218
16219   else
16220     add_name_attribute (die, dwarf2_name (name, 0));
16221 }
16222
16223 /* Called by the final INSN scan whenever we see a var location.  We
16224    use it to drop labels in the right places, and throw the location in
16225    our lookup table.  */
16226
16227 static void
16228 dwarf2out_var_location (rtx loc_note)
16229 {
16230   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16231   struct var_loc_node *newloc;
16232   rtx next_real;
16233   static const char *last_label;
16234   static bool last_in_cold_section_p;
16235   tree decl;
16236
16237   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16238     return;
16239
16240   next_real = next_real_insn (loc_note);
16241   /* If there are no instructions which would be affected by this note,
16242      don't do anything.  */
16243   if (next_real == NULL_RTX)
16244     return;
16245
16246   newloc = GGC_CNEW (struct var_loc_node);
16247   /* If there were no real insns between note we processed last time
16248      and this note, use the label we emitted last time.  */
16249   if (last_var_location_insn != NULL_RTX
16250       && last_var_location_insn == next_real
16251       && last_in_cold_section_p == in_cold_section_p)
16252     newloc->label = last_label;
16253   else
16254     {
16255       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16256       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16257       loclabel_num++;
16258       newloc->label = ggc_strdup (loclabel);
16259     }
16260   newloc->var_loc_note = loc_note;
16261   newloc->next = NULL;
16262
16263   if (cfun && in_cold_section_p)
16264     newloc->section_label = crtl->subsections.cold_section_label;
16265   else
16266     newloc->section_label = text_section_label;
16267
16268   last_var_location_insn = next_real;
16269   last_label = newloc->label;
16270   last_in_cold_section_p = in_cold_section_p;
16271   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16272   add_var_loc_to_decl (decl, newloc);
16273 }
16274
16275 /* We need to reset the locations at the beginning of each
16276    function. We can't do this in the end_function hook, because the
16277    declarations that use the locations won't have been output when
16278    that hook is called.  Also compute have_multiple_function_sections here.  */
16279
16280 static void
16281 dwarf2out_begin_function (tree fun)
16282 {
16283   htab_empty (decl_loc_table);
16284
16285   if (function_section (fun) != text_section)
16286     have_multiple_function_sections = true;
16287
16288   dwarf2out_note_section_used ();
16289 }
16290
16291 /* Output a label to mark the beginning of a source code line entry
16292    and record information relating to this source line, in
16293    'line_info_table' for later output of the .debug_line section.  */
16294
16295 static void
16296 dwarf2out_source_line (unsigned int line, const char *filename,
16297                        int discriminator, bool is_stmt)
16298 {
16299   static bool last_is_stmt = true;
16300
16301   if (debug_info_level >= DINFO_LEVEL_NORMAL
16302       && line != 0)
16303     {
16304       int file_num = maybe_emit_file (lookup_filename (filename));
16305
16306       switch_to_section (current_function_section ());
16307
16308       /* If requested, emit something human-readable.  */
16309       if (flag_debug_asm)
16310         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16311                  filename, line);
16312
16313       if (DWARF2_ASM_LINE_DEBUG_INFO)
16314         {
16315           /* Emit the .loc directive understood by GNU as.  */
16316           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16317           if (is_stmt != last_is_stmt)
16318             {
16319               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
16320               last_is_stmt = is_stmt;
16321             }
16322           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
16323             fprintf (asm_out_file, " discriminator %d", discriminator);
16324           fputc ('\n', asm_out_file);
16325
16326           /* Indicate that line number info exists.  */
16327           line_info_table_in_use++;
16328         }
16329       else if (function_section (current_function_decl) != text_section)
16330         {
16331           dw_separate_line_info_ref line_info;
16332           targetm.asm_out.internal_label (asm_out_file,
16333                                           SEPARATE_LINE_CODE_LABEL,
16334                                           separate_line_info_table_in_use);
16335
16336           /* Expand the line info table if necessary.  */
16337           if (separate_line_info_table_in_use
16338               == separate_line_info_table_allocated)
16339             {
16340               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16341               separate_line_info_table
16342                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16343                                  separate_line_info_table,
16344                                  separate_line_info_table_allocated);
16345               memset (separate_line_info_table
16346                        + separate_line_info_table_in_use,
16347                       0,
16348                       (LINE_INFO_TABLE_INCREMENT
16349                        * sizeof (dw_separate_line_info_entry)));
16350             }
16351
16352           /* Add the new entry at the end of the line_info_table.  */
16353           line_info
16354             = &separate_line_info_table[separate_line_info_table_in_use++];
16355           line_info->dw_file_num = file_num;
16356           line_info->dw_line_num = line;
16357           line_info->function = current_function_funcdef_no;
16358         }
16359       else
16360         {
16361           dw_line_info_ref line_info;
16362
16363           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16364                                      line_info_table_in_use);
16365
16366           /* Expand the line info table if necessary.  */
16367           if (line_info_table_in_use == line_info_table_allocated)
16368             {
16369               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16370               line_info_table
16371                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16372                                  line_info_table_allocated);
16373               memset (line_info_table + line_info_table_in_use, 0,
16374                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16375             }
16376
16377           /* Add the new entry at the end of the line_info_table.  */
16378           line_info = &line_info_table[line_info_table_in_use++];
16379           line_info->dw_file_num = file_num;
16380           line_info->dw_line_num = line;
16381         }
16382     }
16383 }
16384
16385 /* Record the beginning of a new source file.  */
16386
16387 static void
16388 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16389 {
16390   if (flag_eliminate_dwarf2_dups)
16391     {
16392       /* Record the beginning of the file for break_out_includes.  */
16393       dw_die_ref bincl_die;
16394
16395       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16396       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16397     }
16398
16399   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16400     {
16401       int file_num = maybe_emit_file (lookup_filename (filename));
16402
16403       switch_to_section (debug_macinfo_section);
16404       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16405       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16406                                    lineno);
16407
16408       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16409     }
16410 }
16411
16412 /* Record the end of a source file.  */
16413
16414 static void
16415 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16416 {
16417   if (flag_eliminate_dwarf2_dups)
16418     /* Record the end of the file for break_out_includes.  */
16419     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16420
16421   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16422     {
16423       switch_to_section (debug_macinfo_section);
16424       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16425     }
16426 }
16427
16428 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16429    the tail part of the directive line, i.e. the part which is past the
16430    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16431
16432 static void
16433 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16434                   const char *buffer ATTRIBUTE_UNUSED)
16435 {
16436   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16437     {
16438       switch_to_section (debug_macinfo_section);
16439       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16440       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16441       dw2_asm_output_nstring (buffer, -1, "The macro");
16442     }
16443 }
16444
16445 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16446    the tail part of the directive line, i.e. the part which is past the
16447    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16448
16449 static void
16450 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16451                  const char *buffer ATTRIBUTE_UNUSED)
16452 {
16453   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16454     {
16455       switch_to_section (debug_macinfo_section);
16456       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16457       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16458       dw2_asm_output_nstring (buffer, -1, "The macro");
16459     }
16460 }
16461
16462 /* Set up for Dwarf output at the start of compilation.  */
16463
16464 static void
16465 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16466 {
16467   /* Allocate the file_table.  */
16468   file_table = htab_create_ggc (50, file_table_hash,
16469                                 file_table_eq, NULL);
16470
16471   /* Allocate the decl_die_table.  */
16472   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16473                                     decl_die_table_eq, NULL);
16474
16475   /* Allocate the decl_loc_table.  */
16476   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16477                                     decl_loc_table_eq, NULL);
16478
16479   /* Allocate the initial hunk of the decl_scope_table.  */
16480   decl_scope_table = VEC_alloc (tree, gc, 256);
16481
16482   /* Allocate the initial hunk of the abbrev_die_table.  */
16483   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16484   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16485   /* Zero-th entry is allocated, but unused.  */
16486   abbrev_die_table_in_use = 1;
16487
16488   /* Allocate the initial hunk of the line_info_table.  */
16489   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16490   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16491
16492   /* Zero-th entry is allocated, but unused.  */
16493   line_info_table_in_use = 1;
16494
16495   /* Allocate the pubtypes and pubnames vectors.  */
16496   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16497   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16498
16499   /* Generate the initial DIE for the .debug section.  Note that the (string)
16500      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16501      will (typically) be a relative pathname and that this pathname should be
16502      taken as being relative to the directory from which the compiler was
16503      invoked when the given (base) source file was compiled.  We will fill
16504      in this value in dwarf2out_finish.  */
16505   comp_unit_die = gen_compile_unit_die (NULL);
16506
16507   incomplete_types = VEC_alloc (tree, gc, 64);
16508
16509   used_rtx_array = VEC_alloc (rtx, gc, 32);
16510
16511   debug_info_section = get_section (DEBUG_INFO_SECTION,
16512                                     SECTION_DEBUG, NULL);
16513   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16514                                       SECTION_DEBUG, NULL);
16515   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16516                                        SECTION_DEBUG, NULL);
16517   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16518                                        SECTION_DEBUG, NULL);
16519   debug_line_section = get_section (DEBUG_LINE_SECTION,
16520                                     SECTION_DEBUG, NULL);
16521   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16522                                    SECTION_DEBUG, NULL);
16523   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16524                                         SECTION_DEBUG, NULL);
16525   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16526                                         SECTION_DEBUG, NULL);
16527   debug_str_section = get_section (DEBUG_STR_SECTION,
16528                                    DEBUG_STR_SECTION_FLAGS, NULL);
16529   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16530                                       SECTION_DEBUG, NULL);
16531   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16532                                      SECTION_DEBUG, NULL);
16533
16534   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16535   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16536                                DEBUG_ABBREV_SECTION_LABEL, 0);
16537   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16538   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16539                                COLD_TEXT_SECTION_LABEL, 0);
16540   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16541
16542   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16543                                DEBUG_INFO_SECTION_LABEL, 0);
16544   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16545                                DEBUG_LINE_SECTION_LABEL, 0);
16546   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16547                                DEBUG_RANGES_SECTION_LABEL, 0);
16548   switch_to_section (debug_abbrev_section);
16549   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16550   switch_to_section (debug_info_section);
16551   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16552   switch_to_section (debug_line_section);
16553   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16554
16555   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16556     {
16557       switch_to_section (debug_macinfo_section);
16558       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16559                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16560       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16561     }
16562
16563   switch_to_section (text_section);
16564   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16565   if (flag_reorder_blocks_and_partition)
16566     {
16567       cold_text_section = unlikely_text_section ();
16568       switch_to_section (cold_text_section);
16569       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16570     }
16571 }
16572
16573 /* A helper function for dwarf2out_finish called through
16574    ht_forall.  Emit one queued .debug_str string.  */
16575
16576 static int
16577 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16578 {
16579   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16580
16581   if (node->form == DW_FORM_strp)
16582     {
16583       switch_to_section (debug_str_section);
16584       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16585       assemble_string (node->str, strlen (node->str) + 1);
16586     }
16587
16588   return 1;
16589 }
16590
16591 #if ENABLE_ASSERT_CHECKING
16592 /* Verify that all marks are clear.  */
16593
16594 static void
16595 verify_marks_clear (dw_die_ref die)
16596 {
16597   dw_die_ref c;
16598
16599   gcc_assert (! die->die_mark);
16600   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16601 }
16602 #endif /* ENABLE_ASSERT_CHECKING */
16603
16604 /* Clear the marks for a die and its children.
16605    Be cool if the mark isn't set.  */
16606
16607 static void
16608 prune_unmark_dies (dw_die_ref die)
16609 {
16610   dw_die_ref c;
16611
16612   if (die->die_mark)
16613     die->die_mark = 0;
16614   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16615 }
16616
16617 /* Given DIE that we're marking as used, find any other dies
16618    it references as attributes and mark them as used.  */
16619
16620 static void
16621 prune_unused_types_walk_attribs (dw_die_ref die)
16622 {
16623   dw_attr_ref a;
16624   unsigned ix;
16625
16626   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16627     {
16628       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16629         {
16630           /* A reference to another DIE.
16631              Make sure that it will get emitted.  */
16632           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16633         }
16634       /* Set the string's refcount to 0 so that prune_unused_types_mark
16635          accounts properly for it.  */
16636       if (AT_class (a) == dw_val_class_str)
16637         a->dw_attr_val.v.val_str->refcount = 0;
16638     }
16639 }
16640
16641
16642 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16643    to DIE's children.  */
16644
16645 static void
16646 prune_unused_types_mark (dw_die_ref die, int dokids)
16647 {
16648   dw_die_ref c;
16649
16650   if (die->die_mark == 0)
16651     {
16652       /* We haven't done this node yet.  Mark it as used.  */
16653       die->die_mark = 1;
16654
16655       /* We also have to mark its parents as used.
16656          (But we don't want to mark our parents' kids due to this.)  */
16657       if (die->die_parent)
16658         prune_unused_types_mark (die->die_parent, 0);
16659
16660       /* Mark any referenced nodes.  */
16661       prune_unused_types_walk_attribs (die);
16662
16663       /* If this node is a specification,
16664          also mark the definition, if it exists.  */
16665       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16666         prune_unused_types_mark (die->die_definition, 1);
16667     }
16668
16669   if (dokids && die->die_mark != 2)
16670     {
16671       /* We need to walk the children, but haven't done so yet.
16672          Remember that we've walked the kids.  */
16673       die->die_mark = 2;
16674
16675       /* If this is an array type, we need to make sure our
16676          kids get marked, even if they're types.  */
16677       if (die->die_tag == DW_TAG_array_type)
16678         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16679       else
16680         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16681     }
16682 }
16683
16684 /* For local classes, look if any static member functions were emitted
16685    and if so, mark them.  */
16686
16687 static void
16688 prune_unused_types_walk_local_classes (dw_die_ref die)
16689 {
16690   dw_die_ref c;
16691
16692   if (die->die_mark == 2)
16693     return;
16694
16695   switch (die->die_tag)
16696     {
16697     case DW_TAG_structure_type:
16698     case DW_TAG_union_type:
16699     case DW_TAG_class_type:
16700       break;
16701
16702     case DW_TAG_subprogram:
16703       if (!get_AT_flag (die, DW_AT_declaration)
16704           || die->die_definition != NULL)
16705         prune_unused_types_mark (die, 1);
16706       return;
16707
16708     default:
16709       return;
16710     }
16711
16712   /* Mark children.  */
16713   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16714 }
16715
16716 /* Walk the tree DIE and mark types that we actually use.  */
16717
16718 static void
16719 prune_unused_types_walk (dw_die_ref die)
16720 {
16721   dw_die_ref c;
16722
16723   /* Don't do anything if this node is already marked and
16724      children have been marked as well.  */
16725   if (die->die_mark == 2)
16726     return;
16727
16728   switch (die->die_tag)
16729     {
16730     case DW_TAG_structure_type:
16731     case DW_TAG_union_type:
16732     case DW_TAG_class_type:
16733       if (die->die_perennial_p)
16734         break;
16735
16736       for (c = die->die_parent; c; c = c->die_parent)
16737         if (c->die_tag == DW_TAG_subprogram)
16738           break;
16739
16740       /* Finding used static member functions inside of classes
16741          is needed just for local classes, because for other classes
16742          static member function DIEs with DW_AT_specification
16743          are emitted outside of the DW_TAG_*_type.  If we ever change
16744          it, we'd need to call this even for non-local classes.  */
16745       if (c)
16746         prune_unused_types_walk_local_classes (die);
16747
16748       /* It's a type node --- don't mark it.  */
16749       return;
16750
16751     case DW_TAG_const_type:
16752     case DW_TAG_packed_type:
16753     case DW_TAG_pointer_type:
16754     case DW_TAG_reference_type:
16755     case DW_TAG_volatile_type:
16756     case DW_TAG_typedef:
16757     case DW_TAG_array_type:
16758     case DW_TAG_interface_type:
16759     case DW_TAG_friend:
16760     case DW_TAG_variant_part:
16761     case DW_TAG_enumeration_type:
16762     case DW_TAG_subroutine_type:
16763     case DW_TAG_string_type:
16764     case DW_TAG_set_type:
16765     case DW_TAG_subrange_type:
16766     case DW_TAG_ptr_to_member_type:
16767     case DW_TAG_file_type:
16768       if (die->die_perennial_p)
16769         break;
16770
16771       /* It's a type node --- don't mark it.  */
16772       return;
16773
16774     default:
16775       /* Mark everything else.  */
16776       break;
16777   }
16778
16779   if (die->die_mark == 0)
16780     {
16781       die->die_mark = 1;
16782
16783       /* Now, mark any dies referenced from here.  */
16784       prune_unused_types_walk_attribs (die);
16785     }
16786
16787   die->die_mark = 2;
16788
16789   /* Mark children.  */
16790   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16791 }
16792
16793 /* Increment the string counts on strings referred to from DIE's
16794    attributes.  */
16795
16796 static void
16797 prune_unused_types_update_strings (dw_die_ref die)
16798 {
16799   dw_attr_ref a;
16800   unsigned ix;
16801
16802   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16803     if (AT_class (a) == dw_val_class_str)
16804       {
16805         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16806         s->refcount++;
16807         /* Avoid unnecessarily putting strings that are used less than
16808            twice in the hash table.  */
16809         if (s->refcount
16810             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16811           {
16812             void ** slot;
16813             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16814                                              htab_hash_string (s->str),
16815                                              INSERT);
16816             gcc_assert (*slot == NULL);
16817             *slot = s;
16818           }
16819       }
16820 }
16821
16822 /* Remove from the tree DIE any dies that aren't marked.  */
16823
16824 static void
16825 prune_unused_types_prune (dw_die_ref die)
16826 {
16827   dw_die_ref c;
16828
16829   gcc_assert (die->die_mark);
16830   prune_unused_types_update_strings (die);
16831
16832   if (! die->die_child)
16833     return;
16834
16835   c = die->die_child;
16836   do {
16837     dw_die_ref prev = c;
16838     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16839       if (c == die->die_child)
16840         {
16841           /* No marked children between 'prev' and the end of the list.  */
16842           if (prev == c)
16843             /* No marked children at all.  */
16844             die->die_child = NULL;
16845           else
16846             {
16847               prev->die_sib = c->die_sib;
16848               die->die_child = prev;
16849             }
16850           return;
16851         }
16852
16853     if (c != prev->die_sib)
16854       prev->die_sib = c;
16855     prune_unused_types_prune (c);
16856   } while (c != die->die_child);
16857 }
16858
16859
16860 /* Remove dies representing declarations that we never use.  */
16861
16862 static void
16863 prune_unused_types (void)
16864 {
16865   unsigned int i;
16866   limbo_die_node *node;
16867   pubname_ref pub;
16868
16869 #if ENABLE_ASSERT_CHECKING
16870   /* All the marks should already be clear.  */
16871   verify_marks_clear (comp_unit_die);
16872   for (node = limbo_die_list; node; node = node->next)
16873     verify_marks_clear (node->die);
16874 #endif /* ENABLE_ASSERT_CHECKING */
16875
16876   /* Set the mark on nodes that are actually used.  */
16877   prune_unused_types_walk (comp_unit_die);
16878   for (node = limbo_die_list; node; node = node->next)
16879     prune_unused_types_walk (node->die);
16880
16881   /* Also set the mark on nodes referenced from the
16882      pubname_table or arange_table.  */
16883   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16884     prune_unused_types_mark (pub->die, 1);
16885   for (i = 0; i < arange_table_in_use; i++)
16886     prune_unused_types_mark (arange_table[i], 1);
16887
16888   /* Get rid of nodes that aren't marked; and update the string counts.  */
16889   if (debug_str_hash)
16890     htab_empty (debug_str_hash);
16891   prune_unused_types_prune (comp_unit_die);
16892   for (node = limbo_die_list; node; node = node->next)
16893     prune_unused_types_prune (node->die);
16894
16895   /* Leave the marks clear.  */
16896   prune_unmark_dies (comp_unit_die);
16897   for (node = limbo_die_list; node; node = node->next)
16898     prune_unmark_dies (node->die);
16899 }
16900
16901 /* Set the parameter to true if there are any relative pathnames in
16902    the file table.  */
16903 static int
16904 file_table_relative_p (void ** slot, void *param)
16905 {
16906   bool *p = (bool *) param;
16907   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16908   if (!IS_ABSOLUTE_PATH (d->filename))
16909     {
16910       *p = true;
16911       return 0;
16912     }
16913   return 1;
16914 }
16915
16916 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
16917    to the location it would have been added, should we know its
16918    DECL_ASSEMBLER_NAME when we added other attributes.  This will
16919    probably improve compactness of debug info, removing equivalent
16920    abbrevs, and hide any differences caused by deferring the
16921    computation of the assembler name, triggered by e.g. PCH.  */
16922
16923 static inline void
16924 move_linkage_attr (dw_die_ref die)
16925 {
16926   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
16927   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
16928
16929   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
16930
16931   while (--ix > 0)
16932     {
16933       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
16934
16935       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
16936         break;
16937     }
16938
16939   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
16940     {
16941       VEC_pop (dw_attr_node, die->die_attr);
16942       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
16943     }
16944 }
16945
16946 /* Output stuff that dwarf requires at the end of every file,
16947    and generate the DWARF-2 debugging info.  */
16948
16949 static void
16950 dwarf2out_finish (const char *filename)
16951 {
16952   limbo_die_node *node, *next_node;
16953   dw_die_ref die = 0;
16954   unsigned int i;
16955
16956   /* Add the name for the main input file now.  We delayed this from
16957      dwarf2out_init to avoid complications with PCH.  */
16958   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16959   if (!IS_ABSOLUTE_PATH (filename))
16960     add_comp_dir_attribute (comp_unit_die);
16961   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16962     {
16963       bool p = false;
16964       htab_traverse (file_table, file_table_relative_p, &p);
16965       if (p)
16966         add_comp_dir_attribute (comp_unit_die);
16967     }
16968
16969   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16970     {
16971       add_location_or_const_value_attribute (
16972         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16973         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16974         DW_AT_location);
16975     }
16976
16977   /* Traverse the limbo die list, and add parent/child links.  The only
16978      dies without parents that should be here are concrete instances of
16979      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16980      For concrete instances, we can get the parent die from the abstract
16981      instance.  */
16982   for (node = limbo_die_list; node; node = next_node)
16983     {
16984       next_node = node->next;
16985       die = node->die;
16986
16987       if (die->die_parent == NULL)
16988         {
16989           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16990
16991           if (origin)
16992             add_child_die (origin->die_parent, die);
16993           else if (die == comp_unit_die)
16994             ;
16995           else if (errorcount > 0 || sorrycount > 0)
16996             /* It's OK to be confused by errors in the input.  */
16997             add_child_die (comp_unit_die, die);
16998           else
16999             {
17000               /* In certain situations, the lexical block containing a
17001                  nested function can be optimized away, which results
17002                  in the nested function die being orphaned.  Likewise
17003                  with the return type of that nested function.  Force
17004                  this to be a child of the containing function.
17005
17006                  It may happen that even the containing function got fully
17007                  inlined and optimized out.  In that case we are lost and
17008                  assign the empty child.  This should not be big issue as
17009                  the function is likely unreachable too.  */
17010               tree context = NULL_TREE;
17011
17012               gcc_assert (node->created_for);
17013
17014               if (DECL_P (node->created_for))
17015                 context = DECL_CONTEXT (node->created_for);
17016               else if (TYPE_P (node->created_for))
17017                 context = TYPE_CONTEXT (node->created_for);
17018
17019               gcc_assert (context
17020                           && (TREE_CODE (context) == FUNCTION_DECL
17021                               || TREE_CODE (context) == NAMESPACE_DECL));
17022
17023               origin = lookup_decl_die (context);
17024               if (origin)
17025                 add_child_die (origin, die);
17026               else
17027                 add_child_die (comp_unit_die, die);
17028             }
17029         }
17030     }
17031
17032   limbo_die_list = NULL;
17033
17034   for (node = deferred_asm_name; node; node = node->next)
17035     {
17036       tree decl = node->created_for;
17037       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17038         {
17039           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
17040                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
17041           move_linkage_attr (node->die);
17042         }
17043     }
17044
17045   deferred_asm_name = NULL;
17046
17047   /* Walk through the list of incomplete types again, trying once more to
17048      emit full debugging info for them.  */
17049   retry_incomplete_types ();
17050
17051   if (flag_eliminate_unused_debug_types)
17052     prune_unused_types ();
17053
17054   /* Generate separate CUs for each of the include files we've seen.
17055      They will go into limbo_die_list.  */
17056   if (flag_eliminate_dwarf2_dups)
17057     break_out_includes (comp_unit_die);
17058
17059   /* Traverse the DIE's and add add sibling attributes to those DIE's
17060      that have children.  */
17061   add_sibling_attributes (comp_unit_die);
17062   for (node = limbo_die_list; node; node = node->next)
17063     add_sibling_attributes (node->die);
17064
17065   /* Output a terminator label for the .text section.  */
17066   switch_to_section (text_section);
17067   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
17068   if (flag_reorder_blocks_and_partition)
17069     {
17070       switch_to_section (unlikely_text_section ());
17071       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17072     }
17073
17074   /* We can only use the low/high_pc attributes if all of the code was
17075      in .text.  */
17076   if (!have_multiple_function_sections)
17077     {
17078       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17079       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17080     }
17081
17082   else
17083     {
17084       unsigned fde_idx = 0;
17085
17086       /* We need to give .debug_loc and .debug_ranges an appropriate
17087          "base address".  Use zero so that these addresses become
17088          absolute.  Historically, we've emitted the unexpected
17089          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17090          Emit both to give time for other tools to adapt.  */
17091       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17092       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17093
17094       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17095                          add_ranges_by_labels (text_section_label,
17096                                                text_end_label));
17097       if (flag_reorder_blocks_and_partition)
17098         add_ranges_by_labels (cold_text_section_label,
17099                               cold_end_label);
17100
17101       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17102         {
17103           dw_fde_ref fde = &fde_table[fde_idx];
17104
17105           if (fde->dw_fde_switched_sections)
17106             {
17107               add_ranges_by_labels (fde->dw_fde_hot_section_label,
17108                                     fde->dw_fde_hot_section_end_label);
17109               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17110                                     fde->dw_fde_unlikely_section_end_label);
17111             }
17112           else
17113             add_ranges_by_labels (fde->dw_fde_begin,
17114                                   fde->dw_fde_end);
17115         }
17116
17117       add_ranges (NULL);
17118     }
17119
17120   /* Output location list section if necessary.  */
17121   if (have_location_lists)
17122     {
17123       /* Output the location lists info.  */
17124       switch_to_section (debug_loc_section);
17125       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17126                                    DEBUG_LOC_SECTION_LABEL, 0);
17127       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17128       output_location_lists (die);
17129     }
17130
17131   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17132     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17133                     debug_line_section_label);
17134
17135   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17136     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17137
17138   /* Output all of the compilation units.  We put the main one last so that
17139      the offsets are available to output_pubnames.  */
17140   for (node = limbo_die_list; node; node = node->next)
17141     output_comp_unit (node->die, 0);
17142
17143   /* Output the main compilation unit if non-empty or if .debug_macinfo
17144      has been emitted.  */
17145   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17146
17147   /* Output the abbreviation table.  */
17148   switch_to_section (debug_abbrev_section);
17149   output_abbrev_section ();
17150
17151   /* Output public names table if necessary.  */
17152   if (!VEC_empty (pubname_entry, pubname_table))
17153     {
17154       switch_to_section (debug_pubnames_section);
17155       output_pubnames (pubname_table);
17156     }
17157
17158   /* Output public types table if necessary.  */
17159   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
17160      It shouldn't hurt to emit it always, since pure DWARF2 consumers
17161      simply won't look for the section.  */
17162   if (!VEC_empty (pubname_entry, pubtype_table))
17163     {
17164       switch_to_section (debug_pubtypes_section);
17165       output_pubnames (pubtype_table);
17166     }
17167
17168   /* Output the address range information.  We only put functions in the arange
17169      table, so don't write it out if we don't have any.  */
17170   if (fde_table_in_use)
17171     {
17172       switch_to_section (debug_aranges_section);
17173       output_aranges ();
17174     }
17175
17176   /* Output ranges section if necessary.  */
17177   if (ranges_table_in_use)
17178     {
17179       switch_to_section (debug_ranges_section);
17180       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17181       output_ranges ();
17182     }
17183
17184   /* Output the source line correspondence table.  We must do this
17185      even if there is no line information.  Otherwise, on an empty
17186      translation unit, we will generate a present, but empty,
17187      .debug_info section.  IRIX 6.5 `nm' will then complain when
17188      examining the file.  This is done late so that any filenames
17189      used by the debug_info section are marked as 'used'.  */
17190   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17191     {
17192       switch_to_section (debug_line_section);
17193       output_line_info ();
17194     }
17195
17196   /* Have to end the macro section.  */
17197   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17198     {
17199       switch_to_section (debug_macinfo_section);
17200       dw2_asm_output_data (1, 0, "End compilation unit");
17201     }
17202
17203   /* If we emitted any DW_FORM_strp form attribute, output the string
17204      table too.  */
17205   if (debug_str_hash)
17206     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17207 }
17208 #else
17209
17210 /* This should never be used, but its address is needed for comparisons.  */
17211 const struct gcc_debug_hooks dwarf2_debug_hooks =
17212 {
17213   0,            /* init */
17214   0,            /* finish */
17215   0,            /* define */
17216   0,            /* undef */
17217   0,            /* start_source_file */
17218   0,            /* end_source_file */
17219   0,            /* begin_block */
17220   0,            /* end_block */
17221   0,            /* ignore_block */
17222   0,            /* source_line */
17223   0,            /* begin_prologue */
17224   0,            /* end_prologue */
17225   0,            /* end_epilogue */
17226   0,            /* begin_function */
17227   0,            /* end_function */
17228   0,            /* function_decl */
17229   0,            /* global_decl */
17230   0,            /* type_decl */
17231   0,            /* imported_module_or_decl */
17232   0,            /* deferred_inline_function */
17233   0,            /* outlining_inline_function */
17234   0,            /* label */
17235   0,            /* handle_pch */
17236   0,            /* var_location */
17237   0,            /* switch_text_section */
17238   0,            /* set_name */
17239   0             /* start_end_main_source_file */
17240 };
17241
17242 #endif /* DWARF2_DEBUGGING_INFO */
17243
17244 #include "gt-dwarf2out.h"