]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/gcc/dwarf2out.c
MFC r260014, r260099:
[FreeBSD/stable/10.git] / contrib / 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 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 2, 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 COPYING.  If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
70
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
74
75 /* DWARF2 Abbreviation Glossary:
76    CFA = Canonical Frame Address
77            a fixed address on the stack which identifies a call frame.
78            We define it to be the value of SP just before the call insn.
79            The CFA register and offset, which may change during the course
80            of the function, are used to calculate its value at runtime.
81    CFI = Call Frame Instruction
82            an instruction for the DWARF2 abstract machine
83    CIE = Common Information Entry
84            information describing information common to one or more FDEs
85    DIE = Debugging Information Entry
86    FDE = Frame Description Entry
87            information describing the stack call frame, in particular,
88            how to restore registers
89
90    DW_CFA_... = DWARF2 CFA call frame instruction
91    DW_TAG_... = DWARF2 DIE tag */
92
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 #  define DWARF2_FRAME_INFO \
96   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 #  define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
101
102 /* Map register numbers held in the call frame info that gcc has
103    collected using DWARF_FRAME_REGNUM to those that should be output in
104    .debug_frame and .eh_frame.  */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
108
109 /* Decide whether we want to emit frame unwind information for the current
110    translation unit.  */
111
112 int
113 dwarf2out_do_frame (void)
114 {
115   /* We want to emit correct CFA location expressions or lists, so we
116      have to return true if we're going to output debug info, even if
117      we're not going to output frame or unwind info.  */
118   return (write_symbols == DWARF2_DEBUG
119           || write_symbols == VMS_AND_DWARF2_DEBUG
120           || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122           || (DWARF2_UNWIND_INFO
123               && (flag_unwind_tables
124                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
126           );
127 }
128
129 /* The size of the target's pointer type.  */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133
134 /* Array of RTXes referenced by the debugging information, which therefore
135    must be kept around forever.  */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
137
138 /* A pointer to the base of a list of incomplete types which might be
139    completed at some later time.  incomplete_types_list needs to be a
140    VEC(tree,gc) because we want to tell the garbage collector about
141    it.  */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
143
144 /* A pointer to the base of a table of references to declaration
145    scopes.  This table is a display which tracks the nesting
146    of declaration scopes at the current scope and containing
147    scopes.  This table is used to find the proper place to
148    define type declaration DIE's.  */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
150
151 /* Pointers to various DWARF2 sections.  */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
163
164 /* How to start an assembler comment.  */
165 #ifndef ASM_COMMENT_START
166 #define ASM_COMMENT_START ";#"
167 #endif
168
169 typedef struct dw_cfi_struct *dw_cfi_ref;
170 typedef struct dw_fde_struct *dw_fde_ref;
171 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
172
173 /* Call frames are described using a sequence of Call Frame
174    Information instructions.  The register number, offset
175    and address fields are provided as possible operands;
176    their use is selected by the opcode field.  */
177
178 enum dw_cfi_oprnd_type {
179   dw_cfi_oprnd_unused,
180   dw_cfi_oprnd_reg_num,
181   dw_cfi_oprnd_offset,
182   dw_cfi_oprnd_addr,
183   dw_cfi_oprnd_loc
184 };
185
186 typedef union dw_cfi_oprnd_struct GTY(())
187 {
188   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
192 }
193 dw_cfi_oprnd;
194
195 typedef struct dw_cfi_struct GTY(())
196 {
197   dw_cfi_ref dw_cfi_next;
198   enum dwarf_call_frame_info dw_cfi_opc;
199   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200     dw_cfi_oprnd1;
201   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202     dw_cfi_oprnd2;
203 }
204 dw_cfi_node;
205
206 /* This is how we define the location of the CFA. We use to handle it
207    as REG + OFFSET all the time,  but now it can be more complex.
208    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
209    Instead of passing around REG and OFFSET, we pass a copy
210    of this structure.  */
211 typedef struct cfa_loc GTY(())
212 {
213   HOST_WIDE_INT offset;
214   HOST_WIDE_INT base_offset;
215   unsigned int reg;
216   int indirect;            /* 1 if CFA is accessed via a dereference.  */
217 } dw_cfa_location;
218
219 /* All call frame descriptions (FDE's) in the GCC generated DWARF
220    refer to a single Common Information Entry (CIE), defined at
221    the beginning of the .debug_frame section.  This use of a single
222    CIE obviates the need to keep track of multiple CIE's
223    in the DWARF generation routines below.  */
224
225 typedef struct dw_fde_struct GTY(())
226 {
227   tree decl;
228   const char *dw_fde_begin;
229   const char *dw_fde_current_label;
230   const char *dw_fde_end;
231   const char *dw_fde_hot_section_label;
232   const char *dw_fde_hot_section_end_label;
233   const char *dw_fde_unlikely_section_label;
234   const char *dw_fde_unlikely_section_end_label;
235   bool dw_fde_switched_sections;
236   dw_cfi_ref dw_fde_cfi;
237   unsigned funcdef_number;
238   unsigned all_throwers_are_sibcalls : 1;
239   unsigned nothrow : 1;
240   unsigned uses_eh_lsda : 1;
241 }
242 dw_fde_node;
243
244 /* Maximum size (in bytes) of an artificially generated label.  */
245 #define MAX_ARTIFICIAL_LABEL_BYTES      30
246
247 /* The size of addresses as they appear in the Dwarf 2 data.
248    Some architectures use word addresses to refer to code locations,
249    but Dwarf 2 info always uses byte addresses.  On such machines,
250    Dwarf 2 addresses need to be larger than the architecture's
251    pointers.  */
252 #ifndef DWARF2_ADDR_SIZE
253 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
254 #endif
255
256 /* The size in bytes of a DWARF field indicating an offset or length
257    relative to a debug info section, specified to be 4 bytes in the
258    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
259    as PTR_SIZE.  */
260
261 #ifndef DWARF_OFFSET_SIZE
262 #define DWARF_OFFSET_SIZE 4
263 #endif
264
265 /* According to the (draft) DWARF 3 specification, the initial length
266    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
267    bytes are 0xffffffff, followed by the length stored in the next 8
268    bytes.
269
270    However, the SGI/MIPS ABI uses an initial length which is equal to
271    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
272
273 #ifndef DWARF_INITIAL_LENGTH_SIZE
274 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275 #endif
276
277 #define DWARF_VERSION 2
278
279 /* Round SIZE up to the nearest BOUNDARY.  */
280 #define DWARF_ROUND(SIZE,BOUNDARY) \
281   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
282
283 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
284 #ifndef DWARF_CIE_DATA_ALIGNMENT
285 #ifdef STACK_GROWS_DOWNWARD
286 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
287 #else
288 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
289 #endif
290 #endif
291
292 /* CIE identifier.  */
293 #if HOST_BITS_PER_WIDE_INT >= 64
294 #define DWARF_CIE_ID \
295   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296 #else
297 #define DWARF_CIE_ID DW_CIE_ID
298 #endif
299
300 /* A pointer to the base of a table that contains frame description
301    information for each routine.  */
302 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
303
304 /* Number of elements currently allocated for fde_table.  */
305 static GTY(()) unsigned fde_table_allocated;
306
307 /* Number of elements in fde_table currently in use.  */
308 static GTY(()) unsigned fde_table_in_use;
309
310 /* Size (in elements) of increments by which we may expand the
311    fde_table.  */
312 #define FDE_TABLE_INCREMENT 256
313
314 /* A list of call frame insns for the CIE.  */
315 static GTY(()) dw_cfi_ref cie_cfi_head;
316
317 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
318 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
319    attribute that accelerates the lookup of the FDE associated
320    with the subprogram.  This variable holds the table index of the FDE
321    associated with the current function (body) definition.  */
322 static unsigned current_funcdef_fde;
323 #endif
324
325 struct indirect_string_node GTY(())
326 {
327   const char *str;
328   unsigned int refcount;
329   unsigned int form;
330   char *label;
331 };
332
333 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334
335 static GTY(()) int dw2_string_counter;
336 static GTY(()) unsigned long dwarf2out_cfi_label_num;
337
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339
340 /* Forward declarations for functions defined in this file.  */
341
342 static char *stripattributes (const char *);
343 static const char *dwarf_cfi_name (unsigned);
344 static dw_cfi_ref new_cfi (void);
345 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346 static void add_fde_cfi (const char *, dw_cfi_ref);
347 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348 static void lookup_cfa (dw_cfa_location *);
349 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350 static void initial_return_save (rtx);
351 static HOST_WIDE_INT stack_adjust_offset (rtx);
352 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353 static void output_call_frame_info (int);
354 static void dwarf2out_stack_adjust (rtx, bool);
355 static void flush_queued_reg_saves (void);
356 static bool clobbers_queued_reg_save (rtx);
357 static void dwarf2out_frame_debug_expr (rtx, const char *);
358
359 /* Support for complex CFA locations.  */
360 static void output_cfa_loc (dw_cfi_ref);
361 static void get_cfa_from_loc_descr (dw_cfa_location *,
362                                     struct dw_loc_descr_struct *);
363 static struct dw_loc_descr_struct *build_cfa_loc
364   (dw_cfa_location *, HOST_WIDE_INT);
365 static void def_cfa_1 (const char *, dw_cfa_location *);
366
367 /* How to start an assembler comment.  */
368 #ifndef ASM_COMMENT_START
369 #define ASM_COMMENT_START ";#"
370 #endif
371
372 /* Data and reference forms for relocatable data.  */
373 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
374 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
375
376 #ifndef DEBUG_FRAME_SECTION
377 #define DEBUG_FRAME_SECTION     ".debug_frame"
378 #endif
379
380 #ifndef FUNC_BEGIN_LABEL
381 #define FUNC_BEGIN_LABEL        "LFB"
382 #endif
383
384 #ifndef FUNC_END_LABEL
385 #define FUNC_END_LABEL          "LFE"
386 #endif
387
388 #ifndef FRAME_BEGIN_LABEL
389 #define FRAME_BEGIN_LABEL       "Lframe"
390 #endif
391 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
392 #define CIE_END_LABEL           "LECIE"
393 #define FDE_LABEL               "LSFDE"
394 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
395 #define FDE_END_LABEL           "LEFDE"
396 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
397 #define LINE_NUMBER_END_LABEL   "LELT"
398 #define LN_PROLOG_AS_LABEL      "LASLTP"
399 #define LN_PROLOG_END_LABEL     "LELTP"
400 #define DIE_LABEL_PREFIX        "DW"
401
402 /* The DWARF 2 CFA column which tracks the return address.  Normally this
403    is the column for PC, or the first column after all of the hard
404    registers.  */
405 #ifndef DWARF_FRAME_RETURN_COLUMN
406 #ifdef PC_REGNUM
407 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
408 #else
409 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
410 #endif
411 #endif
412
413 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
414    default, we just provide columns for all registers.  */
415 #ifndef DWARF_FRAME_REGNUM
416 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
417 #endif
418 \f
419 /* Hook used by __throw.  */
420
421 rtx
422 expand_builtin_dwarf_sp_column (void)
423 {
424   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
426 }
427
428 /* Return a pointer to a copy of the section string name S with all
429    attributes stripped off, and an asterisk prepended (for assemble_name).  */
430
431 static inline char *
432 stripattributes (const char *s)
433 {
434   char *stripped = XNEWVEC (char, strlen (s) + 2);
435   char *p = stripped;
436
437   *p++ = '*';
438
439   while (*s && *s != ',')
440     *p++ = *s++;
441
442   *p = '\0';
443   return stripped;
444 }
445
446 /* Generate code to initialize the register size table.  */
447
448 void
449 expand_builtin_init_dwarf_reg_sizes (tree address)
450 {
451   unsigned int i;
452   enum machine_mode mode = TYPE_MODE (char_type_node);
453   rtx addr = expand_normal (address);
454   rtx mem = gen_rtx_MEM (BLKmode, addr);
455   bool wrote_return_column = false;
456
457   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
458     {
459       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
460       
461       if (rnum < DWARF_FRAME_REGISTERS)
462         {
463           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
464           enum machine_mode save_mode = reg_raw_mode[i];
465           HOST_WIDE_INT size;
466           
467           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
468             save_mode = choose_hard_reg_mode (i, 1, true);
469           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
470             {
471               if (save_mode == VOIDmode)
472                 continue;
473               wrote_return_column = true;
474             }
475           size = GET_MODE_SIZE (save_mode);
476           if (offset < 0)
477             continue;
478           
479           emit_move_insn (adjust_address (mem, mode, offset),
480                           gen_int_mode (size, mode));
481         }
482     }
483
484 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
485   gcc_assert (wrote_return_column);
486   i = DWARF_ALT_FRAME_RETURN_COLUMN;
487   wrote_return_column = false;
488 #else
489   i = DWARF_FRAME_RETURN_COLUMN;
490 #endif
491
492   if (! wrote_return_column)
493     {
494       enum machine_mode save_mode = Pmode;
495       HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
496       HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
497       emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
498     }
499 }
500
501 /* Convert a DWARF call frame info. operation to its string name */
502
503 static const char *
504 dwarf_cfi_name (unsigned int cfi_opc)
505 {
506   switch (cfi_opc)
507     {
508     case DW_CFA_advance_loc:
509       return "DW_CFA_advance_loc";
510     case DW_CFA_offset:
511       return "DW_CFA_offset";
512     case DW_CFA_restore:
513       return "DW_CFA_restore";
514     case DW_CFA_nop:
515       return "DW_CFA_nop";
516     case DW_CFA_set_loc:
517       return "DW_CFA_set_loc";
518     case DW_CFA_advance_loc1:
519       return "DW_CFA_advance_loc1";
520     case DW_CFA_advance_loc2:
521       return "DW_CFA_advance_loc2";
522     case DW_CFA_advance_loc4:
523       return "DW_CFA_advance_loc4";
524     case DW_CFA_offset_extended:
525       return "DW_CFA_offset_extended";
526     case DW_CFA_restore_extended:
527       return "DW_CFA_restore_extended";
528     case DW_CFA_undefined:
529       return "DW_CFA_undefined";
530     case DW_CFA_same_value:
531       return "DW_CFA_same_value";
532     case DW_CFA_register:
533       return "DW_CFA_register";
534     case DW_CFA_remember_state:
535       return "DW_CFA_remember_state";
536     case DW_CFA_restore_state:
537       return "DW_CFA_restore_state";
538     case DW_CFA_def_cfa:
539       return "DW_CFA_def_cfa";
540     case DW_CFA_def_cfa_register:
541       return "DW_CFA_def_cfa_register";
542     case DW_CFA_def_cfa_offset:
543       return "DW_CFA_def_cfa_offset";
544
545     /* DWARF 3 */
546     case DW_CFA_def_cfa_expression:
547       return "DW_CFA_def_cfa_expression";
548     case DW_CFA_expression:
549       return "DW_CFA_expression";
550     case DW_CFA_offset_extended_sf:
551       return "DW_CFA_offset_extended_sf";
552     case DW_CFA_def_cfa_sf:
553       return "DW_CFA_def_cfa_sf";
554     case DW_CFA_def_cfa_offset_sf:
555       return "DW_CFA_def_cfa_offset_sf";
556
557     /* SGI/MIPS specific */
558     case DW_CFA_MIPS_advance_loc8:
559       return "DW_CFA_MIPS_advance_loc8";
560
561     /* GNU extensions */
562     case DW_CFA_GNU_window_save:
563       return "DW_CFA_GNU_window_save";
564     case DW_CFA_GNU_args_size:
565       return "DW_CFA_GNU_args_size";
566     case DW_CFA_GNU_negative_offset_extended:
567       return "DW_CFA_GNU_negative_offset_extended";
568
569     default:
570       return "DW_CFA_<unknown>";
571     }
572 }
573
574 /* Return a pointer to a newly allocated Call Frame Instruction.  */
575
576 static inline dw_cfi_ref
577 new_cfi (void)
578 {
579   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
580
581   cfi->dw_cfi_next = NULL;
582   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
583   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
584
585   return cfi;
586 }
587
588 /* Add a Call Frame Instruction to list of instructions.  */
589
590 static inline void
591 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
592 {
593   dw_cfi_ref *p;
594
595   /* Find the end of the chain.  */
596   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
597     ;
598
599   *p = cfi;
600 }
601
602 /* Generate a new label for the CFI info to refer to.  */
603
604 char *
605 dwarf2out_cfi_label (void)
606 {
607   static char label[20];
608
609   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
610   ASM_OUTPUT_LABEL (asm_out_file, label);
611   return label;
612 }
613
614 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
615    or to the CIE if LABEL is NULL.  */
616
617 static void
618 add_fde_cfi (const char *label, dw_cfi_ref cfi)
619 {
620   if (label)
621     {
622       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
623
624       if (*label == 0)
625         label = dwarf2out_cfi_label ();
626
627       if (fde->dw_fde_current_label == NULL
628           || strcmp (label, fde->dw_fde_current_label) != 0)
629         {
630           dw_cfi_ref xcfi;
631
632           label = xstrdup (label);
633
634           /* Set the location counter to the new label.  */
635           xcfi = new_cfi ();
636           /* If we have a current label, advance from there, otherwise
637              set the location directly using set_loc.  */
638           xcfi->dw_cfi_opc = fde->dw_fde_current_label
639                              ? DW_CFA_advance_loc4
640                              : DW_CFA_set_loc;
641           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
642           add_cfi (&fde->dw_fde_cfi, xcfi);
643
644           fde->dw_fde_current_label = label;
645         }
646
647       add_cfi (&fde->dw_fde_cfi, cfi);
648     }
649
650   else
651     add_cfi (&cie_cfi_head, cfi);
652 }
653
654 /* Subroutine of lookup_cfa.  */
655
656 static void
657 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
658 {
659   switch (cfi->dw_cfi_opc)
660     {
661     case DW_CFA_def_cfa_offset:
662       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
663       break;
664     case DW_CFA_def_cfa_offset_sf:
665       loc->offset
666         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
667       break;
668     case DW_CFA_def_cfa_register:
669       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
670       break;
671     case DW_CFA_def_cfa:
672       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
673       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
674       break;
675     case DW_CFA_def_cfa_sf:
676       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677       loc->offset
678         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
679       break;
680     case DW_CFA_def_cfa_expression:
681       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
682       break;
683     default:
684       break;
685     }
686 }
687
688 /* Find the previous value for the CFA.  */
689
690 static void
691 lookup_cfa (dw_cfa_location *loc)
692 {
693   dw_cfi_ref cfi;
694
695   loc->reg = INVALID_REGNUM;
696   loc->offset = 0;
697   loc->indirect = 0;
698   loc->base_offset = 0;
699
700   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
701     lookup_cfa_1 (cfi, loc);
702
703   if (fde_table_in_use)
704     {
705       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
706       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
707         lookup_cfa_1 (cfi, loc);
708     }
709 }
710
711 /* The current rule for calculating the DWARF2 canonical frame address.  */
712 static dw_cfa_location cfa;
713
714 /* The register used for saving registers to the stack, and its offset
715    from the CFA.  */
716 static dw_cfa_location cfa_store;
717
718 /* The running total of the size of arguments pushed onto the stack.  */
719 static HOST_WIDE_INT args_size;
720
721 /* The last args_size we actually output.  */
722 static HOST_WIDE_INT old_args_size;
723
724 /* Entry point to update the canonical frame address (CFA).
725    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
726    calculated from REG+OFFSET.  */
727
728 void
729 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
730 {
731   dw_cfa_location loc;
732   loc.indirect = 0;
733   loc.base_offset = 0;
734   loc.reg = reg;
735   loc.offset = offset;
736   def_cfa_1 (label, &loc);
737 }
738
739 /* Determine if two dw_cfa_location structures define the same data.  */
740
741 static bool
742 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
743 {
744   return (loc1->reg == loc2->reg
745           && loc1->offset == loc2->offset
746           && loc1->indirect == loc2->indirect
747           && (loc1->indirect == 0
748               || loc1->base_offset == loc2->base_offset));
749 }
750
751 /* This routine does the actual work.  The CFA is now calculated from
752    the dw_cfa_location structure.  */
753
754 static void
755 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
756 {
757   dw_cfi_ref cfi;
758   dw_cfa_location old_cfa, loc;
759
760   cfa = *loc_p;
761   loc = *loc_p;
762
763   if (cfa_store.reg == loc.reg && loc.indirect == 0)
764     cfa_store.offset = loc.offset;
765
766   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
767   lookup_cfa (&old_cfa);
768
769   /* If nothing changed, no need to issue any call frame instructions.  */
770   if (cfa_equal_p (&loc, &old_cfa))
771     return;
772
773   cfi = new_cfi ();
774
775   if (loc.reg == old_cfa.reg && !loc.indirect)
776     {
777       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
778          the CFA register did not change but the offset did.  */
779       if (loc.offset < 0)
780         {
781           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
782           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
783
784           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
785           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
786         }
787       else
788         {
789           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
790           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
791         }
792     }
793
794 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
795   else if (loc.offset == old_cfa.offset
796            && old_cfa.reg != INVALID_REGNUM
797            && !loc.indirect)
798     {
799       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
800          indicating the CFA register has changed to <register> but the
801          offset has not changed.  */
802       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
803       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
804     }
805 #endif
806
807   else if (loc.indirect == 0)
808     {
809       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
810          indicating the CFA register has changed to <register> with
811          the specified offset.  */
812       if (loc.offset < 0)
813         {
814           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
815           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
816
817           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
818           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
819           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
820         }
821       else
822         {
823           cfi->dw_cfi_opc = DW_CFA_def_cfa;
824           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
825           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
826         }
827     }
828   else
829     {
830       /* Construct a DW_CFA_def_cfa_expression instruction to
831          calculate the CFA using a full location expression since no
832          register-offset pair is available.  */
833       struct dw_loc_descr_struct *loc_list;
834
835       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
836       loc_list = build_cfa_loc (&loc, 0);
837       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
838     }
839
840   add_fde_cfi (label, cfi);
841 }
842
843 /* Add the CFI for saving a register.  REG is the CFA column number.
844    LABEL is passed to add_fde_cfi.
845    If SREG is -1, the register is saved at OFFSET from the CFA;
846    otherwise it is saved in SREG.  */
847
848 static void
849 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
850 {
851   dw_cfi_ref cfi = new_cfi ();
852
853   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
854
855   if (sreg == INVALID_REGNUM)
856     {
857       if (reg & ~0x3f)
858         /* The register number won't fit in 6 bits, so we have to use
859            the long form.  */
860         cfi->dw_cfi_opc = DW_CFA_offset_extended;
861       else
862         cfi->dw_cfi_opc = DW_CFA_offset;
863
864 #ifdef ENABLE_CHECKING
865       {
866         /* If we get an offset that is not a multiple of
867            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
868            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
869            description.  */
870         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
871
872         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
873       }
874 #endif
875       offset /= DWARF_CIE_DATA_ALIGNMENT;
876       if (offset < 0)
877         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
878
879       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
880     }
881   else if (sreg == reg)
882     cfi->dw_cfi_opc = DW_CFA_same_value;
883   else
884     {
885       cfi->dw_cfi_opc = DW_CFA_register;
886       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
887     }
888
889   add_fde_cfi (label, cfi);
890 }
891
892 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
893    This CFI tells the unwinder that it needs to restore the window registers
894    from the previous frame's window save area.
895
896    ??? Perhaps we should note in the CIE where windows are saved (instead of
897    assuming 0(cfa)) and what registers are in the window.  */
898
899 void
900 dwarf2out_window_save (const char *label)
901 {
902   dw_cfi_ref cfi = new_cfi ();
903
904   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
905   add_fde_cfi (label, cfi);
906 }
907
908 /* Add a CFI to update the running total of the size of arguments
909    pushed onto the stack.  */
910
911 void
912 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
913 {
914   dw_cfi_ref cfi;
915
916   if (size == old_args_size)
917     return;
918
919   old_args_size = size;
920
921   cfi = new_cfi ();
922   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
923   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
924   add_fde_cfi (label, cfi);
925 }
926
927 /* Entry point for saving a register to the stack.  REG is the GCC register
928    number.  LABEL and OFFSET are passed to reg_save.  */
929
930 void
931 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
932 {
933   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
934 }
935
936 /* Entry point for saving the return address in the stack.
937    LABEL and OFFSET are passed to reg_save.  */
938
939 void
940 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
941 {
942   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
943 }
944
945 /* Entry point for saving the return address in a register.
946    LABEL and SREG are passed to reg_save.  */
947
948 void
949 dwarf2out_return_reg (const char *label, unsigned int sreg)
950 {
951   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
952 }
953
954 /* Record the initial position of the return address.  RTL is
955    INCOMING_RETURN_ADDR_RTX.  */
956
957 static void
958 initial_return_save (rtx rtl)
959 {
960   unsigned int reg = INVALID_REGNUM;
961   HOST_WIDE_INT offset = 0;
962
963   switch (GET_CODE (rtl))
964     {
965     case REG:
966       /* RA is in a register.  */
967       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
968       break;
969
970     case MEM:
971       /* RA is on the stack.  */
972       rtl = XEXP (rtl, 0);
973       switch (GET_CODE (rtl))
974         {
975         case REG:
976           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
977           offset = 0;
978           break;
979
980         case PLUS:
981           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
982           offset = INTVAL (XEXP (rtl, 1));
983           break;
984
985         case MINUS:
986           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
987           offset = -INTVAL (XEXP (rtl, 1));
988           break;
989
990         default:
991           gcc_unreachable ();
992         }
993
994       break;
995
996     case PLUS:
997       /* The return address is at some offset from any value we can
998          actually load.  For instance, on the SPARC it is in %i7+8. Just
999          ignore the offset for now; it doesn't matter for unwinding frames.  */
1000       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1001       initial_return_save (XEXP (rtl, 0));
1002       return;
1003
1004     default:
1005       gcc_unreachable ();
1006     }
1007
1008   if (reg != DWARF_FRAME_RETURN_COLUMN)
1009     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1010 }
1011
1012 /* Given a SET, calculate the amount of stack adjustment it
1013    contains.  */
1014
1015 static HOST_WIDE_INT
1016 stack_adjust_offset (rtx pattern)
1017 {
1018   rtx src = SET_SRC (pattern);
1019   rtx dest = SET_DEST (pattern);
1020   HOST_WIDE_INT offset = 0;
1021   enum rtx_code code;
1022
1023   if (dest == stack_pointer_rtx)
1024     {
1025       /* (set (reg sp) (plus (reg sp) (const_int))) */
1026       code = GET_CODE (src);
1027       if (! (code == PLUS || code == MINUS)
1028           || XEXP (src, 0) != stack_pointer_rtx
1029           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1030         return 0;
1031
1032       offset = INTVAL (XEXP (src, 1));
1033       if (code == PLUS)
1034         offset = -offset;
1035     }
1036   else if (MEM_P (dest))
1037     {
1038       /* (set (mem (pre_dec (reg sp))) (foo)) */
1039       src = XEXP (dest, 0);
1040       code = GET_CODE (src);
1041
1042       switch (code)
1043         {
1044         case PRE_MODIFY:
1045         case POST_MODIFY:
1046           if (XEXP (src, 0) == stack_pointer_rtx)
1047             {
1048               rtx val = XEXP (XEXP (src, 1), 1);
1049               /* We handle only adjustments by constant amount.  */
1050               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1051                           && GET_CODE (val) == CONST_INT);
1052               offset = -INTVAL (val);
1053               break;
1054             }
1055           return 0;
1056
1057         case PRE_DEC:
1058         case POST_DEC:
1059           if (XEXP (src, 0) == stack_pointer_rtx)
1060             {
1061               offset = GET_MODE_SIZE (GET_MODE (dest));
1062               break;
1063             }
1064           return 0;
1065
1066         case PRE_INC:
1067         case POST_INC:
1068           if (XEXP (src, 0) == stack_pointer_rtx)
1069             {
1070               offset = -GET_MODE_SIZE (GET_MODE (dest));
1071               break;
1072             }
1073           return 0;
1074
1075         default:
1076           return 0;
1077         }
1078     }
1079   else
1080     return 0;
1081
1082   return offset;
1083 }
1084
1085 /* Check INSN to see if it looks like a push or a stack adjustment, and
1086    make a note of it if it does.  EH uses this information to find out how
1087    much extra space it needs to pop off the stack.  */
1088
1089 static void
1090 dwarf2out_stack_adjust (rtx insn, bool after_p)
1091 {
1092   HOST_WIDE_INT offset;
1093   const char *label;
1094   int i;
1095
1096   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1097      with this function.  Proper support would require all frame-related
1098      insns to be marked, and to be able to handle saving state around
1099      epilogues textually in the middle of the function.  */
1100   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1101     return;
1102
1103   /* If only calls can throw, and we have a frame pointer,
1104      save up adjustments until we see the CALL_INSN.  */
1105   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1106     {
1107       if (CALL_P (insn) && !after_p)
1108         {
1109           /* Extract the size of the args from the CALL rtx itself.  */
1110           insn = PATTERN (insn);
1111           if (GET_CODE (insn) == PARALLEL)
1112             insn = XVECEXP (insn, 0, 0);
1113           if (GET_CODE (insn) == SET)
1114             insn = SET_SRC (insn);
1115           gcc_assert (GET_CODE (insn) == CALL);
1116           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1117         }
1118       return;
1119     }
1120
1121   if (CALL_P (insn) && !after_p)
1122     {
1123       if (!flag_asynchronous_unwind_tables)
1124         dwarf2out_args_size ("", args_size);
1125       return;
1126     }
1127   else if (BARRIER_P (insn))
1128     {
1129       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1130          the compiler will have already emitted a stack adjustment, but
1131          doesn't bother for calls to noreturn functions.  */
1132 #ifdef STACK_GROWS_DOWNWARD
1133       offset = -args_size;
1134 #else
1135       offset = args_size;
1136 #endif
1137     }
1138   else if (GET_CODE (PATTERN (insn)) == SET)
1139     offset = stack_adjust_offset (PATTERN (insn));
1140   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1141            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1142     {
1143       /* There may be stack adjustments inside compound insns.  Search
1144          for them.  */
1145       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1146         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1147           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1148     }
1149   else
1150     return;
1151
1152   if (offset == 0)
1153     return;
1154
1155   if (cfa.reg == STACK_POINTER_REGNUM)
1156     cfa.offset += offset;
1157
1158 #ifndef STACK_GROWS_DOWNWARD
1159   offset = -offset;
1160 #endif
1161
1162   args_size += offset;
1163   if (args_size < 0)
1164     args_size = 0;
1165
1166   label = dwarf2out_cfi_label ();
1167   def_cfa_1 (label, &cfa);
1168   if (flag_asynchronous_unwind_tables)
1169     dwarf2out_args_size (label, args_size);
1170 }
1171
1172 #endif
1173
1174 /* We delay emitting a register save until either (a) we reach the end
1175    of the prologue or (b) the register is clobbered.  This clusters
1176    register saves so that there are fewer pc advances.  */
1177
1178 struct queued_reg_save GTY(())
1179 {
1180   struct queued_reg_save *next;
1181   rtx reg;
1182   HOST_WIDE_INT cfa_offset;
1183   rtx saved_reg;
1184 };
1185
1186 static GTY(()) struct queued_reg_save *queued_reg_saves;
1187
1188 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1189 struct reg_saved_in_data GTY(()) {
1190   rtx orig_reg;
1191   rtx saved_in_reg;
1192 };
1193
1194 /* A list of registers saved in other registers.
1195    The list intentionally has a small maximum capacity of 4; if your
1196    port needs more than that, you might consider implementing a
1197    more efficient data structure.  */
1198 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1199 static GTY(()) size_t num_regs_saved_in_regs;
1200
1201 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1202 static const char *last_reg_save_label;
1203
1204 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1205    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1206
1207 static void
1208 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1209 {
1210   struct queued_reg_save *q;
1211
1212   /* Duplicates waste space, but it's also necessary to remove them
1213      for correctness, since the queue gets output in reverse
1214      order.  */
1215   for (q = queued_reg_saves; q != NULL; q = q->next)
1216     if (REGNO (q->reg) == REGNO (reg))
1217       break;
1218
1219   if (q == NULL)
1220     {
1221       q = ggc_alloc (sizeof (*q));
1222       q->next = queued_reg_saves;
1223       queued_reg_saves = q;
1224     }
1225
1226   q->reg = reg;
1227   q->cfa_offset = offset;
1228   q->saved_reg = sreg;
1229
1230   last_reg_save_label = label;
1231 }
1232
1233 /* Output all the entries in QUEUED_REG_SAVES.  */
1234
1235 static void
1236 flush_queued_reg_saves (void)
1237 {
1238   struct queued_reg_save *q;
1239
1240   for (q = queued_reg_saves; q; q = q->next)
1241     {
1242       size_t i;
1243       unsigned int reg, sreg;
1244
1245       for (i = 0; i < num_regs_saved_in_regs; i++)
1246         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1247           break;
1248       if (q->saved_reg && i == num_regs_saved_in_regs)
1249         {
1250           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1251           num_regs_saved_in_regs++;
1252         }
1253       if (i != num_regs_saved_in_regs)
1254         {
1255           regs_saved_in_regs[i].orig_reg = q->reg;
1256           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1257         }
1258
1259       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1260       if (q->saved_reg)
1261         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1262       else
1263         sreg = INVALID_REGNUM;
1264       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1265     }
1266
1267   queued_reg_saves = NULL;
1268   last_reg_save_label = NULL;
1269 }
1270
1271 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1272    location for?  Or, does it clobber a register which we've previously
1273    said that some other register is saved in, and for which we now
1274    have a new location for?  */
1275
1276 static bool
1277 clobbers_queued_reg_save (rtx insn)
1278 {
1279   struct queued_reg_save *q;
1280
1281   for (q = queued_reg_saves; q; q = q->next)
1282     {
1283       size_t i;
1284       if (modified_in_p (q->reg, insn))
1285         return true;
1286       for (i = 0; i < num_regs_saved_in_regs; i++)
1287         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1288             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1289           return true;
1290     }
1291
1292   return false;
1293 }
1294
1295 /* Entry point for saving the first register into the second.  */
1296
1297 void
1298 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1299 {
1300   size_t i;
1301   unsigned int regno, sregno;
1302
1303   for (i = 0; i < num_regs_saved_in_regs; i++)
1304     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1305       break;
1306   if (i == num_regs_saved_in_regs)
1307     {
1308       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1309       num_regs_saved_in_regs++;
1310     }
1311   regs_saved_in_regs[i].orig_reg = reg;
1312   regs_saved_in_regs[i].saved_in_reg = sreg;
1313
1314   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1315   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1316   reg_save (label, regno, sregno, 0);
1317 }
1318
1319 /* What register, if any, is currently saved in REG?  */
1320
1321 static rtx
1322 reg_saved_in (rtx reg)
1323 {
1324   unsigned int regn = REGNO (reg);
1325   size_t i;
1326   struct queued_reg_save *q;
1327
1328   for (q = queued_reg_saves; q; q = q->next)
1329     if (q->saved_reg && regn == REGNO (q->saved_reg))
1330       return q->reg;
1331
1332   for (i = 0; i < num_regs_saved_in_regs; i++)
1333     if (regs_saved_in_regs[i].saved_in_reg
1334         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1335       return regs_saved_in_regs[i].orig_reg;
1336
1337   return NULL_RTX;
1338 }
1339
1340
1341 /* A temporary register holding an integral value used in adjusting SP
1342    or setting up the store_reg.  The "offset" field holds the integer
1343    value, not an offset.  */
1344 static dw_cfa_location cfa_temp;
1345
1346 /* Record call frame debugging information for an expression EXPR,
1347    which either sets SP or FP (adjusting how we calculate the frame
1348    address) or saves a register to the stack or another register.
1349    LABEL indicates the address of EXPR.
1350
1351    This function encodes a state machine mapping rtxes to actions on
1352    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1353    users need not read the source code.
1354
1355   The High-Level Picture
1356
1357   Changes in the register we use to calculate the CFA: Currently we
1358   assume that if you copy the CFA register into another register, we
1359   should take the other one as the new CFA register; this seems to
1360   work pretty well.  If it's wrong for some target, it's simple
1361   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1362
1363   Changes in the register we use for saving registers to the stack:
1364   This is usually SP, but not always.  Again, we deduce that if you
1365   copy SP into another register (and SP is not the CFA register),
1366   then the new register is the one we will be using for register
1367   saves.  This also seems to work.
1368
1369   Register saves: There's not much guesswork about this one; if
1370   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1371   register save, and the register used to calculate the destination
1372   had better be the one we think we're using for this purpose.
1373   It's also assumed that a copy from a call-saved register to another
1374   register is saving that register if RTX_FRAME_RELATED_P is set on
1375   that instruction.  If the copy is from a call-saved register to
1376   the *same* register, that means that the register is now the same
1377   value as in the caller.
1378
1379   Except: If the register being saved is the CFA register, and the
1380   offset is nonzero, we are saving the CFA, so we assume we have to
1381   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1382   the intent is to save the value of SP from the previous frame.
1383
1384   In addition, if a register has previously been saved to a different
1385   register,
1386
1387   Invariants / Summaries of Rules
1388
1389   cfa          current rule for calculating the CFA.  It usually
1390                consists of a register and an offset.
1391   cfa_store    register used by prologue code to save things to the stack
1392                cfa_store.offset is the offset from the value of
1393                cfa_store.reg to the actual CFA
1394   cfa_temp     register holding an integral value.  cfa_temp.offset
1395                stores the value, which will be used to adjust the
1396                stack pointer.  cfa_temp is also used like cfa_store,
1397                to track stores to the stack via fp or a temp reg.
1398
1399   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1400                with cfa.reg as the first operand changes the cfa.reg and its
1401                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1402                cfa_temp.offset.
1403
1404   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1405                expression yielding a constant.  This sets cfa_temp.reg
1406                and cfa_temp.offset.
1407
1408   Rule 5:      Create a new register cfa_store used to save items to the
1409                stack.
1410
1411   Rules 10-14: Save a register to the stack.  Define offset as the
1412                difference of the original location and cfa_store's
1413                location (or cfa_temp's location if cfa_temp is used).
1414
1415   The Rules
1416
1417   "{a,b}" indicates a choice of a xor b.
1418   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1419
1420   Rule 1:
1421   (set <reg1> <reg2>:cfa.reg)
1422   effects: cfa.reg = <reg1>
1423            cfa.offset unchanged
1424            cfa_temp.reg = <reg1>
1425            cfa_temp.offset = cfa.offset
1426
1427   Rule 2:
1428   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1429                               {<const_int>,<reg>:cfa_temp.reg}))
1430   effects: cfa.reg = sp if fp used
1431            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1432            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1433              if cfa_store.reg==sp
1434
1435   Rule 3:
1436   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1437   effects: cfa.reg = fp
1438            cfa_offset += +/- <const_int>
1439
1440   Rule 4:
1441   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1442   constraints: <reg1> != fp
1443                <reg1> != sp
1444   effects: cfa.reg = <reg1>
1445            cfa_temp.reg = <reg1>
1446            cfa_temp.offset = cfa.offset
1447
1448   Rule 5:
1449   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1450   constraints: <reg1> != fp
1451                <reg1> != sp
1452   effects: cfa_store.reg = <reg1>
1453            cfa_store.offset = cfa.offset - cfa_temp.offset
1454
1455   Rule 6:
1456   (set <reg> <const_int>)
1457   effects: cfa_temp.reg = <reg>
1458            cfa_temp.offset = <const_int>
1459
1460   Rule 7:
1461   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1462   effects: cfa_temp.reg = <reg1>
1463            cfa_temp.offset |= <const_int>
1464
1465   Rule 8:
1466   (set <reg> (high <exp>))
1467   effects: none
1468
1469   Rule 9:
1470   (set <reg> (lo_sum <exp> <const_int>))
1471   effects: cfa_temp.reg = <reg>
1472            cfa_temp.offset = <const_int>
1473
1474   Rule 10:
1475   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1476   effects: cfa_store.offset -= <const_int>
1477            cfa.offset = cfa_store.offset if cfa.reg == sp
1478            cfa.reg = sp
1479            cfa.base_offset = -cfa_store.offset
1480
1481   Rule 11:
1482   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1483   effects: cfa_store.offset += -/+ mode_size(mem)
1484            cfa.offset = cfa_store.offset if cfa.reg == sp
1485            cfa.reg = sp
1486            cfa.base_offset = -cfa_store.offset
1487
1488   Rule 12:
1489   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1490
1491        <reg2>)
1492   effects: cfa.reg = <reg1>
1493            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1494
1495   Rule 13:
1496   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1497   effects: cfa.reg = <reg1>
1498            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1499
1500   Rule 14:
1501   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1502   effects: cfa.reg = <reg1>
1503            cfa.base_offset = -cfa_temp.offset
1504            cfa_temp.offset -= mode_size(mem)
1505
1506   Rule 15:
1507   (set <reg> {unspec, unspec_volatile})
1508   effects: target-dependent  */
1509
1510 static void
1511 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1512 {
1513   rtx src, dest;
1514   HOST_WIDE_INT offset;
1515
1516   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1517      the PARALLEL independently. The first element is always processed if
1518      it is a SET. This is for backward compatibility.   Other elements
1519      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1520      flag is set in them.  */
1521   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1522     {
1523       int par_index;
1524       int limit = XVECLEN (expr, 0);
1525
1526       for (par_index = 0; par_index < limit; par_index++)
1527         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1528             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1529                 || par_index == 0))
1530           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1531
1532       return;
1533     }
1534
1535   gcc_assert (GET_CODE (expr) == SET);
1536
1537   src = SET_SRC (expr);
1538   dest = SET_DEST (expr);
1539
1540   if (REG_P (src))
1541     {
1542       rtx rsi = reg_saved_in (src);
1543       if (rsi)
1544         src = rsi;
1545     }
1546
1547   switch (GET_CODE (dest))
1548     {
1549     case REG:
1550       switch (GET_CODE (src))
1551         {
1552           /* Setting FP from SP.  */
1553         case REG:
1554           if (cfa.reg == (unsigned) REGNO (src))
1555             {
1556               /* Rule 1 */
1557               /* Update the CFA rule wrt SP or FP.  Make sure src is
1558                  relative to the current CFA register.
1559
1560                  We used to require that dest be either SP or FP, but the
1561                  ARM copies SP to a temporary register, and from there to
1562                  FP.  So we just rely on the backends to only set
1563                  RTX_FRAME_RELATED_P on appropriate insns.  */
1564               cfa.reg = REGNO (dest);
1565               cfa_temp.reg = cfa.reg;
1566               cfa_temp.offset = cfa.offset;
1567             }
1568           else
1569             {
1570               /* Saving a register in a register.  */
1571               gcc_assert (!fixed_regs [REGNO (dest)]
1572                           /* For the SPARC and its register window.  */
1573                           || (DWARF_FRAME_REGNUM (REGNO (src))
1574                               == DWARF_FRAME_RETURN_COLUMN));
1575               queue_reg_save (label, src, dest, 0);
1576             }
1577           break;
1578
1579         case PLUS:
1580         case MINUS:
1581         case LO_SUM:
1582           if (dest == stack_pointer_rtx)
1583             {
1584               /* Rule 2 */
1585               /* Adjusting SP.  */
1586               switch (GET_CODE (XEXP (src, 1)))
1587                 {
1588                 case CONST_INT:
1589                   offset = INTVAL (XEXP (src, 1));
1590                   break;
1591                 case REG:
1592                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1593                               == cfa_temp.reg);
1594                   offset = cfa_temp.offset;
1595                   break;
1596                 default:
1597                   gcc_unreachable ();
1598                 }
1599
1600               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1601                 {
1602                   /* Restoring SP from FP in the epilogue.  */
1603                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1604                   cfa.reg = STACK_POINTER_REGNUM;
1605                 }
1606               else if (GET_CODE (src) == LO_SUM)
1607                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1608                 ;
1609               else
1610                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1611
1612               if (GET_CODE (src) != MINUS)
1613                 offset = -offset;
1614               if (cfa.reg == STACK_POINTER_REGNUM)
1615                 cfa.offset += offset;
1616               if (cfa_store.reg == STACK_POINTER_REGNUM)
1617                 cfa_store.offset += offset;
1618             }
1619           else if (dest == hard_frame_pointer_rtx)
1620             {
1621               /* Rule 3 */
1622               /* Either setting the FP from an offset of the SP,
1623                  or adjusting the FP */
1624               gcc_assert (frame_pointer_needed);
1625
1626               gcc_assert (REG_P (XEXP (src, 0))
1627                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1628                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1629               offset = INTVAL (XEXP (src, 1));
1630               if (GET_CODE (src) != MINUS)
1631                 offset = -offset;
1632               cfa.offset += offset;
1633               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1634             }
1635           else
1636             {
1637               gcc_assert (GET_CODE (src) != MINUS);
1638
1639               /* Rule 4 */
1640               if (REG_P (XEXP (src, 0))
1641                   && REGNO (XEXP (src, 0)) == cfa.reg
1642                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1643                 {
1644                   /* Setting a temporary CFA register that will be copied
1645                      into the FP later on.  */
1646                   offset = - INTVAL (XEXP (src, 1));
1647                   cfa.offset += offset;
1648                   cfa.reg = REGNO (dest);
1649                   /* Or used to save regs to the stack.  */
1650                   cfa_temp.reg = cfa.reg;
1651                   cfa_temp.offset = cfa.offset;
1652                 }
1653
1654               /* Rule 5 */
1655               else if (REG_P (XEXP (src, 0))
1656                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1657                        && XEXP (src, 1) == stack_pointer_rtx)
1658                 {
1659                   /* Setting a scratch register that we will use instead
1660                      of SP for saving registers to the stack.  */
1661                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1662                   cfa_store.reg = REGNO (dest);
1663                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1664                 }
1665
1666               /* Rule 9 */
1667               else if (GET_CODE (src) == LO_SUM
1668                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1669                 {
1670                   cfa_temp.reg = REGNO (dest);
1671                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1672                 }
1673               else
1674                 gcc_unreachable ();
1675             }
1676           break;
1677
1678           /* Rule 6 */
1679         case CONST_INT:
1680           cfa_temp.reg = REGNO (dest);
1681           cfa_temp.offset = INTVAL (src);
1682           break;
1683
1684           /* Rule 7 */
1685         case IOR:
1686           gcc_assert (REG_P (XEXP (src, 0))
1687                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1688                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1689
1690           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1691             cfa_temp.reg = REGNO (dest);
1692           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1693           break;
1694
1695           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1696              which will fill in all of the bits.  */
1697           /* Rule 8 */
1698         case HIGH:
1699           break;
1700
1701           /* Rule 15 */
1702         case UNSPEC:
1703         case UNSPEC_VOLATILE:
1704           gcc_assert (targetm.dwarf_handle_frame_unspec);
1705           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1706           return;
1707
1708         default:
1709           gcc_unreachable ();
1710         }
1711
1712       def_cfa_1 (label, &cfa);
1713       break;
1714
1715     case MEM:
1716       gcc_assert (REG_P (src));
1717
1718       /* Saving a register to the stack.  Make sure dest is relative to the
1719          CFA register.  */
1720       switch (GET_CODE (XEXP (dest, 0)))
1721         {
1722           /* Rule 10 */
1723           /* With a push.  */
1724         case PRE_MODIFY:
1725           /* We can't handle variable size modifications.  */
1726           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1727                       == CONST_INT);
1728           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1729
1730           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1731                       && cfa_store.reg == STACK_POINTER_REGNUM);
1732
1733           cfa_store.offset += offset;
1734           if (cfa.reg == STACK_POINTER_REGNUM)
1735             cfa.offset = cfa_store.offset;
1736
1737           offset = -cfa_store.offset;
1738           break;
1739
1740           /* Rule 11 */
1741         case PRE_INC:
1742         case PRE_DEC:
1743           offset = GET_MODE_SIZE (GET_MODE (dest));
1744           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1745             offset = -offset;
1746
1747           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1748                       && cfa_store.reg == STACK_POINTER_REGNUM);
1749
1750           cfa_store.offset += offset;
1751           if (cfa.reg == STACK_POINTER_REGNUM)
1752             cfa.offset = cfa_store.offset;
1753
1754           offset = -cfa_store.offset;
1755           break;
1756
1757           /* Rule 12 */
1758           /* With an offset.  */
1759         case PLUS:
1760         case MINUS:
1761         case LO_SUM:
1762           {
1763             int regno;
1764
1765             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1766                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1767             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1768             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1769               offset = -offset;
1770
1771             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1772
1773             if (cfa_store.reg == (unsigned) regno)
1774               offset -= cfa_store.offset;
1775             else
1776               {
1777                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1778                 offset -= cfa_temp.offset;
1779               }
1780           }
1781           break;
1782
1783           /* Rule 13 */
1784           /* Without an offset.  */
1785         case REG:
1786           {
1787             int regno = REGNO (XEXP (dest, 0));
1788
1789             if (cfa_store.reg == (unsigned) regno)
1790               offset = -cfa_store.offset;
1791             else
1792               {
1793                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1794                 offset = -cfa_temp.offset;
1795               }
1796           }
1797           break;
1798
1799           /* Rule 14 */
1800         case POST_INC:
1801           gcc_assert (cfa_temp.reg
1802                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1803           offset = -cfa_temp.offset;
1804           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1805           break;
1806
1807         default:
1808           gcc_unreachable ();
1809         }
1810
1811       if (REGNO (src) != STACK_POINTER_REGNUM
1812           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1813           && (unsigned) REGNO (src) == cfa.reg)
1814         {
1815           /* We're storing the current CFA reg into the stack.  */
1816
1817           if (cfa.offset == 0)
1818             {
1819               /* If the source register is exactly the CFA, assume
1820                  we're saving SP like any other register; this happens
1821                  on the ARM.  */
1822               def_cfa_1 (label, &cfa);
1823               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1824               break;
1825             }
1826           else
1827             {
1828               /* Otherwise, we'll need to look in the stack to
1829                  calculate the CFA.  */
1830               rtx x = XEXP (dest, 0);
1831
1832               if (!REG_P (x))
1833                 x = XEXP (x, 0);
1834               gcc_assert (REG_P (x));
1835
1836               cfa.reg = REGNO (x);
1837               cfa.base_offset = offset;
1838               cfa.indirect = 1;
1839               def_cfa_1 (label, &cfa);
1840               break;
1841             }
1842         }
1843
1844       def_cfa_1 (label, &cfa);
1845       queue_reg_save (label, src, NULL_RTX, offset);
1846       break;
1847
1848     default:
1849       gcc_unreachable ();
1850     }
1851 }
1852
1853 /* Record call frame debugging information for INSN, which either
1854    sets SP or FP (adjusting how we calculate the frame address) or saves a
1855    register to the stack.  If INSN is NULL_RTX, initialize our state.
1856
1857    If AFTER_P is false, we're being called before the insn is emitted,
1858    otherwise after.  Call instructions get invoked twice.  */
1859
1860 void
1861 dwarf2out_frame_debug (rtx insn, bool after_p)
1862 {
1863   const char *label;
1864   rtx src;
1865
1866   if (insn == NULL_RTX)
1867     {
1868       size_t i;
1869
1870       /* Flush any queued register saves.  */
1871       flush_queued_reg_saves ();
1872
1873       /* Set up state for generating call frame debug info.  */
1874       lookup_cfa (&cfa);
1875       gcc_assert (cfa.reg
1876                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1877
1878       cfa.reg = STACK_POINTER_REGNUM;
1879       cfa_store = cfa;
1880       cfa_temp.reg = -1;
1881       cfa_temp.offset = 0;
1882
1883       for (i = 0; i < num_regs_saved_in_regs; i++)
1884         {
1885           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1886           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1887         }
1888       num_regs_saved_in_regs = 0;
1889       return;
1890     }
1891
1892   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1893     flush_queued_reg_saves ();
1894
1895   if (! RTX_FRAME_RELATED_P (insn))
1896     {
1897       if (!ACCUMULATE_OUTGOING_ARGS)
1898         dwarf2out_stack_adjust (insn, after_p);
1899       return;
1900     }
1901
1902   label = dwarf2out_cfi_label ();
1903   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1904   if (src)
1905     insn = XEXP (src, 0);
1906   else
1907     insn = PATTERN (insn);
1908
1909   dwarf2out_frame_debug_expr (insn, label);
1910 }
1911
1912 #endif
1913
1914 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1915 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1916  (enum dwarf_call_frame_info cfi);
1917
1918 static enum dw_cfi_oprnd_type
1919 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1920 {
1921   switch (cfi)
1922     {
1923     case DW_CFA_nop:
1924     case DW_CFA_GNU_window_save:
1925       return dw_cfi_oprnd_unused;
1926
1927     case DW_CFA_set_loc:
1928     case DW_CFA_advance_loc1:
1929     case DW_CFA_advance_loc2:
1930     case DW_CFA_advance_loc4:
1931     case DW_CFA_MIPS_advance_loc8:
1932       return dw_cfi_oprnd_addr;
1933
1934     case DW_CFA_offset:
1935     case DW_CFA_offset_extended:
1936     case DW_CFA_def_cfa:
1937     case DW_CFA_offset_extended_sf:
1938     case DW_CFA_def_cfa_sf:
1939     case DW_CFA_restore_extended:
1940     case DW_CFA_undefined:
1941     case DW_CFA_same_value:
1942     case DW_CFA_def_cfa_register:
1943     case DW_CFA_register:
1944       return dw_cfi_oprnd_reg_num;
1945
1946     case DW_CFA_def_cfa_offset:
1947     case DW_CFA_GNU_args_size:
1948     case DW_CFA_def_cfa_offset_sf:
1949       return dw_cfi_oprnd_offset;
1950
1951     case DW_CFA_def_cfa_expression:
1952     case DW_CFA_expression:
1953       return dw_cfi_oprnd_loc;
1954
1955     default:
1956       gcc_unreachable ();
1957     }
1958 }
1959
1960 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1961 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1962  (enum dwarf_call_frame_info cfi);
1963
1964 static enum dw_cfi_oprnd_type
1965 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1966 {
1967   switch (cfi)
1968     {
1969     case DW_CFA_def_cfa:
1970     case DW_CFA_def_cfa_sf:
1971     case DW_CFA_offset:
1972     case DW_CFA_offset_extended_sf:
1973     case DW_CFA_offset_extended:
1974       return dw_cfi_oprnd_offset;
1975
1976     case DW_CFA_register:
1977       return dw_cfi_oprnd_reg_num;
1978
1979     default:
1980       return dw_cfi_oprnd_unused;
1981     }
1982 }
1983
1984 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1985
1986 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
1987    switch to the data section instead, and write out a synthetic label
1988    for collect2.  */
1989
1990 static void
1991 switch_to_eh_frame_section (void)
1992 {
1993   tree label;
1994
1995 #ifdef EH_FRAME_SECTION_NAME
1996   if (eh_frame_section == 0)
1997     {
1998       int flags;
1999
2000       if (EH_TABLES_CAN_BE_READ_ONLY)
2001         {
2002           int fde_encoding;
2003           int per_encoding;
2004           int lsda_encoding;
2005
2006           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2007                                                        /*global=*/0);
2008           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2009                                                        /*global=*/1);
2010           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2011                                                         /*global=*/0);
2012           flags = ((! flag_pic
2013                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2014                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2015                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2016                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2017                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2018                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2019                    ? 0 : SECTION_WRITE);
2020         }
2021       else
2022         flags = SECTION_WRITE;
2023       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2024     }
2025 #endif
2026
2027   if (eh_frame_section)
2028     switch_to_section (eh_frame_section);
2029   else
2030     {
2031       /* We have no special eh_frame section.  Put the information in
2032          the data section and emit special labels to guide collect2.  */
2033       switch_to_section (data_section);
2034       label = get_file_function_name ("F");
2035       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2036       targetm.asm_out.globalize_label (asm_out_file,
2037                                        IDENTIFIER_POINTER (label));
2038       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2039     }
2040 }
2041
2042 /* Output a Call Frame Information opcode and its operand(s).  */
2043
2044 static void
2045 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2046 {
2047   unsigned long r;
2048   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2049     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2050                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2051                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2052                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2053   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2054     {
2055       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2056       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2057                            "DW_CFA_offset, column 0x%lx", r);
2058       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2059     }
2060   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2061     {
2062       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2063       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2064                            "DW_CFA_restore, column 0x%lx", r);
2065     }
2066   else
2067     {
2068       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2069                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2070
2071       switch (cfi->dw_cfi_opc)
2072         {
2073         case DW_CFA_set_loc:
2074           if (for_eh)
2075             dw2_asm_output_encoded_addr_rtx (
2076                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2077                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2078                 false, NULL);
2079           else
2080             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2081                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2082           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2083           break;
2084
2085         case DW_CFA_advance_loc1:
2086           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2087                                 fde->dw_fde_current_label, NULL);
2088           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2089           break;
2090
2091         case DW_CFA_advance_loc2:
2092           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2093                                 fde->dw_fde_current_label, NULL);
2094           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2095           break;
2096
2097         case DW_CFA_advance_loc4:
2098           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2099                                 fde->dw_fde_current_label, NULL);
2100           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2101           break;
2102
2103         case DW_CFA_MIPS_advance_loc8:
2104           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2105                                 fde->dw_fde_current_label, NULL);
2106           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2107           break;
2108
2109         case DW_CFA_offset_extended:
2110         case DW_CFA_def_cfa:
2111           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2112           dw2_asm_output_data_uleb128 (r, NULL);
2113           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2114           break;
2115
2116         case DW_CFA_offset_extended_sf:
2117         case DW_CFA_def_cfa_sf:
2118           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2119           dw2_asm_output_data_uleb128 (r, NULL);
2120           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2121           break;
2122
2123         case DW_CFA_restore_extended:
2124         case DW_CFA_undefined:
2125         case DW_CFA_same_value:
2126         case DW_CFA_def_cfa_register:
2127           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2128           dw2_asm_output_data_uleb128 (r, NULL);
2129           break;
2130
2131         case DW_CFA_register:
2132           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2133           dw2_asm_output_data_uleb128 (r, NULL);
2134           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2135           dw2_asm_output_data_uleb128 (r, NULL);
2136           break;
2137
2138         case DW_CFA_def_cfa_offset:
2139         case DW_CFA_GNU_args_size:
2140           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2141           break;
2142
2143         case DW_CFA_def_cfa_offset_sf:
2144           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2145           break;
2146
2147         case DW_CFA_GNU_window_save:
2148           break;
2149
2150         case DW_CFA_def_cfa_expression:
2151         case DW_CFA_expression:
2152           output_cfa_loc (cfi);
2153           break;
2154
2155         case DW_CFA_GNU_negative_offset_extended:
2156           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2157           gcc_unreachable ();
2158
2159         default:
2160           break;
2161         }
2162     }
2163 }
2164
2165 /* Output the call frame information used to record information
2166    that relates to calculating the frame pointer, and records the
2167    location of saved registers.  */
2168
2169 static void
2170 output_call_frame_info (int for_eh)
2171 {
2172   unsigned int i;
2173   dw_fde_ref fde;
2174   dw_cfi_ref cfi;
2175   char l1[20], l2[20], section_start_label[20];
2176   bool any_lsda_needed = false;
2177   char augmentation[6];
2178   int augmentation_size;
2179   int fde_encoding = DW_EH_PE_absptr;
2180   int per_encoding = DW_EH_PE_absptr;
2181   int lsda_encoding = DW_EH_PE_absptr;
2182   int return_reg;
2183
2184   /* Don't emit a CIE if there won't be any FDEs.  */
2185   if (fde_table_in_use == 0)
2186     return;
2187
2188   /* If we make FDEs linkonce, we may have to emit an empty label for
2189      an FDE that wouldn't otherwise be emitted.  We want to avoid
2190      having an FDE kept around when the function it refers to is
2191      discarded.  Example where this matters: a primary function
2192      template in C++ requires EH information, but an explicit
2193      specialization doesn't.  */
2194   if (TARGET_USES_WEAK_UNWIND_INFO
2195       && ! flag_asynchronous_unwind_tables
2196 /* APPLE LOCAL begin for-fsf-4_4 5480287 */ \
2197       && flag_exceptions
2198 /* APPLE LOCAL end for-fsf-4_4 5480287 */ \
2199       && for_eh)
2200     for (i = 0; i < fde_table_in_use; i++)
2201       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2202           && !fde_table[i].uses_eh_lsda
2203           && ! DECL_WEAK (fde_table[i].decl))
2204         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2205                                       for_eh, /* empty */ 1);
2206
2207   /* If we don't have any functions we'll want to unwind out of, don't
2208      emit any EH unwind information.  Note that if exceptions aren't
2209      enabled, we won't have collected nothrow information, and if we
2210      asked for asynchronous tables, we always want this info.  */
2211   if (for_eh)
2212     {
2213       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2214
2215       for (i = 0; i < fde_table_in_use; i++)
2216         if (fde_table[i].uses_eh_lsda)
2217           any_eh_needed = any_lsda_needed = true;
2218         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2219           any_eh_needed = true;
2220         else if (! fde_table[i].nothrow
2221                  && ! fde_table[i].all_throwers_are_sibcalls)
2222           any_eh_needed = true;
2223
2224       if (! any_eh_needed)
2225         return;
2226     }
2227
2228   /* We're going to be generating comments, so turn on app.  */
2229   if (flag_debug_asm)
2230     app_enable ();
2231
2232   if (for_eh)
2233     switch_to_eh_frame_section ();
2234   else
2235     {
2236       if (!debug_frame_section)
2237         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2238                                            SECTION_DEBUG, NULL);
2239       switch_to_section (debug_frame_section);
2240     }
2241
2242   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2243   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2244
2245   /* Output the CIE.  */
2246   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2247   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2248   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2249     dw2_asm_output_data (4, 0xffffffff,
2250       "Initial length escape value indicating 64-bit DWARF extension");
2251   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2252                         "Length of Common Information Entry");
2253   ASM_OUTPUT_LABEL (asm_out_file, l1);
2254
2255   /* Now that the CIE pointer is PC-relative for EH,
2256      use 0 to identify the CIE.  */
2257   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2258                        (for_eh ? 0 : DWARF_CIE_ID),
2259                        "CIE Identifier Tag");
2260
2261   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2262
2263   augmentation[0] = 0;
2264   augmentation_size = 0;
2265   if (for_eh)
2266     {
2267       char *p;
2268
2269       /* Augmentation:
2270          z      Indicates that a uleb128 is present to size the
2271                 augmentation section.
2272          L      Indicates the encoding (and thus presence) of
2273                 an LSDA pointer in the FDE augmentation.
2274          R      Indicates a non-default pointer encoding for
2275                 FDE code pointers.
2276          P      Indicates the presence of an encoding + language
2277                 personality routine in the CIE augmentation.  */
2278
2279       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2280       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2281       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2282
2283       p = augmentation + 1;
2284       if (eh_personality_libfunc)
2285         {
2286           *p++ = 'P';
2287           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2288         }
2289       if (any_lsda_needed)
2290         {
2291           *p++ = 'L';
2292           augmentation_size += 1;
2293         }
2294       if (fde_encoding != DW_EH_PE_absptr)
2295         {
2296           *p++ = 'R';
2297           augmentation_size += 1;
2298         }
2299       if (p > augmentation + 1)
2300         {
2301           augmentation[0] = 'z';
2302           *p = '\0';
2303         }
2304
2305       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2306       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2307         {
2308           int offset = (  4             /* Length */
2309                         + 4             /* CIE Id */
2310                         + 1             /* CIE version */
2311                         + strlen (augmentation) + 1     /* Augmentation */
2312                         + size_of_uleb128 (1)           /* Code alignment */
2313                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2314                         + 1             /* RA column */
2315                         + 1             /* Augmentation size */
2316                         + 1             /* Personality encoding */ );
2317           int pad = -offset & (PTR_SIZE - 1);
2318
2319           augmentation_size += pad;
2320
2321           /* Augmentations should be small, so there's scarce need to
2322              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2323           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2324         }
2325     }
2326
2327   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2328   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2329   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2330                                "CIE Data Alignment Factor");
2331
2332   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2333   if (DW_CIE_VERSION == 1)
2334     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2335   else
2336     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2337
2338   if (augmentation[0])
2339     {
2340       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2341       if (eh_personality_libfunc)
2342         {
2343           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2344                                eh_data_format_name (per_encoding));
2345           dw2_asm_output_encoded_addr_rtx (per_encoding,
2346                                            eh_personality_libfunc,
2347                                            true, NULL);
2348         }
2349
2350       if (any_lsda_needed)
2351         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2352                              eh_data_format_name (lsda_encoding));
2353
2354       if (fde_encoding != DW_EH_PE_absptr)
2355         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2356                              eh_data_format_name (fde_encoding));
2357     }
2358
2359   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2360     output_cfi (cfi, NULL, for_eh);
2361
2362   /* Pad the CIE out to an address sized boundary.  */
2363   ASM_OUTPUT_ALIGN (asm_out_file,
2364                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2365   ASM_OUTPUT_LABEL (asm_out_file, l2);
2366
2367   /* Loop through all of the FDE's.  */
2368   for (i = 0; i < fde_table_in_use; i++)
2369     {
2370       fde = &fde_table[i];
2371
2372       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2373       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2374           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2375           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2376           && !fde->uses_eh_lsda)
2377         continue;
2378
2379       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2380       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2381       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2382       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2383       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2384         dw2_asm_output_data (4, 0xffffffff,
2385                              "Initial length escape value indicating 64-bit DWARF extension");
2386       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2387                             "FDE Length");
2388       ASM_OUTPUT_LABEL (asm_out_file, l1);
2389
2390       if (for_eh)
2391         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2392       else
2393         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2394                                debug_frame_section, "FDE CIE offset");
2395
2396       if (for_eh)
2397         {
2398           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2399           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2400           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2401                                            sym_ref,
2402                                            false,
2403                                            "FDE initial location");
2404           if (fde->dw_fde_switched_sections)
2405             {
2406               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode, 
2407                                       fde->dw_fde_unlikely_section_label);
2408               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode, 
2409                                       fde->dw_fde_hot_section_label);
2410               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2411               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2412               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2413                                                "FDE initial location");
2414               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2415                                     fde->dw_fde_hot_section_end_label,
2416                                     fde->dw_fde_hot_section_label,
2417                                     "FDE address range");
2418               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2419                                                "FDE initial location");
2420               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2421                                     fde->dw_fde_unlikely_section_end_label,
2422                                     fde->dw_fde_unlikely_section_label,
2423                                     "FDE address range");
2424             }
2425           else
2426             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2427                                   fde->dw_fde_end, fde->dw_fde_begin,
2428                                   "FDE address range");
2429         }
2430       else
2431         {
2432           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2433                                "FDE initial location");
2434           if (fde->dw_fde_switched_sections)
2435             {
2436               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2437                                    fde->dw_fde_hot_section_label,
2438                                    "FDE initial location");
2439               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2440                                     fde->dw_fde_hot_section_end_label,
2441                                     fde->dw_fde_hot_section_label,
2442                                     "FDE address range");
2443               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2444                                    fde->dw_fde_unlikely_section_label,
2445                                    "FDE initial location");
2446               dw2_asm_output_delta (DWARF2_ADDR_SIZE, 
2447                                     fde->dw_fde_unlikely_section_end_label,
2448                                     fde->dw_fde_unlikely_section_label,
2449                                     "FDE address range");
2450             }
2451           else
2452             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2453                                   fde->dw_fde_end, fde->dw_fde_begin,
2454                                   "FDE address range");
2455         }
2456
2457       if (augmentation[0])
2458         {
2459           if (any_lsda_needed)
2460             {
2461               int size = size_of_encoded_value (lsda_encoding);
2462
2463               if (lsda_encoding == DW_EH_PE_aligned)
2464                 {
2465                   int offset = (  4             /* Length */
2466                                 + 4             /* CIE offset */
2467                                 + 2 * size_of_encoded_value (fde_encoding)
2468                                 + 1             /* Augmentation size */ );
2469                   int pad = -offset & (PTR_SIZE - 1);
2470
2471                   size += pad;
2472                   gcc_assert (size_of_uleb128 (size) == 1);
2473                 }
2474
2475               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2476
2477               if (fde->uses_eh_lsda)
2478                 {
2479                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2480                                                fde->funcdef_number);
2481                   dw2_asm_output_encoded_addr_rtx (
2482                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2483                         false, "Language Specific Data Area");
2484                 }
2485               else
2486                 {
2487                   if (lsda_encoding == DW_EH_PE_aligned)
2488                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2489                   dw2_asm_output_data
2490                     (size_of_encoded_value (lsda_encoding), 0,
2491                      "Language Specific Data Area (none)");
2492                 }
2493             }
2494           else
2495             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2496         }
2497
2498       /* Loop through the Call Frame Instructions associated with
2499          this FDE.  */
2500       fde->dw_fde_current_label = fde->dw_fde_begin;
2501       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2502         output_cfi (cfi, fde, for_eh);
2503
2504       /* Pad the FDE out to an address sized boundary.  */
2505       ASM_OUTPUT_ALIGN (asm_out_file,
2506                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2507       ASM_OUTPUT_LABEL (asm_out_file, l2);
2508     }
2509
2510   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2511     dw2_asm_output_data (4, 0, "End of Table");
2512 #ifdef MIPS_DEBUGGING_INFO
2513   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2514      get a value of 0.  Putting .align 0 after the label fixes it.  */
2515   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2516 #endif
2517
2518   /* Turn off app to make assembly quicker.  */
2519   if (flag_debug_asm)
2520     app_disable ();
2521 }
2522
2523 /* Output a marker (i.e. a label) for the beginning of a function, before
2524    the prologue.  */
2525
2526 void
2527 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2528                           const char *file ATTRIBUTE_UNUSED)
2529 {
2530   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2531   char * dup_label;
2532   dw_fde_ref fde;
2533
2534   current_function_func_begin_label = NULL;
2535
2536 #ifdef TARGET_UNWIND_INFO
2537   /* ??? current_function_func_begin_label is also used by except.c
2538      for call-site information.  We must emit this label if it might
2539      be used.  */
2540   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2541       && ! dwarf2out_do_frame ())
2542     return;
2543 #else
2544   if (! dwarf2out_do_frame ())
2545     return;
2546 #endif
2547
2548   switch_to_section (function_section (current_function_decl));
2549   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2550                                current_function_funcdef_no);
2551   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2552                           current_function_funcdef_no);
2553   dup_label = xstrdup (label);
2554   current_function_func_begin_label = dup_label;
2555
2556 #ifdef TARGET_UNWIND_INFO
2557   /* We can elide the fde allocation if we're not emitting debug info.  */
2558   if (! dwarf2out_do_frame ())
2559     return;
2560 #endif
2561
2562   /* Expand the fde table if necessary.  */
2563   if (fde_table_in_use == fde_table_allocated)
2564     {
2565       fde_table_allocated += FDE_TABLE_INCREMENT;
2566       fde_table = ggc_realloc (fde_table,
2567                                fde_table_allocated * sizeof (dw_fde_node));
2568       memset (fde_table + fde_table_in_use, 0,
2569               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2570     }
2571
2572   /* Record the FDE associated with this function.  */
2573   current_funcdef_fde = fde_table_in_use;
2574
2575   /* Add the new FDE at the end of the fde_table.  */
2576   fde = &fde_table[fde_table_in_use++];
2577   fde->decl = current_function_decl;
2578   fde->dw_fde_begin = dup_label;
2579   fde->dw_fde_current_label = dup_label;
2580   fde->dw_fde_hot_section_label = NULL;
2581   fde->dw_fde_hot_section_end_label = NULL;
2582   fde->dw_fde_unlikely_section_label = NULL;
2583   fde->dw_fde_unlikely_section_end_label = NULL;
2584   fde->dw_fde_switched_sections = false;
2585   fde->dw_fde_end = NULL;
2586   fde->dw_fde_cfi = NULL;
2587   fde->funcdef_number = current_function_funcdef_no;
2588   fde->nothrow = TREE_NOTHROW (current_function_decl);
2589   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2590   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2591
2592   args_size = old_args_size = 0;
2593
2594   /* We only want to output line number information for the genuine dwarf2
2595      prologue case, not the eh frame case.  */
2596 #ifdef DWARF2_DEBUGGING_INFO
2597   if (file)
2598     dwarf2out_source_line (line, file);
2599 #endif
2600 }
2601
2602 /* Output a marker (i.e. a label) for the absolute end of the generated code
2603    for a function definition.  This gets called *after* the epilogue code has
2604    been generated.  */
2605
2606 void
2607 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2608                         const char *file ATTRIBUTE_UNUSED)
2609 {
2610   dw_fde_ref fde;
2611   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2612
2613   /* Output a label to mark the endpoint of the code generated for this
2614      function.  */
2615   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2616                                current_function_funcdef_no);
2617   ASM_OUTPUT_LABEL (asm_out_file, label);
2618   fde = &fde_table[fde_table_in_use - 1];
2619   fde->dw_fde_end = xstrdup (label);
2620 }
2621
2622 void
2623 dwarf2out_frame_init (void)
2624 {
2625   /* Allocate the initial hunk of the fde_table.  */
2626   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2627   fde_table_allocated = FDE_TABLE_INCREMENT;
2628   fde_table_in_use = 0;
2629
2630   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2631      sake of lookup_cfa.  */
2632
2633   /* On entry, the Canonical Frame Address is at SP.  */
2634   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2635
2636 #ifdef DWARF2_UNWIND_INFO
2637   if (DWARF2_UNWIND_INFO)
2638     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2639 #endif
2640 }
2641
2642 void
2643 dwarf2out_frame_finish (void)
2644 {
2645   /* Output call frame information.  */
2646   if (DWARF2_FRAME_INFO)
2647     output_call_frame_info (0);
2648
2649 #ifndef TARGET_UNWIND_INFO
2650   /* Output another copy for the unwinder.  */
2651   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2652     output_call_frame_info (1);
2653 #endif
2654 }
2655 #endif
2656 \f
2657 /* And now, the subset of the debugging information support code necessary
2658    for emitting location expressions.  */
2659
2660 /* Data about a single source file.  */
2661 struct dwarf_file_data GTY(())
2662 {
2663   const char * filename;
2664   int emitted_number;
2665 };
2666
2667 /* We need some way to distinguish DW_OP_addr with a direct symbol
2668    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2669 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2670
2671
2672 typedef struct dw_val_struct *dw_val_ref;
2673 typedef struct die_struct *dw_die_ref;
2674 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2675 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2676
2677 /* Each DIE may have a series of attribute/value pairs.  Values
2678    can take on several forms.  The forms that are used in this
2679    implementation are listed below.  */
2680
2681 enum dw_val_class
2682 {
2683   dw_val_class_addr,
2684   dw_val_class_offset,
2685   dw_val_class_loc,
2686   dw_val_class_loc_list,
2687   dw_val_class_range_list,
2688   dw_val_class_const,
2689   dw_val_class_unsigned_const,
2690   dw_val_class_long_long,
2691   dw_val_class_vec,
2692   dw_val_class_flag,
2693   dw_val_class_die_ref,
2694   dw_val_class_fde_ref,
2695   dw_val_class_lbl_id,
2696   dw_val_class_lineptr,
2697   dw_val_class_str,
2698   dw_val_class_macptr,
2699   dw_val_class_file
2700 };
2701
2702 /* Describe a double word constant value.  */
2703 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2704
2705 typedef struct dw_long_long_struct GTY(())
2706 {
2707   unsigned long hi;
2708   unsigned long low;
2709 }
2710 dw_long_long_const;
2711
2712 /* Describe a floating point constant value, or a vector constant value.  */
2713
2714 typedef struct dw_vec_struct GTY(())
2715 {
2716   unsigned char * GTY((length ("%h.length"))) array;
2717   unsigned length;
2718   unsigned elt_size;
2719 }
2720 dw_vec_const;
2721
2722 /* The dw_val_node describes an attribute's value, as it is
2723    represented internally.  */
2724
2725 typedef struct dw_val_struct GTY(())
2726 {
2727   enum dw_val_class val_class;
2728   union dw_val_struct_union
2729     {
2730       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2731       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2732       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2733       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2734       HOST_WIDE_INT GTY ((default)) val_int;
2735       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2736       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2737       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2738       struct dw_val_die_union
2739         {
2740           dw_die_ref die;
2741           int external;
2742         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2743       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2744       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2745       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2746       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2747       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2748     }
2749   GTY ((desc ("%1.val_class"))) v;
2750 }
2751 dw_val_node;
2752
2753 /* Locations in memory are described using a sequence of stack machine
2754    operations.  */
2755
2756 typedef struct dw_loc_descr_struct GTY(())
2757 {
2758   dw_loc_descr_ref dw_loc_next;
2759   enum dwarf_location_atom dw_loc_opc;
2760   dw_val_node dw_loc_oprnd1;
2761   dw_val_node dw_loc_oprnd2;
2762   int dw_loc_addr;
2763 }
2764 dw_loc_descr_node;
2765
2766 /* Location lists are ranges + location descriptions for that range,
2767    so you can track variables that are in different places over
2768    their entire life.  */
2769 typedef struct dw_loc_list_struct GTY(())
2770 {
2771   dw_loc_list_ref dw_loc_next;
2772   const char *begin; /* Label for begin address of range */
2773   const char *end;  /* Label for end address of range */
2774   char *ll_symbol; /* Label for beginning of location list.
2775                       Only on head of list */
2776   const char *section; /* Section this loclist is relative to */
2777   dw_loc_descr_ref expr;
2778 } dw_loc_list_node;
2779
2780 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2781
2782 static const char *dwarf_stack_op_name (unsigned);
2783 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2784                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2785 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2786 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2787 static unsigned long size_of_locs (dw_loc_descr_ref);
2788 static void output_loc_operands (dw_loc_descr_ref);
2789 static void output_loc_sequence (dw_loc_descr_ref);
2790
2791 /* Convert a DWARF stack opcode into its string name.  */
2792
2793 static const char *
2794 dwarf_stack_op_name (unsigned int op)
2795 {
2796   switch (op)
2797     {
2798     case DW_OP_addr:
2799     case INTERNAL_DW_OP_tls_addr:
2800       return "DW_OP_addr";
2801     case DW_OP_deref:
2802       return "DW_OP_deref";
2803     case DW_OP_const1u:
2804       return "DW_OP_const1u";
2805     case DW_OP_const1s:
2806       return "DW_OP_const1s";
2807     case DW_OP_const2u:
2808       return "DW_OP_const2u";
2809     case DW_OP_const2s:
2810       return "DW_OP_const2s";
2811     case DW_OP_const4u:
2812       return "DW_OP_const4u";
2813     case DW_OP_const4s:
2814       return "DW_OP_const4s";
2815     case DW_OP_const8u:
2816       return "DW_OP_const8u";
2817     case DW_OP_const8s:
2818       return "DW_OP_const8s";
2819     case DW_OP_constu:
2820       return "DW_OP_constu";
2821     case DW_OP_consts:
2822       return "DW_OP_consts";
2823     case DW_OP_dup:
2824       return "DW_OP_dup";
2825     case DW_OP_drop:
2826       return "DW_OP_drop";
2827     case DW_OP_over:
2828       return "DW_OP_over";
2829     case DW_OP_pick:
2830       return "DW_OP_pick";
2831     case DW_OP_swap:
2832       return "DW_OP_swap";
2833     case DW_OP_rot:
2834       return "DW_OP_rot";
2835     case DW_OP_xderef:
2836       return "DW_OP_xderef";
2837     case DW_OP_abs:
2838       return "DW_OP_abs";
2839     case DW_OP_and:
2840       return "DW_OP_and";
2841     case DW_OP_div:
2842       return "DW_OP_div";
2843     case DW_OP_minus:
2844       return "DW_OP_minus";
2845     case DW_OP_mod:
2846       return "DW_OP_mod";
2847     case DW_OP_mul:
2848       return "DW_OP_mul";
2849     case DW_OP_neg:
2850       return "DW_OP_neg";
2851     case DW_OP_not:
2852       return "DW_OP_not";
2853     case DW_OP_or:
2854       return "DW_OP_or";
2855     case DW_OP_plus:
2856       return "DW_OP_plus";
2857     case DW_OP_plus_uconst:
2858       return "DW_OP_plus_uconst";
2859     case DW_OP_shl:
2860       return "DW_OP_shl";
2861     case DW_OP_shr:
2862       return "DW_OP_shr";
2863     case DW_OP_shra:
2864       return "DW_OP_shra";
2865     case DW_OP_xor:
2866       return "DW_OP_xor";
2867     case DW_OP_bra:
2868       return "DW_OP_bra";
2869     case DW_OP_eq:
2870       return "DW_OP_eq";
2871     case DW_OP_ge:
2872       return "DW_OP_ge";
2873     case DW_OP_gt:
2874       return "DW_OP_gt";
2875     case DW_OP_le:
2876       return "DW_OP_le";
2877     case DW_OP_lt:
2878       return "DW_OP_lt";
2879     case DW_OP_ne:
2880       return "DW_OP_ne";
2881     case DW_OP_skip:
2882       return "DW_OP_skip";
2883     case DW_OP_lit0:
2884       return "DW_OP_lit0";
2885     case DW_OP_lit1:
2886       return "DW_OP_lit1";
2887     case DW_OP_lit2:
2888       return "DW_OP_lit2";
2889     case DW_OP_lit3:
2890       return "DW_OP_lit3";
2891     case DW_OP_lit4:
2892       return "DW_OP_lit4";
2893     case DW_OP_lit5:
2894       return "DW_OP_lit5";
2895     case DW_OP_lit6:
2896       return "DW_OP_lit6";
2897     case DW_OP_lit7:
2898       return "DW_OP_lit7";
2899     case DW_OP_lit8:
2900       return "DW_OP_lit8";
2901     case DW_OP_lit9:
2902       return "DW_OP_lit9";
2903     case DW_OP_lit10:
2904       return "DW_OP_lit10";
2905     case DW_OP_lit11:
2906       return "DW_OP_lit11";
2907     case DW_OP_lit12:
2908       return "DW_OP_lit12";
2909     case DW_OP_lit13:
2910       return "DW_OP_lit13";
2911     case DW_OP_lit14:
2912       return "DW_OP_lit14";
2913     case DW_OP_lit15:
2914       return "DW_OP_lit15";
2915     case DW_OP_lit16:
2916       return "DW_OP_lit16";
2917     case DW_OP_lit17:
2918       return "DW_OP_lit17";
2919     case DW_OP_lit18:
2920       return "DW_OP_lit18";
2921     case DW_OP_lit19:
2922       return "DW_OP_lit19";
2923     case DW_OP_lit20:
2924       return "DW_OP_lit20";
2925     case DW_OP_lit21:
2926       return "DW_OP_lit21";
2927     case DW_OP_lit22:
2928       return "DW_OP_lit22";
2929     case DW_OP_lit23:
2930       return "DW_OP_lit23";
2931     case DW_OP_lit24:
2932       return "DW_OP_lit24";
2933     case DW_OP_lit25:
2934       return "DW_OP_lit25";
2935     case DW_OP_lit26:
2936       return "DW_OP_lit26";
2937     case DW_OP_lit27:
2938       return "DW_OP_lit27";
2939     case DW_OP_lit28:
2940       return "DW_OP_lit28";
2941     case DW_OP_lit29:
2942       return "DW_OP_lit29";
2943     case DW_OP_lit30:
2944       return "DW_OP_lit30";
2945     case DW_OP_lit31:
2946       return "DW_OP_lit31";
2947     case DW_OP_reg0:
2948       return "DW_OP_reg0";
2949     case DW_OP_reg1:
2950       return "DW_OP_reg1";
2951     case DW_OP_reg2:
2952       return "DW_OP_reg2";
2953     case DW_OP_reg3:
2954       return "DW_OP_reg3";
2955     case DW_OP_reg4:
2956       return "DW_OP_reg4";
2957     case DW_OP_reg5:
2958       return "DW_OP_reg5";
2959     case DW_OP_reg6:
2960       return "DW_OP_reg6";
2961     case DW_OP_reg7:
2962       return "DW_OP_reg7";
2963     case DW_OP_reg8:
2964       return "DW_OP_reg8";
2965     case DW_OP_reg9:
2966       return "DW_OP_reg9";
2967     case DW_OP_reg10:
2968       return "DW_OP_reg10";
2969     case DW_OP_reg11:
2970       return "DW_OP_reg11";
2971     case DW_OP_reg12:
2972       return "DW_OP_reg12";
2973     case DW_OP_reg13:
2974       return "DW_OP_reg13";
2975     case DW_OP_reg14:
2976       return "DW_OP_reg14";
2977     case DW_OP_reg15:
2978       return "DW_OP_reg15";
2979     case DW_OP_reg16:
2980       return "DW_OP_reg16";
2981     case DW_OP_reg17:
2982       return "DW_OP_reg17";
2983     case DW_OP_reg18:
2984       return "DW_OP_reg18";
2985     case DW_OP_reg19:
2986       return "DW_OP_reg19";
2987     case DW_OP_reg20:
2988       return "DW_OP_reg20";
2989     case DW_OP_reg21:
2990       return "DW_OP_reg21";
2991     case DW_OP_reg22:
2992       return "DW_OP_reg22";
2993     case DW_OP_reg23:
2994       return "DW_OP_reg23";
2995     case DW_OP_reg24:
2996       return "DW_OP_reg24";
2997     case DW_OP_reg25:
2998       return "DW_OP_reg25";
2999     case DW_OP_reg26:
3000       return "DW_OP_reg26";
3001     case DW_OP_reg27:
3002       return "DW_OP_reg27";
3003     case DW_OP_reg28:
3004       return "DW_OP_reg28";
3005     case DW_OP_reg29:
3006       return "DW_OP_reg29";
3007     case DW_OP_reg30:
3008       return "DW_OP_reg30";
3009     case DW_OP_reg31:
3010       return "DW_OP_reg31";
3011     case DW_OP_breg0:
3012       return "DW_OP_breg0";
3013     case DW_OP_breg1:
3014       return "DW_OP_breg1";
3015     case DW_OP_breg2:
3016       return "DW_OP_breg2";
3017     case DW_OP_breg3:
3018       return "DW_OP_breg3";
3019     case DW_OP_breg4:
3020       return "DW_OP_breg4";
3021     case DW_OP_breg5:
3022       return "DW_OP_breg5";
3023     case DW_OP_breg6:
3024       return "DW_OP_breg6";
3025     case DW_OP_breg7:
3026       return "DW_OP_breg7";
3027     case DW_OP_breg8:
3028       return "DW_OP_breg8";
3029     case DW_OP_breg9:
3030       return "DW_OP_breg9";
3031     case DW_OP_breg10:
3032       return "DW_OP_breg10";
3033     case DW_OP_breg11:
3034       return "DW_OP_breg11";
3035     case DW_OP_breg12:
3036       return "DW_OP_breg12";
3037     case DW_OP_breg13:
3038       return "DW_OP_breg13";
3039     case DW_OP_breg14:
3040       return "DW_OP_breg14";
3041     case DW_OP_breg15:
3042       return "DW_OP_breg15";
3043     case DW_OP_breg16:
3044       return "DW_OP_breg16";
3045     case DW_OP_breg17:
3046       return "DW_OP_breg17";
3047     case DW_OP_breg18:
3048       return "DW_OP_breg18";
3049     case DW_OP_breg19:
3050       return "DW_OP_breg19";
3051     case DW_OP_breg20:
3052       return "DW_OP_breg20";
3053     case DW_OP_breg21:
3054       return "DW_OP_breg21";
3055     case DW_OP_breg22:
3056       return "DW_OP_breg22";
3057     case DW_OP_breg23:
3058       return "DW_OP_breg23";
3059     case DW_OP_breg24:
3060       return "DW_OP_breg24";
3061     case DW_OP_breg25:
3062       return "DW_OP_breg25";
3063     case DW_OP_breg26:
3064       return "DW_OP_breg26";
3065     case DW_OP_breg27:
3066       return "DW_OP_breg27";
3067     case DW_OP_breg28:
3068       return "DW_OP_breg28";
3069     case DW_OP_breg29:
3070       return "DW_OP_breg29";
3071     case DW_OP_breg30:
3072       return "DW_OP_breg30";
3073     case DW_OP_breg31:
3074       return "DW_OP_breg31";
3075     case DW_OP_regx:
3076       return "DW_OP_regx";
3077     case DW_OP_fbreg:
3078       return "DW_OP_fbreg";
3079     case DW_OP_bregx:
3080       return "DW_OP_bregx";
3081     case DW_OP_piece:
3082       return "DW_OP_piece";
3083     case DW_OP_deref_size:
3084       return "DW_OP_deref_size";
3085     case DW_OP_xderef_size:
3086       return "DW_OP_xderef_size";
3087     case DW_OP_nop:
3088       return "DW_OP_nop";
3089     case DW_OP_push_object_address:
3090       return "DW_OP_push_object_address";
3091     case DW_OP_call2:
3092       return "DW_OP_call2";
3093     case DW_OP_call4:
3094       return "DW_OP_call4";
3095     case DW_OP_call_ref:
3096       return "DW_OP_call_ref";
3097     case DW_OP_GNU_push_tls_address:
3098       return "DW_OP_GNU_push_tls_address";
3099     default:
3100       return "OP_<unknown>";
3101     }
3102 }
3103
3104 /* Return a pointer to a newly allocated location description.  Location
3105    descriptions are simple expression terms that can be strung
3106    together to form more complicated location (address) descriptions.  */
3107
3108 static inline dw_loc_descr_ref
3109 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3110                unsigned HOST_WIDE_INT oprnd2)
3111 {
3112   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3113
3114   descr->dw_loc_opc = op;
3115   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3116   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3117   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3118   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3119
3120   return descr;
3121 }
3122
3123 /* Add a location description term to a location description expression.  */
3124
3125 static inline void
3126 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3127 {
3128   dw_loc_descr_ref *d;
3129
3130   /* Find the end of the chain.  */
3131   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3132     ;
3133
3134   *d = descr;
3135 }
3136
3137 /* Return the size of a location descriptor.  */
3138
3139 static unsigned long
3140 size_of_loc_descr (dw_loc_descr_ref loc)
3141 {
3142   unsigned long size = 1;
3143
3144   switch (loc->dw_loc_opc)
3145     {
3146     case DW_OP_addr:
3147     case INTERNAL_DW_OP_tls_addr:
3148       size += DWARF2_ADDR_SIZE;
3149       break;
3150     case DW_OP_const1u:
3151     case DW_OP_const1s:
3152       size += 1;
3153       break;
3154     case DW_OP_const2u:
3155     case DW_OP_const2s:
3156       size += 2;
3157       break;
3158     case DW_OP_const4u:
3159     case DW_OP_const4s:
3160       size += 4;
3161       break;
3162     case DW_OP_const8u:
3163     case DW_OP_const8s:
3164       size += 8;
3165       break;
3166     case DW_OP_constu:
3167       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3168       break;
3169     case DW_OP_consts:
3170       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3171       break;
3172     case DW_OP_pick:
3173       size += 1;
3174       break;
3175     case DW_OP_plus_uconst:
3176       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3177       break;
3178     case DW_OP_skip:
3179     case DW_OP_bra:
3180       size += 2;
3181       break;
3182     case DW_OP_breg0:
3183     case DW_OP_breg1:
3184     case DW_OP_breg2:
3185     case DW_OP_breg3:
3186     case DW_OP_breg4:
3187     case DW_OP_breg5:
3188     case DW_OP_breg6:
3189     case DW_OP_breg7:
3190     case DW_OP_breg8:
3191     case DW_OP_breg9:
3192     case DW_OP_breg10:
3193     case DW_OP_breg11:
3194     case DW_OP_breg12:
3195     case DW_OP_breg13:
3196     case DW_OP_breg14:
3197     case DW_OP_breg15:
3198     case DW_OP_breg16:
3199     case DW_OP_breg17:
3200     case DW_OP_breg18:
3201     case DW_OP_breg19:
3202     case DW_OP_breg20:
3203     case DW_OP_breg21:
3204     case DW_OP_breg22:
3205     case DW_OP_breg23:
3206     case DW_OP_breg24:
3207     case DW_OP_breg25:
3208     case DW_OP_breg26:
3209     case DW_OP_breg27:
3210     case DW_OP_breg28:
3211     case DW_OP_breg29:
3212     case DW_OP_breg30:
3213     case DW_OP_breg31:
3214       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3215       break;
3216     case DW_OP_regx:
3217       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3218       break;
3219     case DW_OP_fbreg:
3220       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3221       break;
3222     case DW_OP_bregx:
3223       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3224       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3225       break;
3226     case DW_OP_piece:
3227       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3228       break;
3229     case DW_OP_deref_size:
3230     case DW_OP_xderef_size:
3231       size += 1;
3232       break;
3233     case DW_OP_call2:
3234       size += 2;
3235       break;
3236     case DW_OP_call4:
3237       size += 4;
3238       break;
3239     case DW_OP_call_ref:
3240       size += DWARF2_ADDR_SIZE;
3241       break;
3242     default:
3243       break;
3244     }
3245
3246   return size;
3247 }
3248
3249 /* Return the size of a series of location descriptors.  */
3250
3251 static unsigned long
3252 size_of_locs (dw_loc_descr_ref loc)
3253 {
3254   dw_loc_descr_ref l;
3255   unsigned long size;
3256
3257   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3258      field, to avoid writing to a PCH file.  */
3259   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3260     {
3261       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3262         break;
3263       size += size_of_loc_descr (l);
3264     }
3265   if (! l)
3266     return size;
3267
3268   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3269     {
3270       l->dw_loc_addr = size;
3271       size += size_of_loc_descr (l);
3272     }
3273
3274   return size;
3275 }
3276
3277 /* Output location description stack opcode's operands (if any).  */
3278
3279 static void
3280 output_loc_operands (dw_loc_descr_ref loc)
3281 {
3282   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3283   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3284
3285   switch (loc->dw_loc_opc)
3286     {
3287 #ifdef DWARF2_DEBUGGING_INFO
3288     case DW_OP_addr:
3289       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3290       break;
3291     case DW_OP_const2u:
3292     case DW_OP_const2s:
3293       dw2_asm_output_data (2, val1->v.val_int, NULL);
3294       break;
3295     case DW_OP_const4u:
3296     case DW_OP_const4s:
3297       dw2_asm_output_data (4, val1->v.val_int, NULL);
3298       break;
3299     case DW_OP_const8u:
3300     case DW_OP_const8s:
3301       gcc_assert (HOST_BITS_PER_LONG >= 64);
3302       dw2_asm_output_data (8, val1->v.val_int, NULL);
3303       break;
3304     case DW_OP_skip:
3305     case DW_OP_bra:
3306       {
3307         int offset;
3308
3309         gcc_assert (val1->val_class == dw_val_class_loc);
3310         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3311
3312         dw2_asm_output_data (2, offset, NULL);
3313       }
3314       break;
3315 #else
3316     case DW_OP_addr:
3317     case DW_OP_const2u:
3318     case DW_OP_const2s:
3319     case DW_OP_const4u:
3320     case DW_OP_const4s:
3321     case DW_OP_const8u:
3322     case DW_OP_const8s:
3323     case DW_OP_skip:
3324     case DW_OP_bra:
3325       /* We currently don't make any attempt to make sure these are
3326          aligned properly like we do for the main unwind info, so
3327          don't support emitting things larger than a byte if we're
3328          only doing unwinding.  */
3329       gcc_unreachable ();
3330 #endif
3331     case DW_OP_const1u:
3332     case DW_OP_const1s:
3333       dw2_asm_output_data (1, val1->v.val_int, NULL);
3334       break;
3335     case DW_OP_constu:
3336       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3337       break;
3338     case DW_OP_consts:
3339       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3340       break;
3341     case DW_OP_pick:
3342       dw2_asm_output_data (1, val1->v.val_int, NULL);
3343       break;
3344     case DW_OP_plus_uconst:
3345       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3346       break;
3347     case DW_OP_breg0:
3348     case DW_OP_breg1:
3349     case DW_OP_breg2:
3350     case DW_OP_breg3:
3351     case DW_OP_breg4:
3352     case DW_OP_breg5:
3353     case DW_OP_breg6:
3354     case DW_OP_breg7:
3355     case DW_OP_breg8:
3356     case DW_OP_breg9:
3357     case DW_OP_breg10:
3358     case DW_OP_breg11:
3359     case DW_OP_breg12:
3360     case DW_OP_breg13:
3361     case DW_OP_breg14:
3362     case DW_OP_breg15:
3363     case DW_OP_breg16:
3364     case DW_OP_breg17:
3365     case DW_OP_breg18:
3366     case DW_OP_breg19:
3367     case DW_OP_breg20:
3368     case DW_OP_breg21:
3369     case DW_OP_breg22:
3370     case DW_OP_breg23:
3371     case DW_OP_breg24:
3372     case DW_OP_breg25:
3373     case DW_OP_breg26:
3374     case DW_OP_breg27:
3375     case DW_OP_breg28:
3376     case DW_OP_breg29:
3377     case DW_OP_breg30:
3378     case DW_OP_breg31:
3379       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3380       break;
3381     case DW_OP_regx:
3382       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3383       break;
3384     case DW_OP_fbreg:
3385       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3386       break;
3387     case DW_OP_bregx:
3388       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3389       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3390       break;
3391     case DW_OP_piece:
3392       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3393       break;
3394     case DW_OP_deref_size:
3395     case DW_OP_xderef_size:
3396       dw2_asm_output_data (1, val1->v.val_int, NULL);
3397       break;
3398
3399     case INTERNAL_DW_OP_tls_addr:
3400       if (targetm.asm_out.output_dwarf_dtprel)
3401         {
3402           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3403                                                DWARF2_ADDR_SIZE,
3404                                                val1->v.val_addr);
3405           fputc ('\n', asm_out_file);
3406         }
3407       else
3408         gcc_unreachable ();
3409       break;
3410
3411     default:
3412       /* Other codes have no operands.  */
3413       break;
3414     }
3415 }
3416
3417 /* Output a sequence of location operations.  */
3418
3419 static void
3420 output_loc_sequence (dw_loc_descr_ref loc)
3421 {
3422   for (; loc != NULL; loc = loc->dw_loc_next)
3423     {
3424       /* Output the opcode.  */
3425       dw2_asm_output_data (1, loc->dw_loc_opc,
3426                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3427
3428       /* Output the operand(s) (if any).  */
3429       output_loc_operands (loc);
3430     }
3431 }
3432
3433 /* This routine will generate the correct assembly data for a location
3434    description based on a cfi entry with a complex address.  */
3435
3436 static void
3437 output_cfa_loc (dw_cfi_ref cfi)
3438 {
3439   dw_loc_descr_ref loc;
3440   unsigned long size;
3441
3442   /* Output the size of the block.  */
3443   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3444   size = size_of_locs (loc);
3445   dw2_asm_output_data_uleb128 (size, NULL);
3446
3447   /* Now output the operations themselves.  */
3448   output_loc_sequence (loc);
3449 }
3450
3451 /* This function builds a dwarf location descriptor sequence from a
3452    dw_cfa_location, adding the given OFFSET to the result of the
3453    expression.  */
3454
3455 static struct dw_loc_descr_struct *
3456 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3457 {
3458   struct dw_loc_descr_struct *head, *tmp;
3459
3460   offset += cfa->offset;
3461
3462   if (cfa->indirect)
3463     {
3464       if (cfa->base_offset)
3465         {
3466           if (cfa->reg <= 31)
3467             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3468           else
3469             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3470         }
3471       else if (cfa->reg <= 31)
3472         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3473       else
3474         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3475
3476       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3477       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3478       add_loc_descr (&head, tmp);
3479       if (offset != 0)
3480         {
3481           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3482           add_loc_descr (&head, tmp);
3483         }
3484     }
3485   else
3486     {
3487       if (offset == 0)
3488         if (cfa->reg <= 31)
3489           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3490         else
3491           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3492       else if (cfa->reg <= 31)
3493         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3494       else
3495         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3496     }
3497
3498   return head;
3499 }
3500
3501 /* This function fills in aa dw_cfa_location structure from a dwarf location
3502    descriptor sequence.  */
3503
3504 static void
3505 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3506 {
3507   struct dw_loc_descr_struct *ptr;
3508   cfa->offset = 0;
3509   cfa->base_offset = 0;
3510   cfa->indirect = 0;
3511   cfa->reg = -1;
3512
3513   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3514     {
3515       enum dwarf_location_atom op = ptr->dw_loc_opc;
3516
3517       switch (op)
3518         {
3519         case DW_OP_reg0:
3520         case DW_OP_reg1:
3521         case DW_OP_reg2:
3522         case DW_OP_reg3:
3523         case DW_OP_reg4:
3524         case DW_OP_reg5:
3525         case DW_OP_reg6:
3526         case DW_OP_reg7:
3527         case DW_OP_reg8:
3528         case DW_OP_reg9:
3529         case DW_OP_reg10:
3530         case DW_OP_reg11:
3531         case DW_OP_reg12:
3532         case DW_OP_reg13:
3533         case DW_OP_reg14:
3534         case DW_OP_reg15:
3535         case DW_OP_reg16:
3536         case DW_OP_reg17:
3537         case DW_OP_reg18:
3538         case DW_OP_reg19:
3539         case DW_OP_reg20:
3540         case DW_OP_reg21:
3541         case DW_OP_reg22:
3542         case DW_OP_reg23:
3543         case DW_OP_reg24:
3544         case DW_OP_reg25:
3545         case DW_OP_reg26:
3546         case DW_OP_reg27:
3547         case DW_OP_reg28:
3548         case DW_OP_reg29:
3549         case DW_OP_reg30:
3550         case DW_OP_reg31:
3551           cfa->reg = op - DW_OP_reg0;
3552           break;
3553         case DW_OP_regx:
3554           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3555           break;
3556         case DW_OP_breg0:
3557         case DW_OP_breg1:
3558         case DW_OP_breg2:
3559         case DW_OP_breg3:
3560         case DW_OP_breg4:
3561         case DW_OP_breg5:
3562         case DW_OP_breg6:
3563         case DW_OP_breg7:
3564         case DW_OP_breg8:
3565         case DW_OP_breg9:
3566         case DW_OP_breg10:
3567         case DW_OP_breg11:
3568         case DW_OP_breg12:
3569         case DW_OP_breg13:
3570         case DW_OP_breg14:
3571         case DW_OP_breg15:
3572         case DW_OP_breg16:
3573         case DW_OP_breg17:
3574         case DW_OP_breg18:
3575         case DW_OP_breg19:
3576         case DW_OP_breg20:
3577         case DW_OP_breg21:
3578         case DW_OP_breg22:
3579         case DW_OP_breg23:
3580         case DW_OP_breg24:
3581         case DW_OP_breg25:
3582         case DW_OP_breg26:
3583         case DW_OP_breg27:
3584         case DW_OP_breg28:
3585         case DW_OP_breg29:
3586         case DW_OP_breg30:
3587         case DW_OP_breg31:
3588           cfa->reg = op - DW_OP_breg0;
3589           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3590           break;
3591         case DW_OP_bregx:
3592           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3593           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3594           break;
3595         case DW_OP_deref:
3596           cfa->indirect = 1;
3597           break;
3598         case DW_OP_plus_uconst:
3599           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3600           break;
3601         default:
3602           internal_error ("DW_LOC_OP %s not implemented",
3603                           dwarf_stack_op_name (ptr->dw_loc_opc));
3604         }
3605     }
3606 }
3607 #endif /* .debug_frame support */
3608 \f
3609 /* And now, the support for symbolic debugging information.  */
3610 #ifdef DWARF2_DEBUGGING_INFO
3611
3612 /* .debug_str support.  */
3613 static int output_indirect_string (void **, void *);
3614
3615 static void dwarf2out_init (const char *);
3616 static void dwarf2out_finish (const char *);
3617 static void dwarf2out_define (unsigned int, const char *);
3618 static void dwarf2out_undef (unsigned int, const char *);
3619 static void dwarf2out_start_source_file (unsigned, const char *);
3620 static void dwarf2out_end_source_file (unsigned);
3621 static void dwarf2out_begin_block (unsigned, unsigned);
3622 static void dwarf2out_end_block (unsigned, unsigned);
3623 static bool dwarf2out_ignore_block (tree);
3624 static void dwarf2out_global_decl (tree);
3625 static void dwarf2out_type_decl (tree, int);
3626 static void dwarf2out_imported_module_or_decl (tree, tree);
3627 static void dwarf2out_abstract_function (tree);
3628 static void dwarf2out_var_location (rtx);
3629 static void dwarf2out_begin_function (tree);
3630 static void dwarf2out_switch_text_section (void);
3631
3632 /* The debug hooks structure.  */
3633
3634 const struct gcc_debug_hooks dwarf2_debug_hooks =
3635 {
3636   dwarf2out_init,
3637   dwarf2out_finish,
3638   dwarf2out_define,
3639   dwarf2out_undef,
3640   dwarf2out_start_source_file,
3641   dwarf2out_end_source_file,
3642   dwarf2out_begin_block,
3643   dwarf2out_end_block,
3644   dwarf2out_ignore_block,
3645   dwarf2out_source_line,
3646   dwarf2out_begin_prologue,
3647   debug_nothing_int_charstar,   /* end_prologue */
3648   dwarf2out_end_epilogue,
3649   dwarf2out_begin_function,
3650   debug_nothing_int,            /* end_function */
3651   dwarf2out_decl,               /* function_decl */
3652   dwarf2out_global_decl,
3653   dwarf2out_type_decl,          /* type_decl */
3654   dwarf2out_imported_module_or_decl,
3655   debug_nothing_tree,           /* deferred_inline_function */
3656   /* The DWARF 2 backend tries to reduce debugging bloat by not
3657      emitting the abstract description of inline functions until
3658      something tries to reference them.  */
3659   dwarf2out_abstract_function,  /* outlining_inline_function */
3660   debug_nothing_rtx,            /* label */
3661   debug_nothing_int,            /* handle_pch */
3662   dwarf2out_var_location,
3663   dwarf2out_switch_text_section,
3664   1                             /* start_end_main_source_file */
3665 };
3666 #endif
3667 \f
3668 /* NOTE: In the comments in this file, many references are made to
3669    "Debugging Information Entries".  This term is abbreviated as `DIE'
3670    throughout the remainder of this file.  */
3671
3672 /* An internal representation of the DWARF output is built, and then
3673    walked to generate the DWARF debugging info.  The walk of the internal
3674    representation is done after the entire program has been compiled.
3675    The types below are used to describe the internal representation.  */
3676
3677 /* Various DIE's use offsets relative to the beginning of the
3678    .debug_info section to refer to each other.  */
3679
3680 typedef long int dw_offset;
3681
3682 /* Define typedefs here to avoid circular dependencies.  */
3683
3684 typedef struct dw_attr_struct *dw_attr_ref;
3685 typedef struct dw_line_info_struct *dw_line_info_ref;
3686 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3687 typedef struct pubname_struct *pubname_ref;
3688 typedef struct dw_ranges_struct *dw_ranges_ref;
3689
3690 /* Each entry in the line_info_table maintains the file and
3691    line number associated with the label generated for that
3692    entry.  The label gives the PC value associated with
3693    the line number entry.  */
3694
3695 typedef struct dw_line_info_struct GTY(())
3696 {
3697   unsigned long dw_file_num;
3698   unsigned long dw_line_num;
3699 }
3700 dw_line_info_entry;
3701
3702 /* Line information for functions in separate sections; each one gets its
3703    own sequence.  */
3704 typedef struct dw_separate_line_info_struct GTY(())
3705 {
3706   unsigned long dw_file_num;
3707   unsigned long dw_line_num;
3708   unsigned long function;
3709 }
3710 dw_separate_line_info_entry;
3711
3712 /* Each DIE attribute has a field specifying the attribute kind,
3713    a link to the next attribute in the chain, and an attribute value.
3714    Attributes are typically linked below the DIE they modify.  */
3715
3716 typedef struct dw_attr_struct GTY(())
3717 {
3718   enum dwarf_attribute dw_attr;
3719   dw_val_node dw_attr_val;
3720 }
3721 dw_attr_node;
3722
3723 DEF_VEC_O(dw_attr_node);
3724 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3725
3726 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3727    The children of each node form a circular list linked by
3728    die_sib.  die_child points to the node *before* the "first" child node.  */
3729
3730 typedef struct die_struct GTY(())
3731 {
3732   enum dwarf_tag die_tag;
3733   char *die_symbol;
3734   VEC(dw_attr_node,gc) * die_attr;
3735   dw_die_ref die_parent;
3736   dw_die_ref die_child;
3737   dw_die_ref die_sib;
3738   dw_die_ref die_definition; /* ref from a specification to its definition */
3739   dw_offset die_offset;
3740   unsigned long die_abbrev;
3741   int die_mark;
3742   /* Die is used and must not be pruned as unused.  */
3743   int die_perennial_p;
3744   unsigned int decl_id;
3745 }
3746 die_node;
3747
3748 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3749 #define FOR_EACH_CHILD(die, c, expr) do {       \
3750   c = die->die_child;                           \
3751   if (c) do {                                   \
3752     c = c->die_sib;                             \
3753     expr;                                       \
3754   } while (c != die->die_child);                \
3755 } while (0)
3756
3757 /* The pubname structure */
3758
3759 typedef struct pubname_struct GTY(())
3760 {
3761   dw_die_ref die;
3762   char *name;
3763 }
3764 pubname_entry;
3765
3766 DEF_VEC_O(pubname_entry);
3767 DEF_VEC_ALLOC_O(pubname_entry, gc);
3768
3769 struct dw_ranges_struct GTY(())
3770 {
3771   int block_num;
3772 };
3773
3774 /* The limbo die list structure.  */
3775 typedef struct limbo_die_struct GTY(())
3776 {
3777   dw_die_ref die;
3778   tree created_for;
3779   struct limbo_die_struct *next;
3780 }
3781 limbo_die_node;
3782
3783 /* How to start an assembler comment.  */
3784 #ifndef ASM_COMMENT_START
3785 #define ASM_COMMENT_START ";#"
3786 #endif
3787
3788 /* Define a macro which returns nonzero for a TYPE_DECL which was
3789    implicitly generated for a tagged type.
3790
3791    Note that unlike the gcc front end (which generates a NULL named
3792    TYPE_DECL node for each complete tagged type, each array type, and
3793    each function type node created) the g++ front end generates a
3794    _named_ TYPE_DECL node for each tagged type node created.
3795    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3796    generate a DW_TAG_typedef DIE for them.  */
3797
3798 #define TYPE_DECL_IS_STUB(decl)                         \
3799   (DECL_NAME (decl) == NULL_TREE                        \
3800    || (DECL_ARTIFICIAL (decl)                           \
3801        && is_tagged_type (TREE_TYPE (decl))             \
3802        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3803            /* This is necessary for stub decls that     \
3804               appear in nested inline functions.  */    \
3805            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3806                && (decl_ultimate_origin (decl)          \
3807                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3808
3809 /* Information concerning the compilation unit's programming
3810    language, and compiler version.  */
3811
3812 /* Fixed size portion of the DWARF compilation unit header.  */
3813 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3814   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3815
3816 /* Fixed size portion of public names info.  */
3817 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3818
3819 /* Fixed size portion of the address range info.  */
3820 #define DWARF_ARANGES_HEADER_SIZE                                       \
3821   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3822                 DWARF2_ADDR_SIZE * 2)                                   \
3823    - DWARF_INITIAL_LENGTH_SIZE)
3824
3825 /* Size of padding portion in the address range info.  It must be
3826    aligned to twice the pointer size.  */
3827 #define DWARF_ARANGES_PAD_SIZE \
3828   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3829                 DWARF2_ADDR_SIZE * 2) \
3830    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3831
3832 /* Use assembler line directives if available.  */
3833 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3834 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3835 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3836 #else
3837 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3838 #endif
3839 #endif
3840
3841 /* Minimum line offset in a special line info. opcode.
3842    This value was chosen to give a reasonable range of values.  */
3843 #define DWARF_LINE_BASE  -10
3844
3845 /* First special line opcode - leave room for the standard opcodes.  */
3846 #define DWARF_LINE_OPCODE_BASE  10
3847
3848 /* Range of line offsets in a special line info. opcode.  */
3849 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3850
3851 /* Flag that indicates the initial value of the is_stmt_start flag.
3852    In the present implementation, we do not mark any lines as
3853    the beginning of a source statement, because that information
3854    is not made available by the GCC front-end.  */
3855 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3856
3857 #ifdef DWARF2_DEBUGGING_INFO
3858 /* This location is used by calc_die_sizes() to keep track
3859    the offset of each DIE within the .debug_info section.  */
3860 static unsigned long next_die_offset;
3861 #endif
3862
3863 /* Record the root of the DIE's built for the current compilation unit.  */
3864 static GTY(()) dw_die_ref comp_unit_die;
3865
3866 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3867 static GTY(()) limbo_die_node *limbo_die_list;
3868
3869 /* Filenames referenced by this compilation unit.  */
3870 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3871
3872 /* A hash table of references to DIE's that describe declarations.
3873    The key is a DECL_UID() which is a unique number identifying each decl.  */
3874 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3875
3876 /* Node of the variable location list.  */
3877 struct var_loc_node GTY ((chain_next ("%h.next")))
3878 {
3879   rtx GTY (()) var_loc_note;
3880   const char * GTY (()) label;
3881   const char * GTY (()) section_label;
3882   struct var_loc_node * GTY (()) next;
3883 };
3884
3885 /* Variable location list.  */
3886 struct var_loc_list_def GTY (())
3887 {
3888   struct var_loc_node * GTY (()) first;
3889
3890   /* Do not mark the last element of the chained list because
3891      it is marked through the chain.  */
3892   struct var_loc_node * GTY ((skip ("%h"))) last;
3893
3894   /* DECL_UID of the variable decl.  */
3895   unsigned int decl_id;
3896 };
3897 typedef struct var_loc_list_def var_loc_list;
3898
3899
3900 /* Table of decl location linked lists.  */
3901 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3902
3903 /* A pointer to the base of a list of references to DIE's that
3904    are uniquely identified by their tag, presence/absence of
3905    children DIE's, and list of attribute/value pairs.  */
3906 static GTY((length ("abbrev_die_table_allocated")))
3907   dw_die_ref *abbrev_die_table;
3908
3909 /* Number of elements currently allocated for abbrev_die_table.  */
3910 static GTY(()) unsigned abbrev_die_table_allocated;
3911
3912 /* Number of elements in type_die_table currently in use.  */
3913 static GTY(()) unsigned abbrev_die_table_in_use;
3914
3915 /* Size (in elements) of increments by which we may expand the
3916    abbrev_die_table.  */
3917 #define ABBREV_DIE_TABLE_INCREMENT 256
3918
3919 /* A pointer to the base of a table that contains line information
3920    for each source code line in .text in the compilation unit.  */
3921 static GTY((length ("line_info_table_allocated")))
3922      dw_line_info_ref line_info_table;
3923
3924 /* Number of elements currently allocated for line_info_table.  */
3925 static GTY(()) unsigned line_info_table_allocated;
3926
3927 /* Number of elements in line_info_table currently in use.  */
3928 static GTY(()) unsigned line_info_table_in_use;
3929
3930 /* True if the compilation unit places functions in more than one section.  */
3931 static GTY(()) bool have_multiple_function_sections = false;
3932
3933 /* A pointer to the base of a table that contains line information
3934    for each source code line outside of .text in the compilation unit.  */
3935 static GTY ((length ("separate_line_info_table_allocated")))
3936      dw_separate_line_info_ref separate_line_info_table;
3937
3938 /* Number of elements currently allocated for separate_line_info_table.  */
3939 static GTY(()) unsigned separate_line_info_table_allocated;
3940
3941 /* Number of elements in separate_line_info_table currently in use.  */
3942 static GTY(()) unsigned separate_line_info_table_in_use;
3943
3944 /* Size (in elements) of increments by which we may expand the
3945    line_info_table.  */
3946 #define LINE_INFO_TABLE_INCREMENT 1024
3947
3948 /* A pointer to the base of a table that contains a list of publicly
3949    accessible names.  */
3950 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3951
3952 /* A pointer to the base of a table that contains a list of publicly
3953    accessible types.  */
3954 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3955
3956 /* Array of dies for which we should generate .debug_arange info.  */
3957 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3958
3959 /* Number of elements currently allocated for arange_table.  */
3960 static GTY(()) unsigned arange_table_allocated;
3961
3962 /* Number of elements in arange_table currently in use.  */
3963 static GTY(()) unsigned arange_table_in_use;
3964
3965 /* Size (in elements) of increments by which we may expand the
3966    arange_table.  */
3967 #define ARANGE_TABLE_INCREMENT 64
3968
3969 /* Array of dies for which we should generate .debug_ranges info.  */
3970 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3971
3972 /* Number of elements currently allocated for ranges_table.  */
3973 static GTY(()) unsigned ranges_table_allocated;
3974
3975 /* Number of elements in ranges_table currently in use.  */
3976 static GTY(()) unsigned ranges_table_in_use;
3977
3978 /* Size (in elements) of increments by which we may expand the
3979    ranges_table.  */
3980 #define RANGES_TABLE_INCREMENT 64
3981
3982 /* Whether we have location lists that need outputting */
3983 static GTY(()) bool have_location_lists;
3984
3985 /* Unique label counter.  */
3986 static GTY(()) unsigned int loclabel_num;
3987
3988 #ifdef DWARF2_DEBUGGING_INFO
3989 /* Record whether the function being analyzed contains inlined functions.  */
3990 static int current_function_has_inlines;
3991 #endif
3992 #if 0 && defined (MIPS_DEBUGGING_INFO)
3993 static int comp_unit_has_inlines;
3994 #endif
3995
3996 /* The last file entry emitted by maybe_emit_file().  */
3997 static GTY(()) struct dwarf_file_data * last_emitted_file;
3998
3999 /* Number of internal labels generated by gen_internal_sym().  */
4000 static GTY(()) int label_num;
4001
4002 /* Cached result of previous call to lookup_filename.  */
4003 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4004
4005 #ifdef DWARF2_DEBUGGING_INFO
4006
4007 /* Offset from the "steady-state frame pointer" to the frame base,
4008    within the current function.  */
4009 static HOST_WIDE_INT frame_pointer_fb_offset;
4010
4011 /* Forward declarations for functions defined in this file.  */
4012
4013 static int is_pseudo_reg (rtx);
4014 static tree type_main_variant (tree);
4015 static int is_tagged_type (tree);
4016 static const char *dwarf_tag_name (unsigned);
4017 static const char *dwarf_attr_name (unsigned);
4018 static const char *dwarf_form_name (unsigned);
4019 static tree decl_ultimate_origin (tree);
4020 static tree block_ultimate_origin (tree);
4021 static tree decl_class_context (tree);
4022 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4023 static inline enum dw_val_class AT_class (dw_attr_ref);
4024 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4025 static inline unsigned AT_flag (dw_attr_ref);
4026 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4027 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4028 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4029 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4030 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4031                               unsigned long);
4032 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4033                                unsigned int, unsigned char *);
4034 static hashval_t debug_str_do_hash (const void *);
4035 static int debug_str_eq (const void *, const void *);
4036 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4037 static inline const char *AT_string (dw_attr_ref);
4038 static int AT_string_form (dw_attr_ref);
4039 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4040 static void add_AT_specification (dw_die_ref, dw_die_ref);
4041 static inline dw_die_ref AT_ref (dw_attr_ref);
4042 static inline int AT_ref_external (dw_attr_ref);
4043 static inline void set_AT_ref_external (dw_attr_ref, int);
4044 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4045 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4046 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4047 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4048                              dw_loc_list_ref);
4049 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4050 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4051 static inline rtx AT_addr (dw_attr_ref);
4052 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4053 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4054 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4055 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4056                            unsigned HOST_WIDE_INT);
4057 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4058                                unsigned long);
4059 static inline const char *AT_lbl (dw_attr_ref);
4060 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4061 static const char *get_AT_low_pc (dw_die_ref);
4062 static const char *get_AT_hi_pc (dw_die_ref);
4063 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4064 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4065 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4066 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4067 static bool is_c_family (void);
4068 static bool is_cxx (void);
4069 static bool is_java (void);
4070 static bool is_fortran (void);
4071 static bool is_ada (void);
4072 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4073 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4074 static void add_child_die (dw_die_ref, dw_die_ref);
4075 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4076 static dw_die_ref lookup_type_die (tree);
4077 static void equate_type_number_to_die (tree, dw_die_ref);
4078 static hashval_t decl_die_table_hash (const void *);
4079 static int decl_die_table_eq (const void *, const void *);
4080 static dw_die_ref lookup_decl_die (tree);
4081 static hashval_t decl_loc_table_hash (const void *);
4082 static int decl_loc_table_eq (const void *, const void *);
4083 static var_loc_list *lookup_decl_loc (tree);
4084 static void equate_decl_number_to_die (tree, dw_die_ref);
4085 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4086 static void print_spaces (FILE *);
4087 static void print_die (dw_die_ref, FILE *);
4088 static void print_dwarf_line_table (FILE *);
4089 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4090 static dw_die_ref pop_compile_unit (dw_die_ref);
4091 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4092 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4093 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4094 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4095 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4096 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4097 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4098 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4099 static void compute_section_prefix (dw_die_ref);
4100 static int is_type_die (dw_die_ref);
4101 static int is_comdat_die (dw_die_ref);
4102 static int is_symbol_die (dw_die_ref);
4103 static void assign_symbol_names (dw_die_ref);
4104 static void break_out_includes (dw_die_ref);
4105 static hashval_t htab_cu_hash (const void *);
4106 static int htab_cu_eq (const void *, const void *);
4107 static void htab_cu_del (void *);
4108 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4109 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4110 static void add_sibling_attributes (dw_die_ref);
4111 static void build_abbrev_table (dw_die_ref);
4112 static void output_location_lists (dw_die_ref);
4113 static int constant_size (long unsigned);
4114 static unsigned long size_of_die (dw_die_ref);
4115 static void calc_die_sizes (dw_die_ref);
4116 static void mark_dies (dw_die_ref);
4117 static void unmark_dies (dw_die_ref);
4118 static void unmark_all_dies (dw_die_ref);
4119 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4120 static unsigned long size_of_aranges (void);
4121 static enum dwarf_form value_format (dw_attr_ref);
4122 static void output_value_format (dw_attr_ref);
4123 static void output_abbrev_section (void);
4124 static void output_die_symbol (dw_die_ref);
4125 static void output_die (dw_die_ref);
4126 static void output_compilation_unit_header (void);
4127 static void output_comp_unit (dw_die_ref, int);
4128 static const char *dwarf2_name (tree, int);
4129 static void add_pubname (tree, dw_die_ref);
4130 static void add_pubtype (tree, dw_die_ref);
4131 static void output_pubnames (VEC (pubname_entry,gc) *);
4132 static void add_arange (tree, dw_die_ref);
4133 static void output_aranges (void);
4134 static unsigned int add_ranges (tree);
4135 static void output_ranges (void);
4136 static void output_line_info (void);
4137 static void output_file_names (void);
4138 static dw_die_ref base_type_die (tree);
4139 static tree root_type (tree);
4140 static int is_base_type (tree);
4141 static bool is_subrange_type (tree);
4142 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4143 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4144 static int type_is_enum (tree);
4145 static unsigned int dbx_reg_number (rtx);
4146 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4147 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4148 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4149 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4150 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4151 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4152 static int is_based_loc (rtx);
4153 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4154 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4155 static dw_loc_descr_ref loc_descriptor (rtx);
4156 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4157 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4158 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4159 static tree field_type (tree);
4160 static unsigned int simple_type_align_in_bits (tree);
4161 static unsigned int simple_decl_align_in_bits (tree);
4162 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4163 static HOST_WIDE_INT field_byte_offset (tree);
4164 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4165                                          dw_loc_descr_ref);
4166 static void add_data_member_location_attribute (dw_die_ref, tree);
4167 static void add_const_value_attribute (dw_die_ref, rtx);
4168 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4169 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4170 static void insert_float (rtx, unsigned char *);
4171 static rtx rtl_for_decl_location (tree);
4172 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4173                                                    enum dwarf_attribute);
4174 static void tree_add_const_value_attribute (dw_die_ref, tree);
4175 static void add_name_attribute (dw_die_ref, const char *);
4176 static void add_comp_dir_attribute (dw_die_ref);
4177 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4178 static void add_subscript_info (dw_die_ref, tree);
4179 static void add_byte_size_attribute (dw_die_ref, tree);
4180 static void add_bit_offset_attribute (dw_die_ref, tree);
4181 static void add_bit_size_attribute (dw_die_ref, tree);
4182 static void add_prototyped_attribute (dw_die_ref, tree);
4183 static void add_abstract_origin_attribute (dw_die_ref, tree);
4184 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4185 static void add_src_coords_attributes (dw_die_ref, tree);
4186 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4187 static void push_decl_scope (tree);
4188 static void pop_decl_scope (void);
4189 static dw_die_ref scope_die_for (tree, dw_die_ref);
4190 static inline int local_scope_p (dw_die_ref);
4191 static inline int class_or_namespace_scope_p (dw_die_ref);
4192 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4193 static void add_calling_convention_attribute (dw_die_ref, tree);
4194 static const char *type_tag (tree);
4195 static tree member_declared_type (tree);
4196 #if 0
4197 static const char *decl_start_label (tree);
4198 #endif
4199 static void gen_array_type_die (tree, dw_die_ref);
4200 #if 0
4201 static void gen_entry_point_die (tree, dw_die_ref);
4202 #endif
4203 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4204 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4205 static void gen_inlined_union_type_die (tree, dw_die_ref);
4206 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4207 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4208 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4209 static void gen_formal_types_die (tree, dw_die_ref);
4210 static void gen_subprogram_die (tree, dw_die_ref);
4211 static void gen_variable_die (tree, dw_die_ref);
4212 static void gen_label_die (tree, dw_die_ref);
4213 static void gen_lexical_block_die (tree, dw_die_ref, int);
4214 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4215 static void gen_field_die (tree, dw_die_ref);
4216 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4217 static dw_die_ref gen_compile_unit_die (const char *);
4218 static void gen_inheritance_die (tree, tree, dw_die_ref);
4219 static void gen_member_die (tree, dw_die_ref);
4220 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4221                                                 enum debug_info_usage);
4222 static void gen_subroutine_type_die (tree, dw_die_ref);
4223 static void gen_typedef_die (tree, dw_die_ref);
4224 static void gen_type_die (tree, dw_die_ref);
4225 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4226 static void gen_block_die (tree, dw_die_ref, int);
4227 static void decls_for_scope (tree, dw_die_ref, int);
4228 static int is_redundant_typedef (tree);
4229 static void gen_namespace_die (tree);
4230 static void gen_decl_die (tree, dw_die_ref);
4231 static dw_die_ref force_decl_die (tree);
4232 static dw_die_ref force_type_die (tree);
4233 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4234 static void declare_in_namespace (tree, dw_die_ref);
4235 static struct dwarf_file_data * lookup_filename (const char *);
4236 static void retry_incomplete_types (void);
4237 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4238 static void splice_child_die (dw_die_ref, dw_die_ref);
4239 static int file_info_cmp (const void *, const void *);
4240 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4241                                      const char *, const char *, unsigned);
4242 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4243                                        const char *, const char *,
4244                                        const char *);
4245 static void output_loc_list (dw_loc_list_ref);
4246 static char *gen_internal_sym (const char *);
4247
4248 static void prune_unmark_dies (dw_die_ref);
4249 static void prune_unused_types_mark (dw_die_ref, int);
4250 static void prune_unused_types_walk (dw_die_ref);
4251 static void prune_unused_types_walk_attribs (dw_die_ref);
4252 static void prune_unused_types_prune (dw_die_ref);
4253 static void prune_unused_types (void);
4254 static int maybe_emit_file (struct dwarf_file_data *fd);
4255
4256 /* Section names used to hold DWARF debugging information.  */
4257 #ifndef DEBUG_INFO_SECTION
4258 #define DEBUG_INFO_SECTION      ".debug_info"
4259 #endif
4260 #ifndef DEBUG_ABBREV_SECTION
4261 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4262 #endif
4263 #ifndef DEBUG_ARANGES_SECTION
4264 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4265 #endif
4266 #ifndef DEBUG_MACINFO_SECTION
4267 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4268 #endif
4269 #ifndef DEBUG_LINE_SECTION
4270 #define DEBUG_LINE_SECTION      ".debug_line"
4271 #endif
4272 #ifndef DEBUG_LOC_SECTION
4273 #define DEBUG_LOC_SECTION       ".debug_loc"
4274 #endif
4275 #ifndef DEBUG_PUBNAMES_SECTION
4276 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4277 #endif
4278 #ifndef DEBUG_STR_SECTION
4279 #define DEBUG_STR_SECTION       ".debug_str"
4280 #endif
4281 #ifndef DEBUG_RANGES_SECTION
4282 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4283 #endif
4284
4285 /* Standard ELF section names for compiled code and data.  */
4286 #ifndef TEXT_SECTION_NAME
4287 #define TEXT_SECTION_NAME       ".text"
4288 #endif
4289
4290 /* Section flags for .debug_str section.  */
4291 #define DEBUG_STR_SECTION_FLAGS \
4292   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4293    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4294    : SECTION_DEBUG)
4295
4296 /* Labels we insert at beginning sections we can reference instead of
4297    the section names themselves.  */
4298
4299 #ifndef TEXT_SECTION_LABEL
4300 #define TEXT_SECTION_LABEL              "Ltext"
4301 #endif
4302 #ifndef COLD_TEXT_SECTION_LABEL
4303 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4304 #endif
4305 #ifndef DEBUG_LINE_SECTION_LABEL
4306 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4307 #endif
4308 #ifndef DEBUG_INFO_SECTION_LABEL
4309 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4310 #endif
4311 #ifndef DEBUG_ABBREV_SECTION_LABEL
4312 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4313 #endif
4314 #ifndef DEBUG_LOC_SECTION_LABEL
4315 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4316 #endif
4317 #ifndef DEBUG_RANGES_SECTION_LABEL
4318 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4319 #endif
4320 #ifndef DEBUG_MACINFO_SECTION_LABEL
4321 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4322 #endif
4323
4324 /* Definitions of defaults for formats and names of various special
4325    (artificial) labels which may be generated within this file (when the -g
4326    options is used and DWARF2_DEBUGGING_INFO is in effect.
4327    If necessary, these may be overridden from within the tm.h file, but
4328    typically, overriding these defaults is unnecessary.  */
4329
4330 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4331 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4332 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4333 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES]; 
4334 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4335 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4336 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4337 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4338 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4339 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4340
4341 #ifndef TEXT_END_LABEL
4342 #define TEXT_END_LABEL          "Letext"
4343 #endif
4344 #ifndef COLD_END_LABEL
4345 #define COLD_END_LABEL          "Letext_cold"
4346 #endif
4347 #ifndef BLOCK_BEGIN_LABEL
4348 #define BLOCK_BEGIN_LABEL       "LBB"
4349 #endif
4350 #ifndef BLOCK_END_LABEL
4351 #define BLOCK_END_LABEL         "LBE"
4352 #endif
4353 #ifndef LINE_CODE_LABEL
4354 #define LINE_CODE_LABEL         "LM"
4355 #endif
4356 #ifndef SEPARATE_LINE_CODE_LABEL
4357 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4358 #endif
4359 \f
4360 /* We allow a language front-end to designate a function that is to be
4361    called to "demangle" any name before it is put into a DIE.  */
4362
4363 static const char *(*demangle_name_func) (const char *);
4364
4365 void
4366 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4367 {
4368   demangle_name_func = func;
4369 }
4370
4371 /* Test if rtl node points to a pseudo register.  */
4372
4373 static inline int
4374 is_pseudo_reg (rtx rtl)
4375 {
4376   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4377           || (GET_CODE (rtl) == SUBREG
4378               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4379 }
4380
4381 /* Return a reference to a type, with its const and volatile qualifiers
4382    removed.  */
4383
4384 static inline tree
4385 type_main_variant (tree type)
4386 {
4387   type = TYPE_MAIN_VARIANT (type);
4388
4389   /* ??? There really should be only one main variant among any group of
4390      variants of a given type (and all of the MAIN_VARIANT values for all
4391      members of the group should point to that one type) but sometimes the C
4392      front-end messes this up for array types, so we work around that bug
4393      here.  */
4394   if (TREE_CODE (type) == ARRAY_TYPE)
4395     while (type != TYPE_MAIN_VARIANT (type))
4396       type = TYPE_MAIN_VARIANT (type);
4397
4398   return type;
4399 }
4400
4401 /* Return nonzero if the given type node represents a tagged type.  */
4402
4403 static inline int
4404 is_tagged_type (tree type)
4405 {
4406   enum tree_code code = TREE_CODE (type);
4407
4408   return (code == RECORD_TYPE || code == UNION_TYPE
4409           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4410 }
4411
4412 /* Convert a DIE tag into its string name.  */
4413
4414 static const char *
4415 dwarf_tag_name (unsigned int tag)
4416 {
4417   switch (tag)
4418     {
4419     case DW_TAG_padding:
4420       return "DW_TAG_padding";
4421     case DW_TAG_array_type:
4422       return "DW_TAG_array_type";
4423     case DW_TAG_class_type:
4424       return "DW_TAG_class_type";
4425     case DW_TAG_entry_point:
4426       return "DW_TAG_entry_point";
4427     case DW_TAG_enumeration_type:
4428       return "DW_TAG_enumeration_type";
4429     case DW_TAG_formal_parameter:
4430       return "DW_TAG_formal_parameter";
4431     case DW_TAG_imported_declaration:
4432       return "DW_TAG_imported_declaration";
4433     case DW_TAG_label:
4434       return "DW_TAG_label";
4435     case DW_TAG_lexical_block:
4436       return "DW_TAG_lexical_block";
4437     case DW_TAG_member:
4438       return "DW_TAG_member";
4439     case DW_TAG_pointer_type:
4440       return "DW_TAG_pointer_type";
4441     case DW_TAG_reference_type:
4442       return "DW_TAG_reference_type";
4443     case DW_TAG_compile_unit:
4444       return "DW_TAG_compile_unit";
4445     case DW_TAG_string_type:
4446       return "DW_TAG_string_type";
4447     case DW_TAG_structure_type:
4448       return "DW_TAG_structure_type";
4449     case DW_TAG_subroutine_type:
4450       return "DW_TAG_subroutine_type";
4451     case DW_TAG_typedef:
4452       return "DW_TAG_typedef";
4453     case DW_TAG_union_type:
4454       return "DW_TAG_union_type";
4455     case DW_TAG_unspecified_parameters:
4456       return "DW_TAG_unspecified_parameters";
4457     case DW_TAG_variant:
4458       return "DW_TAG_variant";
4459     case DW_TAG_common_block:
4460       return "DW_TAG_common_block";
4461     case DW_TAG_common_inclusion:
4462       return "DW_TAG_common_inclusion";
4463     case DW_TAG_inheritance:
4464       return "DW_TAG_inheritance";
4465     case DW_TAG_inlined_subroutine:
4466       return "DW_TAG_inlined_subroutine";
4467     case DW_TAG_module:
4468       return "DW_TAG_module";
4469     case DW_TAG_ptr_to_member_type:
4470       return "DW_TAG_ptr_to_member_type";
4471     case DW_TAG_set_type:
4472       return "DW_TAG_set_type";
4473     case DW_TAG_subrange_type:
4474       return "DW_TAG_subrange_type";
4475     case DW_TAG_with_stmt:
4476       return "DW_TAG_with_stmt";
4477     case DW_TAG_access_declaration:
4478       return "DW_TAG_access_declaration";
4479     case DW_TAG_base_type:
4480       return "DW_TAG_base_type";
4481     case DW_TAG_catch_block:
4482       return "DW_TAG_catch_block";
4483     case DW_TAG_const_type:
4484       return "DW_TAG_const_type";
4485     case DW_TAG_constant:
4486       return "DW_TAG_constant";
4487     case DW_TAG_enumerator:
4488       return "DW_TAG_enumerator";
4489     case DW_TAG_file_type:
4490       return "DW_TAG_file_type";
4491     case DW_TAG_friend:
4492       return "DW_TAG_friend";
4493     case DW_TAG_namelist:
4494       return "DW_TAG_namelist";
4495     case DW_TAG_namelist_item:
4496       return "DW_TAG_namelist_item";
4497     case DW_TAG_namespace:
4498       return "DW_TAG_namespace";
4499     case DW_TAG_packed_type:
4500       return "DW_TAG_packed_type";
4501     case DW_TAG_subprogram:
4502       return "DW_TAG_subprogram";
4503     case DW_TAG_template_type_param:
4504       return "DW_TAG_template_type_param";
4505     case DW_TAG_template_value_param:
4506       return "DW_TAG_template_value_param";
4507     case DW_TAG_thrown_type:
4508       return "DW_TAG_thrown_type";
4509     case DW_TAG_try_block:
4510       return "DW_TAG_try_block";
4511     case DW_TAG_variant_part:
4512       return "DW_TAG_variant_part";
4513     case DW_TAG_variable:
4514       return "DW_TAG_variable";
4515     case DW_TAG_volatile_type:
4516       return "DW_TAG_volatile_type";
4517     case DW_TAG_imported_module:
4518       return "DW_TAG_imported_module";
4519     case DW_TAG_MIPS_loop:
4520       return "DW_TAG_MIPS_loop";
4521     case DW_TAG_format_label:
4522       return "DW_TAG_format_label";
4523     case DW_TAG_function_template:
4524       return "DW_TAG_function_template";
4525     case DW_TAG_class_template:
4526       return "DW_TAG_class_template";
4527     case DW_TAG_GNU_BINCL:
4528       return "DW_TAG_GNU_BINCL";
4529     case DW_TAG_GNU_EINCL:
4530       return "DW_TAG_GNU_EINCL";
4531     default:
4532       return "DW_TAG_<unknown>";
4533     }
4534 }
4535
4536 /* Convert a DWARF attribute code into its string name.  */
4537
4538 static const char *
4539 dwarf_attr_name (unsigned int attr)
4540 {
4541   switch (attr)
4542     {
4543     case DW_AT_sibling:
4544       return "DW_AT_sibling";
4545     case DW_AT_location:
4546       return "DW_AT_location";
4547     case DW_AT_name:
4548       return "DW_AT_name";
4549     case DW_AT_ordering:
4550       return "DW_AT_ordering";
4551     case DW_AT_subscr_data:
4552       return "DW_AT_subscr_data";
4553     case DW_AT_byte_size:
4554       return "DW_AT_byte_size";
4555     case DW_AT_bit_offset:
4556       return "DW_AT_bit_offset";
4557     case DW_AT_bit_size:
4558       return "DW_AT_bit_size";
4559     case DW_AT_element_list:
4560       return "DW_AT_element_list";
4561     case DW_AT_stmt_list:
4562       return "DW_AT_stmt_list";
4563     case DW_AT_low_pc:
4564       return "DW_AT_low_pc";
4565     case DW_AT_high_pc:
4566       return "DW_AT_high_pc";
4567     case DW_AT_language:
4568       return "DW_AT_language";
4569     case DW_AT_member:
4570       return "DW_AT_member";
4571     case DW_AT_discr:
4572       return "DW_AT_discr";
4573     case DW_AT_discr_value:
4574       return "DW_AT_discr_value";
4575     case DW_AT_visibility:
4576       return "DW_AT_visibility";
4577     case DW_AT_import:
4578       return "DW_AT_import";
4579     case DW_AT_string_length:
4580       return "DW_AT_string_length";
4581     case DW_AT_common_reference:
4582       return "DW_AT_common_reference";
4583     case DW_AT_comp_dir:
4584       return "DW_AT_comp_dir";
4585     case DW_AT_const_value:
4586       return "DW_AT_const_value";
4587     case DW_AT_containing_type:
4588       return "DW_AT_containing_type";
4589     case DW_AT_default_value:
4590       return "DW_AT_default_value";
4591     case DW_AT_inline:
4592       return "DW_AT_inline";
4593     case DW_AT_is_optional:
4594       return "DW_AT_is_optional";
4595     case DW_AT_lower_bound:
4596       return "DW_AT_lower_bound";
4597     case DW_AT_producer:
4598       return "DW_AT_producer";
4599     case DW_AT_prototyped:
4600       return "DW_AT_prototyped";
4601     case DW_AT_return_addr:
4602       return "DW_AT_return_addr";
4603     case DW_AT_start_scope:
4604       return "DW_AT_start_scope";
4605     case DW_AT_stride_size:
4606       return "DW_AT_stride_size";
4607     case DW_AT_upper_bound:
4608       return "DW_AT_upper_bound";
4609     case DW_AT_abstract_origin:
4610       return "DW_AT_abstract_origin";
4611     case DW_AT_accessibility:
4612       return "DW_AT_accessibility";
4613     case DW_AT_address_class:
4614       return "DW_AT_address_class";
4615     case DW_AT_artificial:
4616       return "DW_AT_artificial";
4617     case DW_AT_base_types:
4618       return "DW_AT_base_types";
4619     case DW_AT_calling_convention:
4620       return "DW_AT_calling_convention";
4621     case DW_AT_count:
4622       return "DW_AT_count";
4623     case DW_AT_data_member_location:
4624       return "DW_AT_data_member_location";
4625     case DW_AT_decl_column:
4626       return "DW_AT_decl_column";
4627     case DW_AT_decl_file:
4628       return "DW_AT_decl_file";
4629     case DW_AT_decl_line:
4630       return "DW_AT_decl_line";
4631     case DW_AT_declaration:
4632       return "DW_AT_declaration";
4633     case DW_AT_discr_list:
4634       return "DW_AT_discr_list";
4635     case DW_AT_encoding:
4636       return "DW_AT_encoding";
4637     case DW_AT_external:
4638       return "DW_AT_external";
4639     case DW_AT_frame_base:
4640       return "DW_AT_frame_base";
4641     case DW_AT_friend:
4642       return "DW_AT_friend";
4643     case DW_AT_identifier_case:
4644       return "DW_AT_identifier_case";
4645     case DW_AT_macro_info:
4646       return "DW_AT_macro_info";
4647     case DW_AT_namelist_items:
4648       return "DW_AT_namelist_items";
4649     case DW_AT_priority:
4650       return "DW_AT_priority";
4651     case DW_AT_segment:
4652       return "DW_AT_segment";
4653     case DW_AT_specification:
4654       return "DW_AT_specification";
4655     case DW_AT_static_link:
4656       return "DW_AT_static_link";
4657     case DW_AT_type:
4658       return "DW_AT_type";
4659     case DW_AT_use_location:
4660       return "DW_AT_use_location";
4661     case DW_AT_variable_parameter:
4662       return "DW_AT_variable_parameter";
4663     case DW_AT_virtuality:
4664       return "DW_AT_virtuality";
4665     case DW_AT_vtable_elem_location:
4666       return "DW_AT_vtable_elem_location";
4667
4668     case DW_AT_allocated:
4669       return "DW_AT_allocated";
4670     case DW_AT_associated:
4671       return "DW_AT_associated";
4672     case DW_AT_data_location:
4673       return "DW_AT_data_location";
4674     case DW_AT_stride:
4675       return "DW_AT_stride";
4676     case DW_AT_entry_pc:
4677       return "DW_AT_entry_pc";
4678     case DW_AT_use_UTF8:
4679       return "DW_AT_use_UTF8";
4680     case DW_AT_extension:
4681       return "DW_AT_extension";
4682     case DW_AT_ranges:
4683       return "DW_AT_ranges";
4684     case DW_AT_trampoline:
4685       return "DW_AT_trampoline";
4686     case DW_AT_call_column:
4687       return "DW_AT_call_column";
4688     case DW_AT_call_file:
4689       return "DW_AT_call_file";
4690     case DW_AT_call_line:
4691       return "DW_AT_call_line";
4692
4693     case DW_AT_MIPS_fde:
4694       return "DW_AT_MIPS_fde";
4695     case DW_AT_MIPS_loop_begin:
4696       return "DW_AT_MIPS_loop_begin";
4697     case DW_AT_MIPS_tail_loop_begin:
4698       return "DW_AT_MIPS_tail_loop_begin";
4699     case DW_AT_MIPS_epilog_begin:
4700       return "DW_AT_MIPS_epilog_begin";
4701     case DW_AT_MIPS_loop_unroll_factor:
4702       return "DW_AT_MIPS_loop_unroll_factor";
4703     case DW_AT_MIPS_software_pipeline_depth:
4704       return "DW_AT_MIPS_software_pipeline_depth";
4705     case DW_AT_MIPS_linkage_name:
4706       return "DW_AT_MIPS_linkage_name";
4707     case DW_AT_MIPS_stride:
4708       return "DW_AT_MIPS_stride";
4709     case DW_AT_MIPS_abstract_name:
4710       return "DW_AT_MIPS_abstract_name";
4711     case DW_AT_MIPS_clone_origin:
4712       return "DW_AT_MIPS_clone_origin";
4713     case DW_AT_MIPS_has_inlines:
4714       return "DW_AT_MIPS_has_inlines";
4715
4716     case DW_AT_sf_names:
4717       return "DW_AT_sf_names";
4718     case DW_AT_src_info:
4719       return "DW_AT_src_info";
4720     case DW_AT_mac_info:
4721       return "DW_AT_mac_info";
4722     case DW_AT_src_coords:
4723       return "DW_AT_src_coords";
4724     case DW_AT_body_begin:
4725       return "DW_AT_body_begin";
4726     case DW_AT_body_end:
4727       return "DW_AT_body_end";
4728     case DW_AT_GNU_vector:
4729       return "DW_AT_GNU_vector";
4730
4731     case DW_AT_VMS_rtnbeg_pd_address:
4732       return "DW_AT_VMS_rtnbeg_pd_address";
4733
4734     default:
4735       return "DW_AT_<unknown>";
4736     }
4737 }
4738
4739 /* Convert a DWARF value form code into its string name.  */
4740
4741 static const char *
4742 dwarf_form_name (unsigned int form)
4743 {
4744   switch (form)
4745     {
4746     case DW_FORM_addr:
4747       return "DW_FORM_addr";
4748     case DW_FORM_block2:
4749       return "DW_FORM_block2";
4750     case DW_FORM_block4:
4751       return "DW_FORM_block4";
4752     case DW_FORM_data2:
4753       return "DW_FORM_data2";
4754     case DW_FORM_data4:
4755       return "DW_FORM_data4";
4756     case DW_FORM_data8:
4757       return "DW_FORM_data8";
4758     case DW_FORM_string:
4759       return "DW_FORM_string";
4760     case DW_FORM_block:
4761       return "DW_FORM_block";
4762     case DW_FORM_block1:
4763       return "DW_FORM_block1";
4764     case DW_FORM_data1:
4765       return "DW_FORM_data1";
4766     case DW_FORM_flag:
4767       return "DW_FORM_flag";
4768     case DW_FORM_sdata:
4769       return "DW_FORM_sdata";
4770     case DW_FORM_strp:
4771       return "DW_FORM_strp";
4772     case DW_FORM_udata:
4773       return "DW_FORM_udata";
4774     case DW_FORM_ref_addr:
4775       return "DW_FORM_ref_addr";
4776     case DW_FORM_ref1:
4777       return "DW_FORM_ref1";
4778     case DW_FORM_ref2:
4779       return "DW_FORM_ref2";
4780     case DW_FORM_ref4:
4781       return "DW_FORM_ref4";
4782     case DW_FORM_ref8:
4783       return "DW_FORM_ref8";
4784     case DW_FORM_ref_udata:
4785       return "DW_FORM_ref_udata";
4786     case DW_FORM_indirect:
4787       return "DW_FORM_indirect";
4788     default:
4789       return "DW_FORM_<unknown>";
4790     }
4791 }
4792 \f
4793 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4794    instance of an inlined instance of a decl which is local to an inline
4795    function, so we have to trace all of the way back through the origin chain
4796    to find out what sort of node actually served as the original seed for the
4797    given block.  */
4798
4799 static tree
4800 decl_ultimate_origin (tree decl)
4801 {
4802   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4803     return NULL_TREE;
4804
4805   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4806      nodes in the function to point to themselves; ignore that if
4807      we're trying to output the abstract instance of this function.  */
4808   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4809     return NULL_TREE;
4810
4811   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4812      most distant ancestor, this should never happen.  */
4813   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4814
4815   return DECL_ABSTRACT_ORIGIN (decl);
4816 }
4817
4818 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4819    instance of an inlined instance of a block which is local to an inline
4820    function, so we have to trace all of the way back through the origin chain
4821    to find out what sort of node actually served as the original seed for the
4822    given block.  */
4823
4824 static tree
4825 block_ultimate_origin (tree block)
4826 {
4827   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4828
4829   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4830      nodes in the function to point to themselves; ignore that if
4831      we're trying to output the abstract instance of this function.  */
4832   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4833     return NULL_TREE;
4834
4835   if (immediate_origin == NULL_TREE)
4836     return NULL_TREE;
4837   else
4838     {
4839       tree ret_val;
4840       tree lookahead = immediate_origin;
4841
4842       do
4843         {
4844           ret_val = lookahead;
4845           lookahead = (TREE_CODE (ret_val) == BLOCK
4846                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4847         }
4848       while (lookahead != NULL && lookahead != ret_val);
4849       
4850       /* The block's abstract origin chain may not be the *ultimate* origin of
4851          the block. It could lead to a DECL that has an abstract origin set.
4852          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4853          will give us if it has one).  Note that DECL's abstract origins are
4854          supposed to be the most distant ancestor (or so decl_ultimate_origin
4855          claims), so we don't need to loop following the DECL origins.  */
4856       if (DECL_P (ret_val))
4857         return DECL_ORIGIN (ret_val);
4858
4859       return ret_val;
4860     }
4861 }
4862
4863 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4864    of a virtual function may refer to a base class, so we check the 'this'
4865    parameter.  */
4866
4867 static tree
4868 decl_class_context (tree decl)
4869 {
4870   tree context = NULL_TREE;
4871
4872   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4873     context = DECL_CONTEXT (decl);
4874   else
4875     context = TYPE_MAIN_VARIANT
4876       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4877
4878   if (context && !TYPE_P (context))
4879     context = NULL_TREE;
4880
4881   return context;
4882 }
4883 \f
4884 /* Add an attribute/value pair to a DIE.  */
4885
4886 static inline void
4887 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4888 {
4889   /* Maybe this should be an assert?  */
4890   if (die == NULL)
4891     return;
4892   
4893   if (die->die_attr == NULL)
4894     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4895   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4896 }
4897
4898 static inline enum dw_val_class
4899 AT_class (dw_attr_ref a)
4900 {
4901   return a->dw_attr_val.val_class;
4902 }
4903
4904 /* Add a flag value attribute to a DIE.  */
4905
4906 static inline void
4907 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4908 {
4909   dw_attr_node attr;
4910
4911   attr.dw_attr = attr_kind;
4912   attr.dw_attr_val.val_class = dw_val_class_flag;
4913   attr.dw_attr_val.v.val_flag = flag;
4914   add_dwarf_attr (die, &attr);
4915 }
4916
4917 static inline unsigned
4918 AT_flag (dw_attr_ref a)
4919 {
4920   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4921   return a->dw_attr_val.v.val_flag;
4922 }
4923
4924 /* Add a signed integer attribute value to a DIE.  */
4925
4926 static inline void
4927 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4928 {
4929   dw_attr_node attr;
4930
4931   attr.dw_attr = attr_kind;
4932   attr.dw_attr_val.val_class = dw_val_class_const;
4933   attr.dw_attr_val.v.val_int = int_val;
4934   add_dwarf_attr (die, &attr);
4935 }
4936
4937 static inline HOST_WIDE_INT
4938 AT_int (dw_attr_ref a)
4939 {
4940   gcc_assert (a && AT_class (a) == dw_val_class_const);
4941   return a->dw_attr_val.v.val_int;
4942 }
4943
4944 /* Add an unsigned integer attribute value to a DIE.  */
4945
4946 static inline void
4947 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4948                  unsigned HOST_WIDE_INT unsigned_val)
4949 {
4950   dw_attr_node attr;
4951
4952   attr.dw_attr = attr_kind;
4953   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4954   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4955   add_dwarf_attr (die, &attr);
4956 }
4957
4958 static inline unsigned HOST_WIDE_INT
4959 AT_unsigned (dw_attr_ref a)
4960 {
4961   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4962   return a->dw_attr_val.v.val_unsigned;
4963 }
4964
4965 /* Add an unsigned double integer attribute value to a DIE.  */
4966
4967 static inline void
4968 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4969                   long unsigned int val_hi, long unsigned int val_low)
4970 {
4971   dw_attr_node attr;
4972
4973   attr.dw_attr = attr_kind;
4974   attr.dw_attr_val.val_class = dw_val_class_long_long;
4975   attr.dw_attr_val.v.val_long_long.hi = val_hi;
4976   attr.dw_attr_val.v.val_long_long.low = val_low;
4977   add_dwarf_attr (die, &attr);
4978 }
4979
4980 /* Add a floating point attribute value to a DIE and return it.  */
4981
4982 static inline void
4983 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4984             unsigned int length, unsigned int elt_size, unsigned char *array)
4985 {
4986   dw_attr_node attr;
4987
4988   attr.dw_attr = attr_kind;
4989   attr.dw_attr_val.val_class = dw_val_class_vec;
4990   attr.dw_attr_val.v.val_vec.length = length;
4991   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4992   attr.dw_attr_val.v.val_vec.array = array;
4993   add_dwarf_attr (die, &attr);
4994 }
4995
4996 /* Hash and equality functions for debug_str_hash.  */
4997
4998 static hashval_t
4999 debug_str_do_hash (const void *x)
5000 {
5001   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5002 }
5003
5004 static int
5005 debug_str_eq (const void *x1, const void *x2)
5006 {
5007   return strcmp ((((const struct indirect_string_node *)x1)->str),
5008                  (const char *)x2) == 0;
5009 }
5010
5011 /* Add a string attribute value to a DIE.  */
5012
5013 static inline void
5014 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5015 {
5016   dw_attr_node attr;
5017   struct indirect_string_node *node;
5018   void **slot;
5019
5020   if (! debug_str_hash)
5021     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5022                                       debug_str_eq, NULL);
5023
5024   slot = htab_find_slot_with_hash (debug_str_hash, str,
5025                                    htab_hash_string (str), INSERT);
5026   if (*slot == NULL)
5027     *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
5028   node = (struct indirect_string_node *) *slot;
5029   node->str = ggc_strdup (str);
5030   node->refcount++;
5031
5032   attr.dw_attr = attr_kind;
5033   attr.dw_attr_val.val_class = dw_val_class_str;
5034   attr.dw_attr_val.v.val_str = node;
5035   add_dwarf_attr (die, &attr);
5036 }
5037
5038 static inline const char *
5039 AT_string (dw_attr_ref a)
5040 {
5041   gcc_assert (a && AT_class (a) == dw_val_class_str);
5042   return a->dw_attr_val.v.val_str->str;
5043 }
5044
5045 /* Find out whether a string should be output inline in DIE
5046    or out-of-line in .debug_str section.  */
5047
5048 static int
5049 AT_string_form (dw_attr_ref a)
5050 {
5051   struct indirect_string_node *node;
5052   unsigned int len;
5053   char label[32];
5054
5055   gcc_assert (a && AT_class (a) == dw_val_class_str);
5056
5057   node = a->dw_attr_val.v.val_str;
5058   if (node->form)
5059     return node->form;
5060
5061   len = strlen (node->str) + 1;
5062
5063   /* If the string is shorter or equal to the size of the reference, it is
5064      always better to put it inline.  */
5065   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5066     return node->form = DW_FORM_string;
5067
5068   /* If we cannot expect the linker to merge strings in .debug_str
5069      section, only put it into .debug_str if it is worth even in this
5070      single module.  */
5071   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5072       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5073     return node->form = DW_FORM_string;
5074
5075   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5076   ++dw2_string_counter;
5077   node->label = xstrdup (label);
5078
5079   return node->form = DW_FORM_strp;
5080 }
5081
5082 /* Add a DIE reference attribute value to a DIE.  */
5083
5084 static inline void
5085 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5086 {
5087   dw_attr_node attr;
5088
5089   attr.dw_attr = attr_kind;
5090   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5091   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5092   attr.dw_attr_val.v.val_die_ref.external = 0;
5093   add_dwarf_attr (die, &attr);
5094 }
5095
5096 /* Add an AT_specification attribute to a DIE, and also make the back
5097    pointer from the specification to the definition.  */
5098
5099 static inline void
5100 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5101 {
5102   add_AT_die_ref (die, DW_AT_specification, targ_die);
5103   gcc_assert (!targ_die->die_definition);
5104   targ_die->die_definition = die;
5105 }
5106
5107 static inline dw_die_ref
5108 AT_ref (dw_attr_ref a)
5109 {
5110   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5111   return a->dw_attr_val.v.val_die_ref.die;
5112 }
5113
5114 static inline int
5115 AT_ref_external (dw_attr_ref a)
5116 {
5117   if (a && AT_class (a) == dw_val_class_die_ref)
5118     return a->dw_attr_val.v.val_die_ref.external;
5119
5120   return 0;
5121 }
5122
5123 static inline void
5124 set_AT_ref_external (dw_attr_ref a, int i)
5125 {
5126   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5127   a->dw_attr_val.v.val_die_ref.external = i;
5128 }
5129
5130 /* Add an FDE reference attribute value to a DIE.  */
5131
5132 static inline void
5133 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5134 {
5135   dw_attr_node attr;
5136
5137   attr.dw_attr = attr_kind;
5138   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5139   attr.dw_attr_val.v.val_fde_index = targ_fde;
5140   add_dwarf_attr (die, &attr);
5141 }
5142
5143 /* Add a location description attribute value to a DIE.  */
5144
5145 static inline void
5146 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5147 {
5148   dw_attr_node attr;
5149
5150   attr.dw_attr = attr_kind;
5151   attr.dw_attr_val.val_class = dw_val_class_loc;
5152   attr.dw_attr_val.v.val_loc = loc;
5153   add_dwarf_attr (die, &attr);
5154 }
5155
5156 static inline dw_loc_descr_ref
5157 AT_loc (dw_attr_ref a)
5158 {
5159   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5160   return a->dw_attr_val.v.val_loc;
5161 }
5162
5163 static inline void
5164 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5165 {
5166   dw_attr_node attr;
5167
5168   attr.dw_attr = attr_kind;
5169   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5170   attr.dw_attr_val.v.val_loc_list = loc_list;
5171   add_dwarf_attr (die, &attr);
5172   have_location_lists = true;
5173 }
5174
5175 static inline dw_loc_list_ref
5176 AT_loc_list (dw_attr_ref a)
5177 {
5178   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5179   return a->dw_attr_val.v.val_loc_list;
5180 }
5181
5182 /* Add an address constant attribute value to a DIE.  */
5183
5184 static inline void
5185 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5186 {
5187   dw_attr_node attr;
5188
5189   attr.dw_attr = attr_kind;
5190   attr.dw_attr_val.val_class = dw_val_class_addr;
5191   attr.dw_attr_val.v.val_addr = addr;
5192   add_dwarf_attr (die, &attr);
5193 }
5194
5195 /* Get the RTX from to an address DIE attribute.  */
5196
5197 static inline rtx
5198 AT_addr (dw_attr_ref a)
5199 {
5200   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5201   return a->dw_attr_val.v.val_addr;
5202 }
5203
5204 /* Add a file attribute value to a DIE.  */
5205
5206 static inline void
5207 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5208              struct dwarf_file_data *fd)
5209 {
5210   dw_attr_node attr;
5211
5212   attr.dw_attr = attr_kind;
5213   attr.dw_attr_val.val_class = dw_val_class_file;
5214   attr.dw_attr_val.v.val_file = fd;
5215   add_dwarf_attr (die, &attr);
5216 }
5217
5218 /* Get the dwarf_file_data from a file DIE attribute.  */
5219
5220 static inline struct dwarf_file_data *
5221 AT_file (dw_attr_ref a)
5222 {
5223   gcc_assert (a && AT_class (a) == dw_val_class_file);
5224   return a->dw_attr_val.v.val_file;
5225 }
5226
5227 /* Add a label identifier attribute value to a DIE.  */
5228
5229 static inline void
5230 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5231 {
5232   dw_attr_node attr;
5233
5234   attr.dw_attr = attr_kind;
5235   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5236   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5237   add_dwarf_attr (die, &attr);
5238 }
5239
5240 /* Add a section offset attribute value to a DIE, an offset into the
5241    debug_line section.  */
5242
5243 static inline void
5244 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5245                 const char *label)
5246 {
5247   dw_attr_node attr;
5248
5249   attr.dw_attr = attr_kind;
5250   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5251   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5252   add_dwarf_attr (die, &attr);
5253 }
5254
5255 /* Add a section offset attribute value to a DIE, an offset into the
5256    debug_macinfo section.  */
5257
5258 static inline void
5259 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5260                const char *label)
5261 {
5262   dw_attr_node attr;
5263
5264   attr.dw_attr = attr_kind;
5265   attr.dw_attr_val.val_class = dw_val_class_macptr;
5266   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5267   add_dwarf_attr (die, &attr);
5268 }
5269
5270 /* Add an offset attribute value to a DIE.  */
5271
5272 static inline void
5273 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5274                unsigned HOST_WIDE_INT offset)
5275 {
5276   dw_attr_node attr;
5277
5278   attr.dw_attr = attr_kind;
5279   attr.dw_attr_val.val_class = dw_val_class_offset;
5280   attr.dw_attr_val.v.val_offset = offset;
5281   add_dwarf_attr (die, &attr);
5282 }
5283
5284 /* Add an range_list attribute value to a DIE.  */
5285
5286 static void
5287 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5288                    long unsigned int offset)
5289 {
5290   dw_attr_node attr;
5291
5292   attr.dw_attr = attr_kind;
5293   attr.dw_attr_val.val_class = dw_val_class_range_list;
5294   attr.dw_attr_val.v.val_offset = offset;
5295   add_dwarf_attr (die, &attr);
5296 }
5297
5298 static inline const char *
5299 AT_lbl (dw_attr_ref a)
5300 {
5301   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5302                     || AT_class (a) == dw_val_class_lineptr
5303                     || AT_class (a) == dw_val_class_macptr));
5304   return a->dw_attr_val.v.val_lbl_id;
5305 }
5306
5307 /* Get the attribute of type attr_kind.  */
5308
5309 static dw_attr_ref
5310 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5311 {
5312   dw_attr_ref a;
5313   unsigned ix;
5314   dw_die_ref spec = NULL;
5315
5316   if (! die)
5317     return NULL;
5318
5319   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5320     if (a->dw_attr == attr_kind)
5321       return a;
5322     else if (a->dw_attr == DW_AT_specification
5323              || a->dw_attr == DW_AT_abstract_origin)
5324       spec = AT_ref (a);
5325   
5326   if (spec)
5327     return get_AT (spec, attr_kind);
5328
5329   return NULL;
5330 }
5331
5332 /* Return the "low pc" attribute value, typically associated with a subprogram
5333    DIE.  Return null if the "low pc" attribute is either not present, or if it
5334    cannot be represented as an assembler label identifier.  */
5335
5336 static inline const char *
5337 get_AT_low_pc (dw_die_ref die)
5338 {
5339   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5340
5341   return a ? AT_lbl (a) : NULL;
5342 }
5343
5344 /* Return the "high pc" attribute value, typically associated with a subprogram
5345    DIE.  Return null if the "high pc" attribute is either not present, or if it
5346    cannot be represented as an assembler label identifier.  */
5347
5348 static inline const char *
5349 get_AT_hi_pc (dw_die_ref die)
5350 {
5351   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5352
5353   return a ? AT_lbl (a) : NULL;
5354 }
5355
5356 /* Return the value of the string attribute designated by ATTR_KIND, or
5357    NULL if it is not present.  */
5358
5359 static inline const char *
5360 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5361 {
5362   dw_attr_ref a = get_AT (die, attr_kind);
5363
5364   return a ? AT_string (a) : NULL;
5365 }
5366
5367 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5368    if it is not present.  */
5369
5370 static inline int
5371 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5372 {
5373   dw_attr_ref a = get_AT (die, attr_kind);
5374
5375   return a ? AT_flag (a) : 0;
5376 }
5377
5378 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5379    if it is not present.  */
5380
5381 static inline unsigned
5382 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5383 {
5384   dw_attr_ref a = get_AT (die, attr_kind);
5385
5386   return a ? AT_unsigned (a) : 0;
5387 }
5388
5389 static inline dw_die_ref
5390 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5391 {
5392   dw_attr_ref a = get_AT (die, attr_kind);
5393
5394   return a ? AT_ref (a) : NULL;
5395 }
5396
5397 static inline struct dwarf_file_data *
5398 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5399 {
5400   dw_attr_ref a = get_AT (die, attr_kind);
5401
5402   return a ? AT_file (a) : NULL;
5403 }
5404
5405 /* Return TRUE if the language is C or C++.  */
5406
5407 static inline bool
5408 is_c_family (void)
5409 {
5410   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5411
5412   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5413           || lang == DW_LANG_C99
5414           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5415 }
5416
5417 /* Return TRUE if the language is C++.  */
5418
5419 static inline bool
5420 is_cxx (void)
5421 {
5422   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5423   
5424   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5425 }
5426
5427 /* Return TRUE if the language is Fortran.  */
5428
5429 static inline bool
5430 is_fortran (void)
5431 {
5432   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5433
5434   return (lang == DW_LANG_Fortran77
5435           || lang == DW_LANG_Fortran90
5436           || lang == DW_LANG_Fortran95);
5437 }
5438
5439 /* Return TRUE if the language is Java.  */
5440
5441 static inline bool
5442 is_java (void)
5443 {
5444   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5445
5446   return lang == DW_LANG_Java;
5447 }
5448
5449 /* Return TRUE if the language is Ada.  */
5450
5451 static inline bool
5452 is_ada (void)
5453 {
5454   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5455
5456   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5457 }
5458
5459 /* Remove the specified attribute if present.  */
5460
5461 static void
5462 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5463 {
5464   dw_attr_ref a;
5465   unsigned ix;
5466
5467   if (! die)
5468     return;
5469
5470   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5471     if (a->dw_attr == attr_kind)
5472       {
5473         if (AT_class (a) == dw_val_class_str)
5474           if (a->dw_attr_val.v.val_str->refcount)
5475             a->dw_attr_val.v.val_str->refcount--;
5476
5477         /* VEC_ordered_remove should help reduce the number of abbrevs
5478            that are needed.  */
5479         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5480         return;
5481       }
5482 }
5483
5484 /* Remove CHILD from its parent.  PREV must have the property that
5485    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5486
5487 static void
5488 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5489 {
5490   gcc_assert (child->die_parent == prev->die_parent);
5491   gcc_assert (prev->die_sib == child);
5492   if (prev == child)
5493     {
5494       gcc_assert (child->die_parent->die_child == child);
5495       prev = NULL;
5496     }
5497   else
5498     prev->die_sib = child->die_sib;
5499   if (child->die_parent->die_child == child)
5500     child->die_parent->die_child = prev;
5501 }
5502
5503 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5504    matches TAG.  */
5505
5506 static void
5507 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5508 {
5509   dw_die_ref c;
5510   
5511   c = die->die_child;
5512   if (c) do {
5513     dw_die_ref prev = c;
5514     c = c->die_sib;
5515     while (c->die_tag == tag)
5516       {
5517         remove_child_with_prev (c, prev);
5518         /* Might have removed every child.  */
5519         if (c == c->die_sib)
5520           return;
5521         c = c->die_sib;
5522       }
5523   } while (c != die->die_child);
5524 }
5525
5526 /* Add a CHILD_DIE as the last child of DIE.  */
5527
5528 static void
5529 add_child_die (dw_die_ref die, dw_die_ref child_die)
5530 {
5531   /* FIXME this should probably be an assert.  */
5532   if (! die || ! child_die)
5533     return;
5534   gcc_assert (die != child_die);
5535
5536   child_die->die_parent = die;
5537   if (die->die_child)
5538     {
5539       child_die->die_sib = die->die_child->die_sib;
5540       die->die_child->die_sib = child_die;
5541     }
5542   else
5543     child_die->die_sib = child_die;
5544   die->die_child = child_die;
5545 }
5546
5547 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5548    is the specification, to the end of PARENT's list of children.  
5549    This is done by removing and re-adding it.  */
5550
5551 static void
5552 splice_child_die (dw_die_ref parent, dw_die_ref child)
5553 {
5554   dw_die_ref p;
5555
5556   /* We want the declaration DIE from inside the class, not the
5557      specification DIE at toplevel.  */
5558   if (child->die_parent != parent)
5559     {
5560       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5561
5562       if (tmp)
5563         child = tmp;
5564     }
5565
5566   gcc_assert (child->die_parent == parent
5567               || (child->die_parent
5568                   == get_AT_ref (parent, DW_AT_specification)));
5569   
5570   for (p = child->die_parent->die_child; ; p = p->die_sib)
5571     if (p->die_sib == child)
5572       {
5573         remove_child_with_prev (child, p);
5574         break;
5575       }
5576
5577   add_child_die (parent, child);
5578 }
5579
5580 /* Return a pointer to a newly created DIE node.  */
5581
5582 static inline dw_die_ref
5583 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5584 {
5585   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5586
5587   die->die_tag = tag_value;
5588
5589   if (parent_die != NULL)
5590     add_child_die (parent_die, die);
5591   else
5592     {
5593       limbo_die_node *limbo_node;
5594
5595       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5596       limbo_node->die = die;
5597       limbo_node->created_for = t;
5598       limbo_node->next = limbo_die_list;
5599       limbo_die_list = limbo_node;
5600     }
5601
5602   return die;
5603 }
5604
5605 /* Return the DIE associated with the given type specifier.  */
5606
5607 static inline dw_die_ref
5608 lookup_type_die (tree type)
5609 {
5610   return TYPE_SYMTAB_DIE (type);
5611 }
5612
5613 /* Equate a DIE to a given type specifier.  */
5614
5615 static inline void
5616 equate_type_number_to_die (tree type, dw_die_ref type_die)
5617 {
5618   TYPE_SYMTAB_DIE (type) = type_die;
5619 }
5620
5621 /* Returns a hash value for X (which really is a die_struct).  */
5622
5623 static hashval_t
5624 decl_die_table_hash (const void *x)
5625 {
5626   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5627 }
5628
5629 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5630
5631 static int
5632 decl_die_table_eq (const void *x, const void *y)
5633 {
5634   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5635 }
5636
5637 /* Return the DIE associated with a given declaration.  */
5638
5639 static inline dw_die_ref
5640 lookup_decl_die (tree decl)
5641 {
5642   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5643 }
5644
5645 /* Returns a hash value for X (which really is a var_loc_list).  */
5646
5647 static hashval_t
5648 decl_loc_table_hash (const void *x)
5649 {
5650   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5651 }
5652
5653 /* Return nonzero if decl_id of var_loc_list X is the same as
5654    UID of decl *Y.  */
5655
5656 static int
5657 decl_loc_table_eq (const void *x, const void *y)
5658 {
5659   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5660 }
5661
5662 /* Return the var_loc list associated with a given declaration.  */
5663
5664 static inline var_loc_list *
5665 lookup_decl_loc (tree decl)
5666 {
5667   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5668 }
5669
5670 /* Equate a DIE to a particular declaration.  */
5671
5672 static void
5673 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5674 {
5675   unsigned int decl_id = DECL_UID (decl);
5676   void **slot;
5677
5678   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5679   *slot = decl_die;
5680   decl_die->decl_id = decl_id;
5681 }
5682
5683 /* Add a variable location node to the linked list for DECL.  */
5684
5685 static void
5686 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5687 {
5688   unsigned int decl_id = DECL_UID (decl);
5689   var_loc_list *temp;
5690   void **slot;
5691
5692   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5693   if (*slot == NULL)
5694     {
5695       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5696       temp->decl_id = decl_id;
5697       *slot = temp;
5698     }
5699   else
5700     temp = *slot;
5701
5702   if (temp->last)
5703     {
5704       /* If the current location is the same as the end of the list,
5705          we have nothing to do.  */
5706       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5707                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5708         {
5709           /* Add LOC to the end of list and update LAST.  */
5710           temp->last->next = loc;
5711           temp->last = loc;
5712         }
5713     }
5714   /* Do not add empty location to the beginning of the list.  */
5715   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5716     {
5717       temp->first = loc;
5718       temp->last = loc;
5719     }
5720 }
5721 \f
5722 /* Keep track of the number of spaces used to indent the
5723    output of the debugging routines that print the structure of
5724    the DIE internal representation.  */
5725 static int print_indent;
5726
5727 /* Indent the line the number of spaces given by print_indent.  */
5728
5729 static inline void
5730 print_spaces (FILE *outfile)
5731 {
5732   fprintf (outfile, "%*s", print_indent, "");
5733 }
5734
5735 /* Print the information associated with a given DIE, and its children.
5736    This routine is a debugging aid only.  */
5737
5738 static void
5739 print_die (dw_die_ref die, FILE *outfile)
5740 {
5741   dw_attr_ref a;
5742   dw_die_ref c;
5743   unsigned ix;
5744
5745   print_spaces (outfile);
5746   fprintf (outfile, "DIE %4ld: %s\n",
5747            die->die_offset, dwarf_tag_name (die->die_tag));
5748   print_spaces (outfile);
5749   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5750   fprintf (outfile, " offset: %ld\n", die->die_offset);
5751
5752   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5753     {
5754       print_spaces (outfile);
5755       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5756
5757       switch (AT_class (a))
5758         {
5759         case dw_val_class_addr:
5760           fprintf (outfile, "address");
5761           break;
5762         case dw_val_class_offset:
5763           fprintf (outfile, "offset");
5764           break;
5765         case dw_val_class_loc:
5766           fprintf (outfile, "location descriptor");
5767           break;
5768         case dw_val_class_loc_list:
5769           fprintf (outfile, "location list -> label:%s",
5770                    AT_loc_list (a)->ll_symbol);
5771           break;
5772         case dw_val_class_range_list:
5773           fprintf (outfile, "range list");
5774           break;
5775         case dw_val_class_const:
5776           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5777           break;
5778         case dw_val_class_unsigned_const:
5779           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5780           break;
5781         case dw_val_class_long_long:
5782           fprintf (outfile, "constant (%lu,%lu)",
5783                    a->dw_attr_val.v.val_long_long.hi,
5784                    a->dw_attr_val.v.val_long_long.low);
5785           break;
5786         case dw_val_class_vec:
5787           fprintf (outfile, "floating-point or vector constant");
5788           break;
5789         case dw_val_class_flag:
5790           fprintf (outfile, "%u", AT_flag (a));
5791           break;
5792         case dw_val_class_die_ref:
5793           if (AT_ref (a) != NULL)
5794             {
5795               if (AT_ref (a)->die_symbol)
5796                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5797               else
5798                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5799             }
5800           else
5801             fprintf (outfile, "die -> <null>");
5802           break;
5803         case dw_val_class_lbl_id:
5804         case dw_val_class_lineptr:
5805         case dw_val_class_macptr:
5806           fprintf (outfile, "label: %s", AT_lbl (a));
5807           break;
5808         case dw_val_class_str:
5809           if (AT_string (a) != NULL)
5810             fprintf (outfile, "\"%s\"", AT_string (a));
5811           else
5812             fprintf (outfile, "<null>");
5813           break;
5814         case dw_val_class_file:
5815           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5816                    AT_file (a)->emitted_number);
5817           break;
5818         default:
5819           break;
5820         }
5821
5822       fprintf (outfile, "\n");
5823     }
5824
5825   if (die->die_child != NULL)
5826     {
5827       print_indent += 4;
5828       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5829       print_indent -= 4;
5830     }
5831   if (print_indent == 0)
5832     fprintf (outfile, "\n");
5833 }
5834
5835 /* Print the contents of the source code line number correspondence table.
5836    This routine is a debugging aid only.  */
5837
5838 static void
5839 print_dwarf_line_table (FILE *outfile)
5840 {
5841   unsigned i;
5842   dw_line_info_ref line_info;
5843
5844   fprintf (outfile, "\n\nDWARF source line information\n");
5845   for (i = 1; i < line_info_table_in_use; i++)
5846     {
5847       line_info = &line_info_table[i];
5848       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5849                line_info->dw_file_num,
5850                line_info->dw_line_num);
5851     }
5852
5853   fprintf (outfile, "\n\n");
5854 }
5855
5856 /* Print the information collected for a given DIE.  */
5857
5858 void
5859 debug_dwarf_die (dw_die_ref die)
5860 {
5861   print_die (die, stderr);
5862 }
5863
5864 /* Print all DWARF information collected for the compilation unit.
5865    This routine is a debugging aid only.  */
5866
5867 void
5868 debug_dwarf (void)
5869 {
5870   print_indent = 0;
5871   print_die (comp_unit_die, stderr);
5872   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5873     print_dwarf_line_table (stderr);
5874 }
5875 \f
5876 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5877    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5878    DIE that marks the start of the DIEs for this include file.  */
5879
5880 static dw_die_ref
5881 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5882 {
5883   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5884   dw_die_ref new_unit = gen_compile_unit_die (filename);
5885
5886   new_unit->die_sib = old_unit;
5887   return new_unit;
5888 }
5889
5890 /* Close an include-file CU and reopen the enclosing one.  */
5891
5892 static dw_die_ref
5893 pop_compile_unit (dw_die_ref old_unit)
5894 {
5895   dw_die_ref new_unit = old_unit->die_sib;
5896
5897   old_unit->die_sib = NULL;
5898   return new_unit;
5899 }
5900
5901 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5902 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5903
5904 /* Calculate the checksum of a location expression.  */
5905
5906 static inline void
5907 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5908 {
5909   CHECKSUM (loc->dw_loc_opc);
5910   CHECKSUM (loc->dw_loc_oprnd1);
5911   CHECKSUM (loc->dw_loc_oprnd2);
5912 }
5913
5914 /* Calculate the checksum of an attribute.  */
5915
5916 static void
5917 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5918 {
5919   dw_loc_descr_ref loc;
5920   rtx r;
5921
5922   CHECKSUM (at->dw_attr);
5923
5924   /* We don't care that this was compiled with a different compiler
5925      snapshot; if the output is the same, that's what matters.  */
5926   if (at->dw_attr == DW_AT_producer)
5927     return;
5928
5929   switch (AT_class (at))
5930     {
5931     case dw_val_class_const:
5932       CHECKSUM (at->dw_attr_val.v.val_int);
5933       break;
5934     case dw_val_class_unsigned_const:
5935       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5936       break;
5937     case dw_val_class_long_long:
5938       CHECKSUM (at->dw_attr_val.v.val_long_long);
5939       break;
5940     case dw_val_class_vec:
5941       CHECKSUM (at->dw_attr_val.v.val_vec);
5942       break;
5943     case dw_val_class_flag:
5944       CHECKSUM (at->dw_attr_val.v.val_flag);
5945       break;
5946     case dw_val_class_str:
5947       CHECKSUM_STRING (AT_string (at));
5948       break;
5949
5950     case dw_val_class_addr:
5951       r = AT_addr (at);
5952       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5953       CHECKSUM_STRING (XSTR (r, 0));
5954       break;
5955
5956     case dw_val_class_offset:
5957       CHECKSUM (at->dw_attr_val.v.val_offset);
5958       break;
5959
5960     case dw_val_class_loc:
5961       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5962         loc_checksum (loc, ctx);
5963       break;
5964
5965     case dw_val_class_die_ref:
5966       die_checksum (AT_ref (at), ctx, mark);
5967       break;
5968
5969     case dw_val_class_fde_ref:
5970     case dw_val_class_lbl_id:
5971     case dw_val_class_lineptr:
5972     case dw_val_class_macptr:
5973       break;
5974
5975     case dw_val_class_file:
5976       CHECKSUM_STRING (AT_file (at)->filename);
5977       break;
5978
5979     default:
5980       break;
5981     }
5982 }
5983
5984 /* Calculate the checksum of a DIE.  */
5985
5986 static void
5987 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5988 {
5989   dw_die_ref c;
5990   dw_attr_ref a;
5991   unsigned ix;
5992
5993   /* To avoid infinite recursion.  */
5994   if (die->die_mark)
5995     {
5996       CHECKSUM (die->die_mark);
5997       return;
5998     }
5999   die->die_mark = ++(*mark);
6000
6001   CHECKSUM (die->die_tag);
6002
6003   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6004     attr_checksum (a, ctx, mark);
6005
6006   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6007 }
6008
6009 #undef CHECKSUM
6010 #undef CHECKSUM_STRING
6011
6012 /* Do the location expressions look same?  */
6013 static inline int
6014 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6015 {
6016   return loc1->dw_loc_opc == loc2->dw_loc_opc
6017          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6018          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6019 }
6020
6021 /* Do the values look the same?  */
6022 static int
6023 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6024 {
6025   dw_loc_descr_ref loc1, loc2;
6026   rtx r1, r2;
6027
6028   if (v1->val_class != v2->val_class)
6029     return 0;
6030
6031   switch (v1->val_class)
6032     {
6033     case dw_val_class_const:
6034       return v1->v.val_int == v2->v.val_int;
6035     case dw_val_class_unsigned_const:
6036       return v1->v.val_unsigned == v2->v.val_unsigned;
6037     case dw_val_class_long_long:
6038       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6039              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6040     case dw_val_class_vec:
6041       if (v1->v.val_vec.length != v2->v.val_vec.length
6042           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6043         return 0;
6044       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6045                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6046         return 0;
6047       return 1;
6048     case dw_val_class_flag:
6049       return v1->v.val_flag == v2->v.val_flag;
6050     case dw_val_class_str:
6051       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6052
6053     case dw_val_class_addr:
6054       r1 = v1->v.val_addr;
6055       r2 = v2->v.val_addr;
6056       if (GET_CODE (r1) != GET_CODE (r2))
6057         return 0;
6058       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6059       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6060
6061     case dw_val_class_offset:
6062       return v1->v.val_offset == v2->v.val_offset;
6063
6064     case dw_val_class_loc:
6065       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6066            loc1 && loc2;
6067            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6068         if (!same_loc_p (loc1, loc2, mark))
6069           return 0;
6070       return !loc1 && !loc2;
6071
6072     case dw_val_class_die_ref:
6073       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6074
6075     case dw_val_class_fde_ref:
6076     case dw_val_class_lbl_id:
6077     case dw_val_class_lineptr:
6078     case dw_val_class_macptr:
6079       return 1;
6080
6081     case dw_val_class_file:
6082       return v1->v.val_file == v2->v.val_file;
6083
6084     default:
6085       return 1;
6086     }
6087 }
6088
6089 /* Do the attributes look the same?  */
6090
6091 static int
6092 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6093 {
6094   if (at1->dw_attr != at2->dw_attr)
6095     return 0;
6096
6097   /* We don't care that this was compiled with a different compiler
6098      snapshot; if the output is the same, that's what matters. */
6099   if (at1->dw_attr == DW_AT_producer)
6100     return 1;
6101
6102   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6103 }
6104
6105 /* Do the dies look the same?  */
6106
6107 static int
6108 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6109 {
6110   dw_die_ref c1, c2;
6111   dw_attr_ref a1;
6112   unsigned ix;
6113
6114   /* To avoid infinite recursion.  */
6115   if (die1->die_mark)
6116     return die1->die_mark == die2->die_mark;
6117   die1->die_mark = die2->die_mark = ++(*mark);
6118
6119   if (die1->die_tag != die2->die_tag)
6120     return 0;
6121
6122   if (VEC_length (dw_attr_node, die1->die_attr)
6123       != VEC_length (dw_attr_node, die2->die_attr))
6124     return 0;
6125   
6126   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6127     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6128       return 0;
6129
6130   c1 = die1->die_child;
6131   c2 = die2->die_child;
6132   if (! c1)
6133     {
6134       if (c2)
6135         return 0;
6136     }
6137   else
6138     for (;;)
6139       {
6140         if (!same_die_p (c1, c2, mark))
6141           return 0;
6142         c1 = c1->die_sib;
6143         c2 = c2->die_sib;
6144         if (c1 == die1->die_child)
6145           {
6146             if (c2 == die2->die_child)
6147               break;
6148             else
6149               return 0;
6150           }
6151     }
6152
6153   return 1;
6154 }
6155
6156 /* Do the dies look the same?  Wrapper around same_die_p.  */
6157
6158 static int
6159 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6160 {
6161   int mark = 0;
6162   int ret = same_die_p (die1, die2, &mark);
6163
6164   unmark_all_dies (die1);
6165   unmark_all_dies (die2);
6166
6167   return ret;
6168 }
6169
6170 /* The prefix to attach to symbols on DIEs in the current comdat debug
6171    info section.  */
6172 static char *comdat_symbol_id;
6173
6174 /* The index of the current symbol within the current comdat CU.  */
6175 static unsigned int comdat_symbol_number;
6176
6177 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6178    children, and set comdat_symbol_id accordingly.  */
6179
6180 static void
6181 compute_section_prefix (dw_die_ref unit_die)
6182 {
6183   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6184   const char *base = die_name ? lbasename (die_name) : "anonymous";
6185   char *name = alloca (strlen (base) + 64);
6186   char *p;
6187   int i, mark;
6188   unsigned char checksum[16];
6189   struct md5_ctx ctx;
6190
6191   /* Compute the checksum of the DIE, then append part of it as hex digits to
6192      the name filename of the unit.  */
6193
6194   md5_init_ctx (&ctx);
6195   mark = 0;
6196   die_checksum (unit_die, &ctx, &mark);
6197   unmark_all_dies (unit_die);
6198   md5_finish_ctx (&ctx, checksum);
6199
6200   sprintf (name, "%s.", base);
6201   clean_symbol_name (name);
6202
6203   p = name + strlen (name);
6204   for (i = 0; i < 4; i++)
6205     {
6206       sprintf (p, "%.2x", checksum[i]);
6207       p += 2;
6208     }
6209
6210   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6211   comdat_symbol_number = 0;
6212 }
6213
6214 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6215
6216 static int
6217 is_type_die (dw_die_ref die)
6218 {
6219   switch (die->die_tag)
6220     {
6221     case DW_TAG_array_type:
6222     case DW_TAG_class_type:
6223     case DW_TAG_enumeration_type:
6224     case DW_TAG_pointer_type:
6225     case DW_TAG_reference_type:
6226     case DW_TAG_string_type:
6227     case DW_TAG_structure_type:
6228     case DW_TAG_subroutine_type:
6229     case DW_TAG_union_type:
6230     case DW_TAG_ptr_to_member_type:
6231     case DW_TAG_set_type:
6232     case DW_TAG_subrange_type:
6233     case DW_TAG_base_type:
6234     case DW_TAG_const_type:
6235     case DW_TAG_file_type:
6236     case DW_TAG_packed_type:
6237     case DW_TAG_volatile_type:
6238     case DW_TAG_typedef:
6239       return 1;
6240     default:
6241       return 0;
6242     }
6243 }
6244
6245 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6246    Basically, we want to choose the bits that are likely to be shared between
6247    compilations (types) and leave out the bits that are specific to individual
6248    compilations (functions).  */
6249
6250 static int
6251 is_comdat_die (dw_die_ref c)
6252 {
6253   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6254      we do for stabs.  The advantage is a greater likelihood of sharing between
6255      objects that don't include headers in the same order (and therefore would
6256      put the base types in a different comdat).  jason 8/28/00 */
6257
6258   if (c->die_tag == DW_TAG_base_type)
6259     return 0;
6260
6261   if (c->die_tag == DW_TAG_pointer_type
6262       || c->die_tag == DW_TAG_reference_type
6263       || c->die_tag == DW_TAG_const_type
6264       || c->die_tag == DW_TAG_volatile_type)
6265     {
6266       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6267
6268       return t ? is_comdat_die (t) : 0;
6269     }
6270
6271   return is_type_die (c);
6272 }
6273
6274 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6275    compilation unit.  */
6276
6277 static int
6278 is_symbol_die (dw_die_ref c)
6279 {
6280   return (is_type_die (c)
6281           || (get_AT (c, DW_AT_declaration)
6282               && !get_AT (c, DW_AT_specification))
6283           || c->die_tag == DW_TAG_namespace);
6284 }
6285
6286 static char *
6287 gen_internal_sym (const char *prefix)
6288 {
6289   char buf[256];
6290
6291   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6292   return xstrdup (buf);
6293 }
6294
6295 /* Assign symbols to all worthy DIEs under DIE.  */
6296
6297 static void
6298 assign_symbol_names (dw_die_ref die)
6299 {
6300   dw_die_ref c;
6301
6302   if (is_symbol_die (die))
6303     {
6304       if (comdat_symbol_id)
6305         {
6306           char *p = alloca (strlen (comdat_symbol_id) + 64);
6307
6308           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6309                    comdat_symbol_id, comdat_symbol_number++);
6310           die->die_symbol = xstrdup (p);
6311         }
6312       else
6313         die->die_symbol = gen_internal_sym ("LDIE");
6314     }
6315
6316   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6317 }
6318
6319 struct cu_hash_table_entry
6320 {
6321   dw_die_ref cu;
6322   unsigned min_comdat_num, max_comdat_num;
6323   struct cu_hash_table_entry *next;
6324 };
6325
6326 /* Routines to manipulate hash table of CUs.  */
6327 static hashval_t
6328 htab_cu_hash (const void *of)
6329 {
6330   const struct cu_hash_table_entry *entry = of;
6331
6332   return htab_hash_string (entry->cu->die_symbol);
6333 }
6334
6335 static int
6336 htab_cu_eq (const void *of1, const void *of2)
6337 {
6338   const struct cu_hash_table_entry *entry1 = of1;
6339   const struct die_struct *entry2 = of2;
6340
6341   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6342 }
6343
6344 static void
6345 htab_cu_del (void *what)
6346 {
6347   struct cu_hash_table_entry *next, *entry = what;
6348
6349   while (entry)
6350     {
6351       next = entry->next;
6352       free (entry);
6353       entry = next;
6354     }
6355 }
6356
6357 /* Check whether we have already seen this CU and set up SYM_NUM
6358    accordingly.  */
6359 static int
6360 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6361 {
6362   struct cu_hash_table_entry dummy;
6363   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6364
6365   dummy.max_comdat_num = 0;
6366
6367   slot = (struct cu_hash_table_entry **)
6368     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6369         INSERT);
6370   entry = *slot;
6371
6372   for (; entry; last = entry, entry = entry->next)
6373     {
6374       if (same_die_p_wrap (cu, entry->cu))
6375         break;
6376     }
6377
6378   if (entry)
6379     {
6380       *sym_num = entry->min_comdat_num;
6381       return 1;
6382     }
6383
6384   entry = XCNEW (struct cu_hash_table_entry);
6385   entry->cu = cu;
6386   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6387   entry->next = *slot;
6388   *slot = entry;
6389
6390   return 0;
6391 }
6392
6393 /* Record SYM_NUM to record of CU in HTABLE.  */
6394 static void
6395 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6396 {
6397   struct cu_hash_table_entry **slot, *entry;
6398
6399   slot = (struct cu_hash_table_entry **)
6400     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6401         NO_INSERT);
6402   entry = *slot;
6403
6404   entry->max_comdat_num = sym_num;
6405 }
6406
6407 /* Traverse the DIE (which is always comp_unit_die), and set up
6408    additional compilation units for each of the include files we see
6409    bracketed by BINCL/EINCL.  */
6410
6411 static void
6412 break_out_includes (dw_die_ref die)
6413 {
6414   dw_die_ref c;
6415   dw_die_ref unit = NULL;
6416   limbo_die_node *node, **pnode;
6417   htab_t cu_hash_table;
6418
6419   c = die->die_child;
6420   if (c) do {
6421     dw_die_ref prev = c;
6422     c = c->die_sib;
6423     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6424            || (unit && is_comdat_die (c)))
6425       {
6426         dw_die_ref next = c->die_sib;
6427
6428         /* This DIE is for a secondary CU; remove it from the main one.  */
6429         remove_child_with_prev (c, prev);
6430         
6431         if (c->die_tag == DW_TAG_GNU_BINCL)
6432           unit = push_new_compile_unit (unit, c);
6433         else if (c->die_tag == DW_TAG_GNU_EINCL)
6434           unit = pop_compile_unit (unit);
6435         else
6436           add_child_die (unit, c);
6437         c = next;
6438         if (c == die->die_child)
6439           break;
6440       }
6441   } while (c != die->die_child);
6442
6443 #if 0
6444   /* We can only use this in debugging, since the frontend doesn't check
6445      to make sure that we leave every include file we enter.  */
6446   gcc_assert (!unit);
6447 #endif
6448
6449   assign_symbol_names (die);
6450   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6451   for (node = limbo_die_list, pnode = &limbo_die_list;
6452        node;
6453        node = node->next)
6454     {
6455       int is_dupl;
6456
6457       compute_section_prefix (node->die);
6458       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6459                         &comdat_symbol_number);
6460       assign_symbol_names (node->die);
6461       if (is_dupl)
6462         *pnode = node->next;
6463       else
6464         {
6465           pnode = &node->next;
6466           record_comdat_symbol_number (node->die, cu_hash_table,
6467                 comdat_symbol_number);
6468         }
6469     }
6470   htab_delete (cu_hash_table);
6471 }
6472
6473 /* Traverse the DIE and add a sibling attribute if it may have the
6474    effect of speeding up access to siblings.  To save some space,
6475    avoid generating sibling attributes for DIE's without children.  */
6476
6477 static void
6478 add_sibling_attributes (dw_die_ref die)
6479 {
6480   dw_die_ref c;
6481
6482   if (! die->die_child)
6483     return;
6484
6485   if (die->die_parent && die != die->die_parent->die_child)
6486     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6487
6488   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6489 }
6490
6491 /* Output all location lists for the DIE and its children.  */
6492
6493 static void
6494 output_location_lists (dw_die_ref die)
6495 {
6496   dw_die_ref c;
6497   dw_attr_ref a;
6498   unsigned ix;
6499
6500   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6501     if (AT_class (a) == dw_val_class_loc_list)
6502       output_loc_list (AT_loc_list (a));
6503
6504   FOR_EACH_CHILD (die, c, output_location_lists (c));
6505 }
6506
6507 /* The format of each DIE (and its attribute value pairs) is encoded in an
6508    abbreviation table.  This routine builds the abbreviation table and assigns
6509    a unique abbreviation id for each abbreviation entry.  The children of each
6510    die are visited recursively.  */
6511
6512 static void
6513 build_abbrev_table (dw_die_ref die)
6514 {
6515   unsigned long abbrev_id;
6516   unsigned int n_alloc;
6517   dw_die_ref c;
6518   dw_attr_ref a;
6519   unsigned ix;
6520
6521   /* Scan the DIE references, and mark as external any that refer to
6522      DIEs from other CUs (i.e. those which are not marked).  */
6523   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6524     if (AT_class (a) == dw_val_class_die_ref
6525         && AT_ref (a)->die_mark == 0)
6526       {
6527         gcc_assert (AT_ref (a)->die_symbol);
6528
6529         set_AT_ref_external (a, 1);
6530       }
6531
6532   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6533     {
6534       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6535       dw_attr_ref die_a, abbrev_a;
6536       unsigned ix;
6537       bool ok = true;
6538       
6539       if (abbrev->die_tag != die->die_tag)
6540         continue;
6541       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6542         continue;
6543       
6544       if (VEC_length (dw_attr_node, abbrev->die_attr)
6545           != VEC_length (dw_attr_node, die->die_attr))
6546         continue;
6547   
6548       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6549         {
6550           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6551           if ((abbrev_a->dw_attr != die_a->dw_attr)
6552               || (value_format (abbrev_a) != value_format (die_a)))
6553             {
6554               ok = false;
6555               break;
6556             }
6557         }
6558       if (ok)
6559         break;
6560     }
6561
6562   if (abbrev_id >= abbrev_die_table_in_use)
6563     {
6564       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6565         {
6566           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6567           abbrev_die_table = ggc_realloc (abbrev_die_table,
6568                                           sizeof (dw_die_ref) * n_alloc);
6569
6570           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6571                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6572           abbrev_die_table_allocated = n_alloc;
6573         }
6574
6575       ++abbrev_die_table_in_use;
6576       abbrev_die_table[abbrev_id] = die;
6577     }
6578
6579   die->die_abbrev = abbrev_id;
6580   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6581 }
6582 \f
6583 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6584
6585 static int
6586 constant_size (long unsigned int value)
6587 {
6588   int log;
6589
6590   if (value == 0)
6591     log = 0;
6592   else
6593     log = floor_log2 (value);
6594
6595   log = log / 8;
6596   log = 1 << (floor_log2 (log) + 1);
6597
6598   return log;
6599 }
6600
6601 /* Return the size of a DIE as it is represented in the
6602    .debug_info section.  */
6603
6604 static unsigned long
6605 size_of_die (dw_die_ref die)
6606 {
6607   unsigned long size = 0;
6608   dw_attr_ref a;
6609   unsigned ix;
6610
6611   size += size_of_uleb128 (die->die_abbrev);
6612   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6613     {
6614       switch (AT_class (a))
6615         {
6616         case dw_val_class_addr:
6617           size += DWARF2_ADDR_SIZE;
6618           break;
6619         case dw_val_class_offset:
6620           size += DWARF_OFFSET_SIZE;
6621           break;
6622         case dw_val_class_loc:
6623           {
6624             unsigned long lsize = size_of_locs (AT_loc (a));
6625
6626             /* Block length.  */
6627             size += constant_size (lsize);
6628             size += lsize;
6629           }
6630           break;
6631         case dw_val_class_loc_list:
6632           size += DWARF_OFFSET_SIZE;
6633           break;
6634         case dw_val_class_range_list:
6635           size += DWARF_OFFSET_SIZE;
6636           break;
6637         case dw_val_class_const:
6638           size += size_of_sleb128 (AT_int (a));
6639           break;
6640         case dw_val_class_unsigned_const:
6641           size += constant_size (AT_unsigned (a));
6642           break;
6643         case dw_val_class_long_long:
6644           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6645           break;
6646         case dw_val_class_vec:
6647           size += 1 + (a->dw_attr_val.v.val_vec.length
6648                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6649           break;
6650         case dw_val_class_flag:
6651           size += 1;
6652           break;
6653         case dw_val_class_die_ref:
6654           if (AT_ref_external (a))
6655             size += DWARF2_ADDR_SIZE;
6656           else
6657             size += DWARF_OFFSET_SIZE;
6658           break;
6659         case dw_val_class_fde_ref:
6660           size += DWARF_OFFSET_SIZE;
6661           break;
6662         case dw_val_class_lbl_id:
6663           size += DWARF2_ADDR_SIZE;
6664           break;
6665         case dw_val_class_lineptr:
6666         case dw_val_class_macptr:
6667           size += DWARF_OFFSET_SIZE;
6668           break;
6669         case dw_val_class_str:
6670           if (AT_string_form (a) == DW_FORM_strp)
6671             size += DWARF_OFFSET_SIZE;
6672           else
6673             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6674           break;
6675         case dw_val_class_file:
6676           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6677           break;
6678         default:
6679           gcc_unreachable ();
6680         }
6681     }
6682
6683   return size;
6684 }
6685
6686 /* Size the debugging information associated with a given DIE.  Visits the
6687    DIE's children recursively.  Updates the global variable next_die_offset, on
6688    each time through.  Uses the current value of next_die_offset to update the
6689    die_offset field in each DIE.  */
6690
6691 static void
6692 calc_die_sizes (dw_die_ref die)
6693 {
6694   dw_die_ref c;
6695
6696   die->die_offset = next_die_offset;
6697   next_die_offset += size_of_die (die);
6698
6699   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6700
6701   if (die->die_child != NULL)
6702     /* Count the null byte used to terminate sibling lists.  */
6703     next_die_offset += 1;
6704 }
6705
6706 /* Set the marks for a die and its children.  We do this so
6707    that we know whether or not a reference needs to use FORM_ref_addr; only
6708    DIEs in the same CU will be marked.  We used to clear out the offset
6709    and use that as the flag, but ran into ordering problems.  */
6710
6711 static void
6712 mark_dies (dw_die_ref die)
6713 {
6714   dw_die_ref c;
6715
6716   gcc_assert (!die->die_mark);
6717
6718   die->die_mark = 1;
6719   FOR_EACH_CHILD (die, c, mark_dies (c));
6720 }
6721
6722 /* Clear the marks for a die and its children.  */
6723
6724 static void
6725 unmark_dies (dw_die_ref die)
6726 {
6727   dw_die_ref c;
6728
6729   gcc_assert (die->die_mark);
6730
6731   die->die_mark = 0;
6732   FOR_EACH_CHILD (die, c, unmark_dies (c));
6733 }
6734
6735 /* Clear the marks for a die, its children and referred dies.  */
6736
6737 static void
6738 unmark_all_dies (dw_die_ref die)
6739 {
6740   dw_die_ref c;
6741   dw_attr_ref a;
6742   unsigned ix;
6743
6744   if (!die->die_mark)
6745     return;
6746   die->die_mark = 0;
6747
6748   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6749
6750   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6751     if (AT_class (a) == dw_val_class_die_ref)
6752       unmark_all_dies (AT_ref (a));
6753 }
6754
6755 /* Return the size of the .debug_pubnames or .debug_pubtypes table  
6756    generated for the compilation unit.  */
6757
6758 static unsigned long
6759 size_of_pubnames (VEC (pubname_entry, gc) * names)
6760 {
6761   unsigned long size;
6762   unsigned i;
6763   pubname_ref p;
6764
6765   size = DWARF_PUBNAMES_HEADER_SIZE;
6766   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6767     if (names != pubtype_table
6768         || p->die->die_offset != 0
6769         || !flag_eliminate_unused_debug_types)
6770       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6771
6772   size += DWARF_OFFSET_SIZE;
6773   return size;
6774 }
6775
6776 /* Return the size of the information in the .debug_aranges section.  */
6777
6778 static unsigned long
6779 size_of_aranges (void)
6780 {
6781   unsigned long size;
6782
6783   size = DWARF_ARANGES_HEADER_SIZE;
6784
6785   /* Count the address/length pair for this compilation unit.  */
6786   size += 2 * DWARF2_ADDR_SIZE;
6787   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6788
6789   /* Count the two zero words used to terminated the address range table.  */
6790   size += 2 * DWARF2_ADDR_SIZE;
6791   return size;
6792 }
6793 \f
6794 /* Select the encoding of an attribute value.  */
6795
6796 static enum dwarf_form
6797 value_format (dw_attr_ref a)
6798 {
6799   switch (a->dw_attr_val.val_class)
6800     {
6801     case dw_val_class_addr:
6802       return DW_FORM_addr;
6803     case dw_val_class_range_list:
6804     case dw_val_class_offset:
6805     case dw_val_class_loc_list:
6806       switch (DWARF_OFFSET_SIZE)
6807         {
6808         case 4:
6809           return DW_FORM_data4;
6810         case 8:
6811           return DW_FORM_data8;
6812         default:
6813           gcc_unreachable ();
6814         }
6815     case dw_val_class_loc:
6816       switch (constant_size (size_of_locs (AT_loc (a))))
6817         {
6818         case 1:
6819           return DW_FORM_block1;
6820         case 2:
6821           return DW_FORM_block2;
6822         default:
6823           gcc_unreachable ();
6824         }
6825     case dw_val_class_const:
6826       return DW_FORM_sdata;
6827     case dw_val_class_unsigned_const:
6828       switch (constant_size (AT_unsigned (a)))
6829         {
6830         case 1:
6831           return DW_FORM_data1;
6832         case 2:
6833           return DW_FORM_data2;
6834         case 4:
6835           return DW_FORM_data4;
6836         case 8:
6837           return DW_FORM_data8;
6838         default:
6839           gcc_unreachable ();
6840         }
6841     case dw_val_class_long_long:
6842       return DW_FORM_block1;
6843     case dw_val_class_vec:
6844       return DW_FORM_block1;
6845     case dw_val_class_flag:
6846       return DW_FORM_flag;
6847     case dw_val_class_die_ref:
6848       if (AT_ref_external (a))
6849         return DW_FORM_ref_addr;
6850       else
6851         return DW_FORM_ref;
6852     case dw_val_class_fde_ref:
6853       return DW_FORM_data;
6854     case dw_val_class_lbl_id:
6855       return DW_FORM_addr;
6856     case dw_val_class_lineptr:
6857     case dw_val_class_macptr:
6858       return DW_FORM_data;
6859     case dw_val_class_str:
6860       return AT_string_form (a);
6861     case dw_val_class_file:
6862       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6863         {
6864         case 1:
6865           return DW_FORM_data1;
6866         case 2:
6867           return DW_FORM_data2;
6868         case 4:
6869           return DW_FORM_data4;
6870         default:
6871           gcc_unreachable ();
6872         }
6873
6874     default:
6875       gcc_unreachable ();
6876     }
6877 }
6878
6879 /* Output the encoding of an attribute value.  */
6880
6881 static void
6882 output_value_format (dw_attr_ref a)
6883 {
6884   enum dwarf_form form = value_format (a);
6885
6886   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6887 }
6888
6889 /* Output the .debug_abbrev section which defines the DIE abbreviation
6890    table.  */
6891
6892 static void
6893 output_abbrev_section (void)
6894 {
6895   unsigned long abbrev_id;
6896
6897   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6898     {
6899       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6900       unsigned ix;
6901       dw_attr_ref a_attr;
6902
6903       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6904       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6905                                    dwarf_tag_name (abbrev->die_tag));
6906
6907       if (abbrev->die_child != NULL)
6908         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6909       else
6910         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6911
6912       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6913            ix++)
6914         {
6915           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6916                                        dwarf_attr_name (a_attr->dw_attr));
6917           output_value_format (a_attr);
6918         }
6919
6920       dw2_asm_output_data (1, 0, NULL);
6921       dw2_asm_output_data (1, 0, NULL);
6922     }
6923
6924   /* Terminate the table.  */
6925   dw2_asm_output_data (1, 0, NULL);
6926 }
6927
6928 /* Output a symbol we can use to refer to this DIE from another CU.  */
6929
6930 static inline void
6931 output_die_symbol (dw_die_ref die)
6932 {
6933   char *sym = die->die_symbol;
6934
6935   if (sym == 0)
6936     return;
6937
6938   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6939     /* We make these global, not weak; if the target doesn't support
6940        .linkonce, it doesn't support combining the sections, so debugging
6941        will break.  */
6942     targetm.asm_out.globalize_label (asm_out_file, sym);
6943
6944   ASM_OUTPUT_LABEL (asm_out_file, sym);
6945 }
6946
6947 /* Return a new location list, given the begin and end range, and the
6948    expression. gensym tells us whether to generate a new internal symbol for
6949    this location list node, which is done for the head of the list only.  */
6950
6951 static inline dw_loc_list_ref
6952 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6953               const char *section, unsigned int gensym)
6954 {
6955   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6956
6957   retlist->begin = begin;
6958   retlist->end = end;
6959   retlist->expr = expr;
6960   retlist->section = section;
6961   if (gensym)
6962     retlist->ll_symbol = gen_internal_sym ("LLST");
6963
6964   return retlist;
6965 }
6966
6967 /* Add a location description expression to a location list.  */
6968
6969 static inline void
6970 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6971                            const char *begin, const char *end,
6972                            const char *section)
6973 {
6974   dw_loc_list_ref *d;
6975
6976   /* Find the end of the chain.  */
6977   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6978     ;
6979
6980   /* Add a new location list node to the list.  */
6981   *d = new_loc_list (descr, begin, end, section, 0);
6982 }
6983
6984 static void
6985 dwarf2out_switch_text_section (void)
6986 {
6987   dw_fde_ref fde;
6988
6989   gcc_assert (cfun);
6990
6991   fde = &fde_table[fde_table_in_use - 1];
6992   fde->dw_fde_switched_sections = true;
6993   fde->dw_fde_hot_section_label = cfun->hot_section_label;
6994   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6995   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6996   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6997   have_multiple_function_sections = true;
6998
6999   /* Reset the current label on switching text sections, so that we
7000      don't attempt to advance_loc4 between labels in different sections.  */
7001   fde->dw_fde_current_label = NULL;
7002 }
7003
7004 /* Output the location list given to us.  */
7005
7006 static void
7007 output_loc_list (dw_loc_list_ref list_head)
7008 {
7009   dw_loc_list_ref curr = list_head;
7010
7011   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7012
7013   /* Walk the location list, and output each range + expression.  */
7014   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7015     {
7016       unsigned long size;
7017       if (!have_multiple_function_sections)
7018         {
7019           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7020                                 "Location list begin address (%s)",
7021                                 list_head->ll_symbol);
7022           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7023                                 "Location list end address (%s)",
7024                                 list_head->ll_symbol);
7025         }
7026       else
7027         {
7028           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7029                                "Location list begin address (%s)",
7030                                list_head->ll_symbol);
7031           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7032                                "Location list end address (%s)",
7033                                list_head->ll_symbol);
7034         }
7035       size = size_of_locs (curr->expr);
7036
7037       /* Output the block length for this list of location operations.  */
7038       gcc_assert (size <= 0xffff);
7039       dw2_asm_output_data (2, size, "%s", "Location expression size");
7040
7041       output_loc_sequence (curr->expr);
7042     }
7043
7044   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7045                        "Location list terminator begin (%s)",
7046                        list_head->ll_symbol);
7047   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7048                        "Location list terminator end (%s)",
7049                        list_head->ll_symbol);
7050 }
7051
7052 /* Output the DIE and its attributes.  Called recursively to generate
7053    the definitions of each child DIE.  */
7054
7055 static void
7056 output_die (dw_die_ref die)
7057 {
7058   dw_attr_ref a;
7059   dw_die_ref c;
7060   unsigned long size;
7061   unsigned ix;
7062
7063   /* If someone in another CU might refer to us, set up a symbol for
7064      them to point to.  */
7065   if (die->die_symbol)
7066     output_die_symbol (die);
7067
7068   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7069                                (unsigned long)die->die_offset,
7070                                dwarf_tag_name (die->die_tag));
7071
7072   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7073     {
7074       const char *name = dwarf_attr_name (a->dw_attr);
7075
7076       switch (AT_class (a))
7077         {
7078         case dw_val_class_addr:
7079           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7080           break;
7081
7082         case dw_val_class_offset:
7083           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7084                                "%s", name);
7085           break;
7086
7087         case dw_val_class_range_list:
7088           {
7089             char *p = strchr (ranges_section_label, '\0');
7090
7091             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7092                      a->dw_attr_val.v.val_offset);
7093             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7094                                    debug_ranges_section, "%s", name);
7095             *p = '\0';
7096           }
7097           break;
7098
7099         case dw_val_class_loc:
7100           size = size_of_locs (AT_loc (a));
7101
7102           /* Output the block length for this list of location operations.  */
7103           dw2_asm_output_data (constant_size (size), size, "%s", name);
7104
7105           output_loc_sequence (AT_loc (a));
7106           break;
7107
7108         case dw_val_class_const:
7109           /* ??? It would be slightly more efficient to use a scheme like is
7110              used for unsigned constants below, but gdb 4.x does not sign
7111              extend.  Gdb 5.x does sign extend.  */
7112           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7113           break;
7114
7115         case dw_val_class_unsigned_const:
7116           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7117                                AT_unsigned (a), "%s", name);
7118           break;
7119
7120         case dw_val_class_long_long:
7121           {
7122             unsigned HOST_WIDE_INT first, second;
7123
7124             dw2_asm_output_data (1,
7125                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7126                                  "%s", name);
7127
7128             if (WORDS_BIG_ENDIAN)
7129               {
7130                 first = a->dw_attr_val.v.val_long_long.hi;
7131                 second = a->dw_attr_val.v.val_long_long.low;
7132               }
7133             else
7134               {
7135                 first = a->dw_attr_val.v.val_long_long.low;
7136                 second = a->dw_attr_val.v.val_long_long.hi;
7137               }
7138
7139             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7140                                  first, "long long constant");
7141             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7142                                  second, NULL);
7143           }
7144           break;
7145
7146         case dw_val_class_vec:
7147           {
7148             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7149             unsigned int len = a->dw_attr_val.v.val_vec.length;
7150             unsigned int i;
7151             unsigned char *p;
7152
7153             dw2_asm_output_data (1, len * elt_size, "%s", name);
7154             if (elt_size > sizeof (HOST_WIDE_INT))
7155               {
7156                 elt_size /= 2;
7157                 len *= 2;
7158               }
7159             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7160                  i < len;
7161                  i++, p += elt_size)
7162               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7163                                    "fp or vector constant word %u", i);
7164             break;
7165           }
7166
7167         case dw_val_class_flag:
7168           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7169           break;
7170
7171         case dw_val_class_loc_list:
7172           {
7173             char *sym = AT_loc_list (a)->ll_symbol;
7174
7175             gcc_assert (sym);
7176             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7177                                    "%s", name);
7178           }
7179           break;
7180
7181         case dw_val_class_die_ref:
7182           if (AT_ref_external (a))
7183             {
7184               char *sym = AT_ref (a)->die_symbol;
7185
7186               gcc_assert (sym);
7187               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7188                                      "%s", name);
7189             }
7190           else
7191             {
7192               gcc_assert (AT_ref (a)->die_offset);
7193               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7194                                    "%s", name);
7195             }
7196           break;
7197
7198         case dw_val_class_fde_ref:
7199           {
7200             char l1[20];
7201
7202             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7203                                          a->dw_attr_val.v.val_fde_index * 2);
7204             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7205                                    "%s", name);
7206           }
7207           break;
7208
7209         case dw_val_class_lbl_id:
7210           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7211           break;
7212
7213         case dw_val_class_lineptr:
7214           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7215                                  debug_line_section, "%s", name);
7216           break;
7217
7218         case dw_val_class_macptr:
7219           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7220                                  debug_macinfo_section, "%s", name);
7221           break;
7222
7223         case dw_val_class_str:
7224           if (AT_string_form (a) == DW_FORM_strp)
7225             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7226                                    a->dw_attr_val.v.val_str->label,
7227                                    debug_str_section,
7228                                    "%s: \"%s\"", name, AT_string (a));
7229           else
7230             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7231           break;
7232
7233         case dw_val_class_file:
7234           {
7235             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7236             
7237             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7238                                  a->dw_attr_val.v.val_file->filename);
7239             break;
7240           }
7241
7242         default:
7243           gcc_unreachable ();
7244         }
7245     }
7246
7247   FOR_EACH_CHILD (die, c, output_die (c));
7248
7249   /* Add null byte to terminate sibling list.  */
7250   if (die->die_child != NULL)
7251     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7252                          (unsigned long) die->die_offset);
7253 }
7254
7255 /* Output the compilation unit that appears at the beginning of the
7256    .debug_info section, and precedes the DIE descriptions.  */
7257
7258 static void
7259 output_compilation_unit_header (void)
7260 {
7261   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7262     dw2_asm_output_data (4, 0xffffffff,
7263       "Initial length escape value indicating 64-bit DWARF extension");
7264   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7265                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7266                        "Length of Compilation Unit Info");
7267   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7268   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7269                          debug_abbrev_section,
7270                          "Offset Into Abbrev. Section");
7271   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7272 }
7273
7274 /* Output the compilation unit DIE and its children.  */
7275
7276 static void
7277 output_comp_unit (dw_die_ref die, int output_if_empty)
7278 {
7279   const char *secname;
7280   char *oldsym, *tmp;
7281
7282   /* Unless we are outputting main CU, we may throw away empty ones.  */
7283   if (!output_if_empty && die->die_child == NULL)
7284     return;
7285
7286   /* Even if there are no children of this DIE, we must output the information
7287      about the compilation unit.  Otherwise, on an empty translation unit, we
7288      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7289      will then complain when examining the file.  First mark all the DIEs in
7290      this CU so we know which get local refs.  */
7291   mark_dies (die);
7292
7293   build_abbrev_table (die);
7294
7295   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7296   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7297   calc_die_sizes (die);
7298
7299   oldsym = die->die_symbol;
7300   if (oldsym)
7301     {
7302       tmp = alloca (strlen (oldsym) + 24);
7303
7304       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7305       secname = tmp;
7306       die->die_symbol = NULL;
7307       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7308     }
7309   else
7310     switch_to_section (debug_info_section);
7311
7312   /* Output debugging information.  */
7313   output_compilation_unit_header ();
7314   output_die (die);
7315
7316   /* Leave the marks on the main CU, so we can check them in
7317      output_pubnames.  */
7318   if (oldsym)
7319     {
7320       unmark_dies (die);
7321       die->die_symbol = oldsym;
7322     }
7323 }
7324
7325 /* Return the DWARF2/3 pubname associated with a decl.  */
7326
7327 static const char *
7328 dwarf2_name (tree decl, int scope)
7329 {
7330   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7331 }
7332
7333 /* Add a new entry to .debug_pubnames if appropriate.  */
7334
7335 static void
7336 add_pubname (tree decl, dw_die_ref die)
7337 {
7338   pubname_entry e;
7339
7340   if (! TREE_PUBLIC (decl))
7341     return;
7342
7343   e.die = die;
7344   e.name = xstrdup (dwarf2_name (decl, 1));
7345   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7346 }
7347
7348 /* Add a new entry to .debug_pubtypes if appropriate.  */
7349
7350 static void
7351 add_pubtype (tree decl, dw_die_ref die)
7352 {
7353   pubname_entry e;
7354
7355   e.name = NULL;
7356   if ((TREE_PUBLIC (decl)
7357        || die->die_parent == comp_unit_die)
7358       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7359     {
7360       e.die = die;
7361       if (TYPE_P (decl))
7362         {
7363           if (TYPE_NAME (decl))
7364             {
7365               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7366                 e.name = xstrdup ((const char *) IDENTIFIER_POINTER 
7367                                                               (TYPE_NAME (decl)));
7368               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7369                        && DECL_NAME (TYPE_NAME (decl)))
7370                 e.name = xstrdup ((const char *) IDENTIFIER_POINTER 
7371                                                   (DECL_NAME (TYPE_NAME (decl))));
7372              else
7373                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7374             }
7375         }
7376       else 
7377         e.name = xstrdup (dwarf2_name (decl, 1));
7378
7379       /* If we don't have a name for the type, there's no point in adding
7380          it to the table.  */
7381       if (e.name && e.name[0] != '\0')
7382         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7383     }
7384 }
7385
7386 /* Output the public names table used to speed up access to externally
7387    visible names; or the public types table used to find type definitions.  */
7388
7389 static void
7390 output_pubnames (VEC (pubname_entry, gc) * names)
7391 {
7392   unsigned i;
7393   unsigned long pubnames_length = size_of_pubnames (names);
7394   pubname_ref pub;
7395
7396   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7397     dw2_asm_output_data (4, 0xffffffff,
7398       "Initial length escape value indicating 64-bit DWARF extension");
7399   if (names == pubname_table)
7400     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7401                          "Length of Public Names Info");
7402   else
7403     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7404                          "Length of Public Type Names Info");
7405   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7406   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7407                          debug_info_section,
7408                          "Offset of Compilation Unit Info");
7409   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7410                        "Compilation Unit Length");
7411
7412   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7413     {
7414       /* We shouldn't see pubnames for DIEs outside of the main CU.  */      
7415       if (names == pubname_table)
7416         gcc_assert (pub->die->die_mark);
7417
7418       if (names != pubtype_table
7419           || pub->die->die_offset != 0
7420           || !flag_eliminate_unused_debug_types)
7421         {
7422           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7423                                "DIE offset");
7424
7425           dw2_asm_output_nstring (pub->name, -1, "external name");
7426         }
7427     }
7428
7429   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7430 }
7431
7432 /* Add a new entry to .debug_aranges if appropriate.  */
7433
7434 static void
7435 add_arange (tree decl, dw_die_ref die)
7436 {
7437   if (! DECL_SECTION_NAME (decl))
7438     return;
7439
7440   if (arange_table_in_use == arange_table_allocated)
7441     {
7442       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7443       arange_table = ggc_realloc (arange_table,
7444                                   (arange_table_allocated
7445                                    * sizeof (dw_die_ref)));
7446       memset (arange_table + arange_table_in_use, 0,
7447               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7448     }
7449
7450   arange_table[arange_table_in_use++] = die;
7451 }
7452
7453 /* Output the information that goes into the .debug_aranges table.
7454    Namely, define the beginning and ending address range of the
7455    text section generated for this compilation unit.  */
7456
7457 static void
7458 output_aranges (void)
7459 {
7460   unsigned i;
7461   unsigned long aranges_length = size_of_aranges ();
7462
7463   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7464     dw2_asm_output_data (4, 0xffffffff,
7465       "Initial length escape value indicating 64-bit DWARF extension");
7466   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7467                        "Length of Address Ranges Info");
7468   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7469   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7470                          debug_info_section,
7471                          "Offset of Compilation Unit Info");
7472   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7473   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7474
7475   /* We need to align to twice the pointer size here.  */
7476   if (DWARF_ARANGES_PAD_SIZE)
7477     {
7478       /* Pad using a 2 byte words so that padding is correct for any
7479          pointer size.  */
7480       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7481                            2 * DWARF2_ADDR_SIZE);
7482       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7483         dw2_asm_output_data (2, 0, NULL);
7484     }
7485
7486   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7487   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7488                         text_section_label, "Length");
7489   if (flag_reorder_blocks_and_partition)
7490     {
7491       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label, 
7492                            "Address");
7493       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7494                             cold_text_section_label, "Length");
7495     }
7496
7497   for (i = 0; i < arange_table_in_use; i++)
7498     {
7499       dw_die_ref die = arange_table[i];
7500
7501       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7502       gcc_assert (die->die_mark);
7503
7504       if (die->die_tag == DW_TAG_subprogram)
7505         {
7506           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7507                                "Address");
7508           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7509                                 get_AT_low_pc (die), "Length");
7510         }
7511       else
7512         {
7513           /* A static variable; extract the symbol from DW_AT_location.
7514              Note that this code isn't currently hit, as we only emit
7515              aranges for functions (jason 9/23/99).  */
7516           dw_attr_ref a = get_AT (die, DW_AT_location);
7517           dw_loc_descr_ref loc;
7518
7519           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7520
7521           loc = AT_loc (a);
7522           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7523
7524           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7525                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7526           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7527                                get_AT_unsigned (die, DW_AT_byte_size),
7528                                "Length");
7529         }
7530     }
7531
7532   /* Output the terminator words.  */
7533   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7534   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7535 }
7536
7537 /* Add a new entry to .debug_ranges.  Return the offset at which it
7538    was placed.  */
7539
7540 static unsigned int
7541 add_ranges (tree block)
7542 {
7543   unsigned int in_use = ranges_table_in_use;
7544
7545   if (in_use == ranges_table_allocated)
7546     {
7547       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7548       ranges_table
7549         = ggc_realloc (ranges_table, (ranges_table_allocated
7550                                       * sizeof (struct dw_ranges_struct)));
7551       memset (ranges_table + ranges_table_in_use, 0,
7552               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7553     }
7554
7555   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7556   ranges_table_in_use = in_use + 1;
7557
7558   return in_use * 2 * DWARF2_ADDR_SIZE;
7559 }
7560
7561 static void
7562 output_ranges (void)
7563 {
7564   unsigned i;
7565   static const char *const start_fmt = "Offset 0x%x";
7566   const char *fmt = start_fmt;
7567
7568   for (i = 0; i < ranges_table_in_use; i++)
7569     {
7570       int block_num = ranges_table[i].block_num;
7571
7572       if (block_num)
7573         {
7574           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7575           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7576
7577           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7578           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7579
7580           /* If all code is in the text section, then the compilation
7581              unit base address defaults to DW_AT_low_pc, which is the
7582              base of the text section.  */
7583           if (!have_multiple_function_sections)
7584             {
7585               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7586                                     text_section_label,
7587                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7588               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7589                                     text_section_label, NULL);
7590             }
7591
7592           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7593              compilation unit base address to zero, which allows us to
7594              use absolute addresses, and not worry about whether the
7595              target supports cross-section arithmetic.  */
7596           else
7597             {
7598               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7599                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7600               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7601             }
7602
7603           fmt = NULL;
7604         }
7605       else
7606         {
7607           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7608           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7609           fmt = start_fmt;
7610         }
7611     }
7612 }
7613
7614 /* Data structure containing information about input files.  */
7615 struct file_info
7616 {
7617   const char *path;     /* Complete file name.  */
7618   const char *fname;    /* File name part.  */
7619   int length;           /* Length of entire string.  */
7620   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7621   int dir_idx;          /* Index in directory table.  */
7622 };
7623
7624 /* Data structure containing information about directories with source
7625    files.  */
7626 struct dir_info
7627 {
7628   const char *path;     /* Path including directory name.  */
7629   int length;           /* Path length.  */
7630   int prefix;           /* Index of directory entry which is a prefix.  */
7631   int count;            /* Number of files in this directory.  */
7632   int dir_idx;          /* Index of directory used as base.  */
7633 };
7634
7635 /* Callback function for file_info comparison.  We sort by looking at
7636    the directories in the path.  */
7637
7638 static int
7639 file_info_cmp (const void *p1, const void *p2)
7640 {
7641   const struct file_info *s1 = p1;
7642   const struct file_info *s2 = p2;
7643   unsigned char *cp1;
7644   unsigned char *cp2;
7645
7646   /* Take care of file names without directories.  We need to make sure that
7647      we return consistent values to qsort since some will get confused if
7648      we return the same value when identical operands are passed in opposite
7649      orders.  So if neither has a directory, return 0 and otherwise return
7650      1 or -1 depending on which one has the directory.  */
7651   if ((s1->path == s1->fname || s2->path == s2->fname))
7652     return (s2->path == s2->fname) - (s1->path == s1->fname);
7653
7654   cp1 = (unsigned char *) s1->path;
7655   cp2 = (unsigned char *) s2->path;
7656
7657   while (1)
7658     {
7659       ++cp1;
7660       ++cp2;
7661       /* Reached the end of the first path?  If so, handle like above.  */
7662       if ((cp1 == (unsigned char *) s1->fname)
7663           || (cp2 == (unsigned char *) s2->fname))
7664         return ((cp2 == (unsigned char *) s2->fname)
7665                 - (cp1 == (unsigned char *) s1->fname));
7666
7667       /* Character of current path component the same?  */
7668       else if (*cp1 != *cp2)
7669         return *cp1 - *cp2;
7670     }
7671 }
7672
7673 struct file_name_acquire_data 
7674 {
7675   struct file_info *files;
7676   int used_files;
7677   int max_files;
7678 };
7679
7680 /* Traversal function for the hash table.  */
7681
7682 static int
7683 file_name_acquire (void ** slot, void *data)
7684 {
7685   struct file_name_acquire_data *fnad = data;
7686   struct dwarf_file_data *d = *slot;
7687   struct file_info *fi;
7688   const char *f;
7689
7690   gcc_assert (fnad->max_files >= d->emitted_number);
7691
7692   if (! d->emitted_number)
7693     return 1;
7694
7695   gcc_assert (fnad->max_files != fnad->used_files);
7696
7697   fi = fnad->files + fnad->used_files++;
7698
7699   /* Skip all leading "./".  */
7700   f = d->filename;
7701   while (f[0] == '.' && f[1] == '/')
7702     f += 2;
7703   
7704   /* Create a new array entry.  */
7705   fi->path = f;
7706   fi->length = strlen (f);
7707   fi->file_idx = d;
7708   
7709   /* Search for the file name part.  */
7710   f = strrchr (f, '/');
7711   fi->fname = f == NULL ? fi->path : f + 1;
7712   return 1;
7713 }
7714
7715 /* Output the directory table and the file name table.  We try to minimize
7716    the total amount of memory needed.  A heuristic is used to avoid large
7717    slowdowns with many input files.  */
7718
7719 static void
7720 output_file_names (void)
7721 {
7722   struct file_name_acquire_data fnad;
7723   int numfiles;
7724   struct file_info *files;
7725   struct dir_info *dirs;
7726   int *saved;
7727   int *savehere;
7728   int *backmap;
7729   int ndirs;
7730   int idx_offset;
7731   int i;
7732   int idx;
7733
7734   if (!last_emitted_file)
7735     {
7736       dw2_asm_output_data (1, 0, "End directory table");
7737       dw2_asm_output_data (1, 0, "End file name table");
7738       return;
7739     }
7740
7741   numfiles = last_emitted_file->emitted_number;
7742
7743   /* Allocate the various arrays we need.  */
7744   files = alloca (numfiles * sizeof (struct file_info));
7745   dirs = alloca (numfiles * sizeof (struct dir_info));
7746
7747   fnad.files = files;
7748   fnad.used_files = 0;
7749   fnad.max_files = numfiles;
7750   htab_traverse (file_table, file_name_acquire, &fnad);
7751   gcc_assert (fnad.used_files == fnad.max_files);
7752
7753   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7754
7755   /* Find all the different directories used.  */
7756   dirs[0].path = files[0].path;
7757   dirs[0].length = files[0].fname - files[0].path;
7758   dirs[0].prefix = -1;
7759   dirs[0].count = 1;
7760   dirs[0].dir_idx = 0;
7761   files[0].dir_idx = 0;
7762   ndirs = 1;
7763
7764   for (i = 1; i < numfiles; i++)
7765     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7766         && memcmp (dirs[ndirs - 1].path, files[i].path,
7767                    dirs[ndirs - 1].length) == 0)
7768       {
7769         /* Same directory as last entry.  */
7770         files[i].dir_idx = ndirs - 1;
7771         ++dirs[ndirs - 1].count;
7772       }
7773     else
7774       {
7775         int j;
7776
7777         /* This is a new directory.  */
7778         dirs[ndirs].path = files[i].path;
7779         dirs[ndirs].length = files[i].fname - files[i].path;
7780         dirs[ndirs].count = 1;
7781         dirs[ndirs].dir_idx = ndirs;
7782         files[i].dir_idx = ndirs;
7783
7784         /* Search for a prefix.  */
7785         dirs[ndirs].prefix = -1;
7786         for (j = 0; j < ndirs; j++)
7787           if (dirs[j].length < dirs[ndirs].length
7788               && dirs[j].length > 1
7789               && (dirs[ndirs].prefix == -1
7790                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7791               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7792             dirs[ndirs].prefix = j;
7793
7794         ++ndirs;
7795       }
7796
7797   /* Now to the actual work.  We have to find a subset of the directories which
7798      allow expressing the file name using references to the directory table
7799      with the least amount of characters.  We do not do an exhaustive search
7800      where we would have to check out every combination of every single
7801      possible prefix.  Instead we use a heuristic which provides nearly optimal
7802      results in most cases and never is much off.  */
7803   saved = alloca (ndirs * sizeof (int));
7804   savehere = alloca (ndirs * sizeof (int));
7805
7806   memset (saved, '\0', ndirs * sizeof (saved[0]));
7807   for (i = 0; i < ndirs; i++)
7808     {
7809       int j;
7810       int total;
7811
7812       /* We can always save some space for the current directory.  But this
7813          does not mean it will be enough to justify adding the directory.  */
7814       savehere[i] = dirs[i].length;
7815       total = (savehere[i] - saved[i]) * dirs[i].count;
7816
7817       for (j = i + 1; j < ndirs; j++)
7818         {
7819           savehere[j] = 0;
7820           if (saved[j] < dirs[i].length)
7821             {
7822               /* Determine whether the dirs[i] path is a prefix of the
7823                  dirs[j] path.  */
7824               int k;
7825
7826               k = dirs[j].prefix;
7827               while (k != -1 && k != (int) i)
7828                 k = dirs[k].prefix;
7829
7830               if (k == (int) i)
7831                 {
7832                   /* Yes it is.  We can possibly save some memory by
7833                      writing the filenames in dirs[j] relative to
7834                      dirs[i].  */
7835                   savehere[j] = dirs[i].length;
7836                   total += (savehere[j] - saved[j]) * dirs[j].count;
7837                 }
7838             }
7839         }
7840
7841       /* Check whether we can save enough to justify adding the dirs[i]
7842          directory.  */
7843       if (total > dirs[i].length + 1)
7844         {
7845           /* It's worthwhile adding.  */
7846           for (j = i; j < ndirs; j++)
7847             if (savehere[j] > 0)
7848               {
7849                 /* Remember how much we saved for this directory so far.  */
7850                 saved[j] = savehere[j];
7851
7852                 /* Remember the prefix directory.  */
7853                 dirs[j].dir_idx = i;
7854               }
7855         }
7856     }
7857
7858   /* Emit the directory name table.  */
7859   idx = 1;
7860   idx_offset = dirs[0].length > 0 ? 1 : 0;
7861   for (i = 1 - idx_offset; i < ndirs; i++)
7862     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7863                             "Directory Entry: 0x%x", i + idx_offset);
7864
7865   dw2_asm_output_data (1, 0, "End directory table");
7866
7867   /* We have to emit them in the order of emitted_number since that's
7868      used in the debug info generation.  To do this efficiently we
7869      generate a back-mapping of the indices first.  */
7870   backmap = alloca (numfiles * sizeof (int));
7871   for (i = 0; i < numfiles; i++)
7872     backmap[files[i].file_idx->emitted_number - 1] = i;
7873
7874   /* Now write all the file names.  */
7875   for (i = 0; i < numfiles; i++)
7876     {
7877       int file_idx = backmap[i];
7878       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7879
7880       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7881                               "File Entry: 0x%x", (unsigned) i + 1);
7882
7883       /* Include directory index.  */
7884       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
7885
7886       /* Modification time.  */
7887       dw2_asm_output_data_uleb128 (0, NULL);
7888
7889       /* File length in bytes.  */
7890       dw2_asm_output_data_uleb128 (0, NULL);
7891     }
7892
7893   dw2_asm_output_data (1, 0, "End file name table");
7894 }
7895
7896
7897 /* Output the source line number correspondence information.  This
7898    information goes into the .debug_line section.  */
7899
7900 static void
7901 output_line_info (void)
7902 {
7903   char l1[20], l2[20], p1[20], p2[20];
7904   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7905   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7906   unsigned opc;
7907   unsigned n_op_args;
7908   unsigned long lt_index;
7909   unsigned long current_line;
7910   long line_offset;
7911   long line_delta;
7912   unsigned long current_file;
7913   unsigned long function;
7914
7915   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7916   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7917   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7918   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7919
7920   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7921     dw2_asm_output_data (4, 0xffffffff,
7922       "Initial length escape value indicating 64-bit DWARF extension");
7923   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7924                         "Length of Source Line Info");
7925   ASM_OUTPUT_LABEL (asm_out_file, l1);
7926
7927   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7928   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7929   ASM_OUTPUT_LABEL (asm_out_file, p1);
7930
7931   /* Define the architecture-dependent minimum instruction length (in
7932    bytes).  In this implementation of DWARF, this field is used for
7933    information purposes only.  Since GCC generates assembly language,
7934    we have no a priori knowledge of how many instruction bytes are
7935    generated for each source line, and therefore can use only the
7936    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7937    commands.  Accordingly, we fix this as `1', which is "correct
7938    enough" for all architectures, and don't let the target override.  */
7939   dw2_asm_output_data (1, 1,
7940                        "Minimum Instruction Length");
7941
7942   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7943                        "Default is_stmt_start flag");
7944   dw2_asm_output_data (1, DWARF_LINE_BASE,
7945                        "Line Base Value (Special Opcodes)");
7946   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7947                        "Line Range Value (Special Opcodes)");
7948   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7949                        "Special Opcode Base");
7950
7951   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7952     {
7953       switch (opc)
7954         {
7955         case DW_LNS_advance_pc:
7956         case DW_LNS_advance_line:
7957         case DW_LNS_set_file:
7958         case DW_LNS_set_column:
7959         case DW_LNS_fixed_advance_pc:
7960           n_op_args = 1;
7961           break;
7962         default:
7963           n_op_args = 0;
7964           break;
7965         }
7966
7967       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7968                            opc, n_op_args);
7969     }
7970
7971   /* Write out the information about the files we use.  */
7972   output_file_names ();
7973   ASM_OUTPUT_LABEL (asm_out_file, p2);
7974
7975   /* We used to set the address register to the first location in the text
7976      section here, but that didn't accomplish anything since we already
7977      have a line note for the opening brace of the first function.  */
7978
7979   /* Generate the line number to PC correspondence table, encoded as
7980      a series of state machine operations.  */
7981   current_file = 1;
7982   current_line = 1;
7983
7984   if (cfun && in_cold_section_p)
7985     strcpy (prev_line_label, cfun->cold_section_label);
7986   else
7987     strcpy (prev_line_label, text_section_label);
7988   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7989     {
7990       dw_line_info_ref line_info = &line_info_table[lt_index];
7991
7992 #if 0
7993       /* Disable this optimization for now; GDB wants to see two line notes
7994          at the beginning of a function so it can find the end of the
7995          prologue.  */
7996
7997       /* Don't emit anything for redundant notes.  Just updating the
7998          address doesn't accomplish anything, because we already assume
7999          that anything after the last address is this line.  */
8000       if (line_info->dw_line_num == current_line
8001           && line_info->dw_file_num == current_file)
8002         continue;
8003 #endif
8004
8005       /* Emit debug info for the address of the current line.
8006
8007          Unfortunately, we have little choice here currently, and must always
8008          use the most general form.  GCC does not know the address delta
8009          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8010          attributes which will give an upper bound on the address range.  We
8011          could perhaps use length attributes to determine when it is safe to
8012          use DW_LNS_fixed_advance_pc.  */
8013
8014       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8015       if (0)
8016         {
8017           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8018           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8019                                "DW_LNS_fixed_advance_pc");
8020           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8021         }
8022       else
8023         {
8024           /* This can handle any delta.  This takes
8025              4+DWARF2_ADDR_SIZE bytes.  */
8026           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8027           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8028           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8029           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8030         }
8031
8032       strcpy (prev_line_label, line_label);
8033
8034       /* Emit debug info for the source file of the current line, if
8035          different from the previous line.  */
8036       if (line_info->dw_file_num != current_file)
8037         {
8038           current_file = line_info->dw_file_num;
8039           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8040           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8041         }
8042
8043       /* Emit debug info for the current line number, choosing the encoding
8044          that uses the least amount of space.  */
8045       if (line_info->dw_line_num != current_line)
8046         {
8047           line_offset = line_info->dw_line_num - current_line;
8048           line_delta = line_offset - DWARF_LINE_BASE;
8049           current_line = line_info->dw_line_num;
8050           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8051             /* This can handle deltas from -10 to 234, using the current
8052                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8053                takes 1 byte.  */
8054             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8055                                  "line %lu", current_line);
8056           else
8057             {
8058               /* This can handle any delta.  This takes at least 4 bytes,
8059                  depending on the value being encoded.  */
8060               dw2_asm_output_data (1, DW_LNS_advance_line,
8061                                    "advance to line %lu", current_line);
8062               dw2_asm_output_data_sleb128 (line_offset, NULL);
8063               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8064             }
8065         }
8066       else
8067         /* We still need to start a new row, so output a copy insn.  */
8068         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8069     }
8070
8071   /* Emit debug info for the address of the end of the function.  */
8072   if (0)
8073     {
8074       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8075                            "DW_LNS_fixed_advance_pc");
8076       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8077     }
8078   else
8079     {
8080       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8081       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8082       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8083       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8084     }
8085
8086   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8087   dw2_asm_output_data_uleb128 (1, NULL);
8088   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8089
8090   function = 0;
8091   current_file = 1;
8092   current_line = 1;
8093   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8094     {
8095       dw_separate_line_info_ref line_info
8096         = &separate_line_info_table[lt_index];
8097
8098 #if 0
8099       /* Don't emit anything for redundant notes.  */
8100       if (line_info->dw_line_num == current_line
8101           && line_info->dw_file_num == current_file
8102           && line_info->function == function)
8103         goto cont;
8104 #endif
8105
8106       /* Emit debug info for the address of the current line.  If this is
8107          a new function, or the first line of a function, then we need
8108          to handle it differently.  */
8109       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8110                                    lt_index);
8111       if (function != line_info->function)
8112         {
8113           function = line_info->function;
8114
8115           /* Set the address register to the first line in the function.  */
8116           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8117           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8118           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8119           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8120         }
8121       else
8122         {
8123           /* ??? See the DW_LNS_advance_pc comment above.  */
8124           if (0)
8125             {
8126               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8127                                    "DW_LNS_fixed_advance_pc");
8128               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8129             }
8130           else
8131             {
8132               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8133               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8134               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8135               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8136             }
8137         }
8138
8139       strcpy (prev_line_label, line_label);
8140
8141       /* Emit debug info for the source file of the current line, if
8142          different from the previous line.  */
8143       if (line_info->dw_file_num != current_file)
8144         {
8145           current_file = line_info->dw_file_num;
8146           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8147           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8148         }
8149
8150       /* Emit debug info for the current line number, choosing the encoding
8151          that uses the least amount of space.  */
8152       if (line_info->dw_line_num != current_line)
8153         {
8154           line_offset = line_info->dw_line_num - current_line;
8155           line_delta = line_offset - DWARF_LINE_BASE;
8156           current_line = line_info->dw_line_num;
8157           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8158             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8159                                  "line %lu", current_line);
8160           else
8161             {
8162               dw2_asm_output_data (1, DW_LNS_advance_line,
8163                                    "advance to line %lu", current_line);
8164               dw2_asm_output_data_sleb128 (line_offset, NULL);
8165               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8166             }
8167         }
8168       else
8169         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8170
8171 #if 0
8172     cont:
8173 #endif
8174
8175       lt_index++;
8176
8177       /* If we're done with a function, end its sequence.  */
8178       if (lt_index == separate_line_info_table_in_use
8179           || separate_line_info_table[lt_index].function != function)
8180         {
8181           current_file = 1;
8182           current_line = 1;
8183
8184           /* Emit debug info for the address of the end of the function.  */
8185           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8186           if (0)
8187             {
8188               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8189                                    "DW_LNS_fixed_advance_pc");
8190               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8191             }
8192           else
8193             {
8194               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8195               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8196               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8197               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8198             }
8199
8200           /* Output the marker for the end of this sequence.  */
8201           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8202           dw2_asm_output_data_uleb128 (1, NULL);
8203           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8204         }
8205     }
8206
8207   /* Output the marker for the end of the line number info.  */
8208   ASM_OUTPUT_LABEL (asm_out_file, l2);
8209 }
8210 \f
8211 /* Given a pointer to a tree node for some base type, return a pointer to
8212    a DIE that describes the given type.
8213
8214    This routine must only be called for GCC type nodes that correspond to
8215    Dwarf base (fundamental) types.  */
8216
8217 static dw_die_ref
8218 base_type_die (tree type)
8219 {
8220   dw_die_ref base_type_result;
8221   enum dwarf_type encoding;
8222
8223   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8224     return 0;
8225
8226   switch (TREE_CODE (type))
8227     {
8228     case INTEGER_TYPE:
8229       if (TYPE_STRING_FLAG (type))
8230         {
8231           if (TYPE_UNSIGNED (type))
8232             encoding = DW_ATE_unsigned_char;
8233           else
8234             encoding = DW_ATE_signed_char;
8235         }
8236       else if (TYPE_UNSIGNED (type))
8237         encoding = DW_ATE_unsigned;
8238       else
8239         encoding = DW_ATE_signed;
8240       break;
8241
8242     case REAL_TYPE:
8243       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8244         encoding = DW_ATE_decimal_float;
8245       else
8246         encoding = DW_ATE_float;
8247       break;
8248
8249       /* Dwarf2 doesn't know anything about complex ints, so use
8250          a user defined type for it.  */
8251     case COMPLEX_TYPE:
8252       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8253         encoding = DW_ATE_complex_float;
8254       else
8255         encoding = DW_ATE_lo_user;
8256       break;
8257
8258     case BOOLEAN_TYPE:
8259       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8260       encoding = DW_ATE_boolean;
8261       break;
8262
8263     default:
8264       /* No other TREE_CODEs are Dwarf fundamental types.  */
8265       gcc_unreachable ();
8266     }
8267
8268   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8269
8270   /* This probably indicates a bug.  */
8271   if (! TYPE_NAME (type))
8272     add_name_attribute (base_type_result, "__unknown__");
8273
8274   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8275                    int_size_in_bytes (type));
8276   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8277
8278   return base_type_result;
8279 }
8280
8281 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8282    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
8283    a given type is generally the same as the given type, except that if the
8284    given type is a pointer or reference type, then the root type of the given
8285    type is the root type of the "basis" type for the pointer or reference
8286    type.  (This definition of the "root" type is recursive.) Also, the root
8287    type of a `const' qualified type or a `volatile' qualified type is the
8288    root type of the given type without the qualifiers.  */
8289
8290 static tree
8291 root_type (tree type)
8292 {
8293   if (TREE_CODE (type) == ERROR_MARK)
8294     return error_mark_node;
8295
8296   switch (TREE_CODE (type))
8297     {
8298     case ERROR_MARK:
8299       return error_mark_node;
8300
8301     case POINTER_TYPE:
8302     case REFERENCE_TYPE:
8303       return type_main_variant (root_type (TREE_TYPE (type)));
8304
8305     default:
8306       return type_main_variant (type);
8307     }
8308 }
8309
8310 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8311    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8312
8313 static inline int
8314 is_base_type (tree type)
8315 {
8316   switch (TREE_CODE (type))
8317     {
8318     case ERROR_MARK:
8319     case VOID_TYPE:
8320     case INTEGER_TYPE:
8321     case REAL_TYPE:
8322     case COMPLEX_TYPE:
8323     case BOOLEAN_TYPE:
8324       return 1;
8325
8326     case ARRAY_TYPE:
8327     case RECORD_TYPE:
8328     case UNION_TYPE:
8329     case QUAL_UNION_TYPE:
8330     case ENUMERAL_TYPE:
8331     case FUNCTION_TYPE:
8332     case METHOD_TYPE:
8333     case POINTER_TYPE:
8334     case REFERENCE_TYPE:
8335     case OFFSET_TYPE:
8336     case LANG_TYPE:
8337     case VECTOR_TYPE:
8338       return 0;
8339
8340     default:
8341       gcc_unreachable ();
8342     }
8343
8344   return 0;
8345 }
8346
8347 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8348    node, return the size in bits for the type if it is a constant, or else
8349    return the alignment for the type if the type's size is not constant, or
8350    else return BITS_PER_WORD if the type actually turns out to be an
8351    ERROR_MARK node.  */
8352
8353 static inline unsigned HOST_WIDE_INT
8354 simple_type_size_in_bits (tree type)
8355 {
8356   if (TREE_CODE (type) == ERROR_MARK)
8357     return BITS_PER_WORD;
8358   else if (TYPE_SIZE (type) == NULL_TREE)
8359     return 0;
8360   else if (host_integerp (TYPE_SIZE (type), 1))
8361     return tree_low_cst (TYPE_SIZE (type), 1);
8362   else
8363     return TYPE_ALIGN (type);
8364 }
8365
8366 /* Return true if the debug information for the given type should be
8367    emitted as a subrange type.  */
8368
8369 static inline bool
8370 is_subrange_type (tree type)
8371 {
8372   tree subtype = TREE_TYPE (type);
8373
8374   /* Subrange types are identified by the fact that they are integer
8375      types, and that they have a subtype which is either an integer type
8376      or an enumeral type.  */
8377
8378   if (TREE_CODE (type) != INTEGER_TYPE
8379       || subtype == NULL_TREE)
8380     return false;
8381
8382   if (TREE_CODE (subtype) != INTEGER_TYPE
8383       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8384     return false;
8385
8386   if (TREE_CODE (type) == TREE_CODE (subtype)
8387       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8388       && TYPE_MIN_VALUE (type) != NULL
8389       && TYPE_MIN_VALUE (subtype) != NULL
8390       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8391       && TYPE_MAX_VALUE (type) != NULL
8392       && TYPE_MAX_VALUE (subtype) != NULL
8393       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8394     {
8395       /* The type and its subtype have the same representation.  If in
8396          addition the two types also have the same name, then the given
8397          type is not a subrange type, but rather a plain base type.  */
8398       /* FIXME: brobecker/2004-03-22:
8399          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8400          therefore be sufficient to check the TYPE_SIZE node pointers
8401          rather than checking the actual size.  Unfortunately, we have
8402          found some cases, such as in the Ada "integer" type, where
8403          this is not the case.  Until this problem is solved, we need to
8404          keep checking the actual size.  */
8405       tree type_name = TYPE_NAME (type);
8406       tree subtype_name = TYPE_NAME (subtype);
8407
8408       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8409         type_name = DECL_NAME (type_name);
8410
8411       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8412         subtype_name = DECL_NAME (subtype_name);
8413
8414       if (type_name == subtype_name)
8415         return false;
8416     }
8417
8418   return true;
8419 }
8420
8421 /*  Given a pointer to a tree node for a subrange type, return a pointer
8422     to a DIE that describes the given type.  */
8423
8424 static dw_die_ref
8425 subrange_type_die (tree type, dw_die_ref context_die)
8426 {
8427   dw_die_ref subrange_die;
8428   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8429
8430   if (context_die == NULL)
8431     context_die = comp_unit_die;
8432
8433   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8434
8435   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8436     {
8437       /* The size of the subrange type and its base type do not match,
8438          so we need to generate a size attribute for the subrange type.  */
8439       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8440     }
8441
8442   if (TYPE_MIN_VALUE (type) != NULL)
8443     add_bound_info (subrange_die, DW_AT_lower_bound,
8444                     TYPE_MIN_VALUE (type));
8445   if (TYPE_MAX_VALUE (type) != NULL)
8446     add_bound_info (subrange_die, DW_AT_upper_bound,
8447                     TYPE_MAX_VALUE (type));
8448
8449   return subrange_die;
8450 }
8451
8452 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8453    entry that chains various modifiers in front of the given type.  */
8454
8455 static dw_die_ref
8456 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8457                    dw_die_ref context_die)
8458 {
8459   enum tree_code code = TREE_CODE (type);
8460   dw_die_ref mod_type_die;
8461   dw_die_ref sub_die = NULL;
8462   tree item_type = NULL;
8463   tree qualified_type;
8464   tree name;
8465
8466   if (code == ERROR_MARK)
8467     return NULL;
8468
8469   /* See if we already have the appropriately qualified variant of
8470      this type.  */
8471   qualified_type
8472     = get_qualified_type (type,
8473                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8474                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8475   
8476   /* If we do, then we can just use its DIE, if it exists.  */
8477   if (qualified_type)
8478     {
8479       mod_type_die = lookup_type_die (qualified_type);
8480       if (mod_type_die)
8481         return mod_type_die;
8482     }
8483   
8484   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8485   
8486   /* Handle C typedef types.  */
8487   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8488     {
8489       tree dtype = TREE_TYPE (name);
8490       
8491       if (qualified_type == dtype)
8492         {
8493           /* For a named type, use the typedef.  */
8494           gen_type_die (qualified_type, context_die);
8495           return lookup_type_die (qualified_type);
8496         }
8497       else if (is_const_type < TYPE_READONLY (dtype)
8498                || is_volatile_type < TYPE_VOLATILE (dtype)
8499                || (is_const_type <= TYPE_READONLY (dtype)
8500                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8501                    && DECL_ORIGINAL_TYPE (name) != type))
8502         /* cv-unqualified version of named type.  Just use the unnamed
8503            type to which it refers.  */
8504         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8505                                   is_const_type, is_volatile_type,
8506                                   context_die);
8507       /* Else cv-qualified version of named type; fall through.  */
8508     }
8509   
8510   if (is_const_type)
8511     {
8512       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8513       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8514     }
8515   else if (is_volatile_type)
8516     {
8517       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8518       sub_die = modified_type_die (type, 0, 0, context_die);
8519     }
8520   else if (code == POINTER_TYPE)
8521     {
8522       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8523       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8524                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8525       item_type = TREE_TYPE (type);
8526     }
8527   else if (code == REFERENCE_TYPE)
8528     {
8529       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8530       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8531                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8532       item_type = TREE_TYPE (type);
8533     }
8534   else if (is_subrange_type (type))
8535     {
8536       mod_type_die = subrange_type_die (type, context_die);
8537       item_type = TREE_TYPE (type);
8538     }
8539   else if (is_base_type (type))
8540     mod_type_die = base_type_die (type);
8541   else
8542     {
8543       gen_type_die (type, context_die);
8544       
8545       /* We have to get the type_main_variant here (and pass that to the
8546          `lookup_type_die' routine) because the ..._TYPE node we have
8547          might simply be a *copy* of some original type node (where the
8548          copy was created to help us keep track of typedef names) and
8549          that copy might have a different TYPE_UID from the original
8550          ..._TYPE node.  */
8551       if (TREE_CODE (type) != VECTOR_TYPE)
8552         return lookup_type_die (type_main_variant (type));
8553       else
8554         /* Vectors have the debugging information in the type,
8555            not the main variant.  */
8556         return lookup_type_die (type);
8557     }
8558   
8559   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8560      don't output a DW_TAG_typedef, since there isn't one in the
8561      user's program; just attach a DW_AT_name to the type.  */
8562   if (name
8563       && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8564     {
8565       if (TREE_CODE (name) == TYPE_DECL)
8566         /* Could just call add_name_and_src_coords_attributes here,
8567            but since this is a builtin type it doesn't have any
8568            useful source coordinates anyway.  */
8569         name = DECL_NAME (name);
8570       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8571     }
8572   
8573   if (qualified_type)
8574     equate_type_number_to_die (qualified_type, mod_type_die);
8575
8576   if (item_type)
8577     /* We must do this after the equate_type_number_to_die call, in case
8578        this is a recursive type.  This ensures that the modified_type_die
8579        recursion will terminate even if the type is recursive.  Recursive
8580        types are possible in Ada.  */
8581     sub_die = modified_type_die (item_type,
8582                                  TYPE_READONLY (item_type),
8583                                  TYPE_VOLATILE (item_type),
8584                                  context_die);
8585
8586   if (sub_die != NULL)
8587     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8588
8589   return mod_type_die;
8590 }
8591
8592 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8593    an enumerated type.  */
8594
8595 static inline int
8596 type_is_enum (tree type)
8597 {
8598   return TREE_CODE (type) == ENUMERAL_TYPE;
8599 }
8600
8601 /* Return the DBX register number described by a given RTL node.  */
8602
8603 static unsigned int
8604 dbx_reg_number (rtx rtl)
8605 {
8606   unsigned regno = REGNO (rtl);
8607
8608   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8609
8610 #ifdef LEAF_REG_REMAP
8611   if (current_function_uses_only_leaf_regs)
8612     {
8613       int leaf_reg = LEAF_REG_REMAP (regno);
8614       if (leaf_reg != -1)
8615         regno = (unsigned) leaf_reg;
8616     }
8617 #endif
8618
8619   return DBX_REGISTER_NUMBER (regno);
8620 }
8621
8622 /* Optionally add a DW_OP_piece term to a location description expression.
8623    DW_OP_piece is only added if the location description expression already
8624    doesn't end with DW_OP_piece.  */
8625
8626 static void
8627 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8628 {
8629   dw_loc_descr_ref loc;
8630
8631   if (*list_head != NULL)
8632     {
8633       /* Find the end of the chain.  */
8634       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8635         ;
8636
8637       if (loc->dw_loc_opc != DW_OP_piece)
8638         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8639     }
8640 }
8641
8642 /* Return a location descriptor that designates a machine register or
8643    zero if there is none.  */
8644
8645 static dw_loc_descr_ref
8646 reg_loc_descriptor (rtx rtl)
8647 {
8648   rtx regs;
8649
8650   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8651     return 0;
8652
8653   regs = targetm.dwarf_register_span (rtl);
8654
8655   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8656     return multiple_reg_loc_descriptor (rtl, regs);
8657   else
8658     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8659 }
8660
8661 /* Return a location descriptor that designates a machine register for
8662    a given hard register number.  */
8663
8664 static dw_loc_descr_ref
8665 one_reg_loc_descriptor (unsigned int regno)
8666 {
8667   if (regno <= 31)
8668     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8669   else
8670     return new_loc_descr (DW_OP_regx, regno, 0);
8671 }
8672
8673 /* Given an RTL of a register, return a location descriptor that
8674    designates a value that spans more than one register.  */
8675
8676 static dw_loc_descr_ref
8677 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8678 {
8679   int nregs, size, i;
8680   unsigned reg;
8681   dw_loc_descr_ref loc_result = NULL;
8682
8683   reg = REGNO (rtl);
8684 #ifdef LEAF_REG_REMAP
8685   if (current_function_uses_only_leaf_regs)
8686     {
8687       int leaf_reg = LEAF_REG_REMAP (reg);
8688       if (leaf_reg != -1)
8689         reg = (unsigned) leaf_reg;
8690     }
8691 #endif
8692   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8693   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8694
8695   /* Simple, contiguous registers.  */
8696   if (regs == NULL_RTX)
8697     {
8698       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8699
8700       loc_result = NULL;
8701       while (nregs--)
8702         {
8703           dw_loc_descr_ref t;
8704
8705           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8706           add_loc_descr (&loc_result, t);
8707           add_loc_descr_op_piece (&loc_result, size);
8708           ++reg;
8709         }
8710       return loc_result;
8711     }
8712
8713   /* Now onto stupid register sets in non contiguous locations.  */
8714
8715   gcc_assert (GET_CODE (regs) == PARALLEL);
8716
8717   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8718   loc_result = NULL;
8719
8720   for (i = 0; i < XVECLEN (regs, 0); ++i)
8721     {
8722       dw_loc_descr_ref t;
8723
8724       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8725       add_loc_descr (&loc_result, t);
8726       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8727       add_loc_descr_op_piece (&loc_result, size);
8728     }
8729   return loc_result;
8730 }
8731
8732 /* Return a location descriptor that designates a constant.  */
8733
8734 static dw_loc_descr_ref
8735 int_loc_descriptor (HOST_WIDE_INT i)
8736 {
8737   enum dwarf_location_atom op;
8738
8739   /* Pick the smallest representation of a constant, rather than just
8740      defaulting to the LEB encoding.  */
8741   if (i >= 0)
8742     {
8743       if (i <= 31)
8744         op = DW_OP_lit0 + i;
8745       else if (i <= 0xff)
8746         op = DW_OP_const1u;
8747       else if (i <= 0xffff)
8748         op = DW_OP_const2u;
8749       else if (HOST_BITS_PER_WIDE_INT == 32
8750                || i <= 0xffffffff)
8751         op = DW_OP_const4u;
8752       else
8753         op = DW_OP_constu;
8754     }
8755   else
8756     {
8757       if (i >= -0x80)
8758         op = DW_OP_const1s;
8759       else if (i >= -0x8000)
8760         op = DW_OP_const2s;
8761       else if (HOST_BITS_PER_WIDE_INT == 32
8762                || i >= -0x80000000)
8763         op = DW_OP_const4s;
8764       else
8765         op = DW_OP_consts;
8766     }
8767
8768   return new_loc_descr (op, i, 0);
8769 }
8770
8771 /* Return a location descriptor that designates a base+offset location.  */
8772
8773 static dw_loc_descr_ref
8774 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8775 {
8776   unsigned int regno;
8777
8778   /* We only use "frame base" when we're sure we're talking about the
8779      post-prologue local stack frame.  We do this by *not* running
8780      register elimination until this point, and recognizing the special
8781      argument pointer and soft frame pointer rtx's.  */
8782   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8783     {
8784       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8785
8786       if (elim != reg)
8787         {
8788           if (GET_CODE (elim) == PLUS)
8789             {
8790               offset += INTVAL (XEXP (elim, 1));
8791               elim = XEXP (elim, 0);
8792             }
8793           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8794                       : stack_pointer_rtx));
8795           offset += frame_pointer_fb_offset;
8796
8797           return new_loc_descr (DW_OP_fbreg, offset, 0);
8798         }
8799     }
8800
8801   regno = dbx_reg_number (reg);
8802   if (regno <= 31)
8803     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8804   else
8805     return new_loc_descr (DW_OP_bregx, regno, offset);
8806 }
8807
8808 /* Return true if this RTL expression describes a base+offset calculation.  */
8809
8810 static inline int
8811 is_based_loc (rtx rtl)
8812 {
8813   return (GET_CODE (rtl) == PLUS
8814           && ((REG_P (XEXP (rtl, 0))
8815                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8816                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8817 }
8818
8819 /* The following routine converts the RTL for a variable or parameter
8820    (resident in memory) into an equivalent Dwarf representation of a
8821    mechanism for getting the address of that same variable onto the top of a
8822    hypothetical "address evaluation" stack.
8823
8824    When creating memory location descriptors, we are effectively transforming
8825    the RTL for a memory-resident object into its Dwarf postfix expression
8826    equivalent.  This routine recursively descends an RTL tree, turning
8827    it into Dwarf postfix code as it goes.
8828
8829    MODE is the mode of the memory reference, needed to handle some
8830    autoincrement addressing modes.
8831
8832    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8833    location list for RTL.
8834
8835    Return 0 if we can't represent the location.  */
8836
8837 static dw_loc_descr_ref
8838 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8839 {
8840   dw_loc_descr_ref mem_loc_result = NULL;
8841   enum dwarf_location_atom op;
8842
8843   /* Note that for a dynamically sized array, the location we will generate a
8844      description of here will be the lowest numbered location which is
8845      actually within the array.  That's *not* necessarily the same as the
8846      zeroth element of the array.  */
8847
8848   rtl = targetm.delegitimize_address (rtl);
8849
8850   switch (GET_CODE (rtl))
8851     {
8852     case POST_INC:
8853     case POST_DEC:
8854     case POST_MODIFY:
8855       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8856          just fall into the SUBREG code.  */
8857
8858       /* ... fall through ...  */
8859
8860     case SUBREG:
8861       /* The case of a subreg may arise when we have a local (register)
8862          variable or a formal (register) parameter which doesn't quite fill
8863          up an entire register.  For now, just assume that it is
8864          legitimate to make the Dwarf info refer to the whole register which
8865          contains the given subreg.  */
8866       rtl = XEXP (rtl, 0);
8867
8868       /* ... fall through ...  */
8869
8870     case REG:
8871       /* Whenever a register number forms a part of the description of the
8872          method for calculating the (dynamic) address of a memory resident
8873          object, DWARF rules require the register number be referred to as
8874          a "base register".  This distinction is not based in any way upon
8875          what category of register the hardware believes the given register
8876          belongs to.  This is strictly DWARF terminology we're dealing with
8877          here. Note that in cases where the location of a memory-resident
8878          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8879          OP_CONST (0)) the actual DWARF location descriptor that we generate
8880          may just be OP_BASEREG (basereg).  This may look deceptively like
8881          the object in question was allocated to a register (rather than in
8882          memory) so DWARF consumers need to be aware of the subtle
8883          distinction between OP_REG and OP_BASEREG.  */
8884       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8885         mem_loc_result = based_loc_descr (rtl, 0);
8886       break;
8887
8888     case MEM:
8889       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8890       if (mem_loc_result != 0)
8891         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8892       break;
8893
8894     case LO_SUM:
8895          rtl = XEXP (rtl, 1);
8896
8897       /* ... fall through ...  */
8898
8899     case LABEL_REF:
8900       /* Some ports can transform a symbol ref into a label ref, because
8901          the symbol ref is too far away and has to be dumped into a constant
8902          pool.  */
8903     case CONST:
8904     case SYMBOL_REF:
8905       /* Alternatively, the symbol in the constant pool might be referenced
8906          by a different symbol.  */
8907       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8908         {
8909           bool marked;
8910           rtx tmp = get_pool_constant_mark (rtl, &marked);
8911
8912           if (GET_CODE (tmp) == SYMBOL_REF)
8913             {
8914               rtl = tmp;
8915               if (CONSTANT_POOL_ADDRESS_P (tmp))
8916                 get_pool_constant_mark (tmp, &marked);
8917               else
8918                 marked = true;
8919             }
8920
8921           /* If all references to this pool constant were optimized away,
8922              it was not output and thus we can't represent it.
8923              FIXME: might try to use DW_OP_const_value here, though
8924              DW_OP_piece complicates it.  */
8925           if (!marked)
8926             return 0;
8927         }
8928
8929       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8930       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8931       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8932       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8933       break;
8934
8935     case PRE_MODIFY:
8936       /* Extract the PLUS expression nested inside and fall into
8937          PLUS code below.  */
8938       rtl = XEXP (rtl, 1);
8939       goto plus;
8940
8941     case PRE_INC:
8942     case PRE_DEC:
8943       /* Turn these into a PLUS expression and fall into the PLUS code
8944          below.  */
8945       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8946                           GEN_INT (GET_CODE (rtl) == PRE_INC
8947                                    ? GET_MODE_UNIT_SIZE (mode)
8948                                    : -GET_MODE_UNIT_SIZE (mode)));
8949
8950       /* ... fall through ...  */
8951
8952     case PLUS:
8953     plus:
8954       if (is_based_loc (rtl))
8955         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8956                                           INTVAL (XEXP (rtl, 1)));
8957       else
8958         {
8959           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8960           if (mem_loc_result == 0)
8961             break;
8962
8963           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8964               && INTVAL (XEXP (rtl, 1)) >= 0)
8965             add_loc_descr (&mem_loc_result,
8966                            new_loc_descr (DW_OP_plus_uconst,
8967                                           INTVAL (XEXP (rtl, 1)), 0));
8968           else
8969             {
8970               add_loc_descr (&mem_loc_result,
8971                              mem_loc_descriptor (XEXP (rtl, 1), mode));
8972               add_loc_descr (&mem_loc_result,
8973                              new_loc_descr (DW_OP_plus, 0, 0));
8974             }
8975         }
8976       break;
8977
8978     /* If a pseudo-reg is optimized away, it is possible for it to
8979        be replaced with a MEM containing a multiply or shift.  */
8980     case MULT:
8981       op = DW_OP_mul;
8982       goto do_binop;
8983
8984     case ASHIFT:
8985       op = DW_OP_shl;
8986       goto do_binop;
8987
8988     case ASHIFTRT:
8989       op = DW_OP_shra;
8990       goto do_binop;
8991
8992     case LSHIFTRT:
8993       op = DW_OP_shr;
8994       goto do_binop;
8995
8996     do_binop:
8997       {
8998         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8999         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
9000
9001         if (op0 == 0 || op1 == 0)
9002           break;
9003
9004         mem_loc_result = op0;
9005         add_loc_descr (&mem_loc_result, op1);
9006         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9007         break;
9008       }
9009
9010     case CONST_INT:
9011       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9012       break;
9013
9014     default:
9015       gcc_unreachable ();
9016     }
9017
9018   return mem_loc_result;
9019 }
9020
9021 /* Return a descriptor that describes the concatenation of two locations.
9022    This is typically a complex variable.  */
9023
9024 static dw_loc_descr_ref
9025 concat_loc_descriptor (rtx x0, rtx x1)
9026 {
9027   dw_loc_descr_ref cc_loc_result = NULL;
9028   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
9029   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
9030
9031   if (x0_ref == 0 || x1_ref == 0)
9032     return 0;
9033
9034   cc_loc_result = x0_ref;
9035   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9036
9037   add_loc_descr (&cc_loc_result, x1_ref);
9038   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9039
9040   return cc_loc_result;
9041 }
9042
9043 /* Output a proper Dwarf location descriptor for a variable or parameter
9044    which is either allocated in a register or in a memory location.  For a
9045    register, we just generate an OP_REG and the register number.  For a
9046    memory location we provide a Dwarf postfix expression describing how to
9047    generate the (dynamic) address of the object onto the address stack.
9048
9049    If we don't know how to describe it, return 0.  */
9050
9051 static dw_loc_descr_ref
9052 loc_descriptor (rtx rtl)
9053 {
9054   dw_loc_descr_ref loc_result = NULL;
9055
9056   switch (GET_CODE (rtl))
9057     {
9058     case SUBREG:
9059       /* The case of a subreg may arise when we have a local (register)
9060          variable or a formal (register) parameter which doesn't quite fill
9061          up an entire register.  For now, just assume that it is
9062          legitimate to make the Dwarf info refer to the whole register which
9063          contains the given subreg.  */
9064       rtl = SUBREG_REG (rtl);
9065
9066       /* ... fall through ...  */
9067
9068     case REG:
9069       loc_result = reg_loc_descriptor (rtl);
9070       break;
9071
9072     case MEM:
9073       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
9074       break;
9075
9076     case CONCAT:
9077       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
9078       break;
9079
9080     case VAR_LOCATION:
9081       /* Single part.  */
9082       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9083         {
9084           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9085           break;
9086         }
9087
9088       rtl = XEXP (rtl, 1);
9089       /* FALLTHRU */
9090
9091     case PARALLEL:
9092       {
9093         rtvec par_elems = XVEC (rtl, 0);
9094         int num_elem = GET_NUM_ELEM (par_elems);
9095         enum machine_mode mode;
9096         int i;
9097
9098         /* Create the first one, so we have something to add to.  */
9099         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9100         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9101         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9102         for (i = 1; i < num_elem; i++)
9103           {
9104             dw_loc_descr_ref temp;
9105
9106             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9107             add_loc_descr (&loc_result, temp);
9108             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9109             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9110           }
9111       }
9112       break;
9113
9114     default:
9115       gcc_unreachable ();
9116     }
9117
9118   return loc_result;
9119 }
9120
9121 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9122    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9123    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9124    top-level invocation, and we require the address of LOC; is 0 if we require
9125    the value of LOC.  */
9126
9127 static dw_loc_descr_ref
9128 loc_descriptor_from_tree_1 (tree loc, int want_address)
9129 {
9130   dw_loc_descr_ref ret, ret1;
9131   int have_address = 0;
9132   enum dwarf_location_atom op;
9133
9134   /* ??? Most of the time we do not take proper care for sign/zero
9135      extending the values properly.  Hopefully this won't be a real
9136      problem...  */
9137
9138   switch (TREE_CODE (loc))
9139     {
9140     case ERROR_MARK:
9141       return 0;
9142
9143     case PLACEHOLDER_EXPR:
9144       /* This case involves extracting fields from an object to determine the
9145          position of other fields.  We don't try to encode this here.  The
9146          only user of this is Ada, which encodes the needed information using
9147          the names of types.  */
9148       return 0;
9149
9150     case CALL_EXPR:
9151       return 0;
9152
9153     case PREINCREMENT_EXPR:
9154     case PREDECREMENT_EXPR:
9155     case POSTINCREMENT_EXPR:
9156     case POSTDECREMENT_EXPR:
9157       /* There are no opcodes for these operations.  */
9158       return 0;
9159
9160     case ADDR_EXPR:
9161       /* If we already want an address, there's nothing we can do.  */
9162       if (want_address)
9163         return 0;
9164
9165       /* Otherwise, process the argument and look for the address.  */
9166       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9167
9168     case VAR_DECL:
9169       if (DECL_THREAD_LOCAL_P (loc))
9170         {
9171           rtx rtl;
9172
9173           /* If this is not defined, we have no way to emit the data.  */
9174           if (!targetm.asm_out.output_dwarf_dtprel)
9175             return 0;
9176
9177           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9178              look up addresses of objects in the current module.  */
9179           if (DECL_EXTERNAL (loc))
9180             return 0;
9181
9182           rtl = rtl_for_decl_location (loc);
9183           if (rtl == NULL_RTX)
9184             return 0;
9185
9186           if (!MEM_P (rtl))
9187             return 0;
9188           rtl = XEXP (rtl, 0);
9189           if (! CONSTANT_P (rtl))
9190             return 0;
9191
9192           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9193           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9194           ret->dw_loc_oprnd1.v.val_addr = rtl;
9195
9196           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9197           add_loc_descr (&ret, ret1);
9198
9199           have_address = 1;
9200           break;
9201         }
9202       /* FALLTHRU */
9203
9204     case PARM_DECL:
9205       if (DECL_HAS_VALUE_EXPR_P (loc))
9206         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9207                                            want_address);
9208       /* FALLTHRU */
9209
9210     case RESULT_DECL:
9211     case FUNCTION_DECL:
9212       {
9213         rtx rtl = rtl_for_decl_location (loc);
9214
9215         if (rtl == NULL_RTX)
9216           return 0;
9217         else if (GET_CODE (rtl) == CONST_INT)
9218           {
9219             HOST_WIDE_INT val = INTVAL (rtl);
9220             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9221               val &= GET_MODE_MASK (DECL_MODE (loc));
9222             ret = int_loc_descriptor (val);
9223           }
9224         else if (GET_CODE (rtl) == CONST_STRING)
9225           return 0;
9226         else if (CONSTANT_P (rtl))
9227           {
9228             ret = new_loc_descr (DW_OP_addr, 0, 0);
9229             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9230             ret->dw_loc_oprnd1.v.val_addr = rtl;
9231           }
9232         else
9233           {
9234             enum machine_mode mode;
9235
9236             /* Certain constructs can only be represented at top-level.  */
9237             if (want_address == 2)
9238               return loc_descriptor (rtl);
9239
9240             mode = GET_MODE (rtl);
9241             if (MEM_P (rtl))
9242               {
9243                 rtl = XEXP (rtl, 0);
9244                 have_address = 1;
9245               }
9246             ret = mem_loc_descriptor (rtl, mode);
9247           }
9248       }
9249       break;
9250
9251     case INDIRECT_REF:
9252       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9253       have_address = 1;
9254       break;
9255
9256     case COMPOUND_EXPR:
9257       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9258
9259     case NOP_EXPR:
9260     case CONVERT_EXPR:
9261     case NON_LVALUE_EXPR:
9262     case VIEW_CONVERT_EXPR:
9263     case SAVE_EXPR:
9264     case MODIFY_EXPR:
9265       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9266
9267     case COMPONENT_REF:
9268     case BIT_FIELD_REF:
9269     case ARRAY_REF:
9270     case ARRAY_RANGE_REF:
9271       {
9272         tree obj, offset;
9273         HOST_WIDE_INT bitsize, bitpos, bytepos;
9274         enum machine_mode mode;
9275         int volatilep;
9276         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9277
9278         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9279                                    &unsignedp, &volatilep, false);
9280
9281         if (obj == loc)
9282           return 0;
9283
9284         ret = loc_descriptor_from_tree_1 (obj, 1);
9285         if (ret == 0
9286             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9287           return 0;
9288
9289         if (offset != NULL_TREE)
9290           {
9291             /* Variable offset.  */
9292             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9293             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9294           }
9295
9296         bytepos = bitpos / BITS_PER_UNIT;
9297         if (bytepos > 0)
9298           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9299         else if (bytepos < 0)
9300           {
9301             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9302             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9303           }
9304
9305         have_address = 1;
9306         break;
9307       }
9308
9309     case INTEGER_CST:
9310       if (host_integerp (loc, 0))
9311         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9312       else
9313         return 0;
9314       break;
9315
9316     case CONSTRUCTOR:
9317       {
9318         /* Get an RTL for this, if something has been emitted.  */
9319         rtx rtl = lookup_constant_def (loc);
9320         enum machine_mode mode;
9321
9322         if (!rtl || !MEM_P (rtl))
9323           return 0;
9324         mode = GET_MODE (rtl);
9325         rtl = XEXP (rtl, 0);
9326         ret = mem_loc_descriptor (rtl, mode);
9327         have_address = 1;
9328         break;
9329       }
9330
9331     case TRUTH_AND_EXPR:
9332     case TRUTH_ANDIF_EXPR:
9333     case BIT_AND_EXPR:
9334       op = DW_OP_and;
9335       goto do_binop;
9336
9337     case TRUTH_XOR_EXPR:
9338     case BIT_XOR_EXPR:
9339       op = DW_OP_xor;
9340       goto do_binop;
9341
9342     case TRUTH_OR_EXPR:
9343     case TRUTH_ORIF_EXPR:
9344     case BIT_IOR_EXPR:
9345       op = DW_OP_or;
9346       goto do_binop;
9347
9348     case FLOOR_DIV_EXPR:
9349     case CEIL_DIV_EXPR:
9350     case ROUND_DIV_EXPR:
9351     case TRUNC_DIV_EXPR:
9352       op = DW_OP_div;
9353       goto do_binop;
9354
9355     case MINUS_EXPR:
9356       op = DW_OP_minus;
9357       goto do_binop;
9358
9359     case FLOOR_MOD_EXPR:
9360     case CEIL_MOD_EXPR:
9361     case ROUND_MOD_EXPR:
9362     case TRUNC_MOD_EXPR:
9363       op = DW_OP_mod;
9364       goto do_binop;
9365
9366     case MULT_EXPR:
9367       op = DW_OP_mul;
9368       goto do_binop;
9369
9370     case LSHIFT_EXPR:
9371       op = DW_OP_shl;
9372       goto do_binop;
9373
9374     case RSHIFT_EXPR:
9375       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9376       goto do_binop;
9377
9378     case PLUS_EXPR:
9379       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9380           && host_integerp (TREE_OPERAND (loc, 1), 0))
9381         {
9382           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9383           if (ret == 0)
9384             return 0;
9385
9386           add_loc_descr (&ret,
9387                          new_loc_descr (DW_OP_plus_uconst,
9388                                         tree_low_cst (TREE_OPERAND (loc, 1),
9389                                                       0),
9390                                         0));
9391           break;
9392         }
9393
9394       op = DW_OP_plus;
9395       goto do_binop;
9396
9397     case LE_EXPR:
9398       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9399         return 0;
9400
9401       op = DW_OP_le;
9402       goto do_binop;
9403
9404     case GE_EXPR:
9405       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9406         return 0;
9407
9408       op = DW_OP_ge;
9409       goto do_binop;
9410
9411     case LT_EXPR:
9412       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9413         return 0;
9414
9415       op = DW_OP_lt;
9416       goto do_binop;
9417
9418     case GT_EXPR:
9419       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9420         return 0;
9421
9422       op = DW_OP_gt;
9423       goto do_binop;
9424
9425     case EQ_EXPR:
9426       op = DW_OP_eq;
9427       goto do_binop;
9428
9429     case NE_EXPR:
9430       op = DW_OP_ne;
9431       goto do_binop;
9432
9433     do_binop:
9434       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9435       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9436       if (ret == 0 || ret1 == 0)
9437         return 0;
9438
9439       add_loc_descr (&ret, ret1);
9440       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9441       break;
9442
9443     case TRUTH_NOT_EXPR:
9444     case BIT_NOT_EXPR:
9445       op = DW_OP_not;
9446       goto do_unop;
9447
9448     case ABS_EXPR:
9449       op = DW_OP_abs;
9450       goto do_unop;
9451
9452     case NEGATE_EXPR:
9453       op = DW_OP_neg;
9454       goto do_unop;
9455
9456     do_unop:
9457       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9458       if (ret == 0)
9459         return 0;
9460
9461       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9462       break;
9463
9464     case MIN_EXPR:
9465     case MAX_EXPR:
9466       {
9467         const enum tree_code code =
9468           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9469
9470         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9471                       build2 (code, integer_type_node,
9472                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9473                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9474       }
9475
9476       /* ... fall through ...  */
9477
9478     case COND_EXPR:
9479       {
9480         dw_loc_descr_ref lhs
9481           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9482         dw_loc_descr_ref rhs
9483           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9484         dw_loc_descr_ref bra_node, jump_node, tmp;
9485
9486         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9487         if (ret == 0 || lhs == 0 || rhs == 0)
9488           return 0;
9489
9490         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9491         add_loc_descr (&ret, bra_node);
9492
9493         add_loc_descr (&ret, rhs);
9494         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9495         add_loc_descr (&ret, jump_node);
9496
9497         add_loc_descr (&ret, lhs);
9498         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9499         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9500
9501         /* ??? Need a node to point the skip at.  Use a nop.  */
9502         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9503         add_loc_descr (&ret, tmp);
9504         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9505         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9506       }
9507       break;
9508
9509     case FIX_TRUNC_EXPR:
9510     case FIX_CEIL_EXPR:
9511     case FIX_FLOOR_EXPR:
9512     case FIX_ROUND_EXPR:
9513       return 0;
9514
9515     default:
9516       /* Leave front-end specific codes as simply unknown.  This comes
9517          up, for instance, with the C STMT_EXPR.  */
9518       if ((unsigned int) TREE_CODE (loc)
9519           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9520         return 0;
9521
9522 #ifdef ENABLE_CHECKING
9523       /* Otherwise this is a generic code; we should just lists all of
9524          these explicitly.  We forgot one.  */
9525       gcc_unreachable ();
9526 #else
9527       /* In a release build, we want to degrade gracefully: better to
9528          generate incomplete debugging information than to crash.  */
9529       return NULL;
9530 #endif
9531     }
9532
9533   /* Show if we can't fill the request for an address.  */
9534   if (want_address && !have_address)
9535     return 0;
9536
9537   /* If we've got an address and don't want one, dereference.  */
9538   if (!want_address && have_address && ret)
9539     {
9540       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9541
9542       if (size > DWARF2_ADDR_SIZE || size == -1)
9543         return 0;
9544       else if (size == DWARF2_ADDR_SIZE)
9545         op = DW_OP_deref;
9546       else
9547         op = DW_OP_deref_size;
9548
9549       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9550     }
9551
9552   return ret;
9553 }
9554
9555 static inline dw_loc_descr_ref
9556 loc_descriptor_from_tree (tree loc)
9557 {
9558   return loc_descriptor_from_tree_1 (loc, 2);
9559 }
9560
9561 /* Given a value, round it up to the lowest multiple of `boundary'
9562    which is not less than the value itself.  */
9563
9564 static inline HOST_WIDE_INT
9565 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9566 {
9567   return (((value + boundary - 1) / boundary) * boundary);
9568 }
9569
9570 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9571    pointer to the declared type for the relevant field variable, or return
9572    `integer_type_node' if the given node turns out to be an
9573    ERROR_MARK node.  */
9574
9575 static inline tree
9576 field_type (tree decl)
9577 {
9578   tree type;
9579
9580   if (TREE_CODE (decl) == ERROR_MARK)
9581     return integer_type_node;
9582
9583   type = DECL_BIT_FIELD_TYPE (decl);
9584   if (type == NULL_TREE)
9585     type = TREE_TYPE (decl);
9586
9587   return type;
9588 }
9589
9590 /* Given a pointer to a tree node, return the alignment in bits for
9591    it, or else return BITS_PER_WORD if the node actually turns out to
9592    be an ERROR_MARK node.  */
9593
9594 static inline unsigned
9595 simple_type_align_in_bits (tree type)
9596 {
9597   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9598 }
9599
9600 static inline unsigned
9601 simple_decl_align_in_bits (tree decl)
9602 {
9603   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9604 }
9605
9606 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9607    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9608    or return 0 if we are unable to determine what that offset is, either
9609    because the argument turns out to be a pointer to an ERROR_MARK node, or
9610    because the offset is actually variable.  (We can't handle the latter case
9611    just yet).  */
9612
9613 static HOST_WIDE_INT
9614 field_byte_offset (tree decl)
9615 {
9616   unsigned int type_align_in_bits;
9617   unsigned int decl_align_in_bits;
9618   unsigned HOST_WIDE_INT type_size_in_bits;
9619   HOST_WIDE_INT object_offset_in_bits;
9620   tree type;
9621   tree field_size_tree;
9622   HOST_WIDE_INT bitpos_int;
9623   HOST_WIDE_INT deepest_bitpos;
9624   unsigned HOST_WIDE_INT field_size_in_bits;
9625
9626   if (TREE_CODE (decl) == ERROR_MARK)
9627     return 0;
9628
9629   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9630
9631   type = field_type (decl);
9632   field_size_tree = DECL_SIZE (decl);
9633
9634   /* The size could be unspecified if there was an error, or for
9635      a flexible array member.  */
9636   if (! field_size_tree)
9637     field_size_tree = bitsize_zero_node;
9638
9639   /* We cannot yet cope with fields whose positions are variable, so
9640      for now, when we see such things, we simply return 0.  Someday, we may
9641      be able to handle such cases, but it will be damn difficult.  */
9642   if (! host_integerp (bit_position (decl), 0))
9643     return 0;
9644
9645   bitpos_int = int_bit_position (decl);
9646
9647   /* If we don't know the size of the field, pretend it's a full word.  */
9648   if (host_integerp (field_size_tree, 1))
9649     field_size_in_bits = tree_low_cst (field_size_tree, 1);
9650   else
9651     field_size_in_bits = BITS_PER_WORD;
9652
9653   type_size_in_bits = simple_type_size_in_bits (type);
9654   type_align_in_bits = simple_type_align_in_bits (type);
9655   decl_align_in_bits = simple_decl_align_in_bits (decl);
9656
9657   /* The GCC front-end doesn't make any attempt to keep track of the starting
9658      bit offset (relative to the start of the containing structure type) of the
9659      hypothetical "containing object" for a bit-field.  Thus, when computing
9660      the byte offset value for the start of the "containing object" of a
9661      bit-field, we must deduce this information on our own. This can be rather
9662      tricky to do in some cases.  For example, handling the following structure
9663      type definition when compiling for an i386/i486 target (which only aligns
9664      long long's to 32-bit boundaries) can be very tricky:
9665
9666          struct S { int field1; long long field2:31; };
9667
9668      Fortunately, there is a simple rule-of-thumb which can be used in such
9669      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9670      structure shown above.  It decides to do this based upon one simple rule
9671      for bit-field allocation.  GCC allocates each "containing object" for each
9672      bit-field at the first (i.e. lowest addressed) legitimate alignment
9673      boundary (based upon the required minimum alignment for the declared type
9674      of the field) which it can possibly use, subject to the condition that
9675      there is still enough available space remaining in the containing object
9676      (when allocated at the selected point) to fully accommodate all of the
9677      bits of the bit-field itself.
9678
9679      This simple rule makes it obvious why GCC allocates 8 bytes for each
9680      object of the structure type shown above.  When looking for a place to
9681      allocate the "containing object" for `field2', the compiler simply tries
9682      to allocate a 64-bit "containing object" at each successive 32-bit
9683      boundary (starting at zero) until it finds a place to allocate that 64-
9684      bit field such that at least 31 contiguous (and previously unallocated)
9685      bits remain within that selected 64 bit field.  (As it turns out, for the
9686      example above, the compiler finds it is OK to allocate the "containing
9687      object" 64-bit field at bit-offset zero within the structure type.)
9688
9689      Here we attempt to work backwards from the limited set of facts we're
9690      given, and we try to deduce from those facts, where GCC must have believed
9691      that the containing object started (within the structure type). The value
9692      we deduce is then used (by the callers of this routine) to generate
9693      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9694      and, in the case of DW_AT_location, regular fields as well).  */
9695
9696   /* Figure out the bit-distance from the start of the structure to the
9697      "deepest" bit of the bit-field.  */
9698   deepest_bitpos = bitpos_int + field_size_in_bits;
9699
9700   /* This is the tricky part.  Use some fancy footwork to deduce where the
9701      lowest addressed bit of the containing object must be.  */
9702   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9703
9704   /* Round up to type_align by default.  This works best for bitfields.  */
9705   object_offset_in_bits += type_align_in_bits - 1;
9706   object_offset_in_bits /= type_align_in_bits;
9707   object_offset_in_bits *= type_align_in_bits;
9708
9709   if (object_offset_in_bits > bitpos_int)
9710     {
9711       /* Sigh, the decl must be packed.  */
9712       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9713
9714       /* Round up to decl_align instead.  */
9715       object_offset_in_bits += decl_align_in_bits - 1;
9716       object_offset_in_bits /= decl_align_in_bits;
9717       object_offset_in_bits *= decl_align_in_bits;
9718     }
9719
9720   return object_offset_in_bits / BITS_PER_UNIT;
9721 }
9722 \f
9723 /* The following routines define various Dwarf attributes and any data
9724    associated with them.  */
9725
9726 /* Add a location description attribute value to a DIE.
9727
9728    This emits location attributes suitable for whole variables and
9729    whole parameters.  Note that the location attributes for struct fields are
9730    generated by the routine `data_member_location_attribute' below.  */
9731
9732 static inline void
9733 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9734                              dw_loc_descr_ref descr)
9735 {
9736   if (descr != 0)
9737     add_AT_loc (die, attr_kind, descr);
9738 }
9739
9740 /* Attach the specialized form of location attribute used for data members of
9741    struct and union types.  In the special case of a FIELD_DECL node which
9742    represents a bit-field, the "offset" part of this special location
9743    descriptor must indicate the distance in bytes from the lowest-addressed
9744    byte of the containing struct or union type to the lowest-addressed byte of
9745    the "containing object" for the bit-field.  (See the `field_byte_offset'
9746    function above).
9747
9748    For any given bit-field, the "containing object" is a hypothetical object
9749    (of some integral or enum type) within which the given bit-field lives.  The
9750    type of this hypothetical "containing object" is always the same as the
9751    declared type of the individual bit-field itself (for GCC anyway... the
9752    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9753    bytes) of the hypothetical "containing object" which will be given in the
9754    DW_AT_byte_size attribute for this bit-field.  (See the
9755    `byte_size_attribute' function below.)  It is also used when calculating the
9756    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9757    function below.)  */
9758
9759 static void
9760 add_data_member_location_attribute (dw_die_ref die, tree decl)
9761 {
9762   HOST_WIDE_INT offset;
9763   dw_loc_descr_ref loc_descr = 0;
9764
9765   if (TREE_CODE (decl) == TREE_BINFO)
9766     {
9767       /* We're working on the TAG_inheritance for a base class.  */
9768       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9769         {
9770           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9771              aren't at a fixed offset from all (sub)objects of the same
9772              type.  We need to extract the appropriate offset from our
9773              vtable.  The following dwarf expression means
9774
9775                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9776
9777              This is specific to the V3 ABI, of course.  */
9778
9779           dw_loc_descr_ref tmp;
9780
9781           /* Make a copy of the object address.  */
9782           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9783           add_loc_descr (&loc_descr, tmp);
9784
9785           /* Extract the vtable address.  */
9786           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9787           add_loc_descr (&loc_descr, tmp);
9788
9789           /* Calculate the address of the offset.  */
9790           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9791           gcc_assert (offset < 0);
9792
9793           tmp = int_loc_descriptor (-offset);
9794           add_loc_descr (&loc_descr, tmp);
9795           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9796           add_loc_descr (&loc_descr, tmp);
9797
9798           /* Extract the offset.  */
9799           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9800           add_loc_descr (&loc_descr, tmp);
9801
9802           /* Add it to the object address.  */
9803           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9804           add_loc_descr (&loc_descr, tmp);
9805         }
9806       else
9807         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9808     }
9809   else
9810     offset = field_byte_offset (decl);
9811
9812   if (! loc_descr)
9813     {
9814       enum dwarf_location_atom op;
9815
9816       /* The DWARF2 standard says that we should assume that the structure
9817          address is already on the stack, so we can specify a structure field
9818          address by using DW_OP_plus_uconst.  */
9819
9820 #ifdef MIPS_DEBUGGING_INFO
9821       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9822          operator correctly.  It works only if we leave the offset on the
9823          stack.  */
9824       op = DW_OP_constu;
9825 #else
9826       op = DW_OP_plus_uconst;
9827 #endif
9828
9829       loc_descr = new_loc_descr (op, offset, 0);
9830     }
9831
9832   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9833 }
9834
9835 /* Writes integer values to dw_vec_const array.  */
9836
9837 static void
9838 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9839 {
9840   while (size != 0)
9841     {
9842       *dest++ = val & 0xff;
9843       val >>= 8;
9844       --size;
9845     }
9846 }
9847
9848 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9849
9850 static HOST_WIDE_INT
9851 extract_int (const unsigned char *src, unsigned int size)
9852 {
9853   HOST_WIDE_INT val = 0;
9854
9855   src += size;
9856   while (size != 0)
9857     {
9858       val <<= 8;
9859       val |= *--src & 0xff;
9860       --size;
9861     }
9862   return val;
9863 }
9864
9865 /* Writes floating point values to dw_vec_const array.  */
9866
9867 static void
9868 insert_float (rtx rtl, unsigned char *array)
9869 {
9870   REAL_VALUE_TYPE rv;
9871   long val[4];
9872   int i;
9873
9874   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9875   real_to_target (val, &rv, GET_MODE (rtl));
9876
9877   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9878   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9879     {
9880       insert_int (val[i], 4, array);
9881       array += 4;
9882     }
9883 }
9884
9885 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9886    does not have a "location" either in memory or in a register.  These
9887    things can arise in GNU C when a constant is passed as an actual parameter
9888    to an inlined function.  They can also arise in C++ where declared
9889    constants do not necessarily get memory "homes".  */
9890
9891 static void
9892 add_const_value_attribute (dw_die_ref die, rtx rtl)
9893 {
9894   switch (GET_CODE (rtl))
9895     {
9896     case CONST_INT:
9897       {
9898         HOST_WIDE_INT val = INTVAL (rtl);
9899
9900         if (val < 0)
9901           add_AT_int (die, DW_AT_const_value, val);
9902         else
9903           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9904       }
9905       break;
9906
9907     case CONST_DOUBLE:
9908       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9909          floating-point constant.  A CONST_DOUBLE is used whenever the
9910          constant requires more than one word in order to be adequately
9911          represented.  We output CONST_DOUBLEs as blocks.  */
9912       {
9913         enum machine_mode mode = GET_MODE (rtl);
9914
9915         if (SCALAR_FLOAT_MODE_P (mode))
9916           {
9917             unsigned int length = GET_MODE_SIZE (mode);
9918             unsigned char *array = ggc_alloc (length);
9919
9920             insert_float (rtl, array);
9921             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9922           }
9923         else
9924           {
9925             /* ??? We really should be using HOST_WIDE_INT throughout.  */
9926             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9927
9928             add_AT_long_long (die, DW_AT_const_value,
9929                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9930           }
9931       }
9932       break;
9933
9934     case CONST_VECTOR:
9935       {
9936         enum machine_mode mode = GET_MODE (rtl);
9937         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9938         unsigned int length = CONST_VECTOR_NUNITS (rtl);
9939         unsigned char *array = ggc_alloc (length * elt_size);
9940         unsigned int i;
9941         unsigned char *p;
9942
9943         switch (GET_MODE_CLASS (mode))
9944           {
9945           case MODE_VECTOR_INT:
9946             for (i = 0, p = array; i < length; i++, p += elt_size)
9947               {
9948                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9949                 HOST_WIDE_INT lo, hi;
9950
9951                 switch (GET_CODE (elt))
9952                   {
9953                   case CONST_INT:
9954                     lo = INTVAL (elt);
9955                     hi = -(lo < 0);
9956                     break;
9957
9958                   case CONST_DOUBLE:
9959                     lo = CONST_DOUBLE_LOW (elt);
9960                     hi = CONST_DOUBLE_HIGH (elt);
9961                     break;
9962
9963                   default:
9964                     gcc_unreachable ();
9965                   }
9966
9967                 if (elt_size <= sizeof (HOST_WIDE_INT))
9968                   insert_int (lo, elt_size, p);
9969                 else
9970                   {
9971                     unsigned char *p0 = p;
9972                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9973
9974                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9975                     if (WORDS_BIG_ENDIAN)
9976                       {
9977                         p0 = p1;
9978                         p1 = p;
9979                       }
9980                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9981                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9982                   }
9983               }
9984             break;
9985
9986           case MODE_VECTOR_FLOAT:
9987             for (i = 0, p = array; i < length; i++, p += elt_size)
9988               {
9989                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9990                 insert_float (elt, p);
9991               }
9992             break;
9993
9994           default:
9995             gcc_unreachable ();
9996           }
9997
9998         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9999       }
10000       break;
10001
10002     case CONST_STRING:
10003       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10004       break;
10005
10006     case SYMBOL_REF:
10007     case LABEL_REF:
10008     case CONST:
10009       add_AT_addr (die, DW_AT_const_value, rtl);
10010       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10011       break;
10012
10013     case PLUS:
10014       /* In cases where an inlined instance of an inline function is passed
10015          the address of an `auto' variable (which is local to the caller) we
10016          can get a situation where the DECL_RTL of the artificial local
10017          variable (for the inlining) which acts as a stand-in for the
10018          corresponding formal parameter (of the inline function) will look
10019          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10020          exactly a compile-time constant expression, but it isn't the address
10021          of the (artificial) local variable either.  Rather, it represents the
10022          *value* which the artificial local variable always has during its
10023          lifetime.  We currently have no way to represent such quasi-constant
10024          values in Dwarf, so for now we just punt and generate nothing.  */
10025       break;
10026
10027     default:
10028       /* No other kinds of rtx should be possible here.  */
10029       gcc_unreachable ();
10030     }
10031
10032 }
10033
10034 /* Determine whether the evaluation of EXPR references any variables
10035    or functions which aren't otherwise used (and therefore may not be
10036    output).  */
10037 static tree
10038 reference_to_unused (tree * tp, int * walk_subtrees,
10039                      void * data ATTRIBUTE_UNUSED)
10040 {
10041   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10042     *walk_subtrees = 0;
10043   
10044   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10045       && ! TREE_ASM_WRITTEN (*tp))
10046     return *tp;
10047   else if (!flag_unit_at_a_time)
10048     return NULL_TREE;
10049   else if (!cgraph_global_info_ready
10050            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10051     return *tp;
10052   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10053     {
10054       struct cgraph_varpool_node *node = cgraph_varpool_node (*tp);
10055       if (!node->needed)
10056         return *tp;
10057     }
10058    else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10059             && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10060     {
10061       struct cgraph_node *node = cgraph_node (*tp);
10062       if (!node->output)
10063         return *tp;
10064     }
10065
10066   return NULL_TREE;
10067 }
10068
10069 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10070    for use in a later add_const_value_attribute call.  */
10071
10072 static rtx
10073 rtl_for_decl_init (tree init, tree type)
10074 {
10075   rtx rtl = NULL_RTX;
10076
10077   /* If a variable is initialized with a string constant without embedded
10078      zeros, build CONST_STRING.  */
10079   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10080     {
10081       tree enttype = TREE_TYPE (type);
10082       tree domain = TYPE_DOMAIN (type);
10083       enum machine_mode mode = TYPE_MODE (enttype);
10084
10085       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10086           && domain
10087           && integer_zerop (TYPE_MIN_VALUE (domain))
10088           && compare_tree_int (TYPE_MAX_VALUE (domain),
10089                                TREE_STRING_LENGTH (init) - 1) == 0
10090           && ((size_t) TREE_STRING_LENGTH (init)
10091               == strlen (TREE_STRING_POINTER (init)) + 1))
10092         rtl = gen_rtx_CONST_STRING (VOIDmode,
10093                                     ggc_strdup (TREE_STRING_POINTER (init)));
10094     }
10095   /* Other aggregates, and complex values, could be represented using
10096      CONCAT: FIXME!  */
10097   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10098     ;
10099   /* Vectors only work if their mode is supported by the target.  
10100      FIXME: generic vectors ought to work too.  */
10101   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10102     ;
10103   /* If the initializer is something that we know will expand into an
10104      immediate RTL constant, expand it now.  We must be careful not to
10105      reference variables which won't be output.  */
10106   else if (initializer_constant_valid_p (init, type)
10107            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10108     {
10109       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10110          possible.  */
10111       if (TREE_CODE (type) == VECTOR_TYPE)
10112         switch (TREE_CODE (init))
10113           {
10114           case VECTOR_CST:
10115             break;
10116           case CONSTRUCTOR:
10117             if (TREE_CONSTANT (init))
10118               {
10119                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10120                 bool constant_p = true;
10121                 tree value;
10122                 unsigned HOST_WIDE_INT ix;
10123
10124                 /* Even when ctor is constant, it might contain non-*_CST
10125                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10126                    belong into VECTOR_CST nodes.  */
10127                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10128                   if (!CONSTANT_CLASS_P (value))
10129                     {
10130                       constant_p = false;
10131                       break;
10132                     }
10133
10134                 if (constant_p)
10135                   {
10136                     init = build_vector_from_ctor (type, elts);
10137                     break;
10138                   }
10139               }
10140             /* FALLTHRU */
10141
10142           default:
10143             return NULL;
10144           }
10145
10146       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10147
10148       /* If expand_expr returns a MEM, it wasn't immediate.  */
10149       gcc_assert (!rtl || !MEM_P (rtl));
10150     }
10151
10152   return rtl;
10153 }
10154
10155 /* Generate RTL for the variable DECL to represent its location.  */
10156
10157 static rtx
10158 rtl_for_decl_location (tree decl)
10159 {
10160   rtx rtl;
10161
10162   /* Here we have to decide where we are going to say the parameter "lives"
10163      (as far as the debugger is concerned).  We only have a couple of
10164      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10165
10166      DECL_RTL normally indicates where the parameter lives during most of the
10167      activation of the function.  If optimization is enabled however, this
10168      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10169      that the parameter doesn't really live anywhere (as far as the code
10170      generation parts of GCC are concerned) during most of the function's
10171      activation.  That will happen (for example) if the parameter is never
10172      referenced within the function.
10173
10174      We could just generate a location descriptor here for all non-NULL
10175      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10176      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10177      where DECL_RTL is NULL or is a pseudo-reg.
10178
10179      Note however that we can only get away with using DECL_INCOMING_RTL as
10180      a backup substitute for DECL_RTL in certain limited cases.  In cases
10181      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10182      we can be sure that the parameter was passed using the same type as it is
10183      declared to have within the function, and that its DECL_INCOMING_RTL
10184      points us to a place where a value of that type is passed.
10185
10186      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10187      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10188      because in these cases DECL_INCOMING_RTL points us to a value of some
10189      type which is *different* from the type of the parameter itself.  Thus,
10190      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10191      such cases, the debugger would end up (for example) trying to fetch a
10192      `float' from a place which actually contains the first part of a
10193      `double'.  That would lead to really incorrect and confusing
10194      output at debug-time.
10195
10196      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10197      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10198      are a couple of exceptions however.  On little-endian machines we can
10199      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10200      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10201      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10202      when (on a little-endian machine) a non-prototyped function has a
10203      parameter declared to be of type `short' or `char'.  In such cases,
10204      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10205      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10206      passed `int' value.  If the debugger then uses that address to fetch
10207      a `short' or a `char' (on a little-endian machine) the result will be
10208      the correct data, so we allow for such exceptional cases below.
10209
10210      Note that our goal here is to describe the place where the given formal
10211      parameter lives during most of the function's activation (i.e. between the
10212      end of the prologue and the start of the epilogue).  We'll do that as best
10213      as we can. Note however that if the given formal parameter is modified
10214      sometime during the execution of the function, then a stack backtrace (at
10215      debug-time) will show the function as having been called with the *new*
10216      value rather than the value which was originally passed in.  This happens
10217      rarely enough that it is not a major problem, but it *is* a problem, and
10218      I'd like to fix it.
10219
10220      A future version of dwarf2out.c may generate two additional attributes for
10221      any given DW_TAG_formal_parameter DIE which will describe the "passed
10222      type" and the "passed location" for the given formal parameter in addition
10223      to the attributes we now generate to indicate the "declared type" and the
10224      "active location" for each parameter.  This additional set of attributes
10225      could be used by debuggers for stack backtraces. Separately, note that
10226      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10227      This happens (for example) for inlined-instances of inline function formal
10228      parameters which are never referenced.  This really shouldn't be
10229      happening.  All PARM_DECL nodes should get valid non-NULL
10230      DECL_INCOMING_RTL values.  FIXME.  */
10231
10232   /* Use DECL_RTL as the "location" unless we find something better.  */
10233   rtl = DECL_RTL_IF_SET (decl);
10234
10235   /* When generating abstract instances, ignore everything except
10236      constants, symbols living in memory, and symbols living in
10237      fixed registers.  */
10238   if (! reload_completed)
10239     {
10240       if (rtl
10241           && (CONSTANT_P (rtl)
10242               || (MEM_P (rtl)
10243                   && CONSTANT_P (XEXP (rtl, 0)))
10244               || (REG_P (rtl)
10245                   && TREE_CODE (decl) == VAR_DECL
10246                   && TREE_STATIC (decl))))
10247         {
10248           rtl = targetm.delegitimize_address (rtl);
10249           return rtl;
10250         }
10251       rtl = NULL_RTX;
10252     }
10253   else if (TREE_CODE (decl) == PARM_DECL)
10254     {
10255       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10256         {
10257           tree declared_type = TREE_TYPE (decl);
10258           tree passed_type = DECL_ARG_TYPE (decl);
10259           enum machine_mode dmode = TYPE_MODE (declared_type);
10260           enum machine_mode pmode = TYPE_MODE (passed_type);
10261
10262           /* This decl represents a formal parameter which was optimized out.
10263              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10264              all cases where (rtl == NULL_RTX) just below.  */
10265           if (dmode == pmode)
10266             rtl = DECL_INCOMING_RTL (decl);
10267           else if (SCALAR_INT_MODE_P (dmode)
10268                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10269                    && DECL_INCOMING_RTL (decl))
10270             {
10271               rtx inc = DECL_INCOMING_RTL (decl);
10272               if (REG_P (inc))
10273                 rtl = inc;
10274               else if (MEM_P (inc))
10275                 {
10276                   if (BYTES_BIG_ENDIAN)
10277                     rtl = adjust_address_nv (inc, dmode,
10278                                              GET_MODE_SIZE (pmode)
10279                                              - GET_MODE_SIZE (dmode));
10280                   else
10281                     rtl = inc;
10282                 }
10283             }
10284         }
10285
10286       /* If the parm was passed in registers, but lives on the stack, then
10287          make a big endian correction if the mode of the type of the
10288          parameter is not the same as the mode of the rtl.  */
10289       /* ??? This is the same series of checks that are made in dbxout.c before
10290          we reach the big endian correction code there.  It isn't clear if all
10291          of these checks are necessary here, but keeping them all is the safe
10292          thing to do.  */
10293       else if (MEM_P (rtl)
10294                && XEXP (rtl, 0) != const0_rtx
10295                && ! CONSTANT_P (XEXP (rtl, 0))
10296                /* Not passed in memory.  */
10297                && !MEM_P (DECL_INCOMING_RTL (decl))
10298                /* Not passed by invisible reference.  */
10299                && (!REG_P (XEXP (rtl, 0))
10300                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10301                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10302 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10303                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10304 #endif
10305                      )
10306                /* Big endian correction check.  */
10307                && BYTES_BIG_ENDIAN
10308                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10309                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10310                    < UNITS_PER_WORD))
10311         {
10312           int offset = (UNITS_PER_WORD
10313                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10314
10315           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10316                              plus_constant (XEXP (rtl, 0), offset));
10317         }
10318     }
10319   else if (TREE_CODE (decl) == VAR_DECL
10320            && rtl
10321            && MEM_P (rtl)
10322            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10323            && BYTES_BIG_ENDIAN)
10324     {
10325       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10326       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10327
10328       /* If a variable is declared "register" yet is smaller than
10329          a register, then if we store the variable to memory, it
10330          looks like we're storing a register-sized value, when in
10331          fact we are not.  We need to adjust the offset of the
10332          storage location to reflect the actual value's bytes,
10333          else gdb will not be able to display it.  */
10334       if (rsize > dsize)
10335         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10336                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10337     }
10338
10339   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10340      and will have been substituted directly into all expressions that use it.
10341      C does not have such a concept, but C++ and other languages do.  */
10342   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10343     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10344
10345   if (rtl)
10346     rtl = targetm.delegitimize_address (rtl);
10347
10348   /* If we don't look past the constant pool, we risk emitting a
10349      reference to a constant pool entry that isn't referenced from
10350      code, and thus is not emitted.  */
10351   if (rtl)
10352     rtl = avoid_constant_pool_reference (rtl);
10353
10354   return rtl;
10355 }
10356
10357 /* We need to figure out what section we should use as the base for the
10358    address ranges where a given location is valid.
10359    1. If this particular DECL has a section associated with it, use that.
10360    2. If this function has a section associated with it, use that.
10361    3. Otherwise, use the text section.
10362    XXX: If you split a variable across multiple sections, we won't notice.  */
10363
10364 static const char *
10365 secname_for_decl (tree decl)
10366 {
10367   const char *secname;
10368
10369   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10370     {
10371       tree sectree = DECL_SECTION_NAME (decl);
10372       secname = TREE_STRING_POINTER (sectree);
10373     }
10374   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10375     {
10376       tree sectree = DECL_SECTION_NAME (current_function_decl);
10377       secname = TREE_STRING_POINTER (sectree);
10378     }
10379   else if (cfun && in_cold_section_p)
10380     secname = cfun->cold_section_label;
10381   else
10382     secname = text_section_label;
10383
10384   return secname;
10385 }
10386
10387 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10388    data attribute for a variable or a parameter.  We generate the
10389    DW_AT_const_value attribute only in those cases where the given variable
10390    or parameter does not have a true "location" either in memory or in a
10391    register.  This can happen (for example) when a constant is passed as an
10392    actual argument in a call to an inline function.  (It's possible that
10393    these things can crop up in other ways also.)  Note that one type of
10394    constant value which can be passed into an inlined function is a constant
10395    pointer.  This can happen for example if an actual argument in an inlined
10396    function call evaluates to a compile-time constant address.  */
10397
10398 static void
10399 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10400                                        enum dwarf_attribute attr)
10401 {
10402   rtx rtl;
10403   dw_loc_descr_ref descr;
10404   var_loc_list *loc_list;
10405   struct var_loc_node *node;
10406   if (TREE_CODE (decl) == ERROR_MARK)
10407     return;
10408
10409   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10410               || TREE_CODE (decl) == RESULT_DECL);
10411              
10412   /* See if we possibly have multiple locations for this variable.  */
10413   loc_list = lookup_decl_loc (decl);
10414
10415   /* If it truly has multiple locations, the first and last node will
10416      differ.  */
10417   if (loc_list && loc_list->first != loc_list->last)
10418     {
10419       const char *endname, *secname;
10420       dw_loc_list_ref list;
10421       rtx varloc;
10422
10423       /* Now that we know what section we are using for a base,
10424          actually construct the list of locations.
10425          The first location information is what is passed to the
10426          function that creates the location list, and the remaining
10427          locations just get added on to that list.
10428          Note that we only know the start address for a location
10429          (IE location changes), so to build the range, we use
10430          the range [current location start, next location start].
10431          This means we have to special case the last node, and generate
10432          a range of [last location start, end of function label].  */
10433
10434       node = loc_list->first;
10435       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10436       secname = secname_for_decl (decl);
10437
10438       list = new_loc_list (loc_descriptor (varloc),
10439                            node->label, node->next->label, secname, 1);
10440       node = node->next;
10441
10442       for (; node->next; node = node->next)
10443         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10444           {
10445             /* The variable has a location between NODE->LABEL and
10446                NODE->NEXT->LABEL.  */
10447             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10448             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10449                                        node->label, node->next->label, secname);
10450           }
10451
10452       /* If the variable has a location at the last label
10453          it keeps its location until the end of function.  */
10454       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10455         {
10456           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10457
10458           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10459           if (!current_function_decl)
10460             endname = text_end_label;
10461           else
10462             {
10463               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10464                                            current_function_funcdef_no);
10465               endname = ggc_strdup (label_id);
10466             }
10467           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10468                                      node->label, endname, secname);
10469         }
10470
10471       /* Finally, add the location list to the DIE, and we are done.  */
10472       add_AT_loc_list (die, attr, list);
10473       return;
10474     }
10475
10476   /* Try to get some constant RTL for this decl, and use that as the value of
10477      the location.  */
10478   
10479   rtl = rtl_for_decl_location (decl);
10480   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10481     {
10482       add_const_value_attribute (die, rtl);
10483       return;
10484     }
10485   
10486   /* If we have tried to generate the location otherwise, and it
10487      didn't work out (we wouldn't be here if we did), and we have a one entry
10488      location list, try generating a location from that.  */
10489   if (loc_list && loc_list->first)
10490     {
10491       node = loc_list->first;
10492       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10493       if (descr)
10494         {
10495           add_AT_location_description (die, attr, descr);
10496           return;
10497         }
10498     }
10499
10500   /* We couldn't get any rtl, so try directly generating the location
10501      description from the tree.  */
10502   descr = loc_descriptor_from_tree (decl);
10503   if (descr)
10504     {
10505       add_AT_location_description (die, attr, descr);
10506       return;
10507     }
10508   /* None of that worked, so it must not really have a location;
10509      try adding a constant value attribute from the DECL_INITIAL.  */
10510   tree_add_const_value_attribute (die, decl);
10511 }
10512
10513 /* If we don't have a copy of this variable in memory for some reason (such
10514    as a C++ member constant that doesn't have an out-of-line definition),
10515    we should tell the debugger about the constant value.  */
10516
10517 static void
10518 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10519 {
10520   tree init = DECL_INITIAL (decl);
10521   tree type = TREE_TYPE (decl);
10522   rtx rtl;
10523
10524   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10525     /* OK */;
10526   else
10527     return;
10528
10529   rtl = rtl_for_decl_init (init, type);
10530   if (rtl)
10531     add_const_value_attribute (var_die, rtl);
10532 }
10533
10534 /* Convert the CFI instructions for the current function into a
10535    location list.  This is used for DW_AT_frame_base when we targeting
10536    a dwarf2 consumer that does not support the dwarf3
10537    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10538    expressions.  */
10539
10540 static dw_loc_list_ref
10541 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10542 {
10543   dw_fde_ref fde;
10544   dw_loc_list_ref list, *list_tail;
10545   dw_cfi_ref cfi;
10546   dw_cfa_location last_cfa, next_cfa;
10547   const char *start_label, *last_label, *section;
10548
10549   fde = &fde_table[fde_table_in_use - 1];
10550
10551   section = secname_for_decl (current_function_decl);
10552   list_tail = &list;
10553   list = NULL;
10554
10555   next_cfa.reg = INVALID_REGNUM;
10556   next_cfa.offset = 0;
10557   next_cfa.indirect = 0;
10558   next_cfa.base_offset = 0;
10559
10560   start_label = fde->dw_fde_begin;
10561
10562   /* ??? Bald assumption that the CIE opcode list does not contain
10563      advance opcodes.  */
10564   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10565     lookup_cfa_1 (cfi, &next_cfa);
10566
10567   last_cfa = next_cfa;
10568   last_label = start_label;
10569
10570   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10571     switch (cfi->dw_cfi_opc)
10572       {
10573       case DW_CFA_set_loc:
10574       case DW_CFA_advance_loc1:
10575       case DW_CFA_advance_loc2:
10576       case DW_CFA_advance_loc4:
10577         if (!cfa_equal_p (&last_cfa, &next_cfa))
10578           {
10579             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10580                                        start_label, last_label, section,
10581                                        list == NULL);
10582
10583             list_tail = &(*list_tail)->dw_loc_next;
10584             last_cfa = next_cfa;
10585             start_label = last_label;
10586           }
10587         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10588         break;
10589
10590       case DW_CFA_advance_loc:
10591         /* The encoding is complex enough that we should never emit this.  */
10592       case DW_CFA_remember_state:
10593       case DW_CFA_restore_state:
10594         /* We don't handle these two in this function.  It would be possible
10595            if it were to be required.  */
10596         gcc_unreachable ();
10597
10598       default:
10599         lookup_cfa_1 (cfi, &next_cfa);
10600         break;
10601       }
10602
10603   if (!cfa_equal_p (&last_cfa, &next_cfa))
10604     {
10605       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10606                                  start_label, last_label, section,
10607                                  list == NULL);
10608       list_tail = &(*list_tail)->dw_loc_next;
10609       start_label = last_label;
10610     }
10611   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10612                              start_label, fde->dw_fde_end, section,
10613                              list == NULL);
10614
10615   return list;
10616 }
10617
10618 /* Compute a displacement from the "steady-state frame pointer" to the
10619    frame base (often the same as the CFA), and store it in
10620    frame_pointer_fb_offset.  OFFSET is added to the displacement
10621    before the latter is negated.  */
10622
10623 static void
10624 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10625 {
10626   rtx reg, elim;
10627
10628 #ifdef FRAME_POINTER_CFA_OFFSET
10629   reg = frame_pointer_rtx;
10630   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10631 #else
10632   reg = arg_pointer_rtx;
10633   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10634 #endif
10635
10636   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10637   if (GET_CODE (elim) == PLUS)
10638     {
10639       offset += INTVAL (XEXP (elim, 1));
10640       elim = XEXP (elim, 0);
10641     }
10642   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10643                        : stack_pointer_rtx));
10644
10645   frame_pointer_fb_offset = -offset;
10646 }
10647
10648 /* Generate a DW_AT_name attribute given some string value to be included as
10649    the value of the attribute.  */
10650
10651 static void
10652 add_name_attribute (dw_die_ref die, const char *name_string)
10653 {
10654   if (name_string != NULL && *name_string != 0)
10655     {
10656       if (demangle_name_func)
10657         name_string = (*demangle_name_func) (name_string);
10658
10659       add_AT_string (die, DW_AT_name, name_string);
10660     }
10661 }
10662
10663 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10664
10665 static void
10666 add_comp_dir_attribute (dw_die_ref die)
10667 {
10668   const char *wd = get_src_pwd ();
10669   if (wd != NULL)
10670     add_AT_string (die, DW_AT_comp_dir, wd);
10671 }
10672
10673 /* Given a tree node describing an array bound (either lower or upper) output
10674    a representation for that bound.  */
10675
10676 static void
10677 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10678 {
10679   switch (TREE_CODE (bound))
10680     {
10681     case ERROR_MARK:
10682       return;
10683
10684     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10685     case INTEGER_CST:
10686       if (! host_integerp (bound, 0)
10687           || (bound_attr == DW_AT_lower_bound
10688               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10689                   || (is_fortran () && integer_onep (bound)))))
10690         /* Use the default.  */
10691         ;
10692       else
10693         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10694       break;
10695
10696     case CONVERT_EXPR:
10697     case NOP_EXPR:
10698     case NON_LVALUE_EXPR:
10699     case VIEW_CONVERT_EXPR:
10700       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10701       break;
10702
10703     case SAVE_EXPR:
10704       break;
10705
10706     case VAR_DECL:
10707     case PARM_DECL:
10708     case RESULT_DECL:
10709       {
10710         dw_die_ref decl_die = lookup_decl_die (bound);
10711
10712         /* ??? Can this happen, or should the variable have been bound
10713            first?  Probably it can, since I imagine that we try to create
10714            the types of parameters in the order in which they exist in
10715            the list, and won't have created a forward reference to a
10716            later parameter.  */
10717         if (decl_die != NULL)
10718           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10719         break;
10720       }
10721
10722     default:
10723       {
10724         /* Otherwise try to create a stack operation procedure to
10725            evaluate the value of the array bound.  */
10726
10727         dw_die_ref ctx, decl_die;
10728         dw_loc_descr_ref loc;
10729
10730         loc = loc_descriptor_from_tree (bound);
10731         if (loc == NULL)
10732           break;
10733
10734         if (current_function_decl == 0)
10735           ctx = comp_unit_die;
10736         else
10737           ctx = lookup_decl_die (current_function_decl);
10738
10739         decl_die = new_die (DW_TAG_variable, ctx, bound);
10740         add_AT_flag (decl_die, DW_AT_artificial, 1);
10741         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10742         add_AT_loc (decl_die, DW_AT_location, loc);
10743
10744         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10745         break;
10746       }
10747     }
10748 }
10749
10750 /* Note that the block of subscript information for an array type also
10751    includes information about the element type of type given array type.  */
10752
10753 static void
10754 add_subscript_info (dw_die_ref type_die, tree type)
10755 {
10756 #ifndef MIPS_DEBUGGING_INFO
10757   unsigned dimension_number;
10758 #endif
10759   tree lower, upper;
10760   dw_die_ref subrange_die;
10761
10762   /* The GNU compilers represent multidimensional array types as sequences of
10763      one dimensional array types whose element types are themselves array
10764      types.  Here we squish that down, so that each multidimensional array
10765      type gets only one array_type DIE in the Dwarf debugging info. The draft
10766      Dwarf specification say that we are allowed to do this kind of
10767      compression in C (because there is no difference between an array or
10768      arrays and a multidimensional array in C) but for other source languages
10769      (e.g. Ada) we probably shouldn't do this.  */
10770
10771   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10772      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10773      We work around this by disabling this feature.  See also
10774      gen_array_type_die.  */
10775 #ifndef MIPS_DEBUGGING_INFO
10776   for (dimension_number = 0;
10777        TREE_CODE (type) == ARRAY_TYPE;
10778        type = TREE_TYPE (type), dimension_number++)
10779 #endif
10780     {
10781       tree domain = TYPE_DOMAIN (type);
10782
10783       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10784          and (in GNU C only) variable bounds.  Handle all three forms
10785          here.  */
10786       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10787       if (domain)
10788         {
10789           /* We have an array type with specified bounds.  */
10790           lower = TYPE_MIN_VALUE (domain);
10791           upper = TYPE_MAX_VALUE (domain);
10792
10793           /* Define the index type.  */
10794           if (TREE_TYPE (domain))
10795             {
10796               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10797                  TREE_TYPE field.  We can't emit debug info for this
10798                  because it is an unnamed integral type.  */
10799               if (TREE_CODE (domain) == INTEGER_TYPE
10800                   && TYPE_NAME (domain) == NULL_TREE
10801                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10802                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10803                 ;
10804               else
10805                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10806                                     type_die);
10807             }
10808
10809           /* ??? If upper is NULL, the array has unspecified length,
10810              but it does have a lower bound.  This happens with Fortran
10811                dimension arr(N:*)
10812              Since the debugger is definitely going to need to know N
10813              to produce useful results, go ahead and output the lower
10814              bound solo, and hope the debugger can cope.  */
10815
10816           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10817           if (upper)
10818             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10819         }
10820
10821       /* Otherwise we have an array type with an unspecified length.  The
10822          DWARF-2 spec does not say how to handle this; let's just leave out the
10823          bounds.  */
10824     }
10825 }
10826
10827 static void
10828 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10829 {
10830   unsigned size;
10831
10832   switch (TREE_CODE (tree_node))
10833     {
10834     case ERROR_MARK:
10835       size = 0;
10836       break;
10837     case ENUMERAL_TYPE:
10838     case RECORD_TYPE:
10839     case UNION_TYPE:
10840     case QUAL_UNION_TYPE:
10841       size = int_size_in_bytes (tree_node);
10842       break;
10843     case FIELD_DECL:
10844       /* For a data member of a struct or union, the DW_AT_byte_size is
10845          generally given as the number of bytes normally allocated for an
10846          object of the *declared* type of the member itself.  This is true
10847          even for bit-fields.  */
10848       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10849       break;
10850     default:
10851       gcc_unreachable ();
10852     }
10853
10854   /* Note that `size' might be -1 when we get to this point.  If it is, that
10855      indicates that the byte size of the entity in question is variable.  We
10856      have no good way of expressing this fact in Dwarf at the present time.
10857      GCC/35998: Avoid passing negative sizes to Dtrace and gdb.  */
10858   add_AT_unsigned (die, DW_AT_byte_size, (size != (unsigned)-1 ? size : 0));
10859 }
10860
10861 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10862    which specifies the distance in bits from the highest order bit of the
10863    "containing object" for the bit-field to the highest order bit of the
10864    bit-field itself.
10865
10866    For any given bit-field, the "containing object" is a hypothetical object
10867    (of some integral or enum type) within which the given bit-field lives.  The
10868    type of this hypothetical "containing object" is always the same as the
10869    declared type of the individual bit-field itself.  The determination of the
10870    exact location of the "containing object" for a bit-field is rather
10871    complicated.  It's handled by the `field_byte_offset' function (above).
10872
10873    Note that it is the size (in bytes) of the hypothetical "containing object"
10874    which will be given in the DW_AT_byte_size attribute for this bit-field.
10875    (See `byte_size_attribute' above).  */
10876
10877 static inline void
10878 add_bit_offset_attribute (dw_die_ref die, tree decl)
10879 {
10880   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10881   tree type = DECL_BIT_FIELD_TYPE (decl);
10882   HOST_WIDE_INT bitpos_int;
10883   HOST_WIDE_INT highest_order_object_bit_offset;
10884   HOST_WIDE_INT highest_order_field_bit_offset;
10885   HOST_WIDE_INT unsigned bit_offset;
10886
10887   /* Must be a field and a bit field.  */
10888   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10889
10890   /* We can't yet handle bit-fields whose offsets are variable, so if we
10891      encounter such things, just return without generating any attribute
10892      whatsoever.  Likewise for variable or too large size.  */
10893   if (! host_integerp (bit_position (decl), 0)
10894       || ! host_integerp (DECL_SIZE (decl), 1))
10895     return;
10896
10897   bitpos_int = int_bit_position (decl);
10898
10899   /* Note that the bit offset is always the distance (in bits) from the
10900      highest-order bit of the "containing object" to the highest-order bit of
10901      the bit-field itself.  Since the "high-order end" of any object or field
10902      is different on big-endian and little-endian machines, the computation
10903      below must take account of these differences.  */
10904   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10905   highest_order_field_bit_offset = bitpos_int;
10906
10907   if (! BYTES_BIG_ENDIAN)
10908     {
10909       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10910       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10911     }
10912
10913   bit_offset
10914     = (! BYTES_BIG_ENDIAN
10915        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10916        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10917
10918   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10919 }
10920
10921 /* For a FIELD_DECL node which represents a bit field, output an attribute
10922    which specifies the length in bits of the given field.  */
10923
10924 static inline void
10925 add_bit_size_attribute (dw_die_ref die, tree decl)
10926 {
10927   /* Must be a field and a bit field.  */
10928   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10929               && DECL_BIT_FIELD_TYPE (decl));
10930
10931   if (host_integerp (DECL_SIZE (decl), 1))
10932     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10933 }
10934
10935 /* If the compiled language is ANSI C, then add a 'prototyped'
10936    attribute, if arg types are given for the parameters of a function.  */
10937
10938 static inline void
10939 add_prototyped_attribute (dw_die_ref die, tree func_type)
10940 {
10941   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10942       && TYPE_ARG_TYPES (func_type) != NULL)
10943     add_AT_flag (die, DW_AT_prototyped, 1);
10944 }
10945
10946 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10947    by looking in either the type declaration or object declaration
10948    equate table.  */
10949
10950 static inline void
10951 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10952 {
10953   dw_die_ref origin_die = NULL;
10954
10955   if (TREE_CODE (origin) != FUNCTION_DECL)
10956     {
10957       /* We may have gotten separated from the block for the inlined
10958          function, if we're in an exception handler or some such; make
10959          sure that the abstract function has been written out.
10960
10961          Doing this for nested functions is wrong, however; functions are
10962          distinct units, and our context might not even be inline.  */
10963       tree fn = origin;
10964
10965       if (TYPE_P (fn))
10966         fn = TYPE_STUB_DECL (fn);
10967       
10968       fn = decl_function_context (fn);
10969       if (fn)
10970         dwarf2out_abstract_function (fn);
10971     }
10972
10973   if (DECL_P (origin))
10974     origin_die = lookup_decl_die (origin);
10975   else if (TYPE_P (origin))
10976     origin_die = lookup_type_die (origin);
10977
10978   /* XXX: Functions that are never lowered don't always have correct block
10979      trees (in the case of java, they simply have no block tree, in some other
10980      languages).  For these functions, there is nothing we can really do to
10981      output correct debug info for inlined functions in all cases.  Rather
10982      than die, we'll just produce deficient debug info now, in that we will
10983      have variables without a proper abstract origin.  In the future, when all
10984      functions are lowered, we should re-add a gcc_assert (origin_die)
10985      here.  */
10986
10987   if (origin_die)
10988       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10989 }
10990
10991 /* We do not currently support the pure_virtual attribute.  */
10992
10993 static inline void
10994 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10995 {
10996   if (DECL_VINDEX (func_decl))
10997     {
10998       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10999
11000       if (host_integerp (DECL_VINDEX (func_decl), 0))
11001         add_AT_loc (die, DW_AT_vtable_elem_location,
11002                     new_loc_descr (DW_OP_constu,
11003                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11004                                    0));
11005
11006       /* GNU extension: Record what type this method came from originally.  */
11007       if (debug_info_level > DINFO_LEVEL_TERSE)
11008         add_AT_die_ref (die, DW_AT_containing_type,
11009                         lookup_type_die (DECL_CONTEXT (func_decl)));
11010     }
11011 }
11012 \f
11013 /* Add source coordinate attributes for the given decl.  */
11014
11015 static void
11016 add_src_coords_attributes (dw_die_ref die, tree decl)
11017 {
11018   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11019
11020   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11021   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11022 }
11023
11024 /* Add a DW_AT_name attribute and source coordinate attribute for the
11025    given decl, but only if it actually has a name.  */
11026
11027 static void
11028 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11029 {
11030   tree decl_name;
11031
11032   decl_name = DECL_NAME (decl);
11033   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11034     {
11035       add_name_attribute (die, dwarf2_name (decl, 0));
11036       if (! DECL_ARTIFICIAL (decl))
11037         add_src_coords_attributes (die, decl);
11038
11039       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11040           && TREE_PUBLIC (decl)
11041           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11042           && !DECL_ABSTRACT (decl)
11043           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11044         add_AT_string (die, DW_AT_MIPS_linkage_name,
11045                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11046     }
11047
11048 #ifdef VMS_DEBUGGING_INFO
11049   /* Get the function's name, as described by its RTL.  This may be different
11050      from the DECL_NAME name used in the source file.  */
11051   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11052     {
11053       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11054                    XEXP (DECL_RTL (decl), 0));
11055       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11056     }
11057 #endif
11058 }
11059
11060 /* Push a new declaration scope.  */
11061
11062 static void
11063 push_decl_scope (tree scope)
11064 {
11065   VEC_safe_push (tree, gc, decl_scope_table, scope);
11066 }
11067
11068 /* Pop a declaration scope.  */
11069
11070 static inline void
11071 pop_decl_scope (void)
11072 {
11073   VEC_pop (tree, decl_scope_table);
11074 }
11075
11076 /* Return the DIE for the scope that immediately contains this type.
11077    Non-named types get global scope.  Named types nested in other
11078    types get their containing scope if it's open, or global scope
11079    otherwise.  All other types (i.e. function-local named types) get
11080    the current active scope.  */
11081
11082 static dw_die_ref
11083 scope_die_for (tree t, dw_die_ref context_die)
11084 {
11085   dw_die_ref scope_die = NULL;
11086   tree containing_scope;
11087   int i;
11088
11089   /* Non-types always go in the current scope.  */
11090   gcc_assert (TYPE_P (t));
11091
11092   containing_scope = TYPE_CONTEXT (t);
11093
11094   /* Use the containing namespace if it was passed in (for a declaration).  */
11095   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11096     {
11097       if (context_die == lookup_decl_die (containing_scope))
11098         /* OK */;
11099       else
11100         containing_scope = NULL_TREE;
11101     }
11102
11103   /* Ignore function type "scopes" from the C frontend.  They mean that
11104      a tagged type is local to a parmlist of a function declarator, but
11105      that isn't useful to DWARF.  */
11106   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11107     containing_scope = NULL_TREE;
11108
11109   if (containing_scope == NULL_TREE)
11110     scope_die = comp_unit_die;
11111   else if (TYPE_P (containing_scope))
11112     {
11113       /* For types, we can just look up the appropriate DIE.  But
11114          first we check to see if we're in the middle of emitting it
11115          so we know where the new DIE should go.  */
11116       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11117         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11118           break;
11119
11120       if (i < 0)
11121         {
11122           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11123                       || TREE_ASM_WRITTEN (containing_scope));
11124
11125           /* If none of the current dies are suitable, we get file scope.  */
11126           scope_die = comp_unit_die;
11127         }
11128       else
11129         scope_die = lookup_type_die (containing_scope);
11130     }
11131   else
11132     scope_die = context_die;
11133
11134   return scope_die;
11135 }
11136
11137 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11138
11139 static inline int
11140 local_scope_p (dw_die_ref context_die)
11141 {
11142   for (; context_die; context_die = context_die->die_parent)
11143     if (context_die->die_tag == DW_TAG_inlined_subroutine
11144         || context_die->die_tag == DW_TAG_subprogram)
11145       return 1;
11146
11147   return 0;
11148 }
11149
11150 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11151    whether or not to treat a DIE in this context as a declaration.  */
11152
11153 static inline int
11154 class_or_namespace_scope_p (dw_die_ref context_die)
11155 {
11156   return (context_die
11157           && (context_die->die_tag == DW_TAG_structure_type
11158               || context_die->die_tag == DW_TAG_union_type
11159               || context_die->die_tag == DW_TAG_namespace));
11160 }
11161
11162 /* Many forms of DIEs require a "type description" attribute.  This
11163    routine locates the proper "type descriptor" die for the type given
11164    by 'type', and adds a DW_AT_type attribute below the given die.  */
11165
11166 static void
11167 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11168                     int decl_volatile, dw_die_ref context_die)
11169 {
11170   enum tree_code code  = TREE_CODE (type);
11171   dw_die_ref type_die  = NULL;
11172
11173   /* ??? If this type is an unnamed subrange type of an integral or
11174      floating-point type, use the inner type.  This is because we have no
11175      support for unnamed types in base_type_die.  This can happen if this is
11176      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11177   if ((code == INTEGER_TYPE || code == REAL_TYPE)
11178       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11179     type = TREE_TYPE (type), code = TREE_CODE (type);
11180
11181   if (code == ERROR_MARK
11182       /* Handle a special case.  For functions whose return type is void, we
11183          generate *no* type attribute.  (Note that no object may have type
11184          `void', so this only applies to function return types).  */
11185       || code == VOID_TYPE)
11186     return;
11187
11188   type_die = modified_type_die (type,
11189                                 decl_const || TYPE_READONLY (type),
11190                                 decl_volatile || TYPE_VOLATILE (type),
11191                                 context_die);
11192
11193   if (type_die != NULL)
11194     add_AT_die_ref (object_die, DW_AT_type, type_die);
11195 }
11196
11197 /* Given an object die, add the calling convention attribute for the
11198    function call type.  */
11199 static void
11200 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11201 {
11202   enum dwarf_calling_convention value = DW_CC_normal;
11203
11204   value = targetm.dwarf_calling_convention (type);
11205
11206   /* Only add the attribute if the backend requests it, and
11207      is not DW_CC_normal.  */
11208   if (value && (value != DW_CC_normal))
11209     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11210 }
11211
11212 /* Given a tree pointer to a struct, class, union, or enum type node, return
11213    a pointer to the (string) tag name for the given type, or zero if the type
11214    was declared without a tag.  */
11215
11216 static const char *
11217 type_tag (tree type)
11218 {
11219   const char *name = 0;
11220
11221   if (TYPE_NAME (type) != 0)
11222     {
11223       tree t = 0;
11224
11225       /* Find the IDENTIFIER_NODE for the type name.  */
11226       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11227         t = TYPE_NAME (type);
11228
11229       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11230          a TYPE_DECL node, regardless of whether or not a `typedef' was
11231          involved.  */
11232       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11233                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11234         t = DECL_NAME (TYPE_NAME (type));
11235
11236       /* Now get the name as a string, or invent one.  */
11237       if (t != 0)
11238         name = IDENTIFIER_POINTER (t);
11239     }
11240
11241   return (name == 0 || *name == '\0') ? 0 : name;
11242 }
11243
11244 /* Return the type associated with a data member, make a special check
11245    for bit field types.  */
11246
11247 static inline tree
11248 member_declared_type (tree member)
11249 {
11250   return (DECL_BIT_FIELD_TYPE (member)
11251           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11252 }
11253
11254 /* Get the decl's label, as described by its RTL. This may be different
11255    from the DECL_NAME name used in the source file.  */
11256
11257 #if 0
11258 static const char *
11259 decl_start_label (tree decl)
11260 {
11261   rtx x;
11262   const char *fnname;
11263
11264   x = DECL_RTL (decl);
11265   gcc_assert (MEM_P (x));
11266
11267   x = XEXP (x, 0);
11268   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11269
11270   fnname = XSTR (x, 0);
11271   return fnname;
11272 }
11273 #endif
11274 \f
11275 /* These routines generate the internal representation of the DIE's for
11276    the compilation unit.  Debugging information is collected by walking
11277    the declaration trees passed in from dwarf2out_decl().  */
11278
11279 static void
11280 gen_array_type_die (tree type, dw_die_ref context_die)
11281 {
11282   dw_die_ref scope_die = scope_die_for (type, context_die);
11283   dw_die_ref array_die;
11284   tree element_type;
11285
11286   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11287      the inner array type comes before the outer array type.  Thus we must
11288      call gen_type_die before we call new_die.  See below also.  */
11289 #ifdef MIPS_DEBUGGING_INFO
11290   gen_type_die (TREE_TYPE (type), context_die);
11291 #endif
11292
11293   array_die = new_die (DW_TAG_array_type, scope_die, type);
11294   add_name_attribute (array_die, type_tag (type));
11295   equate_type_number_to_die (type, array_die);
11296
11297   if (TREE_CODE (type) == VECTOR_TYPE)
11298     {
11299       /* The frontend feeds us a representation for the vector as a struct
11300          containing an array.  Pull out the array type.  */
11301       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11302       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11303     }
11304
11305 #if 0
11306   /* We default the array ordering.  SDB will probably do
11307      the right things even if DW_AT_ordering is not present.  It's not even
11308      an issue until we start to get into multidimensional arrays anyway.  If
11309      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11310      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11311      and when we find out that we need to put these in, we will only do so
11312      for multidimensional arrays.  */
11313   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11314 #endif
11315
11316 #ifdef MIPS_DEBUGGING_INFO
11317   /* The SGI compilers handle arrays of unknown bound by setting
11318      AT_declaration and not emitting any subrange DIEs.  */
11319   if (! TYPE_DOMAIN (type))
11320     add_AT_flag (array_die, DW_AT_declaration, 1);
11321   else
11322 #endif
11323     add_subscript_info (array_die, type);
11324
11325   /* Add representation of the type of the elements of this array type.  */
11326   element_type = TREE_TYPE (type);
11327
11328   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11329      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11330      We work around this by disabling this feature.  See also
11331      add_subscript_info.  */
11332 #ifndef MIPS_DEBUGGING_INFO
11333   while (TREE_CODE (element_type) == ARRAY_TYPE)
11334     element_type = TREE_TYPE (element_type);
11335
11336   gen_type_die (element_type, context_die);
11337 #endif
11338
11339   add_type_attribute (array_die, element_type, 0, 0, context_die);
11340
11341   if (get_AT (array_die, DW_AT_name))
11342     add_pubtype (type, array_die);
11343 }
11344
11345 #if 0
11346 static void
11347 gen_entry_point_die (tree decl, dw_die_ref context_die)
11348 {
11349   tree origin = decl_ultimate_origin (decl);
11350   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11351
11352   if (origin != NULL)
11353     add_abstract_origin_attribute (decl_die, origin);
11354   else
11355     {
11356       add_name_and_src_coords_attributes (decl_die, decl);
11357       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11358                           0, 0, context_die);
11359     }
11360
11361   if (DECL_ABSTRACT (decl))
11362     equate_decl_number_to_die (decl, decl_die);
11363   else
11364     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11365 }
11366 #endif
11367
11368 /* Walk through the list of incomplete types again, trying once more to
11369    emit full debugging info for them.  */
11370
11371 static void
11372 retry_incomplete_types (void)
11373 {
11374   int i;
11375
11376   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11377     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11378 }
11379
11380 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11381
11382 static void
11383 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11384 {
11385   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11386
11387   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11388      be incomplete and such types are not marked.  */
11389   add_abstract_origin_attribute (type_die, type);
11390 }
11391
11392 /* Generate a DIE to represent an inlined instance of a structure type.  */
11393
11394 static void
11395 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11396 {
11397   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11398
11399   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11400      be incomplete and such types are not marked.  */
11401   add_abstract_origin_attribute (type_die, type);
11402 }
11403
11404 /* Generate a DIE to represent an inlined instance of a union type.  */
11405
11406 static void
11407 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11408 {
11409   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11410
11411   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11412      be incomplete and such types are not marked.  */
11413   add_abstract_origin_attribute (type_die, type);
11414 }
11415
11416 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11417    include all of the information about the enumeration values also. Each
11418    enumerated type name/value is listed as a child of the enumerated type
11419    DIE.  */
11420
11421 static dw_die_ref
11422 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11423 {
11424   dw_die_ref type_die = lookup_type_die (type);
11425
11426   if (type_die == NULL)
11427     {
11428       type_die = new_die (DW_TAG_enumeration_type,
11429                           scope_die_for (type, context_die), type);
11430       equate_type_number_to_die (type, type_die);
11431       add_name_attribute (type_die, type_tag (type));
11432     }
11433   else if (! TYPE_SIZE (type))
11434     return type_die;
11435   else
11436     remove_AT (type_die, DW_AT_declaration);
11437
11438   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11439      given enum type is incomplete, do not generate the DW_AT_byte_size
11440      attribute or the DW_AT_element_list attribute.  */
11441   if (TYPE_SIZE (type))
11442     {
11443       tree link;
11444
11445       TREE_ASM_WRITTEN (type) = 1;
11446       add_byte_size_attribute (type_die, type);
11447       if (TYPE_STUB_DECL (type) != NULL_TREE)
11448         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11449
11450       /* If the first reference to this type was as the return type of an
11451          inline function, then it may not have a parent.  Fix this now.  */
11452       if (type_die->die_parent == NULL)
11453         add_child_die (scope_die_for (type, context_die), type_die);
11454
11455       for (link = TYPE_VALUES (type);
11456            link != NULL; link = TREE_CHAIN (link))
11457         {
11458           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11459           tree value = TREE_VALUE (link);
11460
11461           add_name_attribute (enum_die,
11462                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11463
11464           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11465             /* DWARF2 does not provide a way of indicating whether or
11466                not enumeration constants are signed or unsigned.  GDB
11467                always assumes the values are signed, so we output all
11468                values as if they were signed.  That means that
11469                enumeration constants with very large unsigned values
11470                will appear to have negative values in the debugger.  */
11471             add_AT_int (enum_die, DW_AT_const_value,
11472                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11473         }
11474     }
11475   else
11476     add_AT_flag (type_die, DW_AT_declaration, 1);
11477
11478   if (get_AT (type_die, DW_AT_name))
11479     add_pubtype (type, type_die);
11480
11481   return type_die;
11482 }
11483
11484 /* Generate a DIE to represent either a real live formal parameter decl or to
11485    represent just the type of some formal parameter position in some function
11486    type.
11487
11488    Note that this routine is a bit unusual because its argument may be a
11489    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11490    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11491    node.  If it's the former then this function is being called to output a
11492    DIE to represent a formal parameter object (or some inlining thereof).  If
11493    it's the latter, then this function is only being called to output a
11494    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11495    argument type of some subprogram type.  */
11496
11497 static dw_die_ref
11498 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11499 {
11500   dw_die_ref parm_die
11501     = new_die (DW_TAG_formal_parameter, context_die, node);
11502   tree origin;
11503
11504   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11505     {
11506     case tcc_declaration:
11507       origin = decl_ultimate_origin (node);
11508       if (origin != NULL)
11509         add_abstract_origin_attribute (parm_die, origin);
11510       else
11511         {
11512           add_name_and_src_coords_attributes (parm_die, node);
11513           add_type_attribute (parm_die, TREE_TYPE (node),
11514                               TREE_READONLY (node),
11515                               TREE_THIS_VOLATILE (node),
11516                               context_die);
11517           if (DECL_ARTIFICIAL (node))
11518             add_AT_flag (parm_die, DW_AT_artificial, 1);
11519         }
11520
11521       equate_decl_number_to_die (node, parm_die);
11522       if (! DECL_ABSTRACT (node))
11523         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11524
11525       break;
11526
11527     case tcc_type:
11528       /* We were called with some kind of a ..._TYPE node.  */
11529       add_type_attribute (parm_die, node, 0, 0, context_die);
11530       break;
11531
11532     default:
11533       gcc_unreachable ();
11534     }
11535
11536   return parm_die;
11537 }
11538
11539 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11540    at the end of an (ANSI prototyped) formal parameters list.  */
11541
11542 static void
11543 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11544 {
11545   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11546 }
11547
11548 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11549    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11550    parameters as specified in some function type specification (except for
11551    those which appear as part of a function *definition*).  */
11552
11553 static void
11554 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11555 {
11556   tree link;
11557   tree formal_type = NULL;
11558   tree first_parm_type;
11559   tree arg;
11560
11561   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11562     {
11563       arg = DECL_ARGUMENTS (function_or_method_type);
11564       function_or_method_type = TREE_TYPE (function_or_method_type);
11565     }
11566   else
11567     arg = NULL_TREE;
11568
11569   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11570
11571   /* Make our first pass over the list of formal parameter types and output a
11572      DW_TAG_formal_parameter DIE for each one.  */
11573   for (link = first_parm_type; link; )
11574     {
11575       dw_die_ref parm_die;
11576
11577       formal_type = TREE_VALUE (link);
11578       if (formal_type == void_type_node)
11579         break;
11580
11581       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11582       parm_die = gen_formal_parameter_die (formal_type, context_die);
11583       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11584            && link == first_parm_type)
11585           || (arg && DECL_ARTIFICIAL (arg)))
11586         add_AT_flag (parm_die, DW_AT_artificial, 1);
11587
11588       link = TREE_CHAIN (link);
11589       if (arg)
11590         arg = TREE_CHAIN (arg);
11591     }
11592
11593   /* If this function type has an ellipsis, add a
11594      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11595   if (formal_type != void_type_node)
11596     gen_unspecified_parameters_die (function_or_method_type, context_die);
11597
11598   /* Make our second (and final) pass over the list of formal parameter types
11599      and output DIEs to represent those types (as necessary).  */
11600   for (link = TYPE_ARG_TYPES (function_or_method_type);
11601        link && TREE_VALUE (link);
11602        link = TREE_CHAIN (link))
11603     gen_type_die (TREE_VALUE (link), context_die);
11604 }
11605
11606 /* We want to generate the DIE for TYPE so that we can generate the
11607    die for MEMBER, which has been defined; we will need to refer back
11608    to the member declaration nested within TYPE.  If we're trying to
11609    generate minimal debug info for TYPE, processing TYPE won't do the
11610    trick; we need to attach the member declaration by hand.  */
11611
11612 static void
11613 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11614 {
11615   gen_type_die (type, context_die);
11616
11617   /* If we're trying to avoid duplicate debug info, we may not have
11618      emitted the member decl for this function.  Emit it now.  */
11619   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11620       && ! lookup_decl_die (member))
11621     {
11622       dw_die_ref type_die;
11623       gcc_assert (!decl_ultimate_origin (member));
11624
11625       push_decl_scope (type);
11626       type_die = lookup_type_die (type);
11627       if (TREE_CODE (member) == FUNCTION_DECL)
11628         gen_subprogram_die (member, type_die);
11629       else if (TREE_CODE (member) == FIELD_DECL)
11630         {
11631           /* Ignore the nameless fields that are used to skip bits but handle
11632              C++ anonymous unions and structs.  */
11633           if (DECL_NAME (member) != NULL_TREE
11634               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11635               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11636             {
11637               gen_type_die (member_declared_type (member), type_die);
11638               gen_field_die (member, type_die);
11639             }
11640         }
11641       else
11642         gen_variable_die (member, type_die);
11643
11644       pop_decl_scope ();
11645     }
11646 }
11647
11648 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11649    may later generate inlined and/or out-of-line instances of.  */
11650
11651 static void
11652 dwarf2out_abstract_function (tree decl)
11653 {
11654   dw_die_ref old_die;
11655   tree save_fn;
11656   struct function *save_cfun;
11657   tree context;
11658   int was_abstract = DECL_ABSTRACT (decl);
11659
11660   /* Make sure we have the actual abstract inline, not a clone.  */
11661   decl = DECL_ORIGIN (decl);
11662
11663   old_die = lookup_decl_die (decl);
11664   if (old_die && get_AT (old_die, DW_AT_inline))
11665     /* We've already generated the abstract instance.  */
11666     return;
11667
11668   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11669      we don't get confused by DECL_ABSTRACT.  */
11670   if (debug_info_level > DINFO_LEVEL_TERSE)
11671     {
11672       context = decl_class_context (decl);
11673       if (context)
11674         gen_type_die_for_member
11675           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11676     }
11677
11678   /* Pretend we've just finished compiling this function.  */
11679   save_fn = current_function_decl;
11680   save_cfun = cfun;
11681   current_function_decl = decl;
11682   cfun = DECL_STRUCT_FUNCTION (decl);
11683
11684   set_decl_abstract_flags (decl, 1);
11685   dwarf2out_decl (decl);
11686   if (! was_abstract)
11687     set_decl_abstract_flags (decl, 0);
11688
11689   current_function_decl = save_fn;
11690   cfun = save_cfun;
11691 }
11692
11693 /* Helper function of premark_used_types() which gets called through
11694    htab_traverse_resize().
11695
11696    Marks the DIE of a given type in *SLOT as perennial, so it never gets
11697    marked as unused by prune_unused_types.  */
11698 static int
11699 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11700 {
11701   tree type;
11702   dw_die_ref die;
11703
11704   type = *slot;
11705   die = lookup_type_die (type);
11706   if (die != NULL)
11707     die->die_perennial_p = 1;
11708   return 1;
11709 }
11710
11711 /* Mark all members of used_types_hash as perennial.  */
11712 static void
11713 premark_used_types (void)
11714 {
11715   if (cfun && cfun->used_types_hash)
11716     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11717 }
11718
11719 /* Generate a DIE to represent a declared function (either file-scope or
11720    block-local).  */
11721
11722 static void
11723 gen_subprogram_die (tree decl, dw_die_ref context_die)
11724 {
11725   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11726   tree origin = decl_ultimate_origin (decl);
11727   dw_die_ref subr_die;
11728   tree fn_arg_types;
11729   tree outer_scope;
11730   dw_die_ref old_die = lookup_decl_die (decl);
11731   int declaration = (current_function_decl != decl
11732                      || class_or_namespace_scope_p (context_die));
11733
11734   premark_used_types ();
11735
11736   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11737      started to generate the abstract instance of an inline, decided to output
11738      its containing class, and proceeded to emit the declaration of the inline
11739      from the member list for the class.  If so, DECLARATION takes priority;
11740      we'll get back to the abstract instance when done with the class.  */
11741
11742   /* The class-scope declaration DIE must be the primary DIE.  */
11743   if (origin && declaration && class_or_namespace_scope_p (context_die))
11744     {
11745       origin = NULL;
11746       gcc_assert (!old_die);
11747     }
11748
11749   /* Now that the C++ front end lazily declares artificial member fns, we
11750      might need to retrofit the declaration into its class.  */
11751   if (!declaration && !origin && !old_die
11752       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11753       && !class_or_namespace_scope_p (context_die)
11754       && debug_info_level > DINFO_LEVEL_TERSE)
11755     old_die = force_decl_die (decl);
11756
11757   if (origin != NULL)
11758     {
11759       gcc_assert (!declaration || local_scope_p (context_die));
11760
11761       /* Fixup die_parent for the abstract instance of a nested
11762          inline function.  */
11763       if (old_die && old_die->die_parent == NULL)
11764         add_child_die (context_die, old_die);
11765
11766       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11767       add_abstract_origin_attribute (subr_die, origin);
11768     }
11769   else if (old_die)
11770     {
11771       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11772       struct dwarf_file_data * file_index = lookup_filename (s.file);
11773
11774       if (!get_AT_flag (old_die, DW_AT_declaration)
11775           /* We can have a normal definition following an inline one in the
11776              case of redefinition of GNU C extern inlines.
11777              It seems reasonable to use AT_specification in this case.  */
11778           && !get_AT (old_die, DW_AT_inline))
11779         {
11780           /* Detect and ignore this case, where we are trying to output
11781              something we have already output.  */
11782           return;
11783         }
11784
11785       /* If the definition comes from the same place as the declaration,
11786          maybe use the old DIE.  We always want the DIE for this function
11787          that has the *_pc attributes to be under comp_unit_die so the
11788          debugger can find it.  We also need to do this for abstract
11789          instances of inlines, since the spec requires the out-of-line copy
11790          to have the same parent.  For local class methods, this doesn't
11791          apply; we just use the old DIE.  */
11792       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11793           && (DECL_ARTIFICIAL (decl)
11794               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
11795                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11796                       == (unsigned) s.line))))
11797         {
11798           subr_die = old_die;
11799
11800           /* Clear out the declaration attribute and the formal parameters.
11801              Do not remove all children, because it is possible that this
11802              declaration die was forced using force_decl_die(). In such
11803              cases die that forced declaration die (e.g. TAG_imported_module)
11804              is one of the children that we do not want to remove.  */
11805           remove_AT (subr_die, DW_AT_declaration);
11806           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11807         }
11808       else
11809         {
11810           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11811           add_AT_specification (subr_die, old_die);
11812           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11813             add_AT_file (subr_die, DW_AT_decl_file, file_index);
11814           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11815             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
11816         }
11817     }
11818   else
11819     {
11820       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11821
11822       if (TREE_PUBLIC (decl))
11823         add_AT_flag (subr_die, DW_AT_external, 1);
11824
11825       add_name_and_src_coords_attributes (subr_die, decl);
11826       if (debug_info_level > DINFO_LEVEL_TERSE)
11827         {
11828           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11829           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11830                               0, 0, context_die);
11831         }
11832
11833       add_pure_or_virtual_attribute (subr_die, decl);
11834       if (DECL_ARTIFICIAL (decl))
11835         add_AT_flag (subr_die, DW_AT_artificial, 1);
11836
11837       if (TREE_PROTECTED (decl))
11838         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11839       else if (TREE_PRIVATE (decl))
11840         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11841     }
11842
11843   if (declaration)
11844     {
11845       if (!old_die || !get_AT (old_die, DW_AT_inline))
11846         {
11847           add_AT_flag (subr_die, DW_AT_declaration, 1);
11848
11849           /* The first time we see a member function, it is in the context of
11850              the class to which it belongs.  We make sure of this by emitting
11851              the class first.  The next time is the definition, which is
11852              handled above.  The two may come from the same source text.
11853
11854              Note that force_decl_die() forces function declaration die. It is
11855              later reused to represent definition.  */
11856           equate_decl_number_to_die (decl, subr_die);
11857         }
11858     }
11859   else if (DECL_ABSTRACT (decl))
11860     {
11861       if (DECL_DECLARED_INLINE_P (decl))
11862         {
11863           if (cgraph_function_possibly_inlined_p (decl))
11864             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11865           else
11866             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11867         }
11868       else
11869         {
11870           if (cgraph_function_possibly_inlined_p (decl))
11871             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11872           else
11873             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11874         }
11875
11876       equate_decl_number_to_die (decl, subr_die);
11877     }
11878   else if (!DECL_EXTERNAL (decl))
11879     {
11880       HOST_WIDE_INT cfa_fb_offset;
11881
11882       if (!old_die || !get_AT (old_die, DW_AT_inline))
11883         equate_decl_number_to_die (decl, subr_die);
11884
11885       if (!flag_reorder_blocks_and_partition)
11886         {
11887           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11888                                        current_function_funcdef_no);
11889           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11890           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11891                                        current_function_funcdef_no);
11892           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11893           
11894           add_pubname (decl, subr_die);
11895           add_arange (decl, subr_die);
11896         }
11897       else
11898         {  /* Do nothing for now; maybe need to duplicate die, one for
11899               hot section and ond for cold section, then use the hot/cold
11900               section begin/end labels to generate the aranges...  */
11901           /*
11902             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11903             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11904             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11905             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11906
11907             add_pubname (decl, subr_die);
11908             add_arange (decl, subr_die);
11909             add_arange (decl, subr_die);
11910            */
11911         }
11912
11913 #ifdef MIPS_DEBUGGING_INFO
11914       /* Add a reference to the FDE for this routine.  */
11915       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11916 #endif
11917
11918       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11919
11920       /* We define the "frame base" as the function's CFA.  This is more
11921          convenient for several reasons: (1) It's stable across the prologue
11922          and epilogue, which makes it better than just a frame pointer,
11923          (2) With dwarf3, there exists a one-byte encoding that allows us
11924          to reference the .debug_frame data by proxy, but failing that,
11925          (3) We can at least reuse the code inspection and interpretation
11926          code that determines the CFA position at various points in the
11927          function.  */
11928       /* ??? Use some command-line or configury switch to enable the use
11929          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11930          consumers that understand it; fall back to "pure" dwarf2 and
11931          convert the CFA data into a location list.  */
11932       {
11933         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11934         if (list->dw_loc_next)
11935           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11936         else
11937           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11938       }
11939
11940       /* Compute a displacement from the "steady-state frame pointer" to
11941          the CFA.  The former is what all stack slots and argument slots
11942          will reference in the rtl; the later is what we've told the 
11943          debugger about.  We'll need to adjust all frame_base references
11944          by this displacement.  */
11945       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11946
11947       if (cfun->static_chain_decl)
11948         add_AT_location_description (subr_die, DW_AT_static_link,
11949                  loc_descriptor_from_tree (cfun->static_chain_decl));
11950     }
11951
11952   /* Now output descriptions of the arguments for this function. This gets
11953      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11954      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11955      `...' at the end of the formal parameter list.  In order to find out if
11956      there was a trailing ellipsis or not, we must instead look at the type
11957      associated with the FUNCTION_DECL.  This will be a node of type
11958      FUNCTION_TYPE. If the chain of type nodes hanging off of this
11959      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11960      an ellipsis at the end.  */
11961
11962   /* In the case where we are describing a mere function declaration, all we
11963      need to do here (and all we *can* do here) is to describe the *types* of
11964      its formal parameters.  */
11965   if (debug_info_level <= DINFO_LEVEL_TERSE)
11966     ;
11967   else if (declaration)
11968     gen_formal_types_die (decl, subr_die);
11969   else
11970     {
11971       /* Generate DIEs to represent all known formal parameters.  */
11972       tree arg_decls = DECL_ARGUMENTS (decl);
11973       tree parm;
11974
11975       /* When generating DIEs, generate the unspecified_parameters DIE
11976          instead if we come across the arg "__builtin_va_alist" */
11977       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11978         if (TREE_CODE (parm) == PARM_DECL)
11979           {
11980             if (DECL_NAME (parm)
11981                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11982                             "__builtin_va_alist"))
11983               gen_unspecified_parameters_die (parm, subr_die);
11984             else
11985               gen_decl_die (parm, subr_die);
11986           }
11987
11988       /* Decide whether we need an unspecified_parameters DIE at the end.
11989          There are 2 more cases to do this for: 1) the ansi ... declaration -
11990          this is detectable when the end of the arg list is not a
11991          void_type_node 2) an unprototyped function declaration (not a
11992          definition).  This just means that we have no info about the
11993          parameters at all.  */
11994       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11995       if (fn_arg_types != NULL)
11996         {
11997           /* This is the prototyped case, check for....  */
11998           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11999             gen_unspecified_parameters_die (decl, subr_die);
12000         }
12001       else if (DECL_INITIAL (decl) == NULL_TREE)
12002         gen_unspecified_parameters_die (decl, subr_die);
12003     }
12004
12005   /* Output Dwarf info for all of the stuff within the body of the function
12006      (if it has one - it may be just a declaration).  */
12007   outer_scope = DECL_INITIAL (decl);
12008
12009   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12010      a function.  This BLOCK actually represents the outermost binding contour
12011      for the function, i.e. the contour in which the function's formal
12012      parameters and labels get declared. Curiously, it appears that the front
12013      end doesn't actually put the PARM_DECL nodes for the current function onto
12014      the BLOCK_VARS list for this outer scope, but are strung off of the
12015      DECL_ARGUMENTS list for the function instead.
12016
12017      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12018      the LABEL_DECL nodes for the function however, and we output DWARF info
12019      for those in decls_for_scope.  Just within the `outer_scope' there will be
12020      a BLOCK node representing the function's outermost pair of curly braces,
12021      and any blocks used for the base and member initializers of a C++
12022      constructor function.  */
12023   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12024     {
12025       /* Emit a DW_TAG_variable DIE for a named return value.  */
12026       if (DECL_NAME (DECL_RESULT (decl)))
12027         gen_decl_die (DECL_RESULT (decl), subr_die);
12028
12029       current_function_has_inlines = 0;
12030       decls_for_scope (outer_scope, subr_die, 0);
12031
12032 #if 0 && defined (MIPS_DEBUGGING_INFO)
12033       if (current_function_has_inlines)
12034         {
12035           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12036           if (! comp_unit_has_inlines)
12037             {
12038               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12039               comp_unit_has_inlines = 1;
12040             }
12041         }
12042 #endif
12043     }
12044   /* Add the calling convention attribute if requested.  */
12045   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12046
12047 }
12048
12049 /* Generate a DIE to represent a declared data object.  */
12050
12051 static void
12052 gen_variable_die (tree decl, dw_die_ref context_die)
12053 {
12054   tree origin = decl_ultimate_origin (decl);
12055   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12056
12057   dw_die_ref old_die = lookup_decl_die (decl);
12058   int declaration = (DECL_EXTERNAL (decl)
12059                      /* If DECL is COMDAT and has not actually been
12060                         emitted, we cannot take its address; there
12061                         might end up being no definition anywhere in
12062                         the program.  For example, consider the C++
12063                         test case:
12064
12065                           template <class T>
12066                           struct S { static const int i = 7; };
12067
12068                           template <class T>
12069                           const int S<T>::i;
12070
12071                           int f() { return S<int>::i; }
12072                           
12073                         Here, S<int>::i is not DECL_EXTERNAL, but no
12074                         definition is required, so the compiler will
12075                         not emit a definition.  */  
12076                      || (TREE_CODE (decl) == VAR_DECL
12077                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12078                      || class_or_namespace_scope_p (context_die));
12079
12080   if (origin != NULL)
12081     add_abstract_origin_attribute (var_die, origin);
12082
12083   /* Loop unrolling can create multiple blocks that refer to the same
12084      static variable, so we must test for the DW_AT_declaration flag.
12085
12086      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12087      copy decls and set the DECL_ABSTRACT flag on them instead of
12088      sharing them.
12089
12090      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12091
12092      ??? The declare_in_namespace support causes us to get two DIEs for one
12093      variable, both of which are declarations.  We want to avoid considering
12094      one to be a specification, so we must test that this DIE is not a
12095      declaration.  */
12096   else if (old_die && TREE_STATIC (decl) && ! declaration
12097            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12098     {
12099       /* This is a definition of a C++ class level static.  */
12100       add_AT_specification (var_die, old_die);
12101       if (DECL_NAME (decl))
12102         {
12103           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12104           struct dwarf_file_data * file_index = lookup_filename (s.file);
12105
12106           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12107             add_AT_file (var_die, DW_AT_decl_file, file_index);
12108
12109           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12110
12111             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12112         }
12113     }
12114   else
12115     {
12116       add_name_and_src_coords_attributes (var_die, decl);
12117       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12118                           TREE_THIS_VOLATILE (decl), context_die);
12119
12120       if (TREE_PUBLIC (decl))
12121         add_AT_flag (var_die, DW_AT_external, 1);
12122
12123       if (DECL_ARTIFICIAL (decl))
12124         add_AT_flag (var_die, DW_AT_artificial, 1);
12125
12126       if (TREE_PROTECTED (decl))
12127         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12128       else if (TREE_PRIVATE (decl))
12129         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12130     }
12131
12132   if (declaration)
12133     add_AT_flag (var_die, DW_AT_declaration, 1);
12134
12135   if (DECL_ABSTRACT (decl) || declaration)
12136     equate_decl_number_to_die (decl, var_die);
12137
12138   if (! declaration && ! DECL_ABSTRACT (decl))
12139     {
12140       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12141       add_pubname (decl, var_die);
12142     }
12143   else
12144     tree_add_const_value_attribute (var_die, decl);
12145 }
12146
12147 /* Generate a DIE to represent a label identifier.  */
12148
12149 static void
12150 gen_label_die (tree decl, dw_die_ref context_die)
12151 {
12152   tree origin = decl_ultimate_origin (decl);
12153   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12154   rtx insn;
12155   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12156
12157   if (origin != NULL)
12158     add_abstract_origin_attribute (lbl_die, origin);
12159   else
12160     add_name_and_src_coords_attributes (lbl_die, decl);
12161
12162   if (DECL_ABSTRACT (decl))
12163     equate_decl_number_to_die (decl, lbl_die);
12164   else
12165     {
12166       insn = DECL_RTL_IF_SET (decl);
12167
12168       /* Deleted labels are programmer specified labels which have been
12169          eliminated because of various optimizations.  We still emit them
12170          here so that it is possible to put breakpoints on them.  */
12171       if (insn
12172           && (LABEL_P (insn)
12173               || ((NOTE_P (insn)
12174                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
12175         {
12176           /* When optimization is enabled (via -O) some parts of the compiler
12177              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12178              represent source-level labels which were explicitly declared by
12179              the user.  This really shouldn't be happening though, so catch
12180              it if it ever does happen.  */
12181           gcc_assert (!INSN_DELETED_P (insn));
12182
12183           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12184           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12185         }
12186     }
12187 }
12188
12189 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12190    attributes to the DIE for a block STMT, to describe where the inlined
12191    function was called from.  This is similar to add_src_coords_attributes.  */
12192
12193 static inline void
12194 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12195 {
12196   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12197
12198   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12199   add_AT_unsigned (die, DW_AT_call_line, s.line);
12200 }
12201
12202 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12203    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12204
12205 static inline void
12206 add_high_low_attributes (tree stmt, dw_die_ref die)
12207 {
12208   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12209
12210   if (BLOCK_FRAGMENT_CHAIN (stmt))
12211     {
12212       tree chain;
12213
12214       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12215
12216       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12217       do
12218         {
12219           add_ranges (chain);
12220           chain = BLOCK_FRAGMENT_CHAIN (chain);
12221         }
12222       while (chain);
12223       add_ranges (NULL);
12224     }
12225   else
12226     {
12227       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12228                                    BLOCK_NUMBER (stmt));
12229       add_AT_lbl_id (die, DW_AT_low_pc, label);
12230       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12231                                    BLOCK_NUMBER (stmt));
12232       add_AT_lbl_id (die, DW_AT_high_pc, label);
12233     }
12234 }
12235
12236 /* Generate a DIE for a lexical block.  */
12237
12238 static void
12239 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12240 {
12241   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12242
12243   if (! BLOCK_ABSTRACT (stmt))
12244     add_high_low_attributes (stmt, stmt_die);
12245
12246   decls_for_scope (stmt, stmt_die, depth);
12247 }
12248
12249 /* Generate a DIE for an inlined subprogram.  */
12250
12251 static void
12252 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12253 {
12254   tree decl = block_ultimate_origin (stmt);
12255
12256   /* Emit info for the abstract instance first, if we haven't yet.  We
12257      must emit this even if the block is abstract, otherwise when we
12258      emit the block below (or elsewhere), we may end up trying to emit
12259      a die whose origin die hasn't been emitted, and crashing.  */
12260   dwarf2out_abstract_function (decl);
12261
12262   if (! BLOCK_ABSTRACT (stmt))
12263     {
12264       dw_die_ref subr_die
12265         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12266
12267       add_abstract_origin_attribute (subr_die, decl);
12268       add_high_low_attributes (stmt, subr_die);
12269       add_call_src_coords_attributes (stmt, subr_die);
12270
12271       decls_for_scope (stmt, subr_die, depth);
12272       current_function_has_inlines = 1;
12273     }
12274   else
12275     /* We may get here if we're the outer block of function A that was
12276        inlined into function B that was inlined into function C.  When
12277        generating debugging info for C, dwarf2out_abstract_function(B)
12278        would mark all inlined blocks as abstract, including this one.
12279        So, we wouldn't (and shouldn't) expect labels to be generated
12280        for this one.  Instead, just emit debugging info for
12281        declarations within the block.  This is particularly important
12282        in the case of initializers of arguments passed from B to us:
12283        if they're statement expressions containing declarations, we
12284        wouldn't generate dies for their abstract variables, and then,
12285        when generating dies for the real variables, we'd die (pun
12286        intended :-)  */
12287     gen_lexical_block_die (stmt, context_die, depth);
12288 }
12289
12290 /* Generate a DIE for a field in a record, or structure.  */
12291
12292 static void
12293 gen_field_die (tree decl, dw_die_ref context_die)
12294 {
12295   dw_die_ref decl_die;
12296
12297   if (TREE_TYPE (decl) == error_mark_node)
12298     return;
12299
12300   decl_die = new_die (DW_TAG_member, context_die, decl);
12301   add_name_and_src_coords_attributes (decl_die, decl);
12302   add_type_attribute (decl_die, member_declared_type (decl),
12303                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12304                       context_die);
12305
12306   if (DECL_BIT_FIELD_TYPE (decl))
12307     {
12308       add_byte_size_attribute (decl_die, decl);
12309       add_bit_size_attribute (decl_die, decl);
12310       add_bit_offset_attribute (decl_die, decl);
12311     }
12312
12313   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12314     add_data_member_location_attribute (decl_die, decl);
12315
12316   if (DECL_ARTIFICIAL (decl))
12317     add_AT_flag (decl_die, DW_AT_artificial, 1);
12318
12319   if (TREE_PROTECTED (decl))
12320     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12321   else if (TREE_PRIVATE (decl))
12322     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12323
12324   /* Equate decl number to die, so that we can look up this decl later on.  */
12325   equate_decl_number_to_die (decl, decl_die);
12326 }
12327
12328 #if 0
12329 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12330    Use modified_type_die instead.
12331    We keep this code here just in case these types of DIEs may be needed to
12332    represent certain things in other languages (e.g. Pascal) someday.  */
12333
12334 static void
12335 gen_pointer_type_die (tree type, dw_die_ref context_die)
12336 {
12337   dw_die_ref ptr_die
12338     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12339
12340   equate_type_number_to_die (type, ptr_die);
12341   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12342   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12343 }
12344
12345 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12346    Use modified_type_die instead.
12347    We keep this code here just in case these types of DIEs may be needed to
12348    represent certain things in other languages (e.g. Pascal) someday.  */
12349
12350 static void
12351 gen_reference_type_die (tree type, dw_die_ref context_die)
12352 {
12353   dw_die_ref ref_die
12354     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12355
12356   equate_type_number_to_die (type, ref_die);
12357   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12358   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12359 }
12360 #endif
12361
12362 /* Generate a DIE for a pointer to a member type.  */
12363
12364 static void
12365 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12366 {
12367   dw_die_ref ptr_die
12368     = new_die (DW_TAG_ptr_to_member_type,
12369                scope_die_for (type, context_die), type);
12370
12371   equate_type_number_to_die (type, ptr_die);
12372   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12373                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12374   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12375 }
12376
12377 /* Generate the DIE for the compilation unit.  */
12378
12379 static dw_die_ref
12380 gen_compile_unit_die (const char *filename)
12381 {
12382   dw_die_ref die;
12383   char producer[250];
12384   const char *language_string = lang_hooks.name;
12385   int language;
12386
12387   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12388
12389   if (filename)
12390     {
12391       add_name_attribute (die, filename);
12392       /* Don't add cwd for <built-in>.  */
12393       if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12394         add_comp_dir_attribute (die);
12395     }
12396
12397   sprintf (producer, "%s %s", language_string, version_string);
12398
12399 #ifdef MIPS_DEBUGGING_INFO
12400   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12401      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12402      not appear in the producer string, the debugger reaches the conclusion
12403      that the object file is stripped and has no debugging information.
12404      To get the MIPS/SGI debugger to believe that there is debugging
12405      information in the object file, we add a -g to the producer string.  */
12406   if (debug_info_level > DINFO_LEVEL_TERSE)
12407     strcat (producer, " -g");
12408 #endif
12409
12410   add_AT_string (die, DW_AT_producer, producer);
12411
12412   if (strcmp (language_string, "GNU C++") == 0)
12413     language = DW_LANG_C_plus_plus;
12414   else if (strcmp (language_string, "GNU Ada") == 0)
12415     language = DW_LANG_Ada95;
12416   else if (strcmp (language_string, "GNU F77") == 0)
12417     language = DW_LANG_Fortran77;
12418   else if (strcmp (language_string, "GNU F95") == 0)
12419     language = DW_LANG_Fortran95;
12420   else if (strcmp (language_string, "GNU Pascal") == 0)
12421     language = DW_LANG_Pascal83;
12422   else if (strcmp (language_string, "GNU Java") == 0)
12423     language = DW_LANG_Java;
12424   else if (strcmp (language_string, "GNU Objective-C") == 0)
12425     language = DW_LANG_ObjC;
12426   else if (strcmp (language_string, "GNU Objective-C++") == 0)
12427     language = DW_LANG_ObjC_plus_plus;
12428   else
12429     language = DW_LANG_C89;
12430
12431   add_AT_unsigned (die, DW_AT_language, language);
12432   return die;
12433 }
12434
12435 /* Generate the DIE for a base class.  */
12436
12437 static void
12438 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12439 {
12440   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12441
12442   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12443   add_data_member_location_attribute (die, binfo);
12444
12445   if (BINFO_VIRTUAL_P (binfo))
12446     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12447
12448   if (access == access_public_node)
12449     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12450   else if (access == access_protected_node)
12451     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12452 }
12453
12454 /* Generate a DIE for a class member.  */
12455
12456 static void
12457 gen_member_die (tree type, dw_die_ref context_die)
12458 {
12459   tree member;
12460   tree binfo = TYPE_BINFO (type);
12461   dw_die_ref child;
12462
12463   /* If this is not an incomplete type, output descriptions of each of its
12464      members. Note that as we output the DIEs necessary to represent the
12465      members of this record or union type, we will also be trying to output
12466      DIEs to represent the *types* of those members. However the `type'
12467      function (above) will specifically avoid generating type DIEs for member
12468      types *within* the list of member DIEs for this (containing) type except
12469      for those types (of members) which are explicitly marked as also being
12470      members of this (containing) type themselves.  The g++ front- end can
12471      force any given type to be treated as a member of some other (containing)
12472      type by setting the TYPE_CONTEXT of the given (member) type to point to
12473      the TREE node representing the appropriate (containing) type.  */
12474
12475   /* First output info about the base classes.  */
12476   if (binfo)
12477     {
12478       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12479       int i;
12480       tree base;
12481
12482       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12483         gen_inheritance_die (base,
12484                              (accesses ? VEC_index (tree, accesses, i)
12485                               : access_public_node), context_die);
12486     }
12487
12488   /* Now output info about the data members and type members.  */
12489   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12490     {
12491       /* If we thought we were generating minimal debug info for TYPE
12492          and then changed our minds, some of the member declarations
12493          may have already been defined.  Don't define them again, but
12494          do put them in the right order.  */
12495
12496       child = lookup_decl_die (member);
12497       if (child)
12498         splice_child_die (context_die, child);
12499       else
12500         gen_decl_die (member, context_die);
12501     }
12502
12503   /* Now output info about the function members (if any).  */
12504   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12505     {
12506       /* Don't include clones in the member list.  */
12507       if (DECL_ABSTRACT_ORIGIN (member))
12508         continue;
12509
12510       child = lookup_decl_die (member);
12511       if (child)
12512         splice_child_die (context_die, child);
12513       else
12514         gen_decl_die (member, context_die);
12515     }
12516 }
12517
12518 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12519    is set, we pretend that the type was never defined, so we only get the
12520    member DIEs needed by later specification DIEs.  */
12521
12522 static void
12523 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12524                                 enum debug_info_usage usage)
12525 {
12526   dw_die_ref type_die = lookup_type_die (type);
12527   dw_die_ref scope_die = 0;
12528   int nested = 0;
12529   int complete = (TYPE_SIZE (type)
12530                   && (! TYPE_STUB_DECL (type)
12531                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12532   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12533   complete = complete && should_emit_struct_debug (type, usage);
12534
12535   if (type_die && ! complete)
12536     return;
12537
12538   if (TYPE_CONTEXT (type) != NULL_TREE
12539       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12540           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12541     nested = 1;
12542
12543   scope_die = scope_die_for (type, context_die);
12544
12545   if (! type_die || (nested && scope_die == comp_unit_die))
12546     /* First occurrence of type or toplevel definition of nested class.  */
12547     {
12548       dw_die_ref old_die = type_die;
12549
12550       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12551                           ? DW_TAG_structure_type : DW_TAG_union_type,
12552                           scope_die, type);
12553       equate_type_number_to_die (type, type_die);
12554       if (old_die)
12555         add_AT_specification (type_die, old_die);
12556       else
12557         add_name_attribute (type_die, type_tag (type));
12558     }
12559   else
12560     remove_AT (type_die, DW_AT_declaration);
12561
12562   /* If this type has been completed, then give it a byte_size attribute and
12563      then give a list of members.  */
12564   if (complete && !ns_decl)
12565     {
12566       /* Prevent infinite recursion in cases where the type of some member of
12567          this type is expressed in terms of this type itself.  */
12568       TREE_ASM_WRITTEN (type) = 1;
12569       add_byte_size_attribute (type_die, type);
12570       if (TYPE_STUB_DECL (type) != NULL_TREE)
12571         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12572
12573       /* If the first reference to this type was as the return type of an
12574          inline function, then it may not have a parent.  Fix this now.  */
12575       if (type_die->die_parent == NULL)
12576         add_child_die (scope_die, type_die);
12577
12578       push_decl_scope (type);
12579       gen_member_die (type, type_die);
12580       pop_decl_scope ();
12581
12582       /* GNU extension: Record what type our vtable lives in.  */
12583       if (TYPE_VFIELD (type))
12584         {
12585           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12586
12587           gen_type_die (vtype, context_die);
12588           add_AT_die_ref (type_die, DW_AT_containing_type,
12589                           lookup_type_die (vtype));
12590         }
12591     }
12592   else
12593     {
12594       add_AT_flag (type_die, DW_AT_declaration, 1);
12595
12596       /* We don't need to do this for function-local types.  */
12597       if (TYPE_STUB_DECL (type)
12598           && ! decl_function_context (TYPE_STUB_DECL (type)))
12599         VEC_safe_push (tree, gc, incomplete_types, type);
12600     }
12601
12602   if (get_AT (type_die, DW_AT_name))
12603     add_pubtype (type, type_die);
12604 }
12605
12606 /* Generate a DIE for a subroutine _type_.  */
12607
12608 static void
12609 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12610 {
12611   tree return_type = TREE_TYPE (type);
12612   dw_die_ref subr_die
12613     = new_die (DW_TAG_subroutine_type,
12614                scope_die_for (type, context_die), type);
12615
12616   equate_type_number_to_die (type, subr_die);
12617   add_prototyped_attribute (subr_die, type);
12618   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12619   gen_formal_types_die (type, subr_die);
12620
12621   if (get_AT (subr_die, DW_AT_name))
12622     add_pubtype (type, subr_die);
12623 }
12624
12625 /* Generate a DIE for a type definition.  */
12626
12627 static void
12628 gen_typedef_die (tree decl, dw_die_ref context_die)
12629 {
12630   dw_die_ref type_die;
12631   tree origin;
12632
12633   if (TREE_ASM_WRITTEN (decl))
12634     return;
12635
12636   TREE_ASM_WRITTEN (decl) = 1;
12637   type_die = new_die (DW_TAG_typedef, context_die, decl);
12638   origin = decl_ultimate_origin (decl);
12639   if (origin != NULL)
12640     add_abstract_origin_attribute (type_die, origin);
12641   else
12642     {
12643       tree type;
12644
12645       add_name_and_src_coords_attributes (type_die, decl);
12646       if (DECL_ORIGINAL_TYPE (decl))
12647         {
12648           type = DECL_ORIGINAL_TYPE (decl);
12649
12650           gcc_assert (type != TREE_TYPE (decl));
12651           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12652         }
12653       else
12654         type = TREE_TYPE (decl);
12655
12656       add_type_attribute (type_die, type, TREE_READONLY (decl),
12657                           TREE_THIS_VOLATILE (decl), context_die);
12658     }
12659
12660   if (DECL_ABSTRACT (decl))
12661     equate_decl_number_to_die (decl, type_die);
12662
12663   if (get_AT (type_die, DW_AT_name))
12664     add_pubtype (decl, type_die);
12665 }
12666
12667 /* Generate a type description DIE.  */
12668
12669 static void
12670 gen_type_die_with_usage (tree type, dw_die_ref context_die,
12671                                 enum debug_info_usage usage)
12672 {
12673   int need_pop;
12674
12675   if (type == NULL_TREE || type == error_mark_node)
12676     return;
12677
12678   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12679       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12680     {
12681       if (TREE_ASM_WRITTEN (type))
12682         return;
12683
12684       /* Prevent broken recursion; we can't hand off to the same type.  */
12685       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12686
12687       TREE_ASM_WRITTEN (type) = 1;
12688       gen_decl_die (TYPE_NAME (type), context_die);
12689       return;
12690     }
12691
12692   /* We are going to output a DIE to represent the unqualified version
12693      of this type (i.e. without any const or volatile qualifiers) so
12694      get the main variant (i.e. the unqualified version) of this type
12695      now.  (Vectors are special because the debugging info is in the
12696      cloned type itself).  */
12697   if (TREE_CODE (type) != VECTOR_TYPE)
12698     type = type_main_variant (type);
12699
12700   if (TREE_ASM_WRITTEN (type))
12701     return;
12702
12703   switch (TREE_CODE (type))
12704     {
12705     case ERROR_MARK:
12706       break;
12707
12708     case POINTER_TYPE:
12709     case REFERENCE_TYPE:
12710       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12711          ensures that the gen_type_die recursion will terminate even if the
12712          type is recursive.  Recursive types are possible in Ada.  */
12713       /* ??? We could perhaps do this for all types before the switch
12714          statement.  */
12715       TREE_ASM_WRITTEN (type) = 1;
12716
12717       /* For these types, all that is required is that we output a DIE (or a
12718          set of DIEs) to represent the "basis" type.  */
12719       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12720                                 DINFO_USAGE_IND_USE);
12721       break;
12722
12723     case OFFSET_TYPE:
12724       /* This code is used for C++ pointer-to-data-member types.
12725          Output a description of the relevant class type.  */
12726       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12727                                         DINFO_USAGE_IND_USE);
12728
12729       /* Output a description of the type of the object pointed to.  */
12730       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12731                                         DINFO_USAGE_IND_USE);
12732
12733       /* Now output a DIE to represent this pointer-to-data-member type
12734          itself.  */
12735       gen_ptr_to_mbr_type_die (type, context_die);
12736       break;
12737
12738     case FUNCTION_TYPE:
12739       /* Force out return type (in case it wasn't forced out already).  */
12740       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12741                                         DINFO_USAGE_DIR_USE);
12742       gen_subroutine_type_die (type, context_die);
12743       break;
12744
12745     case METHOD_TYPE:
12746       /* Force out return type (in case it wasn't forced out already).  */
12747       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12748                                         DINFO_USAGE_DIR_USE);
12749       gen_subroutine_type_die (type, context_die);
12750       break;
12751
12752     case ARRAY_TYPE:
12753       gen_array_type_die (type, context_die);
12754       break;
12755
12756     case VECTOR_TYPE:
12757       gen_array_type_die (type, context_die);
12758       break;
12759
12760     case ENUMERAL_TYPE:
12761     case RECORD_TYPE:
12762     case UNION_TYPE:
12763     case QUAL_UNION_TYPE:
12764       /* If this is a nested type whose containing class hasn't been written
12765          out yet, writing it out will cover this one, too.  This does not apply
12766          to instantiations of member class templates; they need to be added to
12767          the containing class as they are generated.  FIXME: This hurts the
12768          idea of combining type decls from multiple TUs, since we can't predict
12769          what set of template instantiations we'll get.  */
12770       if (TYPE_CONTEXT (type)
12771           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12772           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12773         {
12774           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
12775
12776           if (TREE_ASM_WRITTEN (type))
12777             return;
12778
12779           /* If that failed, attach ourselves to the stub.  */
12780           push_decl_scope (TYPE_CONTEXT (type));
12781           context_die = lookup_type_die (TYPE_CONTEXT (type));
12782           need_pop = 1;
12783         }
12784       else
12785         {
12786           declare_in_namespace (type, context_die);
12787           need_pop = 0;
12788         }
12789
12790       if (TREE_CODE (type) == ENUMERAL_TYPE)
12791         {
12792           /* This might have been written out by the call to
12793              declare_in_namespace.  */
12794           if (!TREE_ASM_WRITTEN (type))
12795             gen_enumeration_type_die (type, context_die);
12796         }
12797       else
12798         gen_struct_or_union_type_die (type, context_die, usage);
12799
12800       if (need_pop)
12801         pop_decl_scope ();
12802
12803       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12804          it up if it is ever completed.  gen_*_type_die will set it for us
12805          when appropriate.  */
12806       return;
12807
12808     case VOID_TYPE:
12809     case INTEGER_TYPE:
12810     case REAL_TYPE:
12811     case COMPLEX_TYPE:
12812     case BOOLEAN_TYPE:
12813       /* No DIEs needed for fundamental types.  */
12814       break;
12815
12816     case LANG_TYPE:
12817       /* No Dwarf representation currently defined.  */
12818       break;
12819
12820     default:
12821       gcc_unreachable ();
12822     }
12823
12824   TREE_ASM_WRITTEN (type) = 1;
12825 }
12826
12827 static void
12828 gen_type_die (tree type, dw_die_ref context_die)
12829 {
12830   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12831 }
12832
12833 /* Generate a DIE for a tagged type instantiation.  */
12834
12835 static void
12836 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12837 {
12838   if (type == NULL_TREE || type == error_mark_node)
12839     return;
12840
12841   /* We are going to output a DIE to represent the unqualified version of
12842      this type (i.e. without any const or volatile qualifiers) so make sure
12843      that we have the main variant (i.e. the unqualified version) of this
12844      type now.  */
12845   gcc_assert (type == type_main_variant (type));
12846
12847   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12848      an instance of an unresolved type.  */
12849
12850   switch (TREE_CODE (type))
12851     {
12852     case ERROR_MARK:
12853       break;
12854
12855     case ENUMERAL_TYPE:
12856       gen_inlined_enumeration_type_die (type, context_die);
12857       break;
12858
12859     case RECORD_TYPE:
12860       gen_inlined_structure_type_die (type, context_die);
12861       break;
12862
12863     case UNION_TYPE:
12864     case QUAL_UNION_TYPE:
12865       gen_inlined_union_type_die (type, context_die);
12866       break;
12867
12868     default:
12869       gcc_unreachable ();
12870     }
12871 }
12872
12873 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12874    things which are local to the given block.  */
12875
12876 static void
12877 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12878 {
12879   int must_output_die = 0;
12880   tree origin;
12881   tree decl;
12882   enum tree_code origin_code;
12883
12884   /* Ignore blocks that are NULL.  */
12885   if (stmt == NULL_TREE)
12886     return;
12887
12888   /* If the block is one fragment of a non-contiguous block, do not
12889      process the variables, since they will have been done by the
12890      origin block.  Do process subblocks.  */
12891   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12892     {
12893       tree sub;
12894
12895       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12896         gen_block_die (sub, context_die, depth + 1);
12897
12898       return;
12899     }
12900
12901   /* Determine the "ultimate origin" of this block.  This block may be an
12902      inlined instance of an inlined instance of inline function, so we have
12903      to trace all of the way back through the origin chain to find out what
12904      sort of node actually served as the original seed for the creation of
12905      the current block.  */
12906   origin = block_ultimate_origin (stmt);
12907   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12908
12909   /* Determine if we need to output any Dwarf DIEs at all to represent this
12910      block.  */
12911   if (origin_code == FUNCTION_DECL)
12912     /* The outer scopes for inlinings *must* always be represented.  We
12913        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12914     must_output_die = 1;
12915   else
12916     {
12917       /* In the case where the current block represents an inlining of the
12918          "body block" of an inline function, we must *NOT* output any DIE for
12919          this block because we have already output a DIE to represent the whole
12920          inlined function scope and the "body block" of any function doesn't
12921          really represent a different scope according to ANSI C rules.  So we
12922          check here to make sure that this block does not represent a "body
12923          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12924       if (! is_body_block (origin ? origin : stmt))
12925         {
12926           /* Determine if this block directly contains any "significant"
12927              local declarations which we will need to output DIEs for.  */
12928           if (debug_info_level > DINFO_LEVEL_TERSE)
12929             /* We are not in terse mode so *any* local declaration counts
12930                as being a "significant" one.  */
12931             must_output_die = (BLOCK_VARS (stmt) != NULL 
12932                                && (TREE_USED (stmt) 
12933                                    || TREE_ASM_WRITTEN (stmt)
12934                                    || BLOCK_ABSTRACT (stmt)));
12935           else
12936             /* We are in terse mode, so only local (nested) function
12937                definitions count as "significant" local declarations.  */
12938             for (decl = BLOCK_VARS (stmt);
12939                  decl != NULL; decl = TREE_CHAIN (decl))
12940               if (TREE_CODE (decl) == FUNCTION_DECL
12941                   && DECL_INITIAL (decl))
12942                 {
12943                   must_output_die = 1;
12944                   break;
12945                 }
12946         }
12947     }
12948
12949   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12950      DIE for any block which contains no significant local declarations at
12951      all.  Rather, in such cases we just call `decls_for_scope' so that any
12952      needed Dwarf info for any sub-blocks will get properly generated. Note
12953      that in terse mode, our definition of what constitutes a "significant"
12954      local declaration gets restricted to include only inlined function
12955      instances and local (nested) function definitions.  */
12956   if (must_output_die)
12957     {
12958       if (origin_code == FUNCTION_DECL)
12959         gen_inlined_subroutine_die (stmt, context_die, depth);
12960       else
12961         gen_lexical_block_die (stmt, context_die, depth);
12962     }
12963   else
12964     decls_for_scope (stmt, context_die, depth);
12965 }
12966
12967 /* Generate all of the decls declared within a given scope and (recursively)
12968    all of its sub-blocks.  */
12969
12970 static void
12971 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12972 {
12973   tree decl;
12974   tree subblocks;
12975
12976   /* Ignore NULL blocks.  */
12977   if (stmt == NULL_TREE)
12978     return;
12979
12980   if (TREE_USED (stmt))
12981     {
12982       /* Output the DIEs to represent all of the data objects and typedefs
12983          declared directly within this block but not within any nested
12984          sub-blocks.  Also, nested function and tag DIEs have been
12985          generated with a parent of NULL; fix that up now.  */
12986       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12987         {
12988           dw_die_ref die;
12989           
12990           if (TREE_CODE (decl) == FUNCTION_DECL)
12991             die = lookup_decl_die (decl);
12992           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12993             die = lookup_type_die (TREE_TYPE (decl));
12994           else
12995             die = NULL;
12996           
12997           if (die != NULL && die->die_parent == NULL)
12998             add_child_die (context_die, die);
12999           /* Do not produce debug information for static variables since
13000              these might be optimized out.  We are called for these later
13001              in cgraph_varpool_analyze_pending_decls. */
13002           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13003             ;
13004           else
13005             gen_decl_die (decl, context_die);
13006         }
13007     }
13008
13009   /* If we're at -g1, we're not interested in subblocks.  */
13010   if (debug_info_level <= DINFO_LEVEL_TERSE)
13011     return;
13012
13013   /* Output the DIEs to represent all sub-blocks (and the items declared
13014      therein) of this block.  */
13015   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13016        subblocks != NULL;
13017        subblocks = BLOCK_CHAIN (subblocks))
13018     gen_block_die (subblocks, context_die, depth + 1);
13019 }
13020
13021 /* Is this a typedef we can avoid emitting?  */
13022
13023 static inline int
13024 is_redundant_typedef (tree decl)
13025 {
13026   if (TYPE_DECL_IS_STUB (decl))
13027     return 1;
13028
13029   if (DECL_ARTIFICIAL (decl)
13030       && DECL_CONTEXT (decl)
13031       && is_tagged_type (DECL_CONTEXT (decl))
13032       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13033       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13034     /* Also ignore the artificial member typedef for the class name.  */
13035     return 1;
13036
13037   return 0;
13038 }
13039
13040 /* Returns the DIE for decl.  A DIE will always be returned.  */
13041
13042 static dw_die_ref
13043 force_decl_die (tree decl)
13044 {
13045   dw_die_ref decl_die;
13046   unsigned saved_external_flag;
13047   tree save_fn = NULL_TREE;
13048   decl_die = lookup_decl_die (decl);
13049   if (!decl_die)
13050     {
13051       dw_die_ref context_die;
13052       tree decl_context = DECL_CONTEXT (decl);
13053       if (decl_context)
13054         {
13055           /* Find die that represents this context.  */
13056           if (TYPE_P (decl_context))
13057             context_die = force_type_die (decl_context);
13058           else
13059             context_die = force_decl_die (decl_context);
13060         }
13061       else
13062         context_die = comp_unit_die;
13063
13064       decl_die = lookup_decl_die (decl);
13065       if (decl_die)
13066         return decl_die;
13067
13068       switch (TREE_CODE (decl))
13069         {
13070         case FUNCTION_DECL:
13071           /* Clear current_function_decl, so that gen_subprogram_die thinks
13072              that this is a declaration. At this point, we just want to force
13073              declaration die.  */
13074           save_fn = current_function_decl;
13075           current_function_decl = NULL_TREE;
13076           gen_subprogram_die (decl, context_die);
13077           current_function_decl = save_fn;
13078           break;
13079
13080         case VAR_DECL:
13081           /* Set external flag to force declaration die. Restore it after
13082            gen_decl_die() call.  */
13083           saved_external_flag = DECL_EXTERNAL (decl);
13084           DECL_EXTERNAL (decl) = 1;
13085           gen_decl_die (decl, context_die);
13086           DECL_EXTERNAL (decl) = saved_external_flag;
13087           break;
13088
13089         case NAMESPACE_DECL:
13090           dwarf2out_decl (decl);
13091           break;
13092
13093         default:
13094           gcc_unreachable ();
13095         }
13096
13097       /* We should be able to find the DIE now.  */
13098       if (!decl_die)
13099         decl_die = lookup_decl_die (decl);
13100       gcc_assert (decl_die);
13101     }
13102
13103   return decl_die;
13104 }
13105
13106 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13107    always returned.  */
13108
13109 static dw_die_ref
13110 force_type_die (tree type)
13111 {
13112   dw_die_ref type_die;
13113
13114   type_die = lookup_type_die (type);
13115   if (!type_die)
13116     {
13117       dw_die_ref context_die;
13118       if (TYPE_CONTEXT (type))
13119         {
13120           if (TYPE_P (TYPE_CONTEXT (type)))
13121             context_die = force_type_die (TYPE_CONTEXT (type));
13122           else
13123             context_die = force_decl_die (TYPE_CONTEXT (type));
13124         }
13125       else
13126         context_die = comp_unit_die;
13127
13128       type_die = lookup_type_die (type);
13129       if (type_die)
13130         return type_die;
13131       gen_type_die (type, context_die);
13132       type_die = lookup_type_die (type);
13133       gcc_assert (type_die);
13134     }
13135   return type_die;
13136 }
13137
13138 /* Force out any required namespaces to be able to output DECL,
13139    and return the new context_die for it, if it's changed.  */
13140
13141 static dw_die_ref
13142 setup_namespace_context (tree thing, dw_die_ref context_die)
13143 {
13144   tree context = (DECL_P (thing)
13145                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13146   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13147     /* Force out the namespace.  */
13148     context_die = force_decl_die (context);
13149
13150   return context_die;
13151 }
13152
13153 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13154    type) within its namespace, if appropriate.
13155
13156    For compatibility with older debuggers, namespace DIEs only contain
13157    declarations; all definitions are emitted at CU scope.  */
13158
13159 static void
13160 declare_in_namespace (tree thing, dw_die_ref context_die)
13161 {
13162   dw_die_ref ns_context;
13163
13164   if (debug_info_level <= DINFO_LEVEL_TERSE)
13165     return;
13166
13167   /* If this decl is from an inlined function, then don't try to emit it in its
13168      namespace, as we will get confused.  It would have already been emitted
13169      when the abstract instance of the inline function was emitted anyways.  */
13170   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13171     return;
13172
13173   ns_context = setup_namespace_context (thing, context_die);
13174
13175   if (ns_context != context_die)
13176     {
13177       if (DECL_P (thing))
13178         gen_decl_die (thing, ns_context);
13179       else
13180         gen_type_die (thing, ns_context);
13181     }
13182 }
13183
13184 /* Generate a DIE for a namespace or namespace alias.  */
13185
13186 static void
13187 gen_namespace_die (tree decl)
13188 {
13189   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13190
13191   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13192      they are an alias of.  */
13193   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13194     {
13195       /* Output a real namespace.  */
13196       dw_die_ref namespace_die
13197         = new_die (DW_TAG_namespace, context_die, decl);
13198       add_name_and_src_coords_attributes (namespace_die, decl);
13199       equate_decl_number_to_die (decl, namespace_die);
13200     }
13201   else
13202     {
13203       /* Output a namespace alias.  */
13204
13205       /* Force out the namespace we are an alias of, if necessary.  */
13206       dw_die_ref origin_die
13207         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13208
13209       /* Now create the namespace alias DIE.  */
13210       dw_die_ref namespace_die
13211         = new_die (DW_TAG_imported_declaration, context_die, decl);
13212       add_name_and_src_coords_attributes (namespace_die, decl);
13213       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13214       equate_decl_number_to_die (decl, namespace_die);
13215     }
13216 }
13217
13218 /* Generate Dwarf debug information for a decl described by DECL.  */
13219
13220 static void
13221 gen_decl_die (tree decl, dw_die_ref context_die)
13222 {
13223   tree origin;
13224
13225   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13226     return;
13227
13228   switch (TREE_CODE (decl))
13229     {
13230     case ERROR_MARK:
13231       break;
13232
13233     case CONST_DECL:
13234       /* The individual enumerators of an enum type get output when we output
13235          the Dwarf representation of the relevant enum type itself.  */
13236       break;
13237
13238     case FUNCTION_DECL:
13239       /* Don't output any DIEs to represent mere function declarations,
13240          unless they are class members or explicit block externs.  */
13241       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13242           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13243         break;
13244
13245 #if 0
13246       /* FIXME */
13247       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13248          on local redeclarations of global functions.  That seems broken.  */
13249       if (current_function_decl != decl)
13250         /* This is only a declaration.  */;
13251 #endif
13252
13253       /* If we're emitting a clone, emit info for the abstract instance.  */
13254       if (DECL_ORIGIN (decl) != decl)
13255         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13256
13257       /* If we're emitting an out-of-line copy of an inline function,
13258          emit info for the abstract instance and set up to refer to it.  */
13259       else if (cgraph_function_possibly_inlined_p (decl)
13260                && ! DECL_ABSTRACT (decl)
13261                && ! class_or_namespace_scope_p (context_die)
13262                /* dwarf2out_abstract_function won't emit a die if this is just
13263                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13264                   that case, because that works only if we have a die.  */
13265                && DECL_INITIAL (decl) != NULL_TREE)
13266         {
13267           dwarf2out_abstract_function (decl);
13268           set_decl_origin_self (decl);
13269         }
13270
13271       /* Otherwise we're emitting the primary DIE for this decl.  */
13272       else if (debug_info_level > DINFO_LEVEL_TERSE)
13273         {
13274           /* Before we describe the FUNCTION_DECL itself, make sure that we
13275              have described its return type.  */
13276           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13277
13278           /* And its virtual context.  */
13279           if (DECL_VINDEX (decl) != NULL_TREE)
13280             gen_type_die (DECL_CONTEXT (decl), context_die);
13281
13282           /* And its containing type.  */
13283           origin = decl_class_context (decl);
13284           if (origin != NULL_TREE)
13285             gen_type_die_for_member (origin, decl, context_die);
13286
13287           /* And its containing namespace.  */
13288           declare_in_namespace (decl, context_die);
13289         }
13290
13291       /* Now output a DIE to represent the function itself.  */
13292       gen_subprogram_die (decl, context_die);
13293       break;
13294
13295     case TYPE_DECL:
13296       /* If we are in terse mode, don't generate any DIEs to represent any
13297          actual typedefs.  */
13298       if (debug_info_level <= DINFO_LEVEL_TERSE)
13299         break;
13300
13301       /* In the special case of a TYPE_DECL node representing the declaration
13302          of some type tag, if the given TYPE_DECL is marked as having been
13303          instantiated from some other (original) TYPE_DECL node (e.g. one which
13304          was generated within the original definition of an inline function) we
13305          have to generate a special (abbreviated) DW_TAG_structure_type,
13306          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13307       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13308           && is_tagged_type (TREE_TYPE (decl)))
13309         {
13310           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13311           break;
13312         }
13313
13314       if (is_redundant_typedef (decl))
13315         gen_type_die (TREE_TYPE (decl), context_die);
13316       else
13317         /* Output a DIE to represent the typedef itself.  */
13318         gen_typedef_die (decl, context_die);
13319       break;
13320
13321     case LABEL_DECL:
13322       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13323         gen_label_die (decl, context_die);
13324       break;
13325
13326     case VAR_DECL:
13327     case RESULT_DECL:
13328       /* If we are in terse mode, don't generate any DIEs to represent any
13329          variable declarations or definitions.  */
13330       if (debug_info_level <= DINFO_LEVEL_TERSE)
13331         break;
13332
13333       /* Output any DIEs that are needed to specify the type of this data
13334          object.  */
13335       gen_type_die (TREE_TYPE (decl), context_die);
13336
13337       /* And its containing type.  */
13338       origin = decl_class_context (decl);
13339       if (origin != NULL_TREE)
13340         gen_type_die_for_member (origin, decl, context_die);
13341
13342       /* And its containing namespace.  */
13343       declare_in_namespace (decl, context_die);
13344
13345       /* Now output the DIE to represent the data object itself.  This gets
13346          complicated because of the possibility that the VAR_DECL really
13347          represents an inlined instance of a formal parameter for an inline
13348          function.  */
13349       origin = decl_ultimate_origin (decl);
13350       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13351         gen_formal_parameter_die (decl, context_die);
13352       else
13353         gen_variable_die (decl, context_die);
13354       break;
13355
13356     case FIELD_DECL:
13357       /* Ignore the nameless fields that are used to skip bits but handle C++
13358          anonymous unions and structs.  */
13359       if (DECL_NAME (decl) != NULL_TREE
13360           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13361           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13362         {
13363           gen_type_die (member_declared_type (decl), context_die);
13364           gen_field_die (decl, context_die);
13365         }
13366       break;
13367
13368     case PARM_DECL:
13369       gen_type_die (TREE_TYPE (decl), context_die);
13370       gen_formal_parameter_die (decl, context_die);
13371       break;
13372
13373     case NAMESPACE_DECL:
13374       gen_namespace_die (decl);
13375       break;
13376
13377     default:
13378       /* Probably some frontend-internal decl.  Assume we don't care.  */
13379       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13380       break;
13381     }
13382 }
13383 \f
13384 /* Output debug information for global decl DECL.  Called from toplev.c after
13385    compilation proper has finished.  */
13386
13387 static void
13388 dwarf2out_global_decl (tree decl)
13389 {
13390   /* Output DWARF2 information for file-scope tentative data object
13391      declarations, file-scope (extern) function declarations (which had no
13392      corresponding body) and file-scope tagged type declarations and
13393      definitions which have not yet been forced out.  */
13394   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13395     dwarf2out_decl (decl);
13396 }
13397
13398 /* Output debug information for type decl DECL.  Called from toplev.c
13399    and from language front ends (to record built-in types).  */
13400 static void
13401 dwarf2out_type_decl (tree decl, int local)
13402 {
13403   if (!local)
13404     dwarf2out_decl (decl);
13405 }
13406
13407 /* Output debug information for imported module or decl.  */
13408
13409 static void
13410 dwarf2out_imported_module_or_decl (tree decl, tree context)
13411 {
13412   dw_die_ref imported_die, at_import_die;
13413   dw_die_ref scope_die;
13414   expanded_location xloc;
13415
13416   if (debug_info_level <= DINFO_LEVEL_TERSE)
13417     return;
13418
13419   gcc_assert (decl);
13420
13421   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13422      We need decl DIE for reference and scope die. First, get DIE for the decl
13423      itself.  */
13424
13425   /* Get the scope die for decl context. Use comp_unit_die for global module
13426      or decl. If die is not found for non globals, force new die.  */
13427   if (!context)
13428     scope_die = comp_unit_die;
13429   else if (TYPE_P (context))
13430     {
13431       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13432         return;
13433     scope_die = force_type_die (context);
13434     }
13435   else
13436     scope_die = force_decl_die (context);
13437
13438   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13439   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13440     {
13441       if (is_base_type (TREE_TYPE (decl)))
13442         at_import_die = base_type_die (TREE_TYPE (decl));
13443       else
13444         at_import_die = force_type_die (TREE_TYPE (decl));
13445     }
13446   else
13447     {
13448       at_import_die = lookup_decl_die (decl);
13449       if (!at_import_die)
13450         {
13451           /* If we're trying to avoid duplicate debug info, we may not have
13452              emitted the member decl for this field.  Emit it now.  */
13453           if (TREE_CODE (decl) == FIELD_DECL)
13454             {
13455               tree type = DECL_CONTEXT (decl);
13456               dw_die_ref type_context_die;
13457
13458               if (TYPE_CONTEXT (type))
13459                 if (TYPE_P (TYPE_CONTEXT (type)))
13460                   {
13461                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13462                                                    DINFO_USAGE_DIR_USE))
13463                       return;
13464                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13465                   }
13466               else
13467                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13468               else
13469                 type_context_die = comp_unit_die;
13470               gen_type_die_for_member (type, decl, type_context_die);
13471             }
13472           at_import_die = force_decl_die (decl);
13473         }
13474     }
13475
13476   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13477   if (TREE_CODE (decl) == NAMESPACE_DECL)
13478     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13479   else
13480     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13481
13482   xloc = expand_location (input_location);
13483   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13484   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13485   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13486 }
13487
13488 /* Write the debugging output for DECL.  */
13489
13490 void
13491 dwarf2out_decl (tree decl)
13492 {
13493   dw_die_ref context_die = comp_unit_die;
13494
13495   switch (TREE_CODE (decl))
13496     {
13497     case ERROR_MARK:
13498       return;
13499
13500     case FUNCTION_DECL:
13501       /* What we would really like to do here is to filter out all mere
13502          file-scope declarations of file-scope functions which are never
13503          referenced later within this translation unit (and keep all of ones
13504          that *are* referenced later on) but we aren't clairvoyant, so we have
13505          no idea which functions will be referenced in the future (i.e. later
13506          on within the current translation unit). So here we just ignore all
13507          file-scope function declarations which are not also definitions.  If
13508          and when the debugger needs to know something about these functions,
13509          it will have to hunt around and find the DWARF information associated
13510          with the definition of the function.
13511
13512          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13513          nodes represent definitions and which ones represent mere
13514          declarations.  We have to check DECL_INITIAL instead. That's because
13515          the C front-end supports some weird semantics for "extern inline"
13516          function definitions.  These can get inlined within the current
13517          translation unit (and thus, we need to generate Dwarf info for their
13518          abstract instances so that the Dwarf info for the concrete inlined
13519          instances can have something to refer to) but the compiler never
13520          generates any out-of-lines instances of such things (despite the fact
13521          that they *are* definitions).
13522
13523          The important point is that the C front-end marks these "extern
13524          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13525          them anyway. Note that the C++ front-end also plays some similar games
13526          for inline function definitions appearing within include files which
13527          also contain `#pragma interface' pragmas.  */
13528       if (DECL_INITIAL (decl) == NULL_TREE)
13529         return;
13530
13531       /* If we're a nested function, initially use a parent of NULL; if we're
13532          a plain function, this will be fixed up in decls_for_scope.  If
13533          we're a method, it will be ignored, since we already have a DIE.  */
13534       if (decl_function_context (decl)
13535           /* But if we're in terse mode, we don't care about scope.  */
13536           && debug_info_level > DINFO_LEVEL_TERSE)
13537         context_die = NULL;
13538       break;
13539
13540     case VAR_DECL:
13541       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13542          declaration and if the declaration was never even referenced from
13543          within this entire compilation unit.  We suppress these DIEs in
13544          order to save space in the .debug section (by eliminating entries
13545          which are probably useless).  Note that we must not suppress
13546          block-local extern declarations (whether used or not) because that
13547          would screw-up the debugger's name lookup mechanism and cause it to
13548          miss things which really ought to be in scope at a given point.  */
13549       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13550         return;
13551
13552       /* For local statics lookup proper context die.  */
13553       if (TREE_STATIC (decl) && decl_function_context (decl))
13554         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13555
13556       /* If we are in terse mode, don't generate any DIEs to represent any
13557          variable declarations or definitions.  */
13558       if (debug_info_level <= DINFO_LEVEL_TERSE)
13559         return;
13560       break;
13561
13562     case NAMESPACE_DECL:
13563       if (debug_info_level <= DINFO_LEVEL_TERSE)
13564         return;
13565       if (lookup_decl_die (decl) != NULL)
13566         return;
13567       break;
13568
13569     case TYPE_DECL:
13570       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13571       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13572         return;
13573
13574       /* Don't bother trying to generate any DIEs to represent any of the
13575          normal built-in types for the language we are compiling.  */
13576       if (DECL_IS_BUILTIN (decl))
13577         {
13578           /* OK, we need to generate one for `bool' so GDB knows what type
13579              comparisons have.  */
13580           if (is_cxx ()
13581               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13582               && ! DECL_IGNORED_P (decl))
13583             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13584
13585           return;
13586         }
13587
13588       /* If we are in terse mode, don't generate any DIEs for types.  */
13589       if (debug_info_level <= DINFO_LEVEL_TERSE)
13590         return;
13591
13592       /* If we're a function-scope tag, initially use a parent of NULL;
13593          this will be fixed up in decls_for_scope.  */
13594       if (decl_function_context (decl))
13595         context_die = NULL;
13596
13597       break;
13598
13599     default:
13600       return;
13601     }
13602
13603   gen_decl_die (decl, context_die);
13604 }
13605
13606 /* Output a marker (i.e. a label) for the beginning of the generated code for
13607    a lexical block.  */
13608
13609 static void
13610 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13611                        unsigned int blocknum)
13612 {
13613   switch_to_section (current_function_section ());
13614   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13615 }
13616
13617 /* Output a marker (i.e. a label) for the end of the generated code for a
13618    lexical block.  */
13619
13620 static void
13621 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13622 {
13623   switch_to_section (current_function_section ());
13624   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13625 }
13626
13627 /* Returns nonzero if it is appropriate not to emit any debugging
13628    information for BLOCK, because it doesn't contain any instructions.
13629
13630    Don't allow this for blocks with nested functions or local classes
13631    as we would end up with orphans, and in the presence of scheduling
13632    we may end up calling them anyway.  */
13633
13634 static bool
13635 dwarf2out_ignore_block (tree block)
13636 {
13637   tree decl;
13638
13639   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13640     if (TREE_CODE (decl) == FUNCTION_DECL
13641         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13642       return 0;
13643
13644   return 1;
13645 }
13646
13647 /* Hash table routines for file_hash.  */
13648
13649 static int
13650 file_table_eq (const void *p1_p, const void *p2_p)
13651 {
13652   const struct dwarf_file_data * p1 = p1_p;
13653   const char * p2 = p2_p;
13654   return strcmp (p1->filename, p2) == 0;
13655 }
13656
13657 static hashval_t
13658 file_table_hash (const void *p_p)
13659 {
13660   const struct dwarf_file_data * p = p_p;
13661   return htab_hash_string (p->filename);
13662 }
13663
13664 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13665    dwarf2out.c) and return its "index".  The index of each (known) filename is
13666    just a unique number which is associated with only that one filename.  We
13667    need such numbers for the sake of generating labels (in the .debug_sfnames
13668    section) and references to those files numbers (in the .debug_srcinfo
13669    and.debug_macinfo sections).  If the filename given as an argument is not
13670    found in our current list, add it to the list and assign it the next
13671    available unique index number.  In order to speed up searches, we remember
13672    the index of the filename was looked up last.  This handles the majority of
13673    all searches.  */
13674
13675 static struct dwarf_file_data *
13676 lookup_filename (const char *file_name)
13677 {
13678   void ** slot;
13679   struct dwarf_file_data * created;
13680
13681   /* Check to see if the file name that was searched on the previous
13682      call matches this file name.  If so, return the index.  */
13683   if (file_table_last_lookup
13684       && (file_name == file_table_last_lookup->filename
13685           || strcmp (file_table_last_lookup->filename, file_name) == 0))
13686     return file_table_last_lookup;
13687
13688   /* Didn't match the previous lookup, search the table.  */
13689   slot = htab_find_slot_with_hash (file_table, file_name,
13690                                    htab_hash_string (file_name), INSERT);
13691   if (*slot)
13692     return *slot;
13693
13694   created = ggc_alloc (sizeof (struct dwarf_file_data));
13695   created->filename = file_name;
13696   created->emitted_number = 0;
13697   *slot = created;
13698   return created;
13699 }
13700
13701 /* If the assembler will construct the file table, then translate the compiler
13702    internal file table number into the assembler file table number, and emit
13703    a .file directive if we haven't already emitted one yet.  The file table
13704    numbers are different because we prune debug info for unused variables and
13705    types, which may include filenames.  */
13706
13707 static int
13708 maybe_emit_file (struct dwarf_file_data * fd)
13709 {
13710   if (! fd->emitted_number)
13711     {
13712       if (last_emitted_file)
13713         fd->emitted_number = last_emitted_file->emitted_number + 1;
13714       else
13715         fd->emitted_number = 1;
13716       last_emitted_file = fd;
13717       
13718       if (DWARF2_ASM_LINE_DEBUG_INFO)
13719         {
13720           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13721           output_quoted_string (asm_out_file, fd->filename);
13722           fputc ('\n', asm_out_file);
13723         }
13724     }
13725   
13726   return fd->emitted_number;
13727 }
13728
13729 /* Called by the final INSN scan whenever we see a var location.  We
13730    use it to drop labels in the right places, and throw the location in
13731    our lookup table.  */
13732
13733 static void
13734 dwarf2out_var_location (rtx loc_note)
13735 {
13736   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13737   struct var_loc_node *newloc;
13738   rtx prev_insn;
13739   static rtx last_insn;
13740   static const char *last_label;
13741   tree decl;
13742
13743   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13744     return;
13745   prev_insn = PREV_INSN (loc_note);
13746
13747   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13748   /* If the insn we processed last time is the previous insn
13749      and it is also a var location note, use the label we emitted
13750      last time.  */
13751   if (last_insn != NULL_RTX
13752       && last_insn == prev_insn
13753       && NOTE_P (prev_insn)
13754       && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13755     {
13756       newloc->label = last_label;
13757     }
13758   else
13759     {
13760       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13761       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13762       loclabel_num++;
13763       newloc->label = ggc_strdup (loclabel);
13764     }
13765   newloc->var_loc_note = loc_note;
13766   newloc->next = NULL;
13767
13768   if (cfun && in_cold_section_p)
13769     newloc->section_label = cfun->cold_section_label;
13770   else
13771     newloc->section_label = text_section_label;
13772
13773   last_insn = loc_note;
13774   last_label = newloc->label;
13775   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13776   add_var_loc_to_decl (decl, newloc);
13777 }
13778
13779 /* We need to reset the locations at the beginning of each
13780    function. We can't do this in the end_function hook, because the
13781    declarations that use the locations won't have been output when
13782    that hook is called.  Also compute have_multiple_function_sections here.  */
13783
13784 static void
13785 dwarf2out_begin_function (tree fun)
13786 {
13787   htab_empty (decl_loc_table);
13788   
13789   if (function_section (fun) != text_section)
13790     have_multiple_function_sections = true;
13791 }
13792
13793 /* Output a label to mark the beginning of a source code line entry
13794    and record information relating to this source line, in
13795    'line_info_table' for later output of the .debug_line section.  */
13796
13797 static void
13798 dwarf2out_source_line (unsigned int line, const char *filename)
13799 {
13800   if (debug_info_level >= DINFO_LEVEL_NORMAL
13801       && line != 0)
13802     {
13803       int file_num = maybe_emit_file (lookup_filename (filename));
13804       
13805       switch_to_section (current_function_section ());
13806
13807       /* If requested, emit something human-readable.  */
13808       if (flag_debug_asm)
13809         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13810                  filename, line);
13811
13812       if (DWARF2_ASM_LINE_DEBUG_INFO)
13813         {
13814           /* Emit the .loc directive understood by GNU as.  */
13815           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13816
13817           /* Indicate that line number info exists.  */
13818           line_info_table_in_use++;
13819         }
13820       else if (function_section (current_function_decl) != text_section)
13821         {
13822           dw_separate_line_info_ref line_info;
13823           targetm.asm_out.internal_label (asm_out_file, 
13824                                           SEPARATE_LINE_CODE_LABEL,
13825                                           separate_line_info_table_in_use);
13826
13827           /* Expand the line info table if necessary.  */
13828           if (separate_line_info_table_in_use
13829               == separate_line_info_table_allocated)
13830             {
13831               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13832               separate_line_info_table
13833                 = ggc_realloc (separate_line_info_table,
13834                                separate_line_info_table_allocated
13835                                * sizeof (dw_separate_line_info_entry));
13836               memset (separate_line_info_table
13837                        + separate_line_info_table_in_use,
13838                       0,
13839                       (LINE_INFO_TABLE_INCREMENT
13840                        * sizeof (dw_separate_line_info_entry)));
13841             }
13842
13843           /* Add the new entry at the end of the line_info_table.  */
13844           line_info
13845             = &separate_line_info_table[separate_line_info_table_in_use++];
13846           line_info->dw_file_num = file_num;
13847           line_info->dw_line_num = line;
13848           line_info->function = current_function_funcdef_no;
13849         }
13850       else
13851         {
13852           dw_line_info_ref line_info;
13853
13854           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13855                                      line_info_table_in_use);
13856
13857           /* Expand the line info table if necessary.  */
13858           if (line_info_table_in_use == line_info_table_allocated)
13859             {
13860               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13861               line_info_table
13862                 = ggc_realloc (line_info_table,
13863                                (line_info_table_allocated
13864                                 * sizeof (dw_line_info_entry)));
13865               memset (line_info_table + line_info_table_in_use, 0,
13866                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13867             }
13868
13869           /* Add the new entry at the end of the line_info_table.  */
13870           line_info = &line_info_table[line_info_table_in_use++];
13871           line_info->dw_file_num = file_num;
13872           line_info->dw_line_num = line;
13873         }
13874     }
13875 }
13876
13877 /* Record the beginning of a new source file.  */
13878
13879 static void
13880 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13881 {
13882   if (flag_eliminate_dwarf2_dups)
13883     {
13884       /* Record the beginning of the file for break_out_includes.  */
13885       dw_die_ref bincl_die;
13886
13887       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13888       add_AT_string (bincl_die, DW_AT_name, filename);
13889     }
13890
13891   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13892     {
13893       int file_num = maybe_emit_file (lookup_filename (filename));
13894
13895       switch_to_section (debug_macinfo_section);
13896       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13897       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13898                                    lineno);
13899
13900       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
13901     }
13902 }
13903
13904 /* Record the end of a source file.  */
13905
13906 static void
13907 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13908 {
13909   if (flag_eliminate_dwarf2_dups)
13910     /* Record the end of the file for break_out_includes.  */
13911     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13912
13913   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13914     {
13915       switch_to_section (debug_macinfo_section);
13916       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13917     }
13918 }
13919
13920 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
13921    the tail part of the directive line, i.e. the part which is past the
13922    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13923
13924 static void
13925 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13926                   const char *buffer ATTRIBUTE_UNUSED)
13927 {
13928   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13929     {
13930       switch_to_section (debug_macinfo_section);
13931       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13932       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13933       dw2_asm_output_nstring (buffer, -1, "The macro");
13934     }
13935 }
13936
13937 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13938    the tail part of the directive line, i.e. the part which is past the
13939    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13940
13941 static void
13942 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13943                  const char *buffer ATTRIBUTE_UNUSED)
13944 {
13945   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13946     {
13947       switch_to_section (debug_macinfo_section);
13948       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13949       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13950       dw2_asm_output_nstring (buffer, -1, "The macro");
13951     }
13952 }
13953
13954 /* Set up for Dwarf output at the start of compilation.  */
13955
13956 static void
13957 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13958 {
13959   /* Allocate the file_table.  */
13960   file_table = htab_create_ggc (50, file_table_hash,
13961                                 file_table_eq, NULL);
13962
13963   /* Allocate the decl_die_table.  */
13964   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13965                                     decl_die_table_eq, NULL);
13966
13967   /* Allocate the decl_loc_table.  */
13968   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13969                                     decl_loc_table_eq, NULL);
13970
13971   /* Allocate the initial hunk of the decl_scope_table.  */
13972   decl_scope_table = VEC_alloc (tree, gc, 256);
13973
13974   /* Allocate the initial hunk of the abbrev_die_table.  */
13975   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13976                                         * sizeof (dw_die_ref));
13977   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13978   /* Zero-th entry is allocated, but unused.  */
13979   abbrev_die_table_in_use = 1;
13980
13981   /* Allocate the initial hunk of the line_info_table.  */
13982   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13983                                        * sizeof (dw_line_info_entry));
13984   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13985
13986   /* Zero-th entry is allocated, but unused.  */
13987   line_info_table_in_use = 1;
13988
13989   /* Allocate the pubtypes and pubnames vectors.  */
13990   pubname_table = VEC_alloc (pubname_entry, gc, 32);
13991   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
13992
13993   /* Generate the initial DIE for the .debug section.  Note that the (string)
13994      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13995      will (typically) be a relative pathname and that this pathname should be
13996      taken as being relative to the directory from which the compiler was
13997      invoked when the given (base) source file was compiled.  We will fill
13998      in this value in dwarf2out_finish.  */
13999   comp_unit_die = gen_compile_unit_die (NULL);
14000
14001   incomplete_types = VEC_alloc (tree, gc, 64);
14002
14003   used_rtx_array = VEC_alloc (rtx, gc, 32);
14004
14005   debug_info_section = get_section (DEBUG_INFO_SECTION,
14006                                     SECTION_DEBUG, NULL);
14007   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14008                                       SECTION_DEBUG, NULL);
14009   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14010                                        SECTION_DEBUG, NULL);
14011   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14012                                        SECTION_DEBUG, NULL);
14013   debug_line_section = get_section (DEBUG_LINE_SECTION,
14014                                     SECTION_DEBUG, NULL);
14015   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14016                                    SECTION_DEBUG, NULL);
14017   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14018                                         SECTION_DEBUG, NULL);
14019 #ifdef DEBUG_PUBTYPES_SECTION
14020   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14021                                         SECTION_DEBUG, NULL);
14022 #endif
14023   debug_str_section = get_section (DEBUG_STR_SECTION,
14024                                    DEBUG_STR_SECTION_FLAGS, NULL);
14025   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14026                                       SECTION_DEBUG, NULL);
14027   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14028                                      SECTION_DEBUG, NULL);
14029
14030   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14031   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14032                                DEBUG_ABBREV_SECTION_LABEL, 0);
14033   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14034   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label, 
14035                                COLD_TEXT_SECTION_LABEL, 0);
14036   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14037
14038   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14039                                DEBUG_INFO_SECTION_LABEL, 0);
14040   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14041                                DEBUG_LINE_SECTION_LABEL, 0);
14042   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14043                                DEBUG_RANGES_SECTION_LABEL, 0);
14044   switch_to_section (debug_abbrev_section);
14045   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14046   switch_to_section (debug_info_section);
14047   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14048   switch_to_section (debug_line_section);
14049   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14050
14051   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14052     {
14053       switch_to_section (debug_macinfo_section);
14054       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14055                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14056       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14057     }
14058
14059   switch_to_section (text_section);
14060   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14061   if (flag_reorder_blocks_and_partition)
14062     {
14063       switch_to_section (unlikely_text_section ());
14064       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14065     }
14066 }
14067
14068 /* A helper function for dwarf2out_finish called through
14069    ht_forall.  Emit one queued .debug_str string.  */
14070
14071 static int
14072 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14073 {
14074   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14075
14076   if (node->form == DW_FORM_strp)
14077     {
14078       switch_to_section (debug_str_section);
14079       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14080       assemble_string (node->str, strlen (node->str) + 1);
14081     }
14082
14083   return 1;
14084 }
14085
14086 #if ENABLE_ASSERT_CHECKING
14087 /* Verify that all marks are clear.  */
14088
14089 static void
14090 verify_marks_clear (dw_die_ref die)
14091 {
14092   dw_die_ref c;
14093   
14094   gcc_assert (! die->die_mark);
14095   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14096 }
14097 #endif /* ENABLE_ASSERT_CHECKING */
14098
14099 /* Clear the marks for a die and its children.
14100    Be cool if the mark isn't set.  */
14101
14102 static void
14103 prune_unmark_dies (dw_die_ref die)
14104 {
14105   dw_die_ref c;
14106   
14107   if (die->die_mark)
14108     die->die_mark = 0;
14109   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14110 }
14111
14112 /* Given DIE that we're marking as used, find any other dies
14113    it references as attributes and mark them as used.  */
14114
14115 static void
14116 prune_unused_types_walk_attribs (dw_die_ref die)
14117 {
14118   dw_attr_ref a;
14119   unsigned ix;
14120
14121   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14122     {
14123       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14124         {
14125           /* A reference to another DIE.
14126              Make sure that it will get emitted.  */
14127           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14128         }
14129       /* Set the string's refcount to 0 so that prune_unused_types_mark
14130          accounts properly for it.  */
14131       if (AT_class (a) == dw_val_class_str)
14132         a->dw_attr_val.v.val_str->refcount = 0;
14133     }
14134 }
14135
14136
14137 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14138    to DIE's children.  */
14139
14140 static void
14141 prune_unused_types_mark (dw_die_ref die, int dokids)
14142 {
14143   dw_die_ref c;
14144
14145   if (die->die_mark == 0)
14146     {
14147       /* We haven't done this node yet.  Mark it as used.  */
14148       die->die_mark = 1;
14149
14150       /* We also have to mark its parents as used.
14151          (But we don't want to mark our parents' kids due to this.)  */
14152       if (die->die_parent)
14153         prune_unused_types_mark (die->die_parent, 0);
14154
14155       /* Mark any referenced nodes.  */
14156       prune_unused_types_walk_attribs (die);
14157
14158       /* If this node is a specification,
14159          also mark the definition, if it exists.  */
14160       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14161         prune_unused_types_mark (die->die_definition, 1);
14162     }
14163
14164   if (dokids && die->die_mark != 2)
14165     {
14166       /* We need to walk the children, but haven't done so yet.
14167          Remember that we've walked the kids.  */
14168       die->die_mark = 2;
14169
14170       /* If this is an array type, we need to make sure our
14171          kids get marked, even if they're types.  */
14172       if (die->die_tag == DW_TAG_array_type)
14173         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14174       else
14175         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14176     }
14177 }
14178
14179
14180 /* Walk the tree DIE and mark types that we actually use.  */
14181
14182 static void
14183 prune_unused_types_walk (dw_die_ref die)
14184 {
14185   dw_die_ref c;
14186
14187   /* Don't do anything if this node is already marked.  */
14188   if (die->die_mark)
14189     return;
14190
14191   switch (die->die_tag) {
14192   case DW_TAG_const_type:
14193   case DW_TAG_packed_type:
14194   case DW_TAG_pointer_type:
14195   case DW_TAG_reference_type:
14196   case DW_TAG_volatile_type:
14197   case DW_TAG_typedef:
14198   case DW_TAG_array_type:
14199   case DW_TAG_structure_type:
14200   case DW_TAG_union_type:
14201   case DW_TAG_class_type:
14202   case DW_TAG_friend:
14203   case DW_TAG_variant_part:
14204   case DW_TAG_enumeration_type:
14205   case DW_TAG_subroutine_type:
14206   case DW_TAG_string_type:
14207   case DW_TAG_set_type:
14208   case DW_TAG_subrange_type:
14209   case DW_TAG_ptr_to_member_type:
14210   case DW_TAG_file_type:
14211     if (die->die_perennial_p)
14212       break;
14213
14214     /* It's a type node --- don't mark it.  */
14215     return;
14216
14217   default:
14218     /* Mark everything else.  */
14219     break;
14220   }
14221
14222   die->die_mark = 1;
14223
14224   /* Now, mark any dies referenced from here.  */
14225   prune_unused_types_walk_attribs (die);
14226
14227   /* Mark children.  */
14228   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14229 }
14230
14231 /* Increment the string counts on strings referred to from DIE's
14232    attributes.  */
14233
14234 static void
14235 prune_unused_types_update_strings (dw_die_ref die)
14236 {
14237   dw_attr_ref a;
14238   unsigned ix;
14239
14240   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14241     if (AT_class (a) == dw_val_class_str)
14242       {
14243         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14244         s->refcount++;
14245         /* Avoid unnecessarily putting strings that are used less than
14246            twice in the hash table.  */
14247         if (s->refcount
14248             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14249           {
14250             void ** slot;
14251             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14252                                              htab_hash_string (s->str),
14253                                              INSERT);
14254             gcc_assert (*slot == NULL);
14255             *slot = s;
14256           }
14257       }
14258 }
14259
14260 /* Remove from the tree DIE any dies that aren't marked.  */
14261
14262 static void
14263 prune_unused_types_prune (dw_die_ref die)
14264 {
14265   dw_die_ref c;
14266
14267   gcc_assert (die->die_mark);
14268   prune_unused_types_update_strings (die);
14269
14270   if (! die->die_child)
14271     return;
14272   
14273   c = die->die_child;
14274   do {
14275     dw_die_ref prev = c;
14276     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14277       if (c == die->die_child)
14278         {
14279           /* No marked children between 'prev' and the end of the list.  */
14280           if (prev == c)
14281             /* No marked children at all.  */
14282             die->die_child = NULL;
14283           else
14284             {
14285               prev->die_sib = c->die_sib;
14286               die->die_child = prev;
14287             }
14288           return;
14289         }
14290
14291     if (c != prev->die_sib)
14292       prev->die_sib = c;
14293     prune_unused_types_prune (c);
14294   } while (c != die->die_child);
14295 }
14296
14297
14298 /* Remove dies representing declarations that we never use.  */
14299
14300 static void
14301 prune_unused_types (void)
14302 {
14303   unsigned int i;
14304   limbo_die_node *node;
14305   pubname_ref pub;
14306
14307 #if ENABLE_ASSERT_CHECKING
14308   /* All the marks should already be clear.  */
14309   verify_marks_clear (comp_unit_die);
14310   for (node = limbo_die_list; node; node = node->next)
14311     verify_marks_clear (node->die);
14312 #endif /* ENABLE_ASSERT_CHECKING */
14313
14314   /* Set the mark on nodes that are actually used.  */
14315   prune_unused_types_walk (comp_unit_die);
14316   for (node = limbo_die_list; node; node = node->next)
14317     prune_unused_types_walk (node->die);
14318
14319   /* Also set the mark on nodes referenced from the
14320      pubname_table or arange_table.  */
14321   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14322     prune_unused_types_mark (pub->die, 1);
14323   for (i = 0; i < arange_table_in_use; i++)
14324     prune_unused_types_mark (arange_table[i], 1);
14325
14326   /* Get rid of nodes that aren't marked; and update the string counts.  */
14327   if (debug_str_hash)
14328     htab_empty (debug_str_hash);
14329   prune_unused_types_prune (comp_unit_die);
14330   for (node = limbo_die_list; node; node = node->next)
14331     prune_unused_types_prune (node->die);
14332
14333   /* Leave the marks clear.  */
14334   prune_unmark_dies (comp_unit_die);
14335   for (node = limbo_die_list; node; node = node->next)
14336     prune_unmark_dies (node->die);
14337 }
14338
14339 /* Set the parameter to true if there are any relative pathnames in
14340    the file table.  */
14341 static int
14342 file_table_relative_p (void ** slot, void *param)
14343 {
14344   bool *p = param;
14345   struct dwarf_file_data *d = *slot;
14346   if (d->emitted_number && d->filename[0] != DIR_SEPARATOR)
14347     {
14348       *p = true;
14349       return 0;
14350     }
14351   return 1;
14352 }
14353
14354 /* Output stuff that dwarf requires at the end of every file,
14355    and generate the DWARF-2 debugging info.  */
14356
14357 static void
14358 dwarf2out_finish (const char *filename)
14359 {
14360   limbo_die_node *node, *next_node;
14361   dw_die_ref die = 0;
14362
14363   /* Add the name for the main input file now.  We delayed this from
14364      dwarf2out_init to avoid complications with PCH.  */
14365   add_name_attribute (comp_unit_die, filename);
14366   if (filename[0] != DIR_SEPARATOR)
14367     add_comp_dir_attribute (comp_unit_die);
14368   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14369     {
14370       bool p = false;
14371       htab_traverse (file_table, file_table_relative_p, &p);
14372       if (p)
14373         add_comp_dir_attribute (comp_unit_die);
14374     }
14375
14376   /* Traverse the limbo die list, and add parent/child links.  The only
14377      dies without parents that should be here are concrete instances of
14378      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14379      For concrete instances, we can get the parent die from the abstract
14380      instance.  */
14381   for (node = limbo_die_list; node; node = next_node)
14382     {
14383       next_node = node->next;
14384       die = node->die;
14385
14386       if (die->die_parent == NULL)
14387         {
14388           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14389
14390           if (origin)
14391             add_child_die (origin->die_parent, die);
14392           else if (die == comp_unit_die)
14393             ;
14394           else if (errorcount > 0 || sorrycount > 0)
14395             /* It's OK to be confused by errors in the input.  */
14396             add_child_die (comp_unit_die, die);
14397           else
14398             {
14399               /* In certain situations, the lexical block containing a
14400                  nested function can be optimized away, which results
14401                  in the nested function die being orphaned.  Likewise
14402                  with the return type of that nested function.  Force
14403                  this to be a child of the containing function.
14404
14405                  It may happen that even the containing function got fully
14406                  inlined and optimized out.  In that case we are lost and
14407                  assign the empty child.  This should not be big issue as
14408                  the function is likely unreachable too.  */
14409               tree context = NULL_TREE;
14410
14411               gcc_assert (node->created_for);
14412
14413               if (DECL_P (node->created_for))
14414                 context = DECL_CONTEXT (node->created_for);
14415               else if (TYPE_P (node->created_for))
14416                 context = TYPE_CONTEXT (node->created_for);
14417
14418               gcc_assert (context
14419                           && (TREE_CODE (context) == FUNCTION_DECL
14420                               || TREE_CODE (context) == NAMESPACE_DECL));
14421
14422               origin = lookup_decl_die (context);
14423               if (origin)
14424                 add_child_die (origin, die);
14425               else
14426                 add_child_die (comp_unit_die, die);
14427             }
14428         }
14429     }
14430
14431   limbo_die_list = NULL;
14432
14433   /* Walk through the list of incomplete types again, trying once more to
14434      emit full debugging info for them.  */
14435   retry_incomplete_types ();
14436
14437   if (flag_eliminate_unused_debug_types)
14438     prune_unused_types ();
14439
14440   /* Generate separate CUs for each of the include files we've seen.
14441      They will go into limbo_die_list.  */
14442   if (flag_eliminate_dwarf2_dups)
14443     break_out_includes (comp_unit_die);
14444
14445   /* Traverse the DIE's and add add sibling attributes to those DIE's
14446      that have children.  */
14447   add_sibling_attributes (comp_unit_die);
14448   for (node = limbo_die_list; node; node = node->next)
14449     add_sibling_attributes (node->die);
14450
14451   /* Output a terminator label for the .text section.  */
14452   switch_to_section (text_section);
14453   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14454   if (flag_reorder_blocks_and_partition)
14455     {
14456       switch_to_section (unlikely_text_section ());
14457       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14458     }
14459
14460   /* We can only use the low/high_pc attributes if all of the code was
14461      in .text.  */
14462   if (!have_multiple_function_sections)
14463     {
14464       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14465       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14466     }
14467
14468   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14469      "base address".  Use zero so that these addresses become absolute.  */
14470   else if (have_location_lists || ranges_table_in_use)
14471     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14472
14473   /* Output location list section if necessary.  */
14474   if (have_location_lists)
14475     {
14476       /* Output the location lists info.  */
14477       switch_to_section (debug_loc_section);
14478       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14479                                    DEBUG_LOC_SECTION_LABEL, 0);
14480       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14481       output_location_lists (die);
14482     }
14483
14484   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14485     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14486                     debug_line_section_label);
14487
14488   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14489     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14490
14491   /* Output all of the compilation units.  We put the main one last so that
14492      the offsets are available to output_pubnames.  */
14493   for (node = limbo_die_list; node; node = node->next)
14494     output_comp_unit (node->die, 0);
14495
14496   output_comp_unit (comp_unit_die, 0);
14497
14498   /* Output the abbreviation table.  */
14499   switch_to_section (debug_abbrev_section);
14500   output_abbrev_section ();
14501
14502   /* Output public names table if necessary.  */
14503   if (!VEC_empty (pubname_entry, pubname_table))
14504     {
14505       switch_to_section (debug_pubnames_section);
14506       output_pubnames (pubname_table);
14507     }
14508
14509 #ifdef DEBUG_PUBTYPES_SECTION
14510   /* Output public types table if necessary.  */
14511   if (!VEC_empty (pubname_entry, pubtype_table))
14512     {
14513       switch_to_section (debug_pubtypes_section);
14514       output_pubnames (pubtype_table);
14515     }
14516 #endif
14517   
14518   /* Output the address range information.  We only put functions in the arange
14519      table, so don't write it out if we don't have any.  */
14520   if (fde_table_in_use)
14521     {
14522       switch_to_section (debug_aranges_section);
14523       output_aranges ();
14524     }
14525
14526   /* Output ranges section if necessary.  */
14527   if (ranges_table_in_use)
14528     {
14529       switch_to_section (debug_ranges_section);
14530       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14531       output_ranges ();
14532     }
14533
14534   /* Output the source line correspondence table.  We must do this
14535      even if there is no line information.  Otherwise, on an empty
14536      translation unit, we will generate a present, but empty,
14537      .debug_info section.  IRIX 6.5 `nm' will then complain when
14538      examining the file.  This is done late so that any filenames
14539      used by the debug_info section are marked as 'used'.  */
14540   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14541     {
14542       switch_to_section (debug_line_section);
14543       output_line_info ();
14544     }
14545
14546   /* Have to end the macro section.  */
14547   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14548     {
14549       switch_to_section (debug_macinfo_section);
14550       dw2_asm_output_data (1, 0, "End compilation unit");
14551     }
14552
14553   /* If we emitted any DW_FORM_strp form attribute, output the string
14554      table too.  */
14555   if (debug_str_hash)
14556     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14557 }
14558 #else
14559
14560 /* This should never be used, but its address is needed for comparisons.  */
14561 const struct gcc_debug_hooks dwarf2_debug_hooks;
14562
14563 #endif /* DWARF2_DEBUGGING_INFO */
14564
14565 #include "gt-dwarf2out.h"