OSDN Git Service

gcc/cp/
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   unsigned all_throwers_are_sibcalls : 1;
243   unsigned nothrow : 1;
244   unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label.  */
249 #define MAX_ARTIFICIAL_LABEL_BYTES      30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252    Some architectures use word addresses to refer to code locations,
253    but Dwarf 2 info always uses byte addresses.  On such machines,
254    Dwarf 2 addresses need to be larger than the architecture's
255    pointers.  */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261    relative to a debug info section, specified to be 4 bytes in the
262    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
263    as PTR_SIZE.  */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
271    bytes are 0xffffffff, followed by the length stored in the next 8
272    bytes.
273
274    However, the SGI/MIPS ABI uses an initial length which is equal to
275    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY.  */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier.  */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305    information for each routine.  */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table.  */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use.  */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315    fde_table.  */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* Get the current fde_table entry we should use.  */
319
320 static inline dw_fde_ref
321 current_fde (void)
322 {
323   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
324 }
325
326 /* A list of call frame insns for the CIE.  */
327 static GTY(()) dw_cfi_ref cie_cfi_head;
328
329 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
330 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
331    attribute that accelerates the lookup of the FDE associated
332    with the subprogram.  This variable holds the table index of the FDE
333    associated with the current function (body) definition.  */
334 static unsigned current_funcdef_fde;
335 #endif
336
337 struct indirect_string_node GTY(())
338 {
339   const char *str;
340   unsigned int refcount;
341   unsigned int form;
342   char *label;
343 };
344
345 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
346
347 static GTY(()) int dw2_string_counter;
348 static GTY(()) unsigned long dwarf2out_cfi_label_num;
349
350 /* True if the compilation unit places functions in more than one section.  */
351 static GTY(()) bool have_multiple_function_sections = false;
352
353 /* Whether the default text and cold text sections have been used at all.  */
354
355 static GTY(()) bool text_section_used = false;
356 static GTY(()) bool cold_text_section_used = false;
357
358 /* The default cold text section.  */
359 static GTY(()) section *cold_text_section;
360
361 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
362
363 /* Forward declarations for functions defined in this file.  */
364
365 static char *stripattributes (const char *);
366 static const char *dwarf_cfi_name (unsigned);
367 static dw_cfi_ref new_cfi (void);
368 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
369 static void add_fde_cfi (const char *, dw_cfi_ref);
370 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
371 static void lookup_cfa (dw_cfa_location *);
372 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
373 #ifdef DWARF2_UNWIND_INFO
374 static void initial_return_save (rtx);
375 #endif
376 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
377 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
378 static void output_call_frame_info (int);
379 static void dwarf2out_note_section_used (void);
380 static void dwarf2out_stack_adjust (rtx, bool);
381 static void flush_queued_reg_saves (void);
382 static bool clobbers_queued_reg_save (const_rtx);
383 static void dwarf2out_frame_debug_expr (rtx, const char *);
384
385 /* Support for complex CFA locations.  */
386 static void output_cfa_loc (dw_cfi_ref);
387 static void get_cfa_from_loc_descr (dw_cfa_location *,
388                                     struct dw_loc_descr_struct *);
389 static struct dw_loc_descr_struct *build_cfa_loc
390   (dw_cfa_location *, HOST_WIDE_INT);
391 static void def_cfa_1 (const char *, dw_cfa_location *);
392
393 /* How to start an assembler comment.  */
394 #ifndef ASM_COMMENT_START
395 #define ASM_COMMENT_START ";#"
396 #endif
397
398 /* Data and reference forms for relocatable data.  */
399 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
400 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
401
402 #ifndef DEBUG_FRAME_SECTION
403 #define DEBUG_FRAME_SECTION     ".debug_frame"
404 #endif
405
406 #ifndef FUNC_BEGIN_LABEL
407 #define FUNC_BEGIN_LABEL        "LFB"
408 #endif
409
410 #ifndef FUNC_END_LABEL
411 #define FUNC_END_LABEL          "LFE"
412 #endif
413
414 #ifndef FRAME_BEGIN_LABEL
415 #define FRAME_BEGIN_LABEL       "Lframe"
416 #endif
417 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
418 #define CIE_END_LABEL           "LECIE"
419 #define FDE_LABEL               "LSFDE"
420 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
421 #define FDE_END_LABEL           "LEFDE"
422 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
423 #define LINE_NUMBER_END_LABEL   "LELT"
424 #define LN_PROLOG_AS_LABEL      "LASLTP"
425 #define LN_PROLOG_END_LABEL     "LELTP"
426 #define DIE_LABEL_PREFIX        "DW"
427
428 /* The DWARF 2 CFA column which tracks the return address.  Normally this
429    is the column for PC, or the first column after all of the hard
430    registers.  */
431 #ifndef DWARF_FRAME_RETURN_COLUMN
432 #ifdef PC_REGNUM
433 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
434 #else
435 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
436 #endif
437 #endif
438
439 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
440    default, we just provide columns for all registers.  */
441 #ifndef DWARF_FRAME_REGNUM
442 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
443 #endif
444 \f
445 /* Hook used by __throw.  */
446
447 rtx
448 expand_builtin_dwarf_sp_column (void)
449 {
450   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
451   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
452 }
453
454 /* Return a pointer to a copy of the section string name S with all
455    attributes stripped off, and an asterisk prepended (for assemble_name).  */
456
457 static inline char *
458 stripattributes (const char *s)
459 {
460   char *stripped = XNEWVEC (char, strlen (s) + 2);
461   char *p = stripped;
462
463   *p++ = '*';
464
465   while (*s && *s != ',')
466     *p++ = *s++;
467
468   *p = '\0';
469   return stripped;
470 }
471
472 /* MEM is a memory reference for the register size table, each element of
473    which has mode MODE.  Initialize column C as a return address column.  */
474
475 static void
476 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
477 {
478   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
479   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
480   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
481 }
482
483 /* Generate code to initialize the register size table.  */
484
485 void
486 expand_builtin_init_dwarf_reg_sizes (tree address)
487 {
488   unsigned int i;
489   enum machine_mode mode = TYPE_MODE (char_type_node);
490   rtx addr = expand_normal (address);
491   rtx mem = gen_rtx_MEM (BLKmode, addr);
492   bool wrote_return_column = false;
493
494   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
495     {
496       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
497
498       if (rnum < DWARF_FRAME_REGISTERS)
499         {
500           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
501           enum machine_mode save_mode = reg_raw_mode[i];
502           HOST_WIDE_INT size;
503
504           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
505             save_mode = choose_hard_reg_mode (i, 1, true);
506           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
507             {
508               if (save_mode == VOIDmode)
509                 continue;
510               wrote_return_column = true;
511             }
512           size = GET_MODE_SIZE (save_mode);
513           if (offset < 0)
514             continue;
515
516           emit_move_insn (adjust_address (mem, mode, offset),
517                           gen_int_mode (size, mode));
518         }
519     }
520
521   if (!wrote_return_column)
522     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
523
524 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
525   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
526 #endif
527
528   targetm.init_dwarf_reg_sizes_extra (address);
529 }
530
531 /* Convert a DWARF call frame info. operation to its string name */
532
533 static const char *
534 dwarf_cfi_name (unsigned int cfi_opc)
535 {
536   switch (cfi_opc)
537     {
538     case DW_CFA_advance_loc:
539       return "DW_CFA_advance_loc";
540     case DW_CFA_offset:
541       return "DW_CFA_offset";
542     case DW_CFA_restore:
543       return "DW_CFA_restore";
544     case DW_CFA_nop:
545       return "DW_CFA_nop";
546     case DW_CFA_set_loc:
547       return "DW_CFA_set_loc";
548     case DW_CFA_advance_loc1:
549       return "DW_CFA_advance_loc1";
550     case DW_CFA_advance_loc2:
551       return "DW_CFA_advance_loc2";
552     case DW_CFA_advance_loc4:
553       return "DW_CFA_advance_loc4";
554     case DW_CFA_offset_extended:
555       return "DW_CFA_offset_extended";
556     case DW_CFA_restore_extended:
557       return "DW_CFA_restore_extended";
558     case DW_CFA_undefined:
559       return "DW_CFA_undefined";
560     case DW_CFA_same_value:
561       return "DW_CFA_same_value";
562     case DW_CFA_register:
563       return "DW_CFA_register";
564     case DW_CFA_remember_state:
565       return "DW_CFA_remember_state";
566     case DW_CFA_restore_state:
567       return "DW_CFA_restore_state";
568     case DW_CFA_def_cfa:
569       return "DW_CFA_def_cfa";
570     case DW_CFA_def_cfa_register:
571       return "DW_CFA_def_cfa_register";
572     case DW_CFA_def_cfa_offset:
573       return "DW_CFA_def_cfa_offset";
574
575     /* DWARF 3 */
576     case DW_CFA_def_cfa_expression:
577       return "DW_CFA_def_cfa_expression";
578     case DW_CFA_expression:
579       return "DW_CFA_expression";
580     case DW_CFA_offset_extended_sf:
581       return "DW_CFA_offset_extended_sf";
582     case DW_CFA_def_cfa_sf:
583       return "DW_CFA_def_cfa_sf";
584     case DW_CFA_def_cfa_offset_sf:
585       return "DW_CFA_def_cfa_offset_sf";
586
587     /* SGI/MIPS specific */
588     case DW_CFA_MIPS_advance_loc8:
589       return "DW_CFA_MIPS_advance_loc8";
590
591     /* GNU extensions */
592     case DW_CFA_GNU_window_save:
593       return "DW_CFA_GNU_window_save";
594     case DW_CFA_GNU_args_size:
595       return "DW_CFA_GNU_args_size";
596     case DW_CFA_GNU_negative_offset_extended:
597       return "DW_CFA_GNU_negative_offset_extended";
598
599     default:
600       return "DW_CFA_<unknown>";
601     }
602 }
603
604 /* Return a pointer to a newly allocated Call Frame Instruction.  */
605
606 static inline dw_cfi_ref
607 new_cfi (void)
608 {
609   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
610
611   cfi->dw_cfi_next = NULL;
612   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
613   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
614
615   return cfi;
616 }
617
618 /* Add a Call Frame Instruction to list of instructions.  */
619
620 static inline void
621 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
622 {
623   dw_cfi_ref *p;
624
625   /* Find the end of the chain.  */
626   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
627     ;
628
629   *p = cfi;
630 }
631
632 /* Generate a new label for the CFI info to refer to.  */
633
634 char *
635 dwarf2out_cfi_label (void)
636 {
637   static char label[20];
638
639   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
640   ASM_OUTPUT_LABEL (asm_out_file, label);
641   return label;
642 }
643
644 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
645    or to the CIE if LABEL is NULL.  */
646
647 static void
648 add_fde_cfi (const char *label, dw_cfi_ref cfi)
649 {
650   if (label)
651     {
652       dw_fde_ref fde = current_fde ();
653
654       gcc_assert (fde != NULL);
655
656       if (*label == 0)
657         label = dwarf2out_cfi_label ();
658
659       if (fde->dw_fde_current_label == NULL
660           || strcmp (label, fde->dw_fde_current_label) != 0)
661         {
662           dw_cfi_ref xcfi;
663
664           label = xstrdup (label);
665
666           /* Set the location counter to the new label.  */
667           xcfi = new_cfi ();
668           /* If we have a current label, advance from there, otherwise
669              set the location directly using set_loc.  */
670           xcfi->dw_cfi_opc = fde->dw_fde_current_label
671                              ? DW_CFA_advance_loc4
672                              : DW_CFA_set_loc;
673           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
674           add_cfi (&fde->dw_fde_cfi, xcfi);
675
676           fde->dw_fde_current_label = label;
677         }
678
679       add_cfi (&fde->dw_fde_cfi, cfi);
680     }
681
682   else
683     add_cfi (&cie_cfi_head, cfi);
684 }
685
686 /* Subroutine of lookup_cfa.  */
687
688 static void
689 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
690 {
691   switch (cfi->dw_cfi_opc)
692     {
693     case DW_CFA_def_cfa_offset:
694       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
695       break;
696     case DW_CFA_def_cfa_offset_sf:
697       loc->offset
698         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
699       break;
700     case DW_CFA_def_cfa_register:
701       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
702       break;
703     case DW_CFA_def_cfa:
704       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
705       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
706       break;
707     case DW_CFA_def_cfa_sf:
708       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
709       loc->offset
710         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
711       break;
712     case DW_CFA_def_cfa_expression:
713       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
714       break;
715     default:
716       break;
717     }
718 }
719
720 /* Find the previous value for the CFA.  */
721
722 static void
723 lookup_cfa (dw_cfa_location *loc)
724 {
725   dw_cfi_ref cfi;
726   dw_fde_ref fde;
727
728   loc->reg = INVALID_REGNUM;
729   loc->offset = 0;
730   loc->indirect = 0;
731   loc->base_offset = 0;
732
733   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
734     lookup_cfa_1 (cfi, loc);
735
736   fde = current_fde ();
737   if (fde)
738     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
739       lookup_cfa_1 (cfi, loc);
740 }
741
742 /* The current rule for calculating the DWARF2 canonical frame address.  */
743 static dw_cfa_location cfa;
744
745 /* The register used for saving registers to the stack, and its offset
746    from the CFA.  */
747 static dw_cfa_location cfa_store;
748
749 /* The running total of the size of arguments pushed onto the stack.  */
750 static HOST_WIDE_INT args_size;
751
752 /* The last args_size we actually output.  */
753 static HOST_WIDE_INT old_args_size;
754
755 /* Entry point to update the canonical frame address (CFA).
756    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
757    calculated from REG+OFFSET.  */
758
759 void
760 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
761 {
762   dw_cfa_location loc;
763   loc.indirect = 0;
764   loc.base_offset = 0;
765   loc.reg = reg;
766   loc.offset = offset;
767   def_cfa_1 (label, &loc);
768 }
769
770 /* Determine if two dw_cfa_location structures define the same data.  */
771
772 static bool
773 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
774 {
775   return (loc1->reg == loc2->reg
776           && loc1->offset == loc2->offset
777           && loc1->indirect == loc2->indirect
778           && (loc1->indirect == 0
779               || loc1->base_offset == loc2->base_offset));
780 }
781
782 /* This routine does the actual work.  The CFA is now calculated from
783    the dw_cfa_location structure.  */
784
785 static void
786 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
787 {
788   dw_cfi_ref cfi;
789   dw_cfa_location old_cfa, loc;
790
791   cfa = *loc_p;
792   loc = *loc_p;
793
794   if (cfa_store.reg == loc.reg && loc.indirect == 0)
795     cfa_store.offset = loc.offset;
796
797   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
798   lookup_cfa (&old_cfa);
799
800   /* If nothing changed, no need to issue any call frame instructions.  */
801   if (cfa_equal_p (&loc, &old_cfa))
802     return;
803
804   cfi = new_cfi ();
805
806   if (loc.reg == old_cfa.reg && !loc.indirect)
807     {
808       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
809          the CFA register did not change but the offset did.  */
810       if (loc.offset < 0)
811         {
812           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
813           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
814
815           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
816           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
817         }
818       else
819         {
820           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
821           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
822         }
823     }
824
825 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
826   else if (loc.offset == old_cfa.offset
827            && old_cfa.reg != INVALID_REGNUM
828            && !loc.indirect)
829     {
830       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
831          indicating the CFA register has changed to <register> but the
832          offset has not changed.  */
833       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
834       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835     }
836 #endif
837
838   else if (loc.indirect == 0)
839     {
840       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
841          indicating the CFA register has changed to <register> with
842          the specified offset.  */
843       if (loc.offset < 0)
844         {
845           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
846           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
847
848           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
849           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
850           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
851         }
852       else
853         {
854           cfi->dw_cfi_opc = DW_CFA_def_cfa;
855           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
856           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
857         }
858     }
859   else
860     {
861       /* Construct a DW_CFA_def_cfa_expression instruction to
862          calculate the CFA using a full location expression since no
863          register-offset pair is available.  */
864       struct dw_loc_descr_struct *loc_list;
865
866       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
867       loc_list = build_cfa_loc (&loc, 0);
868       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
869     }
870
871   add_fde_cfi (label, cfi);
872 }
873
874 /* Add the CFI for saving a register.  REG is the CFA column number.
875    LABEL is passed to add_fde_cfi.
876    If SREG is -1, the register is saved at OFFSET from the CFA;
877    otherwise it is saved in SREG.  */
878
879 static void
880 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
881 {
882   dw_cfi_ref cfi = new_cfi ();
883
884   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
885
886   if (sreg == INVALID_REGNUM)
887     {
888       if (reg & ~0x3f)
889         /* The register number won't fit in 6 bits, so we have to use
890            the long form.  */
891         cfi->dw_cfi_opc = DW_CFA_offset_extended;
892       else
893         cfi->dw_cfi_opc = DW_CFA_offset;
894
895 #ifdef ENABLE_CHECKING
896       {
897         /* If we get an offset that is not a multiple of
898            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
899            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
900            description.  */
901         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
902
903         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
904       }
905 #endif
906       offset /= DWARF_CIE_DATA_ALIGNMENT;
907       if (offset < 0)
908         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
909
910       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
911     }
912   else if (sreg == reg)
913     cfi->dw_cfi_opc = DW_CFA_same_value;
914   else
915     {
916       cfi->dw_cfi_opc = DW_CFA_register;
917       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
918     }
919
920   add_fde_cfi (label, cfi);
921 }
922
923 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
924    This CFI tells the unwinder that it needs to restore the window registers
925    from the previous frame's window save area.
926
927    ??? Perhaps we should note in the CIE where windows are saved (instead of
928    assuming 0(cfa)) and what registers are in the window.  */
929
930 void
931 dwarf2out_window_save (const char *label)
932 {
933   dw_cfi_ref cfi = new_cfi ();
934
935   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
936   add_fde_cfi (label, cfi);
937 }
938
939 /* Add a CFI to update the running total of the size of arguments
940    pushed onto the stack.  */
941
942 void
943 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
944 {
945   dw_cfi_ref cfi;
946
947   if (size == old_args_size)
948     return;
949
950   old_args_size = size;
951
952   cfi = new_cfi ();
953   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
954   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
955   add_fde_cfi (label, cfi);
956 }
957
958 /* Entry point for saving a register to the stack.  REG is the GCC register
959    number.  LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in the stack.
968    LABEL and OFFSET are passed to reg_save.  */
969
970 void
971 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
974 }
975
976 /* Entry point for saving the return address in a register.
977    LABEL and SREG are passed to reg_save.  */
978
979 void
980 dwarf2out_return_reg (const char *label, unsigned int sreg)
981 {
982   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
983 }
984
985 #ifdef DWARF2_UNWIND_INFO
986 /* Record the initial position of the return address.  RTL is
987    INCOMING_RETURN_ADDR_RTX.  */
988
989 static void
990 initial_return_save (rtx rtl)
991 {
992   unsigned int reg = INVALID_REGNUM;
993   HOST_WIDE_INT offset = 0;
994
995   switch (GET_CODE (rtl))
996     {
997     case REG:
998       /* RA is in a register.  */
999       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1000       break;
1001
1002     case MEM:
1003       /* RA is on the stack.  */
1004       rtl = XEXP (rtl, 0);
1005       switch (GET_CODE (rtl))
1006         {
1007         case REG:
1008           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1009           offset = 0;
1010           break;
1011
1012         case PLUS:
1013           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014           offset = INTVAL (XEXP (rtl, 1));
1015           break;
1016
1017         case MINUS:
1018           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1019           offset = -INTVAL (XEXP (rtl, 1));
1020           break;
1021
1022         default:
1023           gcc_unreachable ();
1024         }
1025
1026       break;
1027
1028     case PLUS:
1029       /* The return address is at some offset from any value we can
1030          actually load.  For instance, on the SPARC it is in %i7+8. Just
1031          ignore the offset for now; it doesn't matter for unwinding frames.  */
1032       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1033       initial_return_save (XEXP (rtl, 0));
1034       return;
1035
1036     default:
1037       gcc_unreachable ();
1038     }
1039
1040   if (reg != DWARF_FRAME_RETURN_COLUMN)
1041     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1042 }
1043 #endif
1044
1045 /* Given a SET, calculate the amount of stack adjustment it
1046    contains.  */
1047
1048 static HOST_WIDE_INT
1049 stack_adjust_offset (const_rtx pattern)
1050 {
1051   const_rtx src = SET_SRC (pattern);
1052   const_rtx dest = SET_DEST (pattern);
1053   HOST_WIDE_INT offset = 0;
1054   enum rtx_code code;
1055
1056   if (dest == stack_pointer_rtx)
1057     {
1058       /* (set (reg sp) (plus (reg sp) (const_int))) */
1059       code = GET_CODE (src);
1060       if (! (code == PLUS || code == MINUS)
1061           || XEXP (src, 0) != stack_pointer_rtx
1062           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1063         return 0;
1064
1065       offset = INTVAL (XEXP (src, 1));
1066       if (code == PLUS)
1067         offset = -offset;
1068     }
1069   else if (MEM_P (dest))
1070     {
1071       /* (set (mem (pre_dec (reg sp))) (foo)) */
1072       src = XEXP (dest, 0);
1073       code = GET_CODE (src);
1074
1075       switch (code)
1076         {
1077         case PRE_MODIFY:
1078         case POST_MODIFY:
1079           if (XEXP (src, 0) == stack_pointer_rtx)
1080             {
1081               rtx val = XEXP (XEXP (src, 1), 1);
1082               /* We handle only adjustments by constant amount.  */
1083               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1084                           && GET_CODE (val) == CONST_INT);
1085               offset = -INTVAL (val);
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_DEC:
1091         case POST_DEC:
1092           if (XEXP (src, 0) == stack_pointer_rtx)
1093             {
1094               offset = GET_MODE_SIZE (GET_MODE (dest));
1095               break;
1096             }
1097           return 0;
1098
1099         case PRE_INC:
1100         case POST_INC:
1101           if (XEXP (src, 0) == stack_pointer_rtx)
1102             {
1103               offset = -GET_MODE_SIZE (GET_MODE (dest));
1104               break;
1105             }
1106           return 0;
1107
1108         default:
1109           return 0;
1110         }
1111     }
1112   else
1113     return 0;
1114
1115   return offset;
1116 }
1117
1118 /* Check INSN to see if it looks like a push or a stack adjustment, and
1119    make a note of it if it does.  EH uses this information to find out how
1120    much extra space it needs to pop off the stack.  */
1121
1122 static void
1123 dwarf2out_stack_adjust (rtx insn, bool after_p)
1124 {
1125   HOST_WIDE_INT offset;
1126   const char *label;
1127   int i;
1128
1129   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1130      with this function.  Proper support would require all frame-related
1131      insns to be marked, and to be able to handle saving state around
1132      epilogues textually in the middle of the function.  */
1133   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1134     return;
1135
1136   /* If only calls can throw, and we have a frame pointer,
1137      save up adjustments until we see the CALL_INSN.  */
1138   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1139     {
1140       if (CALL_P (insn) && !after_p)
1141         {
1142           /* Extract the size of the args from the CALL rtx itself.  */
1143           insn = PATTERN (insn);
1144           if (GET_CODE (insn) == PARALLEL)
1145             insn = XVECEXP (insn, 0, 0);
1146           if (GET_CODE (insn) == SET)
1147             insn = SET_SRC (insn);
1148           gcc_assert (GET_CODE (insn) == CALL);
1149           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1150         }
1151       return;
1152     }
1153
1154   if (CALL_P (insn) && !after_p)
1155     {
1156       if (!flag_asynchronous_unwind_tables)
1157         dwarf2out_args_size ("", args_size);
1158       return;
1159     }
1160   else if (BARRIER_P (insn))
1161     {
1162       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1163          the compiler will have already emitted a stack adjustment, but
1164          doesn't bother for calls to noreturn functions.  */
1165 #ifdef STACK_GROWS_DOWNWARD
1166       offset = -args_size;
1167 #else
1168       offset = args_size;
1169 #endif
1170     }
1171   else if (GET_CODE (PATTERN (insn)) == SET)
1172     offset = stack_adjust_offset (PATTERN (insn));
1173   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1174            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1175     {
1176       /* There may be stack adjustments inside compound insns.  Search
1177          for them.  */
1178       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1179         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1180           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1181     }
1182   else
1183     return;
1184
1185   if (offset == 0)
1186     return;
1187
1188   if (cfa.reg == STACK_POINTER_REGNUM)
1189     cfa.offset += offset;
1190
1191 #ifndef STACK_GROWS_DOWNWARD
1192   offset = -offset;
1193 #endif
1194
1195   args_size += offset;
1196   if (args_size < 0)
1197     args_size = 0;
1198
1199   label = dwarf2out_cfi_label ();
1200   def_cfa_1 (label, &cfa);
1201   if (flag_asynchronous_unwind_tables)
1202     dwarf2out_args_size (label, args_size);
1203 }
1204
1205 #endif
1206
1207 /* We delay emitting a register save until either (a) we reach the end
1208    of the prologue or (b) the register is clobbered.  This clusters
1209    register saves so that there are fewer pc advances.  */
1210
1211 struct queued_reg_save GTY(())
1212 {
1213   struct queued_reg_save *next;
1214   rtx reg;
1215   HOST_WIDE_INT cfa_offset;
1216   rtx saved_reg;
1217 };
1218
1219 static GTY(()) struct queued_reg_save *queued_reg_saves;
1220
1221 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1222 struct reg_saved_in_data GTY(()) {
1223   rtx orig_reg;
1224   rtx saved_in_reg;
1225 };
1226
1227 /* A list of registers saved in other registers.
1228    The list intentionally has a small maximum capacity of 4; if your
1229    port needs more than that, you might consider implementing a
1230    more efficient data structure.  */
1231 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1232 static GTY(()) size_t num_regs_saved_in_regs;
1233
1234 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1235 static const char *last_reg_save_label;
1236
1237 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1238    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1239
1240 static void
1241 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1242 {
1243   struct queued_reg_save *q;
1244
1245   /* Duplicates waste space, but it's also necessary to remove them
1246      for correctness, since the queue gets output in reverse
1247      order.  */
1248   for (q = queued_reg_saves; q != NULL; q = q->next)
1249     if (REGNO (q->reg) == REGNO (reg))
1250       break;
1251
1252   if (q == NULL)
1253     {
1254       q = GGC_NEW (struct queued_reg_save);
1255       q->next = queued_reg_saves;
1256       queued_reg_saves = q;
1257     }
1258
1259   q->reg = reg;
1260   q->cfa_offset = offset;
1261   q->saved_reg = sreg;
1262
1263   last_reg_save_label = label;
1264 }
1265
1266 /* Output all the entries in QUEUED_REG_SAVES.  */
1267
1268 static void
1269 flush_queued_reg_saves (void)
1270 {
1271   struct queued_reg_save *q;
1272
1273   for (q = queued_reg_saves; q; q = q->next)
1274     {
1275       size_t i;
1276       unsigned int reg, sreg;
1277
1278       for (i = 0; i < num_regs_saved_in_regs; i++)
1279         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1280           break;
1281       if (q->saved_reg && i == num_regs_saved_in_regs)
1282         {
1283           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1284           num_regs_saved_in_regs++;
1285         }
1286       if (i != num_regs_saved_in_regs)
1287         {
1288           regs_saved_in_regs[i].orig_reg = q->reg;
1289           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1290         }
1291
1292       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1293       if (q->saved_reg)
1294         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1295       else
1296         sreg = INVALID_REGNUM;
1297       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1298     }
1299
1300   queued_reg_saves = NULL;
1301   last_reg_save_label = NULL;
1302 }
1303
1304 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1305    location for?  Or, does it clobber a register which we've previously
1306    said that some other register is saved in, and for which we now
1307    have a new location for?  */
1308
1309 static bool
1310 clobbers_queued_reg_save (const_rtx insn)
1311 {
1312   struct queued_reg_save *q;
1313
1314   for (q = queued_reg_saves; q; q = q->next)
1315     {
1316       size_t i;
1317       if (modified_in_p (q->reg, insn))
1318         return true;
1319       for (i = 0; i < num_regs_saved_in_regs; i++)
1320         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1321             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1322           return true;
1323     }
1324
1325   return false;
1326 }
1327
1328 /* Entry point for saving the first register into the second.  */
1329
1330 void
1331 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1332 {
1333   size_t i;
1334   unsigned int regno, sregno;
1335
1336   for (i = 0; i < num_regs_saved_in_regs; i++)
1337     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1338       break;
1339   if (i == num_regs_saved_in_regs)
1340     {
1341       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1342       num_regs_saved_in_regs++;
1343     }
1344   regs_saved_in_regs[i].orig_reg = reg;
1345   regs_saved_in_regs[i].saved_in_reg = sreg;
1346
1347   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1348   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1349   reg_save (label, regno, sregno, 0);
1350 }
1351
1352 /* What register, if any, is currently saved in REG?  */
1353
1354 static rtx
1355 reg_saved_in (rtx reg)
1356 {
1357   unsigned int regn = REGNO (reg);
1358   size_t i;
1359   struct queued_reg_save *q;
1360
1361   for (q = queued_reg_saves; q; q = q->next)
1362     if (q->saved_reg && regn == REGNO (q->saved_reg))
1363       return q->reg;
1364
1365   for (i = 0; i < num_regs_saved_in_regs; i++)
1366     if (regs_saved_in_regs[i].saved_in_reg
1367         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1368       return regs_saved_in_regs[i].orig_reg;
1369
1370   return NULL_RTX;
1371 }
1372
1373
1374 /* A temporary register holding an integral value used in adjusting SP
1375    or setting up the store_reg.  The "offset" field holds the integer
1376    value, not an offset.  */
1377 static dw_cfa_location cfa_temp;
1378
1379 /* Record call frame debugging information for an expression EXPR,
1380    which either sets SP or FP (adjusting how we calculate the frame
1381    address) or saves a register to the stack or another register.
1382    LABEL indicates the address of EXPR.
1383
1384    This function encodes a state machine mapping rtxes to actions on
1385    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1386    users need not read the source code.
1387
1388   The High-Level Picture
1389
1390   Changes in the register we use to calculate the CFA: Currently we
1391   assume that if you copy the CFA register into another register, we
1392   should take the other one as the new CFA register; this seems to
1393   work pretty well.  If it's wrong for some target, it's simple
1394   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1395
1396   Changes in the register we use for saving registers to the stack:
1397   This is usually SP, but not always.  Again, we deduce that if you
1398   copy SP into another register (and SP is not the CFA register),
1399   then the new register is the one we will be using for register
1400   saves.  This also seems to work.
1401
1402   Register saves: There's not much guesswork about this one; if
1403   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1404   register save, and the register used to calculate the destination
1405   had better be the one we think we're using for this purpose.
1406   It's also assumed that a copy from a call-saved register to another
1407   register is saving that register if RTX_FRAME_RELATED_P is set on
1408   that instruction.  If the copy is from a call-saved register to
1409   the *same* register, that means that the register is now the same
1410   value as in the caller.
1411
1412   Except: If the register being saved is the CFA register, and the
1413   offset is nonzero, we are saving the CFA, so we assume we have to
1414   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1415   the intent is to save the value of SP from the previous frame.
1416
1417   In addition, if a register has previously been saved to a different
1418   register,
1419
1420   Invariants / Summaries of Rules
1421
1422   cfa          current rule for calculating the CFA.  It usually
1423                consists of a register and an offset.
1424   cfa_store    register used by prologue code to save things to the stack
1425                cfa_store.offset is the offset from the value of
1426                cfa_store.reg to the actual CFA
1427   cfa_temp     register holding an integral value.  cfa_temp.offset
1428                stores the value, which will be used to adjust the
1429                stack pointer.  cfa_temp is also used like cfa_store,
1430                to track stores to the stack via fp or a temp reg.
1431
1432   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1433                with cfa.reg as the first operand changes the cfa.reg and its
1434                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1435                cfa_temp.offset.
1436
1437   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1438                expression yielding a constant.  This sets cfa_temp.reg
1439                and cfa_temp.offset.
1440
1441   Rule 5:      Create a new register cfa_store used to save items to the
1442                stack.
1443
1444   Rules 10-14: Save a register to the stack.  Define offset as the
1445                difference of the original location and cfa_store's
1446                location (or cfa_temp's location if cfa_temp is used).
1447
1448   The Rules
1449
1450   "{a,b}" indicates a choice of a xor b.
1451   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1452
1453   Rule 1:
1454   (set <reg1> <reg2>:cfa.reg)
1455   effects: cfa.reg = <reg1>
1456            cfa.offset unchanged
1457            cfa_temp.reg = <reg1>
1458            cfa_temp.offset = cfa.offset
1459
1460   Rule 2:
1461   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1462                               {<const_int>,<reg>:cfa_temp.reg}))
1463   effects: cfa.reg = sp if fp used
1464            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1465            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1466              if cfa_store.reg==sp
1467
1468   Rule 3:
1469   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1470   effects: cfa.reg = fp
1471            cfa_offset += +/- <const_int>
1472
1473   Rule 4:
1474   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1475   constraints: <reg1> != fp
1476                <reg1> != sp
1477   effects: cfa.reg = <reg1>
1478            cfa_temp.reg = <reg1>
1479            cfa_temp.offset = cfa.offset
1480
1481   Rule 5:
1482   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1483   constraints: <reg1> != fp
1484                <reg1> != sp
1485   effects: cfa_store.reg = <reg1>
1486            cfa_store.offset = cfa.offset - cfa_temp.offset
1487
1488   Rule 6:
1489   (set <reg> <const_int>)
1490   effects: cfa_temp.reg = <reg>
1491            cfa_temp.offset = <const_int>
1492
1493   Rule 7:
1494   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1495   effects: cfa_temp.reg = <reg1>
1496            cfa_temp.offset |= <const_int>
1497
1498   Rule 8:
1499   (set <reg> (high <exp>))
1500   effects: none
1501
1502   Rule 9:
1503   (set <reg> (lo_sum <exp> <const_int>))
1504   effects: cfa_temp.reg = <reg>
1505            cfa_temp.offset = <const_int>
1506
1507   Rule 10:
1508   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1509   effects: cfa_store.offset -= <const_int>
1510            cfa.offset = cfa_store.offset if cfa.reg == sp
1511            cfa.reg = sp
1512            cfa.base_offset = -cfa_store.offset
1513
1514   Rule 11:
1515   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1516   effects: cfa_store.offset += -/+ mode_size(mem)
1517            cfa.offset = cfa_store.offset if cfa.reg == sp
1518            cfa.reg = sp
1519            cfa.base_offset = -cfa_store.offset
1520
1521   Rule 12:
1522   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1523
1524        <reg2>)
1525   effects: cfa.reg = <reg1>
1526            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1527
1528   Rule 13:
1529   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1530   effects: cfa.reg = <reg1>
1531            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1532
1533   Rule 14:
1534   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1535   effects: cfa.reg = <reg1>
1536            cfa.base_offset = -cfa_temp.offset
1537            cfa_temp.offset -= mode_size(mem)
1538
1539   Rule 15:
1540   (set <reg> {unspec, unspec_volatile})
1541   effects: target-dependent  */
1542
1543 static void
1544 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1545 {
1546   rtx src, dest, span;
1547   HOST_WIDE_INT offset;
1548
1549   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1550      the PARALLEL independently. The first element is always processed if
1551      it is a SET. This is for backward compatibility.   Other elements
1552      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1553      flag is set in them.  */
1554   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1555     {
1556       int par_index;
1557       int limit = XVECLEN (expr, 0);
1558       rtx elem;
1559
1560       /* PARALLELs have strict read-modify-write semantics, so we
1561          ought to evaluate every rvalue before changing any lvalue.
1562          It's cumbersome to do that in general, but there's an
1563          easy approximation that is enough for all current users:
1564          handle register saves before register assignments.  */
1565       if (GET_CODE (expr) == PARALLEL)
1566         for (par_index = 0; par_index < limit; par_index++)
1567           {
1568             elem = XVECEXP (expr, 0, par_index);
1569             if (GET_CODE (elem) == SET
1570                 && MEM_P (SET_DEST (elem))
1571                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572               dwarf2out_frame_debug_expr (elem, label);
1573           }
1574
1575       for (par_index = 0; par_index < limit; par_index++)
1576         {
1577           elem = XVECEXP (expr, 0, par_index);
1578           if (GET_CODE (elem) == SET
1579               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1580               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1581             dwarf2out_frame_debug_expr (elem, label);
1582           else if (GET_CODE (elem) == SET
1583                    && par_index != 0
1584                    && !RTX_FRAME_RELATED_P (elem))
1585             {
1586               /* Stack adjustment combining might combine some post-prologue
1587                  stack adjustment into a prologue stack adjustment.  */
1588               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1589
1590               if (offset != 0)
1591                 {
1592                   if (cfa.reg == STACK_POINTER_REGNUM)
1593                     cfa.offset += offset;
1594
1595 #ifndef STACK_GROWS_DOWNWARD
1596                   offset = -offset;
1597 #endif
1598
1599                   args_size += offset;
1600                   if (args_size < 0)
1601                     args_size = 0;
1602
1603                   def_cfa_1 (label, &cfa);
1604                   if (flag_asynchronous_unwind_tables)
1605                     dwarf2out_args_size (label, args_size);
1606                 }
1607             }
1608         }
1609       return;
1610     }
1611
1612   gcc_assert (GET_CODE (expr) == SET);
1613
1614   src = SET_SRC (expr);
1615   dest = SET_DEST (expr);
1616
1617   if (REG_P (src))
1618     {
1619       rtx rsi = reg_saved_in (src);
1620       if (rsi)
1621         src = rsi;
1622     }
1623
1624   switch (GET_CODE (dest))
1625     {
1626     case REG:
1627       switch (GET_CODE (src))
1628         {
1629           /* Setting FP from SP.  */
1630         case REG:
1631           if (cfa.reg == (unsigned) REGNO (src))
1632             {
1633               /* Rule 1 */
1634               /* Update the CFA rule wrt SP or FP.  Make sure src is
1635                  relative to the current CFA register.
1636
1637                  We used to require that dest be either SP or FP, but the
1638                  ARM copies SP to a temporary register, and from there to
1639                  FP.  So we just rely on the backends to only set
1640                  RTX_FRAME_RELATED_P on appropriate insns.  */
1641               cfa.reg = REGNO (dest);
1642               cfa_temp.reg = cfa.reg;
1643               cfa_temp.offset = cfa.offset;
1644             }
1645           else
1646             {
1647               /* Saving a register in a register.  */
1648               gcc_assert (!fixed_regs [REGNO (dest)]
1649                           /* For the SPARC and its register window.  */
1650                           || (DWARF_FRAME_REGNUM (REGNO (src))
1651                               == DWARF_FRAME_RETURN_COLUMN));
1652               queue_reg_save (label, src, dest, 0);
1653             }
1654           break;
1655
1656         case PLUS:
1657         case MINUS:
1658         case LO_SUM:
1659           if (dest == stack_pointer_rtx)
1660             {
1661               /* Rule 2 */
1662               /* Adjusting SP.  */
1663               switch (GET_CODE (XEXP (src, 1)))
1664                 {
1665                 case CONST_INT:
1666                   offset = INTVAL (XEXP (src, 1));
1667                   break;
1668                 case REG:
1669                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1670                               == cfa_temp.reg);
1671                   offset = cfa_temp.offset;
1672                   break;
1673                 default:
1674                   gcc_unreachable ();
1675                 }
1676
1677               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1678                 {
1679                   /* Restoring SP from FP in the epilogue.  */
1680                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1681                   cfa.reg = STACK_POINTER_REGNUM;
1682                 }
1683               else if (GET_CODE (src) == LO_SUM)
1684                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1685                 ;
1686               else
1687                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1688
1689               if (GET_CODE (src) != MINUS)
1690                 offset = -offset;
1691               if (cfa.reg == STACK_POINTER_REGNUM)
1692                 cfa.offset += offset;
1693               if (cfa_store.reg == STACK_POINTER_REGNUM)
1694                 cfa_store.offset += offset;
1695             }
1696           else if (dest == hard_frame_pointer_rtx)
1697             {
1698               /* Rule 3 */
1699               /* Either setting the FP from an offset of the SP,
1700                  or adjusting the FP */
1701               gcc_assert (frame_pointer_needed);
1702
1703               gcc_assert (REG_P (XEXP (src, 0))
1704                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1705                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1706               offset = INTVAL (XEXP (src, 1));
1707               if (GET_CODE (src) != MINUS)
1708                 offset = -offset;
1709               cfa.offset += offset;
1710               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1711             }
1712           else
1713             {
1714               gcc_assert (GET_CODE (src) != MINUS);
1715
1716               /* Rule 4 */
1717               if (REG_P (XEXP (src, 0))
1718                   && REGNO (XEXP (src, 0)) == cfa.reg
1719                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1720                 {
1721                   /* Setting a temporary CFA register that will be copied
1722                      into the FP later on.  */
1723                   offset = - INTVAL (XEXP (src, 1));
1724                   cfa.offset += offset;
1725                   cfa.reg = REGNO (dest);
1726                   /* Or used to save regs to the stack.  */
1727                   cfa_temp.reg = cfa.reg;
1728                   cfa_temp.offset = cfa.offset;
1729                 }
1730
1731               /* Rule 5 */
1732               else if (REG_P (XEXP (src, 0))
1733                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1734                        && XEXP (src, 1) == stack_pointer_rtx)
1735                 {
1736                   /* Setting a scratch register that we will use instead
1737                      of SP for saving registers to the stack.  */
1738                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1739                   cfa_store.reg = REGNO (dest);
1740                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1741                 }
1742
1743               /* Rule 9 */
1744               else if (GET_CODE (src) == LO_SUM
1745                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1746                 {
1747                   cfa_temp.reg = REGNO (dest);
1748                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1749                 }
1750               else
1751                 gcc_unreachable ();
1752             }
1753           break;
1754
1755           /* Rule 6 */
1756         case CONST_INT:
1757           cfa_temp.reg = REGNO (dest);
1758           cfa_temp.offset = INTVAL (src);
1759           break;
1760
1761           /* Rule 7 */
1762         case IOR:
1763           gcc_assert (REG_P (XEXP (src, 0))
1764                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1765                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1766
1767           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1768             cfa_temp.reg = REGNO (dest);
1769           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1770           break;
1771
1772           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1773              which will fill in all of the bits.  */
1774           /* Rule 8 */
1775         case HIGH:
1776           break;
1777
1778           /* Rule 15 */
1779         case UNSPEC:
1780         case UNSPEC_VOLATILE:
1781           gcc_assert (targetm.dwarf_handle_frame_unspec);
1782           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1783           return;
1784
1785         default:
1786           gcc_unreachable ();
1787         }
1788
1789       def_cfa_1 (label, &cfa);
1790       break;
1791
1792     case MEM:
1793       gcc_assert (REG_P (src));
1794
1795       /* Saving a register to the stack.  Make sure dest is relative to the
1796          CFA register.  */
1797       switch (GET_CODE (XEXP (dest, 0)))
1798         {
1799           /* Rule 10 */
1800           /* With a push.  */
1801         case PRE_MODIFY:
1802           /* We can't handle variable size modifications.  */
1803           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1804                       == CONST_INT);
1805           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1806
1807           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1808                       && cfa_store.reg == STACK_POINTER_REGNUM);
1809
1810           cfa_store.offset += offset;
1811           if (cfa.reg == STACK_POINTER_REGNUM)
1812             cfa.offset = cfa_store.offset;
1813
1814           offset = -cfa_store.offset;
1815           break;
1816
1817           /* Rule 11 */
1818         case PRE_INC:
1819         case PRE_DEC:
1820           offset = GET_MODE_SIZE (GET_MODE (dest));
1821           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1822             offset = -offset;
1823
1824           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1825                       && cfa_store.reg == STACK_POINTER_REGNUM);
1826
1827           cfa_store.offset += offset;
1828           if (cfa.reg == STACK_POINTER_REGNUM)
1829             cfa.offset = cfa_store.offset;
1830
1831           offset = -cfa_store.offset;
1832           break;
1833
1834           /* Rule 12 */
1835           /* With an offset.  */
1836         case PLUS:
1837         case MINUS:
1838         case LO_SUM:
1839           {
1840             int regno;
1841
1842             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1843                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1844             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1845             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1846               offset = -offset;
1847
1848             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1849
1850             if (cfa_store.reg == (unsigned) regno)
1851               offset -= cfa_store.offset;
1852             else
1853               {
1854                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1855                 offset -= cfa_temp.offset;
1856               }
1857           }
1858           break;
1859
1860           /* Rule 13 */
1861           /* Without an offset.  */
1862         case REG:
1863           {
1864             int regno = REGNO (XEXP (dest, 0));
1865
1866             if (cfa_store.reg == (unsigned) regno)
1867               offset = -cfa_store.offset;
1868             else
1869               {
1870                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1871                 offset = -cfa_temp.offset;
1872               }
1873           }
1874           break;
1875
1876           /* Rule 14 */
1877         case POST_INC:
1878           gcc_assert (cfa_temp.reg
1879                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1880           offset = -cfa_temp.offset;
1881           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1882           break;
1883
1884         default:
1885           gcc_unreachable ();
1886         }
1887
1888       if (REGNO (src) != STACK_POINTER_REGNUM
1889           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1890           && (unsigned) REGNO (src) == cfa.reg)
1891         {
1892           /* We're storing the current CFA reg into the stack.  */
1893
1894           if (cfa.offset == 0)
1895             {
1896               /* If the source register is exactly the CFA, assume
1897                  we're saving SP like any other register; this happens
1898                  on the ARM.  */
1899               def_cfa_1 (label, &cfa);
1900               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1901               break;
1902             }
1903           else
1904             {
1905               /* Otherwise, we'll need to look in the stack to
1906                  calculate the CFA.  */
1907               rtx x = XEXP (dest, 0);
1908
1909               if (!REG_P (x))
1910                 x = XEXP (x, 0);
1911               gcc_assert (REG_P (x));
1912
1913               cfa.reg = REGNO (x);
1914               cfa.base_offset = offset;
1915               cfa.indirect = 1;
1916               def_cfa_1 (label, &cfa);
1917               break;
1918             }
1919         }
1920
1921       def_cfa_1 (label, &cfa);
1922       {
1923         span = targetm.dwarf_register_span (src);
1924
1925         if (!span)
1926           queue_reg_save (label, src, NULL_RTX, offset);
1927         else
1928           {
1929             /* We have a PARALLEL describing where the contents of SRC
1930                live.  Queue register saves for each piece of the
1931                PARALLEL.  */
1932             int par_index;
1933             int limit;
1934             HOST_WIDE_INT span_offset = offset;
1935
1936             gcc_assert (GET_CODE (span) == PARALLEL);
1937
1938             limit = XVECLEN (span, 0);
1939             for (par_index = 0; par_index < limit; par_index++)
1940               {
1941                 rtx elem = XVECEXP (span, 0, par_index);
1942
1943                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1944                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1945               }
1946           }
1947       }
1948       break;
1949
1950     default:
1951       gcc_unreachable ();
1952     }
1953 }
1954
1955 /* Record call frame debugging information for INSN, which either
1956    sets SP or FP (adjusting how we calculate the frame address) or saves a
1957    register to the stack.  If INSN is NULL_RTX, initialize our state.
1958
1959    If AFTER_P is false, we're being called before the insn is emitted,
1960    otherwise after.  Call instructions get invoked twice.  */
1961
1962 void
1963 dwarf2out_frame_debug (rtx insn, bool after_p)
1964 {
1965   const char *label;
1966   rtx src;
1967
1968   if (insn == NULL_RTX)
1969     {
1970       size_t i;
1971
1972       /* Flush any queued register saves.  */
1973       flush_queued_reg_saves ();
1974
1975       /* Set up state for generating call frame debug info.  */
1976       lookup_cfa (&cfa);
1977       gcc_assert (cfa.reg
1978                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1979
1980       cfa.reg = STACK_POINTER_REGNUM;
1981       cfa_store = cfa;
1982       cfa_temp.reg = -1;
1983       cfa_temp.offset = 0;
1984
1985       for (i = 0; i < num_regs_saved_in_regs; i++)
1986         {
1987           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1988           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1989         }
1990       num_regs_saved_in_regs = 0;
1991       return;
1992     }
1993
1994   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1995     flush_queued_reg_saves ();
1996
1997   if (! RTX_FRAME_RELATED_P (insn))
1998     {
1999       if (!ACCUMULATE_OUTGOING_ARGS)
2000         dwarf2out_stack_adjust (insn, after_p);
2001       return;
2002     }
2003
2004   label = dwarf2out_cfi_label ();
2005   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2006   if (src)
2007     insn = XEXP (src, 0);
2008   else
2009     insn = PATTERN (insn);
2010
2011   dwarf2out_frame_debug_expr (insn, label);
2012 }
2013
2014 #endif
2015
2016 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2017 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2018  (enum dwarf_call_frame_info cfi);
2019
2020 static enum dw_cfi_oprnd_type
2021 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2022 {
2023   switch (cfi)
2024     {
2025     case DW_CFA_nop:
2026     case DW_CFA_GNU_window_save:
2027       return dw_cfi_oprnd_unused;
2028
2029     case DW_CFA_set_loc:
2030     case DW_CFA_advance_loc1:
2031     case DW_CFA_advance_loc2:
2032     case DW_CFA_advance_loc4:
2033     case DW_CFA_MIPS_advance_loc8:
2034       return dw_cfi_oprnd_addr;
2035
2036     case DW_CFA_offset:
2037     case DW_CFA_offset_extended:
2038     case DW_CFA_def_cfa:
2039     case DW_CFA_offset_extended_sf:
2040     case DW_CFA_def_cfa_sf:
2041     case DW_CFA_restore_extended:
2042     case DW_CFA_undefined:
2043     case DW_CFA_same_value:
2044     case DW_CFA_def_cfa_register:
2045     case DW_CFA_register:
2046       return dw_cfi_oprnd_reg_num;
2047
2048     case DW_CFA_def_cfa_offset:
2049     case DW_CFA_GNU_args_size:
2050     case DW_CFA_def_cfa_offset_sf:
2051       return dw_cfi_oprnd_offset;
2052
2053     case DW_CFA_def_cfa_expression:
2054     case DW_CFA_expression:
2055       return dw_cfi_oprnd_loc;
2056
2057     default:
2058       gcc_unreachable ();
2059     }
2060 }
2061
2062 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2063 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2064  (enum dwarf_call_frame_info cfi);
2065
2066 static enum dw_cfi_oprnd_type
2067 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2068 {
2069   switch (cfi)
2070     {
2071     case DW_CFA_def_cfa:
2072     case DW_CFA_def_cfa_sf:
2073     case DW_CFA_offset:
2074     case DW_CFA_offset_extended_sf:
2075     case DW_CFA_offset_extended:
2076       return dw_cfi_oprnd_offset;
2077
2078     case DW_CFA_register:
2079       return dw_cfi_oprnd_reg_num;
2080
2081     default:
2082       return dw_cfi_oprnd_unused;
2083     }
2084 }
2085
2086 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2087
2088 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2089    switch to the data section instead, and write out a synthetic label
2090    for collect2.  */
2091
2092 static void
2093 switch_to_eh_frame_section (void)
2094 {
2095   tree label;
2096
2097 #ifdef EH_FRAME_SECTION_NAME
2098   if (eh_frame_section == 0)
2099     {
2100       int flags;
2101
2102       if (EH_TABLES_CAN_BE_READ_ONLY)
2103         {
2104           int fde_encoding;
2105           int per_encoding;
2106           int lsda_encoding;
2107
2108           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2109                                                        /*global=*/0);
2110           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2111                                                        /*global=*/1);
2112           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2113                                                         /*global=*/0);
2114           flags = ((! flag_pic
2115                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2116                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2117                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2118                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2119                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2120                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2121                    ? 0 : SECTION_WRITE);
2122         }
2123       else
2124         flags = SECTION_WRITE;
2125       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2126     }
2127 #endif
2128
2129   if (eh_frame_section)
2130     switch_to_section (eh_frame_section);
2131   else
2132     {
2133       /* We have no special eh_frame section.  Put the information in
2134          the data section and emit special labels to guide collect2.  */
2135       switch_to_section (data_section);
2136       label = get_file_function_name ("F");
2137       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2138       targetm.asm_out.globalize_label (asm_out_file,
2139                                        IDENTIFIER_POINTER (label));
2140       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2141     }
2142 }
2143
2144 /* Output a Call Frame Information opcode and its operand(s).  */
2145
2146 static void
2147 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2148 {
2149   unsigned long r;
2150   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2151     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2152                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2153                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2154                          ((unsigned HOST_WIDE_INT)
2155                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2156   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2157     {
2158       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2159       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2160                            "DW_CFA_offset, column 0x%lx", r);
2161       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2162     }
2163   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2164     {
2165       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2166       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2167                            "DW_CFA_restore, column 0x%lx", r);
2168     }
2169   else
2170     {
2171       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2172                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2173
2174       switch (cfi->dw_cfi_opc)
2175         {
2176         case DW_CFA_set_loc:
2177           if (for_eh)
2178             dw2_asm_output_encoded_addr_rtx (
2179                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2180                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2181                 false, NULL);
2182           else
2183             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2184                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2185           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2186           break;
2187
2188         case DW_CFA_advance_loc1:
2189           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2190                                 fde->dw_fde_current_label, NULL);
2191           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2192           break;
2193
2194         case DW_CFA_advance_loc2:
2195           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2196                                 fde->dw_fde_current_label, NULL);
2197           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2198           break;
2199
2200         case DW_CFA_advance_loc4:
2201           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2202                                 fde->dw_fde_current_label, NULL);
2203           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2204           break;
2205
2206         case DW_CFA_MIPS_advance_loc8:
2207           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2208                                 fde->dw_fde_current_label, NULL);
2209           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2210           break;
2211
2212         case DW_CFA_offset_extended:
2213         case DW_CFA_def_cfa:
2214           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2215           dw2_asm_output_data_uleb128 (r, NULL);
2216           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2217           break;
2218
2219         case DW_CFA_offset_extended_sf:
2220         case DW_CFA_def_cfa_sf:
2221           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2222           dw2_asm_output_data_uleb128 (r, NULL);
2223           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2224           break;
2225
2226         case DW_CFA_restore_extended:
2227         case DW_CFA_undefined:
2228         case DW_CFA_same_value:
2229         case DW_CFA_def_cfa_register:
2230           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2231           dw2_asm_output_data_uleb128 (r, NULL);
2232           break;
2233
2234         case DW_CFA_register:
2235           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2236           dw2_asm_output_data_uleb128 (r, NULL);
2237           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2238           dw2_asm_output_data_uleb128 (r, NULL);
2239           break;
2240
2241         case DW_CFA_def_cfa_offset:
2242         case DW_CFA_GNU_args_size:
2243           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2244           break;
2245
2246         case DW_CFA_def_cfa_offset_sf:
2247           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2248           break;
2249
2250         case DW_CFA_GNU_window_save:
2251           break;
2252
2253         case DW_CFA_def_cfa_expression:
2254         case DW_CFA_expression:
2255           output_cfa_loc (cfi);
2256           break;
2257
2258         case DW_CFA_GNU_negative_offset_extended:
2259           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2260           gcc_unreachable ();
2261
2262         default:
2263           break;
2264         }
2265     }
2266 }
2267
2268 /* Output the call frame information used to record information
2269    that relates to calculating the frame pointer, and records the
2270    location of saved registers.  */
2271
2272 static void
2273 output_call_frame_info (int for_eh)
2274 {
2275   unsigned int i;
2276   dw_fde_ref fde;
2277   dw_cfi_ref cfi;
2278   char l1[20], l2[20], section_start_label[20];
2279   bool any_lsda_needed = false;
2280   char augmentation[6];
2281   int augmentation_size;
2282   int fde_encoding = DW_EH_PE_absptr;
2283   int per_encoding = DW_EH_PE_absptr;
2284   int lsda_encoding = DW_EH_PE_absptr;
2285   int return_reg;
2286
2287   /* Don't emit a CIE if there won't be any FDEs.  */
2288   if (fde_table_in_use == 0)
2289     return;
2290
2291   /* If we make FDEs linkonce, we may have to emit an empty label for
2292      an FDE that wouldn't otherwise be emitted.  We want to avoid
2293      having an FDE kept around when the function it refers to is
2294      discarded.  Example where this matters: a primary function
2295      template in C++ requires EH information, but an explicit
2296      specialization doesn't.  */
2297   if (TARGET_USES_WEAK_UNWIND_INFO
2298       && ! flag_asynchronous_unwind_tables
2299       && flag_exceptions
2300       && for_eh)
2301     for (i = 0; i < fde_table_in_use; i++)
2302       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2303           && !fde_table[i].uses_eh_lsda
2304           && ! DECL_WEAK (fde_table[i].decl))
2305         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2306                                       for_eh, /* empty */ 1);
2307
2308   /* If we don't have any functions we'll want to unwind out of, don't
2309      emit any EH unwind information.  Note that if exceptions aren't
2310      enabled, we won't have collected nothrow information, and if we
2311      asked for asynchronous tables, we always want this info.  */
2312   if (for_eh)
2313     {
2314       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2315
2316       for (i = 0; i < fde_table_in_use; i++)
2317         if (fde_table[i].uses_eh_lsda)
2318           any_eh_needed = any_lsda_needed = true;
2319         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2320           any_eh_needed = true;
2321         else if (! fde_table[i].nothrow
2322                  && ! fde_table[i].all_throwers_are_sibcalls)
2323           any_eh_needed = true;
2324
2325       if (! any_eh_needed)
2326         return;
2327     }
2328
2329   /* We're going to be generating comments, so turn on app.  */
2330   if (flag_debug_asm)
2331     app_enable ();
2332
2333   if (for_eh)
2334     switch_to_eh_frame_section ();
2335   else
2336     {
2337       if (!debug_frame_section)
2338         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2339                                            SECTION_DEBUG, NULL);
2340       switch_to_section (debug_frame_section);
2341     }
2342
2343   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2344   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2345
2346   /* Output the CIE.  */
2347   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2348   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2349   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2350     dw2_asm_output_data (4, 0xffffffff,
2351       "Initial length escape value indicating 64-bit DWARF extension");
2352   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2353                         "Length of Common Information Entry");
2354   ASM_OUTPUT_LABEL (asm_out_file, l1);
2355
2356   /* Now that the CIE pointer is PC-relative for EH,
2357      use 0 to identify the CIE.  */
2358   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2359                        (for_eh ? 0 : DWARF_CIE_ID),
2360                        "CIE Identifier Tag");
2361
2362   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2363
2364   augmentation[0] = 0;
2365   augmentation_size = 0;
2366   if (for_eh)
2367     {
2368       char *p;
2369
2370       /* Augmentation:
2371          z      Indicates that a uleb128 is present to size the
2372                 augmentation section.
2373          L      Indicates the encoding (and thus presence) of
2374                 an LSDA pointer in the FDE augmentation.
2375          R      Indicates a non-default pointer encoding for
2376                 FDE code pointers.
2377          P      Indicates the presence of an encoding + language
2378                 personality routine in the CIE augmentation.  */
2379
2380       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2381       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2382       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2383
2384       p = augmentation + 1;
2385       if (eh_personality_libfunc)
2386         {
2387           *p++ = 'P';
2388           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2389           assemble_external_libcall (eh_personality_libfunc);
2390         }
2391       if (any_lsda_needed)
2392         {
2393           *p++ = 'L';
2394           augmentation_size += 1;
2395         }
2396       if (fde_encoding != DW_EH_PE_absptr)
2397         {
2398           *p++ = 'R';
2399           augmentation_size += 1;
2400         }
2401       if (p > augmentation + 1)
2402         {
2403           augmentation[0] = 'z';
2404           *p = '\0';
2405         }
2406
2407       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2408       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2409         {
2410           int offset = (  4             /* Length */
2411                         + 4             /* CIE Id */
2412                         + 1             /* CIE version */
2413                         + strlen (augmentation) + 1     /* Augmentation */
2414                         + size_of_uleb128 (1)           /* Code alignment */
2415                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2416                         + 1             /* RA column */
2417                         + 1             /* Augmentation size */
2418                         + 1             /* Personality encoding */ );
2419           int pad = -offset & (PTR_SIZE - 1);
2420
2421           augmentation_size += pad;
2422
2423           /* Augmentations should be small, so there's scarce need to
2424              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2425           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2426         }
2427     }
2428
2429   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2430   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2431   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2432                                "CIE Data Alignment Factor");
2433
2434   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2435   if (DW_CIE_VERSION == 1)
2436     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2437   else
2438     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2439
2440   if (augmentation[0])
2441     {
2442       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2443       if (eh_personality_libfunc)
2444         {
2445           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2446                                eh_data_format_name (per_encoding));
2447           dw2_asm_output_encoded_addr_rtx (per_encoding,
2448                                            eh_personality_libfunc,
2449                                            true, NULL);
2450         }
2451
2452       if (any_lsda_needed)
2453         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2454                              eh_data_format_name (lsda_encoding));
2455
2456       if (fde_encoding != DW_EH_PE_absptr)
2457         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2458                              eh_data_format_name (fde_encoding));
2459     }
2460
2461   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2462     output_cfi (cfi, NULL, for_eh);
2463
2464   /* Pad the CIE out to an address sized boundary.  */
2465   ASM_OUTPUT_ALIGN (asm_out_file,
2466                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2467   ASM_OUTPUT_LABEL (asm_out_file, l2);
2468
2469   /* Loop through all of the FDE's.  */
2470   for (i = 0; i < fde_table_in_use; i++)
2471     {
2472       fde = &fde_table[i];
2473
2474       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2475       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2476           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2477           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2478           && !fde->uses_eh_lsda)
2479         continue;
2480
2481       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2482       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2483       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2484       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2485       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2486         dw2_asm_output_data (4, 0xffffffff,
2487                              "Initial length escape value indicating 64-bit DWARF extension");
2488       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2489                             "FDE Length");
2490       ASM_OUTPUT_LABEL (asm_out_file, l1);
2491
2492       if (for_eh)
2493         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2494       else
2495         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2496                                debug_frame_section, "FDE CIE offset");
2497
2498       if (for_eh)
2499         {
2500           if (fde->dw_fde_switched_sections)
2501             {
2502               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2503                                       fde->dw_fde_unlikely_section_label);
2504               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2505                                       fde->dw_fde_hot_section_label);
2506               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2507               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2508               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2509                                                "FDE initial location");
2510               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2511                                     fde->dw_fde_hot_section_end_label,
2512                                     fde->dw_fde_hot_section_label,
2513                                     "FDE address range");
2514               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2515                                                "FDE initial location");
2516               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2517                                     fde->dw_fde_unlikely_section_end_label,
2518                                     fde->dw_fde_unlikely_section_label,
2519                                     "FDE address range");
2520             }
2521           else
2522             {
2523               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2524               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2525               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2526                                                sym_ref,
2527                                                false,
2528                                                "FDE initial location");
2529               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2530                                     fde->dw_fde_end, fde->dw_fde_begin,
2531                                     "FDE address range");
2532             }
2533         }
2534       else
2535         {
2536           if (fde->dw_fde_switched_sections)
2537             {
2538               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2539                                    fde->dw_fde_hot_section_label,
2540                                    "FDE initial location");
2541               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2542                                     fde->dw_fde_hot_section_end_label,
2543                                     fde->dw_fde_hot_section_label,
2544                                     "FDE address range");
2545               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2546                                    fde->dw_fde_unlikely_section_label,
2547                                    "FDE initial location");
2548               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2549                                     fde->dw_fde_unlikely_section_end_label,
2550                                     fde->dw_fde_unlikely_section_label,
2551                                     "FDE address range");
2552             }
2553           else
2554             {
2555               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2556                                    "FDE initial location");
2557               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2558                                     fde->dw_fde_end, fde->dw_fde_begin,
2559                                     "FDE address range");
2560             }
2561         }
2562
2563       if (augmentation[0])
2564         {
2565           if (any_lsda_needed)
2566             {
2567               int size = size_of_encoded_value (lsda_encoding);
2568
2569               if (lsda_encoding == DW_EH_PE_aligned)
2570                 {
2571                   int offset = (  4             /* Length */
2572                                 + 4             /* CIE offset */
2573                                 + 2 * size_of_encoded_value (fde_encoding)
2574                                 + 1             /* Augmentation size */ );
2575                   int pad = -offset & (PTR_SIZE - 1);
2576
2577                   size += pad;
2578                   gcc_assert (size_of_uleb128 (size) == 1);
2579                 }
2580
2581               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2582
2583               if (fde->uses_eh_lsda)
2584                 {
2585                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2586                                                fde->funcdef_number);
2587                   dw2_asm_output_encoded_addr_rtx (
2588                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2589                         false, "Language Specific Data Area");
2590                 }
2591               else
2592                 {
2593                   if (lsda_encoding == DW_EH_PE_aligned)
2594                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2595                   dw2_asm_output_data
2596                     (size_of_encoded_value (lsda_encoding), 0,
2597                      "Language Specific Data Area (none)");
2598                 }
2599             }
2600           else
2601             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2602         }
2603
2604       /* Loop through the Call Frame Instructions associated with
2605          this FDE.  */
2606       fde->dw_fde_current_label = fde->dw_fde_begin;
2607       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2608         output_cfi (cfi, fde, for_eh);
2609
2610       /* Pad the FDE out to an address sized boundary.  */
2611       ASM_OUTPUT_ALIGN (asm_out_file,
2612                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2613       ASM_OUTPUT_LABEL (asm_out_file, l2);
2614     }
2615
2616   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2617     dw2_asm_output_data (4, 0, "End of Table");
2618 #ifdef MIPS_DEBUGGING_INFO
2619   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2620      get a value of 0.  Putting .align 0 after the label fixes it.  */
2621   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2622 #endif
2623
2624   /* Turn off app to make assembly quicker.  */
2625   if (flag_debug_asm)
2626     app_disable ();
2627 }
2628
2629 /* Output a marker (i.e. a label) for the beginning of a function, before
2630    the prologue.  */
2631
2632 void
2633 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2634                           const char *file ATTRIBUTE_UNUSED)
2635 {
2636   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2637   char * dup_label;
2638   dw_fde_ref fde;
2639
2640   current_function_func_begin_label = NULL;
2641
2642 #ifdef TARGET_UNWIND_INFO
2643   /* ??? current_function_func_begin_label is also used by except.c
2644      for call-site information.  We must emit this label if it might
2645      be used.  */
2646   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2647       && ! dwarf2out_do_frame ())
2648     return;
2649 #else
2650   if (! dwarf2out_do_frame ())
2651     return;
2652 #endif
2653
2654   switch_to_section (function_section (current_function_decl));
2655   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2656                                current_function_funcdef_no);
2657   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2658                           current_function_funcdef_no);
2659   dup_label = xstrdup (label);
2660   current_function_func_begin_label = dup_label;
2661
2662 #ifdef TARGET_UNWIND_INFO
2663   /* We can elide the fde allocation if we're not emitting debug info.  */
2664   if (! dwarf2out_do_frame ())
2665     return;
2666 #endif
2667
2668   /* Expand the fde table if necessary.  */
2669   if (fde_table_in_use == fde_table_allocated)
2670     {
2671       fde_table_allocated += FDE_TABLE_INCREMENT;
2672       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
2673       memset (fde_table + fde_table_in_use, 0,
2674               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2675     }
2676
2677   /* Record the FDE associated with this function.  */
2678   current_funcdef_fde = fde_table_in_use;
2679
2680   /* Add the new FDE at the end of the fde_table.  */
2681   fde = &fde_table[fde_table_in_use++];
2682   fde->decl = current_function_decl;
2683   fde->dw_fde_begin = dup_label;
2684   fde->dw_fde_current_label = dup_label;
2685   fde->dw_fde_hot_section_label = NULL;
2686   fde->dw_fde_hot_section_end_label = NULL;
2687   fde->dw_fde_unlikely_section_label = NULL;
2688   fde->dw_fde_unlikely_section_end_label = NULL;
2689   fde->dw_fde_switched_sections = false;
2690   fde->dw_fde_end = NULL;
2691   fde->dw_fde_cfi = NULL;
2692   fde->funcdef_number = current_function_funcdef_no;
2693   fde->nothrow = TREE_NOTHROW (current_function_decl);
2694   fde->uses_eh_lsda = crtl->uses_eh_lsda;
2695   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
2696
2697   args_size = old_args_size = 0;
2698
2699   /* We only want to output line number information for the genuine dwarf2
2700      prologue case, not the eh frame case.  */
2701 #ifdef DWARF2_DEBUGGING_INFO
2702   if (file)
2703     dwarf2out_source_line (line, file);
2704 #endif
2705 }
2706
2707 /* Output a marker (i.e. a label) for the absolute end of the generated code
2708    for a function definition.  This gets called *after* the epilogue code has
2709    been generated.  */
2710
2711 void
2712 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2713                         const char *file ATTRIBUTE_UNUSED)
2714 {
2715   dw_fde_ref fde;
2716   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2717
2718   /* Output a label to mark the endpoint of the code generated for this
2719      function.  */
2720   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2721                                current_function_funcdef_no);
2722   ASM_OUTPUT_LABEL (asm_out_file, label);
2723   fde = current_fde ();
2724   gcc_assert (fde != NULL);
2725   fde->dw_fde_end = xstrdup (label);
2726 }
2727
2728 void
2729 dwarf2out_frame_init (void)
2730 {
2731   /* Allocate the initial hunk of the fde_table.  */
2732   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
2733   fde_table_allocated = FDE_TABLE_INCREMENT;
2734   fde_table_in_use = 0;
2735
2736   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2737      sake of lookup_cfa.  */
2738
2739   /* On entry, the Canonical Frame Address is at SP.  */
2740   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2741
2742 #ifdef DWARF2_UNWIND_INFO
2743   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2744     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2745 #endif
2746 }
2747
2748 void
2749 dwarf2out_frame_finish (void)
2750 {
2751   /* Output call frame information.  */
2752   if (DWARF2_FRAME_INFO)
2753     output_call_frame_info (0);
2754
2755 #ifndef TARGET_UNWIND_INFO
2756   /* Output another copy for the unwinder.  */
2757   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2758     output_call_frame_info (1);
2759 #endif
2760 }
2761
2762 /* Note that the current function section is being used for code.  */
2763
2764 static void
2765 dwarf2out_note_section_used (void)
2766 {
2767   section *sec = current_function_section ();
2768   if (sec == text_section)
2769     text_section_used = true;
2770   else if (sec == cold_text_section)
2771     cold_text_section_used = true;
2772 }
2773
2774 void
2775 dwarf2out_switch_text_section (void)
2776 {
2777   dw_fde_ref fde = current_fde ();
2778
2779   gcc_assert (cfun && fde);
2780
2781   fde->dw_fde_switched_sections = true;
2782   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2783   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2784   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2785   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2786   have_multiple_function_sections = true;
2787
2788   /* Reset the current label on switching text sections, so that we
2789      don't attempt to advance_loc4 between labels in different sections.  */
2790   fde->dw_fde_current_label = NULL;
2791
2792   /* There is no need to mark used sections when not debugging.  */
2793   if (cold_text_section != NULL)
2794     dwarf2out_note_section_used ();
2795 }
2796 #endif
2797 \f
2798 /* And now, the subset of the debugging information support code necessary
2799    for emitting location expressions.  */
2800
2801 /* Data about a single source file.  */
2802 struct dwarf_file_data GTY(())
2803 {
2804   const char * filename;
2805   int emitted_number;
2806 };
2807
2808 /* We need some way to distinguish DW_OP_addr with a direct symbol
2809    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2810 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2811
2812
2813 typedef struct dw_val_struct *dw_val_ref;
2814 typedef struct die_struct *dw_die_ref;
2815 typedef const struct die_struct *const_dw_die_ref;
2816 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2817 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2818
2819 /* Each DIE may have a series of attribute/value pairs.  Values
2820    can take on several forms.  The forms that are used in this
2821    implementation are listed below.  */
2822
2823 enum dw_val_class
2824 {
2825   dw_val_class_addr,
2826   dw_val_class_offset,
2827   dw_val_class_loc,
2828   dw_val_class_loc_list,
2829   dw_val_class_range_list,
2830   dw_val_class_const,
2831   dw_val_class_unsigned_const,
2832   dw_val_class_long_long,
2833   dw_val_class_vec,
2834   dw_val_class_flag,
2835   dw_val_class_die_ref,
2836   dw_val_class_fde_ref,
2837   dw_val_class_lbl_id,
2838   dw_val_class_lineptr,
2839   dw_val_class_str,
2840   dw_val_class_macptr,
2841   dw_val_class_file
2842 };
2843
2844 /* Describe a double word constant value.  */
2845 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2846
2847 typedef struct dw_long_long_struct GTY(())
2848 {
2849   unsigned long hi;
2850   unsigned long low;
2851 }
2852 dw_long_long_const;
2853
2854 /* Describe a floating point constant value, or a vector constant value.  */
2855
2856 typedef struct dw_vec_struct GTY(())
2857 {
2858   unsigned char * GTY((length ("%h.length"))) array;
2859   unsigned length;
2860   unsigned elt_size;
2861 }
2862 dw_vec_const;
2863
2864 /* The dw_val_node describes an attribute's value, as it is
2865    represented internally.  */
2866
2867 typedef struct dw_val_struct GTY(())
2868 {
2869   enum dw_val_class val_class;
2870   union dw_val_struct_union
2871     {
2872       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2873       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2874       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2875       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2876       HOST_WIDE_INT GTY ((default)) val_int;
2877       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2878       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2879       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2880       struct dw_val_die_union
2881         {
2882           dw_die_ref die;
2883           int external;
2884         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2885       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2886       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2887       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2888       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2889       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2890     }
2891   GTY ((desc ("%1.val_class"))) v;
2892 }
2893 dw_val_node;
2894
2895 /* Locations in memory are described using a sequence of stack machine
2896    operations.  */
2897
2898 typedef struct dw_loc_descr_struct GTY(())
2899 {
2900   dw_loc_descr_ref dw_loc_next;
2901   enum dwarf_location_atom dw_loc_opc;
2902   dw_val_node dw_loc_oprnd1;
2903   dw_val_node dw_loc_oprnd2;
2904   int dw_loc_addr;
2905 }
2906 dw_loc_descr_node;
2907
2908 /* Location lists are ranges + location descriptions for that range,
2909    so you can track variables that are in different places over
2910    their entire life.  */
2911 typedef struct dw_loc_list_struct GTY(())
2912 {
2913   dw_loc_list_ref dw_loc_next;
2914   const char *begin; /* Label for begin address of range */
2915   const char *end;  /* Label for end address of range */
2916   char *ll_symbol; /* Label for beginning of location list.
2917                       Only on head of list */
2918   const char *section; /* Section this loclist is relative to */
2919   dw_loc_descr_ref expr;
2920 } dw_loc_list_node;
2921
2922 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2923
2924 static const char *dwarf_stack_op_name (unsigned);
2925 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2926                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2927 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2928 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2929 static unsigned long size_of_locs (dw_loc_descr_ref);
2930 static void output_loc_operands (dw_loc_descr_ref);
2931 static void output_loc_sequence (dw_loc_descr_ref);
2932
2933 /* Convert a DWARF stack opcode into its string name.  */
2934
2935 static const char *
2936 dwarf_stack_op_name (unsigned int op)
2937 {
2938   switch (op)
2939     {
2940     case DW_OP_addr:
2941     case INTERNAL_DW_OP_tls_addr:
2942       return "DW_OP_addr";
2943     case DW_OP_deref:
2944       return "DW_OP_deref";
2945     case DW_OP_const1u:
2946       return "DW_OP_const1u";
2947     case DW_OP_const1s:
2948       return "DW_OP_const1s";
2949     case DW_OP_const2u:
2950       return "DW_OP_const2u";
2951     case DW_OP_const2s:
2952       return "DW_OP_const2s";
2953     case DW_OP_const4u:
2954       return "DW_OP_const4u";
2955     case DW_OP_const4s:
2956       return "DW_OP_const4s";
2957     case DW_OP_const8u:
2958       return "DW_OP_const8u";
2959     case DW_OP_const8s:
2960       return "DW_OP_const8s";
2961     case DW_OP_constu:
2962       return "DW_OP_constu";
2963     case DW_OP_consts:
2964       return "DW_OP_consts";
2965     case DW_OP_dup:
2966       return "DW_OP_dup";
2967     case DW_OP_drop:
2968       return "DW_OP_drop";
2969     case DW_OP_over:
2970       return "DW_OP_over";
2971     case DW_OP_pick:
2972       return "DW_OP_pick";
2973     case DW_OP_swap:
2974       return "DW_OP_swap";
2975     case DW_OP_rot:
2976       return "DW_OP_rot";
2977     case DW_OP_xderef:
2978       return "DW_OP_xderef";
2979     case DW_OP_abs:
2980       return "DW_OP_abs";
2981     case DW_OP_and:
2982       return "DW_OP_and";
2983     case DW_OP_div:
2984       return "DW_OP_div";
2985     case DW_OP_minus:
2986       return "DW_OP_minus";
2987     case DW_OP_mod:
2988       return "DW_OP_mod";
2989     case DW_OP_mul:
2990       return "DW_OP_mul";
2991     case DW_OP_neg:
2992       return "DW_OP_neg";
2993     case DW_OP_not:
2994       return "DW_OP_not";
2995     case DW_OP_or:
2996       return "DW_OP_or";
2997     case DW_OP_plus:
2998       return "DW_OP_plus";
2999     case DW_OP_plus_uconst:
3000       return "DW_OP_plus_uconst";
3001     case DW_OP_shl:
3002       return "DW_OP_shl";
3003     case DW_OP_shr:
3004       return "DW_OP_shr";
3005     case DW_OP_shra:
3006       return "DW_OP_shra";
3007     case DW_OP_xor:
3008       return "DW_OP_xor";
3009     case DW_OP_bra:
3010       return "DW_OP_bra";
3011     case DW_OP_eq:
3012       return "DW_OP_eq";
3013     case DW_OP_ge:
3014       return "DW_OP_ge";
3015     case DW_OP_gt:
3016       return "DW_OP_gt";
3017     case DW_OP_le:
3018       return "DW_OP_le";
3019     case DW_OP_lt:
3020       return "DW_OP_lt";
3021     case DW_OP_ne:
3022       return "DW_OP_ne";
3023     case DW_OP_skip:
3024       return "DW_OP_skip";
3025     case DW_OP_lit0:
3026       return "DW_OP_lit0";
3027     case DW_OP_lit1:
3028       return "DW_OP_lit1";
3029     case DW_OP_lit2:
3030       return "DW_OP_lit2";
3031     case DW_OP_lit3:
3032       return "DW_OP_lit3";
3033     case DW_OP_lit4:
3034       return "DW_OP_lit4";
3035     case DW_OP_lit5:
3036       return "DW_OP_lit5";
3037     case DW_OP_lit6:
3038       return "DW_OP_lit6";
3039     case DW_OP_lit7:
3040       return "DW_OP_lit7";
3041     case DW_OP_lit8:
3042       return "DW_OP_lit8";
3043     case DW_OP_lit9:
3044       return "DW_OP_lit9";
3045     case DW_OP_lit10:
3046       return "DW_OP_lit10";
3047     case DW_OP_lit11:
3048       return "DW_OP_lit11";
3049     case DW_OP_lit12:
3050       return "DW_OP_lit12";
3051     case DW_OP_lit13:
3052       return "DW_OP_lit13";
3053     case DW_OP_lit14:
3054       return "DW_OP_lit14";
3055     case DW_OP_lit15:
3056       return "DW_OP_lit15";
3057     case DW_OP_lit16:
3058       return "DW_OP_lit16";
3059     case DW_OP_lit17:
3060       return "DW_OP_lit17";
3061     case DW_OP_lit18:
3062       return "DW_OP_lit18";
3063     case DW_OP_lit19:
3064       return "DW_OP_lit19";
3065     case DW_OP_lit20:
3066       return "DW_OP_lit20";
3067     case DW_OP_lit21:
3068       return "DW_OP_lit21";
3069     case DW_OP_lit22:
3070       return "DW_OP_lit22";
3071     case DW_OP_lit23:
3072       return "DW_OP_lit23";
3073     case DW_OP_lit24:
3074       return "DW_OP_lit24";
3075     case DW_OP_lit25:
3076       return "DW_OP_lit25";
3077     case DW_OP_lit26:
3078       return "DW_OP_lit26";
3079     case DW_OP_lit27:
3080       return "DW_OP_lit27";
3081     case DW_OP_lit28:
3082       return "DW_OP_lit28";
3083     case DW_OP_lit29:
3084       return "DW_OP_lit29";
3085     case DW_OP_lit30:
3086       return "DW_OP_lit30";
3087     case DW_OP_lit31:
3088       return "DW_OP_lit31";
3089     case DW_OP_reg0:
3090       return "DW_OP_reg0";
3091     case DW_OP_reg1:
3092       return "DW_OP_reg1";
3093     case DW_OP_reg2:
3094       return "DW_OP_reg2";
3095     case DW_OP_reg3:
3096       return "DW_OP_reg3";
3097     case DW_OP_reg4:
3098       return "DW_OP_reg4";
3099     case DW_OP_reg5:
3100       return "DW_OP_reg5";
3101     case DW_OP_reg6:
3102       return "DW_OP_reg6";
3103     case DW_OP_reg7:
3104       return "DW_OP_reg7";
3105     case DW_OP_reg8:
3106       return "DW_OP_reg8";
3107     case DW_OP_reg9:
3108       return "DW_OP_reg9";
3109     case DW_OP_reg10:
3110       return "DW_OP_reg10";
3111     case DW_OP_reg11:
3112       return "DW_OP_reg11";
3113     case DW_OP_reg12:
3114       return "DW_OP_reg12";
3115     case DW_OP_reg13:
3116       return "DW_OP_reg13";
3117     case DW_OP_reg14:
3118       return "DW_OP_reg14";
3119     case DW_OP_reg15:
3120       return "DW_OP_reg15";
3121     case DW_OP_reg16:
3122       return "DW_OP_reg16";
3123     case DW_OP_reg17:
3124       return "DW_OP_reg17";
3125     case DW_OP_reg18:
3126       return "DW_OP_reg18";
3127     case DW_OP_reg19:
3128       return "DW_OP_reg19";
3129     case DW_OP_reg20:
3130       return "DW_OP_reg20";
3131     case DW_OP_reg21:
3132       return "DW_OP_reg21";
3133     case DW_OP_reg22:
3134       return "DW_OP_reg22";
3135     case DW_OP_reg23:
3136       return "DW_OP_reg23";
3137     case DW_OP_reg24:
3138       return "DW_OP_reg24";
3139     case DW_OP_reg25:
3140       return "DW_OP_reg25";
3141     case DW_OP_reg26:
3142       return "DW_OP_reg26";
3143     case DW_OP_reg27:
3144       return "DW_OP_reg27";
3145     case DW_OP_reg28:
3146       return "DW_OP_reg28";
3147     case DW_OP_reg29:
3148       return "DW_OP_reg29";
3149     case DW_OP_reg30:
3150       return "DW_OP_reg30";
3151     case DW_OP_reg31:
3152       return "DW_OP_reg31";
3153     case DW_OP_breg0:
3154       return "DW_OP_breg0";
3155     case DW_OP_breg1:
3156       return "DW_OP_breg1";
3157     case DW_OP_breg2:
3158       return "DW_OP_breg2";
3159     case DW_OP_breg3:
3160       return "DW_OP_breg3";
3161     case DW_OP_breg4:
3162       return "DW_OP_breg4";
3163     case DW_OP_breg5:
3164       return "DW_OP_breg5";
3165     case DW_OP_breg6:
3166       return "DW_OP_breg6";
3167     case DW_OP_breg7:
3168       return "DW_OP_breg7";
3169     case DW_OP_breg8:
3170       return "DW_OP_breg8";
3171     case DW_OP_breg9:
3172       return "DW_OP_breg9";
3173     case DW_OP_breg10:
3174       return "DW_OP_breg10";
3175     case DW_OP_breg11:
3176       return "DW_OP_breg11";
3177     case DW_OP_breg12:
3178       return "DW_OP_breg12";
3179     case DW_OP_breg13:
3180       return "DW_OP_breg13";
3181     case DW_OP_breg14:
3182       return "DW_OP_breg14";
3183     case DW_OP_breg15:
3184       return "DW_OP_breg15";
3185     case DW_OP_breg16:
3186       return "DW_OP_breg16";
3187     case DW_OP_breg17:
3188       return "DW_OP_breg17";
3189     case DW_OP_breg18:
3190       return "DW_OP_breg18";
3191     case DW_OP_breg19:
3192       return "DW_OP_breg19";
3193     case DW_OP_breg20:
3194       return "DW_OP_breg20";
3195     case DW_OP_breg21:
3196       return "DW_OP_breg21";
3197     case DW_OP_breg22:
3198       return "DW_OP_breg22";
3199     case DW_OP_breg23:
3200       return "DW_OP_breg23";
3201     case DW_OP_breg24:
3202       return "DW_OP_breg24";
3203     case DW_OP_breg25:
3204       return "DW_OP_breg25";
3205     case DW_OP_breg26:
3206       return "DW_OP_breg26";
3207     case DW_OP_breg27:
3208       return "DW_OP_breg27";
3209     case DW_OP_breg28:
3210       return "DW_OP_breg28";
3211     case DW_OP_breg29:
3212       return "DW_OP_breg29";
3213     case DW_OP_breg30:
3214       return "DW_OP_breg30";
3215     case DW_OP_breg31:
3216       return "DW_OP_breg31";
3217     case DW_OP_regx:
3218       return "DW_OP_regx";
3219     case DW_OP_fbreg:
3220       return "DW_OP_fbreg";
3221     case DW_OP_bregx:
3222       return "DW_OP_bregx";
3223     case DW_OP_piece:
3224       return "DW_OP_piece";
3225     case DW_OP_deref_size:
3226       return "DW_OP_deref_size";
3227     case DW_OP_xderef_size:
3228       return "DW_OP_xderef_size";
3229     case DW_OP_nop:
3230       return "DW_OP_nop";
3231     case DW_OP_push_object_address:
3232       return "DW_OP_push_object_address";
3233     case DW_OP_call2:
3234       return "DW_OP_call2";
3235     case DW_OP_call4:
3236       return "DW_OP_call4";
3237     case DW_OP_call_ref:
3238       return "DW_OP_call_ref";
3239     case DW_OP_GNU_push_tls_address:
3240       return "DW_OP_GNU_push_tls_address";
3241     case DW_OP_GNU_uninit:
3242       return "DW_OP_GNU_uninit";
3243     default:
3244       return "OP_<unknown>";
3245     }
3246 }
3247
3248 /* Return a pointer to a newly allocated location description.  Location
3249    descriptions are simple expression terms that can be strung
3250    together to form more complicated location (address) descriptions.  */
3251
3252 static inline dw_loc_descr_ref
3253 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3254                unsigned HOST_WIDE_INT oprnd2)
3255 {
3256   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3257
3258   descr->dw_loc_opc = op;
3259   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3260   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3261   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3262   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3263
3264   return descr;
3265 }
3266
3267 /* Add a location description term to a location description expression.  */
3268
3269 static inline void
3270 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3271 {
3272   dw_loc_descr_ref *d;
3273
3274   /* Find the end of the chain.  */
3275   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3276     ;
3277
3278   *d = descr;
3279 }
3280
3281 /* Return the size of a location descriptor.  */
3282
3283 static unsigned long
3284 size_of_loc_descr (dw_loc_descr_ref loc)
3285 {
3286   unsigned long size = 1;
3287
3288   switch (loc->dw_loc_opc)
3289     {
3290     case DW_OP_addr:
3291     case INTERNAL_DW_OP_tls_addr:
3292       size += DWARF2_ADDR_SIZE;
3293       break;
3294     case DW_OP_const1u:
3295     case DW_OP_const1s:
3296       size += 1;
3297       break;
3298     case DW_OP_const2u:
3299     case DW_OP_const2s:
3300       size += 2;
3301       break;
3302     case DW_OP_const4u:
3303     case DW_OP_const4s:
3304       size += 4;
3305       break;
3306     case DW_OP_const8u:
3307     case DW_OP_const8s:
3308       size += 8;
3309       break;
3310     case DW_OP_constu:
3311       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3312       break;
3313     case DW_OP_consts:
3314       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3315       break;
3316     case DW_OP_pick:
3317       size += 1;
3318       break;
3319     case DW_OP_plus_uconst:
3320       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3321       break;
3322     case DW_OP_skip:
3323     case DW_OP_bra:
3324       size += 2;
3325       break;
3326     case DW_OP_breg0:
3327     case DW_OP_breg1:
3328     case DW_OP_breg2:
3329     case DW_OP_breg3:
3330     case DW_OP_breg4:
3331     case DW_OP_breg5:
3332     case DW_OP_breg6:
3333     case DW_OP_breg7:
3334     case DW_OP_breg8:
3335     case DW_OP_breg9:
3336     case DW_OP_breg10:
3337     case DW_OP_breg11:
3338     case DW_OP_breg12:
3339     case DW_OP_breg13:
3340     case DW_OP_breg14:
3341     case DW_OP_breg15:
3342     case DW_OP_breg16:
3343     case DW_OP_breg17:
3344     case DW_OP_breg18:
3345     case DW_OP_breg19:
3346     case DW_OP_breg20:
3347     case DW_OP_breg21:
3348     case DW_OP_breg22:
3349     case DW_OP_breg23:
3350     case DW_OP_breg24:
3351     case DW_OP_breg25:
3352     case DW_OP_breg26:
3353     case DW_OP_breg27:
3354     case DW_OP_breg28:
3355     case DW_OP_breg29:
3356     case DW_OP_breg30:
3357     case DW_OP_breg31:
3358       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3359       break;
3360     case DW_OP_regx:
3361       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3362       break;
3363     case DW_OP_fbreg:
3364       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3365       break;
3366     case DW_OP_bregx:
3367       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3368       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3369       break;
3370     case DW_OP_piece:
3371       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3372       break;
3373     case DW_OP_deref_size:
3374     case DW_OP_xderef_size:
3375       size += 1;
3376       break;
3377     case DW_OP_call2:
3378       size += 2;
3379       break;
3380     case DW_OP_call4:
3381       size += 4;
3382       break;
3383     case DW_OP_call_ref:
3384       size += DWARF2_ADDR_SIZE;
3385       break;
3386     default:
3387       break;
3388     }
3389
3390   return size;
3391 }
3392
3393 /* Return the size of a series of location descriptors.  */
3394
3395 static unsigned long
3396 size_of_locs (dw_loc_descr_ref loc)
3397 {
3398   dw_loc_descr_ref l;
3399   unsigned long size;
3400
3401   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3402      field, to avoid writing to a PCH file.  */
3403   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3404     {
3405       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3406         break;
3407       size += size_of_loc_descr (l);
3408     }
3409   if (! l)
3410     return size;
3411
3412   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3413     {
3414       l->dw_loc_addr = size;
3415       size += size_of_loc_descr (l);
3416     }
3417
3418   return size;
3419 }
3420
3421 /* Output location description stack opcode's operands (if any).  */
3422
3423 static void
3424 output_loc_operands (dw_loc_descr_ref loc)
3425 {
3426   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3427   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3428
3429   switch (loc->dw_loc_opc)
3430     {
3431 #ifdef DWARF2_DEBUGGING_INFO
3432     case DW_OP_addr:
3433       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3434       break;
3435     case DW_OP_const2u:
3436     case DW_OP_const2s:
3437       dw2_asm_output_data (2, val1->v.val_int, NULL);
3438       break;
3439     case DW_OP_const4u:
3440     case DW_OP_const4s:
3441       dw2_asm_output_data (4, val1->v.val_int, NULL);
3442       break;
3443     case DW_OP_const8u:
3444     case DW_OP_const8s:
3445       gcc_assert (HOST_BITS_PER_LONG >= 64);
3446       dw2_asm_output_data (8, val1->v.val_int, NULL);
3447       break;
3448     case DW_OP_skip:
3449     case DW_OP_bra:
3450       {
3451         int offset;
3452
3453         gcc_assert (val1->val_class == dw_val_class_loc);
3454         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3455
3456         dw2_asm_output_data (2, offset, NULL);
3457       }
3458       break;
3459 #else
3460     case DW_OP_addr:
3461     case DW_OP_const2u:
3462     case DW_OP_const2s:
3463     case DW_OP_const4u:
3464     case DW_OP_const4s:
3465     case DW_OP_const8u:
3466     case DW_OP_const8s:
3467     case DW_OP_skip:
3468     case DW_OP_bra:
3469       /* We currently don't make any attempt to make sure these are
3470          aligned properly like we do for the main unwind info, so
3471          don't support emitting things larger than a byte if we're
3472          only doing unwinding.  */
3473       gcc_unreachable ();
3474 #endif
3475     case DW_OP_const1u:
3476     case DW_OP_const1s:
3477       dw2_asm_output_data (1, val1->v.val_int, NULL);
3478       break;
3479     case DW_OP_constu:
3480       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3481       break;
3482     case DW_OP_consts:
3483       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3484       break;
3485     case DW_OP_pick:
3486       dw2_asm_output_data (1, val1->v.val_int, NULL);
3487       break;
3488     case DW_OP_plus_uconst:
3489       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3490       break;
3491     case DW_OP_breg0:
3492     case DW_OP_breg1:
3493     case DW_OP_breg2:
3494     case DW_OP_breg3:
3495     case DW_OP_breg4:
3496     case DW_OP_breg5:
3497     case DW_OP_breg6:
3498     case DW_OP_breg7:
3499     case DW_OP_breg8:
3500     case DW_OP_breg9:
3501     case DW_OP_breg10:
3502     case DW_OP_breg11:
3503     case DW_OP_breg12:
3504     case DW_OP_breg13:
3505     case DW_OP_breg14:
3506     case DW_OP_breg15:
3507     case DW_OP_breg16:
3508     case DW_OP_breg17:
3509     case DW_OP_breg18:
3510     case DW_OP_breg19:
3511     case DW_OP_breg20:
3512     case DW_OP_breg21:
3513     case DW_OP_breg22:
3514     case DW_OP_breg23:
3515     case DW_OP_breg24:
3516     case DW_OP_breg25:
3517     case DW_OP_breg26:
3518     case DW_OP_breg27:
3519     case DW_OP_breg28:
3520     case DW_OP_breg29:
3521     case DW_OP_breg30:
3522     case DW_OP_breg31:
3523       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3524       break;
3525     case DW_OP_regx:
3526       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3527       break;
3528     case DW_OP_fbreg:
3529       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3530       break;
3531     case DW_OP_bregx:
3532       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3533       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3534       break;
3535     case DW_OP_piece:
3536       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3537       break;
3538     case DW_OP_deref_size:
3539     case DW_OP_xderef_size:
3540       dw2_asm_output_data (1, val1->v.val_int, NULL);
3541       break;
3542
3543     case INTERNAL_DW_OP_tls_addr:
3544       if (targetm.asm_out.output_dwarf_dtprel)
3545         {
3546           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3547                                                DWARF2_ADDR_SIZE,
3548                                                val1->v.val_addr);
3549           fputc ('\n', asm_out_file);
3550         }
3551       else
3552         gcc_unreachable ();
3553       break;
3554
3555     default:
3556       /* Other codes have no operands.  */
3557       break;
3558     }
3559 }
3560
3561 /* Output a sequence of location operations.  */
3562
3563 static void
3564 output_loc_sequence (dw_loc_descr_ref loc)
3565 {
3566   for (; loc != NULL; loc = loc->dw_loc_next)
3567     {
3568       /* Output the opcode.  */
3569       dw2_asm_output_data (1, loc->dw_loc_opc,
3570                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3571
3572       /* Output the operand(s) (if any).  */
3573       output_loc_operands (loc);
3574     }
3575 }
3576
3577 /* This routine will generate the correct assembly data for a location
3578    description based on a cfi entry with a complex address.  */
3579
3580 static void
3581 output_cfa_loc (dw_cfi_ref cfi)
3582 {
3583   dw_loc_descr_ref loc;
3584   unsigned long size;
3585
3586   /* Output the size of the block.  */
3587   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3588   size = size_of_locs (loc);
3589   dw2_asm_output_data_uleb128 (size, NULL);
3590
3591   /* Now output the operations themselves.  */
3592   output_loc_sequence (loc);
3593 }
3594
3595 /* This function builds a dwarf location descriptor sequence from a
3596    dw_cfa_location, adding the given OFFSET to the result of the
3597    expression.  */
3598
3599 static struct dw_loc_descr_struct *
3600 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3601 {
3602   struct dw_loc_descr_struct *head, *tmp;
3603
3604   offset += cfa->offset;
3605
3606   if (cfa->indirect)
3607     {
3608       if (cfa->base_offset)
3609         {
3610           if (cfa->reg <= 31)
3611             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3612           else
3613             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3614         }
3615       else if (cfa->reg <= 31)
3616         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3617       else
3618         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3619
3620       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3621       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3622       add_loc_descr (&head, tmp);
3623       if (offset != 0)
3624         {
3625           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3626           add_loc_descr (&head, tmp);
3627         }
3628     }
3629   else
3630     {
3631       if (offset == 0)
3632         if (cfa->reg <= 31)
3633           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3634         else
3635           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3636       else if (cfa->reg <= 31)
3637         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3638       else
3639         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3640     }
3641
3642   return head;
3643 }
3644
3645 /* This function fills in aa dw_cfa_location structure from a dwarf location
3646    descriptor sequence.  */
3647
3648 static void
3649 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3650 {
3651   struct dw_loc_descr_struct *ptr;
3652   cfa->offset = 0;
3653   cfa->base_offset = 0;
3654   cfa->indirect = 0;
3655   cfa->reg = -1;
3656
3657   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3658     {
3659       enum dwarf_location_atom op = ptr->dw_loc_opc;
3660
3661       switch (op)
3662         {
3663         case DW_OP_reg0:
3664         case DW_OP_reg1:
3665         case DW_OP_reg2:
3666         case DW_OP_reg3:
3667         case DW_OP_reg4:
3668         case DW_OP_reg5:
3669         case DW_OP_reg6:
3670         case DW_OP_reg7:
3671         case DW_OP_reg8:
3672         case DW_OP_reg9:
3673         case DW_OP_reg10:
3674         case DW_OP_reg11:
3675         case DW_OP_reg12:
3676         case DW_OP_reg13:
3677         case DW_OP_reg14:
3678         case DW_OP_reg15:
3679         case DW_OP_reg16:
3680         case DW_OP_reg17:
3681         case DW_OP_reg18:
3682         case DW_OP_reg19:
3683         case DW_OP_reg20:
3684         case DW_OP_reg21:
3685         case DW_OP_reg22:
3686         case DW_OP_reg23:
3687         case DW_OP_reg24:
3688         case DW_OP_reg25:
3689         case DW_OP_reg26:
3690         case DW_OP_reg27:
3691         case DW_OP_reg28:
3692         case DW_OP_reg29:
3693         case DW_OP_reg30:
3694         case DW_OP_reg31:
3695           cfa->reg = op - DW_OP_reg0;
3696           break;
3697         case DW_OP_regx:
3698           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3699           break;
3700         case DW_OP_breg0:
3701         case DW_OP_breg1:
3702         case DW_OP_breg2:
3703         case DW_OP_breg3:
3704         case DW_OP_breg4:
3705         case DW_OP_breg5:
3706         case DW_OP_breg6:
3707         case DW_OP_breg7:
3708         case DW_OP_breg8:
3709         case DW_OP_breg9:
3710         case DW_OP_breg10:
3711         case DW_OP_breg11:
3712         case DW_OP_breg12:
3713         case DW_OP_breg13:
3714         case DW_OP_breg14:
3715         case DW_OP_breg15:
3716         case DW_OP_breg16:
3717         case DW_OP_breg17:
3718         case DW_OP_breg18:
3719         case DW_OP_breg19:
3720         case DW_OP_breg20:
3721         case DW_OP_breg21:
3722         case DW_OP_breg22:
3723         case DW_OP_breg23:
3724         case DW_OP_breg24:
3725         case DW_OP_breg25:
3726         case DW_OP_breg26:
3727         case DW_OP_breg27:
3728         case DW_OP_breg28:
3729         case DW_OP_breg29:
3730         case DW_OP_breg30:
3731         case DW_OP_breg31:
3732           cfa->reg = op - DW_OP_breg0;
3733           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3734           break;
3735         case DW_OP_bregx:
3736           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3737           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3738           break;
3739         case DW_OP_deref:
3740           cfa->indirect = 1;
3741           break;
3742         case DW_OP_plus_uconst:
3743           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3744           break;
3745         default:
3746           internal_error ("DW_LOC_OP %s not implemented",
3747                           dwarf_stack_op_name (ptr->dw_loc_opc));
3748         }
3749     }
3750 }
3751 #endif /* .debug_frame support */
3752 \f
3753 /* And now, the support for symbolic debugging information.  */
3754 #ifdef DWARF2_DEBUGGING_INFO
3755
3756 /* .debug_str support.  */
3757 static int output_indirect_string (void **, void *);
3758
3759 static void dwarf2out_init (const char *);
3760 static void dwarf2out_finish (const char *);
3761 static void dwarf2out_define (unsigned int, const char *);
3762 static void dwarf2out_undef (unsigned int, const char *);
3763 static void dwarf2out_start_source_file (unsigned, const char *);
3764 static void dwarf2out_end_source_file (unsigned);
3765 static void dwarf2out_begin_block (unsigned, unsigned);
3766 static void dwarf2out_end_block (unsigned, unsigned);
3767 static bool dwarf2out_ignore_block (const_tree);
3768 static void dwarf2out_global_decl (tree);
3769 static void dwarf2out_type_decl (tree, int);
3770 static void dwarf2out_imported_module_or_decl (tree, tree);
3771 static void dwarf2out_abstract_function (tree);
3772 static void dwarf2out_var_location (rtx);
3773 static void dwarf2out_begin_function (tree);
3774
3775 /* The debug hooks structure.  */
3776
3777 const struct gcc_debug_hooks dwarf2_debug_hooks =
3778 {
3779   dwarf2out_init,
3780   dwarf2out_finish,
3781   dwarf2out_define,
3782   dwarf2out_undef,
3783   dwarf2out_start_source_file,
3784   dwarf2out_end_source_file,
3785   dwarf2out_begin_block,
3786   dwarf2out_end_block,
3787   dwarf2out_ignore_block,
3788   dwarf2out_source_line,
3789   dwarf2out_begin_prologue,
3790   debug_nothing_int_charstar,   /* end_prologue */
3791   dwarf2out_end_epilogue,
3792   dwarf2out_begin_function,
3793   debug_nothing_int,            /* end_function */
3794   dwarf2out_decl,               /* function_decl */
3795   dwarf2out_global_decl,
3796   dwarf2out_type_decl,          /* type_decl */
3797   dwarf2out_imported_module_or_decl,
3798   debug_nothing_tree,           /* deferred_inline_function */
3799   /* The DWARF 2 backend tries to reduce debugging bloat by not
3800      emitting the abstract description of inline functions until
3801      something tries to reference them.  */
3802   dwarf2out_abstract_function,  /* outlining_inline_function */
3803   debug_nothing_rtx,            /* label */
3804   debug_nothing_int,            /* handle_pch */
3805   dwarf2out_var_location,
3806   dwarf2out_switch_text_section,
3807   1                             /* start_end_main_source_file */
3808 };
3809 #endif
3810 \f
3811 /* NOTE: In the comments in this file, many references are made to
3812    "Debugging Information Entries".  This term is abbreviated as `DIE'
3813    throughout the remainder of this file.  */
3814
3815 /* An internal representation of the DWARF output is built, and then
3816    walked to generate the DWARF debugging info.  The walk of the internal
3817    representation is done after the entire program has been compiled.
3818    The types below are used to describe the internal representation.  */
3819
3820 /* Various DIE's use offsets relative to the beginning of the
3821    .debug_info section to refer to each other.  */
3822
3823 typedef long int dw_offset;
3824
3825 /* Define typedefs here to avoid circular dependencies.  */
3826
3827 typedef struct dw_attr_struct *dw_attr_ref;
3828 typedef struct dw_line_info_struct *dw_line_info_ref;
3829 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3830 typedef struct pubname_struct *pubname_ref;
3831 typedef struct dw_ranges_struct *dw_ranges_ref;
3832 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3833
3834 /* Each entry in the line_info_table maintains the file and
3835    line number associated with the label generated for that
3836    entry.  The label gives the PC value associated with
3837    the line number entry.  */
3838
3839 typedef struct dw_line_info_struct GTY(())
3840 {
3841   unsigned long dw_file_num;
3842   unsigned long dw_line_num;
3843 }
3844 dw_line_info_entry;
3845
3846 /* Line information for functions in separate sections; each one gets its
3847    own sequence.  */
3848 typedef struct dw_separate_line_info_struct GTY(())
3849 {
3850   unsigned long dw_file_num;
3851   unsigned long dw_line_num;
3852   unsigned long function;
3853 }
3854 dw_separate_line_info_entry;
3855
3856 /* Each DIE attribute has a field specifying the attribute kind,
3857    a link to the next attribute in the chain, and an attribute value.
3858    Attributes are typically linked below the DIE they modify.  */
3859
3860 typedef struct dw_attr_struct GTY(())
3861 {
3862   enum dwarf_attribute dw_attr;
3863   dw_val_node dw_attr_val;
3864 }
3865 dw_attr_node;
3866
3867 DEF_VEC_O(dw_attr_node);
3868 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3869
3870 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3871    The children of each node form a circular list linked by
3872    die_sib.  die_child points to the node *before* the "first" child node.  */
3873
3874 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
3875 {
3876   enum dwarf_tag die_tag;
3877   char *die_symbol;
3878   VEC(dw_attr_node,gc) * die_attr;
3879   dw_die_ref die_parent;
3880   dw_die_ref die_child;
3881   dw_die_ref die_sib;
3882   dw_die_ref die_definition; /* ref from a specification to its definition */
3883   dw_offset die_offset;
3884   unsigned long die_abbrev;
3885   int die_mark;
3886   /* Die is used and must not be pruned as unused.  */
3887   int die_perennial_p;
3888   unsigned int decl_id;
3889 }
3890 die_node;
3891
3892 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3893 #define FOR_EACH_CHILD(die, c, expr) do {       \
3894   c = die->die_child;                           \
3895   if (c) do {                                   \
3896     c = c->die_sib;                             \
3897     expr;                                       \
3898   } while (c != die->die_child);                \
3899 } while (0)
3900
3901 /* The pubname structure */
3902
3903 typedef struct pubname_struct GTY(())
3904 {
3905   dw_die_ref die;
3906   const char *name;
3907 }
3908 pubname_entry;
3909
3910 DEF_VEC_O(pubname_entry);
3911 DEF_VEC_ALLOC_O(pubname_entry, gc);
3912
3913 struct dw_ranges_struct GTY(())
3914 {
3915   /* If this is positive, it's a block number, otherwise it's a
3916      bitwise-negated index into dw_ranges_by_label.  */
3917   int num;
3918 };
3919
3920 struct dw_ranges_by_label_struct GTY(())
3921 {
3922   const char *begin;
3923   const char *end;
3924 };
3925
3926 /* The limbo die list structure.  */
3927 typedef struct limbo_die_struct GTY(())
3928 {
3929   dw_die_ref die;
3930   tree created_for;
3931   struct limbo_die_struct *next;
3932 }
3933 limbo_die_node;
3934
3935 /* How to start an assembler comment.  */
3936 #ifndef ASM_COMMENT_START
3937 #define ASM_COMMENT_START ";#"
3938 #endif
3939
3940 /* Define a macro which returns nonzero for a TYPE_DECL which was
3941    implicitly generated for a tagged type.
3942
3943    Note that unlike the gcc front end (which generates a NULL named
3944    TYPE_DECL node for each complete tagged type, each array type, and
3945    each function type node created) the g++ front end generates a
3946    _named_ TYPE_DECL node for each tagged type node created.
3947    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3948    generate a DW_TAG_typedef DIE for them.  */
3949
3950 #define TYPE_DECL_IS_STUB(decl)                         \
3951   (DECL_NAME (decl) == NULL_TREE                        \
3952    || (DECL_ARTIFICIAL (decl)                           \
3953        && is_tagged_type (TREE_TYPE (decl))             \
3954        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3955            /* This is necessary for stub decls that     \
3956               appear in nested inline functions.  */    \
3957            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3958                && (decl_ultimate_origin (decl)          \
3959                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3960
3961 /* Information concerning the compilation unit's programming
3962    language, and compiler version.  */
3963
3964 /* Fixed size portion of the DWARF compilation unit header.  */
3965 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3966   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3967
3968 /* Fixed size portion of public names info.  */
3969 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3970
3971 /* Fixed size portion of the address range info.  */
3972 #define DWARF_ARANGES_HEADER_SIZE                                       \
3973   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3974                 DWARF2_ADDR_SIZE * 2)                                   \
3975    - DWARF_INITIAL_LENGTH_SIZE)
3976
3977 /* Size of padding portion in the address range info.  It must be
3978    aligned to twice the pointer size.  */
3979 #define DWARF_ARANGES_PAD_SIZE \
3980   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3981                 DWARF2_ADDR_SIZE * 2)                              \
3982    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3983
3984 /* Use assembler line directives if available.  */
3985 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3986 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3987 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3988 #else
3989 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3990 #endif
3991 #endif
3992
3993 /* Minimum line offset in a special line info. opcode.
3994    This value was chosen to give a reasonable range of values.  */
3995 #define DWARF_LINE_BASE  -10
3996
3997 /* First special line opcode - leave room for the standard opcodes.  */
3998 #define DWARF_LINE_OPCODE_BASE  10
3999
4000 /* Range of line offsets in a special line info. opcode.  */
4001 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4002
4003 /* Flag that indicates the initial value of the is_stmt_start flag.
4004    In the present implementation, we do not mark any lines as
4005    the beginning of a source statement, because that information
4006    is not made available by the GCC front-end.  */
4007 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4008
4009 #ifdef DWARF2_DEBUGGING_INFO
4010 /* This location is used by calc_die_sizes() to keep track
4011    the offset of each DIE within the .debug_info section.  */
4012 static unsigned long next_die_offset;
4013 #endif
4014
4015 /* Record the root of the DIE's built for the current compilation unit.  */
4016 static GTY(()) dw_die_ref comp_unit_die;
4017
4018 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4019 static GTY(()) limbo_die_node *limbo_die_list;
4020
4021 /* Filenames referenced by this compilation unit.  */
4022 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4023
4024 /* A hash table of references to DIE's that describe declarations.
4025    The key is a DECL_UID() which is a unique number identifying each decl.  */
4026 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4027
4028 /* Node of the variable location list.  */
4029 struct var_loc_node GTY ((chain_next ("%h.next")))
4030 {
4031   rtx GTY (()) var_loc_note;
4032   const char * GTY (()) label;
4033   const char * GTY (()) section_label;
4034   struct var_loc_node * GTY (()) next;
4035 };
4036
4037 /* Variable location list.  */
4038 struct var_loc_list_def GTY (())
4039 {
4040   struct var_loc_node * GTY (()) first;
4041
4042   /* Do not mark the last element of the chained list because
4043      it is marked through the chain.  */
4044   struct var_loc_node * GTY ((skip ("%h"))) last;
4045
4046   /* DECL_UID of the variable decl.  */
4047   unsigned int decl_id;
4048 };
4049 typedef struct var_loc_list_def var_loc_list;
4050
4051
4052 /* Table of decl location linked lists.  */
4053 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4054
4055 /* A pointer to the base of a list of references to DIE's that
4056    are uniquely identified by their tag, presence/absence of
4057    children DIE's, and list of attribute/value pairs.  */
4058 static GTY((length ("abbrev_die_table_allocated")))
4059   dw_die_ref *abbrev_die_table;
4060
4061 /* Number of elements currently allocated for abbrev_die_table.  */
4062 static GTY(()) unsigned abbrev_die_table_allocated;
4063
4064 /* Number of elements in type_die_table currently in use.  */
4065 static GTY(()) unsigned abbrev_die_table_in_use;
4066
4067 /* Size (in elements) of increments by which we may expand the
4068    abbrev_die_table.  */
4069 #define ABBREV_DIE_TABLE_INCREMENT 256
4070
4071 /* A pointer to the base of a table that contains line information
4072    for each source code line in .text in the compilation unit.  */
4073 static GTY((length ("line_info_table_allocated")))
4074      dw_line_info_ref line_info_table;
4075
4076 /* Number of elements currently allocated for line_info_table.  */
4077 static GTY(()) unsigned line_info_table_allocated;
4078
4079 /* Number of elements in line_info_table currently in use.  */
4080 static GTY(()) unsigned line_info_table_in_use;
4081
4082 /* A pointer to the base of a table that contains line information
4083    for each source code line outside of .text in the compilation unit.  */
4084 static GTY ((length ("separate_line_info_table_allocated")))
4085      dw_separate_line_info_ref separate_line_info_table;
4086
4087 /* Number of elements currently allocated for separate_line_info_table.  */
4088 static GTY(()) unsigned separate_line_info_table_allocated;
4089
4090 /* Number of elements in separate_line_info_table currently in use.  */
4091 static GTY(()) unsigned separate_line_info_table_in_use;
4092
4093 /* Size (in elements) of increments by which we may expand the
4094    line_info_table.  */
4095 #define LINE_INFO_TABLE_INCREMENT 1024
4096
4097 /* A pointer to the base of a table that contains a list of publicly
4098    accessible names.  */
4099 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4100
4101 /* A pointer to the base of a table that contains a list of publicly
4102    accessible types.  */
4103 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4104
4105 /* Array of dies for which we should generate .debug_arange info.  */
4106 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4107
4108 /* Number of elements currently allocated for arange_table.  */
4109 static GTY(()) unsigned arange_table_allocated;
4110
4111 /* Number of elements in arange_table currently in use.  */
4112 static GTY(()) unsigned arange_table_in_use;
4113
4114 /* Size (in elements) of increments by which we may expand the
4115    arange_table.  */
4116 #define ARANGE_TABLE_INCREMENT 64
4117
4118 /* Array of dies for which we should generate .debug_ranges info.  */
4119 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4120
4121 /* Number of elements currently allocated for ranges_table.  */
4122 static GTY(()) unsigned ranges_table_allocated;
4123
4124 /* Number of elements in ranges_table currently in use.  */
4125 static GTY(()) unsigned ranges_table_in_use;
4126
4127 /* Array of pairs of labels referenced in ranges_table.  */
4128 static GTY ((length ("ranges_by_label_allocated")))
4129      dw_ranges_by_label_ref ranges_by_label;
4130
4131 /* Number of elements currently allocated for ranges_by_label.  */
4132 static GTY(()) unsigned ranges_by_label_allocated;
4133
4134 /* Number of elements in ranges_by_label currently in use.  */
4135 static GTY(()) unsigned ranges_by_label_in_use;
4136
4137 /* Size (in elements) of increments by which we may expand the
4138    ranges_table.  */
4139 #define RANGES_TABLE_INCREMENT 64
4140
4141 /* Whether we have location lists that need outputting */
4142 static GTY(()) bool have_location_lists;
4143
4144 /* Unique label counter.  */
4145 static GTY(()) unsigned int loclabel_num;
4146
4147 #ifdef DWARF2_DEBUGGING_INFO
4148 /* Record whether the function being analyzed contains inlined functions.  */
4149 static int current_function_has_inlines;
4150 #endif
4151 #if 0 && defined (MIPS_DEBUGGING_INFO)
4152 static int comp_unit_has_inlines;
4153 #endif
4154
4155 /* The last file entry emitted by maybe_emit_file().  */
4156 static GTY(()) struct dwarf_file_data * last_emitted_file;
4157
4158 /* Number of internal labels generated by gen_internal_sym().  */
4159 static GTY(()) int label_num;
4160
4161 /* Cached result of previous call to lookup_filename.  */
4162 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4163
4164 #ifdef DWARF2_DEBUGGING_INFO
4165
4166 /* Offset from the "steady-state frame pointer" to the frame base,
4167    within the current function.  */
4168 static HOST_WIDE_INT frame_pointer_fb_offset;
4169
4170 /* Forward declarations for functions defined in this file.  */
4171
4172 static int is_pseudo_reg (const_rtx);
4173 static tree type_main_variant (tree);
4174 static int is_tagged_type (const_tree);
4175 static const char *dwarf_tag_name (unsigned);
4176 static const char *dwarf_attr_name (unsigned);
4177 static const char *dwarf_form_name (unsigned);
4178 static tree decl_ultimate_origin (const_tree);
4179 static tree block_ultimate_origin (const_tree);
4180 static tree decl_class_context (tree);
4181 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4182 static inline enum dw_val_class AT_class (dw_attr_ref);
4183 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4184 static inline unsigned AT_flag (dw_attr_ref);
4185 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4186 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4187 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4188 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4189 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4190                               unsigned long);
4191 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4192                                unsigned int, unsigned char *);
4193 static hashval_t debug_str_do_hash (const void *);
4194 static int debug_str_eq (const void *, const void *);
4195 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4196 static inline const char *AT_string (dw_attr_ref);
4197 static int AT_string_form (dw_attr_ref);
4198 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4199 static void add_AT_specification (dw_die_ref, dw_die_ref);
4200 static inline dw_die_ref AT_ref (dw_attr_ref);
4201 static inline int AT_ref_external (dw_attr_ref);
4202 static inline void set_AT_ref_external (dw_attr_ref, int);
4203 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4204 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4205 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4206 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4207                              dw_loc_list_ref);
4208 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4209 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4210 static inline rtx AT_addr (dw_attr_ref);
4211 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4212 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4213 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4214 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4215                            unsigned HOST_WIDE_INT);
4216 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4217                                unsigned long);
4218 static inline const char *AT_lbl (dw_attr_ref);
4219 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4220 static const char *get_AT_low_pc (dw_die_ref);
4221 static const char *get_AT_hi_pc (dw_die_ref);
4222 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4223 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4224 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4225 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4226 static bool is_c_family (void);
4227 static bool is_cxx (void);
4228 static bool is_java (void);
4229 static bool is_fortran (void);
4230 static bool is_ada (void);
4231 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4232 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4233 static void add_child_die (dw_die_ref, dw_die_ref);
4234 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4235 static dw_die_ref lookup_type_die (tree);
4236 static void equate_type_number_to_die (tree, dw_die_ref);
4237 static hashval_t decl_die_table_hash (const void *);
4238 static int decl_die_table_eq (const void *, const void *);
4239 static dw_die_ref lookup_decl_die (tree);
4240 static hashval_t decl_loc_table_hash (const void *);
4241 static int decl_loc_table_eq (const void *, const void *);
4242 static var_loc_list *lookup_decl_loc (const_tree);
4243 static void equate_decl_number_to_die (tree, dw_die_ref);
4244 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4245 static void print_spaces (FILE *);
4246 static void print_die (dw_die_ref, FILE *);
4247 static void print_dwarf_line_table (FILE *);
4248 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4249 static dw_die_ref pop_compile_unit (dw_die_ref);
4250 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4251 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4252 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4253 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4254 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4255 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4256 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4257 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4258 static void compute_section_prefix (dw_die_ref);
4259 static int is_type_die (dw_die_ref);
4260 static int is_comdat_die (dw_die_ref);
4261 static int is_symbol_die (dw_die_ref);
4262 static void assign_symbol_names (dw_die_ref);
4263 static void break_out_includes (dw_die_ref);
4264 static hashval_t htab_cu_hash (const void *);
4265 static int htab_cu_eq (const void *, const void *);
4266 static void htab_cu_del (void *);
4267 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4268 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4269 static void add_sibling_attributes (dw_die_ref);
4270 static void build_abbrev_table (dw_die_ref);
4271 static void output_location_lists (dw_die_ref);
4272 static int constant_size (long unsigned);
4273 static unsigned long size_of_die (dw_die_ref);
4274 static void calc_die_sizes (dw_die_ref);
4275 static void mark_dies (dw_die_ref);
4276 static void unmark_dies (dw_die_ref);
4277 static void unmark_all_dies (dw_die_ref);
4278 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4279 static unsigned long size_of_aranges (void);
4280 static enum dwarf_form value_format (dw_attr_ref);
4281 static void output_value_format (dw_attr_ref);
4282 static void output_abbrev_section (void);
4283 static void output_die_symbol (dw_die_ref);
4284 static void output_die (dw_die_ref);
4285 static void output_compilation_unit_header (void);
4286 static void output_comp_unit (dw_die_ref, int);
4287 static const char *dwarf2_name (tree, int);
4288 static void add_pubname (tree, dw_die_ref);
4289 static void add_pubname_string (const char *, dw_die_ref);
4290 static void add_pubtype (tree, dw_die_ref);
4291 static void output_pubnames (VEC (pubname_entry,gc) *);
4292 static void add_arange (tree, dw_die_ref);
4293 static void output_aranges (void);
4294 static unsigned int add_ranges_num (int);
4295 static unsigned int add_ranges (const_tree);
4296 static unsigned int add_ranges_by_labels (const char *, const char *);
4297 static void output_ranges (void);
4298 static void output_line_info (void);
4299 static void output_file_names (void);
4300 static dw_die_ref base_type_die (tree);
4301 static int is_base_type (tree);
4302 static bool is_subrange_type (const_tree);
4303 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4304 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4305 static int type_is_enum (const_tree);
4306 static unsigned int dbx_reg_number (const_rtx);
4307 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4308 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4309 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4310                                                 enum var_init_status);
4311 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4312                                                      enum var_init_status);
4313 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4314 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4315                                          enum var_init_status);
4316 static int is_based_loc (const_rtx);
4317 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4318                                             enum var_init_status);
4319 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4320                                                enum var_init_status);
4321 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4322 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4323 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4324 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4325 static tree field_type (const_tree);
4326 static unsigned int simple_type_align_in_bits (const_tree);
4327 static unsigned int simple_decl_align_in_bits (const_tree);
4328 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4329 static HOST_WIDE_INT field_byte_offset (const_tree);
4330 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4331                                          dw_loc_descr_ref);
4332 static void add_data_member_location_attribute (dw_die_ref, tree);
4333 static void add_const_value_attribute (dw_die_ref, rtx);
4334 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4335 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4336 static void insert_float (const_rtx, unsigned char *);
4337 static rtx rtl_for_decl_location (tree);
4338 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4339                                                    enum dwarf_attribute);
4340 static void tree_add_const_value_attribute (dw_die_ref, tree);
4341 static void add_name_attribute (dw_die_ref, const char *);
4342 static void add_comp_dir_attribute (dw_die_ref);
4343 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4344 static void add_subscript_info (dw_die_ref, tree);
4345 static void add_byte_size_attribute (dw_die_ref, tree);
4346 static void add_bit_offset_attribute (dw_die_ref, tree);
4347 static void add_bit_size_attribute (dw_die_ref, tree);
4348 static void add_prototyped_attribute (dw_die_ref, tree);
4349 static void add_abstract_origin_attribute (dw_die_ref, tree);
4350 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4351 static void add_src_coords_attributes (dw_die_ref, tree);
4352 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4353 static void push_decl_scope (tree);
4354 static void pop_decl_scope (void);
4355 static dw_die_ref scope_die_for (tree, dw_die_ref);
4356 static inline int local_scope_p (dw_die_ref);
4357 static inline int class_or_namespace_scope_p (dw_die_ref);
4358 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4359 static void add_calling_convention_attribute (dw_die_ref, tree);
4360 static const char *type_tag (const_tree);
4361 static tree member_declared_type (const_tree);
4362 #if 0
4363 static const char *decl_start_label (tree);
4364 #endif
4365 static void gen_array_type_die (tree, dw_die_ref);
4366 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4367 #if 0
4368 static void gen_entry_point_die (tree, dw_die_ref);
4369 #endif
4370 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4371 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4372 static void gen_inlined_union_type_die (tree, dw_die_ref);
4373 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4374 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4375 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4376 static void gen_formal_types_die (tree, dw_die_ref);
4377 static void gen_subprogram_die (tree, dw_die_ref);
4378 static void gen_variable_die (tree, dw_die_ref);
4379 static void gen_label_die (tree, dw_die_ref);
4380 static void gen_lexical_block_die (tree, dw_die_ref, int);
4381 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4382 static void gen_field_die (tree, dw_die_ref);
4383 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4384 static dw_die_ref gen_compile_unit_die (const char *);
4385 static void gen_inheritance_die (tree, tree, dw_die_ref);
4386 static void gen_member_die (tree, dw_die_ref);
4387 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4388                                                 enum debug_info_usage);
4389 static void gen_subroutine_type_die (tree, dw_die_ref);
4390 static void gen_typedef_die (tree, dw_die_ref);
4391 static void gen_type_die (tree, dw_die_ref);
4392 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4393 static void gen_block_die (tree, dw_die_ref, int);
4394 static void decls_for_scope (tree, dw_die_ref, int);
4395 static int is_redundant_typedef (const_tree);
4396 static void gen_namespace_die (tree);
4397 static void gen_decl_die (tree, dw_die_ref);
4398 static dw_die_ref force_decl_die (tree);
4399 static dw_die_ref force_type_die (tree);
4400 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4401 static void declare_in_namespace (tree, dw_die_ref);
4402 static struct dwarf_file_data * lookup_filename (const char *);
4403 static void retry_incomplete_types (void);
4404 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4405 static void splice_child_die (dw_die_ref, dw_die_ref);
4406 static int file_info_cmp (const void *, const void *);
4407 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4408                                      const char *, const char *, unsigned);
4409 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4410                                        const char *, const char *,
4411                                        const char *);
4412 static void output_loc_list (dw_loc_list_ref);
4413 static char *gen_internal_sym (const char *);
4414
4415 static void prune_unmark_dies (dw_die_ref);
4416 static void prune_unused_types_mark (dw_die_ref, int);
4417 static void prune_unused_types_walk (dw_die_ref);
4418 static void prune_unused_types_walk_attribs (dw_die_ref);
4419 static void prune_unused_types_prune (dw_die_ref);
4420 static void prune_unused_types (void);
4421 static int maybe_emit_file (struct dwarf_file_data *fd);
4422
4423 /* Section names used to hold DWARF debugging information.  */
4424 #ifndef DEBUG_INFO_SECTION
4425 #define DEBUG_INFO_SECTION      ".debug_info"
4426 #endif
4427 #ifndef DEBUG_ABBREV_SECTION
4428 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4429 #endif
4430 #ifndef DEBUG_ARANGES_SECTION
4431 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4432 #endif
4433 #ifndef DEBUG_MACINFO_SECTION
4434 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4435 #endif
4436 #ifndef DEBUG_LINE_SECTION
4437 #define DEBUG_LINE_SECTION      ".debug_line"
4438 #endif
4439 #ifndef DEBUG_LOC_SECTION
4440 #define DEBUG_LOC_SECTION       ".debug_loc"
4441 #endif
4442 #ifndef DEBUG_PUBNAMES_SECTION
4443 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4444 #endif
4445 #ifndef DEBUG_STR_SECTION
4446 #define DEBUG_STR_SECTION       ".debug_str"
4447 #endif
4448 #ifndef DEBUG_RANGES_SECTION
4449 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4450 #endif
4451
4452 /* Standard ELF section names for compiled code and data.  */
4453 #ifndef TEXT_SECTION_NAME
4454 #define TEXT_SECTION_NAME       ".text"
4455 #endif
4456
4457 /* Section flags for .debug_str section.  */
4458 #define DEBUG_STR_SECTION_FLAGS \
4459   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4460    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4461    : SECTION_DEBUG)
4462
4463 /* Labels we insert at beginning sections we can reference instead of
4464    the section names themselves.  */
4465
4466 #ifndef TEXT_SECTION_LABEL
4467 #define TEXT_SECTION_LABEL              "Ltext"
4468 #endif
4469 #ifndef COLD_TEXT_SECTION_LABEL
4470 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4471 #endif
4472 #ifndef DEBUG_LINE_SECTION_LABEL
4473 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4474 #endif
4475 #ifndef DEBUG_INFO_SECTION_LABEL
4476 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4477 #endif
4478 #ifndef DEBUG_ABBREV_SECTION_LABEL
4479 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4480 #endif
4481 #ifndef DEBUG_LOC_SECTION_LABEL
4482 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4483 #endif
4484 #ifndef DEBUG_RANGES_SECTION_LABEL
4485 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4486 #endif
4487 #ifndef DEBUG_MACINFO_SECTION_LABEL
4488 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4489 #endif
4490
4491 /* Definitions of defaults for formats and names of various special
4492    (artificial) labels which may be generated within this file (when the -g
4493    options is used and DWARF2_DEBUGGING_INFO is in effect.
4494    If necessary, these may be overridden from within the tm.h file, but
4495    typically, overriding these defaults is unnecessary.  */
4496
4497 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4498 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4499 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4500 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4501 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4502 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4503 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4504 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4505 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4506 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4507
4508 #ifndef TEXT_END_LABEL
4509 #define TEXT_END_LABEL          "Letext"
4510 #endif
4511 #ifndef COLD_END_LABEL
4512 #define COLD_END_LABEL          "Letext_cold"
4513 #endif
4514 #ifndef BLOCK_BEGIN_LABEL
4515 #define BLOCK_BEGIN_LABEL       "LBB"
4516 #endif
4517 #ifndef BLOCK_END_LABEL
4518 #define BLOCK_END_LABEL         "LBE"
4519 #endif
4520 #ifndef LINE_CODE_LABEL
4521 #define LINE_CODE_LABEL         "LM"
4522 #endif
4523 #ifndef SEPARATE_LINE_CODE_LABEL
4524 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4525 #endif
4526
4527 \f
4528 /* We allow a language front-end to designate a function that is to be
4529    called to "demangle" any name before it is put into a DIE.  */
4530
4531 static const char *(*demangle_name_func) (const char *);
4532
4533 void
4534 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4535 {
4536   demangle_name_func = func;
4537 }
4538
4539 /* Test if rtl node points to a pseudo register.  */
4540
4541 static inline int
4542 is_pseudo_reg (const_rtx rtl)
4543 {
4544   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4545           || (GET_CODE (rtl) == SUBREG
4546               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4547 }
4548
4549 /* Return a reference to a type, with its const and volatile qualifiers
4550    removed.  */
4551
4552 static inline tree
4553 type_main_variant (tree type)
4554 {
4555   type = TYPE_MAIN_VARIANT (type);
4556
4557   /* ??? There really should be only one main variant among any group of
4558      variants of a given type (and all of the MAIN_VARIANT values for all
4559      members of the group should point to that one type) but sometimes the C
4560      front-end messes this up for array types, so we work around that bug
4561      here.  */
4562   if (TREE_CODE (type) == ARRAY_TYPE)
4563     while (type != TYPE_MAIN_VARIANT (type))
4564       type = TYPE_MAIN_VARIANT (type);
4565
4566   return type;
4567 }
4568
4569 /* Return nonzero if the given type node represents a tagged type.  */
4570
4571 static inline int
4572 is_tagged_type (const_tree type)
4573 {
4574   enum tree_code code = TREE_CODE (type);
4575
4576   return (code == RECORD_TYPE || code == UNION_TYPE
4577           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4578 }
4579
4580 /* Convert a DIE tag into its string name.  */
4581
4582 static const char *
4583 dwarf_tag_name (unsigned int tag)
4584 {
4585   switch (tag)
4586     {
4587     case DW_TAG_padding:
4588       return "DW_TAG_padding";
4589     case DW_TAG_array_type:
4590       return "DW_TAG_array_type";
4591     case DW_TAG_class_type:
4592       return "DW_TAG_class_type";
4593     case DW_TAG_entry_point:
4594       return "DW_TAG_entry_point";
4595     case DW_TAG_enumeration_type:
4596       return "DW_TAG_enumeration_type";
4597     case DW_TAG_formal_parameter:
4598       return "DW_TAG_formal_parameter";
4599     case DW_TAG_imported_declaration:
4600       return "DW_TAG_imported_declaration";
4601     case DW_TAG_label:
4602       return "DW_TAG_label";
4603     case DW_TAG_lexical_block:
4604       return "DW_TAG_lexical_block";
4605     case DW_TAG_member:
4606       return "DW_TAG_member";
4607     case DW_TAG_pointer_type:
4608       return "DW_TAG_pointer_type";
4609     case DW_TAG_reference_type:
4610       return "DW_TAG_reference_type";
4611     case DW_TAG_compile_unit:
4612       return "DW_TAG_compile_unit";
4613     case DW_TAG_string_type:
4614       return "DW_TAG_string_type";
4615     case DW_TAG_structure_type:
4616       return "DW_TAG_structure_type";
4617     case DW_TAG_subroutine_type:
4618       return "DW_TAG_subroutine_type";
4619     case DW_TAG_typedef:
4620       return "DW_TAG_typedef";
4621     case DW_TAG_union_type:
4622       return "DW_TAG_union_type";
4623     case DW_TAG_unspecified_parameters:
4624       return "DW_TAG_unspecified_parameters";
4625     case DW_TAG_variant:
4626       return "DW_TAG_variant";
4627     case DW_TAG_common_block:
4628       return "DW_TAG_common_block";
4629     case DW_TAG_common_inclusion:
4630       return "DW_TAG_common_inclusion";
4631     case DW_TAG_inheritance:
4632       return "DW_TAG_inheritance";
4633     case DW_TAG_inlined_subroutine:
4634       return "DW_TAG_inlined_subroutine";
4635     case DW_TAG_module:
4636       return "DW_TAG_module";
4637     case DW_TAG_ptr_to_member_type:
4638       return "DW_TAG_ptr_to_member_type";
4639     case DW_TAG_set_type:
4640       return "DW_TAG_set_type";
4641     case DW_TAG_subrange_type:
4642       return "DW_TAG_subrange_type";
4643     case DW_TAG_with_stmt:
4644       return "DW_TAG_with_stmt";
4645     case DW_TAG_access_declaration:
4646       return "DW_TAG_access_declaration";
4647     case DW_TAG_base_type:
4648       return "DW_TAG_base_type";
4649     case DW_TAG_catch_block:
4650       return "DW_TAG_catch_block";
4651     case DW_TAG_const_type:
4652       return "DW_TAG_const_type";
4653     case DW_TAG_constant:
4654       return "DW_TAG_constant";
4655     case DW_TAG_enumerator:
4656       return "DW_TAG_enumerator";
4657     case DW_TAG_file_type:
4658       return "DW_TAG_file_type";
4659     case DW_TAG_friend:
4660       return "DW_TAG_friend";
4661     case DW_TAG_namelist:
4662       return "DW_TAG_namelist";
4663     case DW_TAG_namelist_item:
4664       return "DW_TAG_namelist_item";
4665     case DW_TAG_packed_type:
4666       return "DW_TAG_packed_type";
4667     case DW_TAG_subprogram:
4668       return "DW_TAG_subprogram";
4669     case DW_TAG_template_type_param:
4670       return "DW_TAG_template_type_param";
4671     case DW_TAG_template_value_param:
4672       return "DW_TAG_template_value_param";
4673     case DW_TAG_thrown_type:
4674       return "DW_TAG_thrown_type";
4675     case DW_TAG_try_block:
4676       return "DW_TAG_try_block";
4677     case DW_TAG_variant_part:
4678       return "DW_TAG_variant_part";
4679     case DW_TAG_variable:
4680       return "DW_TAG_variable";
4681     case DW_TAG_volatile_type:
4682       return "DW_TAG_volatile_type";
4683     case DW_TAG_dwarf_procedure:
4684       return "DW_TAG_dwarf_procedure";
4685     case DW_TAG_restrict_type:
4686       return "DW_TAG_restrict_type";
4687     case DW_TAG_interface_type:
4688       return "DW_TAG_interface_type";
4689     case DW_TAG_namespace:
4690       return "DW_TAG_namespace";
4691     case DW_TAG_imported_module:
4692       return "DW_TAG_imported_module";
4693     case DW_TAG_unspecified_type:
4694       return "DW_TAG_unspecified_type";
4695     case DW_TAG_partial_unit:
4696       return "DW_TAG_partial_unit";
4697     case DW_TAG_imported_unit:
4698       return "DW_TAG_imported_unit";
4699     case DW_TAG_condition:
4700       return "DW_TAG_condition";
4701     case DW_TAG_shared_type:
4702       return "DW_TAG_shared_type";
4703     case DW_TAG_MIPS_loop:
4704       return "DW_TAG_MIPS_loop";
4705     case DW_TAG_format_label:
4706       return "DW_TAG_format_label";
4707     case DW_TAG_function_template:
4708       return "DW_TAG_function_template";
4709     case DW_TAG_class_template:
4710       return "DW_TAG_class_template";
4711     case DW_TAG_GNU_BINCL:
4712       return "DW_TAG_GNU_BINCL";
4713     case DW_TAG_GNU_EINCL:
4714       return "DW_TAG_GNU_EINCL";
4715     default:
4716       return "DW_TAG_<unknown>";
4717     }
4718 }
4719
4720 /* Convert a DWARF attribute code into its string name.  */
4721
4722 static const char *
4723 dwarf_attr_name (unsigned int attr)
4724 {
4725   switch (attr)
4726     {
4727     case DW_AT_sibling:
4728       return "DW_AT_sibling";
4729     case DW_AT_location:
4730       return "DW_AT_location";
4731     case DW_AT_name:
4732       return "DW_AT_name";
4733     case DW_AT_ordering:
4734       return "DW_AT_ordering";
4735     case DW_AT_subscr_data:
4736       return "DW_AT_subscr_data";
4737     case DW_AT_byte_size:
4738       return "DW_AT_byte_size";
4739     case DW_AT_bit_offset:
4740       return "DW_AT_bit_offset";
4741     case DW_AT_bit_size:
4742       return "DW_AT_bit_size";
4743     case DW_AT_element_list:
4744       return "DW_AT_element_list";
4745     case DW_AT_stmt_list:
4746       return "DW_AT_stmt_list";
4747     case DW_AT_low_pc:
4748       return "DW_AT_low_pc";
4749     case DW_AT_high_pc:
4750       return "DW_AT_high_pc";
4751     case DW_AT_language:
4752       return "DW_AT_language";
4753     case DW_AT_member:
4754       return "DW_AT_member";
4755     case DW_AT_discr:
4756       return "DW_AT_discr";
4757     case DW_AT_discr_value:
4758       return "DW_AT_discr_value";
4759     case DW_AT_visibility:
4760       return "DW_AT_visibility";
4761     case DW_AT_import:
4762       return "DW_AT_import";
4763     case DW_AT_string_length:
4764       return "DW_AT_string_length";
4765     case DW_AT_common_reference:
4766       return "DW_AT_common_reference";
4767     case DW_AT_comp_dir:
4768       return "DW_AT_comp_dir";
4769     case DW_AT_const_value:
4770       return "DW_AT_const_value";
4771     case DW_AT_containing_type:
4772       return "DW_AT_containing_type";
4773     case DW_AT_default_value:
4774       return "DW_AT_default_value";
4775     case DW_AT_inline:
4776       return "DW_AT_inline";
4777     case DW_AT_is_optional:
4778       return "DW_AT_is_optional";
4779     case DW_AT_lower_bound:
4780       return "DW_AT_lower_bound";
4781     case DW_AT_producer:
4782       return "DW_AT_producer";
4783     case DW_AT_prototyped:
4784       return "DW_AT_prototyped";
4785     case DW_AT_return_addr:
4786       return "DW_AT_return_addr";
4787     case DW_AT_start_scope:
4788       return "DW_AT_start_scope";
4789     case DW_AT_bit_stride:
4790       return "DW_AT_bit_stride";
4791     case DW_AT_upper_bound:
4792       return "DW_AT_upper_bound";
4793     case DW_AT_abstract_origin:
4794       return "DW_AT_abstract_origin";
4795     case DW_AT_accessibility:
4796       return "DW_AT_accessibility";
4797     case DW_AT_address_class:
4798       return "DW_AT_address_class";
4799     case DW_AT_artificial:
4800       return "DW_AT_artificial";
4801     case DW_AT_base_types:
4802       return "DW_AT_base_types";
4803     case DW_AT_calling_convention:
4804       return "DW_AT_calling_convention";
4805     case DW_AT_count:
4806       return "DW_AT_count";
4807     case DW_AT_data_member_location:
4808       return "DW_AT_data_member_location";
4809     case DW_AT_decl_column:
4810       return "DW_AT_decl_column";
4811     case DW_AT_decl_file:
4812       return "DW_AT_decl_file";
4813     case DW_AT_decl_line:
4814       return "DW_AT_decl_line";
4815     case DW_AT_declaration:
4816       return "DW_AT_declaration";
4817     case DW_AT_discr_list:
4818       return "DW_AT_discr_list";
4819     case DW_AT_encoding:
4820       return "DW_AT_encoding";
4821     case DW_AT_external:
4822       return "DW_AT_external";
4823     case DW_AT_frame_base:
4824       return "DW_AT_frame_base";
4825     case DW_AT_friend:
4826       return "DW_AT_friend";
4827     case DW_AT_identifier_case:
4828       return "DW_AT_identifier_case";
4829     case DW_AT_macro_info:
4830       return "DW_AT_macro_info";
4831     case DW_AT_namelist_items:
4832       return "DW_AT_namelist_items";
4833     case DW_AT_priority:
4834       return "DW_AT_priority";
4835     case DW_AT_segment:
4836       return "DW_AT_segment";
4837     case DW_AT_specification:
4838       return "DW_AT_specification";
4839     case DW_AT_static_link:
4840       return "DW_AT_static_link";
4841     case DW_AT_type:
4842       return "DW_AT_type";
4843     case DW_AT_use_location:
4844       return "DW_AT_use_location";
4845     case DW_AT_variable_parameter:
4846       return "DW_AT_variable_parameter";
4847     case DW_AT_virtuality:
4848       return "DW_AT_virtuality";
4849     case DW_AT_vtable_elem_location:
4850       return "DW_AT_vtable_elem_location";
4851
4852     case DW_AT_allocated:
4853       return "DW_AT_allocated";
4854     case DW_AT_associated:
4855       return "DW_AT_associated";
4856     case DW_AT_data_location:
4857       return "DW_AT_data_location";
4858     case DW_AT_byte_stride:
4859       return "DW_AT_byte_stride";
4860     case DW_AT_entry_pc:
4861       return "DW_AT_entry_pc";
4862     case DW_AT_use_UTF8:
4863       return "DW_AT_use_UTF8";
4864     case DW_AT_extension:
4865       return "DW_AT_extension";
4866     case DW_AT_ranges:
4867       return "DW_AT_ranges";
4868     case DW_AT_trampoline:
4869       return "DW_AT_trampoline";
4870     case DW_AT_call_column:
4871       return "DW_AT_call_column";
4872     case DW_AT_call_file:
4873       return "DW_AT_call_file";
4874     case DW_AT_call_line:
4875       return "DW_AT_call_line";
4876
4877     case DW_AT_MIPS_fde:
4878       return "DW_AT_MIPS_fde";
4879     case DW_AT_MIPS_loop_begin:
4880       return "DW_AT_MIPS_loop_begin";
4881     case DW_AT_MIPS_tail_loop_begin:
4882       return "DW_AT_MIPS_tail_loop_begin";
4883     case DW_AT_MIPS_epilog_begin:
4884       return "DW_AT_MIPS_epilog_begin";
4885     case DW_AT_MIPS_loop_unroll_factor:
4886       return "DW_AT_MIPS_loop_unroll_factor";
4887     case DW_AT_MIPS_software_pipeline_depth:
4888       return "DW_AT_MIPS_software_pipeline_depth";
4889     case DW_AT_MIPS_linkage_name:
4890       return "DW_AT_MIPS_linkage_name";
4891     case DW_AT_MIPS_stride:
4892       return "DW_AT_MIPS_stride";
4893     case DW_AT_MIPS_abstract_name:
4894       return "DW_AT_MIPS_abstract_name";
4895     case DW_AT_MIPS_clone_origin:
4896       return "DW_AT_MIPS_clone_origin";
4897     case DW_AT_MIPS_has_inlines:
4898       return "DW_AT_MIPS_has_inlines";
4899
4900     case DW_AT_sf_names:
4901       return "DW_AT_sf_names";
4902     case DW_AT_src_info:
4903       return "DW_AT_src_info";
4904     case DW_AT_mac_info:
4905       return "DW_AT_mac_info";
4906     case DW_AT_src_coords:
4907       return "DW_AT_src_coords";
4908     case DW_AT_body_begin:
4909       return "DW_AT_body_begin";
4910     case DW_AT_body_end:
4911       return "DW_AT_body_end";
4912     case DW_AT_GNU_vector:
4913       return "DW_AT_GNU_vector";
4914
4915     case DW_AT_VMS_rtnbeg_pd_address:
4916       return "DW_AT_VMS_rtnbeg_pd_address";
4917
4918     default:
4919       return "DW_AT_<unknown>";
4920     }
4921 }
4922
4923 /* Convert a DWARF value form code into its string name.  */
4924
4925 static const char *
4926 dwarf_form_name (unsigned int form)
4927 {
4928   switch (form)
4929     {
4930     case DW_FORM_addr:
4931       return "DW_FORM_addr";
4932     case DW_FORM_block2:
4933       return "DW_FORM_block2";
4934     case DW_FORM_block4:
4935       return "DW_FORM_block4";
4936     case DW_FORM_data2:
4937       return "DW_FORM_data2";
4938     case DW_FORM_data4:
4939       return "DW_FORM_data4";
4940     case DW_FORM_data8:
4941       return "DW_FORM_data8";
4942     case DW_FORM_string:
4943       return "DW_FORM_string";
4944     case DW_FORM_block:
4945       return "DW_FORM_block";
4946     case DW_FORM_block1:
4947       return "DW_FORM_block1";
4948     case DW_FORM_data1:
4949       return "DW_FORM_data1";
4950     case DW_FORM_flag:
4951       return "DW_FORM_flag";
4952     case DW_FORM_sdata:
4953       return "DW_FORM_sdata";
4954     case DW_FORM_strp:
4955       return "DW_FORM_strp";
4956     case DW_FORM_udata:
4957       return "DW_FORM_udata";
4958     case DW_FORM_ref_addr:
4959       return "DW_FORM_ref_addr";
4960     case DW_FORM_ref1:
4961       return "DW_FORM_ref1";
4962     case DW_FORM_ref2:
4963       return "DW_FORM_ref2";
4964     case DW_FORM_ref4:
4965       return "DW_FORM_ref4";
4966     case DW_FORM_ref8:
4967       return "DW_FORM_ref8";
4968     case DW_FORM_ref_udata:
4969       return "DW_FORM_ref_udata";
4970     case DW_FORM_indirect:
4971       return "DW_FORM_indirect";
4972     default:
4973       return "DW_FORM_<unknown>";
4974     }
4975 }
4976 \f
4977 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4978    instance of an inlined instance of a decl which is local to an inline
4979    function, so we have to trace all of the way back through the origin chain
4980    to find out what sort of node actually served as the original seed for the
4981    given block.  */
4982
4983 static tree
4984 decl_ultimate_origin (const_tree decl)
4985 {
4986   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4987     return NULL_TREE;
4988
4989   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4990      nodes in the function to point to themselves; ignore that if
4991      we're trying to output the abstract instance of this function.  */
4992   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4993     return NULL_TREE;
4994
4995   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4996      most distant ancestor, this should never happen.  */
4997   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4998
4999   return DECL_ABSTRACT_ORIGIN (decl);
5000 }
5001
5002 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5003    instance of an inlined instance of a block which is local to an inline
5004    function, so we have to trace all of the way back through the origin chain
5005    to find out what sort of node actually served as the original seed for the
5006    given block.  */
5007
5008 static tree
5009 block_ultimate_origin (const_tree block)
5010 {
5011   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5012
5013   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5014      nodes in the function to point to themselves; ignore that if
5015      we're trying to output the abstract instance of this function.  */
5016   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5017     return NULL_TREE;
5018
5019   if (immediate_origin == NULL_TREE)
5020     return NULL_TREE;
5021   else
5022     {
5023       tree ret_val;
5024       tree lookahead = immediate_origin;
5025
5026       do
5027         {
5028           ret_val = lookahead;
5029           lookahead = (TREE_CODE (ret_val) == BLOCK
5030                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5031         }
5032       while (lookahead != NULL && lookahead != ret_val);
5033
5034       /* The block's abstract origin chain may not be the *ultimate* origin of
5035          the block. It could lead to a DECL that has an abstract origin set.
5036          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5037          will give us if it has one).  Note that DECL's abstract origins are
5038          supposed to be the most distant ancestor (or so decl_ultimate_origin
5039          claims), so we don't need to loop following the DECL origins.  */
5040       if (DECL_P (ret_val))
5041         return DECL_ORIGIN (ret_val);
5042
5043       return ret_val;
5044     }
5045 }
5046
5047 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5048    of a virtual function may refer to a base class, so we check the 'this'
5049    parameter.  */
5050
5051 static tree
5052 decl_class_context (tree decl)
5053 {
5054   tree context = NULL_TREE;
5055
5056   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5057     context = DECL_CONTEXT (decl);
5058   else
5059     context = TYPE_MAIN_VARIANT
5060       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5061
5062   if (context && !TYPE_P (context))
5063     context = NULL_TREE;
5064
5065   return context;
5066 }
5067 \f
5068 /* Add an attribute/value pair to a DIE.  */
5069
5070 static inline void
5071 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5072 {
5073   /* Maybe this should be an assert?  */
5074   if (die == NULL)
5075     return;
5076
5077   if (die->die_attr == NULL)
5078     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5079   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5080 }
5081
5082 static inline enum dw_val_class
5083 AT_class (dw_attr_ref a)
5084 {
5085   return a->dw_attr_val.val_class;
5086 }
5087
5088 /* Add a flag value attribute to a DIE.  */
5089
5090 static inline void
5091 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5092 {
5093   dw_attr_node attr;
5094
5095   attr.dw_attr = attr_kind;
5096   attr.dw_attr_val.val_class = dw_val_class_flag;
5097   attr.dw_attr_val.v.val_flag = flag;
5098   add_dwarf_attr (die, &attr);
5099 }
5100
5101 static inline unsigned
5102 AT_flag (dw_attr_ref a)
5103 {
5104   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5105   return a->dw_attr_val.v.val_flag;
5106 }
5107
5108 /* Add a signed integer attribute value to a DIE.  */
5109
5110 static inline void
5111 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5112 {
5113   dw_attr_node attr;
5114
5115   attr.dw_attr = attr_kind;
5116   attr.dw_attr_val.val_class = dw_val_class_const;
5117   attr.dw_attr_val.v.val_int = int_val;
5118   add_dwarf_attr (die, &attr);
5119 }
5120
5121 static inline HOST_WIDE_INT
5122 AT_int (dw_attr_ref a)
5123 {
5124   gcc_assert (a && AT_class (a) == dw_val_class_const);
5125   return a->dw_attr_val.v.val_int;
5126 }
5127
5128 /* Add an unsigned integer attribute value to a DIE.  */
5129
5130 static inline void
5131 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5132                  unsigned HOST_WIDE_INT unsigned_val)
5133 {
5134   dw_attr_node attr;
5135
5136   attr.dw_attr = attr_kind;
5137   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5138   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5139   add_dwarf_attr (die, &attr);
5140 }
5141
5142 static inline unsigned HOST_WIDE_INT
5143 AT_unsigned (dw_attr_ref a)
5144 {
5145   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5146   return a->dw_attr_val.v.val_unsigned;
5147 }
5148
5149 /* Add an unsigned double integer attribute value to a DIE.  */
5150
5151 static inline void
5152 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5153                   long unsigned int val_hi, long unsigned int val_low)
5154 {
5155   dw_attr_node attr;
5156
5157   attr.dw_attr = attr_kind;
5158   attr.dw_attr_val.val_class = dw_val_class_long_long;
5159   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5160   attr.dw_attr_val.v.val_long_long.low = val_low;
5161   add_dwarf_attr (die, &attr);
5162 }
5163
5164 /* Add a floating point attribute value to a DIE and return it.  */
5165
5166 static inline void
5167 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5168             unsigned int length, unsigned int elt_size, unsigned char *array)
5169 {
5170   dw_attr_node attr;
5171
5172   attr.dw_attr = attr_kind;
5173   attr.dw_attr_val.val_class = dw_val_class_vec;
5174   attr.dw_attr_val.v.val_vec.length = length;
5175   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5176   attr.dw_attr_val.v.val_vec.array = array;
5177   add_dwarf_attr (die, &attr);
5178 }
5179
5180 /* Hash and equality functions for debug_str_hash.  */
5181
5182 static hashval_t
5183 debug_str_do_hash (const void *x)
5184 {
5185   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5186 }
5187
5188 static int
5189 debug_str_eq (const void *x1, const void *x2)
5190 {
5191   return strcmp ((((const struct indirect_string_node *)x1)->str),
5192                  (const char *)x2) == 0;
5193 }
5194
5195 /* Add a string attribute value to a DIE.  */
5196
5197 static inline void
5198 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5199 {
5200   dw_attr_node attr;
5201   struct indirect_string_node *node;
5202   void **slot;
5203
5204   if (! debug_str_hash)
5205     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5206                                       debug_str_eq, NULL);
5207
5208   slot = htab_find_slot_with_hash (debug_str_hash, str,
5209                                    htab_hash_string (str), INSERT);
5210   if (*slot == NULL)
5211     {
5212       node = (struct indirect_string_node *)
5213                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5214       node->str = ggc_strdup (str);
5215       *slot = node;
5216     }
5217   else
5218     node = (struct indirect_string_node *) *slot;
5219
5220   node->refcount++;
5221
5222   attr.dw_attr = attr_kind;
5223   attr.dw_attr_val.val_class = dw_val_class_str;
5224   attr.dw_attr_val.v.val_str = node;
5225   add_dwarf_attr (die, &attr);
5226 }
5227
5228 static inline const char *
5229 AT_string (dw_attr_ref a)
5230 {
5231   gcc_assert (a && AT_class (a) == dw_val_class_str);
5232   return a->dw_attr_val.v.val_str->str;
5233 }
5234
5235 /* Find out whether a string should be output inline in DIE
5236    or out-of-line in .debug_str section.  */
5237
5238 static int
5239 AT_string_form (dw_attr_ref a)
5240 {
5241   struct indirect_string_node *node;
5242   unsigned int len;
5243   char label[32];
5244
5245   gcc_assert (a && AT_class (a) == dw_val_class_str);
5246
5247   node = a->dw_attr_val.v.val_str;
5248   if (node->form)
5249     return node->form;
5250
5251   len = strlen (node->str) + 1;
5252
5253   /* If the string is shorter or equal to the size of the reference, it is
5254      always better to put it inline.  */
5255   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5256     return node->form = DW_FORM_string;
5257
5258   /* If we cannot expect the linker to merge strings in .debug_str
5259      section, only put it into .debug_str if it is worth even in this
5260      single module.  */
5261   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5262       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5263     return node->form = DW_FORM_string;
5264
5265   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5266   ++dw2_string_counter;
5267   node->label = xstrdup (label);
5268
5269   return node->form = DW_FORM_strp;
5270 }
5271
5272 /* Add a DIE reference attribute value to a DIE.  */
5273
5274 static inline void
5275 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5276 {
5277   dw_attr_node attr;
5278
5279   attr.dw_attr = attr_kind;
5280   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5281   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5282   attr.dw_attr_val.v.val_die_ref.external = 0;
5283   add_dwarf_attr (die, &attr);
5284 }
5285
5286 /* Add an AT_specification attribute to a DIE, and also make the back
5287    pointer from the specification to the definition.  */
5288
5289 static inline void
5290 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5291 {
5292   add_AT_die_ref (die, DW_AT_specification, targ_die);
5293   gcc_assert (!targ_die->die_definition);
5294   targ_die->die_definition = die;
5295 }
5296
5297 static inline dw_die_ref
5298 AT_ref (dw_attr_ref a)
5299 {
5300   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5301   return a->dw_attr_val.v.val_die_ref.die;
5302 }
5303
5304 static inline int
5305 AT_ref_external (dw_attr_ref a)
5306 {
5307   if (a && AT_class (a) == dw_val_class_die_ref)
5308     return a->dw_attr_val.v.val_die_ref.external;
5309
5310   return 0;
5311 }
5312
5313 static inline void
5314 set_AT_ref_external (dw_attr_ref a, int i)
5315 {
5316   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5317   a->dw_attr_val.v.val_die_ref.external = i;
5318 }
5319
5320 /* Add an FDE reference attribute value to a DIE.  */
5321
5322 static inline void
5323 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5324 {
5325   dw_attr_node attr;
5326
5327   attr.dw_attr = attr_kind;
5328   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5329   attr.dw_attr_val.v.val_fde_index = targ_fde;
5330   add_dwarf_attr (die, &attr);
5331 }
5332
5333 /* Add a location description attribute value to a DIE.  */
5334
5335 static inline void
5336 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5337 {
5338   dw_attr_node attr;
5339
5340   attr.dw_attr = attr_kind;
5341   attr.dw_attr_val.val_class = dw_val_class_loc;
5342   attr.dw_attr_val.v.val_loc = loc;
5343   add_dwarf_attr (die, &attr);
5344 }
5345
5346 static inline dw_loc_descr_ref
5347 AT_loc (dw_attr_ref a)
5348 {
5349   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5350   return a->dw_attr_val.v.val_loc;
5351 }
5352
5353 static inline void
5354 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5355 {
5356   dw_attr_node attr;
5357
5358   attr.dw_attr = attr_kind;
5359   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5360   attr.dw_attr_val.v.val_loc_list = loc_list;
5361   add_dwarf_attr (die, &attr);
5362   have_location_lists = true;
5363 }
5364
5365 static inline dw_loc_list_ref
5366 AT_loc_list (dw_attr_ref a)
5367 {
5368   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5369   return a->dw_attr_val.v.val_loc_list;
5370 }
5371
5372 /* Add an address constant attribute value to a DIE.  */
5373
5374 static inline void
5375 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5376 {
5377   dw_attr_node attr;
5378
5379   attr.dw_attr = attr_kind;
5380   attr.dw_attr_val.val_class = dw_val_class_addr;
5381   attr.dw_attr_val.v.val_addr = addr;
5382   add_dwarf_attr (die, &attr);
5383 }
5384
5385 /* Get the RTX from to an address DIE attribute.  */
5386
5387 static inline rtx
5388 AT_addr (dw_attr_ref a)
5389 {
5390   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5391   return a->dw_attr_val.v.val_addr;
5392 }
5393
5394 /* Add a file attribute value to a DIE.  */
5395
5396 static inline void
5397 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5398              struct dwarf_file_data *fd)
5399 {
5400   dw_attr_node attr;
5401
5402   attr.dw_attr = attr_kind;
5403   attr.dw_attr_val.val_class = dw_val_class_file;
5404   attr.dw_attr_val.v.val_file = fd;
5405   add_dwarf_attr (die, &attr);
5406 }
5407
5408 /* Get the dwarf_file_data from a file DIE attribute.  */
5409
5410 static inline struct dwarf_file_data *
5411 AT_file (dw_attr_ref a)
5412 {
5413   gcc_assert (a && AT_class (a) == dw_val_class_file);
5414   return a->dw_attr_val.v.val_file;
5415 }
5416
5417 /* Add a label identifier attribute value to a DIE.  */
5418
5419 static inline void
5420 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5421 {
5422   dw_attr_node attr;
5423
5424   attr.dw_attr = attr_kind;
5425   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5426   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5427   add_dwarf_attr (die, &attr);
5428 }
5429
5430 /* Add a section offset attribute value to a DIE, an offset into the
5431    debug_line section.  */
5432
5433 static inline void
5434 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5435                 const char *label)
5436 {
5437   dw_attr_node attr;
5438
5439   attr.dw_attr = attr_kind;
5440   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5441   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5442   add_dwarf_attr (die, &attr);
5443 }
5444
5445 /* Add a section offset attribute value to a DIE, an offset into the
5446    debug_macinfo section.  */
5447
5448 static inline void
5449 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5450                const char *label)
5451 {
5452   dw_attr_node attr;
5453
5454   attr.dw_attr = attr_kind;
5455   attr.dw_attr_val.val_class = dw_val_class_macptr;
5456   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5457   add_dwarf_attr (die, &attr);
5458 }
5459
5460 /* Add an offset attribute value to a DIE.  */
5461
5462 static inline void
5463 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5464                unsigned HOST_WIDE_INT offset)
5465 {
5466   dw_attr_node attr;
5467
5468   attr.dw_attr = attr_kind;
5469   attr.dw_attr_val.val_class = dw_val_class_offset;
5470   attr.dw_attr_val.v.val_offset = offset;
5471   add_dwarf_attr (die, &attr);
5472 }
5473
5474 /* Add an range_list attribute value to a DIE.  */
5475
5476 static void
5477 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5478                    long unsigned int offset)
5479 {
5480   dw_attr_node attr;
5481
5482   attr.dw_attr = attr_kind;
5483   attr.dw_attr_val.val_class = dw_val_class_range_list;
5484   attr.dw_attr_val.v.val_offset = offset;
5485   add_dwarf_attr (die, &attr);
5486 }
5487
5488 static inline const char *
5489 AT_lbl (dw_attr_ref a)
5490 {
5491   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5492                     || AT_class (a) == dw_val_class_lineptr
5493                     || AT_class (a) == dw_val_class_macptr));
5494   return a->dw_attr_val.v.val_lbl_id;
5495 }
5496
5497 /* Get the attribute of type attr_kind.  */
5498
5499 static dw_attr_ref
5500 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5501 {
5502   dw_attr_ref a;
5503   unsigned ix;
5504   dw_die_ref spec = NULL;
5505
5506   if (! die)
5507     return NULL;
5508
5509   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5510     if (a->dw_attr == attr_kind)
5511       return a;
5512     else if (a->dw_attr == DW_AT_specification
5513              || a->dw_attr == DW_AT_abstract_origin)
5514       spec = AT_ref (a);
5515
5516   if (spec)
5517     return get_AT (spec, attr_kind);
5518
5519   return NULL;
5520 }
5521
5522 /* Return the "low pc" attribute value, typically associated with a subprogram
5523    DIE.  Return null if the "low pc" attribute is either not present, or if it
5524    cannot be represented as an assembler label identifier.  */
5525
5526 static inline const char *
5527 get_AT_low_pc (dw_die_ref die)
5528 {
5529   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5530
5531   return a ? AT_lbl (a) : NULL;
5532 }
5533
5534 /* Return the "high pc" attribute value, typically associated with a subprogram
5535    DIE.  Return null if the "high pc" attribute is either not present, or if it
5536    cannot be represented as an assembler label identifier.  */
5537
5538 static inline const char *
5539 get_AT_hi_pc (dw_die_ref die)
5540 {
5541   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5542
5543   return a ? AT_lbl (a) : NULL;
5544 }
5545
5546 /* Return the value of the string attribute designated by ATTR_KIND, or
5547    NULL if it is not present.  */
5548
5549 static inline const char *
5550 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5551 {
5552   dw_attr_ref a = get_AT (die, attr_kind);
5553
5554   return a ? AT_string (a) : NULL;
5555 }
5556
5557 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5558    if it is not present.  */
5559
5560 static inline int
5561 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5562 {
5563   dw_attr_ref a = get_AT (die, attr_kind);
5564
5565   return a ? AT_flag (a) : 0;
5566 }
5567
5568 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5569    if it is not present.  */
5570
5571 static inline unsigned
5572 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5573 {
5574   dw_attr_ref a = get_AT (die, attr_kind);
5575
5576   return a ? AT_unsigned (a) : 0;
5577 }
5578
5579 static inline dw_die_ref
5580 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5581 {
5582   dw_attr_ref a = get_AT (die, attr_kind);
5583
5584   return a ? AT_ref (a) : NULL;
5585 }
5586
5587 static inline struct dwarf_file_data *
5588 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5589 {
5590   dw_attr_ref a = get_AT (die, attr_kind);
5591
5592   return a ? AT_file (a) : NULL;
5593 }
5594
5595 /* Return TRUE if the language is C or C++.  */
5596
5597 static inline bool
5598 is_c_family (void)
5599 {
5600   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5601
5602   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5603           || lang == DW_LANG_C99
5604           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5605 }
5606
5607 /* Return TRUE if the language is C++.  */
5608
5609 static inline bool
5610 is_cxx (void)
5611 {
5612   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5613
5614   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5615 }
5616
5617 /* Return TRUE if the language is Fortran.  */
5618
5619 static inline bool
5620 is_fortran (void)
5621 {
5622   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5623
5624   return (lang == DW_LANG_Fortran77
5625           || lang == DW_LANG_Fortran90
5626           || lang == DW_LANG_Fortran95);
5627 }
5628
5629 /* Return TRUE if the language is Java.  */
5630
5631 static inline bool
5632 is_java (void)
5633 {
5634   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5635
5636   return lang == DW_LANG_Java;
5637 }
5638
5639 /* Return TRUE if the language is Ada.  */
5640
5641 static inline bool
5642 is_ada (void)
5643 {
5644   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5645
5646   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5647 }
5648
5649 /* Remove the specified attribute if present.  */
5650
5651 static void
5652 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5653 {
5654   dw_attr_ref a;
5655   unsigned ix;
5656
5657   if (! die)
5658     return;
5659
5660   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5661     if (a->dw_attr == attr_kind)
5662       {
5663         if (AT_class (a) == dw_val_class_str)
5664           if (a->dw_attr_val.v.val_str->refcount)
5665             a->dw_attr_val.v.val_str->refcount--;
5666
5667         /* VEC_ordered_remove should help reduce the number of abbrevs
5668            that are needed.  */
5669         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5670         return;
5671       }
5672 }
5673
5674 /* Remove CHILD from its parent.  PREV must have the property that
5675    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5676
5677 static void
5678 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5679 {
5680   gcc_assert (child->die_parent == prev->die_parent);
5681   gcc_assert (prev->die_sib == child);
5682   if (prev == child)
5683     {
5684       gcc_assert (child->die_parent->die_child == child);
5685       prev = NULL;
5686     }
5687   else
5688     prev->die_sib = child->die_sib;
5689   if (child->die_parent->die_child == child)
5690     child->die_parent->die_child = prev;
5691 }
5692
5693 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5694    matches TAG.  */
5695
5696 static void
5697 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5698 {
5699   dw_die_ref c;
5700
5701   c = die->die_child;
5702   if (c) do {
5703     dw_die_ref prev = c;
5704     c = c->die_sib;
5705     while (c->die_tag == tag)
5706       {
5707         remove_child_with_prev (c, prev);
5708         /* Might have removed every child.  */
5709         if (c == c->die_sib)
5710           return;
5711         c = c->die_sib;
5712       }
5713   } while (c != die->die_child);
5714 }
5715
5716 /* Add a CHILD_DIE as the last child of DIE.  */
5717
5718 static void
5719 add_child_die (dw_die_ref die, dw_die_ref child_die)
5720 {
5721   /* FIXME this should probably be an assert.  */
5722   if (! die || ! child_die)
5723     return;
5724   gcc_assert (die != child_die);
5725
5726   child_die->die_parent = die;
5727   if (die->die_child)
5728     {
5729       child_die->die_sib = die->die_child->die_sib;
5730       die->die_child->die_sib = child_die;
5731     }
5732   else
5733     child_die->die_sib = child_die;
5734   die->die_child = child_die;
5735 }
5736
5737 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5738    is the specification, to the end of PARENT's list of children.
5739    This is done by removing and re-adding it.  */
5740
5741 static void
5742 splice_child_die (dw_die_ref parent, dw_die_ref child)
5743 {
5744   dw_die_ref p;
5745
5746   /* We want the declaration DIE from inside the class, not the
5747      specification DIE at toplevel.  */
5748   if (child->die_parent != parent)
5749     {
5750       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5751
5752       if (tmp)
5753         child = tmp;
5754     }
5755
5756   gcc_assert (child->die_parent == parent
5757               || (child->die_parent
5758                   == get_AT_ref (parent, DW_AT_specification)));
5759
5760   for (p = child->die_parent->die_child; ; p = p->die_sib)
5761     if (p->die_sib == child)
5762       {
5763         remove_child_with_prev (child, p);
5764         break;
5765       }
5766
5767   add_child_die (parent, child);
5768 }
5769
5770 /* Return a pointer to a newly created DIE node.  */
5771
5772 static inline dw_die_ref
5773 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5774 {
5775   dw_die_ref die = GGC_CNEW (die_node);
5776
5777   die->die_tag = tag_value;
5778
5779   if (parent_die != NULL)
5780     add_child_die (parent_die, die);
5781   else
5782     {
5783       limbo_die_node *limbo_node;
5784
5785       limbo_node = GGC_CNEW (limbo_die_node);
5786       limbo_node->die = die;
5787       limbo_node->created_for = t;
5788       limbo_node->next = limbo_die_list;
5789       limbo_die_list = limbo_node;
5790     }
5791
5792   return die;
5793 }
5794
5795 /* Return the DIE associated with the given type specifier.  */
5796
5797 static inline dw_die_ref
5798 lookup_type_die (tree type)
5799 {
5800   return TYPE_SYMTAB_DIE (type);
5801 }
5802
5803 /* Equate a DIE to a given type specifier.  */
5804
5805 static inline void
5806 equate_type_number_to_die (tree type, dw_die_ref type_die)
5807 {
5808   TYPE_SYMTAB_DIE (type) = type_die;
5809 }
5810
5811 /* Returns a hash value for X (which really is a die_struct).  */
5812
5813 static hashval_t
5814 decl_die_table_hash (const void *x)
5815 {
5816   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5817 }
5818
5819 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5820
5821 static int
5822 decl_die_table_eq (const void *x, const void *y)
5823 {
5824   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5825 }
5826
5827 /* Return the DIE associated with a given declaration.  */
5828
5829 static inline dw_die_ref
5830 lookup_decl_die (tree decl)
5831 {
5832   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5833 }
5834
5835 /* Returns a hash value for X (which really is a var_loc_list).  */
5836
5837 static hashval_t
5838 decl_loc_table_hash (const void *x)
5839 {
5840   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5841 }
5842
5843 /* Return nonzero if decl_id of var_loc_list X is the same as
5844    UID of decl *Y.  */
5845
5846 static int
5847 decl_loc_table_eq (const void *x, const void *y)
5848 {
5849   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5850 }
5851
5852 /* Return the var_loc list associated with a given declaration.  */
5853
5854 static inline var_loc_list *
5855 lookup_decl_loc (const_tree decl)
5856 {
5857   return (var_loc_list *)
5858     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5859 }
5860
5861 /* Equate a DIE to a particular declaration.  */
5862
5863 static void
5864 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5865 {
5866   unsigned int decl_id = DECL_UID (decl);
5867   void **slot;
5868
5869   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5870   *slot = decl_die;
5871   decl_die->decl_id = decl_id;
5872 }
5873
5874 /* Add a variable location node to the linked list for DECL.  */
5875
5876 static void
5877 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5878 {
5879   unsigned int decl_id = DECL_UID (decl);
5880   var_loc_list *temp;
5881   void **slot;
5882
5883   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5884   if (*slot == NULL)
5885     {
5886       temp = GGC_CNEW (var_loc_list);
5887       temp->decl_id = decl_id;
5888       *slot = temp;
5889     }
5890   else
5891     temp = (var_loc_list *) *slot;
5892
5893   if (temp->last)
5894     {
5895       /* If the current location is the same as the end of the list,
5896          and either both or neither of the locations is uninitialized,
5897          we have nothing to do.  */
5898       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5899                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5900           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5901                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5902               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5903                    == VAR_INIT_STATUS_UNINITIALIZED)
5904                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5905                       == VAR_INIT_STATUS_UNINITIALIZED))))
5906         {
5907           /* Add LOC to the end of list and update LAST.  */
5908           temp->last->next = loc;
5909           temp->last = loc;
5910         }
5911     }
5912   /* Do not add empty location to the beginning of the list.  */
5913   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5914     {
5915       temp->first = loc;
5916       temp->last = loc;
5917     }
5918 }
5919 \f
5920 /* Keep track of the number of spaces used to indent the
5921    output of the debugging routines that print the structure of
5922    the DIE internal representation.  */
5923 static int print_indent;
5924
5925 /* Indent the line the number of spaces given by print_indent.  */
5926
5927 static inline void
5928 print_spaces (FILE *outfile)
5929 {
5930   fprintf (outfile, "%*s", print_indent, "");
5931 }
5932
5933 /* Print the information associated with a given DIE, and its children.
5934    This routine is a debugging aid only.  */
5935
5936 static void
5937 print_die (dw_die_ref die, FILE *outfile)
5938 {
5939   dw_attr_ref a;
5940   dw_die_ref c;
5941   unsigned ix;
5942
5943   print_spaces (outfile);
5944   fprintf (outfile, "DIE %4ld: %s\n",
5945            die->die_offset, dwarf_tag_name (die->die_tag));
5946   print_spaces (outfile);
5947   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5948   fprintf (outfile, " offset: %ld\n", die->die_offset);
5949
5950   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5951     {
5952       print_spaces (outfile);
5953       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5954
5955       switch (AT_class (a))
5956         {
5957         case dw_val_class_addr:
5958           fprintf (outfile, "address");
5959           break;
5960         case dw_val_class_offset:
5961           fprintf (outfile, "offset");
5962           break;
5963         case dw_val_class_loc:
5964           fprintf (outfile, "location descriptor");
5965           break;
5966         case dw_val_class_loc_list:
5967           fprintf (outfile, "location list -> label:%s",
5968                    AT_loc_list (a)->ll_symbol);
5969           break;
5970         case dw_val_class_range_list:
5971           fprintf (outfile, "range list");
5972           break;
5973         case dw_val_class_const:
5974           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5975           break;
5976         case dw_val_class_unsigned_const:
5977           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5978           break;
5979         case dw_val_class_long_long:
5980           fprintf (outfile, "constant (%lu,%lu)",
5981                    a->dw_attr_val.v.val_long_long.hi,
5982                    a->dw_attr_val.v.val_long_long.low);
5983           break;
5984         case dw_val_class_vec:
5985           fprintf (outfile, "floating-point or vector constant");
5986           break;
5987         case dw_val_class_flag:
5988           fprintf (outfile, "%u", AT_flag (a));
5989           break;
5990         case dw_val_class_die_ref:
5991           if (AT_ref (a) != NULL)
5992             {
5993               if (AT_ref (a)->die_symbol)
5994                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5995               else
5996                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5997             }
5998           else
5999             fprintf (outfile, "die -> <null>");
6000           break;
6001         case dw_val_class_lbl_id:
6002         case dw_val_class_lineptr:
6003         case dw_val_class_macptr:
6004           fprintf (outfile, "label: %s", AT_lbl (a));
6005           break;
6006         case dw_val_class_str:
6007           if (AT_string (a) != NULL)
6008             fprintf (outfile, "\"%s\"", AT_string (a));
6009           else
6010             fprintf (outfile, "<null>");
6011           break;
6012         case dw_val_class_file:
6013           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6014                    AT_file (a)->emitted_number);
6015           break;
6016         default:
6017           break;
6018         }
6019
6020       fprintf (outfile, "\n");
6021     }
6022
6023   if (die->die_child != NULL)
6024     {
6025       print_indent += 4;
6026       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6027       print_indent -= 4;
6028     }
6029   if (print_indent == 0)
6030     fprintf (outfile, "\n");
6031 }
6032
6033 /* Print the contents of the source code line number correspondence table.
6034    This routine is a debugging aid only.  */
6035
6036 static void
6037 print_dwarf_line_table (FILE *outfile)
6038 {
6039   unsigned i;
6040   dw_line_info_ref line_info;
6041
6042   fprintf (outfile, "\n\nDWARF source line information\n");
6043   for (i = 1; i < line_info_table_in_use; i++)
6044     {
6045       line_info = &line_info_table[i];
6046       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6047                line_info->dw_file_num,
6048                line_info->dw_line_num);
6049     }
6050
6051   fprintf (outfile, "\n\n");
6052 }
6053
6054 /* Print the information collected for a given DIE.  */
6055
6056 void
6057 debug_dwarf_die (dw_die_ref die)
6058 {
6059   print_die (die, stderr);
6060 }
6061
6062 /* Print all DWARF information collected for the compilation unit.
6063    This routine is a debugging aid only.  */
6064
6065 void
6066 debug_dwarf (void)
6067 {
6068   print_indent = 0;
6069   print_die (comp_unit_die, stderr);
6070   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6071     print_dwarf_line_table (stderr);
6072 }
6073 \f
6074 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6075    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6076    DIE that marks the start of the DIEs for this include file.  */
6077
6078 static dw_die_ref
6079 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6080 {
6081   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6082   dw_die_ref new_unit = gen_compile_unit_die (filename);
6083
6084   new_unit->die_sib = old_unit;
6085   return new_unit;
6086 }
6087
6088 /* Close an include-file CU and reopen the enclosing one.  */
6089
6090 static dw_die_ref
6091 pop_compile_unit (dw_die_ref old_unit)
6092 {
6093   dw_die_ref new_unit = old_unit->die_sib;
6094
6095   old_unit->die_sib = NULL;
6096   return new_unit;
6097 }
6098
6099 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6100 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6101
6102 /* Calculate the checksum of a location expression.  */
6103
6104 static inline void
6105 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6106 {
6107   CHECKSUM (loc->dw_loc_opc);
6108   CHECKSUM (loc->dw_loc_oprnd1);
6109   CHECKSUM (loc->dw_loc_oprnd2);
6110 }
6111
6112 /* Calculate the checksum of an attribute.  */
6113
6114 static void
6115 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6116 {
6117   dw_loc_descr_ref loc;
6118   rtx r;
6119
6120   CHECKSUM (at->dw_attr);
6121
6122   /* We don't care that this was compiled with a different compiler
6123      snapshot; if the output is the same, that's what matters.  */
6124   if (at->dw_attr == DW_AT_producer)
6125     return;
6126
6127   switch (AT_class (at))
6128     {
6129     case dw_val_class_const:
6130       CHECKSUM (at->dw_attr_val.v.val_int);
6131       break;
6132     case dw_val_class_unsigned_const:
6133       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6134       break;
6135     case dw_val_class_long_long:
6136       CHECKSUM (at->dw_attr_val.v.val_long_long);
6137       break;
6138     case dw_val_class_vec:
6139       CHECKSUM (at->dw_attr_val.v.val_vec);
6140       break;
6141     case dw_val_class_flag:
6142       CHECKSUM (at->dw_attr_val.v.val_flag);
6143       break;
6144     case dw_val_class_str:
6145       CHECKSUM_STRING (AT_string (at));
6146       break;
6147
6148     case dw_val_class_addr:
6149       r = AT_addr (at);
6150       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6151       CHECKSUM_STRING (XSTR (r, 0));
6152       break;
6153
6154     case dw_val_class_offset:
6155       CHECKSUM (at->dw_attr_val.v.val_offset);
6156       break;
6157
6158     case dw_val_class_loc:
6159       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6160         loc_checksum (loc, ctx);
6161       break;
6162
6163     case dw_val_class_die_ref:
6164       die_checksum (AT_ref (at), ctx, mark);
6165       break;
6166
6167     case dw_val_class_fde_ref:
6168     case dw_val_class_lbl_id:
6169     case dw_val_class_lineptr:
6170     case dw_val_class_macptr:
6171       break;
6172
6173     case dw_val_class_file:
6174       CHECKSUM_STRING (AT_file (at)->filename);
6175       break;
6176
6177     default:
6178       break;
6179     }
6180 }
6181
6182 /* Calculate the checksum of a DIE.  */
6183
6184 static void
6185 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6186 {
6187   dw_die_ref c;
6188   dw_attr_ref a;
6189   unsigned ix;
6190
6191   /* To avoid infinite recursion.  */
6192   if (die->die_mark)
6193     {
6194       CHECKSUM (die->die_mark);
6195       return;
6196     }
6197   die->die_mark = ++(*mark);
6198
6199   CHECKSUM (die->die_tag);
6200
6201   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6202     attr_checksum (a, ctx, mark);
6203
6204   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6205 }
6206
6207 #undef CHECKSUM
6208 #undef CHECKSUM_STRING
6209
6210 /* Do the location expressions look same?  */
6211 static inline int
6212 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6213 {
6214   return loc1->dw_loc_opc == loc2->dw_loc_opc
6215          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6216          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6217 }
6218
6219 /* Do the values look the same?  */
6220 static int
6221 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6222 {
6223   dw_loc_descr_ref loc1, loc2;
6224   rtx r1, r2;
6225
6226   if (v1->val_class != v2->val_class)
6227     return 0;
6228
6229   switch (v1->val_class)
6230     {
6231     case dw_val_class_const:
6232       return v1->v.val_int == v2->v.val_int;
6233     case dw_val_class_unsigned_const:
6234       return v1->v.val_unsigned == v2->v.val_unsigned;
6235     case dw_val_class_long_long:
6236       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6237              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6238     case dw_val_class_vec:
6239       if (v1->v.val_vec.length != v2->v.val_vec.length
6240           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6241         return 0;
6242       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6243                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6244         return 0;
6245       return 1;
6246     case dw_val_class_flag:
6247       return v1->v.val_flag == v2->v.val_flag;
6248     case dw_val_class_str:
6249       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6250
6251     case dw_val_class_addr:
6252       r1 = v1->v.val_addr;
6253       r2 = v2->v.val_addr;
6254       if (GET_CODE (r1) != GET_CODE (r2))
6255         return 0;
6256       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6257       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6258
6259     case dw_val_class_offset:
6260       return v1->v.val_offset == v2->v.val_offset;
6261
6262     case dw_val_class_loc:
6263       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6264            loc1 && loc2;
6265            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6266         if (!same_loc_p (loc1, loc2, mark))
6267           return 0;
6268       return !loc1 && !loc2;
6269
6270     case dw_val_class_die_ref:
6271       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6272
6273     case dw_val_class_fde_ref:
6274     case dw_val_class_lbl_id:
6275     case dw_val_class_lineptr:
6276     case dw_val_class_macptr:
6277       return 1;
6278
6279     case dw_val_class_file:
6280       return v1->v.val_file == v2->v.val_file;
6281
6282     default:
6283       return 1;
6284     }
6285 }
6286
6287 /* Do the attributes look the same?  */
6288
6289 static int
6290 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6291 {
6292   if (at1->dw_attr != at2->dw_attr)
6293     return 0;
6294
6295   /* We don't care that this was compiled with a different compiler
6296      snapshot; if the output is the same, that's what matters. */
6297   if (at1->dw_attr == DW_AT_producer)
6298     return 1;
6299
6300   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6301 }
6302
6303 /* Do the dies look the same?  */
6304
6305 static int
6306 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6307 {
6308   dw_die_ref c1, c2;
6309   dw_attr_ref a1;
6310   unsigned ix;
6311
6312   /* To avoid infinite recursion.  */
6313   if (die1->die_mark)
6314     return die1->die_mark == die2->die_mark;
6315   die1->die_mark = die2->die_mark = ++(*mark);
6316
6317   if (die1->die_tag != die2->die_tag)
6318     return 0;
6319
6320   if (VEC_length (dw_attr_node, die1->die_attr)
6321       != VEC_length (dw_attr_node, die2->die_attr))
6322     return 0;
6323
6324   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6325     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6326       return 0;
6327
6328   c1 = die1->die_child;
6329   c2 = die2->die_child;
6330   if (! c1)
6331     {
6332       if (c2)
6333         return 0;
6334     }
6335   else
6336     for (;;)
6337       {
6338         if (!same_die_p (c1, c2, mark))
6339           return 0;
6340         c1 = c1->die_sib;
6341         c2 = c2->die_sib;
6342         if (c1 == die1->die_child)
6343           {
6344             if (c2 == die2->die_child)
6345               break;
6346             else
6347               return 0;
6348           }
6349     }
6350
6351   return 1;
6352 }
6353
6354 /* Do the dies look the same?  Wrapper around same_die_p.  */
6355
6356 static int
6357 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6358 {
6359   int mark = 0;
6360   int ret = same_die_p (die1, die2, &mark);
6361
6362   unmark_all_dies (die1);
6363   unmark_all_dies (die2);
6364
6365   return ret;
6366 }
6367
6368 /* The prefix to attach to symbols on DIEs in the current comdat debug
6369    info section.  */
6370 static char *comdat_symbol_id;
6371
6372 /* The index of the current symbol within the current comdat CU.  */
6373 static unsigned int comdat_symbol_number;
6374
6375 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6376    children, and set comdat_symbol_id accordingly.  */
6377
6378 static void
6379 compute_section_prefix (dw_die_ref unit_die)
6380 {
6381   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6382   const char *base = die_name ? lbasename (die_name) : "anonymous";
6383   char *name = XALLOCAVEC (char, strlen (base) + 64);
6384   char *p;
6385   int i, mark;
6386   unsigned char checksum[16];
6387   struct md5_ctx ctx;
6388
6389   /* Compute the checksum of the DIE, then append part of it as hex digits to
6390      the name filename of the unit.  */
6391
6392   md5_init_ctx (&ctx);
6393   mark = 0;
6394   die_checksum (unit_die, &ctx, &mark);
6395   unmark_all_dies (unit_die);
6396   md5_finish_ctx (&ctx, checksum);
6397
6398   sprintf (name, "%s.", base);
6399   clean_symbol_name (name);
6400
6401   p = name + strlen (name);
6402   for (i = 0; i < 4; i++)
6403     {
6404       sprintf (p, "%.2x", checksum[i]);
6405       p += 2;
6406     }
6407
6408   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6409   comdat_symbol_number = 0;
6410 }
6411
6412 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6413
6414 static int
6415 is_type_die (dw_die_ref die)
6416 {
6417   switch (die->die_tag)
6418     {
6419     case DW_TAG_array_type:
6420     case DW_TAG_class_type:
6421     case DW_TAG_interface_type:
6422     case DW_TAG_enumeration_type:
6423     case DW_TAG_pointer_type:
6424     case DW_TAG_reference_type:
6425     case DW_TAG_string_type:
6426     case DW_TAG_structure_type:
6427     case DW_TAG_subroutine_type:
6428     case DW_TAG_union_type:
6429     case DW_TAG_ptr_to_member_type:
6430     case DW_TAG_set_type:
6431     case DW_TAG_subrange_type:
6432     case DW_TAG_base_type:
6433     case DW_TAG_const_type:
6434     case DW_TAG_file_type:
6435     case DW_TAG_packed_type:
6436     case DW_TAG_volatile_type:
6437     case DW_TAG_typedef:
6438       return 1;
6439     default:
6440       return 0;
6441     }
6442 }
6443
6444 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6445    Basically, we want to choose the bits that are likely to be shared between
6446    compilations (types) and leave out the bits that are specific to individual
6447    compilations (functions).  */
6448
6449 static int
6450 is_comdat_die (dw_die_ref c)
6451 {
6452   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6453      we do for stabs.  The advantage is a greater likelihood of sharing between
6454      objects that don't include headers in the same order (and therefore would
6455      put the base types in a different comdat).  jason 8/28/00 */
6456
6457   if (c->die_tag == DW_TAG_base_type)
6458     return 0;
6459
6460   if (c->die_tag == DW_TAG_pointer_type
6461       || c->die_tag == DW_TAG_reference_type
6462       || c->die_tag == DW_TAG_const_type
6463       || c->die_tag == DW_TAG_volatile_type)
6464     {
6465       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6466
6467       return t ? is_comdat_die (t) : 0;
6468     }
6469
6470   return is_type_die (c);
6471 }
6472
6473 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6474    compilation unit.  */
6475
6476 static int
6477 is_symbol_die (dw_die_ref c)
6478 {
6479   return (is_type_die (c)
6480           || (get_AT (c, DW_AT_declaration)
6481               && !get_AT (c, DW_AT_specification))
6482           || c->die_tag == DW_TAG_namespace);
6483 }
6484
6485 static char *
6486 gen_internal_sym (const char *prefix)
6487 {
6488   char buf[256];
6489
6490   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6491   return xstrdup (buf);
6492 }
6493
6494 /* Assign symbols to all worthy DIEs under DIE.  */
6495
6496 static void
6497 assign_symbol_names (dw_die_ref die)
6498 {
6499   dw_die_ref c;
6500
6501   if (is_symbol_die (die))
6502     {
6503       if (comdat_symbol_id)
6504         {
6505           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6506
6507           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6508                    comdat_symbol_id, comdat_symbol_number++);
6509           die->die_symbol = xstrdup (p);
6510         }
6511       else
6512         die->die_symbol = gen_internal_sym ("LDIE");
6513     }
6514
6515   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6516 }
6517
6518 struct cu_hash_table_entry
6519 {
6520   dw_die_ref cu;
6521   unsigned min_comdat_num, max_comdat_num;
6522   struct cu_hash_table_entry *next;
6523 };
6524
6525 /* Routines to manipulate hash table of CUs.  */
6526 static hashval_t
6527 htab_cu_hash (const void *of)
6528 {
6529   const struct cu_hash_table_entry *const entry =
6530     (const struct cu_hash_table_entry *) of;
6531
6532   return htab_hash_string (entry->cu->die_symbol);
6533 }
6534
6535 static int
6536 htab_cu_eq (const void *of1, const void *of2)
6537 {
6538   const struct cu_hash_table_entry *const entry1 =
6539     (const struct cu_hash_table_entry *) of1;
6540   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6541
6542   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6543 }
6544
6545 static void
6546 htab_cu_del (void *what)
6547 {
6548   struct cu_hash_table_entry *next,
6549     *entry = (struct cu_hash_table_entry *) what;
6550
6551   while (entry)
6552     {
6553       next = entry->next;
6554       free (entry);
6555       entry = next;
6556     }
6557 }
6558
6559 /* Check whether we have already seen this CU and set up SYM_NUM
6560    accordingly.  */
6561 static int
6562 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6563 {
6564   struct cu_hash_table_entry dummy;
6565   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6566
6567   dummy.max_comdat_num = 0;
6568
6569   slot = (struct cu_hash_table_entry **)
6570     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6571         INSERT);
6572   entry = *slot;
6573
6574   for (; entry; last = entry, entry = entry->next)
6575     {
6576       if (same_die_p_wrap (cu, entry->cu))
6577         break;
6578     }
6579
6580   if (entry)
6581     {
6582       *sym_num = entry->min_comdat_num;
6583       return 1;
6584     }
6585
6586   entry = XCNEW (struct cu_hash_table_entry);
6587   entry->cu = cu;
6588   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6589   entry->next = *slot;
6590   *slot = entry;
6591
6592   return 0;
6593 }
6594
6595 /* Record SYM_NUM to record of CU in HTABLE.  */
6596 static void
6597 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6598 {
6599   struct cu_hash_table_entry **slot, *entry;
6600
6601   slot = (struct cu_hash_table_entry **)
6602     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6603         NO_INSERT);
6604   entry = *slot;
6605
6606   entry->max_comdat_num = sym_num;
6607 }
6608
6609 /* Traverse the DIE (which is always comp_unit_die), and set up
6610    additional compilation units for each of the include files we see
6611    bracketed by BINCL/EINCL.  */
6612
6613 static void
6614 break_out_includes (dw_die_ref die)
6615 {
6616   dw_die_ref c;
6617   dw_die_ref unit = NULL;
6618   limbo_die_node *node, **pnode;
6619   htab_t cu_hash_table;
6620
6621   c = die->die_child;
6622   if (c) do {
6623     dw_die_ref prev = c;
6624     c = c->die_sib;
6625     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6626            || (unit && is_comdat_die (c)))
6627       {
6628         dw_die_ref next = c->die_sib;
6629
6630         /* This DIE is for a secondary CU; remove it from the main one.  */
6631         remove_child_with_prev (c, prev);
6632
6633         if (c->die_tag == DW_TAG_GNU_BINCL)
6634           unit = push_new_compile_unit (unit, c);
6635         else if (c->die_tag == DW_TAG_GNU_EINCL)
6636           unit = pop_compile_unit (unit);
6637         else
6638           add_child_die (unit, c);
6639         c = next;
6640         if (c == die->die_child)
6641           break;
6642       }
6643   } while (c != die->die_child);
6644
6645 #if 0
6646   /* We can only use this in debugging, since the frontend doesn't check
6647      to make sure that we leave every include file we enter.  */
6648   gcc_assert (!unit);
6649 #endif
6650
6651   assign_symbol_names (die);
6652   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6653   for (node = limbo_die_list, pnode = &limbo_die_list;
6654        node;
6655        node = node->next)
6656     {
6657       int is_dupl;
6658
6659       compute_section_prefix (node->die);
6660       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6661                         &comdat_symbol_number);
6662       assign_symbol_names (node->die);
6663       if (is_dupl)
6664         *pnode = node->next;
6665       else
6666         {
6667           pnode = &node->next;
6668           record_comdat_symbol_number (node->die, cu_hash_table,
6669                 comdat_symbol_number);
6670         }
6671     }
6672   htab_delete (cu_hash_table);
6673 }
6674
6675 /* Traverse the DIE and add a sibling attribute if it may have the
6676    effect of speeding up access to siblings.  To save some space,
6677    avoid generating sibling attributes for DIE's without children.  */
6678
6679 static void
6680 add_sibling_attributes (dw_die_ref die)
6681 {
6682   dw_die_ref c;
6683
6684   if (! die->die_child)
6685     return;
6686
6687   if (die->die_parent && die != die->die_parent->die_child)
6688     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6689
6690   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6691 }
6692
6693 /* Output all location lists for the DIE and its children.  */
6694
6695 static void
6696 output_location_lists (dw_die_ref die)
6697 {
6698   dw_die_ref c;
6699   dw_attr_ref a;
6700   unsigned ix;
6701
6702   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6703     if (AT_class (a) == dw_val_class_loc_list)
6704       output_loc_list (AT_loc_list (a));
6705
6706   FOR_EACH_CHILD (die, c, output_location_lists (c));
6707 }
6708
6709 /* The format of each DIE (and its attribute value pairs) is encoded in an
6710    abbreviation table.  This routine builds the abbreviation table and assigns
6711    a unique abbreviation id for each abbreviation entry.  The children of each
6712    die are visited recursively.  */
6713
6714 static void
6715 build_abbrev_table (dw_die_ref die)
6716 {
6717   unsigned long abbrev_id;
6718   unsigned int n_alloc;
6719   dw_die_ref c;
6720   dw_attr_ref a;
6721   unsigned ix;
6722
6723   /* Scan the DIE references, and mark as external any that refer to
6724      DIEs from other CUs (i.e. those which are not marked).  */
6725   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6726     if (AT_class (a) == dw_val_class_die_ref
6727         && AT_ref (a)->die_mark == 0)
6728       {
6729         gcc_assert (AT_ref (a)->die_symbol);
6730
6731         set_AT_ref_external (a, 1);
6732       }
6733
6734   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6735     {
6736       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6737       dw_attr_ref die_a, abbrev_a;
6738       unsigned ix;
6739       bool ok = true;
6740
6741       if (abbrev->die_tag != die->die_tag)
6742         continue;
6743       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6744         continue;
6745
6746       if (VEC_length (dw_attr_node, abbrev->die_attr)
6747           != VEC_length (dw_attr_node, die->die_attr))
6748         continue;
6749
6750       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6751         {
6752           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6753           if ((abbrev_a->dw_attr != die_a->dw_attr)
6754               || (value_format (abbrev_a) != value_format (die_a)))
6755             {
6756               ok = false;
6757               break;
6758             }
6759         }
6760       if (ok)
6761         break;
6762     }
6763
6764   if (abbrev_id >= abbrev_die_table_in_use)
6765     {
6766       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6767         {
6768           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6769           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
6770                                             n_alloc);
6771
6772           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6773                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6774           abbrev_die_table_allocated = n_alloc;
6775         }
6776
6777       ++abbrev_die_table_in_use;
6778       abbrev_die_table[abbrev_id] = die;
6779     }
6780
6781   die->die_abbrev = abbrev_id;
6782   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6783 }
6784 \f
6785 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6786
6787 static int
6788 constant_size (long unsigned int value)
6789 {
6790   int log;
6791
6792   if (value == 0)
6793     log = 0;
6794   else
6795     log = floor_log2 (value);
6796
6797   log = log / 8;
6798   log = 1 << (floor_log2 (log) + 1);
6799
6800   return log;
6801 }
6802
6803 /* Return the size of a DIE as it is represented in the
6804    .debug_info section.  */
6805
6806 static unsigned long
6807 size_of_die (dw_die_ref die)
6808 {
6809   unsigned long size = 0;
6810   dw_attr_ref a;
6811   unsigned ix;
6812
6813   size += size_of_uleb128 (die->die_abbrev);
6814   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6815     {
6816       switch (AT_class (a))
6817         {
6818         case dw_val_class_addr:
6819           size += DWARF2_ADDR_SIZE;
6820           break;
6821         case dw_val_class_offset:
6822           size += DWARF_OFFSET_SIZE;
6823           break;
6824         case dw_val_class_loc:
6825           {
6826             unsigned long lsize = size_of_locs (AT_loc (a));
6827
6828             /* Block length.  */
6829             size += constant_size (lsize);
6830             size += lsize;
6831           }
6832           break;
6833         case dw_val_class_loc_list:
6834           size += DWARF_OFFSET_SIZE;
6835           break;
6836         case dw_val_class_range_list:
6837           size += DWARF_OFFSET_SIZE;
6838           break;
6839         case dw_val_class_const:
6840           size += size_of_sleb128 (AT_int (a));
6841           break;
6842         case dw_val_class_unsigned_const:
6843           size += constant_size (AT_unsigned (a));
6844           break;
6845         case dw_val_class_long_long:
6846           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6847           break;
6848         case dw_val_class_vec:
6849           size += 1 + (a->dw_attr_val.v.val_vec.length
6850                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6851           break;
6852         case dw_val_class_flag:
6853           size += 1;
6854           break;
6855         case dw_val_class_die_ref:
6856           if (AT_ref_external (a))
6857             size += DWARF2_ADDR_SIZE;
6858           else
6859             size += DWARF_OFFSET_SIZE;
6860           break;
6861         case dw_val_class_fde_ref:
6862           size += DWARF_OFFSET_SIZE;
6863           break;
6864         case dw_val_class_lbl_id:
6865           size += DWARF2_ADDR_SIZE;
6866           break;
6867         case dw_val_class_lineptr:
6868         case dw_val_class_macptr:
6869           size += DWARF_OFFSET_SIZE;
6870           break;
6871         case dw_val_class_str:
6872           if (AT_string_form (a) == DW_FORM_strp)
6873             size += DWARF_OFFSET_SIZE;
6874           else
6875             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6876           break;
6877         case dw_val_class_file:
6878           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6879           break;
6880         default:
6881           gcc_unreachable ();
6882         }
6883     }
6884
6885   return size;
6886 }
6887
6888 /* Size the debugging information associated with a given DIE.  Visits the
6889    DIE's children recursively.  Updates the global variable next_die_offset, on
6890    each time through.  Uses the current value of next_die_offset to update the
6891    die_offset field in each DIE.  */
6892
6893 static void
6894 calc_die_sizes (dw_die_ref die)
6895 {
6896   dw_die_ref c;
6897
6898   die->die_offset = next_die_offset;
6899   next_die_offset += size_of_die (die);
6900
6901   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6902
6903   if (die->die_child != NULL)
6904     /* Count the null byte used to terminate sibling lists.  */
6905     next_die_offset += 1;
6906 }
6907
6908 /* Set the marks for a die and its children.  We do this so
6909    that we know whether or not a reference needs to use FORM_ref_addr; only
6910    DIEs in the same CU will be marked.  We used to clear out the offset
6911    and use that as the flag, but ran into ordering problems.  */
6912
6913 static void
6914 mark_dies (dw_die_ref die)
6915 {
6916   dw_die_ref c;
6917
6918   gcc_assert (!die->die_mark);
6919
6920   die->die_mark = 1;
6921   FOR_EACH_CHILD (die, c, mark_dies (c));
6922 }
6923
6924 /* Clear the marks for a die and its children.  */
6925
6926 static void
6927 unmark_dies (dw_die_ref die)
6928 {
6929   dw_die_ref c;
6930
6931   gcc_assert (die->die_mark);
6932
6933   die->die_mark = 0;
6934   FOR_EACH_CHILD (die, c, unmark_dies (c));
6935 }
6936
6937 /* Clear the marks for a die, its children and referred dies.  */
6938
6939 static void
6940 unmark_all_dies (dw_die_ref die)
6941 {
6942   dw_die_ref c;
6943   dw_attr_ref a;
6944   unsigned ix;
6945
6946   if (!die->die_mark)
6947     return;
6948   die->die_mark = 0;
6949
6950   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6951
6952   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6953     if (AT_class (a) == dw_val_class_die_ref)
6954       unmark_all_dies (AT_ref (a));
6955 }
6956
6957 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6958    generated for the compilation unit.  */
6959
6960 static unsigned long
6961 size_of_pubnames (VEC (pubname_entry, gc) * names)
6962 {
6963   unsigned long size;
6964   unsigned i;
6965   pubname_ref p;
6966
6967   size = DWARF_PUBNAMES_HEADER_SIZE;
6968   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6969     if (names != pubtype_table
6970         || p->die->die_offset != 0
6971         || !flag_eliminate_unused_debug_types)
6972       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6973
6974   size += DWARF_OFFSET_SIZE;
6975   return size;
6976 }
6977
6978 /* Return the size of the information in the .debug_aranges section.  */
6979
6980 static unsigned long
6981 size_of_aranges (void)
6982 {
6983   unsigned long size;
6984
6985   size = DWARF_ARANGES_HEADER_SIZE;
6986
6987   /* Count the address/length pair for this compilation unit.  */
6988   if (text_section_used)
6989     size += 2 * DWARF2_ADDR_SIZE;
6990   if (cold_text_section_used)
6991     size += 2 * DWARF2_ADDR_SIZE;
6992   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6993
6994   /* Count the two zero words used to terminated the address range table.  */
6995   size += 2 * DWARF2_ADDR_SIZE;
6996   return size;
6997 }
6998 \f
6999 /* Select the encoding of an attribute value.  */
7000
7001 static enum dwarf_form
7002 value_format (dw_attr_ref a)
7003 {
7004   switch (a->dw_attr_val.val_class)
7005     {
7006     case dw_val_class_addr:
7007       return DW_FORM_addr;
7008     case dw_val_class_range_list:
7009     case dw_val_class_offset:
7010     case dw_val_class_loc_list:
7011       switch (DWARF_OFFSET_SIZE)
7012         {
7013         case 4:
7014           return DW_FORM_data4;
7015         case 8:
7016           return DW_FORM_data8;
7017         default:
7018           gcc_unreachable ();
7019         }
7020     case dw_val_class_loc:
7021       switch (constant_size (size_of_locs (AT_loc (a))))
7022         {
7023         case 1:
7024           return DW_FORM_block1;
7025         case 2:
7026           return DW_FORM_block2;
7027         default:
7028           gcc_unreachable ();
7029         }
7030     case dw_val_class_const:
7031       return DW_FORM_sdata;
7032     case dw_val_class_unsigned_const:
7033       switch (constant_size (AT_unsigned (a)))
7034         {
7035         case 1:
7036           return DW_FORM_data1;
7037         case 2:
7038           return DW_FORM_data2;
7039         case 4:
7040           return DW_FORM_data4;
7041         case 8:
7042           return DW_FORM_data8;
7043         default:
7044           gcc_unreachable ();
7045         }
7046     case dw_val_class_long_long:
7047       return DW_FORM_block1;
7048     case dw_val_class_vec:
7049       return DW_FORM_block1;
7050     case dw_val_class_flag:
7051       return DW_FORM_flag;
7052     case dw_val_class_die_ref:
7053       if (AT_ref_external (a))
7054         return DW_FORM_ref_addr;
7055       else
7056         return DW_FORM_ref;
7057     case dw_val_class_fde_ref:
7058       return DW_FORM_data;
7059     case dw_val_class_lbl_id:
7060       return DW_FORM_addr;
7061     case dw_val_class_lineptr:
7062     case dw_val_class_macptr:
7063       return DW_FORM_data;
7064     case dw_val_class_str:
7065       return AT_string_form (a);
7066     case dw_val_class_file:
7067       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7068         {
7069         case 1:
7070           return DW_FORM_data1;
7071         case 2:
7072           return DW_FORM_data2;
7073         case 4:
7074           return DW_FORM_data4;
7075         default:
7076           gcc_unreachable ();
7077         }
7078
7079     default:
7080       gcc_unreachable ();
7081     }
7082 }
7083
7084 /* Output the encoding of an attribute value.  */
7085
7086 static void
7087 output_value_format (dw_attr_ref a)
7088 {
7089   enum dwarf_form form = value_format (a);
7090
7091   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7092 }
7093
7094 /* Output the .debug_abbrev section which defines the DIE abbreviation
7095    table.  */
7096
7097 static void
7098 output_abbrev_section (void)
7099 {
7100   unsigned long abbrev_id;
7101
7102   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7103     {
7104       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7105       unsigned ix;
7106       dw_attr_ref a_attr;
7107
7108       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7109       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7110                                    dwarf_tag_name (abbrev->die_tag));
7111
7112       if (abbrev->die_child != NULL)
7113         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7114       else
7115         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7116
7117       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7118            ix++)
7119         {
7120           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7121                                        dwarf_attr_name (a_attr->dw_attr));
7122           output_value_format (a_attr);
7123         }
7124
7125       dw2_asm_output_data (1, 0, NULL);
7126       dw2_asm_output_data (1, 0, NULL);
7127     }
7128
7129   /* Terminate the table.  */
7130   dw2_asm_output_data (1, 0, NULL);
7131 }
7132
7133 /* Output a symbol we can use to refer to this DIE from another CU.  */
7134
7135 static inline void
7136 output_die_symbol (dw_die_ref die)
7137 {
7138   char *sym = die->die_symbol;
7139
7140   if (sym == 0)
7141     return;
7142
7143   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7144     /* We make these global, not weak; if the target doesn't support
7145        .linkonce, it doesn't support combining the sections, so debugging
7146        will break.  */
7147     targetm.asm_out.globalize_label (asm_out_file, sym);
7148
7149   ASM_OUTPUT_LABEL (asm_out_file, sym);
7150 }
7151
7152 /* Return a new location list, given the begin and end range, and the
7153    expression. gensym tells us whether to generate a new internal symbol for
7154    this location list node, which is done for the head of the list only.  */
7155
7156 static inline dw_loc_list_ref
7157 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7158               const char *section, unsigned int gensym)
7159 {
7160   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7161
7162   retlist->begin = begin;
7163   retlist->end = end;
7164   retlist->expr = expr;
7165   retlist->section = section;
7166   if (gensym)
7167     retlist->ll_symbol = gen_internal_sym ("LLST");
7168
7169   return retlist;
7170 }
7171
7172 /* Add a location description expression to a location list.  */
7173
7174 static inline void
7175 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7176                            const char *begin, const char *end,
7177                            const char *section)
7178 {
7179   dw_loc_list_ref *d;
7180
7181   /* Find the end of the chain.  */
7182   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7183     ;
7184
7185   /* Add a new location list node to the list.  */
7186   *d = new_loc_list (descr, begin, end, section, 0);
7187 }
7188
7189 /* Output the location list given to us.  */
7190
7191 static void
7192 output_loc_list (dw_loc_list_ref list_head)
7193 {
7194   dw_loc_list_ref curr = list_head;
7195
7196   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7197
7198   /* Walk the location list, and output each range + expression.  */
7199   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7200     {
7201       unsigned long size;
7202       /* Don't output an entry that starts and ends at the same address.  */
7203       if (strcmp (curr->begin, curr->end) == 0)
7204         continue;
7205       if (!have_multiple_function_sections)
7206         {
7207           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7208                                 "Location list begin address (%s)",
7209                                 list_head->ll_symbol);
7210           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7211                                 "Location list end address (%s)",
7212                                 list_head->ll_symbol);
7213         }
7214       else
7215         {
7216           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7217                                "Location list begin address (%s)",
7218                                list_head->ll_symbol);
7219           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7220                                "Location list end address (%s)",
7221                                list_head->ll_symbol);
7222         }
7223       size = size_of_locs (curr->expr);
7224
7225       /* Output the block length for this list of location operations.  */
7226       gcc_assert (size <= 0xffff);
7227       dw2_asm_output_data (2, size, "%s", "Location expression size");
7228
7229       output_loc_sequence (curr->expr);
7230     }
7231
7232   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7233                        "Location list terminator begin (%s)",
7234                        list_head->ll_symbol);
7235   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7236                        "Location list terminator end (%s)",
7237                        list_head->ll_symbol);
7238 }
7239
7240 /* Output the DIE and its attributes.  Called recursively to generate
7241    the definitions of each child DIE.  */
7242
7243 static void
7244 output_die (dw_die_ref die)
7245 {
7246   dw_attr_ref a;
7247   dw_die_ref c;
7248   unsigned long size;
7249   unsigned ix;
7250
7251   /* If someone in another CU might refer to us, set up a symbol for
7252      them to point to.  */
7253   if (die->die_symbol)
7254     output_die_symbol (die);
7255
7256   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7257                                (unsigned long)die->die_offset,
7258                                dwarf_tag_name (die->die_tag));
7259
7260   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7261     {
7262       const char *name = dwarf_attr_name (a->dw_attr);
7263
7264       switch (AT_class (a))
7265         {
7266         case dw_val_class_addr:
7267           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7268           break;
7269
7270         case dw_val_class_offset:
7271           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7272                                "%s", name);
7273           break;
7274
7275         case dw_val_class_range_list:
7276           {
7277             char *p = strchr (ranges_section_label, '\0');
7278
7279             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7280                      a->dw_attr_val.v.val_offset);
7281             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7282                                    debug_ranges_section, "%s", name);
7283             *p = '\0';
7284           }
7285           break;
7286
7287         case dw_val_class_loc:
7288           size = size_of_locs (AT_loc (a));
7289
7290           /* Output the block length for this list of location operations.  */
7291           dw2_asm_output_data (constant_size (size), size, "%s", name);
7292
7293           output_loc_sequence (AT_loc (a));
7294           break;
7295
7296         case dw_val_class_const:
7297           /* ??? It would be slightly more efficient to use a scheme like is
7298              used for unsigned constants below, but gdb 4.x does not sign
7299              extend.  Gdb 5.x does sign extend.  */
7300           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7301           break;
7302
7303         case dw_val_class_unsigned_const:
7304           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7305                                AT_unsigned (a), "%s", name);
7306           break;
7307
7308         case dw_val_class_long_long:
7309           {
7310             unsigned HOST_WIDE_INT first, second;
7311
7312             dw2_asm_output_data (1,
7313                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7314                                  "%s", name);
7315
7316             if (WORDS_BIG_ENDIAN)
7317               {
7318                 first = a->dw_attr_val.v.val_long_long.hi;
7319                 second = a->dw_attr_val.v.val_long_long.low;
7320               }
7321             else
7322               {
7323                 first = a->dw_attr_val.v.val_long_long.low;
7324                 second = a->dw_attr_val.v.val_long_long.hi;
7325               }
7326
7327             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7328                                  first, "long long constant");
7329             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7330                                  second, NULL);
7331           }
7332           break;
7333
7334         case dw_val_class_vec:
7335           {
7336             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7337             unsigned int len = a->dw_attr_val.v.val_vec.length;
7338             unsigned int i;
7339             unsigned char *p;
7340
7341             dw2_asm_output_data (1, len * elt_size, "%s", name);
7342             if (elt_size > sizeof (HOST_WIDE_INT))
7343               {
7344                 elt_size /= 2;
7345                 len *= 2;
7346               }
7347             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7348                  i < len;
7349                  i++, p += elt_size)
7350               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7351                                    "fp or vector constant word %u", i);
7352             break;
7353           }
7354
7355         case dw_val_class_flag:
7356           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7357           break;
7358
7359         case dw_val_class_loc_list:
7360           {
7361             char *sym = AT_loc_list (a)->ll_symbol;
7362
7363             gcc_assert (sym);
7364             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7365                                    "%s", name);
7366           }
7367           break;
7368
7369         case dw_val_class_die_ref:
7370           if (AT_ref_external (a))
7371             {
7372               char *sym = AT_ref (a)->die_symbol;
7373
7374               gcc_assert (sym);
7375               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7376                                      "%s", name);
7377             }
7378           else
7379             {
7380               gcc_assert (AT_ref (a)->die_offset);
7381               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7382                                    "%s", name);
7383             }
7384           break;
7385
7386         case dw_val_class_fde_ref:
7387           {
7388             char l1[20];
7389
7390             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7391                                          a->dw_attr_val.v.val_fde_index * 2);
7392             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7393                                    "%s", name);
7394           }
7395           break;
7396
7397         case dw_val_class_lbl_id:
7398           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7399           break;
7400
7401         case dw_val_class_lineptr:
7402           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7403                                  debug_line_section, "%s", name);
7404           break;
7405
7406         case dw_val_class_macptr:
7407           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7408                                  debug_macinfo_section, "%s", name);
7409           break;
7410
7411         case dw_val_class_str:
7412           if (AT_string_form (a) == DW_FORM_strp)
7413             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7414                                    a->dw_attr_val.v.val_str->label,
7415                                    debug_str_section,
7416                                    "%s: \"%s\"", name, AT_string (a));
7417           else
7418             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7419           break;
7420
7421         case dw_val_class_file:
7422           {
7423             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7424
7425             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7426                                  a->dw_attr_val.v.val_file->filename);
7427             break;
7428           }
7429
7430         default:
7431           gcc_unreachable ();
7432         }
7433     }
7434
7435   FOR_EACH_CHILD (die, c, output_die (c));
7436
7437   /* Add null byte to terminate sibling list.  */
7438   if (die->die_child != NULL)
7439     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7440                          (unsigned long) die->die_offset);
7441 }
7442
7443 /* Output the compilation unit that appears at the beginning of the
7444    .debug_info section, and precedes the DIE descriptions.  */
7445
7446 static void
7447 output_compilation_unit_header (void)
7448 {
7449   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7450     dw2_asm_output_data (4, 0xffffffff,
7451       "Initial length escape value indicating 64-bit DWARF extension");
7452   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7453                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7454                        "Length of Compilation Unit Info");
7455   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7456   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7457                          debug_abbrev_section,
7458                          "Offset Into Abbrev. Section");
7459   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7460 }
7461
7462 /* Output the compilation unit DIE and its children.  */
7463
7464 static void
7465 output_comp_unit (dw_die_ref die, int output_if_empty)
7466 {
7467   const char *secname;
7468   char *oldsym, *tmp;
7469
7470   /* Unless we are outputting main CU, we may throw away empty ones.  */
7471   if (!output_if_empty && die->die_child == NULL)
7472     return;
7473
7474   /* Even if there are no children of this DIE, we must output the information
7475      about the compilation unit.  Otherwise, on an empty translation unit, we
7476      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7477      will then complain when examining the file.  First mark all the DIEs in
7478      this CU so we know which get local refs.  */
7479   mark_dies (die);
7480
7481   build_abbrev_table (die);
7482
7483   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7484   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7485   calc_die_sizes (die);
7486
7487   oldsym = die->die_symbol;
7488   if (oldsym)
7489     {
7490       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
7491
7492       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7493       secname = tmp;
7494       die->die_symbol = NULL;
7495       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7496     }
7497   else
7498     switch_to_section (debug_info_section);
7499
7500   /* Output debugging information.  */
7501   output_compilation_unit_header ();
7502   output_die (die);
7503
7504   /* Leave the marks on the main CU, so we can check them in
7505      output_pubnames.  */
7506   if (oldsym)
7507     {
7508       unmark_dies (die);
7509       die->die_symbol = oldsym;
7510     }
7511 }
7512
7513 /* Return the DWARF2/3 pubname associated with a decl.  */
7514
7515 static const char *
7516 dwarf2_name (tree decl, int scope)
7517 {
7518   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7519 }
7520
7521 /* Add a new entry to .debug_pubnames if appropriate.  */
7522
7523 static void
7524 add_pubname_string (const char *str, dw_die_ref die)
7525 {
7526   pubname_entry e;
7527
7528   e.die = die;
7529   e.name = xstrdup (str);
7530   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7531 }
7532
7533 static void
7534 add_pubname (tree decl, dw_die_ref die)
7535 {
7536
7537   if (TREE_PUBLIC (decl))
7538     add_pubname_string (dwarf2_name (decl, 1), die);
7539 }
7540
7541 /* Add a new entry to .debug_pubtypes if appropriate.  */
7542
7543 static void
7544 add_pubtype (tree decl, dw_die_ref die)
7545 {
7546   pubname_entry e;
7547
7548   e.name = NULL;
7549   if ((TREE_PUBLIC (decl)
7550        || die->die_parent == comp_unit_die)
7551       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7552     {
7553       e.die = die;
7554       if (TYPE_P (decl))
7555         {
7556           if (TYPE_NAME (decl))
7557             {
7558               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7559                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7560               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7561                        && DECL_NAME (TYPE_NAME (decl)))
7562                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7563               else
7564                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7565             }
7566         }
7567       else
7568         e.name = xstrdup (dwarf2_name (decl, 1));
7569
7570       /* If we don't have a name for the type, there's no point in adding
7571          it to the table.  */
7572       if (e.name && e.name[0] != '\0')
7573         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7574     }
7575 }
7576
7577 /* Output the public names table used to speed up access to externally
7578    visible names; or the public types table used to find type definitions.  */
7579
7580 static void
7581 output_pubnames (VEC (pubname_entry, gc) * names)
7582 {
7583   unsigned i;
7584   unsigned long pubnames_length = size_of_pubnames (names);
7585   pubname_ref pub;
7586
7587   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7588     dw2_asm_output_data (4, 0xffffffff,
7589       "Initial length escape value indicating 64-bit DWARF extension");
7590   if (names == pubname_table)
7591     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7592                          "Length of Public Names Info");
7593   else
7594     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7595                          "Length of Public Type Names Info");
7596   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7597   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7598                          debug_info_section,
7599                          "Offset of Compilation Unit Info");
7600   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7601                        "Compilation Unit Length");
7602
7603   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7604     {
7605       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7606       if (names == pubname_table)
7607         gcc_assert (pub->die->die_mark);
7608
7609       if (names != pubtype_table
7610           || pub->die->die_offset != 0
7611           || !flag_eliminate_unused_debug_types)
7612         {
7613           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7614                                "DIE offset");
7615
7616           dw2_asm_output_nstring (pub->name, -1, "external name");
7617         }
7618     }
7619
7620   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7621 }
7622
7623 /* Add a new entry to .debug_aranges if appropriate.  */
7624
7625 static void
7626 add_arange (tree decl, dw_die_ref die)
7627 {
7628   if (! DECL_SECTION_NAME (decl))
7629     return;
7630
7631   if (arange_table_in_use == arange_table_allocated)
7632     {
7633       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7634       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
7635                                     arange_table_allocated);
7636       memset (arange_table + arange_table_in_use, 0,
7637               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7638     }
7639
7640   arange_table[arange_table_in_use++] = die;
7641 }
7642
7643 /* Output the information that goes into the .debug_aranges table.
7644    Namely, define the beginning and ending address range of the
7645    text section generated for this compilation unit.  */
7646
7647 static void
7648 output_aranges (void)
7649 {
7650   unsigned i;
7651   unsigned long aranges_length = size_of_aranges ();
7652
7653   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7654     dw2_asm_output_data (4, 0xffffffff,
7655       "Initial length escape value indicating 64-bit DWARF extension");
7656   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7657                        "Length of Address Ranges Info");
7658   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7659   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7660                          debug_info_section,
7661                          "Offset of Compilation Unit Info");
7662   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7663   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7664
7665   /* We need to align to twice the pointer size here.  */
7666   if (DWARF_ARANGES_PAD_SIZE)
7667     {
7668       /* Pad using a 2 byte words so that padding is correct for any
7669          pointer size.  */
7670       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7671                            2 * DWARF2_ADDR_SIZE);
7672       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7673         dw2_asm_output_data (2, 0, NULL);
7674     }
7675
7676   /* It is necessary not to output these entries if the sections were
7677      not used; if the sections were not used, the length will be 0 and
7678      the address may end up as 0 if the section is discarded by ld
7679      --gc-sections, leaving an invalid (0, 0) entry that can be
7680      confused with the terminator.  */
7681   if (text_section_used)
7682     {
7683       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7684       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7685                             text_section_label, "Length");
7686     }
7687   if (cold_text_section_used)
7688     {
7689       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7690                            "Address");
7691       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7692                             cold_text_section_label, "Length");
7693     }
7694
7695   for (i = 0; i < arange_table_in_use; i++)
7696     {
7697       dw_die_ref die = arange_table[i];
7698
7699       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7700       gcc_assert (die->die_mark);
7701
7702       if (die->die_tag == DW_TAG_subprogram)
7703         {
7704           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7705                                "Address");
7706           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7707                                 get_AT_low_pc (die), "Length");
7708         }
7709       else
7710         {
7711           /* A static variable; extract the symbol from DW_AT_location.
7712              Note that this code isn't currently hit, as we only emit
7713              aranges for functions (jason 9/23/99).  */
7714           dw_attr_ref a = get_AT (die, DW_AT_location);
7715           dw_loc_descr_ref loc;
7716
7717           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7718
7719           loc = AT_loc (a);
7720           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7721
7722           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7723                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7724           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7725                                get_AT_unsigned (die, DW_AT_byte_size),
7726                                "Length");
7727         }
7728     }
7729
7730   /* Output the terminator words.  */
7731   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7732   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7733 }
7734
7735 /* Add a new entry to .debug_ranges.  Return the offset at which it
7736    was placed.  */
7737
7738 static unsigned int
7739 add_ranges_num (int num)
7740 {
7741   unsigned int in_use = ranges_table_in_use;
7742
7743   if (in_use == ranges_table_allocated)
7744     {
7745       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7746       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
7747                                     ranges_table_allocated);
7748       memset (ranges_table + ranges_table_in_use, 0,
7749               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7750     }
7751
7752   ranges_table[in_use].num = num;
7753   ranges_table_in_use = in_use + 1;
7754
7755   return in_use * 2 * DWARF2_ADDR_SIZE;
7756 }
7757
7758 /* Add a new entry to .debug_ranges corresponding to a block, or a
7759    range terminator if BLOCK is NULL.  */
7760
7761 static unsigned int
7762 add_ranges (const_tree block)
7763 {
7764   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7765 }
7766
7767 /* Add a new entry to .debug_ranges corresponding to a pair of
7768    labels.  */
7769
7770 static unsigned int
7771 add_ranges_by_labels (const char *begin, const char *end)
7772 {
7773   unsigned int in_use = ranges_by_label_in_use;
7774
7775   if (in_use == ranges_by_label_allocated)
7776     {
7777       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7778       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
7779                                        ranges_by_label,
7780                                        ranges_by_label_allocated);
7781       memset (ranges_by_label + ranges_by_label_in_use, 0,
7782               RANGES_TABLE_INCREMENT
7783               * sizeof (struct dw_ranges_by_label_struct));
7784     }
7785
7786   ranges_by_label[in_use].begin = begin;
7787   ranges_by_label[in_use].end = end;
7788   ranges_by_label_in_use = in_use + 1;
7789
7790   return add_ranges_num (-(int)in_use - 1);
7791 }
7792
7793 static void
7794 output_ranges (void)
7795 {
7796   unsigned i;
7797   static const char *const start_fmt = "Offset 0x%x";
7798   const char *fmt = start_fmt;
7799
7800   for (i = 0; i < ranges_table_in_use; i++)
7801     {
7802       int block_num = ranges_table[i].num;
7803
7804       if (block_num > 0)
7805         {
7806           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7807           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7808
7809           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7810           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7811
7812           /* If all code is in the text section, then the compilation
7813              unit base address defaults to DW_AT_low_pc, which is the
7814              base of the text section.  */
7815           if (!have_multiple_function_sections)
7816             {
7817               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7818                                     text_section_label,
7819                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7820               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7821                                     text_section_label, NULL);
7822             }
7823
7824           /* Otherwise, the compilation unit base address is zero,
7825              which allows us to use absolute addresses, and not worry
7826              about whether the target supports cross-section
7827              arithmetic.  */
7828           else
7829             {
7830               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7831                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7832               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7833             }
7834
7835           fmt = NULL;
7836         }
7837
7838       /* Negative block_num stands for an index into ranges_by_label.  */
7839       else if (block_num < 0)
7840         {
7841           int lab_idx = - block_num - 1;
7842
7843           if (!have_multiple_function_sections)
7844             {
7845               gcc_unreachable ();
7846 #if 0
7847               /* If we ever use add_ranges_by_labels () for a single
7848                  function section, all we have to do is to take out
7849                  the #if 0 above.  */
7850               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7851                                     ranges_by_label[lab_idx].begin,
7852                                     text_section_label,
7853                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7854               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7855                                     ranges_by_label[lab_idx].end,
7856                                     text_section_label, NULL);
7857 #endif
7858             }
7859           else
7860             {
7861               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7862                                    ranges_by_label[lab_idx].begin,
7863                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7864               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7865                                    ranges_by_label[lab_idx].end,
7866                                    NULL);
7867             }
7868         }
7869       else
7870         {
7871           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7872           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7873           fmt = start_fmt;
7874         }
7875     }
7876 }
7877
7878 /* Data structure containing information about input files.  */
7879 struct file_info
7880 {
7881   const char *path;     /* Complete file name.  */
7882   const char *fname;    /* File name part.  */
7883   int length;           /* Length of entire string.  */
7884   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7885   int dir_idx;          /* Index in directory table.  */
7886 };
7887
7888 /* Data structure containing information about directories with source
7889    files.  */
7890 struct dir_info
7891 {
7892   const char *path;     /* Path including directory name.  */
7893   int length;           /* Path length.  */
7894   int prefix;           /* Index of directory entry which is a prefix.  */
7895   int count;            /* Number of files in this directory.  */
7896   int dir_idx;          /* Index of directory used as base.  */
7897 };
7898
7899 /* Callback function for file_info comparison.  We sort by looking at
7900    the directories in the path.  */
7901
7902 static int
7903 file_info_cmp (const void *p1, const void *p2)
7904 {
7905   const struct file_info *const s1 = (const struct file_info *) p1;
7906   const struct file_info *const s2 = (const struct file_info *) p2;
7907   const unsigned char *cp1;
7908   const unsigned char *cp2;
7909
7910   /* Take care of file names without directories.  We need to make sure that
7911      we return consistent values to qsort since some will get confused if
7912      we return the same value when identical operands are passed in opposite
7913      orders.  So if neither has a directory, return 0 and otherwise return
7914      1 or -1 depending on which one has the directory.  */
7915   if ((s1->path == s1->fname || s2->path == s2->fname))
7916     return (s2->path == s2->fname) - (s1->path == s1->fname);
7917
7918   cp1 = (const unsigned char *) s1->path;
7919   cp2 = (const unsigned char *) s2->path;
7920
7921   while (1)
7922     {
7923       ++cp1;
7924       ++cp2;
7925       /* Reached the end of the first path?  If so, handle like above.  */
7926       if ((cp1 == (const unsigned char *) s1->fname)
7927           || (cp2 == (const unsigned char *) s2->fname))
7928         return ((cp2 == (const unsigned char *) s2->fname)
7929                 - (cp1 == (const unsigned char *) s1->fname));
7930
7931       /* Character of current path component the same?  */
7932       else if (*cp1 != *cp2)
7933         return *cp1 - *cp2;
7934     }
7935 }
7936
7937 struct file_name_acquire_data
7938 {
7939   struct file_info *files;
7940   int used_files;
7941   int max_files;
7942 };
7943
7944 /* Traversal function for the hash table.  */
7945
7946 static int
7947 file_name_acquire (void ** slot, void *data)
7948 {
7949   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
7950   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
7951   struct file_info *fi;
7952   const char *f;
7953
7954   gcc_assert (fnad->max_files >= d->emitted_number);
7955
7956   if (! d->emitted_number)
7957     return 1;
7958
7959   gcc_assert (fnad->max_files != fnad->used_files);
7960
7961   fi = fnad->files + fnad->used_files++;
7962
7963   /* Skip all leading "./".  */
7964   f = d->filename;
7965   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7966     f += 2;
7967
7968   /* Create a new array entry.  */
7969   fi->path = f;
7970   fi->length = strlen (f);
7971   fi->file_idx = d;
7972
7973   /* Search for the file name part.  */
7974   f = strrchr (f, DIR_SEPARATOR);
7975 #if defined (DIR_SEPARATOR_2)
7976   {
7977     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7978
7979     if (g != NULL)
7980       {
7981         if (f == NULL || f < g)
7982           f = g;
7983       }
7984   }
7985 #endif
7986
7987   fi->fname = f == NULL ? fi->path : f + 1;
7988   return 1;
7989 }
7990
7991 /* Output the directory table and the file name table.  We try to minimize
7992    the total amount of memory needed.  A heuristic is used to avoid large
7993    slowdowns with many input files.  */
7994
7995 static void
7996 output_file_names (void)
7997 {
7998   struct file_name_acquire_data fnad;
7999   int numfiles;
8000   struct file_info *files;
8001   struct dir_info *dirs;
8002   int *saved;
8003   int *savehere;
8004   int *backmap;
8005   int ndirs;
8006   int idx_offset;
8007   int i;
8008   int idx;
8009
8010   if (!last_emitted_file)
8011     {
8012       dw2_asm_output_data (1, 0, "End directory table");
8013       dw2_asm_output_data (1, 0, "End file name table");
8014       return;
8015     }
8016
8017   numfiles = last_emitted_file->emitted_number;
8018
8019   /* Allocate the various arrays we need.  */
8020   files = XALLOCAVEC (struct file_info, numfiles);
8021   dirs = XALLOCAVEC (struct dir_info, numfiles);
8022
8023   fnad.files = files;
8024   fnad.used_files = 0;
8025   fnad.max_files = numfiles;
8026   htab_traverse (file_table, file_name_acquire, &fnad);
8027   gcc_assert (fnad.used_files == fnad.max_files);
8028
8029   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8030
8031   /* Find all the different directories used.  */
8032   dirs[0].path = files[0].path;
8033   dirs[0].length = files[0].fname - files[0].path;
8034   dirs[0].prefix = -1;
8035   dirs[0].count = 1;
8036   dirs[0].dir_idx = 0;
8037   files[0].dir_idx = 0;
8038   ndirs = 1;
8039
8040   for (i = 1; i < numfiles; i++)
8041     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8042         && memcmp (dirs[ndirs - 1].path, files[i].path,
8043                    dirs[ndirs - 1].length) == 0)
8044       {
8045         /* Same directory as last entry.  */
8046         files[i].dir_idx = ndirs - 1;
8047         ++dirs[ndirs - 1].count;
8048       }
8049     else
8050       {
8051         int j;
8052
8053         /* This is a new directory.  */
8054         dirs[ndirs].path = files[i].path;
8055         dirs[ndirs].length = files[i].fname - files[i].path;
8056         dirs[ndirs].count = 1;
8057         dirs[ndirs].dir_idx = ndirs;
8058         files[i].dir_idx = ndirs;
8059
8060         /* Search for a prefix.  */
8061         dirs[ndirs].prefix = -1;
8062         for (j = 0; j < ndirs; j++)
8063           if (dirs[j].length < dirs[ndirs].length
8064               && dirs[j].length > 1
8065               && (dirs[ndirs].prefix == -1
8066                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8067               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8068             dirs[ndirs].prefix = j;
8069
8070         ++ndirs;
8071       }
8072
8073   /* Now to the actual work.  We have to find a subset of the directories which
8074      allow expressing the file name using references to the directory table
8075      with the least amount of characters.  We do not do an exhaustive search
8076      where we would have to check out every combination of every single
8077      possible prefix.  Instead we use a heuristic which provides nearly optimal
8078      results in most cases and never is much off.  */
8079   saved = XALLOCAVEC (int, ndirs);
8080   savehere = XALLOCAVEC (int, ndirs);
8081
8082   memset (saved, '\0', ndirs * sizeof (saved[0]));
8083   for (i = 0; i < ndirs; i++)
8084     {
8085       int j;
8086       int total;
8087
8088       /* We can always save some space for the current directory.  But this
8089          does not mean it will be enough to justify adding the directory.  */
8090       savehere[i] = dirs[i].length;
8091       total = (savehere[i] - saved[i]) * dirs[i].count;
8092
8093       for (j = i + 1; j < ndirs; j++)
8094         {
8095           savehere[j] = 0;
8096           if (saved[j] < dirs[i].length)
8097             {
8098               /* Determine whether the dirs[i] path is a prefix of the
8099                  dirs[j] path.  */
8100               int k;
8101
8102               k = dirs[j].prefix;
8103               while (k != -1 && k != (int) i)
8104                 k = dirs[k].prefix;
8105
8106               if (k == (int) i)
8107                 {
8108                   /* Yes it is.  We can possibly save some memory by
8109                      writing the filenames in dirs[j] relative to
8110                      dirs[i].  */
8111                   savehere[j] = dirs[i].length;
8112                   total += (savehere[j] - saved[j]) * dirs[j].count;
8113                 }
8114             }
8115         }
8116
8117       /* Check whether we can save enough to justify adding the dirs[i]
8118          directory.  */
8119       if (total > dirs[i].length + 1)
8120         {
8121           /* It's worthwhile adding.  */
8122           for (j = i; j < ndirs; j++)
8123             if (savehere[j] > 0)
8124               {
8125                 /* Remember how much we saved for this directory so far.  */
8126                 saved[j] = savehere[j];
8127
8128                 /* Remember the prefix directory.  */
8129                 dirs[j].dir_idx = i;
8130               }
8131         }
8132     }
8133
8134   /* Emit the directory name table.  */
8135   idx = 1;
8136   idx_offset = dirs[0].length > 0 ? 1 : 0;
8137   for (i = 1 - idx_offset; i < ndirs; i++)
8138     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8139                             "Directory Entry: 0x%x", i + idx_offset);
8140
8141   dw2_asm_output_data (1, 0, "End directory table");
8142
8143   /* We have to emit them in the order of emitted_number since that's
8144      used in the debug info generation.  To do this efficiently we
8145      generate a back-mapping of the indices first.  */
8146   backmap = XALLOCAVEC (int, numfiles);
8147   for (i = 0; i < numfiles; i++)
8148     backmap[files[i].file_idx->emitted_number - 1] = i;
8149
8150   /* Now write all the file names.  */
8151   for (i = 0; i < numfiles; i++)
8152     {
8153       int file_idx = backmap[i];
8154       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8155
8156       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8157                               "File Entry: 0x%x", (unsigned) i + 1);
8158
8159       /* Include directory index.  */
8160       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8161
8162       /* Modification time.  */
8163       dw2_asm_output_data_uleb128 (0, NULL);
8164
8165       /* File length in bytes.  */
8166       dw2_asm_output_data_uleb128 (0, NULL);
8167     }
8168
8169   dw2_asm_output_data (1, 0, "End file name table");
8170 }
8171
8172
8173 /* Output the source line number correspondence information.  This
8174    information goes into the .debug_line section.  */
8175
8176 static void
8177 output_line_info (void)
8178 {
8179   char l1[20], l2[20], p1[20], p2[20];
8180   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8181   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8182   unsigned opc;
8183   unsigned n_op_args;
8184   unsigned long lt_index;
8185   unsigned long current_line;
8186   long line_offset;
8187   long line_delta;
8188   unsigned long current_file;
8189   unsigned long function;
8190
8191   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8192   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8193   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8194   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8195
8196   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8197     dw2_asm_output_data (4, 0xffffffff,
8198       "Initial length escape value indicating 64-bit DWARF extension");
8199   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8200                         "Length of Source Line Info");
8201   ASM_OUTPUT_LABEL (asm_out_file, l1);
8202
8203   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8204   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8205   ASM_OUTPUT_LABEL (asm_out_file, p1);
8206
8207   /* Define the architecture-dependent minimum instruction length (in
8208    bytes).  In this implementation of DWARF, this field is used for
8209    information purposes only.  Since GCC generates assembly language,
8210    we have no a priori knowledge of how many instruction bytes are
8211    generated for each source line, and therefore can use only the
8212    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8213    commands.  Accordingly, we fix this as `1', which is "correct
8214    enough" for all architectures, and don't let the target override.  */
8215   dw2_asm_output_data (1, 1,
8216                        "Minimum Instruction Length");
8217
8218   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8219                        "Default is_stmt_start flag");
8220   dw2_asm_output_data (1, DWARF_LINE_BASE,
8221                        "Line Base Value (Special Opcodes)");
8222   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8223                        "Line Range Value (Special Opcodes)");
8224   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8225                        "Special Opcode Base");
8226
8227   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8228     {
8229       switch (opc)
8230         {
8231         case DW_LNS_advance_pc:
8232         case DW_LNS_advance_line:
8233         case DW_LNS_set_file:
8234         case DW_LNS_set_column:
8235         case DW_LNS_fixed_advance_pc:
8236           n_op_args = 1;
8237           break;
8238         default:
8239           n_op_args = 0;
8240           break;
8241         }
8242
8243       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8244                            opc, n_op_args);
8245     }
8246
8247   /* Write out the information about the files we use.  */
8248   output_file_names ();
8249   ASM_OUTPUT_LABEL (asm_out_file, p2);
8250
8251   /* We used to set the address register to the first location in the text
8252      section here, but that didn't accomplish anything since we already
8253      have a line note for the opening brace of the first function.  */
8254
8255   /* Generate the line number to PC correspondence table, encoded as
8256      a series of state machine operations.  */
8257   current_file = 1;
8258   current_line = 1;
8259
8260   if (cfun && in_cold_section_p)
8261     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8262   else
8263     strcpy (prev_line_label, text_section_label);
8264   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8265     {
8266       dw_line_info_ref line_info = &line_info_table[lt_index];
8267
8268 #if 0
8269       /* Disable this optimization for now; GDB wants to see two line notes
8270          at the beginning of a function so it can find the end of the
8271          prologue.  */
8272
8273       /* Don't emit anything for redundant notes.  Just updating the
8274          address doesn't accomplish anything, because we already assume
8275          that anything after the last address is this line.  */
8276       if (line_info->dw_line_num == current_line
8277           && line_info->dw_file_num == current_file)
8278         continue;
8279 #endif
8280
8281       /* Emit debug info for the address of the current line.
8282
8283          Unfortunately, we have little choice here currently, and must always
8284          use the most general form.  GCC does not know the address delta
8285          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8286          attributes which will give an upper bound on the address range.  We
8287          could perhaps use length attributes to determine when it is safe to
8288          use DW_LNS_fixed_advance_pc.  */
8289
8290       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8291       if (0)
8292         {
8293           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8294           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8295                                "DW_LNS_fixed_advance_pc");
8296           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8297         }
8298       else
8299         {
8300           /* This can handle any delta.  This takes
8301              4+DWARF2_ADDR_SIZE bytes.  */
8302           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8303           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8304           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8305           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8306         }
8307
8308       strcpy (prev_line_label, line_label);
8309
8310       /* Emit debug info for the source file of the current line, if
8311          different from the previous line.  */
8312       if (line_info->dw_file_num != current_file)
8313         {
8314           current_file = line_info->dw_file_num;
8315           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8316           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8317         }
8318
8319       /* Emit debug info for the current line number, choosing the encoding
8320          that uses the least amount of space.  */
8321       if (line_info->dw_line_num != current_line)
8322         {
8323           line_offset = line_info->dw_line_num - current_line;
8324           line_delta = line_offset - DWARF_LINE_BASE;
8325           current_line = line_info->dw_line_num;
8326           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8327             /* This can handle deltas from -10 to 234, using the current
8328                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8329                takes 1 byte.  */
8330             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8331                                  "line %lu", current_line);
8332           else
8333             {
8334               /* This can handle any delta.  This takes at least 4 bytes,
8335                  depending on the value being encoded.  */
8336               dw2_asm_output_data (1, DW_LNS_advance_line,
8337                                    "advance to line %lu", current_line);
8338               dw2_asm_output_data_sleb128 (line_offset, NULL);
8339               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8340             }
8341         }
8342       else
8343         /* We still need to start a new row, so output a copy insn.  */
8344         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8345     }
8346
8347   /* Emit debug info for the address of the end of the function.  */
8348   if (0)
8349     {
8350       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8351                            "DW_LNS_fixed_advance_pc");
8352       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8353     }
8354   else
8355     {
8356       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8357       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8358       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8359       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8360     }
8361
8362   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8363   dw2_asm_output_data_uleb128 (1, NULL);
8364   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8365
8366   function = 0;
8367   current_file = 1;
8368   current_line = 1;
8369   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8370     {
8371       dw_separate_line_info_ref line_info
8372         = &separate_line_info_table[lt_index];
8373
8374 #if 0
8375       /* Don't emit anything for redundant notes.  */
8376       if (line_info->dw_line_num == current_line
8377           && line_info->dw_file_num == current_file
8378           && line_info->function == function)
8379         goto cont;
8380 #endif
8381
8382       /* Emit debug info for the address of the current line.  If this is
8383          a new function, or the first line of a function, then we need
8384          to handle it differently.  */
8385       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8386                                    lt_index);
8387       if (function != line_info->function)
8388         {
8389           function = line_info->function;
8390
8391           /* Set the address register to the first line in the function.  */
8392           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8393           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8394           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8395           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8396         }
8397       else
8398         {
8399           /* ??? See the DW_LNS_advance_pc comment above.  */
8400           if (0)
8401             {
8402               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8403                                    "DW_LNS_fixed_advance_pc");
8404               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8405             }
8406           else
8407             {
8408               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8409               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8410               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8411               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8412             }
8413         }
8414
8415       strcpy (prev_line_label, line_label);
8416
8417       /* Emit debug info for the source file of the current line, if
8418          different from the previous line.  */
8419       if (line_info->dw_file_num != current_file)
8420         {
8421           current_file = line_info->dw_file_num;
8422           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8423           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8424         }
8425
8426       /* Emit debug info for the current line number, choosing the encoding
8427          that uses the least amount of space.  */
8428       if (line_info->dw_line_num != current_line)
8429         {
8430           line_offset = line_info->dw_line_num - current_line;
8431           line_delta = line_offset - DWARF_LINE_BASE;
8432           current_line = line_info->dw_line_num;
8433           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8434             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8435                                  "line %lu", current_line);
8436           else
8437             {
8438               dw2_asm_output_data (1, DW_LNS_advance_line,
8439                                    "advance to line %lu", current_line);
8440               dw2_asm_output_data_sleb128 (line_offset, NULL);
8441               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8442             }
8443         }
8444       else
8445         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8446
8447 #if 0
8448     cont:
8449 #endif
8450
8451       lt_index++;
8452
8453       /* If we're done with a function, end its sequence.  */
8454       if (lt_index == separate_line_info_table_in_use
8455           || separate_line_info_table[lt_index].function != function)
8456         {
8457           current_file = 1;
8458           current_line = 1;
8459
8460           /* Emit debug info for the address of the end of the function.  */
8461           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8462           if (0)
8463             {
8464               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8465                                    "DW_LNS_fixed_advance_pc");
8466               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8467             }
8468           else
8469             {
8470               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8471               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8472               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8473               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8474             }
8475
8476           /* Output the marker for the end of this sequence.  */
8477           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8478           dw2_asm_output_data_uleb128 (1, NULL);
8479           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8480         }
8481     }
8482
8483   /* Output the marker for the end of the line number info.  */
8484   ASM_OUTPUT_LABEL (asm_out_file, l2);
8485 }
8486 \f
8487 /* Given a pointer to a tree node for some base type, return a pointer to
8488    a DIE that describes the given type.
8489
8490    This routine must only be called for GCC type nodes that correspond to
8491    Dwarf base (fundamental) types.  */
8492
8493 static dw_die_ref
8494 base_type_die (tree type)
8495 {
8496   dw_die_ref base_type_result;
8497   enum dwarf_type encoding;
8498
8499   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8500     return 0;
8501
8502   switch (TREE_CODE (type))
8503     {
8504     case INTEGER_TYPE:
8505       if (TYPE_STRING_FLAG (type))
8506         {
8507           if (TYPE_UNSIGNED (type))
8508             encoding = DW_ATE_unsigned_char;
8509           else
8510             encoding = DW_ATE_signed_char;
8511         }
8512       else if (TYPE_UNSIGNED (type))
8513         encoding = DW_ATE_unsigned;
8514       else
8515         encoding = DW_ATE_signed;
8516       break;
8517
8518     case REAL_TYPE:
8519       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8520         encoding = DW_ATE_decimal_float;
8521       else
8522         encoding = DW_ATE_float;
8523       break;
8524
8525     case FIXED_POINT_TYPE:
8526       if (TYPE_UNSIGNED (type))
8527         encoding = DW_ATE_unsigned_fixed;
8528       else
8529         encoding = DW_ATE_signed_fixed;
8530       break;
8531
8532       /* Dwarf2 doesn't know anything about complex ints, so use
8533          a user defined type for it.  */
8534     case COMPLEX_TYPE:
8535       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8536         encoding = DW_ATE_complex_float;
8537       else
8538         encoding = DW_ATE_lo_user;
8539       break;
8540
8541     case BOOLEAN_TYPE:
8542       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8543       encoding = DW_ATE_boolean;
8544       break;
8545
8546     default:
8547       /* No other TREE_CODEs are Dwarf fundamental types.  */
8548       gcc_unreachable ();
8549     }
8550
8551   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8552
8553   /* This probably indicates a bug.  */
8554   if (! TYPE_NAME (type))
8555     add_name_attribute (base_type_result, "__unknown__");
8556
8557   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8558                    int_size_in_bytes (type));
8559   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8560
8561   return base_type_result;
8562 }
8563
8564 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8565    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8566
8567 static inline int
8568 is_base_type (tree type)
8569 {
8570   switch (TREE_CODE (type))
8571     {
8572     case ERROR_MARK:
8573     case VOID_TYPE:
8574     case INTEGER_TYPE:
8575     case REAL_TYPE:
8576     case FIXED_POINT_TYPE:
8577     case COMPLEX_TYPE:
8578     case BOOLEAN_TYPE:
8579       return 1;
8580
8581     case ARRAY_TYPE:
8582     case RECORD_TYPE:
8583     case UNION_TYPE:
8584     case QUAL_UNION_TYPE:
8585     case ENUMERAL_TYPE:
8586     case FUNCTION_TYPE:
8587     case METHOD_TYPE:
8588     case POINTER_TYPE:
8589     case REFERENCE_TYPE:
8590     case OFFSET_TYPE:
8591     case LANG_TYPE:
8592     case VECTOR_TYPE:
8593       return 0;
8594
8595     default:
8596       gcc_unreachable ();
8597     }
8598
8599   return 0;
8600 }
8601
8602 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8603    node, return the size in bits for the type if it is a constant, or else
8604    return the alignment for the type if the type's size is not constant, or
8605    else return BITS_PER_WORD if the type actually turns out to be an
8606    ERROR_MARK node.  */
8607
8608 static inline unsigned HOST_WIDE_INT
8609 simple_type_size_in_bits (const_tree type)
8610 {
8611   if (TREE_CODE (type) == ERROR_MARK)
8612     return BITS_PER_WORD;
8613   else if (TYPE_SIZE (type) == NULL_TREE)
8614     return 0;
8615   else if (host_integerp (TYPE_SIZE (type), 1))
8616     return tree_low_cst (TYPE_SIZE (type), 1);
8617   else
8618     return TYPE_ALIGN (type);
8619 }
8620
8621 /* Return true if the debug information for the given type should be
8622    emitted as a subrange type.  */
8623
8624 static inline bool
8625 is_subrange_type (const_tree type)
8626 {
8627   tree subtype = TREE_TYPE (type);
8628
8629   /* Subrange types are identified by the fact that they are integer
8630      types, and that they have a subtype which is either an integer type
8631      or an enumeral type.  */
8632
8633   if (TREE_CODE (type) != INTEGER_TYPE
8634       || subtype == NULL_TREE)
8635     return false;
8636
8637   if (TREE_CODE (subtype) != INTEGER_TYPE
8638       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8639     return false;
8640
8641   if (TREE_CODE (type) == TREE_CODE (subtype)
8642       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8643       && TYPE_MIN_VALUE (type) != NULL
8644       && TYPE_MIN_VALUE (subtype) != NULL
8645       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8646       && TYPE_MAX_VALUE (type) != NULL
8647       && TYPE_MAX_VALUE (subtype) != NULL
8648       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8649     {
8650       /* The type and its subtype have the same representation.  If in
8651          addition the two types also have the same name, then the given
8652          type is not a subrange type, but rather a plain base type.  */
8653       /* FIXME: brobecker/2004-03-22:
8654          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8655          therefore be sufficient to check the TYPE_SIZE node pointers
8656          rather than checking the actual size.  Unfortunately, we have
8657          found some cases, such as in the Ada "integer" type, where
8658          this is not the case.  Until this problem is solved, we need to
8659          keep checking the actual size.  */
8660       tree type_name = TYPE_NAME (type);
8661       tree subtype_name = TYPE_NAME (subtype);
8662
8663       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8664         type_name = DECL_NAME (type_name);
8665
8666       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8667         subtype_name = DECL_NAME (subtype_name);
8668
8669       if (type_name == subtype_name)
8670         return false;
8671     }
8672
8673   return true;
8674 }
8675
8676 /*  Given a pointer to a tree node for a subrange type, return a pointer
8677     to a DIE that describes the given type.  */
8678
8679 static dw_die_ref
8680 subrange_type_die (tree type, dw_die_ref context_die)
8681 {
8682   dw_die_ref subrange_die;
8683   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8684
8685   if (context_die == NULL)
8686     context_die = comp_unit_die;
8687
8688   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8689
8690   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8691     {
8692       /* The size of the subrange type and its base type do not match,
8693          so we need to generate a size attribute for the subrange type.  */
8694       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8695     }
8696
8697   if (TYPE_MIN_VALUE (type) != NULL)
8698     add_bound_info (subrange_die, DW_AT_lower_bound,
8699                     TYPE_MIN_VALUE (type));
8700   if (TYPE_MAX_VALUE (type) != NULL)
8701     add_bound_info (subrange_die, DW_AT_upper_bound,
8702                     TYPE_MAX_VALUE (type));
8703
8704   return subrange_die;
8705 }
8706
8707 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8708    entry that chains various modifiers in front of the given type.  */
8709
8710 static dw_die_ref
8711 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8712                    dw_die_ref context_die)
8713 {
8714   enum tree_code code = TREE_CODE (type);
8715   dw_die_ref mod_type_die;
8716   dw_die_ref sub_die = NULL;
8717   tree item_type = NULL;
8718   tree qualified_type;
8719   tree name;
8720
8721   if (code == ERROR_MARK)
8722     return NULL;
8723
8724   /* See if we already have the appropriately qualified variant of
8725      this type.  */
8726   qualified_type
8727     = get_qualified_type (type,
8728                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8729                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8730
8731   /* If we do, then we can just use its DIE, if it exists.  */
8732   if (qualified_type)
8733     {
8734       mod_type_die = lookup_type_die (qualified_type);
8735       if (mod_type_die)
8736         return mod_type_die;
8737     }
8738
8739   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8740
8741   /* Handle C typedef types.  */
8742   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8743     {
8744       tree dtype = TREE_TYPE (name);
8745
8746       if (qualified_type == dtype)
8747         {
8748           /* For a named type, use the typedef.  */
8749           gen_type_die (qualified_type, context_die);
8750           return lookup_type_die (qualified_type);
8751         }
8752       else if (is_const_type < TYPE_READONLY (dtype)
8753                || is_volatile_type < TYPE_VOLATILE (dtype)
8754                || (is_const_type <= TYPE_READONLY (dtype)
8755                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8756                    && DECL_ORIGINAL_TYPE (name) != type))
8757         /* cv-unqualified version of named type.  Just use the unnamed
8758            type to which it refers.  */
8759         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8760                                   is_const_type, is_volatile_type,
8761                                   context_die);
8762       /* Else cv-qualified version of named type; fall through.  */
8763     }
8764
8765   if (is_const_type)
8766     {
8767       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8768       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8769     }
8770   else if (is_volatile_type)
8771     {
8772       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8773       sub_die = modified_type_die (type, 0, 0, context_die);
8774     }
8775   else if (code == POINTER_TYPE)
8776     {
8777       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8778       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8779                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8780       item_type = TREE_TYPE (type);
8781     }
8782   else if (code == REFERENCE_TYPE)
8783     {
8784       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8785       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8786                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8787       item_type = TREE_TYPE (type);
8788     }
8789   else if (is_subrange_type (type))
8790     {
8791       mod_type_die = subrange_type_die (type, context_die);
8792       item_type = TREE_TYPE (type);
8793     }
8794   else if (is_base_type (type))
8795     mod_type_die = base_type_die (type);
8796   else
8797     {
8798       gen_type_die (type, context_die);
8799
8800       /* We have to get the type_main_variant here (and pass that to the
8801          `lookup_type_die' routine) because the ..._TYPE node we have
8802          might simply be a *copy* of some original type node (where the
8803          copy was created to help us keep track of typedef names) and
8804          that copy might have a different TYPE_UID from the original
8805          ..._TYPE node.  */
8806       if (TREE_CODE (type) != VECTOR_TYPE)
8807         return lookup_type_die (type_main_variant (type));
8808       else
8809         /* Vectors have the debugging information in the type,
8810            not the main variant.  */
8811         return lookup_type_die (type);
8812     }
8813
8814   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8815      don't output a DW_TAG_typedef, since there isn't one in the
8816      user's program; just attach a DW_AT_name to the type.  */
8817   if (name
8818       && (TREE_CODE (name) != TYPE_DECL
8819           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8820     {
8821       if (TREE_CODE (name) == TYPE_DECL)
8822         /* Could just call add_name_and_src_coords_attributes here,
8823            but since this is a builtin type it doesn't have any
8824            useful source coordinates anyway.  */
8825         name = DECL_NAME (name);
8826       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8827     }
8828
8829   if (qualified_type)
8830     equate_type_number_to_die (qualified_type, mod_type_die);
8831
8832   if (item_type)
8833     /* We must do this after the equate_type_number_to_die call, in case
8834        this is a recursive type.  This ensures that the modified_type_die
8835        recursion will terminate even if the type is recursive.  Recursive
8836        types are possible in Ada.  */
8837     sub_die = modified_type_die (item_type,
8838                                  TYPE_READONLY (item_type),
8839                                  TYPE_VOLATILE (item_type),
8840                                  context_die);
8841
8842   if (sub_die != NULL)
8843     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8844
8845   return mod_type_die;
8846 }
8847
8848 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8849    an enumerated type.  */
8850
8851 static inline int
8852 type_is_enum (const_tree type)
8853 {
8854   return TREE_CODE (type) == ENUMERAL_TYPE;
8855 }
8856
8857 /* Return the DBX register number described by a given RTL node.  */
8858
8859 static unsigned int
8860 dbx_reg_number (const_rtx rtl)
8861 {
8862   unsigned regno = REGNO (rtl);
8863
8864   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8865
8866 #ifdef LEAF_REG_REMAP
8867   if (current_function_uses_only_leaf_regs)
8868     {
8869       int leaf_reg = LEAF_REG_REMAP (regno);
8870       if (leaf_reg != -1)
8871         regno = (unsigned) leaf_reg;
8872     }
8873 #endif
8874
8875   return DBX_REGISTER_NUMBER (regno);
8876 }
8877
8878 /* Optionally add a DW_OP_piece term to a location description expression.
8879    DW_OP_piece is only added if the location description expression already
8880    doesn't end with DW_OP_piece.  */
8881
8882 static void
8883 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8884 {
8885   dw_loc_descr_ref loc;
8886
8887   if (*list_head != NULL)
8888     {
8889       /* Find the end of the chain.  */
8890       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8891         ;
8892
8893       if (loc->dw_loc_opc != DW_OP_piece)
8894         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8895     }
8896 }
8897
8898 /* Return a location descriptor that designates a machine register or
8899    zero if there is none.  */
8900
8901 static dw_loc_descr_ref
8902 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8903 {
8904   rtx regs;
8905
8906   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8907     return 0;
8908
8909   regs = targetm.dwarf_register_span (rtl);
8910
8911   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8912     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8913   else
8914     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8915 }
8916
8917 /* Return a location descriptor that designates a machine register for
8918    a given hard register number.  */
8919
8920 static dw_loc_descr_ref
8921 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8922 {
8923   dw_loc_descr_ref reg_loc_descr;
8924   if (regno <= 31)
8925     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8926   else
8927     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8928
8929   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8930     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8931
8932   return reg_loc_descr;
8933 }
8934
8935 /* Given an RTL of a register, return a location descriptor that
8936    designates a value that spans more than one register.  */
8937
8938 static dw_loc_descr_ref
8939 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8940                              enum var_init_status initialized)
8941 {
8942   int nregs, size, i;
8943   unsigned reg;
8944   dw_loc_descr_ref loc_result = NULL;
8945
8946   reg = REGNO (rtl);
8947 #ifdef LEAF_REG_REMAP
8948   if (current_function_uses_only_leaf_regs)
8949     {
8950       int leaf_reg = LEAF_REG_REMAP (reg);
8951       if (leaf_reg != -1)
8952         reg = (unsigned) leaf_reg;
8953     }
8954 #endif
8955   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8956   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8957
8958   /* Simple, contiguous registers.  */
8959   if (regs == NULL_RTX)
8960     {
8961       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8962
8963       loc_result = NULL;
8964       while (nregs--)
8965         {
8966           dw_loc_descr_ref t;
8967
8968           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8969                                       VAR_INIT_STATUS_INITIALIZED);
8970           add_loc_descr (&loc_result, t);
8971           add_loc_descr_op_piece (&loc_result, size);
8972           ++reg;
8973         }
8974       return loc_result;
8975     }
8976
8977   /* Now onto stupid register sets in non contiguous locations.  */
8978
8979   gcc_assert (GET_CODE (regs) == PARALLEL);
8980
8981   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8982   loc_result = NULL;
8983
8984   for (i = 0; i < XVECLEN (regs, 0); ++i)
8985     {
8986       dw_loc_descr_ref t;
8987
8988       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8989                                   VAR_INIT_STATUS_INITIALIZED);
8990       add_loc_descr (&loc_result, t);
8991       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8992       add_loc_descr_op_piece (&loc_result, size);
8993     }
8994
8995   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8996     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8997   return loc_result;
8998 }
8999
9000 /* Return a location descriptor that designates a constant.  */
9001
9002 static dw_loc_descr_ref
9003 int_loc_descriptor (HOST_WIDE_INT i)
9004 {
9005   enum dwarf_location_atom op;
9006
9007   /* Pick the smallest representation of a constant, rather than just
9008      defaulting to the LEB encoding.  */
9009   if (i >= 0)
9010     {
9011       if (i <= 31)
9012         op = DW_OP_lit0 + i;
9013       else if (i <= 0xff)
9014         op = DW_OP_const1u;
9015       else if (i <= 0xffff)
9016         op = DW_OP_const2u;
9017       else if (HOST_BITS_PER_WIDE_INT == 32
9018                || i <= 0xffffffff)
9019         op = DW_OP_const4u;
9020       else
9021         op = DW_OP_constu;
9022     }
9023   else
9024     {
9025       if (i >= -0x80)
9026         op = DW_OP_const1s;
9027       else if (i >= -0x8000)
9028         op = DW_OP_const2s;
9029       else if (HOST_BITS_PER_WIDE_INT == 32
9030                || i >= -0x80000000)
9031         op = DW_OP_const4s;
9032       else
9033         op = DW_OP_consts;
9034     }
9035
9036   return new_loc_descr (op, i, 0);
9037 }
9038
9039 /* Return a location descriptor that designates a base+offset location.  */
9040
9041 static dw_loc_descr_ref
9042 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9043                  enum var_init_status initialized)
9044 {
9045   unsigned int regno;
9046   dw_loc_descr_ref result;
9047
9048   /* We only use "frame base" when we're sure we're talking about the
9049      post-prologue local stack frame.  We do this by *not* running
9050      register elimination until this point, and recognizing the special
9051      argument pointer and soft frame pointer rtx's.  */
9052   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9053     {
9054       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9055
9056       if (elim != reg)
9057         {
9058           if (GET_CODE (elim) == PLUS)
9059             {
9060               offset += INTVAL (XEXP (elim, 1));
9061               elim = XEXP (elim, 0);
9062             }
9063           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9064                       : stack_pointer_rtx));
9065           offset += frame_pointer_fb_offset;
9066
9067           return new_loc_descr (DW_OP_fbreg, offset, 0);
9068         }
9069     }
9070
9071   regno = dbx_reg_number (reg);
9072   if (regno <= 31)
9073     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9074   else
9075     result = new_loc_descr (DW_OP_bregx, regno, offset);
9076
9077   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9078     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9079
9080   return result;
9081 }
9082
9083 /* Return true if this RTL expression describes a base+offset calculation.  */
9084
9085 static inline int
9086 is_based_loc (const_rtx rtl)
9087 {
9088   return (GET_CODE (rtl) == PLUS
9089           && ((REG_P (XEXP (rtl, 0))
9090                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9091                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9092 }
9093
9094 /* Return a descriptor that describes the concatenation of N locations
9095    used to form the address of a memory location.  */
9096
9097 static dw_loc_descr_ref
9098 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9099                             enum var_init_status initialized)
9100 {
9101   unsigned int i;
9102   dw_loc_descr_ref cc_loc_result = NULL;
9103   unsigned int n = XVECLEN (concatn, 0);
9104
9105   for (i = 0; i < n; ++i)
9106     {
9107       dw_loc_descr_ref ref;
9108       rtx x = XVECEXP (concatn, 0, i);
9109
9110       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9111       if (ref == NULL)
9112         return NULL;
9113
9114       add_loc_descr (&cc_loc_result, ref);
9115       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9116     }
9117
9118   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9119     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9120
9121   return cc_loc_result;
9122 }
9123
9124 /* The following routine converts the RTL for a variable or parameter
9125    (resident in memory) into an equivalent Dwarf representation of a
9126    mechanism for getting the address of that same variable onto the top of a
9127    hypothetical "address evaluation" stack.
9128
9129    When creating memory location descriptors, we are effectively transforming
9130    the RTL for a memory-resident object into its Dwarf postfix expression
9131    equivalent.  This routine recursively descends an RTL tree, turning
9132    it into Dwarf postfix code as it goes.
9133
9134    MODE is the mode of the memory reference, needed to handle some
9135    autoincrement addressing modes.
9136
9137    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9138    location list for RTL.
9139
9140    Return 0 if we can't represent the location.  */
9141
9142 static dw_loc_descr_ref
9143 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9144                     enum var_init_status initialized)
9145 {
9146   dw_loc_descr_ref mem_loc_result = NULL;
9147   enum dwarf_location_atom op;
9148
9149   /* Note that for a dynamically sized array, the location we will generate a
9150      description of here will be the lowest numbered location which is
9151      actually within the array.  That's *not* necessarily the same as the
9152      zeroth element of the array.  */
9153
9154   rtl = targetm.delegitimize_address (rtl);
9155
9156   switch (GET_CODE (rtl))
9157     {
9158     case POST_INC:
9159     case POST_DEC:
9160     case POST_MODIFY:
9161       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9162          just fall into the SUBREG code.  */
9163
9164       /* ... fall through ...  */
9165
9166     case SUBREG:
9167       /* The case of a subreg may arise when we have a local (register)
9168          variable or a formal (register) parameter which doesn't quite fill
9169          up an entire register.  For now, just assume that it is
9170          legitimate to make the Dwarf info refer to the whole register which
9171          contains the given subreg.  */
9172       rtl = XEXP (rtl, 0);
9173
9174       /* ... fall through ...  */
9175
9176     case REG:
9177       /* Whenever a register number forms a part of the description of the
9178          method for calculating the (dynamic) address of a memory resident
9179          object, DWARF rules require the register number be referred to as
9180          a "base register".  This distinction is not based in any way upon
9181          what category of register the hardware believes the given register
9182          belongs to.  This is strictly DWARF terminology we're dealing with
9183          here. Note that in cases where the location of a memory-resident
9184          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9185          OP_CONST (0)) the actual DWARF location descriptor that we generate
9186          may just be OP_BASEREG (basereg).  This may look deceptively like
9187          the object in question was allocated to a register (rather than in
9188          memory) so DWARF consumers need to be aware of the subtle
9189          distinction between OP_REG and OP_BASEREG.  */
9190       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9191         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9192       break;
9193
9194     case MEM:
9195       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9196                                            VAR_INIT_STATUS_INITIALIZED);
9197       if (mem_loc_result != 0)
9198         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9199       break;
9200
9201     case LO_SUM:
9202          rtl = XEXP (rtl, 1);
9203
9204       /* ... fall through ...  */
9205
9206     case LABEL_REF:
9207       /* Some ports can transform a symbol ref into a label ref, because
9208          the symbol ref is too far away and has to be dumped into a constant
9209          pool.  */
9210     case CONST:
9211     case SYMBOL_REF:
9212       /* Alternatively, the symbol in the constant pool might be referenced
9213          by a different symbol.  */
9214       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9215         {
9216           bool marked;
9217           rtx tmp = get_pool_constant_mark (rtl, &marked);
9218
9219           if (GET_CODE (tmp) == SYMBOL_REF)
9220             {
9221               rtl = tmp;
9222               if (CONSTANT_POOL_ADDRESS_P (tmp))
9223                 get_pool_constant_mark (tmp, &marked);
9224               else
9225                 marked = true;
9226             }
9227
9228           /* If all references to this pool constant were optimized away,
9229              it was not output and thus we can't represent it.
9230              FIXME: might try to use DW_OP_const_value here, though
9231              DW_OP_piece complicates it.  */
9232           if (!marked)
9233             return 0;
9234         }
9235
9236       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9237       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9238       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9239       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9240       break;
9241
9242     case PRE_MODIFY:
9243       /* Extract the PLUS expression nested inside and fall into
9244          PLUS code below.  */
9245       rtl = XEXP (rtl, 1);
9246       goto plus;
9247
9248     case PRE_INC:
9249     case PRE_DEC:
9250       /* Turn these into a PLUS expression and fall into the PLUS code
9251          below.  */
9252       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9253                           GEN_INT (GET_CODE (rtl) == PRE_INC
9254                                    ? GET_MODE_UNIT_SIZE (mode)
9255                                    : -GET_MODE_UNIT_SIZE (mode)));
9256
9257       /* ... fall through ...  */
9258
9259     case PLUS:
9260     plus:
9261       if (is_based_loc (rtl))
9262         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9263                                           INTVAL (XEXP (rtl, 1)),
9264                                           VAR_INIT_STATUS_INITIALIZED);
9265       else
9266         {
9267           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9268                                                VAR_INIT_STATUS_INITIALIZED);
9269           if (mem_loc_result == 0)
9270             break;
9271
9272           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9273               && INTVAL (XEXP (rtl, 1)) >= 0)
9274             add_loc_descr (&mem_loc_result,
9275                            new_loc_descr (DW_OP_plus_uconst,
9276                                           INTVAL (XEXP (rtl, 1)), 0));
9277           else
9278             {
9279               add_loc_descr (&mem_loc_result,
9280                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9281                                                  VAR_INIT_STATUS_INITIALIZED));
9282               add_loc_descr (&mem_loc_result,
9283                              new_loc_descr (DW_OP_plus, 0, 0));
9284             }
9285         }
9286       break;
9287
9288     /* If a pseudo-reg is optimized away, it is possible for it to
9289        be replaced with a MEM containing a multiply or shift.  */
9290     case MULT:
9291       op = DW_OP_mul;
9292       goto do_binop;
9293
9294     case ASHIFT:
9295       op = DW_OP_shl;
9296       goto do_binop;
9297
9298     case ASHIFTRT:
9299       op = DW_OP_shra;
9300       goto do_binop;
9301
9302     case LSHIFTRT:
9303       op = DW_OP_shr;
9304       goto do_binop;
9305
9306     do_binop:
9307       {
9308         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9309                                                    VAR_INIT_STATUS_INITIALIZED);
9310         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9311                                                    VAR_INIT_STATUS_INITIALIZED);
9312
9313         if (op0 == 0 || op1 == 0)
9314           break;
9315
9316         mem_loc_result = op0;
9317         add_loc_descr (&mem_loc_result, op1);
9318         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9319         break;
9320       }
9321
9322     case CONST_INT:
9323       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9324       break;
9325
9326     case CONCATN:
9327       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9328                                                    VAR_INIT_STATUS_INITIALIZED);
9329       break;
9330
9331     default:
9332       gcc_unreachable ();
9333     }
9334
9335   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9336     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9337
9338   return mem_loc_result;
9339 }
9340
9341 /* Return a descriptor that describes the concatenation of two locations.
9342    This is typically a complex variable.  */
9343
9344 static dw_loc_descr_ref
9345 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9346 {
9347   dw_loc_descr_ref cc_loc_result = NULL;
9348   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9349   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9350
9351   if (x0_ref == 0 || x1_ref == 0)
9352     return 0;
9353
9354   cc_loc_result = x0_ref;
9355   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9356
9357   add_loc_descr (&cc_loc_result, x1_ref);
9358   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9359
9360   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9361     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9362
9363   return cc_loc_result;
9364 }
9365
9366 /* Return a descriptor that describes the concatenation of N
9367    locations.  */
9368
9369 static dw_loc_descr_ref
9370 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9371 {
9372   unsigned int i;
9373   dw_loc_descr_ref cc_loc_result = NULL;
9374   unsigned int n = XVECLEN (concatn, 0);
9375
9376   for (i = 0; i < n; ++i)
9377     {
9378       dw_loc_descr_ref ref;
9379       rtx x = XVECEXP (concatn, 0, i);
9380
9381       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9382       if (ref == NULL)
9383         return NULL;
9384
9385       add_loc_descr (&cc_loc_result, ref);
9386       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9387     }
9388
9389   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9390     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9391
9392   return cc_loc_result;
9393 }
9394
9395 /* Output a proper Dwarf location descriptor for a variable or parameter
9396    which is either allocated in a register or in a memory location.  For a
9397    register, we just generate an OP_REG and the register number.  For a
9398    memory location we provide a Dwarf postfix expression describing how to
9399    generate the (dynamic) address of the object onto the address stack.
9400
9401    If we don't know how to describe it, return 0.  */
9402
9403 static dw_loc_descr_ref
9404 loc_descriptor (rtx rtl, enum var_init_status initialized)
9405 {
9406   dw_loc_descr_ref loc_result = NULL;
9407
9408   switch (GET_CODE (rtl))
9409     {
9410     case SUBREG:
9411       /* The case of a subreg may arise when we have a local (register)
9412          variable or a formal (register) parameter which doesn't quite fill
9413          up an entire register.  For now, just assume that it is
9414          legitimate to make the Dwarf info refer to the whole register which
9415          contains the given subreg.  */
9416       rtl = SUBREG_REG (rtl);
9417
9418       /* ... fall through ...  */
9419
9420     case REG:
9421       loc_result = reg_loc_descriptor (rtl, initialized);
9422       break;
9423
9424     case MEM:
9425       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9426                                        initialized);
9427       break;
9428
9429     case CONCAT:
9430       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9431                                           initialized);
9432       break;
9433
9434     case CONCATN:
9435       loc_result = concatn_loc_descriptor (rtl, initialized);
9436       break;
9437
9438     case VAR_LOCATION:
9439       /* Single part.  */
9440       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9441         {
9442           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9443           break;
9444         }
9445
9446       rtl = XEXP (rtl, 1);
9447       /* FALLTHRU */
9448
9449     case PARALLEL:
9450       {
9451         rtvec par_elems = XVEC (rtl, 0);
9452         int num_elem = GET_NUM_ELEM (par_elems);
9453         enum machine_mode mode;
9454         int i;
9455
9456         /* Create the first one, so we have something to add to.  */
9457         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9458                                      initialized);
9459         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9460         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9461         for (i = 1; i < num_elem; i++)
9462           {
9463             dw_loc_descr_ref temp;
9464
9465             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9466                                    initialized);
9467             add_loc_descr (&loc_result, temp);
9468             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9469             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9470           }
9471       }
9472       break;
9473
9474     default:
9475       gcc_unreachable ();
9476     }
9477
9478   return loc_result;
9479 }
9480
9481 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9482    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9483    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9484    top-level invocation, and we require the address of LOC; is 0 if we require
9485    the value of LOC.  */
9486
9487 static dw_loc_descr_ref
9488 loc_descriptor_from_tree_1 (tree loc, int want_address)
9489 {
9490   dw_loc_descr_ref ret, ret1;
9491   int have_address = 0;
9492   enum dwarf_location_atom op;
9493
9494   /* ??? Most of the time we do not take proper care for sign/zero
9495      extending the values properly.  Hopefully this won't be a real
9496      problem...  */
9497
9498   switch (TREE_CODE (loc))
9499     {
9500     case ERROR_MARK:
9501       return 0;
9502
9503     case PLACEHOLDER_EXPR:
9504       /* This case involves extracting fields from an object to determine the
9505          position of other fields.  We don't try to encode this here.  The
9506          only user of this is Ada, which encodes the needed information using
9507          the names of types.  */
9508       return 0;
9509
9510     case CALL_EXPR:
9511       return 0;
9512
9513     case PREINCREMENT_EXPR:
9514     case PREDECREMENT_EXPR:
9515     case POSTINCREMENT_EXPR:
9516     case POSTDECREMENT_EXPR:
9517       /* There are no opcodes for these operations.  */
9518       return 0;
9519
9520     case ADDR_EXPR:
9521       /* If we already want an address, there's nothing we can do.  */
9522       if (want_address)
9523         return 0;
9524
9525       /* Otherwise, process the argument and look for the address.  */
9526       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9527
9528     case VAR_DECL:
9529       if (DECL_THREAD_LOCAL_P (loc))
9530         {
9531           rtx rtl;
9532           unsigned first_op;
9533           unsigned second_op;
9534
9535           if (targetm.have_tls)
9536             {
9537               /* If this is not defined, we have no way to emit the
9538                  data.  */
9539               if (!targetm.asm_out.output_dwarf_dtprel)
9540                 return 0;
9541
9542                /* The way DW_OP_GNU_push_tls_address is specified, we
9543                   can only look up addresses of objects in the current
9544                   module.  */
9545               if (DECL_EXTERNAL (loc))
9546                 return 0;
9547               first_op = INTERNAL_DW_OP_tls_addr;
9548               second_op = DW_OP_GNU_push_tls_address;
9549             }
9550           else
9551             {
9552               if (!targetm.emutls.debug_form_tls_address)
9553                 return 0;
9554               loc = emutls_decl (loc);
9555               first_op = DW_OP_addr;
9556               second_op = DW_OP_form_tls_address;
9557             }
9558
9559           rtl = rtl_for_decl_location (loc);
9560           if (rtl == NULL_RTX)
9561             return 0;
9562
9563           if (!MEM_P (rtl))
9564             return 0;
9565           rtl = XEXP (rtl, 0);
9566           if (! CONSTANT_P (rtl))
9567             return 0;
9568
9569           ret = new_loc_descr (first_op, 0, 0);
9570           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9571           ret->dw_loc_oprnd1.v.val_addr = rtl;
9572
9573           ret1 = new_loc_descr (second_op, 0, 0);
9574           add_loc_descr (&ret, ret1);
9575
9576           have_address = 1;
9577           break;
9578         }
9579       /* FALLTHRU */
9580
9581     case PARM_DECL:
9582       if (DECL_HAS_VALUE_EXPR_P (loc))
9583         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9584                                            want_address);
9585       /* FALLTHRU */
9586
9587     case RESULT_DECL:
9588     case FUNCTION_DECL:
9589       {
9590         rtx rtl = rtl_for_decl_location (loc);
9591
9592         if (rtl == NULL_RTX)
9593           return 0;
9594         else if (GET_CODE (rtl) == CONST_INT)
9595           {
9596             HOST_WIDE_INT val = INTVAL (rtl);
9597             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9598               val &= GET_MODE_MASK (DECL_MODE (loc));
9599             ret = int_loc_descriptor (val);
9600           }
9601         else if (GET_CODE (rtl) == CONST_STRING)
9602           return 0;
9603         else if (CONSTANT_P (rtl))
9604           {
9605             ret = new_loc_descr (DW_OP_addr, 0, 0);
9606             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9607             ret->dw_loc_oprnd1.v.val_addr = rtl;
9608           }
9609         else
9610           {
9611             enum machine_mode mode;
9612
9613             /* Certain constructs can only be represented at top-level.  */
9614             if (want_address == 2)
9615               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9616
9617             mode = GET_MODE (rtl);
9618             if (MEM_P (rtl))
9619               {
9620                 rtl = XEXP (rtl, 0);
9621                 have_address = 1;
9622               }
9623             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9624           }
9625       }
9626       break;
9627
9628     case INDIRECT_REF:
9629       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9630       have_address = 1;
9631       break;
9632
9633     case COMPOUND_EXPR:
9634       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9635
9636     CASE_CONVERT:
9637     case VIEW_CONVERT_EXPR:
9638     case SAVE_EXPR:
9639     case GIMPLE_MODIFY_STMT:
9640       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9641                                          want_address);
9642
9643     case COMPONENT_REF:
9644     case BIT_FIELD_REF:
9645     case ARRAY_REF:
9646     case ARRAY_RANGE_REF:
9647       {
9648         tree obj, offset;
9649         HOST_WIDE_INT bitsize, bitpos, bytepos;
9650         enum machine_mode mode;
9651         int volatilep;
9652         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9653
9654         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9655                                    &unsignedp, &volatilep, false);
9656
9657         if (obj == loc)
9658           return 0;
9659
9660         ret = loc_descriptor_from_tree_1 (obj, 1);
9661         if (ret == 0
9662             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9663           return 0;
9664
9665         if (offset != NULL_TREE)
9666           {
9667             /* Variable offset.  */
9668             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9669             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9670           }
9671
9672         bytepos = bitpos / BITS_PER_UNIT;
9673         if (bytepos > 0)
9674           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9675         else if (bytepos < 0)
9676           {
9677             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9678             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9679           }
9680
9681         have_address = 1;
9682         break;
9683       }
9684
9685     case INTEGER_CST:
9686       if (host_integerp (loc, 0))
9687         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9688       else
9689         return 0;
9690       break;
9691
9692     case CONSTRUCTOR:
9693       {
9694         /* Get an RTL for this, if something has been emitted.  */
9695         rtx rtl = lookup_constant_def (loc);
9696         enum machine_mode mode;
9697
9698         if (!rtl || !MEM_P (rtl))
9699           return 0;
9700         mode = GET_MODE (rtl);
9701         rtl = XEXP (rtl, 0);
9702         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9703         have_address = 1;
9704         break;
9705       }
9706
9707     case TRUTH_AND_EXPR:
9708     case TRUTH_ANDIF_EXPR:
9709     case BIT_AND_EXPR:
9710       op = DW_OP_and;
9711       goto do_binop;
9712
9713     case TRUTH_XOR_EXPR:
9714     case BIT_XOR_EXPR:
9715       op = DW_OP_xor;
9716       goto do_binop;
9717
9718     case TRUTH_OR_EXPR:
9719     case TRUTH_ORIF_EXPR:
9720     case BIT_IOR_EXPR:
9721       op = DW_OP_or;
9722       goto do_binop;
9723
9724     case FLOOR_DIV_EXPR:
9725     case CEIL_DIV_EXPR:
9726     case ROUND_DIV_EXPR:
9727     case TRUNC_DIV_EXPR:
9728       op = DW_OP_div;
9729       goto do_binop;
9730
9731     case MINUS_EXPR:
9732       op = DW_OP_minus;
9733       goto do_binop;
9734
9735     case FLOOR_MOD_EXPR:
9736     case CEIL_MOD_EXPR:
9737     case ROUND_MOD_EXPR:
9738     case TRUNC_MOD_EXPR:
9739       op = DW_OP_mod;
9740       goto do_binop;
9741
9742     case MULT_EXPR:
9743       op = DW_OP_mul;
9744       goto do_binop;
9745
9746     case LSHIFT_EXPR:
9747       op = DW_OP_shl;
9748       goto do_binop;
9749
9750     case RSHIFT_EXPR:
9751       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9752       goto do_binop;
9753
9754     case POINTER_PLUS_EXPR:
9755     case PLUS_EXPR:
9756       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9757           && host_integerp (TREE_OPERAND (loc, 1), 0))
9758         {
9759           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9760           if (ret == 0)
9761             return 0;
9762
9763           add_loc_descr (&ret,
9764                          new_loc_descr (DW_OP_plus_uconst,
9765                                         tree_low_cst (TREE_OPERAND (loc, 1),
9766                                                       0),
9767                                         0));
9768           break;
9769         }
9770
9771       op = DW_OP_plus;
9772       goto do_binop;
9773
9774     case LE_EXPR:
9775       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9776         return 0;
9777
9778       op = DW_OP_le;
9779       goto do_binop;
9780
9781     case GE_EXPR:
9782       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9783         return 0;
9784
9785       op = DW_OP_ge;
9786       goto do_binop;
9787
9788     case LT_EXPR:
9789       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9790         return 0;
9791
9792       op = DW_OP_lt;
9793       goto do_binop;
9794
9795     case GT_EXPR:
9796       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9797         return 0;
9798
9799       op = DW_OP_gt;
9800       goto do_binop;
9801
9802     case EQ_EXPR:
9803       op = DW_OP_eq;
9804       goto do_binop;
9805
9806     case NE_EXPR:
9807       op = DW_OP_ne;
9808       goto do_binop;
9809
9810     do_binop:
9811       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9812       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9813       if (ret == 0 || ret1 == 0)
9814         return 0;
9815
9816       add_loc_descr (&ret, ret1);
9817       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9818       break;
9819
9820     case TRUTH_NOT_EXPR:
9821     case BIT_NOT_EXPR:
9822       op = DW_OP_not;
9823       goto do_unop;
9824
9825     case ABS_EXPR:
9826       op = DW_OP_abs;
9827       goto do_unop;
9828
9829     case NEGATE_EXPR:
9830       op = DW_OP_neg;
9831       goto do_unop;
9832
9833     do_unop:
9834       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9835       if (ret == 0)
9836         return 0;
9837
9838       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9839       break;
9840
9841     case MIN_EXPR:
9842     case MAX_EXPR:
9843       {
9844         const enum tree_code code =
9845           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9846
9847         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9848                       build2 (code, integer_type_node,
9849                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9850                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9851       }
9852
9853       /* ... fall through ...  */
9854
9855     case COND_EXPR:
9856       {
9857         dw_loc_descr_ref lhs
9858           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9859         dw_loc_descr_ref rhs
9860           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9861         dw_loc_descr_ref bra_node, jump_node, tmp;
9862
9863         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9864         if (ret == 0 || lhs == 0 || rhs == 0)
9865           return 0;
9866
9867         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9868         add_loc_descr (&ret, bra_node);
9869
9870         add_loc_descr (&ret, rhs);
9871         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9872         add_loc_descr (&ret, jump_node);
9873
9874         add_loc_descr (&ret, lhs);
9875         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9876         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9877
9878         /* ??? Need a node to point the skip at.  Use a nop.  */
9879         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9880         add_loc_descr (&ret, tmp);
9881         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9882         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9883       }
9884       break;
9885
9886     case FIX_TRUNC_EXPR:
9887       return 0;
9888
9889     default:
9890       /* Leave front-end specific codes as simply unknown.  This comes
9891          up, for instance, with the C STMT_EXPR.  */
9892       if ((unsigned int) TREE_CODE (loc)
9893           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9894         return 0;
9895
9896 #ifdef ENABLE_CHECKING
9897       /* Otherwise this is a generic code; we should just lists all of
9898          these explicitly.  We forgot one.  */
9899       gcc_unreachable ();
9900 #else
9901       /* In a release build, we want to degrade gracefully: better to
9902          generate incomplete debugging information than to crash.  */
9903       return NULL;
9904 #endif
9905     }
9906
9907   /* Show if we can't fill the request for an address.  */
9908   if (want_address && !have_address)
9909     return 0;
9910
9911   /* If we've got an address and don't want one, dereference.  */
9912   if (!want_address && have_address && ret)
9913     {
9914       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9915
9916       if (size > DWARF2_ADDR_SIZE || size == -1)
9917         return 0;
9918       else if (size == DWARF2_ADDR_SIZE)
9919         op = DW_OP_deref;
9920       else
9921         op = DW_OP_deref_size;
9922
9923       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9924     }
9925
9926   return ret;
9927 }
9928
9929 static inline dw_loc_descr_ref
9930 loc_descriptor_from_tree (tree loc)
9931 {
9932   return loc_descriptor_from_tree_1 (loc, 2);
9933 }
9934
9935 /* Given a value, round it up to the lowest multiple of `boundary'
9936    which is not less than the value itself.  */
9937
9938 static inline HOST_WIDE_INT
9939 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9940 {
9941   return (((value + boundary - 1) / boundary) * boundary);
9942 }
9943
9944 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9945    pointer to the declared type for the relevant field variable, or return
9946    `integer_type_node' if the given node turns out to be an
9947    ERROR_MARK node.  */
9948
9949 static inline tree
9950 field_type (const_tree decl)
9951 {
9952   tree type;
9953
9954   if (TREE_CODE (decl) == ERROR_MARK)
9955     return integer_type_node;
9956
9957   type = DECL_BIT_FIELD_TYPE (decl);
9958   if (type == NULL_TREE)
9959     type = TREE_TYPE (decl);
9960
9961   return type;
9962 }
9963
9964 /* Given a pointer to a tree node, return the alignment in bits for
9965    it, or else return BITS_PER_WORD if the node actually turns out to
9966    be an ERROR_MARK node.  */
9967
9968 static inline unsigned
9969 simple_type_align_in_bits (const_tree type)
9970 {
9971   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9972 }
9973
9974 static inline unsigned
9975 simple_decl_align_in_bits (const_tree decl)
9976 {
9977   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9978 }
9979
9980 /* Return the result of rounding T up to ALIGN.  */
9981
9982 static inline HOST_WIDE_INT
9983 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9984 {
9985   /* We must be careful if T is negative because HOST_WIDE_INT can be
9986      either "above" or "below" unsigned int as per the C promotion
9987      rules, depending on the host, thus making the signedness of the
9988      direct multiplication and division unpredictable.  */
9989   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9990
9991   u += align - 1;
9992   u /= align;
9993   u *= align;
9994
9995   return (HOST_WIDE_INT) u;
9996 }
9997
9998 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9999    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10000    or return 0 if we are unable to determine what that offset is, either
10001    because the argument turns out to be a pointer to an ERROR_MARK node, or
10002    because the offset is actually variable.  (We can't handle the latter case
10003    just yet).  */
10004
10005 static HOST_WIDE_INT
10006 field_byte_offset (const_tree decl)
10007 {
10008   HOST_WIDE_INT object_offset_in_bits;
10009   HOST_WIDE_INT bitpos_int;
10010
10011   if (TREE_CODE (decl) == ERROR_MARK)
10012     return 0;
10013
10014   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10015
10016   /* We cannot yet cope with fields whose positions are variable, so
10017      for now, when we see such things, we simply return 0.  Someday, we may
10018      be able to handle such cases, but it will be damn difficult.  */
10019   if (! host_integerp (bit_position (decl), 0))
10020     return 0;
10021
10022   bitpos_int = int_bit_position (decl);
10023
10024 #ifdef PCC_BITFIELD_TYPE_MATTERS
10025   if (PCC_BITFIELD_TYPE_MATTERS)
10026     {
10027       tree type;
10028       tree field_size_tree;
10029       HOST_WIDE_INT deepest_bitpos;
10030       unsigned HOST_WIDE_INT field_size_in_bits;
10031       unsigned int type_align_in_bits;
10032       unsigned int decl_align_in_bits;
10033       unsigned HOST_WIDE_INT type_size_in_bits;
10034
10035       type = field_type (decl);
10036       field_size_tree = DECL_SIZE (decl);
10037
10038       /* The size could be unspecified if there was an error, or for
10039          a flexible array member.  */
10040       if (! field_size_tree)
10041         field_size_tree = bitsize_zero_node;
10042
10043       /* If we don't know the size of the field, pretend it's a full word.  */
10044       if (host_integerp (field_size_tree, 1))
10045         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10046       else
10047         field_size_in_bits = BITS_PER_WORD;
10048
10049       type_size_in_bits = simple_type_size_in_bits (type);
10050       type_align_in_bits = simple_type_align_in_bits (type);
10051       decl_align_in_bits = simple_decl_align_in_bits (decl);
10052
10053       /* The GCC front-end doesn't make any attempt to keep track of the
10054          starting bit offset (relative to the start of the containing
10055          structure type) of the hypothetical "containing object" for a
10056          bit-field.  Thus, when computing the byte offset value for the
10057          start of the "containing object" of a bit-field, we must deduce
10058          this information on our own. This can be rather tricky to do in
10059          some cases.  For example, handling the following structure type
10060          definition when compiling for an i386/i486 target (which only
10061          aligns long long's to 32-bit boundaries) can be very tricky:
10062
10063          struct S { int field1; long long field2:31; };
10064
10065          Fortunately, there is a simple rule-of-thumb which can be used
10066          in such cases.  When compiling for an i386/i486, GCC will
10067          allocate 8 bytes for the structure shown above.  It decides to
10068          do this based upon one simple rule for bit-field allocation.
10069          GCC allocates each "containing object" for each bit-field at
10070          the first (i.e. lowest addressed) legitimate alignment boundary
10071          (based upon the required minimum alignment for the declared
10072          type of the field) which it can possibly use, subject to the
10073          condition that there is still enough available space remaining
10074          in the containing object (when allocated at the selected point)
10075          to fully accommodate all of the bits of the bit-field itself.
10076
10077          This simple rule makes it obvious why GCC allocates 8 bytes for
10078          each object of the structure type shown above.  When looking
10079          for a place to allocate the "containing object" for `field2',
10080          the compiler simply tries to allocate a 64-bit "containing
10081          object" at each successive 32-bit boundary (starting at zero)
10082          until it finds a place to allocate that 64- bit field such that
10083          at least 31 contiguous (and previously unallocated) bits remain
10084          within that selected 64 bit field.  (As it turns out, for the
10085          example above, the compiler finds it is OK to allocate the
10086          "containing object" 64-bit field at bit-offset zero within the
10087          structure type.)
10088
10089          Here we attempt to work backwards from the limited set of facts
10090          we're given, and we try to deduce from those facts, where GCC
10091          must have believed that the containing object started (within
10092          the structure type). The value we deduce is then used (by the
10093          callers of this routine) to generate DW_AT_location and
10094          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10095          the case of DW_AT_location, regular fields as well).  */
10096
10097       /* Figure out the bit-distance from the start of the structure to
10098          the "deepest" bit of the bit-field.  */
10099       deepest_bitpos = bitpos_int + field_size_in_bits;
10100
10101       /* This is the tricky part.  Use some fancy footwork to deduce
10102          where the lowest addressed bit of the containing object must
10103          be.  */
10104       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10105
10106       /* Round up to type_align by default.  This works best for
10107          bitfields.  */
10108       object_offset_in_bits
10109         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10110
10111       if (object_offset_in_bits > bitpos_int)
10112         {
10113           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10114
10115           /* Round up to decl_align instead.  */
10116           object_offset_in_bits
10117             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10118         }
10119     }
10120   else
10121 #endif
10122     object_offset_in_bits = bitpos_int;
10123
10124   return object_offset_in_bits / BITS_PER_UNIT;
10125 }
10126 \f
10127 /* The following routines define various Dwarf attributes and any data
10128    associated with them.  */
10129
10130 /* Add a location description attribute value to a DIE.
10131
10132    This emits location attributes suitable for whole variables and
10133    whole parameters.  Note that the location attributes for struct fields are
10134    generated by the routine `data_member_location_attribute' below.  */
10135
10136 static inline void
10137 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10138                              dw_loc_descr_ref descr)
10139 {
10140   if (descr != 0)
10141     add_AT_loc (die, attr_kind, descr);
10142 }
10143
10144 /* Attach the specialized form of location attribute used for data members of
10145    struct and union types.  In the special case of a FIELD_DECL node which
10146    represents a bit-field, the "offset" part of this special location
10147    descriptor must indicate the distance in bytes from the lowest-addressed
10148    byte of the containing struct or union type to the lowest-addressed byte of
10149    the "containing object" for the bit-field.  (See the `field_byte_offset'
10150    function above).
10151
10152    For any given bit-field, the "containing object" is a hypothetical object
10153    (of some integral or enum type) within which the given bit-field lives.  The
10154    type of this hypothetical "containing object" is always the same as the
10155    declared type of the individual bit-field itself (for GCC anyway... the
10156    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10157    bytes) of the hypothetical "containing object" which will be given in the
10158    DW_AT_byte_size attribute for this bit-field.  (See the
10159    `byte_size_attribute' function below.)  It is also used when calculating the
10160    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10161    function below.)  */
10162
10163 static void
10164 add_data_member_location_attribute (dw_die_ref die, tree decl)
10165 {
10166   HOST_WIDE_INT offset;
10167   dw_loc_descr_ref loc_descr = 0;
10168
10169   if (TREE_CODE (decl) == TREE_BINFO)
10170     {
10171       /* We're working on the TAG_inheritance for a base class.  */
10172       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10173         {
10174           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10175              aren't at a fixed offset from all (sub)objects of the same
10176              type.  We need to extract the appropriate offset from our
10177              vtable.  The following dwarf expression means
10178
10179                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10180
10181              This is specific to the V3 ABI, of course.  */
10182
10183           dw_loc_descr_ref tmp;
10184
10185           /* Make a copy of the object address.  */
10186           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10187           add_loc_descr (&loc_descr, tmp);
10188
10189           /* Extract the vtable address.  */
10190           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10191           add_loc_descr (&loc_descr, tmp);
10192
10193           /* Calculate the address of the offset.  */
10194           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10195           gcc_assert (offset < 0);
10196
10197           tmp = int_loc_descriptor (-offset);
10198           add_loc_descr (&loc_descr, tmp);
10199           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10200           add_loc_descr (&loc_descr, tmp);
10201
10202           /* Extract the offset.  */
10203           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10204           add_loc_descr (&loc_descr, tmp);
10205
10206           /* Add it to the object address.  */
10207           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10208           add_loc_descr (&loc_descr, tmp);
10209         }
10210       else
10211         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10212     }
10213   else
10214     offset = field_byte_offset (decl);
10215
10216   if (! loc_descr)
10217     {
10218       enum dwarf_location_atom op;
10219
10220       /* The DWARF2 standard says that we should assume that the structure
10221          address is already on the stack, so we can specify a structure field
10222          address by using DW_OP_plus_uconst.  */
10223
10224 #ifdef MIPS_DEBUGGING_INFO
10225       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10226          operator correctly.  It works only if we leave the offset on the
10227          stack.  */
10228       op = DW_OP_constu;
10229 #else
10230       op = DW_OP_plus_uconst;
10231 #endif
10232
10233       loc_descr = new_loc_descr (op, offset, 0);
10234     }
10235
10236   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10237 }
10238
10239 /* Writes integer values to dw_vec_const array.  */
10240
10241 static void
10242 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10243 {
10244   while (size != 0)
10245     {
10246       *dest++ = val & 0xff;
10247       val >>= 8;
10248       --size;
10249     }
10250 }
10251
10252 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10253
10254 static HOST_WIDE_INT
10255 extract_int (const unsigned char *src, unsigned int size)
10256 {
10257   HOST_WIDE_INT val = 0;
10258
10259   src += size;
10260   while (size != 0)
10261     {
10262       val <<= 8;
10263       val |= *--src & 0xff;
10264       --size;
10265     }
10266   return val;
10267 }
10268
10269 /* Writes floating point values to dw_vec_const array.  */
10270
10271 static void
10272 insert_float (const_rtx rtl, unsigned char *array)
10273 {
10274   REAL_VALUE_TYPE rv;
10275   long val[4];
10276   int i;
10277
10278   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10279   real_to_target (val, &rv, GET_MODE (rtl));
10280
10281   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10282   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10283     {
10284       insert_int (val[i], 4, array);
10285       array += 4;
10286     }
10287 }
10288
10289 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10290    does not have a "location" either in memory or in a register.  These
10291    things can arise in GNU C when a constant is passed as an actual parameter
10292    to an inlined function.  They can also arise in C++ where declared
10293    constants do not necessarily get memory "homes".  */
10294
10295 static void
10296 add_const_value_attribute (dw_die_ref die, rtx rtl)
10297 {
10298   switch (GET_CODE (rtl))
10299     {
10300     case CONST_INT:
10301       {
10302         HOST_WIDE_INT val = INTVAL (rtl);
10303
10304         if (val < 0)
10305           add_AT_int (die, DW_AT_const_value, val);
10306         else
10307           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10308       }
10309       break;
10310
10311     case CONST_DOUBLE:
10312       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10313          floating-point constant.  A CONST_DOUBLE is used whenever the
10314          constant requires more than one word in order to be adequately
10315          represented.  We output CONST_DOUBLEs as blocks.  */
10316       {
10317         enum machine_mode mode = GET_MODE (rtl);
10318
10319         if (SCALAR_FLOAT_MODE_P (mode))
10320           {
10321             unsigned int length = GET_MODE_SIZE (mode);
10322             unsigned char *array = GGC_NEWVEC (unsigned char, length);
10323
10324             insert_float (rtl, array);
10325             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10326           }
10327         else
10328           {
10329             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10330             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10331
10332             add_AT_long_long (die, DW_AT_const_value,
10333                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10334           }
10335       }
10336       break;
10337
10338     case CONST_VECTOR:
10339       {
10340         enum machine_mode mode = GET_MODE (rtl);
10341         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10342         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10343         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
10344         unsigned int i;
10345         unsigned char *p;
10346
10347         switch (GET_MODE_CLASS (mode))
10348           {
10349           case MODE_VECTOR_INT:
10350             for (i = 0, p = array; i < length; i++, p += elt_size)
10351               {
10352                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10353                 HOST_WIDE_INT lo, hi;
10354
10355                 switch (GET_CODE (elt))
10356                   {
10357                   case CONST_INT:
10358                     lo = INTVAL (elt);
10359                     hi = -(lo < 0);
10360                     break;
10361
10362                   case CONST_DOUBLE:
10363                     lo = CONST_DOUBLE_LOW (elt);
10364                     hi = CONST_DOUBLE_HIGH (elt);
10365                     break;
10366
10367                   default:
10368                     gcc_unreachable ();
10369                   }
10370
10371                 if (elt_size <= sizeof (HOST_WIDE_INT))
10372                   insert_int (lo, elt_size, p);
10373                 else
10374                   {
10375                     unsigned char *p0 = p;
10376                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10377
10378                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10379                     if (WORDS_BIG_ENDIAN)
10380                       {
10381                         p0 = p1;
10382                         p1 = p;
10383                       }
10384                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10385                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10386                   }
10387               }
10388             break;
10389
10390           case MODE_VECTOR_FLOAT:
10391             for (i = 0, p = array; i < length; i++, p += elt_size)
10392               {
10393                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10394                 insert_float (elt, p);
10395               }
10396             break;
10397
10398           default:
10399             gcc_unreachable ();
10400           }
10401
10402         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10403       }
10404       break;
10405
10406     case CONST_STRING:
10407       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10408       break;
10409
10410     case SYMBOL_REF:
10411     case LABEL_REF:
10412     case CONST:
10413       add_AT_addr (die, DW_AT_const_value, rtl);
10414       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10415       break;
10416
10417     case PLUS:
10418       /* In cases where an inlined instance of an inline function is passed
10419          the address of an `auto' variable (which is local to the caller) we
10420          can get a situation where the DECL_RTL of the artificial local
10421          variable (for the inlining) which acts as a stand-in for the
10422          corresponding formal parameter (of the inline function) will look
10423          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10424          exactly a compile-time constant expression, but it isn't the address
10425          of the (artificial) local variable either.  Rather, it represents the
10426          *value* which the artificial local variable always has during its
10427          lifetime.  We currently have no way to represent such quasi-constant
10428          values in Dwarf, so for now we just punt and generate nothing.  */
10429       break;
10430
10431     default:
10432       /* No other kinds of rtx should be possible here.  */
10433       gcc_unreachable ();
10434     }
10435
10436 }
10437
10438 /* Determine whether the evaluation of EXPR references any variables
10439    or functions which aren't otherwise used (and therefore may not be
10440    output).  */
10441 static tree
10442 reference_to_unused (tree * tp, int * walk_subtrees,
10443                      void * data ATTRIBUTE_UNUSED)
10444 {
10445   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10446     *walk_subtrees = 0;
10447
10448   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10449       && ! TREE_ASM_WRITTEN (*tp))
10450     return *tp;
10451   else if (!flag_unit_at_a_time)
10452     return NULL_TREE;
10453   /* ???  The C++ FE emits debug information for using decls, so
10454      putting gcc_unreachable here falls over.  See PR31899.  For now
10455      be conservative.  */
10456   else if (!cgraph_global_info_ready
10457            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10458     return *tp;
10459   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10460     {
10461       struct varpool_node *node = varpool_node (*tp);
10462       if (!node->needed)
10463         return *tp;
10464     }
10465   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10466            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10467     {
10468       struct cgraph_node *node = cgraph_node (*tp);
10469       if (!node->output)
10470         return *tp;
10471     }
10472   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10473     return *tp;
10474
10475   return NULL_TREE;
10476 }
10477
10478 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10479    for use in a later add_const_value_attribute call.  */
10480
10481 static rtx
10482 rtl_for_decl_init (tree init, tree type)
10483 {
10484   rtx rtl = NULL_RTX;
10485
10486   /* If a variable is initialized with a string constant without embedded
10487      zeros, build CONST_STRING.  */
10488   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10489     {
10490       tree enttype = TREE_TYPE (type);
10491       tree domain = TYPE_DOMAIN (type);
10492       enum machine_mode mode = TYPE_MODE (enttype);
10493
10494       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10495           && domain
10496           && integer_zerop (TYPE_MIN_VALUE (domain))
10497           && compare_tree_int (TYPE_MAX_VALUE (domain),
10498                                TREE_STRING_LENGTH (init) - 1) == 0
10499           && ((size_t) TREE_STRING_LENGTH (init)
10500               == strlen (TREE_STRING_POINTER (init)) + 1))
10501         rtl = gen_rtx_CONST_STRING (VOIDmode,
10502                                     ggc_strdup (TREE_STRING_POINTER (init)));
10503     }
10504   /* Other aggregates, and complex values, could be represented using
10505      CONCAT: FIXME!  */
10506   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10507     ;
10508   /* Vectors only work if their mode is supported by the target.
10509      FIXME: generic vectors ought to work too.  */
10510   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10511     ;
10512   /* If the initializer is something that we know will expand into an
10513      immediate RTL constant, expand it now.  We must be careful not to
10514      reference variables which won't be output.  */
10515   else if (initializer_constant_valid_p (init, type)
10516            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10517     {
10518       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10519          possible.  */
10520       if (TREE_CODE (type) == VECTOR_TYPE)
10521         switch (TREE_CODE (init))
10522           {
10523           case VECTOR_CST:
10524             break;
10525           case CONSTRUCTOR:
10526             if (TREE_CONSTANT (init))
10527               {
10528                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10529                 bool constant_p = true;
10530                 tree value;
10531                 unsigned HOST_WIDE_INT ix;
10532
10533                 /* Even when ctor is constant, it might contain non-*_CST
10534                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10535                    belong into VECTOR_CST nodes.  */
10536                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10537                   if (!CONSTANT_CLASS_P (value))
10538                     {
10539                       constant_p = false;
10540                       break;
10541                     }
10542
10543                 if (constant_p)
10544                   {
10545                     init = build_vector_from_ctor (type, elts);
10546                     break;
10547                   }
10548               }
10549             /* FALLTHRU */
10550
10551           default:
10552             return NULL;
10553           }
10554
10555       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10556
10557       /* If expand_expr returns a MEM, it wasn't immediate.  */
10558       gcc_assert (!rtl || !MEM_P (rtl));
10559     }
10560
10561   return rtl;
10562 }
10563
10564 /* Generate RTL for the variable DECL to represent its location.  */
10565
10566 static rtx
10567 rtl_for_decl_location (tree decl)
10568 {
10569   rtx rtl;
10570
10571   /* Here we have to decide where we are going to say the parameter "lives"
10572      (as far as the debugger is concerned).  We only have a couple of
10573      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10574
10575      DECL_RTL normally indicates where the parameter lives during most of the
10576      activation of the function.  If optimization is enabled however, this
10577      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10578      that the parameter doesn't really live anywhere (as far as the code
10579      generation parts of GCC are concerned) during most of the function's
10580      activation.  That will happen (for example) if the parameter is never
10581      referenced within the function.
10582
10583      We could just generate a location descriptor here for all non-NULL
10584      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10585      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10586      where DECL_RTL is NULL or is a pseudo-reg.
10587
10588      Note however that we can only get away with using DECL_INCOMING_RTL as
10589      a backup substitute for DECL_RTL in certain limited cases.  In cases
10590      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10591      we can be sure that the parameter was passed using the same type as it is
10592      declared to have within the function, and that its DECL_INCOMING_RTL
10593      points us to a place where a value of that type is passed.
10594
10595      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10596      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10597      because in these cases DECL_INCOMING_RTL points us to a value of some
10598      type which is *different* from the type of the parameter itself.  Thus,
10599      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10600      such cases, the debugger would end up (for example) trying to fetch a
10601      `float' from a place which actually contains the first part of a
10602      `double'.  That would lead to really incorrect and confusing
10603      output at debug-time.
10604
10605      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10606      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10607      are a couple of exceptions however.  On little-endian machines we can
10608      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10609      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10610      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10611      when (on a little-endian machine) a non-prototyped function has a
10612      parameter declared to be of type `short' or `char'.  In such cases,
10613      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10614      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10615      passed `int' value.  If the debugger then uses that address to fetch
10616      a `short' or a `char' (on a little-endian machine) the result will be
10617      the correct data, so we allow for such exceptional cases below.
10618
10619      Note that our goal here is to describe the place where the given formal
10620      parameter lives during most of the function's activation (i.e. between the
10621      end of the prologue and the start of the epilogue).  We'll do that as best
10622      as we can. Note however that if the given formal parameter is modified
10623      sometime during the execution of the function, then a stack backtrace (at
10624      debug-time) will show the function as having been called with the *new*
10625      value rather than the value which was originally passed in.  This happens
10626      rarely enough that it is not a major problem, but it *is* a problem, and
10627      I'd like to fix it.
10628
10629      A future version of dwarf2out.c may generate two additional attributes for
10630      any given DW_TAG_formal_parameter DIE which will describe the "passed
10631      type" and the "passed location" for the given formal parameter in addition
10632      to the attributes we now generate to indicate the "declared type" and the
10633      "active location" for each parameter.  This additional set of attributes
10634      could be used by debuggers for stack backtraces. Separately, note that
10635      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10636      This happens (for example) for inlined-instances of inline function formal
10637      parameters which are never referenced.  This really shouldn't be
10638      happening.  All PARM_DECL nodes should get valid non-NULL
10639      DECL_INCOMING_RTL values.  FIXME.  */
10640
10641   /* Use DECL_RTL as the "location" unless we find something better.  */
10642   rtl = DECL_RTL_IF_SET (decl);
10643
10644   /* When generating abstract instances, ignore everything except
10645      constants, symbols living in memory, and symbols living in
10646      fixed registers.  */
10647   if (! reload_completed)
10648     {
10649       if (rtl
10650           && (CONSTANT_P (rtl)
10651               || (MEM_P (rtl)
10652                   && CONSTANT_P (XEXP (rtl, 0)))
10653               || (REG_P (rtl)
10654                   && TREE_CODE (decl) == VAR_DECL
10655                   && TREE_STATIC (decl))))
10656         {
10657           rtl = targetm.delegitimize_address (rtl);
10658           return rtl;
10659         }
10660       rtl = NULL_RTX;
10661     }
10662   else if (TREE_CODE (decl) == PARM_DECL)
10663     {
10664       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10665         {
10666           tree declared_type = TREE_TYPE (decl);
10667           tree passed_type = DECL_ARG_TYPE (decl);
10668           enum machine_mode dmode = TYPE_MODE (declared_type);
10669           enum machine_mode pmode = TYPE_MODE (passed_type);
10670
10671           /* This decl represents a formal parameter which was optimized out.
10672              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10673              all cases where (rtl == NULL_RTX) just below.  */
10674           if (dmode == pmode)
10675             rtl = DECL_INCOMING_RTL (decl);
10676           else if (SCALAR_INT_MODE_P (dmode)
10677                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10678                    && DECL_INCOMING_RTL (decl))
10679             {
10680               rtx inc = DECL_INCOMING_RTL (decl);
10681               if (REG_P (inc))
10682                 rtl = inc;
10683               else if (MEM_P (inc))
10684                 {
10685                   if (BYTES_BIG_ENDIAN)
10686                     rtl = adjust_address_nv (inc, dmode,
10687                                              GET_MODE_SIZE (pmode)
10688                                              - GET_MODE_SIZE (dmode));
10689                   else
10690                     rtl = inc;
10691                 }
10692             }
10693         }
10694
10695       /* If the parm was passed in registers, but lives on the stack, then
10696          make a big endian correction if the mode of the type of the
10697          parameter is not the same as the mode of the rtl.  */
10698       /* ??? This is the same series of checks that are made in dbxout.c before
10699          we reach the big endian correction code there.  It isn't clear if all
10700          of these checks are necessary here, but keeping them all is the safe
10701          thing to do.  */
10702       else if (MEM_P (rtl)
10703                && XEXP (rtl, 0) != const0_rtx
10704                && ! CONSTANT_P (XEXP (rtl, 0))
10705                /* Not passed in memory.  */
10706                && !MEM_P (DECL_INCOMING_RTL (decl))
10707                /* Not passed by invisible reference.  */
10708                && (!REG_P (XEXP (rtl, 0))
10709                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10710                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10711 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10712                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10713 #endif
10714                      )
10715                /* Big endian correction check.  */
10716                && BYTES_BIG_ENDIAN
10717                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10718                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10719                    < UNITS_PER_WORD))
10720         {
10721           int offset = (UNITS_PER_WORD
10722                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10723
10724           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10725                              plus_constant (XEXP (rtl, 0), offset));
10726         }
10727     }
10728   else if (TREE_CODE (decl) == VAR_DECL
10729            && rtl
10730            && MEM_P (rtl)
10731            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10732            && BYTES_BIG_ENDIAN)
10733     {
10734       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10735       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10736
10737       /* If a variable is declared "register" yet is smaller than
10738          a register, then if we store the variable to memory, it
10739          looks like we're storing a register-sized value, when in
10740          fact we are not.  We need to adjust the offset of the
10741          storage location to reflect the actual value's bytes,
10742          else gdb will not be able to display it.  */
10743       if (rsize > dsize)
10744         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10745                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10746     }
10747
10748   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10749      and will have been substituted directly into all expressions that use it.
10750      C does not have such a concept, but C++ and other languages do.  */
10751   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10752     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10753
10754   if (rtl)
10755     rtl = targetm.delegitimize_address (rtl);
10756
10757   /* If we don't look past the constant pool, we risk emitting a
10758      reference to a constant pool entry that isn't referenced from
10759      code, and thus is not emitted.  */
10760   if (rtl)
10761     rtl = avoid_constant_pool_reference (rtl);
10762
10763   return rtl;
10764 }
10765
10766 /* We need to figure out what section we should use as the base for the
10767    address ranges where a given location is valid.
10768    1. If this particular DECL has a section associated with it, use that.
10769    2. If this function has a section associated with it, use that.
10770    3. Otherwise, use the text section.
10771    XXX: If you split a variable across multiple sections, we won't notice.  */
10772
10773 static const char *
10774 secname_for_decl (const_tree decl)
10775 {
10776   const char *secname;
10777
10778   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10779     {
10780       tree sectree = DECL_SECTION_NAME (decl);
10781       secname = TREE_STRING_POINTER (sectree);
10782     }
10783   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10784     {
10785       tree sectree = DECL_SECTION_NAME (current_function_decl);
10786       secname = TREE_STRING_POINTER (sectree);
10787     }
10788   else if (cfun && in_cold_section_p)
10789     secname = crtl->subsections.cold_section_label;
10790   else
10791     secname = text_section_label;
10792
10793   return secname;
10794 }
10795
10796 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
10797    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10798    value is the offset into the common block for the symbol.  */
10799
10800 static tree
10801 fortran_common (tree decl, HOST_WIDE_INT *value)
10802 {
10803   tree val_expr, cvar;
10804   enum machine_mode mode;
10805   HOST_WIDE_INT bitsize, bitpos;
10806   tree offset;
10807   int volatilep = 0, unsignedp = 0;
10808
10809   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10810      it does not have a value (the offset into the common area), or if it
10811      is thread local (as opposed to global) then it isn't common, and shouldn't
10812      be handled as such.  */
10813   if (TREE_CODE (decl) != VAR_DECL
10814       || !TREE_PUBLIC (decl)
10815       || !TREE_STATIC (decl)
10816       || !DECL_HAS_VALUE_EXPR_P (decl)
10817       || !is_fortran ())
10818     return NULL_TREE;
10819
10820   val_expr = DECL_VALUE_EXPR (decl);
10821   if (TREE_CODE (val_expr) != COMPONENT_REF)
10822     return NULL_TREE;
10823
10824   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
10825                               &mode, &unsignedp, &volatilep, true);
10826
10827   if (cvar == NULL_TREE
10828       || TREE_CODE (cvar) != VAR_DECL
10829       || DECL_ARTIFICIAL (cvar)
10830       || !TREE_PUBLIC (cvar))
10831     return NULL_TREE;
10832
10833   *value = 0;
10834   if (offset != NULL)
10835     {
10836       if (!host_integerp (offset, 0))
10837         return NULL_TREE;
10838       *value = tree_low_cst (offset, 0);
10839     }
10840   if (bitpos != 0)
10841     *value += bitpos / BITS_PER_UNIT;
10842
10843   return cvar;
10844 }
10845
10846
10847 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10848    data attribute for a variable or a parameter.  We generate the
10849    DW_AT_const_value attribute only in those cases where the given variable
10850    or parameter does not have a true "location" either in memory or in a
10851    register.  This can happen (for example) when a constant is passed as an
10852    actual argument in a call to an inline function.  (It's possible that
10853    these things can crop up in other ways also.)  Note that one type of
10854    constant value which can be passed into an inlined function is a constant
10855    pointer.  This can happen for example if an actual argument in an inlined
10856    function call evaluates to a compile-time constant address.  */
10857
10858 static void
10859 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10860                                        enum dwarf_attribute attr)
10861 {
10862   rtx rtl;
10863   dw_loc_descr_ref descr;
10864   var_loc_list *loc_list;
10865   struct var_loc_node *node;
10866   if (TREE_CODE (decl) == ERROR_MARK)
10867     return;
10868
10869   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10870               || TREE_CODE (decl) == RESULT_DECL);
10871
10872   /* See if we possibly have multiple locations for this variable.  */
10873   loc_list = lookup_decl_loc (decl);
10874
10875   /* If it truly has multiple locations, the first and last node will
10876      differ.  */
10877   if (loc_list && loc_list->first != loc_list->last)
10878     {
10879       const char *endname, *secname;
10880       dw_loc_list_ref list;
10881       rtx varloc;
10882       enum var_init_status initialized;
10883
10884       /* Now that we know what section we are using for a base,
10885          actually construct the list of locations.
10886          The first location information is what is passed to the
10887          function that creates the location list, and the remaining
10888          locations just get added on to that list.
10889          Note that we only know the start address for a location
10890          (IE location changes), so to build the range, we use
10891          the range [current location start, next location start].
10892          This means we have to special case the last node, and generate
10893          a range of [last location start, end of function label].  */
10894
10895       node = loc_list->first;
10896       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10897       secname = secname_for_decl (decl);
10898
10899       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10900         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10901       else
10902         initialized = VAR_INIT_STATUS_INITIALIZED;
10903
10904       list = new_loc_list (loc_descriptor (varloc, initialized),
10905                            node->label, node->next->label, secname, 1);
10906       node = node->next;
10907
10908       for (; node->next; node = node->next)
10909         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10910           {
10911             /* The variable has a location between NODE->LABEL and
10912                NODE->NEXT->LABEL.  */
10913             enum var_init_status initialized =
10914               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10915             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10916             add_loc_descr_to_loc_list (&list,
10917                                        loc_descriptor (varloc, initialized),
10918                                        node->label, node->next->label, secname);
10919           }
10920
10921       /* If the variable has a location at the last label
10922          it keeps its location until the end of function.  */
10923       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10924         {
10925           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10926           enum var_init_status initialized =
10927             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10928
10929           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10930           if (!current_function_decl)
10931             endname = text_end_label;
10932           else
10933             {
10934               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10935                                            current_function_funcdef_no);
10936               endname = ggc_strdup (label_id);
10937             }
10938           add_loc_descr_to_loc_list (&list,
10939                                      loc_descriptor (varloc, initialized),
10940                                      node->label, endname, secname);
10941         }
10942
10943       /* Finally, add the location list to the DIE, and we are done.  */
10944       add_AT_loc_list (die, attr, list);
10945       return;
10946     }
10947
10948   /* Try to get some constant RTL for this decl, and use that as the value of
10949      the location.  */
10950
10951   rtl = rtl_for_decl_location (decl);
10952   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10953     {
10954       add_const_value_attribute (die, rtl);
10955       return;
10956     }
10957
10958   /* If we have tried to generate the location otherwise, and it
10959      didn't work out (we wouldn't be here if we did), and we have a one entry
10960      location list, try generating a location from that.  */
10961   if (loc_list && loc_list->first)
10962     {
10963       enum var_init_status status;
10964       node = loc_list->first;
10965       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10966       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10967       if (descr)
10968         {
10969           add_AT_location_description (die, attr, descr);
10970           return;
10971         }
10972     }
10973
10974   /* We couldn't get any rtl, so try directly generating the location
10975      description from the tree.  */
10976   descr = loc_descriptor_from_tree (decl);
10977   if (descr)
10978     {
10979       add_AT_location_description (die, attr, descr);
10980       return;
10981     }
10982   /* None of that worked, so it must not really have a location;
10983      try adding a constant value attribute from the DECL_INITIAL.  */
10984   tree_add_const_value_attribute (die, decl);
10985 }
10986
10987 /* If we don't have a copy of this variable in memory for some reason (such
10988    as a C++ member constant that doesn't have an out-of-line definition),
10989    we should tell the debugger about the constant value.  */
10990
10991 static void
10992 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10993 {
10994   tree init = DECL_INITIAL (decl);
10995   tree type = TREE_TYPE (decl);
10996   rtx rtl;
10997
10998   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10999     /* OK */;
11000   else
11001     return;
11002
11003   rtl = rtl_for_decl_init (init, type);
11004   if (rtl)
11005     add_const_value_attribute (var_die, rtl);
11006 }
11007
11008 /* Convert the CFI instructions for the current function into a
11009    location list.  This is used for DW_AT_frame_base when we targeting
11010    a dwarf2 consumer that does not support the dwarf3
11011    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11012    expressions.  */
11013
11014 static dw_loc_list_ref
11015 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11016 {
11017   dw_fde_ref fde;
11018   dw_loc_list_ref list, *list_tail;
11019   dw_cfi_ref cfi;
11020   dw_cfa_location last_cfa, next_cfa;
11021   const char *start_label, *last_label, *section;
11022
11023   fde = current_fde ();
11024   gcc_assert (fde != NULL);
11025
11026   section = secname_for_decl (current_function_decl);
11027   list_tail = &list;
11028   list = NULL;
11029
11030   next_cfa.reg = INVALID_REGNUM;
11031   next_cfa.offset = 0;
11032   next_cfa.indirect = 0;
11033   next_cfa.base_offset = 0;
11034
11035   start_label = fde->dw_fde_begin;
11036
11037   /* ??? Bald assumption that the CIE opcode list does not contain
11038      advance opcodes.  */
11039   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11040     lookup_cfa_1 (cfi, &next_cfa);
11041
11042   last_cfa = next_cfa;
11043   last_label = start_label;
11044
11045   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11046     switch (cfi->dw_cfi_opc)
11047       {
11048       case DW_CFA_set_loc:
11049       case DW_CFA_advance_loc1:
11050       case DW_CFA_advance_loc2:
11051       case DW_CFA_advance_loc4:
11052         if (!cfa_equal_p (&last_cfa, &next_cfa))
11053           {
11054             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11055                                        start_label, last_label, section,
11056                                        list == NULL);
11057
11058             list_tail = &(*list_tail)->dw_loc_next;
11059             last_cfa = next_cfa;
11060             start_label = last_label;
11061           }
11062         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11063         break;
11064
11065       case DW_CFA_advance_loc:
11066         /* The encoding is complex enough that we should never emit this.  */
11067       case DW_CFA_remember_state:
11068       case DW_CFA_restore_state:
11069         /* We don't handle these two in this function.  It would be possible
11070            if it were to be required.  */
11071         gcc_unreachable ();
11072
11073       default:
11074         lookup_cfa_1 (cfi, &next_cfa);
11075         break;
11076       }
11077
11078   if (!cfa_equal_p (&last_cfa, &next_cfa))
11079     {
11080       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11081                                  start_label, last_label, section,
11082                                  list == NULL);
11083       list_tail = &(*list_tail)->dw_loc_next;
11084       start_label = last_label;
11085     }
11086   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11087                              start_label, fde->dw_fde_end, section,
11088                              list == NULL);
11089
11090   return list;
11091 }
11092
11093 /* Compute a displacement from the "steady-state frame pointer" to the
11094    frame base (often the same as the CFA), and store it in
11095    frame_pointer_fb_offset.  OFFSET is added to the displacement
11096    before the latter is negated.  */
11097
11098 static void
11099 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11100 {
11101   rtx reg, elim;
11102
11103 #ifdef FRAME_POINTER_CFA_OFFSET
11104   reg = frame_pointer_rtx;
11105   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11106 #else
11107   reg = arg_pointer_rtx;
11108   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11109 #endif
11110
11111   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11112   if (GET_CODE (elim) == PLUS)
11113     {
11114       offset += INTVAL (XEXP (elim, 1));
11115       elim = XEXP (elim, 0);
11116     }
11117   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11118                        : stack_pointer_rtx));
11119
11120   frame_pointer_fb_offset = -offset;
11121 }
11122
11123 /* Generate a DW_AT_name attribute given some string value to be included as
11124    the value of the attribute.  */
11125
11126 static void
11127 add_name_attribute (dw_die_ref die, const char *name_string)
11128 {
11129   if (name_string != NULL && *name_string != 0)
11130     {
11131       if (demangle_name_func)
11132         name_string = (*demangle_name_func) (name_string);
11133
11134       add_AT_string (die, DW_AT_name, name_string);
11135     }
11136 }
11137
11138 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11139
11140 static void
11141 add_comp_dir_attribute (dw_die_ref die)
11142 {
11143   const char *wd = get_src_pwd ();
11144   if (wd != NULL)
11145     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11146 }
11147
11148 /* Given a tree node describing an array bound (either lower or upper) output
11149    a representation for that bound.  */
11150
11151 static void
11152 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11153 {
11154   switch (TREE_CODE (bound))
11155     {
11156     case ERROR_MARK:
11157       return;
11158
11159     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11160     case INTEGER_CST:
11161       if (! host_integerp (bound, 0)
11162           || (bound_attr == DW_AT_lower_bound
11163               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11164                   || (is_fortran () && integer_onep (bound)))))
11165         /* Use the default.  */
11166         ;
11167       else
11168         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11169       break;
11170
11171     CASE_CONVERT:
11172     case VIEW_CONVERT_EXPR:
11173       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11174       break;
11175
11176     case SAVE_EXPR:
11177       break;
11178
11179     case VAR_DECL:
11180     case PARM_DECL:
11181     case RESULT_DECL:
11182       {
11183         dw_die_ref decl_die = lookup_decl_die (bound);
11184
11185         /* ??? Can this happen, or should the variable have been bound
11186            first?  Probably it can, since I imagine that we try to create
11187            the types of parameters in the order in which they exist in
11188            the list, and won't have created a forward reference to a
11189            later parameter.  */
11190         if (decl_die != NULL)
11191           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11192         break;
11193       }
11194
11195     default:
11196       {
11197         /* Otherwise try to create a stack operation procedure to
11198            evaluate the value of the array bound.  */
11199
11200         dw_die_ref ctx, decl_die;
11201         dw_loc_descr_ref loc;
11202
11203         loc = loc_descriptor_from_tree (bound);
11204         if (loc == NULL)
11205           break;
11206
11207         if (current_function_decl == 0)
11208           ctx = comp_unit_die;
11209         else
11210           ctx = lookup_decl_die (current_function_decl);
11211
11212         decl_die = new_die (DW_TAG_variable, ctx, bound);
11213         add_AT_flag (decl_die, DW_AT_artificial, 1);
11214         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11215         add_AT_loc (decl_die, DW_AT_location, loc);
11216
11217         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11218         break;
11219       }
11220     }
11221 }
11222
11223 /* Note that the block of subscript information for an array type also
11224    includes information about the element type of type given array type.  */
11225
11226 static void
11227 add_subscript_info (dw_die_ref type_die, tree type)
11228 {
11229 #ifndef MIPS_DEBUGGING_INFO
11230   unsigned dimension_number;
11231 #endif
11232   tree lower, upper;
11233   dw_die_ref subrange_die;
11234
11235   /* The GNU compilers represent multidimensional array types as sequences of
11236      one dimensional array types whose element types are themselves array
11237      types.  Here we squish that down, so that each multidimensional array
11238      type gets only one array_type DIE in the Dwarf debugging info. The draft
11239      Dwarf specification say that we are allowed to do this kind of
11240      compression in C (because there is no difference between an array or
11241      arrays and a multidimensional array in C) but for other source languages
11242      (e.g. Ada) we probably shouldn't do this.  */
11243
11244   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11245      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11246      We work around this by disabling this feature.  See also
11247      gen_array_type_die.  */
11248 #ifndef MIPS_DEBUGGING_INFO
11249   for (dimension_number = 0;
11250        TREE_CODE (type) == ARRAY_TYPE;
11251        type = TREE_TYPE (type), dimension_number++)
11252 #endif
11253     {
11254       tree domain = TYPE_DOMAIN (type);
11255
11256       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11257          and (in GNU C only) variable bounds.  Handle all three forms
11258          here.  */
11259       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11260       if (domain)
11261         {
11262           /* We have an array type with specified bounds.  */
11263           lower = TYPE_MIN_VALUE (domain);
11264           upper = TYPE_MAX_VALUE (domain);
11265
11266           /* Define the index type.  */
11267           if (TREE_TYPE (domain))
11268             {
11269               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11270                  TREE_TYPE field.  We can't emit debug info for this
11271                  because it is an unnamed integral type.  */
11272               if (TREE_CODE (domain) == INTEGER_TYPE
11273                   && TYPE_NAME (domain) == NULL_TREE
11274                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11275                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11276                 ;
11277               else
11278                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11279                                     type_die);
11280             }
11281
11282           /* ??? If upper is NULL, the array has unspecified length,
11283              but it does have a lower bound.  This happens with Fortran
11284                dimension arr(N:*)
11285              Since the debugger is definitely going to need to know N
11286              to produce useful results, go ahead and output the lower
11287              bound solo, and hope the debugger can cope.  */
11288
11289           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11290           if (upper)
11291             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11292         }
11293
11294       /* Otherwise we have an array type with an unspecified length.  The
11295          DWARF-2 spec does not say how to handle this; let's just leave out the
11296          bounds.  */
11297     }
11298 }
11299
11300 static void
11301 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11302 {
11303   unsigned size;
11304
11305   switch (TREE_CODE (tree_node))
11306     {
11307     case ERROR_MARK:
11308       size = 0;
11309       break;
11310     case ENUMERAL_TYPE:
11311     case RECORD_TYPE:
11312     case UNION_TYPE:
11313     case QUAL_UNION_TYPE:
11314       size = int_size_in_bytes (tree_node);
11315       break;
11316     case FIELD_DECL:
11317       /* For a data member of a struct or union, the DW_AT_byte_size is
11318          generally given as the number of bytes normally allocated for an
11319          object of the *declared* type of the member itself.  This is true
11320          even for bit-fields.  */
11321       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11322       break;
11323     default:
11324       gcc_unreachable ();
11325     }
11326
11327   /* Note that `size' might be -1 when we get to this point.  If it is, that
11328      indicates that the byte size of the entity in question is variable.  We
11329      have no good way of expressing this fact in Dwarf at the present time,
11330      so just let the -1 pass on through.  */
11331   add_AT_unsigned (die, DW_AT_byte_size, size);
11332 }
11333
11334 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11335    which specifies the distance in bits from the highest order bit of the
11336    "containing object" for the bit-field to the highest order bit of the
11337    bit-field itself.
11338
11339    For any given bit-field, the "containing object" is a hypothetical object
11340    (of some integral or enum type) within which the given bit-field lives.  The
11341    type of this hypothetical "containing object" is always the same as the
11342    declared type of the individual bit-field itself.  The determination of the
11343    exact location of the "containing object" for a bit-field is rather
11344    complicated.  It's handled by the `field_byte_offset' function (above).
11345
11346    Note that it is the size (in bytes) of the hypothetical "containing object"
11347    which will be given in the DW_AT_byte_size attribute for this bit-field.
11348    (See `byte_size_attribute' above).  */
11349
11350 static inline void
11351 add_bit_offset_attribute (dw_die_ref die, tree decl)
11352 {
11353   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11354   tree type = DECL_BIT_FIELD_TYPE (decl);
11355   HOST_WIDE_INT bitpos_int;
11356   HOST_WIDE_INT highest_order_object_bit_offset;
11357   HOST_WIDE_INT highest_order_field_bit_offset;
11358   HOST_WIDE_INT unsigned bit_offset;
11359
11360   /* Must be a field and a bit field.  */
11361   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11362
11363   /* We can't yet handle bit-fields whose offsets are variable, so if we
11364      encounter such things, just return without generating any attribute
11365      whatsoever.  Likewise for variable or too large size.  */
11366   if (! host_integerp (bit_position (decl), 0)
11367       || ! host_integerp (DECL_SIZE (decl), 1))
11368     return;
11369
11370   bitpos_int = int_bit_position (decl);
11371
11372   /* Note that the bit offset is always the distance (in bits) from the
11373      highest-order bit of the "containing object" to the highest-order bit of
11374      the bit-field itself.  Since the "high-order end" of any object or field
11375      is different on big-endian and little-endian machines, the computation
11376      below must take account of these differences.  */
11377   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11378   highest_order_field_bit_offset = bitpos_int;
11379
11380   if (! BYTES_BIG_ENDIAN)
11381     {
11382       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11383       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11384     }
11385
11386   bit_offset
11387     = (! BYTES_BIG_ENDIAN
11388        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11389        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11390
11391   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11392 }
11393
11394 /* For a FIELD_DECL node which represents a bit field, output an attribute
11395    which specifies the length in bits of the given field.  */
11396
11397 static inline void
11398 add_bit_size_attribute (dw_die_ref die, tree decl)
11399 {
11400   /* Must be a field and a bit field.  */
11401   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11402               && DECL_BIT_FIELD_TYPE (decl));
11403
11404   if (host_integerp (DECL_SIZE (decl), 1))
11405     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11406 }
11407
11408 /* If the compiled language is ANSI C, then add a 'prototyped'
11409    attribute, if arg types are given for the parameters of a function.  */
11410
11411 static inline void
11412 add_prototyped_attribute (dw_die_ref die, tree func_type)
11413 {
11414   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11415       && TYPE_ARG_TYPES (func_type) != NULL)
11416     add_AT_flag (die, DW_AT_prototyped, 1);
11417 }
11418
11419 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11420    by looking in either the type declaration or object declaration
11421    equate table.  */
11422
11423 static inline void
11424 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11425 {
11426   dw_die_ref origin_die = NULL;
11427
11428   if (TREE_CODE (origin) != FUNCTION_DECL)
11429     {
11430       /* We may have gotten separated from the block for the inlined
11431          function, if we're in an exception handler or some such; make
11432          sure that the abstract function has been written out.
11433
11434          Doing this for nested functions is wrong, however; functions are
11435          distinct units, and our context might not even be inline.  */
11436       tree fn = origin;
11437
11438       if (TYPE_P (fn))
11439         fn = TYPE_STUB_DECL (fn);
11440
11441       fn = decl_function_context (fn);
11442       if (fn)
11443         dwarf2out_abstract_function (fn);
11444     }
11445
11446   if (DECL_P (origin))
11447     origin_die = lookup_decl_die (origin);
11448   else if (TYPE_P (origin))
11449     origin_die = lookup_type_die (origin);
11450
11451   /* XXX: Functions that are never lowered don't always have correct block
11452      trees (in the case of java, they simply have no block tree, in some other
11453      languages).  For these functions, there is nothing we can really do to
11454      output correct debug info for inlined functions in all cases.  Rather
11455      than die, we'll just produce deficient debug info now, in that we will
11456      have variables without a proper abstract origin.  In the future, when all
11457      functions are lowered, we should re-add a gcc_assert (origin_die)
11458      here.  */
11459
11460   if (origin_die)
11461       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11462 }
11463
11464 /* We do not currently support the pure_virtual attribute.  */
11465
11466 static inline void
11467 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11468 {
11469   if (DECL_VINDEX (func_decl))
11470     {
11471       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11472
11473       if (host_integerp (DECL_VINDEX (func_decl), 0))
11474         add_AT_loc (die, DW_AT_vtable_elem_location,
11475                     new_loc_descr (DW_OP_constu,
11476                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11477                                    0));
11478
11479       /* GNU extension: Record what type this method came from originally.  */
11480       if (debug_info_level > DINFO_LEVEL_TERSE)
11481         add_AT_die_ref (die, DW_AT_containing_type,
11482                         lookup_type_die (DECL_CONTEXT (func_decl)));
11483     }
11484 }
11485 \f
11486 /* Add source coordinate attributes for the given decl.  */
11487
11488 static void
11489 add_src_coords_attributes (dw_die_ref die, tree decl)
11490 {
11491   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11492
11493   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11494   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11495 }
11496
11497 /* Add a DW_AT_name attribute and source coordinate attribute for the
11498    given decl, but only if it actually has a name.  */
11499
11500 static void
11501 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11502 {
11503   tree decl_name;
11504
11505   decl_name = DECL_NAME (decl);
11506   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11507     {
11508       add_name_attribute (die, dwarf2_name (decl, 0));
11509       if (! DECL_ARTIFICIAL (decl))
11510         add_src_coords_attributes (die, decl);
11511
11512       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11513           && TREE_PUBLIC (decl)
11514           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11515           && !DECL_ABSTRACT (decl)
11516           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11517           && !is_fortran ())
11518         add_AT_string (die, DW_AT_MIPS_linkage_name,
11519                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11520     }
11521
11522 #ifdef VMS_DEBUGGING_INFO
11523   /* Get the function's name, as described by its RTL.  This may be different
11524      from the DECL_NAME name used in the source file.  */
11525   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11526     {
11527       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11528                    XEXP (DECL_RTL (decl), 0));
11529       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11530     }
11531 #endif
11532 }
11533
11534 /* Push a new declaration scope.  */
11535
11536 static void
11537 push_decl_scope (tree scope)
11538 {
11539   VEC_safe_push (tree, gc, decl_scope_table, scope);
11540 }
11541
11542 /* Pop a declaration scope.  */
11543
11544 static inline void
11545 pop_decl_scope (void)
11546 {
11547   VEC_pop (tree, decl_scope_table);
11548 }
11549
11550 /* Return the DIE for the scope that immediately contains this type.
11551    Non-named types get global scope.  Named types nested in other
11552    types get their containing scope if it's open, or global scope
11553    otherwise.  All other types (i.e. function-local named types) get
11554    the current active scope.  */
11555
11556 static dw_die_ref
11557 scope_die_for (tree t, dw_die_ref context_die)
11558 {
11559   dw_die_ref scope_die = NULL;
11560   tree containing_scope;
11561   int i;
11562
11563   /* Non-types always go in the current scope.  */
11564   gcc_assert (TYPE_P (t));
11565
11566   containing_scope = TYPE_CONTEXT (t);
11567
11568   /* Use the containing namespace if it was passed in (for a declaration).  */
11569   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11570     {
11571       if (context_die == lookup_decl_die (containing_scope))
11572         /* OK */;
11573       else
11574         containing_scope = NULL_TREE;
11575     }
11576
11577   /* Ignore function type "scopes" from the C frontend.  They mean that
11578      a tagged type is local to a parmlist of a function declarator, but
11579      that isn't useful to DWARF.  */
11580   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11581     containing_scope = NULL_TREE;
11582
11583   if (containing_scope == NULL_TREE)
11584     scope_die = comp_unit_die;
11585   else if (TYPE_P (containing_scope))
11586     {
11587       /* For types, we can just look up the appropriate DIE.  But
11588          first we check to see if we're in the middle of emitting it
11589          so we know where the new DIE should go.  */
11590       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11591         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11592           break;
11593
11594       if (i < 0)
11595         {
11596           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11597                       || TREE_ASM_WRITTEN (containing_scope));
11598
11599           /* If none of the current dies are suitable, we get file scope.  */
11600           scope_die = comp_unit_die;
11601         }
11602       else
11603         scope_die = lookup_type_die (containing_scope);
11604     }
11605   else
11606     scope_die = context_die;
11607
11608   return scope_die;
11609 }
11610
11611 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11612
11613 static inline int
11614 local_scope_p (dw_die_ref context_die)
11615 {
11616   for (; context_die; context_die = context_die->die_parent)
11617     if (context_die->die_tag == DW_TAG_inlined_subroutine
11618         || context_die->die_tag == DW_TAG_subprogram)
11619       return 1;
11620
11621   return 0;
11622 }
11623
11624 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11625    whether or not to treat a DIE in this context as a declaration.  */
11626
11627 static inline int
11628 class_or_namespace_scope_p (dw_die_ref context_die)
11629 {
11630   return (context_die
11631           && (context_die->die_tag == DW_TAG_structure_type
11632               || context_die->die_tag == DW_TAG_class_type
11633               || context_die->die_tag == DW_TAG_interface_type
11634               || context_die->die_tag == DW_TAG_union_type
11635               || context_die->die_tag == DW_TAG_namespace));
11636 }
11637
11638 /* Many forms of DIEs require a "type description" attribute.  This
11639    routine locates the proper "type descriptor" die for the type given
11640    by 'type', and adds a DW_AT_type attribute below the given die.  */
11641
11642 static void
11643 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11644                     int decl_volatile, dw_die_ref context_die)
11645 {
11646   enum tree_code code  = TREE_CODE (type);
11647   dw_die_ref type_die  = NULL;
11648
11649   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11650      or fixed-point type, use the inner type.  This is because we have no
11651      support for unnamed types in base_type_die.  This can happen if this is
11652      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11653   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11654       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11655     type = TREE_TYPE (type), code = TREE_CODE (type);
11656
11657   if (code == ERROR_MARK
11658       /* Handle a special case.  For functions whose return type is void, we
11659          generate *no* type attribute.  (Note that no object may have type
11660          `void', so this only applies to function return types).  */
11661       || code == VOID_TYPE)
11662     return;
11663
11664   type_die = modified_type_die (type,
11665                                 decl_const || TYPE_READONLY (type),
11666                                 decl_volatile || TYPE_VOLATILE (type),
11667                                 context_die);
11668
11669   if (type_die != NULL)
11670     add_AT_die_ref (object_die, DW_AT_type, type_die);
11671 }
11672
11673 /* Given an object die, add the calling convention attribute for the
11674    function call type.  */
11675 static void
11676 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11677 {
11678   enum dwarf_calling_convention value = DW_CC_normal;
11679
11680   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11681
11682   /* DWARF doesn't provide a way to identify a program's source-level
11683      entry point.  DW_AT_calling_convention attributes are only meant
11684      to describe functions' calling conventions.  However, lacking a
11685      better way to signal the Fortran main program, we use this for the
11686      time being, following existing custom.  */
11687   if (is_fortran ()
11688       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11689     value = DW_CC_program;
11690
11691   /* Only add the attribute if the backend requests it, and
11692      is not DW_CC_normal.  */
11693   if (value && (value != DW_CC_normal))
11694     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11695 }
11696
11697 /* Given a tree pointer to a struct, class, union, or enum type node, return
11698    a pointer to the (string) tag name for the given type, or zero if the type
11699    was declared without a tag.  */
11700
11701 static const char *
11702 type_tag (const_tree type)
11703 {
11704   const char *name = 0;
11705
11706   if (TYPE_NAME (type) != 0)
11707     {
11708       tree t = 0;
11709
11710       /* Find the IDENTIFIER_NODE for the type name.  */
11711       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11712         t = TYPE_NAME (type);
11713
11714       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11715          a TYPE_DECL node, regardless of whether or not a `typedef' was
11716          involved.  */
11717       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11718                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11719         {
11720           /* We want to be extra verbose.  Don't call dwarf_name if
11721              DECL_NAME isn't set.  The default hook for decl_printable_name
11722              doesn't like that, and in this context it's correct to return
11723              0, instead of "<anonymous>" or the like.  */
11724           if (DECL_NAME (TYPE_NAME (type)))
11725             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11726         }
11727
11728       /* Now get the name as a string, or invent one.  */
11729       if (!name && t != 0)
11730         name = IDENTIFIER_POINTER (t);
11731     }
11732
11733   return (name == 0 || *name == '\0') ? 0 : name;
11734 }
11735
11736 /* Return the type associated with a data member, make a special check
11737    for bit field types.  */
11738
11739 static inline tree
11740 member_declared_type (const_tree member)
11741 {
11742   return (DECL_BIT_FIELD_TYPE (member)
11743           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11744 }
11745
11746 /* Get the decl's label, as described by its RTL. This may be different
11747    from the DECL_NAME name used in the source file.  */
11748
11749 #if 0
11750 static const char *
11751 decl_start_label (tree decl)
11752 {
11753   rtx x;
11754   const char *fnname;
11755
11756   x = DECL_RTL (decl);
11757   gcc_assert (MEM_P (x));
11758
11759   x = XEXP (x, 0);
11760   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11761
11762   fnname = XSTR (x, 0);
11763   return fnname;
11764 }
11765 #endif
11766 \f
11767 /* These routines generate the internal representation of the DIE's for
11768    the compilation unit.  Debugging information is collected by walking
11769    the declaration trees passed in from dwarf2out_decl().  */
11770
11771 static void
11772 gen_array_type_die (tree type, dw_die_ref context_die)
11773 {
11774   dw_die_ref scope_die = scope_die_for (type, context_die);
11775   dw_die_ref array_die;
11776   tree element_type;
11777
11778   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11779      the inner array type comes before the outer array type.  Thus we must
11780      call gen_type_die before we call new_die.  See below also.  */
11781 #ifdef MIPS_DEBUGGING_INFO
11782   gen_type_die (TREE_TYPE (type), context_die);
11783 #endif
11784
11785   array_die = new_die (DW_TAG_array_type, scope_die, type);
11786   add_name_attribute (array_die, type_tag (type));
11787   equate_type_number_to_die (type, array_die);
11788
11789   if (TREE_CODE (type) == VECTOR_TYPE)
11790     {
11791       /* The frontend feeds us a representation for the vector as a struct
11792          containing an array.  Pull out the array type.  */
11793       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11794       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11795     }
11796
11797   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11798   if (is_fortran ()
11799       && TREE_CODE (type) == ARRAY_TYPE
11800       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11801     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11802
11803 #if 0
11804   /* We default the array ordering.  SDB will probably do
11805      the right things even if DW_AT_ordering is not present.  It's not even
11806      an issue until we start to get into multidimensional arrays anyway.  If
11807      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11808      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11809      and when we find out that we need to put these in, we will only do so
11810      for multidimensional arrays.  */
11811   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11812 #endif
11813
11814 #ifdef MIPS_DEBUGGING_INFO
11815   /* The SGI compilers handle arrays of unknown bound by setting
11816      AT_declaration and not emitting any subrange DIEs.  */
11817   if (! TYPE_DOMAIN (type))
11818     add_AT_flag (array_die, DW_AT_declaration, 1);
11819   else
11820 #endif
11821     add_subscript_info (array_die, type);
11822
11823   /* Add representation of the type of the elements of this array type.  */
11824   element_type = TREE_TYPE (type);
11825
11826   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11827      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11828      We work around this by disabling this feature.  See also
11829      add_subscript_info.  */
11830 #ifndef MIPS_DEBUGGING_INFO
11831   while (TREE_CODE (element_type) == ARRAY_TYPE)
11832     element_type = TREE_TYPE (element_type);
11833
11834   gen_type_die (element_type, context_die);
11835 #endif
11836
11837   add_type_attribute (array_die, element_type, 0, 0, context_die);
11838
11839   if (get_AT (array_die, DW_AT_name))
11840     add_pubtype (type, array_die);
11841 }
11842
11843 static dw_loc_descr_ref
11844 descr_info_loc (tree val, tree base_decl)
11845 {
11846   HOST_WIDE_INT size;
11847   dw_loc_descr_ref loc, loc2;
11848   enum dwarf_location_atom op;
11849
11850   if (val == base_decl)
11851     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11852
11853   switch (TREE_CODE (val))
11854     {
11855     CASE_CONVERT:
11856       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11857     case INTEGER_CST:
11858       if (host_integerp (val, 0))
11859         return int_loc_descriptor (tree_low_cst (val, 0));
11860       break;
11861     case INDIRECT_REF:
11862       size = int_size_in_bytes (TREE_TYPE (val));
11863       if (size < 0)
11864         break;
11865       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11866       if (!loc)
11867         break;
11868       if (size == DWARF2_ADDR_SIZE)
11869         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11870       else
11871         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11872       return loc;
11873     case POINTER_PLUS_EXPR:
11874     case PLUS_EXPR:
11875       if (host_integerp (TREE_OPERAND (val, 1), 1)
11876           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11877              < 16384)
11878         {
11879           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11880           if (!loc)
11881             break;
11882           add_loc_descr (&loc,
11883                          new_loc_descr (DW_OP_plus_uconst,
11884                                         tree_low_cst (TREE_OPERAND (val, 1),
11885                                                       1), 0));
11886         }
11887       else
11888         {
11889           op = DW_OP_plus;
11890         do_binop:
11891           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11892           if (!loc)
11893             break;
11894           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11895           if (!loc2)
11896             break;
11897           add_loc_descr (&loc, loc2);
11898           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11899         }
11900       return loc;
11901     case MINUS_EXPR:
11902       op = DW_OP_minus;
11903       goto do_binop;
11904     case MULT_EXPR:
11905       op = DW_OP_mul;
11906       goto do_binop;
11907     case EQ_EXPR:
11908       op = DW_OP_eq;
11909       goto do_binop;
11910     case NE_EXPR:
11911       op = DW_OP_ne;
11912       goto do_binop;
11913     default:
11914       break;
11915     }
11916   return NULL;
11917 }
11918
11919 static void
11920 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11921                       tree val, tree base_decl)
11922 {
11923   dw_loc_descr_ref loc;
11924
11925   if (host_integerp (val, 0))
11926     {
11927       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11928       return;
11929     }
11930
11931   loc = descr_info_loc (val, base_decl);
11932   if (!loc)
11933     return;
11934
11935   add_AT_loc (die, attr, loc);
11936 }
11937
11938 /* This routine generates DIE for array with hidden descriptor, details
11939    are filled into *info by a langhook.  */
11940
11941 static void
11942 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11943                           dw_die_ref context_die)
11944 {
11945   dw_die_ref scope_die = scope_die_for (type, context_die);
11946   dw_die_ref array_die;
11947   int dim;
11948
11949   array_die = new_die (DW_TAG_array_type, scope_die, type);
11950   add_name_attribute (array_die, type_tag (type));
11951   equate_type_number_to_die (type, array_die);
11952
11953   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11954   if (is_fortran ()
11955       && info->ndimensions >= 2)
11956     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11957
11958   if (info->data_location)
11959     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11960                           info->base_decl);
11961   if (info->associated)
11962     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11963                           info->base_decl);
11964   if (info->allocated)
11965     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11966                           info->base_decl);
11967
11968   for (dim = 0; dim < info->ndimensions; dim++)
11969     {
11970       dw_die_ref subrange_die
11971         = new_die (DW_TAG_subrange_type, array_die, NULL);
11972
11973       if (info->dimen[dim].lower_bound)
11974         {
11975           /* If it is the default value, omit it.  */
11976           if ((is_c_family () || is_java ())
11977               && integer_zerop (info->dimen[dim].lower_bound))
11978             ;
11979           else if (is_fortran ()
11980                    && integer_onep (info->dimen[dim].lower_bound))
11981             ;
11982           else
11983             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11984                                   info->dimen[dim].lower_bound,
11985                                   info->base_decl);
11986         }
11987       if (info->dimen[dim].upper_bound)
11988         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11989                               info->dimen[dim].upper_bound,
11990                               info->base_decl);
11991       if (info->dimen[dim].stride)
11992         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11993                               info->dimen[dim].stride,
11994                               info->base_decl);
11995     }
11996
11997   gen_type_die (info->element_type, context_die);
11998   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11999
12000   if (get_AT (array_die, DW_AT_name))
12001     add_pubtype (type, array_die);
12002 }
12003
12004 #if 0
12005 static void
12006 gen_entry_point_die (tree decl, dw_die_ref context_die)
12007 {
12008   tree origin = decl_ultimate_origin (decl);
12009   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12010
12011   if (origin != NULL)
12012     add_abstract_origin_attribute (decl_die, origin);
12013   else
12014     {
12015       add_name_and_src_coords_attributes (decl_die, decl);
12016       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12017                           0, 0, context_die);
12018     }
12019
12020   if (DECL_ABSTRACT (decl))
12021     equate_decl_number_to_die (decl, decl_die);
12022   else
12023     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12024 }
12025 #endif
12026
12027 /* Walk through the list of incomplete types again, trying once more to
12028    emit full debugging info for them.  */
12029
12030 static void
12031 retry_incomplete_types (void)
12032 {
12033   int i;
12034
12035   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12036     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12037 }
12038
12039 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12040
12041 static void
12042 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12043 {
12044   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12045
12046   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12047      be incomplete and such types are not marked.  */
12048   add_abstract_origin_attribute (type_die, type);
12049 }
12050
12051 /* Determine what tag to use for a record type.  */
12052
12053 static enum dwarf_tag
12054 record_type_tag (tree type)
12055 {
12056   if (! lang_hooks.types.classify_record)
12057     return DW_TAG_structure_type;
12058
12059   switch (lang_hooks.types.classify_record (type))
12060     {
12061     case RECORD_IS_STRUCT:
12062       return DW_TAG_structure_type;
12063
12064     case RECORD_IS_CLASS:
12065       return DW_TAG_class_type;
12066
12067     case RECORD_IS_INTERFACE:
12068       return DW_TAG_interface_type;
12069
12070     default:
12071       gcc_unreachable ();
12072     }
12073 }
12074
12075 /* Generate a DIE to represent an inlined instance of a structure type.  */
12076
12077 static void
12078 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12079 {
12080   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12081
12082   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12083      be incomplete and such types are not marked.  */
12084   add_abstract_origin_attribute (type_die, type);
12085 }
12086
12087 /* Generate a DIE to represent an inlined instance of a union type.  */
12088
12089 static void
12090 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12091 {
12092   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12093
12094   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12095      be incomplete and such types are not marked.  */
12096   add_abstract_origin_attribute (type_die, type);
12097 }
12098
12099 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12100    include all of the information about the enumeration values also. Each
12101    enumerated type name/value is listed as a child of the enumerated type
12102    DIE.  */
12103
12104 static dw_die_ref
12105 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12106 {
12107   dw_die_ref type_die = lookup_type_die (type);
12108
12109   if (type_die == NULL)
12110     {
12111       type_die = new_die (DW_TAG_enumeration_type,
12112                           scope_die_for (type, context_die), type);
12113       equate_type_number_to_die (type, type_die);
12114       add_name_attribute (type_die, type_tag (type));
12115     }
12116   else if (! TYPE_SIZE (type))
12117     return type_die;
12118   else
12119     remove_AT (type_die, DW_AT_declaration);
12120
12121   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12122      given enum type is incomplete, do not generate the DW_AT_byte_size
12123      attribute or the DW_AT_element_list attribute.  */
12124   if (TYPE_SIZE (type))
12125     {
12126       tree link;
12127
12128       TREE_ASM_WRITTEN (type) = 1;
12129       add_byte_size_attribute (type_die, type);
12130       if (TYPE_STUB_DECL (type) != NULL_TREE)
12131         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12132
12133       /* If the first reference to this type was as the return type of an
12134          inline function, then it may not have a parent.  Fix this now.  */
12135       if (type_die->die_parent == NULL)
12136         add_child_die (scope_die_for (type, context_die), type_die);
12137
12138       for (link = TYPE_VALUES (type);
12139            link != NULL; link = TREE_CHAIN (link))
12140         {
12141           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12142           tree value = TREE_VALUE (link);
12143
12144           add_name_attribute (enum_die,
12145                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12146
12147           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12148             /* DWARF2 does not provide a way of indicating whether or
12149                not enumeration constants are signed or unsigned.  GDB
12150                always assumes the values are signed, so we output all
12151                values as if they were signed.  That means that
12152                enumeration constants with very large unsigned values
12153                will appear to have negative values in the debugger.  */
12154             add_AT_int (enum_die, DW_AT_const_value,
12155                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12156         }
12157     }
12158   else
12159     add_AT_flag (type_die, DW_AT_declaration, 1);
12160
12161   if (get_AT (type_die, DW_AT_name))
12162     add_pubtype (type, type_die);
12163
12164   return type_die;
12165 }
12166
12167 /* Generate a DIE to represent either a real live formal parameter decl or to
12168    represent just the type of some formal parameter position in some function
12169    type.
12170
12171    Note that this routine is a bit unusual because its argument may be a
12172    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12173    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12174    node.  If it's the former then this function is being called to output a
12175    DIE to represent a formal parameter object (or some inlining thereof).  If
12176    it's the latter, then this function is only being called to output a
12177    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12178    argument type of some subprogram type.  */
12179
12180 static dw_die_ref
12181 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12182 {
12183   dw_die_ref parm_die
12184     = new_die (DW_TAG_formal_parameter, context_die, node);
12185   tree origin;
12186
12187   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12188     {
12189     case tcc_declaration:
12190       origin = decl_ultimate_origin (node);
12191       if (origin != NULL)
12192         add_abstract_origin_attribute (parm_die, origin);
12193       else
12194         {
12195           tree type = TREE_TYPE (node);
12196           add_name_and_src_coords_attributes (parm_die, node);
12197           if (DECL_BY_REFERENCE (node))
12198             type = TREE_TYPE (type);
12199           add_type_attribute (parm_die, type,
12200                               TREE_READONLY (node),
12201                               TREE_THIS_VOLATILE (node),
12202                               context_die);
12203           if (DECL_ARTIFICIAL (node))
12204             add_AT_flag (parm_die, DW_AT_artificial, 1);
12205         }
12206
12207       equate_decl_number_to_die (node, parm_die);
12208       if (! DECL_ABSTRACT (node))
12209         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12210
12211       break;
12212
12213     case tcc_type:
12214       /* We were called with some kind of a ..._TYPE node.  */
12215       add_type_attribute (parm_die, node, 0, 0, context_die);
12216       break;
12217
12218     default:
12219       gcc_unreachable ();
12220     }
12221
12222   return parm_die;
12223 }
12224
12225 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12226    at the end of an (ANSI prototyped) formal parameters list.  */
12227
12228 static void
12229 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12230 {
12231   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12232 }
12233
12234 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12235    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12236    parameters as specified in some function type specification (except for
12237    those which appear as part of a function *definition*).  */
12238
12239 static void
12240 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12241 {
12242   tree link;
12243   tree formal_type = NULL;
12244   tree first_parm_type;
12245   tree arg;
12246
12247   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12248     {
12249       arg = DECL_ARGUMENTS (function_or_method_type);
12250       function_or_method_type = TREE_TYPE (function_or_method_type);
12251     }
12252   else
12253     arg = NULL_TREE;
12254
12255   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12256
12257   /* Make our first pass over the list of formal parameter types and output a
12258      DW_TAG_formal_parameter DIE for each one.  */
12259   for (link = first_parm_type; link; )
12260     {
12261       dw_die_ref parm_die;
12262
12263       formal_type = TREE_VALUE (link);
12264       if (formal_type == void_type_node)
12265         break;
12266
12267       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12268       parm_die = gen_formal_parameter_die (formal_type, context_die);
12269       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12270            && link == first_parm_type)
12271           || (arg && DECL_ARTIFICIAL (arg)))
12272         add_AT_flag (parm_die, DW_AT_artificial, 1);
12273
12274       link = TREE_CHAIN (link);
12275       if (arg)
12276         arg = TREE_CHAIN (arg);
12277     }
12278
12279   /* If this function type has an ellipsis, add a
12280      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12281   if (formal_type != void_type_node)
12282     gen_unspecified_parameters_die (function_or_method_type, context_die);
12283
12284   /* Make our second (and final) pass over the list of formal parameter types
12285      and output DIEs to represent those types (as necessary).  */
12286   for (link = TYPE_ARG_TYPES (function_or_method_type);
12287        link && TREE_VALUE (link);
12288        link = TREE_CHAIN (link))
12289     gen_type_die (TREE_VALUE (link), context_die);
12290 }
12291
12292 /* We want to generate the DIE for TYPE so that we can generate the
12293    die for MEMBER, which has been defined; we will need to refer back
12294    to the member declaration nested within TYPE.  If we're trying to
12295    generate minimal debug info for TYPE, processing TYPE won't do the
12296    trick; we need to attach the member declaration by hand.  */
12297
12298 static void
12299 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12300 {
12301   gen_type_die (type, context_die);
12302
12303   /* If we're trying to avoid duplicate debug info, we may not have
12304      emitted the member decl for this function.  Emit it now.  */
12305   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12306       && ! lookup_decl_die (member))
12307     {
12308       dw_die_ref type_die;
12309       gcc_assert (!decl_ultimate_origin (member));
12310
12311       push_decl_scope (type);
12312       type_die = lookup_type_die (type);
12313       if (TREE_CODE (member) == FUNCTION_DECL)
12314         gen_subprogram_die (member, type_die);
12315       else if (TREE_CODE (member) == FIELD_DECL)
12316         {
12317           /* Ignore the nameless fields that are used to skip bits but handle
12318              C++ anonymous unions and structs.  */
12319           if (DECL_NAME (member) != NULL_TREE
12320               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12321               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12322             {
12323               gen_type_die (member_declared_type (member), type_die);
12324               gen_field_die (member, type_die);
12325             }
12326         }
12327       else
12328         gen_variable_die (member, type_die);
12329
12330       pop_decl_scope ();
12331     }
12332 }
12333
12334 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12335    may later generate inlined and/or out-of-line instances of.  */
12336
12337 static void
12338 dwarf2out_abstract_function (tree decl)
12339 {
12340   dw_die_ref old_die;
12341   tree save_fn;
12342   tree context;
12343   int was_abstract = DECL_ABSTRACT (decl);
12344
12345   /* Make sure we have the actual abstract inline, not a clone.  */
12346   decl = DECL_ORIGIN (decl);
12347
12348   old_die = lookup_decl_die (decl);
12349   if (old_die && get_AT (old_die, DW_AT_inline))
12350     /* We've already generated the abstract instance.  */
12351     return;
12352
12353   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12354      we don't get confused by DECL_ABSTRACT.  */
12355   if (debug_info_level > DINFO_LEVEL_TERSE)
12356     {
12357       context = decl_class_context (decl);
12358       if (context)
12359         gen_type_die_for_member
12360           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12361     }
12362
12363   /* Pretend we've just finished compiling this function.  */
12364   save_fn = current_function_decl;
12365   current_function_decl = decl;
12366   push_cfun (DECL_STRUCT_FUNCTION (decl));
12367
12368   set_decl_abstract_flags (decl, 1);
12369   dwarf2out_decl (decl);
12370   if (! was_abstract)
12371     set_decl_abstract_flags (decl, 0);
12372
12373   current_function_decl = save_fn;
12374   pop_cfun ();
12375 }
12376
12377 /* Helper function of premark_used_types() which gets called through
12378    htab_traverse_resize().
12379
12380    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12381    marked as unused by prune_unused_types.  */
12382 static int
12383 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12384 {
12385   tree type;
12386   dw_die_ref die;
12387
12388   type = (tree) *slot;
12389   die = lookup_type_die (type);
12390   if (die != NULL)
12391     die->die_perennial_p = 1;
12392   return 1;
12393 }
12394
12395 /* Mark all members of used_types_hash as perennial.  */
12396 static void
12397 premark_used_types (void)
12398 {
12399   if (cfun && cfun->used_types_hash)
12400     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12401 }
12402
12403 /* Generate a DIE to represent a declared function (either file-scope or
12404    block-local).  */
12405
12406 static void
12407 gen_subprogram_die (tree decl, dw_die_ref context_die)
12408 {
12409   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12410   tree origin = decl_ultimate_origin (decl);
12411   dw_die_ref subr_die;
12412   tree fn_arg_types;
12413   tree outer_scope;
12414   dw_die_ref old_die = lookup_decl_die (decl);
12415   int declaration = (current_function_decl != decl
12416                      || class_or_namespace_scope_p (context_die));
12417
12418   premark_used_types ();
12419
12420   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12421      started to generate the abstract instance of an inline, decided to output
12422      its containing class, and proceeded to emit the declaration of the inline
12423      from the member list for the class.  If so, DECLARATION takes priority;
12424      we'll get back to the abstract instance when done with the class.  */
12425
12426   /* The class-scope declaration DIE must be the primary DIE.  */
12427   if (origin && declaration && class_or_namespace_scope_p (context_die))
12428     {
12429       origin = NULL;
12430       gcc_assert (!old_die);
12431     }
12432
12433   /* Now that the C++ front end lazily declares artificial member fns, we
12434      might need to retrofit the declaration into its class.  */
12435   if (!declaration && !origin && !old_die
12436       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12437       && !class_or_namespace_scope_p (context_die)
12438       && debug_info_level > DINFO_LEVEL_TERSE)
12439     old_die = force_decl_die (decl);
12440
12441   if (origin != NULL)
12442     {
12443       gcc_assert (!declaration || local_scope_p (context_die));
12444
12445       /* Fixup die_parent for the abstract instance of a nested
12446          inline function.  */
12447       if (old_die && old_die->die_parent == NULL)
12448         add_child_die (context_die, old_die);
12449
12450       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12451       add_abstract_origin_attribute (subr_die, origin);
12452     }
12453   else if (old_die)
12454     {
12455       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12456       struct dwarf_file_data * file_index = lookup_filename (s.file);
12457
12458       if (!get_AT_flag (old_die, DW_AT_declaration)
12459           /* We can have a normal definition following an inline one in the
12460              case of redefinition of GNU C extern inlines.
12461              It seems reasonable to use AT_specification in this case.  */
12462           && !get_AT (old_die, DW_AT_inline))
12463         {
12464           /* Detect and ignore this case, where we are trying to output
12465              something we have already output.  */
12466           return;
12467         }
12468
12469       /* If the definition comes from the same place as the declaration,
12470          maybe use the old DIE.  We always want the DIE for this function
12471          that has the *_pc attributes to be under comp_unit_die so the
12472          debugger can find it.  We also need to do this for abstract
12473          instances of inlines, since the spec requires the out-of-line copy
12474          to have the same parent.  For local class methods, this doesn't
12475          apply; we just use the old DIE.  */
12476       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12477           && (DECL_ARTIFICIAL (decl)
12478               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12479                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12480                       == (unsigned) s.line))))
12481         {
12482           subr_die = old_die;
12483
12484           /* Clear out the declaration attribute and the formal parameters.
12485              Do not remove all children, because it is possible that this
12486              declaration die was forced using force_decl_die(). In such
12487              cases die that forced declaration die (e.g. TAG_imported_module)
12488              is one of the children that we do not want to remove.  */
12489           remove_AT (subr_die, DW_AT_declaration);
12490           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12491         }
12492       else
12493         {
12494           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12495           add_AT_specification (subr_die, old_die);
12496           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12497             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12498           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12499             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12500         }
12501     }
12502   else
12503     {
12504       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12505
12506       if (TREE_PUBLIC (decl))
12507         add_AT_flag (subr_die, DW_AT_external, 1);
12508
12509       add_name_and_src_coords_attributes (subr_die, decl);
12510       if (debug_info_level > DINFO_LEVEL_TERSE)
12511         {
12512           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12513           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12514                               0, 0, context_die);
12515         }
12516
12517       add_pure_or_virtual_attribute (subr_die, decl);
12518       if (DECL_ARTIFICIAL (decl))
12519         add_AT_flag (subr_die, DW_AT_artificial, 1);
12520
12521       if (TREE_PROTECTED (decl))
12522         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12523       else if (TREE_PRIVATE (decl))
12524         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12525     }
12526
12527   if (declaration)
12528     {
12529       if (!old_die || !get_AT (old_die, DW_AT_inline))
12530         {
12531           add_AT_flag (subr_die, DW_AT_declaration, 1);
12532
12533           /* The first time we see a member function, it is in the context of
12534              the class to which it belongs.  We make sure of this by emitting
12535              the class first.  The next time is the definition, which is
12536              handled above.  The two may come from the same source text.
12537
12538              Note that force_decl_die() forces function declaration die. It is
12539              later reused to represent definition.  */
12540           equate_decl_number_to_die (decl, subr_die);
12541         }
12542     }
12543   else if (DECL_ABSTRACT (decl))
12544     {
12545       if (DECL_DECLARED_INLINE_P (decl))
12546         {
12547           if (cgraph_function_possibly_inlined_p (decl))
12548             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12549           else
12550             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12551         }
12552       else
12553         {
12554           if (cgraph_function_possibly_inlined_p (decl))
12555             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12556           else
12557             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12558         }
12559
12560       if (DECL_DECLARED_INLINE_P (decl)
12561           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12562         add_AT_flag (subr_die, DW_AT_artificial, 1);
12563
12564       equate_decl_number_to_die (decl, subr_die);
12565     }
12566   else if (!DECL_EXTERNAL (decl))
12567     {
12568       HOST_WIDE_INT cfa_fb_offset;
12569
12570       if (!old_die || !get_AT (old_die, DW_AT_inline))
12571         equate_decl_number_to_die (decl, subr_die);
12572
12573       if (!flag_reorder_blocks_and_partition)
12574         {
12575           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12576                                        current_function_funcdef_no);
12577           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12578           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12579                                        current_function_funcdef_no);
12580           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12581
12582           add_pubname (decl, subr_die);
12583           add_arange (decl, subr_die);
12584         }
12585       else
12586         {  /* Do nothing for now; maybe need to duplicate die, one for
12587               hot section and one for cold section, then use the hot/cold
12588               section begin/end labels to generate the aranges...  */
12589           /*
12590             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12591             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12592             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12593             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12594
12595             add_pubname (decl, subr_die);
12596             add_arange (decl, subr_die);
12597             add_arange (decl, subr_die);
12598            */
12599         }
12600
12601 #ifdef MIPS_DEBUGGING_INFO
12602       /* Add a reference to the FDE for this routine.  */
12603       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12604 #endif
12605
12606       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12607
12608       /* We define the "frame base" as the function's CFA.  This is more
12609          convenient for several reasons: (1) It's stable across the prologue
12610          and epilogue, which makes it better than just a frame pointer,
12611          (2) With dwarf3, there exists a one-byte encoding that allows us
12612          to reference the .debug_frame data by proxy, but failing that,
12613          (3) We can at least reuse the code inspection and interpretation
12614          code that determines the CFA position at various points in the
12615          function.  */
12616       /* ??? Use some command-line or configury switch to enable the use
12617          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12618          consumers that understand it; fall back to "pure" dwarf2 and
12619          convert the CFA data into a location list.  */
12620       {
12621         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12622         if (list->dw_loc_next)
12623           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12624         else
12625           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12626       }
12627
12628       /* Compute a displacement from the "steady-state frame pointer" to
12629          the CFA.  The former is what all stack slots and argument slots
12630          will reference in the rtl; the later is what we've told the
12631          debugger about.  We'll need to adjust all frame_base references
12632          by this displacement.  */
12633       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12634
12635       if (cfun->static_chain_decl)
12636         add_AT_location_description (subr_die, DW_AT_static_link,
12637                  loc_descriptor_from_tree (cfun->static_chain_decl));
12638     }
12639
12640   /* Now output descriptions of the arguments for this function. This gets
12641      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12642      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12643      `...' at the end of the formal parameter list.  In order to find out if
12644      there was a trailing ellipsis or not, we must instead look at the type
12645      associated with the FUNCTION_DECL.  This will be a node of type
12646      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12647      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12648      an ellipsis at the end.  */
12649
12650   /* In the case where we are describing a mere function declaration, all we
12651      need to do here (and all we *can* do here) is to describe the *types* of
12652      its formal parameters.  */
12653   if (debug_info_level <= DINFO_LEVEL_TERSE)
12654     ;
12655   else if (declaration)
12656     gen_formal_types_die (decl, subr_die);
12657   else
12658     {
12659       /* Generate DIEs to represent all known formal parameters.  */
12660       tree arg_decls = DECL_ARGUMENTS (decl);
12661       tree parm;
12662
12663       /* When generating DIEs, generate the unspecified_parameters DIE
12664          instead if we come across the arg "__builtin_va_alist" */
12665       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12666         if (TREE_CODE (parm) == PARM_DECL)
12667           {
12668             if (DECL_NAME (parm)
12669                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12670                             "__builtin_va_alist"))
12671               gen_unspecified_parameters_die (parm, subr_die);
12672             else
12673               gen_decl_die (parm, subr_die);
12674           }
12675
12676       /* Decide whether we need an unspecified_parameters DIE at the end.
12677          There are 2 more cases to do this for: 1) the ansi ... declaration -
12678          this is detectable when the end of the arg list is not a
12679          void_type_node 2) an unprototyped function declaration (not a
12680          definition).  This just means that we have no info about the
12681          parameters at all.  */
12682       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12683       if (fn_arg_types != NULL)
12684         {
12685           /* This is the prototyped case, check for....  */
12686           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12687             gen_unspecified_parameters_die (decl, subr_die);
12688         }
12689       else if (DECL_INITIAL (decl) == NULL_TREE)
12690         gen_unspecified_parameters_die (decl, subr_die);
12691     }
12692
12693   /* Output Dwarf info for all of the stuff within the body of the function
12694      (if it has one - it may be just a declaration).  */
12695   outer_scope = DECL_INITIAL (decl);
12696
12697   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12698      a function.  This BLOCK actually represents the outermost binding contour
12699      for the function, i.e. the contour in which the function's formal
12700      parameters and labels get declared. Curiously, it appears that the front
12701      end doesn't actually put the PARM_DECL nodes for the current function onto
12702      the BLOCK_VARS list for this outer scope, but are strung off of the
12703      DECL_ARGUMENTS list for the function instead.
12704
12705      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12706      the LABEL_DECL nodes for the function however, and we output DWARF info
12707      for those in decls_for_scope.  Just within the `outer_scope' there will be
12708      a BLOCK node representing the function's outermost pair of curly braces,
12709      and any blocks used for the base and member initializers of a C++
12710      constructor function.  */
12711   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12712     {
12713       /* Emit a DW_TAG_variable DIE for a named return value.  */
12714       if (DECL_NAME (DECL_RESULT (decl)))
12715         gen_decl_die (DECL_RESULT (decl), subr_die);
12716
12717       current_function_has_inlines = 0;
12718       decls_for_scope (outer_scope, subr_die, 0);
12719
12720 #if 0 && defined (MIPS_DEBUGGING_INFO)
12721       if (current_function_has_inlines)
12722         {
12723           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12724           if (! comp_unit_has_inlines)
12725             {
12726               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12727               comp_unit_has_inlines = 1;
12728             }
12729         }
12730 #endif
12731     }
12732   /* Add the calling convention attribute if requested.  */
12733   add_calling_convention_attribute (subr_die, decl);
12734
12735 }
12736
12737 /* Generate a DIE to represent a declared data object.  */
12738
12739 static void
12740 gen_variable_die (tree decl, dw_die_ref context_die)
12741 {
12742   HOST_WIDE_INT off;
12743   tree com_decl;
12744   dw_die_ref var_die;
12745   tree origin = decl_ultimate_origin (decl);
12746   dw_die_ref old_die = lookup_decl_die (decl);
12747   int declaration = (DECL_EXTERNAL (decl)
12748                      /* If DECL is COMDAT and has not actually been
12749                         emitted, we cannot take its address; there
12750                         might end up being no definition anywhere in
12751                         the program.  For example, consider the C++
12752                         test case:
12753
12754                           template <class T>
12755                           struct S { static const int i = 7; };
12756
12757                           template <class T>
12758                           const int S<T>::i;
12759
12760                           int f() { return S<int>::i; }
12761
12762                         Here, S<int>::i is not DECL_EXTERNAL, but no
12763                         definition is required, so the compiler will
12764                         not emit a definition.  */
12765                      || (TREE_CODE (decl) == VAR_DECL
12766                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12767                      || class_or_namespace_scope_p (context_die));
12768
12769   com_decl = fortran_common (decl, &off);
12770
12771   /* Symbol in common gets emitted as a child of the common block, in the form
12772      of a data member.
12773
12774      ??? This creates a new common block die for every common block symbol.
12775      Better to share same common block die for all symbols in that block.  */
12776   if (com_decl)
12777     {
12778       tree field;
12779       dw_die_ref com_die;
12780       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
12781       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
12782
12783       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12784       var_die = new_die (DW_TAG_common_block, context_die, decl);
12785       add_name_and_src_coords_attributes (var_die, field);
12786       add_AT_flag (var_die, DW_AT_external, 1);
12787       add_AT_loc (var_die, DW_AT_location, loc);
12788       com_die = new_die (DW_TAG_member, var_die, decl);
12789       add_name_and_src_coords_attributes (com_die, decl);
12790       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12791                           TREE_THIS_VOLATILE (decl), context_die);
12792       add_AT_loc (com_die, DW_AT_data_member_location,
12793                   int_loc_descriptor (off));
12794       add_pubname_string (cnam, var_die); /* ??? needed? */
12795       return;
12796     }
12797
12798   var_die = new_die (DW_TAG_variable, context_die, decl);
12799
12800   if (origin != NULL)
12801     add_abstract_origin_attribute (var_die, origin);
12802
12803   /* Loop unrolling can create multiple blocks that refer to the same
12804      static variable, so we must test for the DW_AT_declaration flag.
12805
12806      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12807      copy decls and set the DECL_ABSTRACT flag on them instead of
12808      sharing them.
12809
12810      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12811
12812      ??? The declare_in_namespace support causes us to get two DIEs for one
12813      variable, both of which are declarations.  We want to avoid considering
12814      one to be a specification, so we must test that this DIE is not a
12815      declaration.  */
12816   else if (old_die && TREE_STATIC (decl) && ! declaration
12817            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12818     {
12819       /* This is a definition of a C++ class level static.  */
12820       add_AT_specification (var_die, old_die);
12821       if (DECL_NAME (decl))
12822         {
12823           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12824           struct dwarf_file_data * file_index = lookup_filename (s.file);
12825
12826           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12827             add_AT_file (var_die, DW_AT_decl_file, file_index);
12828
12829           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12830             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12831         }
12832     }
12833   else
12834     {
12835       tree type = TREE_TYPE (decl);
12836       if ((TREE_CODE (decl) == PARM_DECL
12837            || TREE_CODE (decl) == RESULT_DECL)
12838           && DECL_BY_REFERENCE (decl))
12839         type = TREE_TYPE (type);
12840
12841       add_name_and_src_coords_attributes (var_die, decl);
12842       add_type_attribute (var_die, type, TREE_READONLY (decl),
12843                           TREE_THIS_VOLATILE (decl), context_die);
12844
12845       if (TREE_PUBLIC (decl))
12846         add_AT_flag (var_die, DW_AT_external, 1);
12847
12848       if (DECL_ARTIFICIAL (decl))
12849         add_AT_flag (var_die, DW_AT_artificial, 1);
12850
12851       if (TREE_PROTECTED (decl))
12852         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12853       else if (TREE_PRIVATE (decl))
12854         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12855     }
12856
12857   if (declaration)
12858     add_AT_flag (var_die, DW_AT_declaration, 1);
12859
12860   if (DECL_ABSTRACT (decl) || declaration)
12861     equate_decl_number_to_die (decl, var_die);
12862
12863   if (! declaration && ! DECL_ABSTRACT (decl))
12864     {
12865       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12866       add_pubname (decl, var_die);
12867     }
12868   else
12869     tree_add_const_value_attribute (var_die, decl);
12870 }
12871
12872 /* Generate a DIE to represent a label identifier.  */
12873
12874 static void
12875 gen_label_die (tree decl, dw_die_ref context_die)
12876 {
12877   tree origin = decl_ultimate_origin (decl);
12878   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12879   rtx insn;
12880   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12881
12882   if (origin != NULL)
12883     add_abstract_origin_attribute (lbl_die, origin);
12884   else
12885     add_name_and_src_coords_attributes (lbl_die, decl);
12886
12887   if (DECL_ABSTRACT (decl))
12888     equate_decl_number_to_die (decl, lbl_die);
12889   else
12890     {
12891       insn = DECL_RTL_IF_SET (decl);
12892
12893       /* Deleted labels are programmer specified labels which have been
12894          eliminated because of various optimizations.  We still emit them
12895          here so that it is possible to put breakpoints on them.  */
12896       if (insn
12897           && (LABEL_P (insn)
12898               || ((NOTE_P (insn)
12899                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12900         {
12901           /* When optimization is enabled (via -O) some parts of the compiler
12902              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12903              represent source-level labels which were explicitly declared by
12904              the user.  This really shouldn't be happening though, so catch
12905              it if it ever does happen.  */
12906           gcc_assert (!INSN_DELETED_P (insn));
12907
12908           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12909           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12910         }
12911     }
12912 }
12913
12914 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12915    attributes to the DIE for a block STMT, to describe where the inlined
12916    function was called from.  This is similar to add_src_coords_attributes.  */
12917
12918 static inline void
12919 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12920 {
12921   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12922
12923   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12924   add_AT_unsigned (die, DW_AT_call_line, s.line);
12925 }
12926
12927
12928 /* If STMT's abstract origin is a function declaration and STMT's
12929    first subblock's abstract origin is the function's outermost block,
12930    then we're looking at the main entry point.  */
12931 static bool
12932 is_inlined_entry_point (const_tree stmt)
12933 {
12934   tree decl, block;
12935
12936   if (!stmt || TREE_CODE (stmt) != BLOCK)
12937     return false;
12938
12939   decl = block_ultimate_origin (stmt);
12940
12941   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12942     return false;
12943
12944   block = BLOCK_SUBBLOCKS (stmt);
12945
12946   if (block)
12947     {
12948       if (TREE_CODE (block) != BLOCK)
12949         return false;
12950
12951       block = block_ultimate_origin (block);
12952     }
12953
12954   return block == DECL_INITIAL (decl);
12955 }
12956
12957 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12958    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12959
12960 static inline void
12961 add_high_low_attributes (tree stmt, dw_die_ref die)
12962 {
12963   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12964
12965   if (BLOCK_FRAGMENT_CHAIN (stmt))
12966     {
12967       tree chain;
12968
12969       if (is_inlined_entry_point (stmt))
12970         {
12971           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12972                                        BLOCK_NUMBER (stmt));
12973           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12974         }
12975
12976       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12977
12978       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12979       do
12980         {
12981           add_ranges (chain);
12982           chain = BLOCK_FRAGMENT_CHAIN (chain);
12983         }
12984       while (chain);
12985       add_ranges (NULL);
12986     }
12987   else
12988     {
12989       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12990                                    BLOCK_NUMBER (stmt));
12991       add_AT_lbl_id (die, DW_AT_low_pc, label);
12992       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12993                                    BLOCK_NUMBER (stmt));
12994       add_AT_lbl_id (die, DW_AT_high_pc, label);
12995     }
12996 }
12997
12998 /* Generate a DIE for a lexical block.  */
12999
13000 static void
13001 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13002 {
13003   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13004
13005   if (! BLOCK_ABSTRACT (stmt))
13006     add_high_low_attributes (stmt, stmt_die);
13007
13008   decls_for_scope (stmt, stmt_die, depth);
13009 }
13010
13011 /* Generate a DIE for an inlined subprogram.  */
13012
13013 static void
13014 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13015 {
13016   tree decl = block_ultimate_origin (stmt);
13017
13018   /* Emit info for the abstract instance first, if we haven't yet.  We
13019      must emit this even if the block is abstract, otherwise when we
13020      emit the block below (or elsewhere), we may end up trying to emit
13021      a die whose origin die hasn't been emitted, and crashing.  */
13022   dwarf2out_abstract_function (decl);
13023
13024   if (! BLOCK_ABSTRACT (stmt))
13025     {
13026       dw_die_ref subr_die
13027         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13028
13029       add_abstract_origin_attribute (subr_die, decl);
13030       add_high_low_attributes (stmt, subr_die);
13031       add_call_src_coords_attributes (stmt, subr_die);
13032
13033       decls_for_scope (stmt, subr_die, depth);
13034       current_function_has_inlines = 1;
13035     }
13036   else
13037     /* We may get here if we're the outer block of function A that was
13038        inlined into function B that was inlined into function C.  When
13039        generating debugging info for C, dwarf2out_abstract_function(B)
13040        would mark all inlined blocks as abstract, including this one.
13041        So, we wouldn't (and shouldn't) expect labels to be generated
13042        for this one.  Instead, just emit debugging info for
13043        declarations within the block.  This is particularly important
13044        in the case of initializers of arguments passed from B to us:
13045        if they're statement expressions containing declarations, we
13046        wouldn't generate dies for their abstract variables, and then,
13047        when generating dies for the real variables, we'd die (pun
13048        intended :-)  */
13049     gen_lexical_block_die (stmt, context_die, depth);
13050 }
13051
13052 /* Generate a DIE for a field in a record, or structure.  */
13053
13054 static void
13055 gen_field_die (tree decl, dw_die_ref context_die)
13056 {
13057   dw_die_ref decl_die;
13058
13059   if (TREE_TYPE (decl) == error_mark_node)
13060     return;
13061
13062   decl_die = new_die (DW_TAG_member, context_die, decl);
13063   add_name_and_src_coords_attributes (decl_die, decl);
13064   add_type_attribute (decl_die, member_declared_type (decl),
13065                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13066                       context_die);
13067
13068   if (DECL_BIT_FIELD_TYPE (decl))
13069     {
13070       add_byte_size_attribute (decl_die, decl);
13071       add_bit_size_attribute (decl_die, decl);
13072       add_bit_offset_attribute (decl_die, decl);
13073     }
13074
13075   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13076     add_data_member_location_attribute (decl_die, decl);
13077
13078   if (DECL_ARTIFICIAL (decl))
13079     add_AT_flag (decl_die, DW_AT_artificial, 1);
13080
13081   if (TREE_PROTECTED (decl))
13082     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13083   else if (TREE_PRIVATE (decl))
13084     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13085
13086   /* Equate decl number to die, so that we can look up this decl later on.  */
13087   equate_decl_number_to_die (decl, decl_die);
13088 }
13089
13090 #if 0
13091 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13092    Use modified_type_die instead.
13093    We keep this code here just in case these types of DIEs may be needed to
13094    represent certain things in other languages (e.g. Pascal) someday.  */
13095
13096 static void
13097 gen_pointer_type_die (tree type, dw_die_ref context_die)
13098 {
13099   dw_die_ref ptr_die
13100     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13101
13102   equate_type_number_to_die (type, ptr_die);
13103   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13104   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13105 }
13106
13107 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13108    Use modified_type_die instead.
13109    We keep this code here just in case these types of DIEs may be needed to
13110    represent certain things in other languages (e.g. Pascal) someday.  */
13111
13112 static void
13113 gen_reference_type_die (tree type, dw_die_ref context_die)
13114 {
13115   dw_die_ref ref_die
13116     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13117
13118   equate_type_number_to_die (type, ref_die);
13119   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13120   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13121 }
13122 #endif
13123
13124 /* Generate a DIE for a pointer to a member type.  */
13125
13126 static void
13127 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13128 {
13129   dw_die_ref ptr_die
13130     = new_die (DW_TAG_ptr_to_member_type,
13131                scope_die_for (type, context_die), type);
13132
13133   equate_type_number_to_die (type, ptr_die);
13134   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13135                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13136   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13137 }
13138
13139 /* Generate the DIE for the compilation unit.  */
13140
13141 static dw_die_ref
13142 gen_compile_unit_die (const char *filename)
13143 {
13144   dw_die_ref die;
13145   char producer[250];
13146   const char *language_string = lang_hooks.name;
13147   int language;
13148
13149   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13150
13151   if (filename)
13152     {
13153       add_name_attribute (die, filename);
13154       /* Don't add cwd for <built-in>.  */
13155       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13156         add_comp_dir_attribute (die);
13157     }
13158
13159   sprintf (producer, "%s %s", language_string, version_string);
13160
13161 #ifdef MIPS_DEBUGGING_INFO
13162   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13163      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13164      not appear in the producer string, the debugger reaches the conclusion
13165      that the object file is stripped and has no debugging information.
13166      To get the MIPS/SGI debugger to believe that there is debugging
13167      information in the object file, we add a -g to the producer string.  */
13168   if (debug_info_level > DINFO_LEVEL_TERSE)
13169     strcat (producer, " -g");
13170 #endif
13171
13172   add_AT_string (die, DW_AT_producer, producer);
13173
13174   if (strcmp (language_string, "GNU C++") == 0)
13175     language = DW_LANG_C_plus_plus;
13176   else if (strcmp (language_string, "GNU Ada") == 0)
13177     language = DW_LANG_Ada95;
13178   else if (strcmp (language_string, "GNU F77") == 0)
13179     language = DW_LANG_Fortran77;
13180   else if (strcmp (language_string, "GNU Fortran") == 0)
13181     language = DW_LANG_Fortran95;
13182   else if (strcmp (language_string, "GNU Pascal") == 0)
13183     language = DW_LANG_Pascal83;
13184   else if (strcmp (language_string, "GNU Java") == 0)
13185     language = DW_LANG_Java;
13186   else if (strcmp (language_string, "GNU Objective-C") == 0)
13187     language = DW_LANG_ObjC;
13188   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13189     language = DW_LANG_ObjC_plus_plus;
13190   else
13191     language = DW_LANG_C89;
13192
13193   add_AT_unsigned (die, DW_AT_language, language);
13194   return die;
13195 }
13196
13197 /* Generate the DIE for a base class.  */
13198
13199 static void
13200 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13201 {
13202   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13203
13204   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13205   add_data_member_location_attribute (die, binfo);
13206
13207   if (BINFO_VIRTUAL_P (binfo))
13208     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13209
13210   if (access == access_public_node)
13211     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13212   else if (access == access_protected_node)
13213     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13214 }
13215
13216 /* Generate a DIE for a class member.  */
13217
13218 static void
13219 gen_member_die (tree type, dw_die_ref context_die)
13220 {
13221   tree member;
13222   tree binfo = TYPE_BINFO (type);
13223   dw_die_ref child;
13224
13225   /* If this is not an incomplete type, output descriptions of each of its
13226      members. Note that as we output the DIEs necessary to represent the
13227      members of this record or union type, we will also be trying to output
13228      DIEs to represent the *types* of those members. However the `type'
13229      function (above) will specifically avoid generating type DIEs for member
13230      types *within* the list of member DIEs for this (containing) type except
13231      for those types (of members) which are explicitly marked as also being
13232      members of this (containing) type themselves.  The g++ front- end can
13233      force any given type to be treated as a member of some other (containing)
13234      type by setting the TYPE_CONTEXT of the given (member) type to point to
13235      the TREE node representing the appropriate (containing) type.  */
13236
13237   /* First output info about the base classes.  */
13238   if (binfo)
13239     {
13240       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13241       int i;
13242       tree base;
13243
13244       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13245         gen_inheritance_die (base,
13246                              (accesses ? VEC_index (tree, accesses, i)
13247                               : access_public_node), context_die);
13248     }
13249
13250   /* Now output info about the data members and type members.  */
13251   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13252     {
13253       /* If we thought we were generating minimal debug info for TYPE
13254          and then changed our minds, some of the member declarations
13255          may have already been defined.  Don't define them again, but
13256          do put them in the right order.  */
13257
13258       child = lookup_decl_die (member);
13259       if (child)
13260         splice_child_die (context_die, child);
13261       else
13262         gen_decl_die (member, context_die);
13263     }
13264
13265   /* Now output info about the function members (if any).  */
13266   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13267     {
13268       /* Don't include clones in the member list.  */
13269       if (DECL_ABSTRACT_ORIGIN (member))
13270         continue;
13271
13272       child = lookup_decl_die (member);
13273       if (child)
13274         splice_child_die (context_die, child);
13275       else
13276         gen_decl_die (member, context_die);
13277     }
13278 }
13279
13280 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13281    is set, we pretend that the type was never defined, so we only get the
13282    member DIEs needed by later specification DIEs.  */
13283
13284 static void
13285 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13286                                 enum debug_info_usage usage)
13287 {
13288   dw_die_ref type_die = lookup_type_die (type);
13289   dw_die_ref scope_die = 0;
13290   int nested = 0;
13291   int complete = (TYPE_SIZE (type)
13292                   && (! TYPE_STUB_DECL (type)
13293                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13294   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13295   complete = complete && should_emit_struct_debug (type, usage);
13296
13297   if (type_die && ! complete)
13298     return;
13299
13300   if (TYPE_CONTEXT (type) != NULL_TREE
13301       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13302           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13303     nested = 1;
13304
13305   scope_die = scope_die_for (type, context_die);
13306
13307   if (! type_die || (nested && scope_die == comp_unit_die))
13308     /* First occurrence of type or toplevel definition of nested class.  */
13309     {
13310       dw_die_ref old_die = type_die;
13311
13312       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13313                           ? record_type_tag (type) : DW_TAG_union_type,
13314                           scope_die, type);
13315       equate_type_number_to_die (type, type_die);
13316       if (old_die)
13317         add_AT_specification (type_die, old_die);
13318       else
13319         add_name_attribute (type_die, type_tag (type));
13320     }
13321   else
13322     remove_AT (type_die, DW_AT_declaration);
13323
13324   /* If this type has been completed, then give it a byte_size attribute and
13325      then give a list of members.  */
13326   if (complete && !ns_decl)
13327     {
13328       /* Prevent infinite recursion in cases where the type of some member of
13329          this type is expressed in terms of this type itself.  */
13330       TREE_ASM_WRITTEN (type) = 1;
13331       add_byte_size_attribute (type_die, type);
13332       if (TYPE_STUB_DECL (type) != NULL_TREE)
13333         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13334
13335       /* If the first reference to this type was as the return type of an
13336          inline function, then it may not have a parent.  Fix this now.  */
13337       if (type_die->die_parent == NULL)
13338         add_child_die (scope_die, type_die);
13339
13340       push_decl_scope (type);
13341       gen_member_die (type, type_die);
13342       pop_decl_scope ();
13343
13344       /* GNU extension: Record what type our vtable lives in.  */
13345       if (TYPE_VFIELD (type))
13346         {
13347           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13348
13349           gen_type_die (vtype, context_die);
13350           add_AT_die_ref (type_die, DW_AT_containing_type,
13351                           lookup_type_die (vtype));
13352         }
13353     }
13354   else
13355     {
13356       add_AT_flag (type_die, DW_AT_declaration, 1);
13357
13358       /* We don't need to do this for function-local types.  */
13359       if (TYPE_STUB_DECL (type)
13360           && ! decl_function_context (TYPE_STUB_DECL (type)))
13361         VEC_safe_push (tree, gc, incomplete_types, type);
13362     }
13363
13364   if (get_AT (type_die, DW_AT_name))
13365     add_pubtype (type, type_die);
13366 }
13367
13368 /* Generate a DIE for a subroutine _type_.  */
13369
13370 static void
13371 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13372 {
13373   tree return_type = TREE_TYPE (type);
13374   dw_die_ref subr_die
13375     = new_die (DW_TAG_subroutine_type,
13376                scope_die_for (type, context_die), type);
13377
13378   equate_type_number_to_die (type, subr_die);
13379   add_prototyped_attribute (subr_die, type);
13380   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13381   gen_formal_types_die (type, subr_die);
13382
13383   if (get_AT (subr_die, DW_AT_name))
13384     add_pubtype (type, subr_die);
13385 }
13386
13387 /* Generate a DIE for a type definition.  */
13388
13389 static void
13390 gen_typedef_die (tree decl, dw_die_ref context_die)
13391 {
13392   dw_die_ref type_die;
13393   tree origin;
13394
13395   if (TREE_ASM_WRITTEN (decl))
13396     return;
13397
13398   TREE_ASM_WRITTEN (decl) = 1;
13399   type_die = new_die (DW_TAG_typedef, context_die, decl);
13400   origin = decl_ultimate_origin (decl);
13401   if (origin != NULL)
13402     add_abstract_origin_attribute (type_die, origin);
13403   else
13404     {
13405       tree type;
13406
13407       add_name_and_src_coords_attributes (type_die, decl);
13408       if (DECL_ORIGINAL_TYPE (decl))
13409         {
13410           type = DECL_ORIGINAL_TYPE (decl);
13411
13412           gcc_assert (type != TREE_TYPE (decl));
13413           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13414         }
13415       else
13416         type = TREE_TYPE (decl);
13417
13418       add_type_attribute (type_die, type, TREE_READONLY (decl),
13419                           TREE_THIS_VOLATILE (decl), context_die);
13420     }
13421
13422   if (DECL_ABSTRACT (decl))
13423     equate_decl_number_to_die (decl, type_die);
13424
13425   if (get_AT (type_die, DW_AT_name))
13426     add_pubtype (decl, type_die);
13427 }
13428
13429 /* Generate a type description DIE.  */
13430
13431 static void
13432 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13433                                 enum debug_info_usage usage)
13434 {
13435   int need_pop;
13436   struct array_descr_info info;
13437
13438   if (type == NULL_TREE || type == error_mark_node)
13439     return;
13440
13441   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13442       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13443     {
13444       if (TREE_ASM_WRITTEN (type))
13445         return;
13446
13447       /* Prevent broken recursion; we can't hand off to the same type.  */
13448       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13449
13450       TREE_ASM_WRITTEN (type) = 1;
13451       gen_decl_die (TYPE_NAME (type), context_die);
13452       return;
13453     }
13454
13455   /* If this is an array type with hidden descriptor, handle it first.  */
13456   if (!TREE_ASM_WRITTEN (type)
13457       && lang_hooks.types.get_array_descr_info
13458       && lang_hooks.types.get_array_descr_info (type, &info))
13459     {
13460       gen_descr_array_type_die (type, &info, context_die);
13461       TREE_ASM_WRITTEN (type) = 1;
13462       return;
13463     }
13464
13465   /* We are going to output a DIE to represent the unqualified version
13466      of this type (i.e. without any const or volatile qualifiers) so
13467      get the main variant (i.e. the unqualified version) of this type
13468      now.  (Vectors are special because the debugging info is in the
13469      cloned type itself).  */
13470   if (TREE_CODE (type) != VECTOR_TYPE)
13471     type = type_main_variant (type);
13472
13473   if (TREE_ASM_WRITTEN (type))
13474     return;
13475
13476   switch (TREE_CODE (type))
13477     {
13478     case ERROR_MARK:
13479       break;
13480
13481     case POINTER_TYPE:
13482     case REFERENCE_TYPE:
13483       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13484          ensures that the gen_type_die recursion will terminate even if the
13485          type is recursive.  Recursive types are possible in Ada.  */
13486       /* ??? We could perhaps do this for all types before the switch
13487          statement.  */
13488       TREE_ASM_WRITTEN (type) = 1;
13489
13490       /* For these types, all that is required is that we output a DIE (or a
13491          set of DIEs) to represent the "basis" type.  */
13492       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13493                                 DINFO_USAGE_IND_USE);
13494       break;
13495
13496     case OFFSET_TYPE:
13497       /* This code is used for C++ pointer-to-data-member types.
13498          Output a description of the relevant class type.  */
13499       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13500                                         DINFO_USAGE_IND_USE);
13501
13502       /* Output a description of the type of the object pointed to.  */
13503       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13504                                         DINFO_USAGE_IND_USE);
13505
13506       /* Now output a DIE to represent this pointer-to-data-member type
13507          itself.  */
13508       gen_ptr_to_mbr_type_die (type, context_die);
13509       break;
13510
13511     case FUNCTION_TYPE:
13512       /* Force out return type (in case it wasn't forced out already).  */
13513       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13514                                         DINFO_USAGE_DIR_USE);
13515       gen_subroutine_type_die (type, context_die);
13516       break;
13517
13518     case METHOD_TYPE:
13519       /* Force out return type (in case it wasn't forced out already).  */
13520       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13521                                         DINFO_USAGE_DIR_USE);
13522       gen_subroutine_type_die (type, context_die);
13523       break;
13524
13525     case ARRAY_TYPE:
13526       gen_array_type_die (type, context_die);
13527       break;
13528
13529     case VECTOR_TYPE:
13530       gen_array_type_die (type, context_die);
13531       break;
13532
13533     case ENUMERAL_TYPE:
13534     case RECORD_TYPE:
13535     case UNION_TYPE:
13536     case QUAL_UNION_TYPE:
13537       /* If this is a nested type whose containing class hasn't been written
13538          out yet, writing it out will cover this one, too.  This does not apply
13539          to instantiations of member class templates; they need to be added to
13540          the containing class as they are generated.  FIXME: This hurts the
13541          idea of combining type decls from multiple TUs, since we can't predict
13542          what set of template instantiations we'll get.  */
13543       if (TYPE_CONTEXT (type)
13544           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13545           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13546         {
13547           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13548
13549           if (TREE_ASM_WRITTEN (type))
13550             return;
13551
13552           /* If that failed, attach ourselves to the stub.  */
13553           push_decl_scope (TYPE_CONTEXT (type));
13554           context_die = lookup_type_die (TYPE_CONTEXT (type));
13555           need_pop = 1;
13556         }
13557       else
13558         {
13559           declare_in_namespace (type, context_die);
13560           need_pop = 0;
13561         }
13562
13563       if (TREE_CODE (type) == ENUMERAL_TYPE)
13564         {
13565           /* This might have been written out by the call to
13566              declare_in_namespace.  */
13567           if (!TREE_ASM_WRITTEN (type))
13568             gen_enumeration_type_die (type, context_die);
13569         }
13570       else
13571         gen_struct_or_union_type_die (type, context_die, usage);
13572
13573       if (need_pop)
13574         pop_decl_scope ();
13575
13576       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13577          it up if it is ever completed.  gen_*_type_die will set it for us
13578          when appropriate.  */
13579       return;
13580
13581     case VOID_TYPE:
13582     case INTEGER_TYPE:
13583     case REAL_TYPE:
13584     case FIXED_POINT_TYPE:
13585     case COMPLEX_TYPE:
13586     case BOOLEAN_TYPE:
13587       /* No DIEs needed for fundamental types.  */
13588       break;
13589
13590     case LANG_TYPE:
13591       /* No Dwarf representation currently defined.  */
13592       break;
13593
13594     default:
13595       gcc_unreachable ();
13596     }
13597
13598   TREE_ASM_WRITTEN (type) = 1;
13599 }
13600
13601 static void
13602 gen_type_die (tree type, dw_die_ref context_die)
13603 {
13604   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13605 }
13606
13607 /* Generate a DIE for a tagged type instantiation.  */
13608
13609 static void
13610 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13611 {
13612   if (type == NULL_TREE || type == error_mark_node)
13613     return;
13614
13615   /* We are going to output a DIE to represent the unqualified version of
13616      this type (i.e. without any const or volatile qualifiers) so make sure
13617      that we have the main variant (i.e. the unqualified version) of this
13618      type now.  */
13619   gcc_assert (type == type_main_variant (type));
13620
13621   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13622      an instance of an unresolved type.  */
13623
13624   switch (TREE_CODE (type))
13625     {
13626     case ERROR_MARK:
13627       break;
13628
13629     case ENUMERAL_TYPE:
13630       gen_inlined_enumeration_type_die (type, context_die);
13631       break;
13632
13633     case RECORD_TYPE:
13634       gen_inlined_structure_type_die (type, context_die);
13635       break;
13636
13637     case UNION_TYPE:
13638     case QUAL_UNION_TYPE:
13639       gen_inlined_union_type_die (type, context_die);
13640       break;
13641
13642     default:
13643       gcc_unreachable ();
13644     }
13645 }
13646
13647 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13648    things which are local to the given block.  */
13649
13650 static void
13651 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13652 {
13653   int must_output_die = 0;
13654   tree origin;
13655   tree decl;
13656   enum tree_code origin_code;
13657
13658   /* Ignore blocks that are NULL.  */
13659   if (stmt == NULL_TREE)
13660     return;
13661
13662   /* If the block is one fragment of a non-contiguous block, do not
13663      process the variables, since they will have been done by the
13664      origin block.  Do process subblocks.  */
13665   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13666     {
13667       tree sub;
13668
13669       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13670         gen_block_die (sub, context_die, depth + 1);
13671
13672       return;
13673     }
13674
13675   /* Determine the "ultimate origin" of this block.  This block may be an
13676      inlined instance of an inlined instance of inline function, so we have
13677      to trace all of the way back through the origin chain to find out what
13678      sort of node actually served as the original seed for the creation of
13679      the current block.  */
13680   origin = block_ultimate_origin (stmt);
13681   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13682
13683   /* Determine if we need to output any Dwarf DIEs at all to represent this
13684      block.  */
13685   if (origin_code == FUNCTION_DECL)
13686     /* The outer scopes for inlinings *must* always be represented.  We
13687        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13688     must_output_die = 1;
13689   else
13690     {
13691       /* In the case where the current block represents an inlining of the
13692          "body block" of an inline function, we must *NOT* output any DIE for
13693          this block because we have already output a DIE to represent the whole
13694          inlined function scope and the "body block" of any function doesn't
13695          really represent a different scope according to ANSI C rules.  So we
13696          check here to make sure that this block does not represent a "body
13697          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13698       if (! is_body_block (origin ? origin : stmt))
13699         {
13700           /* Determine if this block directly contains any "significant"
13701              local declarations which we will need to output DIEs for.  */
13702           if (debug_info_level > DINFO_LEVEL_TERSE)
13703             /* We are not in terse mode so *any* local declaration counts
13704                as being a "significant" one.  */
13705             must_output_die = (BLOCK_VARS (stmt) != NULL
13706                                && (TREE_USED (stmt)
13707                                    || TREE_ASM_WRITTEN (stmt)
13708                                    || BLOCK_ABSTRACT (stmt)));
13709           else
13710             /* We are in terse mode, so only local (nested) function
13711                definitions count as "significant" local declarations.  */
13712             for (decl = BLOCK_VARS (stmt);
13713                  decl != NULL; decl = TREE_CHAIN (decl))
13714               if (TREE_CODE (decl) == FUNCTION_DECL
13715                   && DECL_INITIAL (decl))
13716                 {
13717                   must_output_die = 1;
13718                   break;
13719                 }
13720         }
13721     }
13722
13723   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13724      DIE for any block which contains no significant local declarations at
13725      all.  Rather, in such cases we just call `decls_for_scope' so that any
13726      needed Dwarf info for any sub-blocks will get properly generated. Note
13727      that in terse mode, our definition of what constitutes a "significant"
13728      local declaration gets restricted to include only inlined function
13729      instances and local (nested) function definitions.  */
13730   if (must_output_die)
13731     {
13732       if (origin_code == FUNCTION_DECL)
13733         gen_inlined_subroutine_die (stmt, context_die, depth);
13734       else
13735         gen_lexical_block_die (stmt, context_die, depth);
13736     }
13737   else
13738     decls_for_scope (stmt, context_die, depth);
13739 }
13740
13741 /* Generate all of the decls declared within a given scope and (recursively)
13742    all of its sub-blocks.  */
13743
13744 static void
13745 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13746 {
13747   tree decl;
13748   tree subblocks;
13749
13750   /* Ignore NULL blocks.  */
13751   if (stmt == NULL_TREE)
13752     return;
13753
13754   if (TREE_USED (stmt))
13755     {
13756       /* Output the DIEs to represent all of the data objects and typedefs
13757          declared directly within this block but not within any nested
13758          sub-blocks.  Also, nested function and tag DIEs have been
13759          generated with a parent of NULL; fix that up now.  */
13760       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13761         {
13762           dw_die_ref die;
13763
13764           if (TREE_CODE (decl) == FUNCTION_DECL)
13765             die = lookup_decl_die (decl);
13766           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13767             die = lookup_type_die (TREE_TYPE (decl));
13768           else
13769             die = NULL;
13770
13771           if (die != NULL && die->die_parent == NULL)
13772             add_child_die (context_die, die);
13773           /* Do not produce debug information for static variables since
13774              these might be optimized out.  We are called for these later
13775              in varpool_analyze_pending_decls.
13776
13777              But *do* produce it for Fortran COMMON variables because,
13778              even though they are static, their names can differ depending
13779              on the scope, which we need to preserve.  */
13780           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13781               && !(is_fortran () && TREE_PUBLIC (decl)))
13782             ;
13783           else
13784             gen_decl_die (decl, context_die);
13785         }
13786     }
13787
13788   /* If we're at -g1, we're not interested in subblocks.  */
13789   if (debug_info_level <= DINFO_LEVEL_TERSE)
13790     return;
13791
13792   /* Output the DIEs to represent all sub-blocks (and the items declared
13793      therein) of this block.  */
13794   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13795        subblocks != NULL;
13796        subblocks = BLOCK_CHAIN (subblocks))
13797     gen_block_die (subblocks, context_die, depth + 1);
13798 }
13799
13800 /* Is this a typedef we can avoid emitting?  */
13801
13802 static inline int
13803 is_redundant_typedef (const_tree decl)
13804 {
13805   if (TYPE_DECL_IS_STUB (decl))
13806     return 1;
13807
13808   if (DECL_ARTIFICIAL (decl)
13809       && DECL_CONTEXT (decl)
13810       && is_tagged_type (DECL_CONTEXT (decl))
13811       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13812       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13813     /* Also ignore the artificial member typedef for the class name.  */
13814     return 1;
13815
13816   return 0;
13817 }
13818
13819 /* Returns the DIE for decl.  A DIE will always be returned.  */
13820
13821 static dw_die_ref
13822 force_decl_die (tree decl)
13823 {
13824   dw_die_ref decl_die;
13825   unsigned saved_external_flag;
13826   tree save_fn = NULL_TREE;
13827   decl_die = lookup_decl_die (decl);
13828   if (!decl_die)
13829     {
13830       dw_die_ref context_die;
13831       tree decl_context = DECL_CONTEXT (decl);
13832       if (decl_context)
13833         {
13834           /* Find die that represents this context.  */
13835           if (TYPE_P (decl_context))
13836             context_die = force_type_die (decl_context);
13837           else
13838             context_die = force_decl_die (decl_context);
13839         }
13840       else
13841         context_die = comp_unit_die;
13842
13843       decl_die = lookup_decl_die (decl);
13844       if (decl_die)
13845         return decl_die;
13846
13847       switch (TREE_CODE (decl))
13848         {
13849         case FUNCTION_DECL:
13850           /* Clear current_function_decl, so that gen_subprogram_die thinks
13851              that this is a declaration. At this point, we just want to force
13852              declaration die.  */
13853           save_fn = current_function_decl;
13854           current_function_decl = NULL_TREE;
13855           gen_subprogram_die (decl, context_die);
13856           current_function_decl = save_fn;
13857           break;
13858
13859         case VAR_DECL:
13860           /* Set external flag to force declaration die. Restore it after
13861            gen_decl_die() call.  */
13862           saved_external_flag = DECL_EXTERNAL (decl);
13863           DECL_EXTERNAL (decl) = 1;
13864           gen_decl_die (decl, context_die);
13865           DECL_EXTERNAL (decl) = saved_external_flag;
13866           break;
13867
13868         case NAMESPACE_DECL:
13869           dwarf2out_decl (decl);
13870           break;
13871
13872         default:
13873           gcc_unreachable ();
13874         }
13875
13876       /* We should be able to find the DIE now.  */
13877       if (!decl_die)
13878         decl_die = lookup_decl_die (decl);
13879       gcc_assert (decl_die);
13880     }
13881
13882   return decl_die;
13883 }
13884
13885 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13886    always returned.  */
13887
13888 static dw_die_ref
13889 force_type_die (tree type)
13890 {
13891   dw_die_ref type_die;
13892
13893   type_die = lookup_type_die (type);
13894   if (!type_die)
13895     {
13896       dw_die_ref context_die;
13897       if (TYPE_CONTEXT (type))
13898         {
13899           if (TYPE_P (TYPE_CONTEXT (type)))
13900             context_die = force_type_die (TYPE_CONTEXT (type));
13901           else
13902             context_die = force_decl_die (TYPE_CONTEXT (type));
13903         }
13904       else
13905         context_die = comp_unit_die;
13906
13907       type_die = modified_type_die (type, TYPE_READONLY (type),
13908                                     TYPE_VOLATILE (type), context_die);
13909       gcc_assert (type_die);
13910     }
13911   return type_die;
13912 }
13913
13914 /* Force out any required namespaces to be able to output DECL,
13915    and return the new context_die for it, if it's changed.  */
13916
13917 static dw_die_ref
13918 setup_namespace_context (tree thing, dw_die_ref context_die)
13919 {
13920   tree context = (DECL_P (thing)
13921                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13922   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13923     /* Force out the namespace.  */
13924     context_die = force_decl_die (context);
13925
13926   return context_die;
13927 }
13928
13929 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13930    type) within its namespace, if appropriate.
13931
13932    For compatibility with older debuggers, namespace DIEs only contain
13933    declarations; all definitions are emitted at CU scope.  */
13934
13935 static void
13936 declare_in_namespace (tree thing, dw_die_ref context_die)
13937 {
13938   dw_die_ref ns_context;
13939
13940   if (debug_info_level <= DINFO_LEVEL_TERSE)
13941     return;
13942
13943   /* If this decl is from an inlined function, then don't try to emit it in its
13944      namespace, as we will get confused.  It would have already been emitted
13945      when the abstract instance of the inline function was emitted anyways.  */
13946   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13947     return;
13948
13949   ns_context = setup_namespace_context (thing, context_die);
13950
13951   if (ns_context != context_die)
13952     {
13953       if (DECL_P (thing))
13954         gen_decl_die (thing, ns_context);
13955       else
13956         gen_type_die (thing, ns_context);
13957     }
13958 }
13959
13960 /* Generate a DIE for a namespace or namespace alias.  */
13961
13962 static void
13963 gen_namespace_die (tree decl)
13964 {
13965   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13966
13967   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13968      they are an alias of.  */
13969   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13970     {
13971       /* Output a real namespace.  */
13972       dw_die_ref namespace_die
13973         = new_die (DW_TAG_namespace, context_die, decl);
13974       add_name_and_src_coords_attributes (namespace_die, decl);
13975       equate_decl_number_to_die (decl, namespace_die);
13976     }
13977   else
13978     {
13979       /* Output a namespace alias.  */
13980
13981       /* Force out the namespace we are an alias of, if necessary.  */
13982       dw_die_ref origin_die
13983         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13984
13985       /* Now create the namespace alias DIE.  */
13986       dw_die_ref namespace_die
13987         = new_die (DW_TAG_imported_declaration, context_die, decl);
13988       add_name_and_src_coords_attributes (namespace_die, decl);
13989       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13990       equate_decl_number_to_die (decl, namespace_die);
13991     }
13992 }
13993
13994 /* Generate Dwarf debug information for a decl described by DECL.  */
13995
13996 static void
13997 gen_decl_die (tree decl, dw_die_ref context_die)
13998 {
13999   tree origin;
14000
14001   if (DECL_P (decl) && DECL_IGNORED_P (decl))
14002     return;
14003
14004   switch (TREE_CODE (decl))
14005     {
14006     case ERROR_MARK:
14007       break;
14008
14009     case CONST_DECL:
14010       /* The individual enumerators of an enum type get output when we output
14011          the Dwarf representation of the relevant enum type itself.  */
14012       break;
14013
14014     case FUNCTION_DECL:
14015       /* Don't output any DIEs to represent mere function declarations,
14016          unless they are class members or explicit block externs.  */
14017       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14018           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14019         break;
14020
14021 #if 0
14022       /* FIXME */
14023       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14024          on local redeclarations of global functions.  That seems broken.  */
14025       if (current_function_decl != decl)
14026         /* This is only a declaration.  */;
14027 #endif
14028
14029       /* If we're emitting a clone, emit info for the abstract instance.  */
14030       if (DECL_ORIGIN (decl) != decl)
14031         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14032
14033       /* If we're emitting an out-of-line copy of an inline function,
14034          emit info for the abstract instance and set up to refer to it.  */
14035       else if (cgraph_function_possibly_inlined_p (decl)
14036                && ! DECL_ABSTRACT (decl)
14037                && ! class_or_namespace_scope_p (context_die)
14038                /* dwarf2out_abstract_function won't emit a die if this is just
14039                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14040                   that case, because that works only if we have a die.  */
14041                && DECL_INITIAL (decl) != NULL_TREE)
14042         {
14043           dwarf2out_abstract_function (decl);
14044           set_decl_origin_self (decl);
14045         }
14046
14047       /* Otherwise we're emitting the primary DIE for this decl.  */
14048       else if (debug_info_level > DINFO_LEVEL_TERSE)
14049         {
14050           /* Before we describe the FUNCTION_DECL itself, make sure that we
14051              have described its return type.  */
14052           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14053
14054           /* And its virtual context.  */
14055           if (DECL_VINDEX (decl) != NULL_TREE)
14056             gen_type_die (DECL_CONTEXT (decl), context_die);
14057
14058           /* And its containing type.  */
14059           origin = decl_class_context (decl);
14060           if (origin != NULL_TREE)
14061             gen_type_die_for_member (origin, decl, context_die);
14062
14063           /* And its containing namespace.  */
14064           declare_in_namespace (decl, context_die);
14065         }
14066
14067       /* Now output a DIE to represent the function itself.  */
14068       gen_subprogram_die (decl, context_die);
14069       break;
14070
14071     case TYPE_DECL:
14072       /* If we are in terse mode, don't generate any DIEs to represent any
14073          actual typedefs.  */
14074       if (debug_info_level <= DINFO_LEVEL_TERSE)
14075         break;
14076
14077       /* In the special case of a TYPE_DECL node representing the declaration
14078          of some type tag, if the given TYPE_DECL is marked as having been
14079          instantiated from some other (original) TYPE_DECL node (e.g. one which
14080          was generated within the original definition of an inline function) we
14081          have to generate a special (abbreviated) DW_TAG_structure_type,
14082          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14083       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14084           && is_tagged_type (TREE_TYPE (decl)))
14085         {
14086           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14087           break;
14088         }
14089
14090       if (is_redundant_typedef (decl))
14091         gen_type_die (TREE_TYPE (decl), context_die);
14092       else
14093         /* Output a DIE to represent the typedef itself.  */
14094         gen_typedef_die (decl, context_die);
14095       break;
14096
14097     case LABEL_DECL:
14098       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14099         gen_label_die (decl, context_die);
14100       break;
14101
14102     case VAR_DECL:
14103     case RESULT_DECL:
14104       /* If we are in terse mode, don't generate any DIEs to represent any
14105          variable declarations or definitions.  */
14106       if (debug_info_level <= DINFO_LEVEL_TERSE)
14107         break;
14108
14109       /* If this is the global definition of the Fortran COMMON block, we don't
14110          need to do anything.  Syntactically, the block itself has no identity,
14111          just its constituent identifiers.  */
14112       if (TREE_CODE (decl) == VAR_DECL
14113           && TREE_PUBLIC (decl)
14114           && TREE_STATIC (decl)
14115           && is_fortran ()
14116           && !DECL_HAS_VALUE_EXPR_P (decl))
14117         break;
14118
14119       /* Output any DIEs that are needed to specify the type of this data
14120          object.  */
14121       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14122         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14123       else
14124         gen_type_die (TREE_TYPE (decl), context_die);
14125
14126       /* And its containing type.  */
14127       origin = decl_class_context (decl);
14128       if (origin != NULL_TREE)
14129         gen_type_die_for_member (origin, decl, context_die);
14130
14131       /* And its containing namespace.  */
14132       declare_in_namespace (decl, context_die);
14133
14134       /* Now output the DIE to represent the data object itself.  This gets
14135          complicated because of the possibility that the VAR_DECL really
14136          represents an inlined instance of a formal parameter for an inline
14137          function.  */
14138       origin = decl_ultimate_origin (decl);
14139       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14140         gen_formal_parameter_die (decl, context_die);
14141       else
14142         gen_variable_die (decl, context_die);
14143       break;
14144
14145     case FIELD_DECL:
14146       /* Ignore the nameless fields that are used to skip bits but handle C++
14147          anonymous unions and structs.  */
14148       if (DECL_NAME (decl) != NULL_TREE
14149           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14150           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14151         {
14152           gen_type_die (member_declared_type (decl), context_die);
14153           gen_field_die (decl, context_die);
14154         }
14155       break;
14156
14157     case PARM_DECL:
14158       if (DECL_BY_REFERENCE (decl))
14159         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14160       else
14161         gen_type_die (TREE_TYPE (decl), context_die);
14162       gen_formal_parameter_die (decl, context_die);
14163       break;
14164
14165     case NAMESPACE_DECL:
14166       gen_namespace_die (decl);
14167       break;
14168
14169     default:
14170       /* Probably some frontend-internal decl.  Assume we don't care.  */
14171       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14172       break;
14173     }
14174 }
14175 \f
14176 /* Output debug information for global decl DECL.  Called from toplev.c after
14177    compilation proper has finished.  */
14178
14179 static void
14180 dwarf2out_global_decl (tree decl)
14181 {
14182   /* Output DWARF2 information for file-scope tentative data object
14183      declarations, file-scope (extern) function declarations (which
14184      had no corresponding body) and file-scope tagged type declarations
14185      and definitions which have not yet been forced out.
14186
14187      Ignore the global decl of any Fortran COMMON blocks which also
14188      wind up here though they have already been described in the local
14189      scope for the procedures using them.  */
14190   if (TREE_CODE (decl) == VAR_DECL
14191       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14192     return;
14193
14194   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14195     dwarf2out_decl (decl);
14196 }
14197
14198 /* Output debug information for type decl DECL.  Called from toplev.c
14199    and from language front ends (to record built-in types).  */
14200 static void
14201 dwarf2out_type_decl (tree decl, int local)
14202 {
14203   if (!local)
14204     dwarf2out_decl (decl);
14205 }
14206
14207 /* Output debug information for imported module or decl.  */
14208
14209 static void
14210 dwarf2out_imported_module_or_decl (tree decl, tree context)
14211 {
14212   dw_die_ref imported_die, at_import_die;
14213   dw_die_ref scope_die;
14214   expanded_location xloc;
14215
14216   if (debug_info_level <= DINFO_LEVEL_TERSE)
14217     return;
14218
14219   gcc_assert (decl);
14220
14221   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14222      We need decl DIE for reference and scope die. First, get DIE for the decl
14223      itself.  */
14224
14225   /* Get the scope die for decl context. Use comp_unit_die for global module
14226      or decl. If die is not found for non globals, force new die.  */
14227   if (!context)
14228     scope_die = comp_unit_die;
14229   else if (TYPE_P (context))
14230     {
14231       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14232         return;
14233     scope_die = force_type_die (context);
14234     }
14235   else
14236     scope_die = force_decl_die (context);
14237
14238   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14239   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14240     {
14241       if (is_base_type (TREE_TYPE (decl)))
14242         at_import_die = base_type_die (TREE_TYPE (decl));
14243       else
14244         at_import_die = force_type_die (TREE_TYPE (decl));
14245     }
14246   else
14247     {
14248       at_import_die = lookup_decl_die (decl);
14249       if (!at_import_die)
14250         {
14251           /* If we're trying to avoid duplicate debug info, we may not have
14252              emitted the member decl for this field.  Emit it now.  */
14253           if (TREE_CODE (decl) == FIELD_DECL)
14254             {
14255               tree type = DECL_CONTEXT (decl);
14256               dw_die_ref type_context_die;
14257
14258               if (TYPE_CONTEXT (type))
14259                 if (TYPE_P (TYPE_CONTEXT (type)))
14260                   {
14261                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14262                                                    DINFO_USAGE_DIR_USE))
14263                       return;
14264                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14265                   }
14266               else
14267                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14268               else
14269                 type_context_die = comp_unit_die;
14270               gen_type_die_for_member (type, decl, type_context_die);
14271             }
14272           at_import_die = force_decl_die (decl);
14273         }
14274     }
14275
14276   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14277   if (TREE_CODE (decl) == NAMESPACE_DECL)
14278     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14279   else
14280     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14281
14282   xloc = expand_location (input_location);
14283   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14284   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14285   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14286 }
14287
14288 /* Write the debugging output for DECL.  */
14289
14290 void
14291 dwarf2out_decl (tree decl)
14292 {
14293   dw_die_ref context_die = comp_unit_die;
14294
14295   switch (TREE_CODE (decl))
14296     {
14297     case ERROR_MARK:
14298       return;
14299
14300     case FUNCTION_DECL:
14301       /* What we would really like to do here is to filter out all mere
14302          file-scope declarations of file-scope functions which are never
14303          referenced later within this translation unit (and keep all of ones
14304          that *are* referenced later on) but we aren't clairvoyant, so we have
14305          no idea which functions will be referenced in the future (i.e. later
14306          on within the current translation unit). So here we just ignore all
14307          file-scope function declarations which are not also definitions.  If
14308          and when the debugger needs to know something about these functions,
14309          it will have to hunt around and find the DWARF information associated
14310          with the definition of the function.
14311
14312          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14313          nodes represent definitions and which ones represent mere
14314          declarations.  We have to check DECL_INITIAL instead. That's because
14315          the C front-end supports some weird semantics for "extern inline"
14316          function definitions.  These can get inlined within the current
14317          translation unit (and thus, we need to generate Dwarf info for their
14318          abstract instances so that the Dwarf info for the concrete inlined
14319          instances can have something to refer to) but the compiler never
14320          generates any out-of-lines instances of such things (despite the fact
14321          that they *are* definitions).
14322
14323          The important point is that the C front-end marks these "extern
14324          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14325          them anyway. Note that the C++ front-end also plays some similar games
14326          for inline function definitions appearing within include files which
14327          also contain `#pragma interface' pragmas.  */
14328       if (DECL_INITIAL (decl) == NULL_TREE)
14329         return;
14330
14331       /* If we're a nested function, initially use a parent of NULL; if we're
14332          a plain function, this will be fixed up in decls_for_scope.  If
14333          we're a method, it will be ignored, since we already have a DIE.  */
14334       if (decl_function_context (decl)
14335           /* But if we're in terse mode, we don't care about scope.  */
14336           && debug_info_level > DINFO_LEVEL_TERSE)
14337         context_die = NULL;
14338       break;
14339
14340     case VAR_DECL:
14341       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14342          declaration and if the declaration was never even referenced from
14343          within this entire compilation unit.  We suppress these DIEs in
14344          order to save space in the .debug section (by eliminating entries
14345          which are probably useless).  Note that we must not suppress
14346          block-local extern declarations (whether used or not) because that
14347          would screw-up the debugger's name lookup mechanism and cause it to
14348          miss things which really ought to be in scope at a given point.  */
14349       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14350         return;
14351
14352       /* For local statics lookup proper context die.  */
14353       if (TREE_STATIC (decl) && decl_function_context (decl))
14354         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14355
14356       /* If we are in terse mode, don't generate any DIEs to represent any
14357          variable declarations or definitions.  */
14358       if (debug_info_level <= DINFO_LEVEL_TERSE)
14359         return;
14360       break;
14361
14362     case NAMESPACE_DECL:
14363       if (debug_info_level <= DINFO_LEVEL_TERSE)
14364         return;
14365       if (lookup_decl_die (decl) != NULL)
14366         return;
14367       break;
14368
14369     case TYPE_DECL:
14370       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14371       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14372         return;
14373
14374       /* Don't bother trying to generate any DIEs to represent any of the
14375          normal built-in types for the language we are compiling.  */
14376       if (DECL_IS_BUILTIN (decl))
14377         {
14378           /* OK, we need to generate one for `bool' so GDB knows what type
14379              comparisons have.  */
14380           if (is_cxx ()
14381               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14382               && ! DECL_IGNORED_P (decl))
14383             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14384
14385           return;
14386         }
14387
14388       /* If we are in terse mode, don't generate any DIEs for types.  */
14389       if (debug_info_level <= DINFO_LEVEL_TERSE)
14390         return;
14391
14392       /* If we're a function-scope tag, initially use a parent of NULL;
14393          this will be fixed up in decls_for_scope.  */
14394       if (decl_function_context (decl))
14395         context_die = NULL;
14396
14397       break;
14398
14399     default:
14400       return;
14401     }
14402
14403   gen_decl_die (decl, context_die);
14404 }
14405
14406 /* Output a marker (i.e. a label) for the beginning of the generated code for
14407    a lexical block.  */
14408
14409 static void
14410 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14411                        unsigned int blocknum)
14412 {
14413   switch_to_section (current_function_section ());
14414   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14415 }
14416
14417 /* Output a marker (i.e. a label) for the end of the generated code for a
14418    lexical block.  */
14419
14420 static void
14421 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14422 {
14423   switch_to_section (current_function_section ());
14424   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14425 }
14426
14427 /* Returns nonzero if it is appropriate not to emit any debugging
14428    information for BLOCK, because it doesn't contain any instructions.
14429
14430    Don't allow this for blocks with nested functions or local classes
14431    as we would end up with orphans, and in the presence of scheduling
14432    we may end up calling them anyway.  */
14433
14434 static bool
14435 dwarf2out_ignore_block (const_tree block)
14436 {
14437   tree decl;
14438
14439   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14440     if (TREE_CODE (decl) == FUNCTION_DECL
14441         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14442       return 0;
14443
14444   return 1;
14445 }
14446
14447 /* Hash table routines for file_hash.  */
14448
14449 static int
14450 file_table_eq (const void *p1_p, const void *p2_p)
14451 {
14452   const struct dwarf_file_data *const p1 =
14453     (const struct dwarf_file_data *) p1_p;
14454   const char *const p2 = (const char *) p2_p;
14455   return strcmp (p1->filename, p2) == 0;
14456 }
14457
14458 static hashval_t
14459 file_table_hash (const void *p_p)
14460 {
14461   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
14462   return htab_hash_string (p->filename);
14463 }
14464
14465 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14466    dwarf2out.c) and return its "index".  The index of each (known) filename is
14467    just a unique number which is associated with only that one filename.  We
14468    need such numbers for the sake of generating labels (in the .debug_sfnames
14469    section) and references to those files numbers (in the .debug_srcinfo
14470    and.debug_macinfo sections).  If the filename given as an argument is not
14471    found in our current list, add it to the list and assign it the next
14472    available unique index number.  In order to speed up searches, we remember
14473    the index of the filename was looked up last.  This handles the majority of
14474    all searches.  */
14475
14476 static struct dwarf_file_data *
14477 lookup_filename (const char *file_name)
14478 {
14479   void ** slot;
14480   struct dwarf_file_data * created;
14481
14482   /* Check to see if the file name that was searched on the previous
14483      call matches this file name.  If so, return the index.  */
14484   if (file_table_last_lookup
14485       && (file_name == file_table_last_lookup->filename
14486           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14487     return file_table_last_lookup;
14488
14489   /* Didn't match the previous lookup, search the table.  */
14490   slot = htab_find_slot_with_hash (file_table, file_name,
14491                                    htab_hash_string (file_name), INSERT);
14492   if (*slot)
14493     return (struct dwarf_file_data *) *slot;
14494
14495   created = GGC_NEW (struct dwarf_file_data);
14496   created->filename = file_name;
14497   created->emitted_number = 0;
14498   *slot = created;
14499   return created;
14500 }
14501
14502 /* If the assembler will construct the file table, then translate the compiler
14503    internal file table number into the assembler file table number, and emit
14504    a .file directive if we haven't already emitted one yet.  The file table
14505    numbers are different because we prune debug info for unused variables and
14506    types, which may include filenames.  */
14507
14508 static int
14509 maybe_emit_file (struct dwarf_file_data * fd)
14510 {
14511   if (! fd->emitted_number)
14512     {
14513       if (last_emitted_file)
14514         fd->emitted_number = last_emitted_file->emitted_number + 1;
14515       else
14516         fd->emitted_number = 1;
14517       last_emitted_file = fd;
14518
14519       if (DWARF2_ASM_LINE_DEBUG_INFO)
14520         {
14521           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14522           output_quoted_string (asm_out_file,
14523                                 remap_debug_filename (fd->filename));
14524           fputc ('\n', asm_out_file);
14525         }
14526     }
14527
14528   return fd->emitted_number;
14529 }
14530
14531 /* Called by the final INSN scan whenever we see a var location.  We
14532    use it to drop labels in the right places, and throw the location in
14533    our lookup table.  */
14534
14535 static void
14536 dwarf2out_var_location (rtx loc_note)
14537 {
14538   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14539   struct var_loc_node *newloc;
14540   rtx prev_insn;
14541   static rtx last_insn;
14542   static const char *last_label;
14543   tree decl;
14544
14545   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14546     return;
14547   prev_insn = PREV_INSN (loc_note);
14548
14549   newloc = GGC_CNEW (struct var_loc_node);
14550   /* If the insn we processed last time is the previous insn
14551      and it is also a var location note, use the label we emitted
14552      last time.  */
14553   if (last_insn != NULL_RTX
14554       && last_insn == prev_insn
14555       && NOTE_P (prev_insn)
14556       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14557     {
14558       newloc->label = last_label;
14559     }
14560   else
14561     {
14562       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14563       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14564       loclabel_num++;
14565       newloc->label = ggc_strdup (loclabel);
14566     }
14567   newloc->var_loc_note = loc_note;
14568   newloc->next = NULL;
14569
14570   if (cfun && in_cold_section_p)
14571     newloc->section_label = crtl->subsections.cold_section_label;
14572   else
14573     newloc->section_label = text_section_label;
14574
14575   last_insn = loc_note;
14576   last_label = newloc->label;
14577   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14578   add_var_loc_to_decl (decl, newloc);
14579 }
14580
14581 /* We need to reset the locations at the beginning of each
14582    function. We can't do this in the end_function hook, because the
14583    declarations that use the locations won't have been output when
14584    that hook is called.  Also compute have_multiple_function_sections here.  */
14585
14586 static void
14587 dwarf2out_begin_function (tree fun)
14588 {
14589   htab_empty (decl_loc_table);
14590
14591   if (function_section (fun) != text_section)
14592     have_multiple_function_sections = true;
14593
14594   dwarf2out_note_section_used ();
14595 }
14596
14597 /* Output a label to mark the beginning of a source code line entry
14598    and record information relating to this source line, in
14599    'line_info_table' for later output of the .debug_line section.  */
14600
14601 static void
14602 dwarf2out_source_line (unsigned int line, const char *filename)
14603 {
14604   if (debug_info_level >= DINFO_LEVEL_NORMAL
14605       && line != 0)
14606     {
14607       int file_num = maybe_emit_file (lookup_filename (filename));
14608
14609       switch_to_section (current_function_section ());
14610
14611       /* If requested, emit something human-readable.  */
14612       if (flag_debug_asm)
14613         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14614                  filename, line);
14615
14616       if (DWARF2_ASM_LINE_DEBUG_INFO)
14617         {
14618           /* Emit the .loc directive understood by GNU as.  */
14619           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14620
14621           /* Indicate that line number info exists.  */
14622           line_info_table_in_use++;
14623         }
14624       else if (function_section (current_function_decl) != text_section)
14625         {
14626           dw_separate_line_info_ref line_info;
14627           targetm.asm_out.internal_label (asm_out_file,
14628                                           SEPARATE_LINE_CODE_LABEL,
14629                                           separate_line_info_table_in_use);
14630
14631           /* Expand the line info table if necessary.  */
14632           if (separate_line_info_table_in_use
14633               == separate_line_info_table_allocated)
14634             {
14635               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14636               separate_line_info_table
14637                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
14638                                  separate_line_info_table,
14639                                  separate_line_info_table_allocated);
14640               memset (separate_line_info_table
14641                        + separate_line_info_table_in_use,
14642                       0,
14643                       (LINE_INFO_TABLE_INCREMENT
14644                        * sizeof (dw_separate_line_info_entry)));
14645             }
14646
14647           /* Add the new entry at the end of the line_info_table.  */
14648           line_info
14649             = &separate_line_info_table[separate_line_info_table_in_use++];
14650           line_info->dw_file_num = file_num;
14651           line_info->dw_line_num = line;
14652           line_info->function = current_function_funcdef_no;
14653         }
14654       else
14655         {
14656           dw_line_info_ref line_info;
14657
14658           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14659                                      line_info_table_in_use);
14660
14661           /* Expand the line info table if necessary.  */
14662           if (line_info_table_in_use == line_info_table_allocated)
14663             {
14664               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14665               line_info_table
14666                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
14667                                  line_info_table_allocated);
14668               memset (line_info_table + line_info_table_in_use, 0,
14669                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14670             }
14671
14672           /* Add the new entry at the end of the line_info_table.  */
14673           line_info = &line_info_table[line_info_table_in_use++];
14674           line_info->dw_file_num = file_num;
14675           line_info->dw_line_num = line;
14676         }
14677     }
14678 }
14679
14680 /* Record the beginning of a new source file.  */
14681
14682 static void
14683 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14684 {
14685   if (flag_eliminate_dwarf2_dups)
14686     {
14687       /* Record the beginning of the file for break_out_includes.  */
14688       dw_die_ref bincl_die;
14689
14690       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14691       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14692     }
14693
14694   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14695     {
14696       int file_num = maybe_emit_file (lookup_filename (filename));
14697
14698       switch_to_section (debug_macinfo_section);
14699       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14700       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14701                                    lineno);
14702
14703       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14704     }
14705 }
14706
14707 /* Record the end of a source file.  */
14708
14709 static void
14710 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14711 {
14712   if (flag_eliminate_dwarf2_dups)
14713     /* Record the end of the file for break_out_includes.  */
14714     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14715
14716   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14717     {
14718       switch_to_section (debug_macinfo_section);
14719       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14720     }
14721 }
14722
14723 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14724    the tail part of the directive line, i.e. the part which is past the
14725    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14726
14727 static void
14728 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14729                   const char *buffer ATTRIBUTE_UNUSED)
14730 {
14731   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14732     {
14733       switch_to_section (debug_macinfo_section);
14734       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14735       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14736       dw2_asm_output_nstring (buffer, -1, "The macro");
14737     }
14738 }
14739
14740 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14741    the tail part of the directive line, i.e. the part which is past the
14742    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14743
14744 static void
14745 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14746                  const char *buffer ATTRIBUTE_UNUSED)
14747 {
14748   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14749     {
14750       switch_to_section (debug_macinfo_section);
14751       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14752       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14753       dw2_asm_output_nstring (buffer, -1, "The macro");
14754     }
14755 }
14756
14757 /* Set up for Dwarf output at the start of compilation.  */
14758
14759 static void
14760 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14761 {
14762   /* Allocate the file_table.  */
14763   file_table = htab_create_ggc (50, file_table_hash,
14764                                 file_table_eq, NULL);
14765
14766   /* Allocate the decl_die_table.  */
14767   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14768                                     decl_die_table_eq, NULL);
14769
14770   /* Allocate the decl_loc_table.  */
14771   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14772                                     decl_loc_table_eq, NULL);
14773
14774   /* Allocate the initial hunk of the decl_scope_table.  */
14775   decl_scope_table = VEC_alloc (tree, gc, 256);
14776
14777   /* Allocate the initial hunk of the abbrev_die_table.  */
14778   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
14779   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14780   /* Zero-th entry is allocated, but unused.  */
14781   abbrev_die_table_in_use = 1;
14782
14783   /* Allocate the initial hunk of the line_info_table.  */
14784   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
14785   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14786
14787   /* Zero-th entry is allocated, but unused.  */
14788   line_info_table_in_use = 1;
14789
14790   /* Allocate the pubtypes and pubnames vectors.  */
14791   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14792   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14793
14794   /* Generate the initial DIE for the .debug section.  Note that the (string)
14795      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14796      will (typically) be a relative pathname and that this pathname should be
14797      taken as being relative to the directory from which the compiler was
14798      invoked when the given (base) source file was compiled.  We will fill
14799      in this value in dwarf2out_finish.  */
14800   comp_unit_die = gen_compile_unit_die (NULL);
14801
14802   incomplete_types = VEC_alloc (tree, gc, 64);
14803
14804   used_rtx_array = VEC_alloc (rtx, gc, 32);
14805
14806   debug_info_section = get_section (DEBUG_INFO_SECTION,
14807                                     SECTION_DEBUG, NULL);
14808   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14809                                       SECTION_DEBUG, NULL);
14810   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14811                                        SECTION_DEBUG, NULL);
14812   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14813                                        SECTION_DEBUG, NULL);
14814   debug_line_section = get_section (DEBUG_LINE_SECTION,
14815                                     SECTION_DEBUG, NULL);
14816   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14817                                    SECTION_DEBUG, NULL);
14818   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14819                                         SECTION_DEBUG, NULL);
14820 #ifdef DEBUG_PUBTYPES_SECTION
14821   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14822                                         SECTION_DEBUG, NULL);
14823 #endif
14824   debug_str_section = get_section (DEBUG_STR_SECTION,
14825                                    DEBUG_STR_SECTION_FLAGS, NULL);
14826   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14827                                       SECTION_DEBUG, NULL);
14828   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14829                                      SECTION_DEBUG, NULL);
14830
14831   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14832   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14833                                DEBUG_ABBREV_SECTION_LABEL, 0);
14834   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14835   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14836                                COLD_TEXT_SECTION_LABEL, 0);
14837   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14838
14839   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14840                                DEBUG_INFO_SECTION_LABEL, 0);
14841   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14842                                DEBUG_LINE_SECTION_LABEL, 0);
14843   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14844                                DEBUG_RANGES_SECTION_LABEL, 0);
14845   switch_to_section (debug_abbrev_section);
14846   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14847   switch_to_section (debug_info_section);
14848   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14849   switch_to_section (debug_line_section);
14850   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14851
14852   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14853     {
14854       switch_to_section (debug_macinfo_section);
14855       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14856                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14857       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14858     }
14859
14860   switch_to_section (text_section);
14861   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14862   if (flag_reorder_blocks_and_partition)
14863     {
14864       cold_text_section = unlikely_text_section ();
14865       switch_to_section (cold_text_section);
14866       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14867     }
14868 }
14869
14870 /* A helper function for dwarf2out_finish called through
14871    ht_forall.  Emit one queued .debug_str string.  */
14872
14873 static int
14874 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14875 {
14876   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14877
14878   if (node->form == DW_FORM_strp)
14879     {
14880       switch_to_section (debug_str_section);
14881       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14882       assemble_string (node->str, strlen (node->str) + 1);
14883     }
14884
14885   return 1;
14886 }
14887
14888 #if ENABLE_ASSERT_CHECKING
14889 /* Verify that all marks are clear.  */
14890
14891 static void
14892 verify_marks_clear (dw_die_ref die)
14893 {
14894   dw_die_ref c;
14895
14896   gcc_assert (! die->die_mark);
14897   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14898 }
14899 #endif /* ENABLE_ASSERT_CHECKING */
14900
14901 /* Clear the marks for a die and its children.
14902    Be cool if the mark isn't set.  */
14903
14904 static void
14905 prune_unmark_dies (dw_die_ref die)
14906 {
14907   dw_die_ref c;
14908
14909   if (die->die_mark)
14910     die->die_mark = 0;
14911   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14912 }
14913
14914 /* Given DIE that we're marking as used, find any other dies
14915    it references as attributes and mark them as used.  */
14916
14917 static void
14918 prune_unused_types_walk_attribs (dw_die_ref die)
14919 {
14920   dw_attr_ref a;
14921   unsigned ix;
14922
14923   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14924     {
14925       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14926         {
14927           /* A reference to another DIE.
14928              Make sure that it will get emitted.  */
14929           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14930         }
14931       /* Set the string's refcount to 0 so that prune_unused_types_mark
14932          accounts properly for it.  */
14933       if (AT_class (a) == dw_val_class_str)
14934         a->dw_attr_val.v.val_str->refcount = 0;
14935     }
14936 }
14937
14938
14939 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14940    to DIE's children.  */
14941
14942 static void
14943 prune_unused_types_mark (dw_die_ref die, int dokids)
14944 {
14945   dw_die_ref c;
14946
14947   if (die->die_mark == 0)
14948     {
14949       /* We haven't done this node yet.  Mark it as used.  */
14950       die->die_mark = 1;
14951
14952       /* We also have to mark its parents as used.
14953          (But we don't want to mark our parents' kids due to this.)  */
14954       if (die->die_parent)
14955         prune_unused_types_mark (die->die_parent, 0);
14956
14957       /* Mark any referenced nodes.  */
14958       prune_unused_types_walk_attribs (die);
14959
14960       /* If this node is a specification,
14961          also mark the definition, if it exists.  */
14962       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14963         prune_unused_types_mark (die->die_definition, 1);
14964     }
14965
14966   if (dokids && die->die_mark != 2)
14967     {
14968       /* We need to walk the children, but haven't done so yet.
14969          Remember that we've walked the kids.  */
14970       die->die_mark = 2;
14971
14972       /* If this is an array type, we need to make sure our
14973          kids get marked, even if they're types.  */
14974       if (die->die_tag == DW_TAG_array_type)
14975         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14976       else
14977         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14978     }
14979 }
14980
14981
14982 /* Walk the tree DIE and mark types that we actually use.  */
14983
14984 static void
14985 prune_unused_types_walk (dw_die_ref die)
14986 {
14987   dw_die_ref c;
14988
14989   /* Don't do anything if this node is already marked.  */
14990   if (die->die_mark)
14991     return;
14992
14993   switch (die->die_tag)
14994     {
14995     case DW_TAG_const_type:
14996     case DW_TAG_packed_type:
14997     case DW_TAG_pointer_type:
14998     case DW_TAG_reference_type:
14999     case DW_TAG_volatile_type:
15000     case DW_TAG_typedef:
15001     case DW_TAG_array_type:
15002     case DW_TAG_structure_type:
15003     case DW_TAG_union_type:
15004     case DW_TAG_class_type:
15005     case DW_TAG_interface_type:
15006     case DW_TAG_friend:
15007     case DW_TAG_variant_part:
15008     case DW_TAG_enumeration_type:
15009     case DW_TAG_subroutine_type:
15010     case DW_TAG_string_type:
15011     case DW_TAG_set_type:
15012     case DW_TAG_subrange_type:
15013     case DW_TAG_ptr_to_member_type:
15014     case DW_TAG_file_type:
15015       if (die->die_perennial_p)
15016         break;
15017
15018       /* It's a type node --- don't mark it.  */
15019       return;
15020
15021     default:
15022       /* Mark everything else.  */
15023       break;
15024   }
15025
15026   die->die_mark = 1;
15027
15028   /* Now, mark any dies referenced from here.  */
15029   prune_unused_types_walk_attribs (die);
15030
15031   /* Mark children.  */
15032   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15033 }
15034
15035 /* Increment the string counts on strings referred to from DIE's
15036    attributes.  */
15037
15038 static void
15039 prune_unused_types_update_strings (dw_die_ref die)
15040 {
15041   dw_attr_ref a;
15042   unsigned ix;
15043
15044   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15045     if (AT_class (a) == dw_val_class_str)
15046       {
15047         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15048         s->refcount++;
15049         /* Avoid unnecessarily putting strings that are used less than
15050            twice in the hash table.  */
15051         if (s->refcount
15052             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15053           {
15054             void ** slot;
15055             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15056                                              htab_hash_string (s->str),
15057                                              INSERT);
15058             gcc_assert (*slot == NULL);
15059             *slot = s;
15060           }
15061       }
15062 }
15063
15064 /* Remove from the tree DIE any dies that aren't marked.  */
15065
15066 static void
15067 prune_unused_types_prune (dw_die_ref die)
15068 {
15069   dw_die_ref c;
15070
15071   gcc_assert (die->die_mark);
15072   prune_unused_types_update_strings (die);
15073
15074   if (! die->die_child)
15075     return;
15076
15077   c = die->die_child;
15078   do {
15079     dw_die_ref prev = c;
15080     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15081       if (c == die->die_child)
15082         {
15083           /* No marked children between 'prev' and the end of the list.  */
15084           if (prev == c)
15085             /* No marked children at all.  */
15086             die->die_child = NULL;
15087           else
15088             {
15089               prev->die_sib = c->die_sib;
15090               die->die_child = prev;
15091             }
15092           return;
15093         }
15094
15095     if (c != prev->die_sib)
15096       prev->die_sib = c;
15097     prune_unused_types_prune (c);
15098   } while (c != die->die_child);
15099 }
15100
15101
15102 /* Remove dies representing declarations that we never use.  */
15103
15104 static void
15105 prune_unused_types (void)
15106 {
15107   unsigned int i;
15108   limbo_die_node *node;
15109   pubname_ref pub;
15110
15111 #if ENABLE_ASSERT_CHECKING
15112   /* All the marks should already be clear.  */
15113   verify_marks_clear (comp_unit_die);
15114   for (node = limbo_die_list; node; node = node->next)
15115     verify_marks_clear (node->die);
15116 #endif /* ENABLE_ASSERT_CHECKING */
15117
15118   /* Set the mark on nodes that are actually used.  */
15119   prune_unused_types_walk (comp_unit_die);
15120   for (node = limbo_die_list; node; node = node->next)
15121     prune_unused_types_walk (node->die);
15122
15123   /* Also set the mark on nodes referenced from the
15124      pubname_table or arange_table.  */
15125   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15126     prune_unused_types_mark (pub->die, 1);
15127   for (i = 0; i < arange_table_in_use; i++)
15128     prune_unused_types_mark (arange_table[i], 1);
15129
15130   /* Get rid of nodes that aren't marked; and update the string counts.  */
15131   if (debug_str_hash)
15132     htab_empty (debug_str_hash);
15133   prune_unused_types_prune (comp_unit_die);
15134   for (node = limbo_die_list; node; node = node->next)
15135     prune_unused_types_prune (node->die);
15136
15137   /* Leave the marks clear.  */
15138   prune_unmark_dies (comp_unit_die);
15139   for (node = limbo_die_list; node; node = node->next)
15140     prune_unmark_dies (node->die);
15141 }
15142
15143 /* Set the parameter to true if there are any relative pathnames in
15144    the file table.  */
15145 static int
15146 file_table_relative_p (void ** slot, void *param)
15147 {
15148   bool *p = (bool *) param;
15149   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15150   if (!IS_ABSOLUTE_PATH (d->filename))
15151     {
15152       *p = true;
15153       return 0;
15154     }
15155   return 1;
15156 }
15157
15158 /* Output stuff that dwarf requires at the end of every file,
15159    and generate the DWARF-2 debugging info.  */
15160
15161 static void
15162 dwarf2out_finish (const char *filename)
15163 {
15164   limbo_die_node *node, *next_node;
15165   dw_die_ref die = 0;
15166
15167   /* Add the name for the main input file now.  We delayed this from
15168      dwarf2out_init to avoid complications with PCH.  */
15169   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15170   if (!IS_ABSOLUTE_PATH (filename))
15171     add_comp_dir_attribute (comp_unit_die);
15172   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15173     {
15174       bool p = false;
15175       htab_traverse (file_table, file_table_relative_p, &p);
15176       if (p)
15177         add_comp_dir_attribute (comp_unit_die);
15178     }
15179
15180   /* Traverse the limbo die list, and add parent/child links.  The only
15181      dies without parents that should be here are concrete instances of
15182      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15183      For concrete instances, we can get the parent die from the abstract
15184      instance.  */
15185   for (node = limbo_die_list; node; node = next_node)
15186     {
15187       next_node = node->next;
15188       die = node->die;
15189
15190       if (die->die_parent == NULL)
15191         {
15192           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15193
15194           if (origin)
15195             add_child_die (origin->die_parent, die);
15196           else if (die == comp_unit_die)
15197             ;
15198           else if (errorcount > 0 || sorrycount > 0)
15199             /* It's OK to be confused by errors in the input.  */
15200             add_child_die (comp_unit_die, die);
15201           else
15202             {
15203               /* In certain situations, the lexical block containing a
15204                  nested function can be optimized away, which results
15205                  in the nested function die being orphaned.  Likewise
15206                  with the return type of that nested function.  Force
15207                  this to be a child of the containing function.
15208
15209                  It may happen that even the containing function got fully
15210                  inlined and optimized out.  In that case we are lost and
15211                  assign the empty child.  This should not be big issue as
15212                  the function is likely unreachable too.  */
15213               tree context = NULL_TREE;
15214
15215               gcc_assert (node->created_for);
15216
15217               if (DECL_P (node->created_for))
15218                 context = DECL_CONTEXT (node->created_for);
15219               else if (TYPE_P (node->created_for))
15220                 context = TYPE_CONTEXT (node->created_for);
15221
15222               gcc_assert (context
15223                           && (TREE_CODE (context) == FUNCTION_DECL
15224                               || TREE_CODE (context) == NAMESPACE_DECL));
15225
15226               origin = lookup_decl_die (context);
15227               if (origin)
15228                 add_child_die (origin, die);
15229               else
15230                 add_child_die (comp_unit_die, die);
15231             }
15232         }
15233     }
15234
15235   limbo_die_list = NULL;
15236
15237   /* Walk through the list of incomplete types again, trying once more to
15238      emit full debugging info for them.  */
15239   retry_incomplete_types ();
15240
15241   if (flag_eliminate_unused_debug_types)
15242     prune_unused_types ();
15243
15244   /* Generate separate CUs for each of the include files we've seen.
15245      They will go into limbo_die_list.  */
15246   if (flag_eliminate_dwarf2_dups)
15247     break_out_includes (comp_unit_die);
15248
15249   /* Traverse the DIE's and add add sibling attributes to those DIE's
15250      that have children.  */
15251   add_sibling_attributes (comp_unit_die);
15252   for (node = limbo_die_list; node; node = node->next)
15253     add_sibling_attributes (node->die);
15254
15255   /* Output a terminator label for the .text section.  */
15256   switch_to_section (text_section);
15257   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15258   if (flag_reorder_blocks_and_partition)
15259     {
15260       switch_to_section (unlikely_text_section ());
15261       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15262     }
15263
15264   /* We can only use the low/high_pc attributes if all of the code was
15265      in .text.  */
15266   if (!have_multiple_function_sections)
15267     {
15268       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15269       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15270     }
15271
15272   else
15273     {
15274       unsigned fde_idx = 0;
15275
15276       /* We need to give .debug_loc and .debug_ranges an appropriate
15277          "base address".  Use zero so that these addresses become
15278          absolute.  Historically, we've emitted the unexpected
15279          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15280          Emit both to give time for other tools to adapt.  */
15281       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15282       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15283
15284       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15285                          add_ranges_by_labels (text_section_label,
15286                                                text_end_label));
15287       if (flag_reorder_blocks_and_partition)
15288         add_ranges_by_labels (cold_text_section_label,
15289                               cold_end_label);
15290
15291       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15292         {
15293           dw_fde_ref fde = &fde_table[fde_idx];
15294
15295           if (fde->dw_fde_switched_sections)
15296             {
15297               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15298                                     fde->dw_fde_hot_section_end_label);
15299               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15300                                     fde->dw_fde_unlikely_section_end_label);
15301             }
15302           else
15303             add_ranges_by_labels (fde->dw_fde_begin,
15304                                   fde->dw_fde_end);
15305         }
15306
15307       add_ranges (NULL);
15308     }
15309
15310   /* Output location list section if necessary.  */
15311   if (have_location_lists)
15312     {
15313       /* Output the location lists info.  */
15314       switch_to_section (debug_loc_section);
15315       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15316                                    DEBUG_LOC_SECTION_LABEL, 0);
15317       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15318       output_location_lists (die);
15319     }
15320
15321   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15322     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15323                     debug_line_section_label);
15324
15325   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15326     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15327
15328   /* Output all of the compilation units.  We put the main one last so that
15329      the offsets are available to output_pubnames.  */
15330   for (node = limbo_die_list; node; node = node->next)
15331     output_comp_unit (node->die, 0);
15332
15333   output_comp_unit (comp_unit_die, 0);
15334
15335   /* Output the abbreviation table.  */
15336   switch_to_section (debug_abbrev_section);
15337   output_abbrev_section ();
15338
15339   /* Output public names table if necessary.  */
15340   if (!VEC_empty (pubname_entry, pubname_table))
15341     {
15342       switch_to_section (debug_pubnames_section);
15343       output_pubnames (pubname_table);
15344     }
15345
15346 #ifdef DEBUG_PUBTYPES_SECTION
15347   /* Output public types table if necessary.  */
15348   if (!VEC_empty (pubname_entry, pubtype_table))
15349     {
15350       switch_to_section (debug_pubtypes_section);
15351       output_pubnames (pubtype_table);
15352     }
15353 #endif
15354
15355   /* Output the address range information.  We only put functions in the arange
15356      table, so don't write it out if we don't have any.  */
15357   if (fde_table_in_use)
15358     {
15359       switch_to_section (debug_aranges_section);
15360       output_aranges ();
15361     }
15362
15363   /* Output ranges section if necessary.  */
15364   if (ranges_table_in_use)
15365     {
15366       switch_to_section (debug_ranges_section);
15367       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15368       output_ranges ();
15369     }
15370
15371   /* Output the source line correspondence table.  We must do this
15372      even if there is no line information.  Otherwise, on an empty
15373      translation unit, we will generate a present, but empty,
15374      .debug_info section.  IRIX 6.5 `nm' will then complain when
15375      examining the file.  This is done late so that any filenames
15376      used by the debug_info section are marked as 'used'.  */
15377   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15378     {
15379       switch_to_section (debug_line_section);
15380       output_line_info ();
15381     }
15382
15383   /* Have to end the macro section.  */
15384   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15385     {
15386       switch_to_section (debug_macinfo_section);
15387       dw2_asm_output_data (1, 0, "End compilation unit");
15388     }
15389
15390   /* If we emitted any DW_FORM_strp form attribute, output the string
15391      table too.  */
15392   if (debug_str_hash)
15393     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15394 }
15395 #else
15396
15397 /* This should never be used, but its address is needed for comparisons.  */
15398 const struct gcc_debug_hooks dwarf2_debug_hooks;
15399
15400 #endif /* DWARF2_DEBUGGING_INFO */
15401
15402 #include "gt-dwarf2out.h"