OSDN Git Service

2006-03-08 Andreas Tobler <a.tobler@schweiz.ch>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006 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_str_section;
160 static GTY(()) section *debug_ranges_section;
161
162 /* How to start an assembler comment.  */
163 #ifndef ASM_COMMENT_START
164 #define ASM_COMMENT_START ";#"
165 #endif
166
167 typedef struct dw_cfi_struct *dw_cfi_ref;
168 typedef struct dw_fde_struct *dw_fde_ref;
169 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
170
171 /* Call frames are described using a sequence of Call Frame
172    Information instructions.  The register number, offset
173    and address fields are provided as possible operands;
174    their use is selected by the opcode field.  */
175
176 enum dw_cfi_oprnd_type {
177   dw_cfi_oprnd_unused,
178   dw_cfi_oprnd_reg_num,
179   dw_cfi_oprnd_offset,
180   dw_cfi_oprnd_addr,
181   dw_cfi_oprnd_loc
182 };
183
184 typedef union dw_cfi_oprnd_struct GTY(())
185 {
186   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
187   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
188   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
189   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
190 }
191 dw_cfi_oprnd;
192
193 typedef struct dw_cfi_struct GTY(())
194 {
195   dw_cfi_ref dw_cfi_next;
196   enum dwarf_call_frame_info dw_cfi_opc;
197   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
198     dw_cfi_oprnd1;
199   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
200     dw_cfi_oprnd2;
201 }
202 dw_cfi_node;
203
204 /* This is how we define the location of the CFA. We use to handle it
205    as REG + OFFSET all the time,  but now it can be more complex.
206    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
207    Instead of passing around REG and OFFSET, we pass a copy
208    of this structure.  */
209 typedef struct cfa_loc GTY(())
210 {
211   HOST_WIDE_INT offset;
212   HOST_WIDE_INT base_offset;
213   unsigned int reg;
214   int indirect;            /* 1 if CFA is accessed via a dereference.  */
215 } dw_cfa_location;
216
217 /* All call frame descriptions (FDE's) in the GCC generated DWARF
218    refer to a single Common Information Entry (CIE), defined at
219    the beginning of the .debug_frame section.  This use of a single
220    CIE obviates the need to keep track of multiple CIE's
221    in the DWARF generation routines below.  */
222
223 typedef struct dw_fde_struct GTY(())
224 {
225   tree decl;
226   const char *dw_fde_begin;
227   const char *dw_fde_current_label;
228   const char *dw_fde_end;
229   const char *dw_fde_hot_section_label;
230   const char *dw_fde_hot_section_end_label;
231   const char *dw_fde_unlikely_section_label;
232   const char *dw_fde_unlikely_section_end_label;
233   bool dw_fde_switched_sections;
234   dw_cfi_ref dw_fde_cfi;
235   unsigned funcdef_number;
236   unsigned all_throwers_are_sibcalls : 1;
237   unsigned nothrow : 1;
238   unsigned uses_eh_lsda : 1;
239 }
240 dw_fde_node;
241
242 /* Maximum size (in bytes) of an artificially generated label.  */
243 #define MAX_ARTIFICIAL_LABEL_BYTES      30
244
245 /* The size of addresses as they appear in the Dwarf 2 data.
246    Some architectures use word addresses to refer to code locations,
247    but Dwarf 2 info always uses byte addresses.  On such machines,
248    Dwarf 2 addresses need to be larger than the architecture's
249    pointers.  */
250 #ifndef DWARF2_ADDR_SIZE
251 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
252 #endif
253
254 /* The size in bytes of a DWARF field indicating an offset or length
255    relative to a debug info section, specified to be 4 bytes in the
256    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
257    as PTR_SIZE.  */
258
259 #ifndef DWARF_OFFSET_SIZE
260 #define DWARF_OFFSET_SIZE 4
261 #endif
262
263 /* According to the (draft) DWARF 3 specification, the initial length
264    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
265    bytes are 0xffffffff, followed by the length stored in the next 8
266    bytes.
267
268    However, the SGI/MIPS ABI uses an initial length which is equal to
269    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
270
271 #ifndef DWARF_INITIAL_LENGTH_SIZE
272 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
273 #endif
274
275 #define DWARF_VERSION 2
276
277 /* Round SIZE up to the nearest BOUNDARY.  */
278 #define DWARF_ROUND(SIZE,BOUNDARY) \
279   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
280
281 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
282 #ifndef DWARF_CIE_DATA_ALIGNMENT
283 #ifdef STACK_GROWS_DOWNWARD
284 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
285 #else
286 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
287 #endif
288 #endif
289
290 /* A pointer to the base of a table that contains frame description
291    information for each routine.  */
292 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
293
294 /* Number of elements currently allocated for fde_table.  */
295 static GTY(()) unsigned fde_table_allocated;
296
297 /* Number of elements in fde_table currently in use.  */
298 static GTY(()) unsigned fde_table_in_use;
299
300 /* Size (in elements) of increments by which we may expand the
301    fde_table.  */
302 #define FDE_TABLE_INCREMENT 256
303
304 /* A list of call frame insns for the CIE.  */
305 static GTY(()) dw_cfi_ref cie_cfi_head;
306
307 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
308 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
309    attribute that accelerates the lookup of the FDE associated
310    with the subprogram.  This variable holds the table index of the FDE
311    associated with the current function (body) definition.  */
312 static unsigned current_funcdef_fde;
313 #endif
314
315 struct indirect_string_node GTY(())
316 {
317   const char *str;
318   unsigned int refcount;
319   unsigned int form;
320   char *label;
321 };
322
323 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
324
325 static GTY(()) int dw2_string_counter;
326 static GTY(()) unsigned long dwarf2out_cfi_label_num;
327
328 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
329
330 /* Forward declarations for functions defined in this file.  */
331
332 static char *stripattributes (const char *);
333 static const char *dwarf_cfi_name (unsigned);
334 static dw_cfi_ref new_cfi (void);
335 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
336 static void add_fde_cfi (const char *, dw_cfi_ref);
337 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
338 static void lookup_cfa (dw_cfa_location *);
339 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
340 static void initial_return_save (rtx);
341 static HOST_WIDE_INT stack_adjust_offset (rtx);
342 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
343 static void output_call_frame_info (int);
344 static void dwarf2out_stack_adjust (rtx, bool);
345 static void flush_queued_reg_saves (void);
346 static bool clobbers_queued_reg_save (rtx);
347 static void dwarf2out_frame_debug_expr (rtx, const char *);
348
349 /* Support for complex CFA locations.  */
350 static void output_cfa_loc (dw_cfi_ref);
351 static void get_cfa_from_loc_descr (dw_cfa_location *,
352                                     struct dw_loc_descr_struct *);
353 static struct dw_loc_descr_struct *build_cfa_loc
354   (dw_cfa_location *, HOST_WIDE_INT);
355 static void def_cfa_1 (const char *, dw_cfa_location *);
356
357 /* How to start an assembler comment.  */
358 #ifndef ASM_COMMENT_START
359 #define ASM_COMMENT_START ";#"
360 #endif
361
362 /* Data and reference forms for relocatable data.  */
363 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
364 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
365
366 #ifndef DEBUG_FRAME_SECTION
367 #define DEBUG_FRAME_SECTION     ".debug_frame"
368 #endif
369
370 #ifndef FUNC_BEGIN_LABEL
371 #define FUNC_BEGIN_LABEL        "LFB"
372 #endif
373
374 #ifndef FUNC_END_LABEL
375 #define FUNC_END_LABEL          "LFE"
376 #endif
377
378 #ifndef FRAME_BEGIN_LABEL
379 #define FRAME_BEGIN_LABEL       "Lframe"
380 #endif
381 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
382 #define CIE_END_LABEL           "LECIE"
383 #define FDE_LABEL               "LSFDE"
384 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
385 #define FDE_END_LABEL           "LEFDE"
386 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
387 #define LINE_NUMBER_END_LABEL   "LELT"
388 #define LN_PROLOG_AS_LABEL      "LASLTP"
389 #define LN_PROLOG_END_LABEL     "LELTP"
390 #define DIE_LABEL_PREFIX        "DW"
391
392 /* The DWARF 2 CFA column which tracks the return address.  Normally this
393    is the column for PC, or the first column after all of the hard
394    registers.  */
395 #ifndef DWARF_FRAME_RETURN_COLUMN
396 #ifdef PC_REGNUM
397 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
398 #else
399 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
400 #endif
401 #endif
402
403 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
404    default, we just provide columns for all registers.  */
405 #ifndef DWARF_FRAME_REGNUM
406 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
407 #endif
408 \f
409 /* Hook used by __throw.  */
410
411 rtx
412 expand_builtin_dwarf_sp_column (void)
413 {
414   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
415   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
416 }
417
418 /* Return a pointer to a copy of the section string name S with all
419    attributes stripped off, and an asterisk prepended (for assemble_name).  */
420
421 static inline char *
422 stripattributes (const char *s)
423 {
424   char *stripped = XNEWVEC (char, strlen (s) + 2);
425   char *p = stripped;
426
427   *p++ = '*';
428
429   while (*s && *s != ',')
430     *p++ = *s++;
431
432   *p = '\0';
433   return stripped;
434 }
435
436 /* Generate code to initialize the register size table.  */
437
438 void
439 expand_builtin_init_dwarf_reg_sizes (tree address)
440 {
441   unsigned int i;
442   enum machine_mode mode = TYPE_MODE (char_type_node);
443   rtx addr = expand_normal (address);
444   rtx mem = gen_rtx_MEM (BLKmode, addr);
445   bool wrote_return_column = false;
446
447   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
448     {
449       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
450       
451       if (rnum < DWARF_FRAME_REGISTERS)
452         {
453           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
454           enum machine_mode save_mode = reg_raw_mode[i];
455           HOST_WIDE_INT size;
456           
457           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
458             save_mode = choose_hard_reg_mode (i, 1, true);
459           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
460             {
461               if (save_mode == VOIDmode)
462                 continue;
463               wrote_return_column = true;
464             }
465           size = GET_MODE_SIZE (save_mode);
466           if (offset < 0)
467             continue;
468           
469           emit_move_insn (adjust_address (mem, mode, offset),
470                           gen_int_mode (size, mode));
471         }
472     }
473
474 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
475   gcc_assert (wrote_return_column);
476   i = DWARF_ALT_FRAME_RETURN_COLUMN;
477   wrote_return_column = false;
478 #else
479   i = DWARF_FRAME_RETURN_COLUMN;
480 #endif
481
482   if (! wrote_return_column)
483     {
484       enum machine_mode save_mode = Pmode;
485       HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
486       HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
487       emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
488     }
489 }
490
491 /* Convert a DWARF call frame info. operation to its string name */
492
493 static const char *
494 dwarf_cfi_name (unsigned int cfi_opc)
495 {
496   switch (cfi_opc)
497     {
498     case DW_CFA_advance_loc:
499       return "DW_CFA_advance_loc";
500     case DW_CFA_offset:
501       return "DW_CFA_offset";
502     case DW_CFA_restore:
503       return "DW_CFA_restore";
504     case DW_CFA_nop:
505       return "DW_CFA_nop";
506     case DW_CFA_set_loc:
507       return "DW_CFA_set_loc";
508     case DW_CFA_advance_loc1:
509       return "DW_CFA_advance_loc1";
510     case DW_CFA_advance_loc2:
511       return "DW_CFA_advance_loc2";
512     case DW_CFA_advance_loc4:
513       return "DW_CFA_advance_loc4";
514     case DW_CFA_offset_extended:
515       return "DW_CFA_offset_extended";
516     case DW_CFA_restore_extended:
517       return "DW_CFA_restore_extended";
518     case DW_CFA_undefined:
519       return "DW_CFA_undefined";
520     case DW_CFA_same_value:
521       return "DW_CFA_same_value";
522     case DW_CFA_register:
523       return "DW_CFA_register";
524     case DW_CFA_remember_state:
525       return "DW_CFA_remember_state";
526     case DW_CFA_restore_state:
527       return "DW_CFA_restore_state";
528     case DW_CFA_def_cfa:
529       return "DW_CFA_def_cfa";
530     case DW_CFA_def_cfa_register:
531       return "DW_CFA_def_cfa_register";
532     case DW_CFA_def_cfa_offset:
533       return "DW_CFA_def_cfa_offset";
534
535     /* DWARF 3 */
536     case DW_CFA_def_cfa_expression:
537       return "DW_CFA_def_cfa_expression";
538     case DW_CFA_expression:
539       return "DW_CFA_expression";
540     case DW_CFA_offset_extended_sf:
541       return "DW_CFA_offset_extended_sf";
542     case DW_CFA_def_cfa_sf:
543       return "DW_CFA_def_cfa_sf";
544     case DW_CFA_def_cfa_offset_sf:
545       return "DW_CFA_def_cfa_offset_sf";
546
547     /* SGI/MIPS specific */
548     case DW_CFA_MIPS_advance_loc8:
549       return "DW_CFA_MIPS_advance_loc8";
550
551     /* GNU extensions */
552     case DW_CFA_GNU_window_save:
553       return "DW_CFA_GNU_window_save";
554     case DW_CFA_GNU_args_size:
555       return "DW_CFA_GNU_args_size";
556     case DW_CFA_GNU_negative_offset_extended:
557       return "DW_CFA_GNU_negative_offset_extended";
558
559     default:
560       return "DW_CFA_<unknown>";
561     }
562 }
563
564 /* Return a pointer to a newly allocated Call Frame Instruction.  */
565
566 static inline dw_cfi_ref
567 new_cfi (void)
568 {
569   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
570
571   cfi->dw_cfi_next = NULL;
572   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
573   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
574
575   return cfi;
576 }
577
578 /* Add a Call Frame Instruction to list of instructions.  */
579
580 static inline void
581 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
582 {
583   dw_cfi_ref *p;
584
585   /* Find the end of the chain.  */
586   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
587     ;
588
589   *p = cfi;
590 }
591
592 /* Generate a new label for the CFI info to refer to.  */
593
594 char *
595 dwarf2out_cfi_label (void)
596 {
597   static char label[20];
598
599   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
600   ASM_OUTPUT_LABEL (asm_out_file, label);
601   return label;
602 }
603
604 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
605    or to the CIE if LABEL is NULL.  */
606
607 static void
608 add_fde_cfi (const char *label, dw_cfi_ref cfi)
609 {
610   if (label)
611     {
612       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
613
614       if (*label == 0)
615         label = dwarf2out_cfi_label ();
616
617       if (fde->dw_fde_current_label == NULL
618           || strcmp (label, fde->dw_fde_current_label) != 0)
619         {
620           dw_cfi_ref xcfi;
621
622           fde->dw_fde_current_label = label = xstrdup (label);
623
624           /* Set the location counter to the new label.  */
625           xcfi = new_cfi ();
626           xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
627           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
628           add_cfi (&fde->dw_fde_cfi, xcfi);
629         }
630
631       add_cfi (&fde->dw_fde_cfi, cfi);
632     }
633
634   else
635     add_cfi (&cie_cfi_head, cfi);
636 }
637
638 /* Subroutine of lookup_cfa.  */
639
640 static void
641 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
642 {
643   switch (cfi->dw_cfi_opc)
644     {
645     case DW_CFA_def_cfa_offset:
646       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
647       break;
648     case DW_CFA_def_cfa_offset_sf:
649       loc->offset
650         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
651       break;
652     case DW_CFA_def_cfa_register:
653       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
654       break;
655     case DW_CFA_def_cfa:
656       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
657       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
658       break;
659     case DW_CFA_def_cfa_sf:
660       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
661       loc->offset
662         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
663       break;
664     case DW_CFA_def_cfa_expression:
665       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
666       break;
667     default:
668       break;
669     }
670 }
671
672 /* Find the previous value for the CFA.  */
673
674 static void
675 lookup_cfa (dw_cfa_location *loc)
676 {
677   dw_cfi_ref cfi;
678
679   loc->reg = INVALID_REGNUM;
680   loc->offset = 0;
681   loc->indirect = 0;
682   loc->base_offset = 0;
683
684   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
685     lookup_cfa_1 (cfi, loc);
686
687   if (fde_table_in_use)
688     {
689       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
690       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
691         lookup_cfa_1 (cfi, loc);
692     }
693 }
694
695 /* The current rule for calculating the DWARF2 canonical frame address.  */
696 static dw_cfa_location cfa;
697
698 /* The register used for saving registers to the stack, and its offset
699    from the CFA.  */
700 static dw_cfa_location cfa_store;
701
702 /* The running total of the size of arguments pushed onto the stack.  */
703 static HOST_WIDE_INT args_size;
704
705 /* The last args_size we actually output.  */
706 static HOST_WIDE_INT old_args_size;
707
708 /* Entry point to update the canonical frame address (CFA).
709    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
710    calculated from REG+OFFSET.  */
711
712 void
713 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
714 {
715   dw_cfa_location loc;
716   loc.indirect = 0;
717   loc.base_offset = 0;
718   loc.reg = reg;
719   loc.offset = offset;
720   def_cfa_1 (label, &loc);
721 }
722
723 /* Determine if two dw_cfa_location structures define the same data.  */
724
725 static bool
726 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
727 {
728   return (loc1->reg == loc2->reg
729           && loc1->offset == loc2->offset
730           && loc1->indirect == loc2->indirect
731           && (loc1->indirect == 0
732               || loc1->base_offset == loc2->base_offset));
733 }
734
735 /* This routine does the actual work.  The CFA is now calculated from
736    the dw_cfa_location structure.  */
737
738 static void
739 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
740 {
741   dw_cfi_ref cfi;
742   dw_cfa_location old_cfa, loc;
743
744   cfa = *loc_p;
745   loc = *loc_p;
746
747   if (cfa_store.reg == loc.reg && loc.indirect == 0)
748     cfa_store.offset = loc.offset;
749
750   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
751   lookup_cfa (&old_cfa);
752
753   /* If nothing changed, no need to issue any call frame instructions.  */
754   if (cfa_equal_p (&loc, &old_cfa))
755     return;
756
757   cfi = new_cfi ();
758
759   if (loc.reg == old_cfa.reg && !loc.indirect)
760     {
761       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
762          the CFA register did not change but the offset did.  */
763       if (loc.offset < 0)
764         {
765           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
766           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
767
768           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
769           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
770         }
771       else
772         {
773           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
774           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
775         }
776     }
777
778 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
779   else if (loc.offset == old_cfa.offset
780            && old_cfa.reg != INVALID_REGNUM
781            && !loc.indirect)
782     {
783       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
784          indicating the CFA register has changed to <register> but the
785          offset has not changed.  */
786       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
787       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
788     }
789 #endif
790
791   else if (loc.indirect == 0)
792     {
793       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
794          indicating the CFA register has changed to <register> with
795          the specified offset.  */
796       if (loc.offset < 0)
797         {
798           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
799           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
800
801           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
802           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
803           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
804         }
805       else
806         {
807           cfi->dw_cfi_opc = DW_CFA_def_cfa;
808           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
809           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
810         }
811     }
812   else
813     {
814       /* Construct a DW_CFA_def_cfa_expression instruction to
815          calculate the CFA using a full location expression since no
816          register-offset pair is available.  */
817       struct dw_loc_descr_struct *loc_list;
818
819       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
820       loc_list = build_cfa_loc (&loc, 0);
821       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
822     }
823
824   add_fde_cfi (label, cfi);
825 }
826
827 /* Add the CFI for saving a register.  REG is the CFA column number.
828    LABEL is passed to add_fde_cfi.
829    If SREG is -1, the register is saved at OFFSET from the CFA;
830    otherwise it is saved in SREG.  */
831
832 static void
833 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
834 {
835   dw_cfi_ref cfi = new_cfi ();
836
837   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
838
839   if (sreg == INVALID_REGNUM)
840     {
841       if (reg & ~0x3f)
842         /* The register number won't fit in 6 bits, so we have to use
843            the long form.  */
844         cfi->dw_cfi_opc = DW_CFA_offset_extended;
845       else
846         cfi->dw_cfi_opc = DW_CFA_offset;
847
848 #ifdef ENABLE_CHECKING
849       {
850         /* If we get an offset that is not a multiple of
851            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
852            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
853            description.  */
854         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
855
856         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
857       }
858 #endif
859       offset /= DWARF_CIE_DATA_ALIGNMENT;
860       if (offset < 0)
861         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
862
863       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
864     }
865   else if (sreg == reg)
866     cfi->dw_cfi_opc = DW_CFA_same_value;
867   else
868     {
869       cfi->dw_cfi_opc = DW_CFA_register;
870       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
871     }
872
873   add_fde_cfi (label, cfi);
874 }
875
876 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
877    This CFI tells the unwinder that it needs to restore the window registers
878    from the previous frame's window save area.
879
880    ??? Perhaps we should note in the CIE where windows are saved (instead of
881    assuming 0(cfa)) and what registers are in the window.  */
882
883 void
884 dwarf2out_window_save (const char *label)
885 {
886   dw_cfi_ref cfi = new_cfi ();
887
888   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
889   add_fde_cfi (label, cfi);
890 }
891
892 /* Add a CFI to update the running total of the size of arguments
893    pushed onto the stack.  */
894
895 void
896 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
897 {
898   dw_cfi_ref cfi;
899
900   if (size == old_args_size)
901     return;
902
903   old_args_size = size;
904
905   cfi = new_cfi ();
906   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
907   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
908   add_fde_cfi (label, cfi);
909 }
910
911 /* Entry point for saving a register to the stack.  REG is the GCC register
912    number.  LABEL and OFFSET are passed to reg_save.  */
913
914 void
915 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
916 {
917   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
918 }
919
920 /* Entry point for saving the return address in the stack.
921    LABEL and OFFSET are passed to reg_save.  */
922
923 void
924 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
925 {
926   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
927 }
928
929 /* Entry point for saving the return address in a register.
930    LABEL and SREG are passed to reg_save.  */
931
932 void
933 dwarf2out_return_reg (const char *label, unsigned int sreg)
934 {
935   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
936 }
937
938 /* Record the initial position of the return address.  RTL is
939    INCOMING_RETURN_ADDR_RTX.  */
940
941 static void
942 initial_return_save (rtx rtl)
943 {
944   unsigned int reg = INVALID_REGNUM;
945   HOST_WIDE_INT offset = 0;
946
947   switch (GET_CODE (rtl))
948     {
949     case REG:
950       /* RA is in a register.  */
951       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
952       break;
953
954     case MEM:
955       /* RA is on the stack.  */
956       rtl = XEXP (rtl, 0);
957       switch (GET_CODE (rtl))
958         {
959         case REG:
960           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
961           offset = 0;
962           break;
963
964         case PLUS:
965           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
966           offset = INTVAL (XEXP (rtl, 1));
967           break;
968
969         case MINUS:
970           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
971           offset = -INTVAL (XEXP (rtl, 1));
972           break;
973
974         default:
975           gcc_unreachable ();
976         }
977
978       break;
979
980     case PLUS:
981       /* The return address is at some offset from any value we can
982          actually load.  For instance, on the SPARC it is in %i7+8. Just
983          ignore the offset for now; it doesn't matter for unwinding frames.  */
984       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
985       initial_return_save (XEXP (rtl, 0));
986       return;
987
988     default:
989       gcc_unreachable ();
990     }
991
992   if (reg != DWARF_FRAME_RETURN_COLUMN)
993     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
994 }
995
996 /* Given a SET, calculate the amount of stack adjustment it
997    contains.  */
998
999 static HOST_WIDE_INT
1000 stack_adjust_offset (rtx pattern)
1001 {
1002   rtx src = SET_SRC (pattern);
1003   rtx dest = SET_DEST (pattern);
1004   HOST_WIDE_INT offset = 0;
1005   enum rtx_code code;
1006
1007   if (dest == stack_pointer_rtx)
1008     {
1009       /* (set (reg sp) (plus (reg sp) (const_int))) */
1010       code = GET_CODE (src);
1011       if (! (code == PLUS || code == MINUS)
1012           || XEXP (src, 0) != stack_pointer_rtx
1013           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1014         return 0;
1015
1016       offset = INTVAL (XEXP (src, 1));
1017       if (code == PLUS)
1018         offset = -offset;
1019     }
1020   else if (MEM_P (dest))
1021     {
1022       /* (set (mem (pre_dec (reg sp))) (foo)) */
1023       src = XEXP (dest, 0);
1024       code = GET_CODE (src);
1025
1026       switch (code)
1027         {
1028         case PRE_MODIFY:
1029         case POST_MODIFY:
1030           if (XEXP (src, 0) == stack_pointer_rtx)
1031             {
1032               rtx val = XEXP (XEXP (src, 1), 1);
1033               /* We handle only adjustments by constant amount.  */
1034               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1035                           && GET_CODE (val) == CONST_INT);
1036               offset = -INTVAL (val);
1037               break;
1038             }
1039           return 0;
1040
1041         case PRE_DEC:
1042         case POST_DEC:
1043           if (XEXP (src, 0) == stack_pointer_rtx)
1044             {
1045               offset = GET_MODE_SIZE (GET_MODE (dest));
1046               break;
1047             }
1048           return 0;
1049
1050         case PRE_INC:
1051         case POST_INC:
1052           if (XEXP (src, 0) == stack_pointer_rtx)
1053             {
1054               offset = -GET_MODE_SIZE (GET_MODE (dest));
1055               break;
1056             }
1057           return 0;
1058
1059         default:
1060           return 0;
1061         }
1062     }
1063   else
1064     return 0;
1065
1066   return offset;
1067 }
1068
1069 /* Check INSN to see if it looks like a push or a stack adjustment, and
1070    make a note of it if it does.  EH uses this information to find out how
1071    much extra space it needs to pop off the stack.  */
1072
1073 static void
1074 dwarf2out_stack_adjust (rtx insn, bool after_p)
1075 {
1076   HOST_WIDE_INT offset;
1077   const char *label;
1078   int i;
1079
1080   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1081      with this function.  Proper support would require all frame-related
1082      insns to be marked, and to be able to handle saving state around
1083      epilogues textually in the middle of the function.  */
1084   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1085     return;
1086
1087   /* If only calls can throw, and we have a frame pointer,
1088      save up adjustments until we see the CALL_INSN.  */
1089   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1090     {
1091       if (CALL_P (insn) && !after_p)
1092         {
1093           /* Extract the size of the args from the CALL rtx itself.  */
1094           insn = PATTERN (insn);
1095           if (GET_CODE (insn) == PARALLEL)
1096             insn = XVECEXP (insn, 0, 0);
1097           if (GET_CODE (insn) == SET)
1098             insn = SET_SRC (insn);
1099           gcc_assert (GET_CODE (insn) == CALL);
1100           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1101         }
1102       return;
1103     }
1104
1105   if (CALL_P (insn) && !after_p)
1106     {
1107       if (!flag_asynchronous_unwind_tables)
1108         dwarf2out_args_size ("", args_size);
1109       return;
1110     }
1111   else if (BARRIER_P (insn))
1112     {
1113       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1114          the compiler will have already emitted a stack adjustment, but
1115          doesn't bother for calls to noreturn functions.  */
1116 #ifdef STACK_GROWS_DOWNWARD
1117       offset = -args_size;
1118 #else
1119       offset = args_size;
1120 #endif
1121     }
1122   else if (GET_CODE (PATTERN (insn)) == SET)
1123     offset = stack_adjust_offset (PATTERN (insn));
1124   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1125            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1126     {
1127       /* There may be stack adjustments inside compound insns.  Search
1128          for them.  */
1129       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1130         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1131           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1132     }
1133   else
1134     return;
1135
1136   if (offset == 0)
1137     return;
1138
1139   if (cfa.reg == STACK_POINTER_REGNUM)
1140     cfa.offset += offset;
1141
1142 #ifndef STACK_GROWS_DOWNWARD
1143   offset = -offset;
1144 #endif
1145
1146   args_size += offset;
1147   if (args_size < 0)
1148     args_size = 0;
1149
1150   label = dwarf2out_cfi_label ();
1151   def_cfa_1 (label, &cfa);
1152   if (flag_asynchronous_unwind_tables)
1153     dwarf2out_args_size (label, args_size);
1154 }
1155
1156 #endif
1157
1158 /* We delay emitting a register save until either (a) we reach the end
1159    of the prologue or (b) the register is clobbered.  This clusters
1160    register saves so that there are fewer pc advances.  */
1161
1162 struct queued_reg_save GTY(())
1163 {
1164   struct queued_reg_save *next;
1165   rtx reg;
1166   HOST_WIDE_INT cfa_offset;
1167   rtx saved_reg;
1168 };
1169
1170 static GTY(()) struct queued_reg_save *queued_reg_saves;
1171
1172 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1173 struct reg_saved_in_data GTY(()) {
1174   rtx orig_reg;
1175   rtx saved_in_reg;
1176 };
1177
1178 /* A list of registers saved in other registers.
1179    The list intentionally has a small maximum capacity of 4; if your
1180    port needs more than that, you might consider implementing a
1181    more efficient data structure.  */
1182 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1183 static GTY(()) size_t num_regs_saved_in_regs;
1184
1185 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1186 static const char *last_reg_save_label;
1187
1188 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1189    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1190
1191 static void
1192 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1193 {
1194   struct queued_reg_save *q;
1195
1196   /* Duplicates waste space, but it's also necessary to remove them
1197      for correctness, since the queue gets output in reverse
1198      order.  */
1199   for (q = queued_reg_saves; q != NULL; q = q->next)
1200     if (REGNO (q->reg) == REGNO (reg))
1201       break;
1202
1203   if (q == NULL)
1204     {
1205       q = ggc_alloc (sizeof (*q));
1206       q->next = queued_reg_saves;
1207       queued_reg_saves = q;
1208     }
1209
1210   q->reg = reg;
1211   q->cfa_offset = offset;
1212   q->saved_reg = sreg;
1213
1214   last_reg_save_label = label;
1215 }
1216
1217 /* Output all the entries in QUEUED_REG_SAVES.  */
1218
1219 static void
1220 flush_queued_reg_saves (void)
1221 {
1222   struct queued_reg_save *q;
1223
1224   for (q = queued_reg_saves; q; q = q->next)
1225     {
1226       size_t i;
1227       unsigned int reg, sreg;
1228
1229       for (i = 0; i < num_regs_saved_in_regs; i++)
1230         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1231           break;
1232       if (q->saved_reg && i == num_regs_saved_in_regs)
1233         {
1234           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1235           num_regs_saved_in_regs++;
1236         }
1237       if (i != num_regs_saved_in_regs)
1238         {
1239           regs_saved_in_regs[i].orig_reg = q->reg;
1240           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1241         }
1242
1243       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1244       if (q->saved_reg)
1245         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1246       else
1247         sreg = INVALID_REGNUM;
1248       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1249     }
1250
1251   queued_reg_saves = NULL;
1252   last_reg_save_label = NULL;
1253 }
1254
1255 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1256    location for?  Or, does it clobber a register which we've previously
1257    said that some other register is saved in, and for which we now
1258    have a new location for?  */
1259
1260 static bool
1261 clobbers_queued_reg_save (rtx insn)
1262 {
1263   struct queued_reg_save *q;
1264
1265   for (q = queued_reg_saves; q; q = q->next)
1266     {
1267       size_t i;
1268       if (modified_in_p (q->reg, insn))
1269         return true;
1270       for (i = 0; i < num_regs_saved_in_regs; i++)
1271         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1272             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1273           return true;
1274     }
1275
1276   return false;
1277 }
1278
1279 /* Entry point for saving the first register into the second.  */
1280
1281 void
1282 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1283 {
1284   size_t i;
1285   unsigned int regno, sregno;
1286
1287   for (i = 0; i < num_regs_saved_in_regs; i++)
1288     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1289       break;
1290   if (i == num_regs_saved_in_regs)
1291     {
1292       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1293       num_regs_saved_in_regs++;
1294     }
1295   regs_saved_in_regs[i].orig_reg = reg;
1296   regs_saved_in_regs[i].saved_in_reg = sreg;
1297
1298   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1299   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1300   reg_save (label, regno, sregno, 0);
1301 }
1302
1303 /* What register, if any, is currently saved in REG?  */
1304
1305 static rtx
1306 reg_saved_in (rtx reg)
1307 {
1308   unsigned int regn = REGNO (reg);
1309   size_t i;
1310   struct queued_reg_save *q;
1311
1312   for (q = queued_reg_saves; q; q = q->next)
1313     if (q->saved_reg && regn == REGNO (q->saved_reg))
1314       return q->reg;
1315
1316   for (i = 0; i < num_regs_saved_in_regs; i++)
1317     if (regs_saved_in_regs[i].saved_in_reg
1318         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1319       return regs_saved_in_regs[i].orig_reg;
1320
1321   return NULL_RTX;
1322 }
1323
1324
1325 /* A temporary register holding an integral value used in adjusting SP
1326    or setting up the store_reg.  The "offset" field holds the integer
1327    value, not an offset.  */
1328 static dw_cfa_location cfa_temp;
1329
1330 /* Record call frame debugging information for an expression EXPR,
1331    which either sets SP or FP (adjusting how we calculate the frame
1332    address) or saves a register to the stack or another register.
1333    LABEL indicates the address of EXPR.
1334
1335    This function encodes a state machine mapping rtxes to actions on
1336    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1337    users need not read the source code.
1338
1339   The High-Level Picture
1340
1341   Changes in the register we use to calculate the CFA: Currently we
1342   assume that if you copy the CFA register into another register, we
1343   should take the other one as the new CFA register; this seems to
1344   work pretty well.  If it's wrong for some target, it's simple
1345   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1346
1347   Changes in the register we use for saving registers to the stack:
1348   This is usually SP, but not always.  Again, we deduce that if you
1349   copy SP into another register (and SP is not the CFA register),
1350   then the new register is the one we will be using for register
1351   saves.  This also seems to work.
1352
1353   Register saves: There's not much guesswork about this one; if
1354   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1355   register save, and the register used to calculate the destination
1356   had better be the one we think we're using for this purpose.
1357   It's also assumed that a copy from a call-saved register to another
1358   register is saving that register if RTX_FRAME_RELATED_P is set on
1359   that instruction.  If the copy is from a call-saved register to
1360   the *same* register, that means that the register is now the same
1361   value as in the caller.
1362
1363   Except: If the register being saved is the CFA register, and the
1364   offset is nonzero, we are saving the CFA, so we assume we have to
1365   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1366   the intent is to save the value of SP from the previous frame.
1367
1368   In addition, if a register has previously been saved to a different
1369   register,
1370
1371   Invariants / Summaries of Rules
1372
1373   cfa          current rule for calculating the CFA.  It usually
1374                consists of a register and an offset.
1375   cfa_store    register used by prologue code to save things to the stack
1376                cfa_store.offset is the offset from the value of
1377                cfa_store.reg to the actual CFA
1378   cfa_temp     register holding an integral value.  cfa_temp.offset
1379                stores the value, which will be used to adjust the
1380                stack pointer.  cfa_temp is also used like cfa_store,
1381                to track stores to the stack via fp or a temp reg.
1382
1383   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1384                with cfa.reg as the first operand changes the cfa.reg and its
1385                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1386                cfa_temp.offset.
1387
1388   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1389                expression yielding a constant.  This sets cfa_temp.reg
1390                and cfa_temp.offset.
1391
1392   Rule 5:      Create a new register cfa_store used to save items to the
1393                stack.
1394
1395   Rules 10-14: Save a register to the stack.  Define offset as the
1396                difference of the original location and cfa_store's
1397                location (or cfa_temp's location if cfa_temp is used).
1398
1399   The Rules
1400
1401   "{a,b}" indicates a choice of a xor b.
1402   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1403
1404   Rule 1:
1405   (set <reg1> <reg2>:cfa.reg)
1406   effects: cfa.reg = <reg1>
1407            cfa.offset unchanged
1408            cfa_temp.reg = <reg1>
1409            cfa_temp.offset = cfa.offset
1410
1411   Rule 2:
1412   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1413                               {<const_int>,<reg>:cfa_temp.reg}))
1414   effects: cfa.reg = sp if fp used
1415            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1416            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1417              if cfa_store.reg==sp
1418
1419   Rule 3:
1420   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1421   effects: cfa.reg = fp
1422            cfa_offset += +/- <const_int>
1423
1424   Rule 4:
1425   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1426   constraints: <reg1> != fp
1427                <reg1> != sp
1428   effects: cfa.reg = <reg1>
1429            cfa_temp.reg = <reg1>
1430            cfa_temp.offset = cfa.offset
1431
1432   Rule 5:
1433   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1434   constraints: <reg1> != fp
1435                <reg1> != sp
1436   effects: cfa_store.reg = <reg1>
1437            cfa_store.offset = cfa.offset - cfa_temp.offset
1438
1439   Rule 6:
1440   (set <reg> <const_int>)
1441   effects: cfa_temp.reg = <reg>
1442            cfa_temp.offset = <const_int>
1443
1444   Rule 7:
1445   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1446   effects: cfa_temp.reg = <reg1>
1447            cfa_temp.offset |= <const_int>
1448
1449   Rule 8:
1450   (set <reg> (high <exp>))
1451   effects: none
1452
1453   Rule 9:
1454   (set <reg> (lo_sum <exp> <const_int>))
1455   effects: cfa_temp.reg = <reg>
1456            cfa_temp.offset = <const_int>
1457
1458   Rule 10:
1459   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1460   effects: cfa_store.offset -= <const_int>
1461            cfa.offset = cfa_store.offset if cfa.reg == sp
1462            cfa.reg = sp
1463            cfa.base_offset = -cfa_store.offset
1464
1465   Rule 11:
1466   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1467   effects: cfa_store.offset += -/+ mode_size(mem)
1468            cfa.offset = cfa_store.offset if cfa.reg == sp
1469            cfa.reg = sp
1470            cfa.base_offset = -cfa_store.offset
1471
1472   Rule 12:
1473   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1474
1475        <reg2>)
1476   effects: cfa.reg = <reg1>
1477            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1478
1479   Rule 13:
1480   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1481   effects: cfa.reg = <reg1>
1482            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1483
1484   Rule 14:
1485   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1486   effects: cfa.reg = <reg1>
1487            cfa.base_offset = -cfa_temp.offset
1488            cfa_temp.offset -= mode_size(mem)
1489
1490   Rule 15:
1491   (set <reg> {unspec, unspec_volatile})
1492   effects: target-dependent  */
1493
1494 static void
1495 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1496 {
1497   rtx src, dest;
1498   HOST_WIDE_INT offset;
1499
1500   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1501      the PARALLEL independently. The first element is always processed if
1502      it is a SET. This is for backward compatibility.   Other elements
1503      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1504      flag is set in them.  */
1505   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1506     {
1507       int par_index;
1508       int limit = XVECLEN (expr, 0);
1509
1510       for (par_index = 0; par_index < limit; par_index++)
1511         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1512             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1513                 || par_index == 0))
1514           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1515
1516       return;
1517     }
1518
1519   gcc_assert (GET_CODE (expr) == SET);
1520
1521   src = SET_SRC (expr);
1522   dest = SET_DEST (expr);
1523
1524   if (REG_P (src))
1525     {
1526       rtx rsi = reg_saved_in (src);
1527       if (rsi)
1528         src = rsi;
1529     }
1530
1531   switch (GET_CODE (dest))
1532     {
1533     case REG:
1534       switch (GET_CODE (src))
1535         {
1536           /* Setting FP from SP.  */
1537         case REG:
1538           if (cfa.reg == (unsigned) REGNO (src))
1539             {
1540               /* Rule 1 */
1541               /* Update the CFA rule wrt SP or FP.  Make sure src is
1542                  relative to the current CFA register.
1543
1544                  We used to require that dest be either SP or FP, but the
1545                  ARM copies SP to a temporary register, and from there to
1546                  FP.  So we just rely on the backends to only set
1547                  RTX_FRAME_RELATED_P on appropriate insns.  */
1548               cfa.reg = REGNO (dest);
1549               cfa_temp.reg = cfa.reg;
1550               cfa_temp.offset = cfa.offset;
1551             }
1552           else
1553             {
1554               /* Saving a register in a register.  */
1555               gcc_assert (!fixed_regs [REGNO (dest)]
1556                           /* For the SPARC and its register window.  */
1557                           || (DWARF_FRAME_REGNUM (REGNO (src))
1558                               == DWARF_FRAME_RETURN_COLUMN));
1559               queue_reg_save (label, src, dest, 0);
1560             }
1561           break;
1562
1563         case PLUS:
1564         case MINUS:
1565         case LO_SUM:
1566           if (dest == stack_pointer_rtx)
1567             {
1568               /* Rule 2 */
1569               /* Adjusting SP.  */
1570               switch (GET_CODE (XEXP (src, 1)))
1571                 {
1572                 case CONST_INT:
1573                   offset = INTVAL (XEXP (src, 1));
1574                   break;
1575                 case REG:
1576                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1577                               == cfa_temp.reg);
1578                   offset = cfa_temp.offset;
1579                   break;
1580                 default:
1581                   gcc_unreachable ();
1582                 }
1583
1584               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1585                 {
1586                   /* Restoring SP from FP in the epilogue.  */
1587                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1588                   cfa.reg = STACK_POINTER_REGNUM;
1589                 }
1590               else if (GET_CODE (src) == LO_SUM)
1591                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1592                 ;
1593               else
1594                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1595
1596               if (GET_CODE (src) != MINUS)
1597                 offset = -offset;
1598               if (cfa.reg == STACK_POINTER_REGNUM)
1599                 cfa.offset += offset;
1600               if (cfa_store.reg == STACK_POINTER_REGNUM)
1601                 cfa_store.offset += offset;
1602             }
1603           else if (dest == hard_frame_pointer_rtx)
1604             {
1605               /* Rule 3 */
1606               /* Either setting the FP from an offset of the SP,
1607                  or adjusting the FP */
1608               gcc_assert (frame_pointer_needed);
1609
1610               gcc_assert (REG_P (XEXP (src, 0))
1611                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1612                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1613               offset = INTVAL (XEXP (src, 1));
1614               if (GET_CODE (src) != MINUS)
1615                 offset = -offset;
1616               cfa.offset += offset;
1617               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1618             }
1619           else
1620             {
1621               gcc_assert (GET_CODE (src) != MINUS);
1622
1623               /* Rule 4 */
1624               if (REG_P (XEXP (src, 0))
1625                   && REGNO (XEXP (src, 0)) == cfa.reg
1626                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1627                 {
1628                   /* Setting a temporary CFA register that will be copied
1629                      into the FP later on.  */
1630                   offset = - INTVAL (XEXP (src, 1));
1631                   cfa.offset += offset;
1632                   cfa.reg = REGNO (dest);
1633                   /* Or used to save regs to the stack.  */
1634                   cfa_temp.reg = cfa.reg;
1635                   cfa_temp.offset = cfa.offset;
1636                 }
1637
1638               /* Rule 5 */
1639               else if (REG_P (XEXP (src, 0))
1640                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1641                        && XEXP (src, 1) == stack_pointer_rtx)
1642                 {
1643                   /* Setting a scratch register that we will use instead
1644                      of SP for saving registers to the stack.  */
1645                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1646                   cfa_store.reg = REGNO (dest);
1647                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1648                 }
1649
1650               /* Rule 9 */
1651               else if (GET_CODE (src) == LO_SUM
1652                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1653                 {
1654                   cfa_temp.reg = REGNO (dest);
1655                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1656                 }
1657               else
1658                 gcc_unreachable ();
1659             }
1660           break;
1661
1662           /* Rule 6 */
1663         case CONST_INT:
1664           cfa_temp.reg = REGNO (dest);
1665           cfa_temp.offset = INTVAL (src);
1666           break;
1667
1668           /* Rule 7 */
1669         case IOR:
1670           gcc_assert (REG_P (XEXP (src, 0))
1671                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1672                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1673
1674           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1675             cfa_temp.reg = REGNO (dest);
1676           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1677           break;
1678
1679           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1680              which will fill in all of the bits.  */
1681           /* Rule 8 */
1682         case HIGH:
1683           break;
1684
1685           /* Rule 15 */
1686         case UNSPEC:
1687         case UNSPEC_VOLATILE:
1688           gcc_assert (targetm.dwarf_handle_frame_unspec);
1689           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1690           return;
1691
1692         default:
1693           gcc_unreachable ();
1694         }
1695
1696       def_cfa_1 (label, &cfa);
1697       break;
1698
1699     case MEM:
1700       gcc_assert (REG_P (src));
1701
1702       /* Saving a register to the stack.  Make sure dest is relative to the
1703          CFA register.  */
1704       switch (GET_CODE (XEXP (dest, 0)))
1705         {
1706           /* Rule 10 */
1707           /* With a push.  */
1708         case PRE_MODIFY:
1709           /* We can't handle variable size modifications.  */
1710           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1711                       == CONST_INT);
1712           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1713
1714           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1715                       && cfa_store.reg == STACK_POINTER_REGNUM);
1716
1717           cfa_store.offset += offset;
1718           if (cfa.reg == STACK_POINTER_REGNUM)
1719             cfa.offset = cfa_store.offset;
1720
1721           offset = -cfa_store.offset;
1722           break;
1723
1724           /* Rule 11 */
1725         case PRE_INC:
1726         case PRE_DEC:
1727           offset = GET_MODE_SIZE (GET_MODE (dest));
1728           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1729             offset = -offset;
1730
1731           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1732                       && cfa_store.reg == STACK_POINTER_REGNUM);
1733
1734           cfa_store.offset += offset;
1735           if (cfa.reg == STACK_POINTER_REGNUM)
1736             cfa.offset = cfa_store.offset;
1737
1738           offset = -cfa_store.offset;
1739           break;
1740
1741           /* Rule 12 */
1742           /* With an offset.  */
1743         case PLUS:
1744         case MINUS:
1745         case LO_SUM:
1746           {
1747             int regno;
1748
1749             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1750                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1751             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1752             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1753               offset = -offset;
1754
1755             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1756
1757             if (cfa_store.reg == (unsigned) regno)
1758               offset -= cfa_store.offset;
1759             else
1760               {
1761                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1762                 offset -= cfa_temp.offset;
1763               }
1764           }
1765           break;
1766
1767           /* Rule 13 */
1768           /* Without an offset.  */
1769         case REG:
1770           {
1771             int regno = REGNO (XEXP (dest, 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 14 */
1784         case POST_INC:
1785           gcc_assert (cfa_temp.reg
1786                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1787           offset = -cfa_temp.offset;
1788           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1789           break;
1790
1791         default:
1792           gcc_unreachable ();
1793         }
1794
1795       if (REGNO (src) != STACK_POINTER_REGNUM
1796           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1797           && (unsigned) REGNO (src) == cfa.reg)
1798         {
1799           /* We're storing the current CFA reg into the stack.  */
1800
1801           if (cfa.offset == 0)
1802             {
1803               /* If the source register is exactly the CFA, assume
1804                  we're saving SP like any other register; this happens
1805                  on the ARM.  */
1806               def_cfa_1 (label, &cfa);
1807               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1808               break;
1809             }
1810           else
1811             {
1812               /* Otherwise, we'll need to look in the stack to
1813                  calculate the CFA.  */
1814               rtx x = XEXP (dest, 0);
1815
1816               if (!REG_P (x))
1817                 x = XEXP (x, 0);
1818               gcc_assert (REG_P (x));
1819
1820               cfa.reg = REGNO (x);
1821               cfa.base_offset = offset;
1822               cfa.indirect = 1;
1823               def_cfa_1 (label, &cfa);
1824               break;
1825             }
1826         }
1827
1828       def_cfa_1 (label, &cfa);
1829       queue_reg_save (label, src, NULL_RTX, offset);
1830       break;
1831
1832     default:
1833       gcc_unreachable ();
1834     }
1835 }
1836
1837 /* Record call frame debugging information for INSN, which either
1838    sets SP or FP (adjusting how we calculate the frame address) or saves a
1839    register to the stack.  If INSN is NULL_RTX, initialize our state.
1840
1841    If AFTER_P is false, we're being called before the insn is emitted,
1842    otherwise after.  Call instructions get invoked twice.  */
1843
1844 void
1845 dwarf2out_frame_debug (rtx insn, bool after_p)
1846 {
1847   const char *label;
1848   rtx src;
1849
1850   if (insn == NULL_RTX)
1851     {
1852       size_t i;
1853
1854       /* Flush any queued register saves.  */
1855       flush_queued_reg_saves ();
1856
1857       /* Set up state for generating call frame debug info.  */
1858       lookup_cfa (&cfa);
1859       gcc_assert (cfa.reg
1860                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1861
1862       cfa.reg = STACK_POINTER_REGNUM;
1863       cfa_store = cfa;
1864       cfa_temp.reg = -1;
1865       cfa_temp.offset = 0;
1866
1867       for (i = 0; i < num_regs_saved_in_regs; i++)
1868         {
1869           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1870           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1871         }
1872       num_regs_saved_in_regs = 0;
1873       return;
1874     }
1875
1876   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1877     flush_queued_reg_saves ();
1878
1879   if (! RTX_FRAME_RELATED_P (insn))
1880     {
1881       if (!ACCUMULATE_OUTGOING_ARGS)
1882         dwarf2out_stack_adjust (insn, after_p);
1883       return;
1884     }
1885
1886   label = dwarf2out_cfi_label ();
1887   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1888   if (src)
1889     insn = XEXP (src, 0);
1890   else
1891     insn = PATTERN (insn);
1892
1893   dwarf2out_frame_debug_expr (insn, label);
1894 }
1895
1896 #endif
1897
1898 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1899 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1900  (enum dwarf_call_frame_info cfi);
1901
1902 static enum dw_cfi_oprnd_type
1903 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1904 {
1905   switch (cfi)
1906     {
1907     case DW_CFA_nop:
1908     case DW_CFA_GNU_window_save:
1909       return dw_cfi_oprnd_unused;
1910
1911     case DW_CFA_set_loc:
1912     case DW_CFA_advance_loc1:
1913     case DW_CFA_advance_loc2:
1914     case DW_CFA_advance_loc4:
1915     case DW_CFA_MIPS_advance_loc8:
1916       return dw_cfi_oprnd_addr;
1917
1918     case DW_CFA_offset:
1919     case DW_CFA_offset_extended:
1920     case DW_CFA_def_cfa:
1921     case DW_CFA_offset_extended_sf:
1922     case DW_CFA_def_cfa_sf:
1923     case DW_CFA_restore_extended:
1924     case DW_CFA_undefined:
1925     case DW_CFA_same_value:
1926     case DW_CFA_def_cfa_register:
1927     case DW_CFA_register:
1928       return dw_cfi_oprnd_reg_num;
1929
1930     case DW_CFA_def_cfa_offset:
1931     case DW_CFA_GNU_args_size:
1932     case DW_CFA_def_cfa_offset_sf:
1933       return dw_cfi_oprnd_offset;
1934
1935     case DW_CFA_def_cfa_expression:
1936     case DW_CFA_expression:
1937       return dw_cfi_oprnd_loc;
1938
1939     default:
1940       gcc_unreachable ();
1941     }
1942 }
1943
1944 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1945 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1946  (enum dwarf_call_frame_info cfi);
1947
1948 static enum dw_cfi_oprnd_type
1949 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1950 {
1951   switch (cfi)
1952     {
1953     case DW_CFA_def_cfa:
1954     case DW_CFA_def_cfa_sf:
1955     case DW_CFA_offset:
1956     case DW_CFA_offset_extended_sf:
1957     case DW_CFA_offset_extended:
1958       return dw_cfi_oprnd_offset;
1959
1960     case DW_CFA_register:
1961       return dw_cfi_oprnd_reg_num;
1962
1963     default:
1964       return dw_cfi_oprnd_unused;
1965     }
1966 }
1967
1968 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1969
1970 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
1971    switch to the data section instead, and write out a synthetic label
1972    for collect2.  */
1973
1974 static void
1975 switch_to_eh_frame_section (void)
1976 {
1977   tree label;
1978
1979 #ifdef EH_FRAME_SECTION_NAME
1980   if (eh_frame_section == 0)
1981     {
1982       int flags;
1983
1984       if (EH_TABLES_CAN_BE_READ_ONLY)
1985         {
1986           int fde_encoding;
1987           int per_encoding;
1988           int lsda_encoding;
1989
1990           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
1991                                                        /*global=*/0);
1992           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
1993                                                        /*global=*/1);
1994           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
1995                                                         /*global=*/0);
1996           flags = ((! flag_pic
1997                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
1998                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
1999                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2000                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2001                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2002                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2003                    ? 0 : SECTION_WRITE);
2004         }
2005       else
2006         flags = SECTION_WRITE;
2007       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2008     }
2009 #endif
2010
2011   if (eh_frame_section)
2012     switch_to_section (eh_frame_section);
2013   else
2014     {
2015       /* We have no special eh_frame section.  Put the information in
2016          the data section and emit special labels to guide collect2.  */
2017       switch_to_section (data_section);
2018       label = get_file_function_name ('F');
2019       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2020       targetm.asm_out.globalize_label (asm_out_file,
2021                                        IDENTIFIER_POINTER (label));
2022       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2023     }
2024 }
2025
2026 /* Output a Call Frame Information opcode and its operand(s).  */
2027
2028 static void
2029 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2030 {
2031   unsigned long r;
2032   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2033     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2034                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2035                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2036                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2037   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2038     {
2039       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2040       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2041                            "DW_CFA_offset, column 0x%lx", r);
2042       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2043     }
2044   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2045     {
2046       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2047       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2048                            "DW_CFA_restore, column 0x%lx", r);
2049     }
2050   else
2051     {
2052       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2053                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2054
2055       switch (cfi->dw_cfi_opc)
2056         {
2057         case DW_CFA_set_loc:
2058           if (for_eh)
2059             dw2_asm_output_encoded_addr_rtx (
2060                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2061                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2062                 false, NULL);
2063           else
2064             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2065                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2066           break;
2067
2068         case DW_CFA_advance_loc1:
2069           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2070                                 fde->dw_fde_current_label, NULL);
2071           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2072           break;
2073
2074         case DW_CFA_advance_loc2:
2075           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2076                                 fde->dw_fde_current_label, NULL);
2077           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2078           break;
2079
2080         case DW_CFA_advance_loc4:
2081           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2082                                 fde->dw_fde_current_label, NULL);
2083           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2084           break;
2085
2086         case DW_CFA_MIPS_advance_loc8:
2087           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2088                                 fde->dw_fde_current_label, NULL);
2089           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2090           break;
2091
2092         case DW_CFA_offset_extended:
2093         case DW_CFA_def_cfa:
2094           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2095           dw2_asm_output_data_uleb128 (r, NULL);
2096           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2097           break;
2098
2099         case DW_CFA_offset_extended_sf:
2100         case DW_CFA_def_cfa_sf:
2101           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2102           dw2_asm_output_data_uleb128 (r, NULL);
2103           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2104           break;
2105
2106         case DW_CFA_restore_extended:
2107         case DW_CFA_undefined:
2108         case DW_CFA_same_value:
2109         case DW_CFA_def_cfa_register:
2110           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2111           dw2_asm_output_data_uleb128 (r, NULL);
2112           break;
2113
2114         case DW_CFA_register:
2115           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2116           dw2_asm_output_data_uleb128 (r, NULL);
2117           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2118           dw2_asm_output_data_uleb128 (r, NULL);
2119           break;
2120
2121         case DW_CFA_def_cfa_offset:
2122         case DW_CFA_GNU_args_size:
2123           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2124           break;
2125
2126         case DW_CFA_def_cfa_offset_sf:
2127           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2128           break;
2129
2130         case DW_CFA_GNU_window_save:
2131           break;
2132
2133         case DW_CFA_def_cfa_expression:
2134         case DW_CFA_expression:
2135           output_cfa_loc (cfi);
2136           break;
2137
2138         case DW_CFA_GNU_negative_offset_extended:
2139           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2140           gcc_unreachable ();
2141
2142         default:
2143           break;
2144         }
2145     }
2146 }
2147
2148 /* Output the call frame information used to record information
2149    that relates to calculating the frame pointer, and records the
2150    location of saved registers.  */
2151
2152 static void
2153 output_call_frame_info (int for_eh)
2154 {
2155   unsigned int i;
2156   dw_fde_ref fde;
2157   dw_cfi_ref cfi;
2158   char l1[20], l2[20], section_start_label[20];
2159   bool any_lsda_needed = false;
2160   char augmentation[6];
2161   int augmentation_size;
2162   int fde_encoding = DW_EH_PE_absptr;
2163   int per_encoding = DW_EH_PE_absptr;
2164   int lsda_encoding = DW_EH_PE_absptr;
2165   int return_reg;
2166
2167   /* Don't emit a CIE if there won't be any FDEs.  */
2168   if (fde_table_in_use == 0)
2169     return;
2170
2171   /* If we make FDEs linkonce, we may have to emit an empty label for
2172      an FDE that wouldn't otherwise be emitted.  We want to avoid
2173      having an FDE kept around when the function it refers to is
2174      discarded.  Example where this matters: a primary function
2175      template in C++ requires EH information, but an explicit
2176      specialization doesn't.  */
2177   if (TARGET_USES_WEAK_UNWIND_INFO
2178       && ! flag_asynchronous_unwind_tables
2179       && for_eh)
2180     for (i = 0; i < fde_table_in_use; i++)
2181       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2182           && !fde_table[i].uses_eh_lsda
2183           && ! DECL_WEAK (fde_table[i].decl))
2184         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2185                                       for_eh, /* empty */ 1);
2186
2187   /* If we don't have any functions we'll want to unwind out of, don't
2188      emit any EH unwind information.  Note that if exceptions aren't
2189      enabled, we won't have collected nothrow information, and if we
2190      asked for asynchronous tables, we always want this info.  */
2191   if (for_eh)
2192     {
2193       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2194
2195       for (i = 0; i < fde_table_in_use; i++)
2196         if (fde_table[i].uses_eh_lsda)
2197           any_eh_needed = any_lsda_needed = true;
2198         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2199           any_eh_needed = true;
2200         else if (! fde_table[i].nothrow
2201                  && ! fde_table[i].all_throwers_are_sibcalls)
2202           any_eh_needed = true;
2203
2204       if (! any_eh_needed)
2205         return;
2206     }
2207
2208   /* We're going to be generating comments, so turn on app.  */
2209   if (flag_debug_asm)
2210     app_enable ();
2211
2212   if (for_eh)
2213     switch_to_eh_frame_section ();
2214   else
2215     switch_to_section (get_section (DEBUG_FRAME_SECTION, SECTION_DEBUG, NULL));
2216
2217   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2218   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2219
2220   /* Output the CIE.  */
2221   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2222   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2223   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2224                         "Length of Common Information Entry");
2225   ASM_OUTPUT_LABEL (asm_out_file, l1);
2226
2227   /* Now that the CIE pointer is PC-relative for EH,
2228      use 0 to identify the CIE.  */
2229   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2230                        (for_eh ? 0 : DW_CIE_ID),
2231                        "CIE Identifier Tag");
2232
2233   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2234
2235   augmentation[0] = 0;
2236   augmentation_size = 0;
2237   if (for_eh)
2238     {
2239       char *p;
2240
2241       /* Augmentation:
2242          z      Indicates that a uleb128 is present to size the
2243                 augmentation section.
2244          L      Indicates the encoding (and thus presence) of
2245                 an LSDA pointer in the FDE augmentation.
2246          R      Indicates a non-default pointer encoding for
2247                 FDE code pointers.
2248          P      Indicates the presence of an encoding + language
2249                 personality routine in the CIE augmentation.  */
2250
2251       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2252       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2253       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2254
2255       p = augmentation + 1;
2256       if (eh_personality_libfunc)
2257         {
2258           *p++ = 'P';
2259           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2260         }
2261       if (any_lsda_needed)
2262         {
2263           *p++ = 'L';
2264           augmentation_size += 1;
2265         }
2266       if (fde_encoding != DW_EH_PE_absptr)
2267         {
2268           *p++ = 'R';
2269           augmentation_size += 1;
2270         }
2271       if (p > augmentation + 1)
2272         {
2273           augmentation[0] = 'z';
2274           *p = '\0';
2275         }
2276
2277       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2278       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2279         {
2280           int offset = (  4             /* Length */
2281                         + 4             /* CIE Id */
2282                         + 1             /* CIE version */
2283                         + strlen (augmentation) + 1     /* Augmentation */
2284                         + size_of_uleb128 (1)           /* Code alignment */
2285                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2286                         + 1             /* RA column */
2287                         + 1             /* Augmentation size */
2288                         + 1             /* Personality encoding */ );
2289           int pad = -offset & (PTR_SIZE - 1);
2290
2291           augmentation_size += pad;
2292
2293           /* Augmentations should be small, so there's scarce need to
2294              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2295           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2296         }
2297     }
2298
2299   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2300   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2301   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2302                                "CIE Data Alignment Factor");
2303
2304   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2305   if (DW_CIE_VERSION == 1)
2306     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2307   else
2308     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2309
2310   if (augmentation[0])
2311     {
2312       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2313       if (eh_personality_libfunc)
2314         {
2315           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2316                                eh_data_format_name (per_encoding));
2317           dw2_asm_output_encoded_addr_rtx (per_encoding,
2318                                            eh_personality_libfunc,
2319                                            true, NULL);
2320         }
2321
2322       if (any_lsda_needed)
2323         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2324                              eh_data_format_name (lsda_encoding));
2325
2326       if (fde_encoding != DW_EH_PE_absptr)
2327         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2328                              eh_data_format_name (fde_encoding));
2329     }
2330
2331   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2332     output_cfi (cfi, NULL, for_eh);
2333
2334   /* Pad the CIE out to an address sized boundary.  */
2335   ASM_OUTPUT_ALIGN (asm_out_file,
2336                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2337   ASM_OUTPUT_LABEL (asm_out_file, l2);
2338
2339   /* Loop through all of the FDE's.  */
2340   for (i = 0; i < fde_table_in_use; i++)
2341     {
2342       fde = &fde_table[i];
2343
2344       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2345       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2346           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2347           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2348           && !fde->uses_eh_lsda)
2349         continue;
2350
2351       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2352       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2353       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2354       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2355       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2356                             "FDE Length");
2357       ASM_OUTPUT_LABEL (asm_out_file, l1);
2358
2359       if (for_eh)
2360         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2361       else
2362         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2363                                "FDE CIE offset");
2364
2365       if (for_eh)
2366         {
2367           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2368           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2369           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2370                                            sym_ref,
2371                                            false,
2372                                            "FDE initial location");
2373           if (fde->dw_fde_switched_sections)
2374             {
2375               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode, 
2376                                       fde->dw_fde_unlikely_section_label);
2377               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode, 
2378                                       fde->dw_fde_hot_section_label);
2379               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2380               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2381               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2382                                                "FDE initial location");
2383               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2384                                     fde->dw_fde_hot_section_end_label,
2385                                     fde->dw_fde_hot_section_label,
2386                                     "FDE address range");
2387               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2388                                                "FDE initial location");
2389               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2390                                     fde->dw_fde_unlikely_section_end_label,
2391                                     fde->dw_fde_unlikely_section_label,
2392                                     "FDE address range");
2393             }
2394           else
2395             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2396                                   fde->dw_fde_end, fde->dw_fde_begin,
2397                                   "FDE address range");
2398         }
2399       else
2400         {
2401           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2402                                "FDE initial location");
2403           if (fde->dw_fde_switched_sections)
2404             {
2405               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2406                                    fde->dw_fde_hot_section_label,
2407                                    "FDE initial location");
2408               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2409                                     fde->dw_fde_hot_section_end_label,
2410                                     fde->dw_fde_hot_section_label,
2411                                     "FDE address range");
2412               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2413                                    fde->dw_fde_unlikely_section_label,
2414                                    "FDE initial location");
2415               dw2_asm_output_delta (DWARF2_ADDR_SIZE, 
2416                                     fde->dw_fde_unlikely_section_end_label,
2417                                     fde->dw_fde_unlikely_section_label,
2418                                     "FDE address range");
2419             }
2420           else
2421             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2422                                   fde->dw_fde_end, fde->dw_fde_begin,
2423                                   "FDE address range");
2424         }
2425
2426       if (augmentation[0])
2427         {
2428           if (any_lsda_needed)
2429             {
2430               int size = size_of_encoded_value (lsda_encoding);
2431
2432               if (lsda_encoding == DW_EH_PE_aligned)
2433                 {
2434                   int offset = (  4             /* Length */
2435                                 + 4             /* CIE offset */
2436                                 + 2 * size_of_encoded_value (fde_encoding)
2437                                 + 1             /* Augmentation size */ );
2438                   int pad = -offset & (PTR_SIZE - 1);
2439
2440                   size += pad;
2441                   gcc_assert (size_of_uleb128 (size) == 1);
2442                 }
2443
2444               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2445
2446               if (fde->uses_eh_lsda)
2447                 {
2448                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2449                                                fde->funcdef_number);
2450                   dw2_asm_output_encoded_addr_rtx (
2451                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2452                         false, "Language Specific Data Area");
2453                 }
2454               else
2455                 {
2456                   if (lsda_encoding == DW_EH_PE_aligned)
2457                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2458                   dw2_asm_output_data
2459                     (size_of_encoded_value (lsda_encoding), 0,
2460                      "Language Specific Data Area (none)");
2461                 }
2462             }
2463           else
2464             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2465         }
2466
2467       /* Loop through the Call Frame Instructions associated with
2468          this FDE.  */
2469       fde->dw_fde_current_label = fde->dw_fde_begin;
2470       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2471         output_cfi (cfi, fde, for_eh);
2472
2473       /* Pad the FDE out to an address sized boundary.  */
2474       ASM_OUTPUT_ALIGN (asm_out_file,
2475                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2476       ASM_OUTPUT_LABEL (asm_out_file, l2);
2477     }
2478
2479   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2480     dw2_asm_output_data (4, 0, "End of Table");
2481 #ifdef MIPS_DEBUGGING_INFO
2482   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2483      get a value of 0.  Putting .align 0 after the label fixes it.  */
2484   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2485 #endif
2486
2487   /* Turn off app to make assembly quicker.  */
2488   if (flag_debug_asm)
2489     app_disable ();
2490 }
2491
2492 /* Output a marker (i.e. a label) for the beginning of a function, before
2493    the prologue.  */
2494
2495 void
2496 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2497                           const char *file ATTRIBUTE_UNUSED)
2498 {
2499   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2500   char * dup_label;
2501   dw_fde_ref fde;
2502
2503   current_function_func_begin_label = NULL;
2504
2505 #ifdef TARGET_UNWIND_INFO
2506   /* ??? current_function_func_begin_label is also used by except.c
2507      for call-site information.  We must emit this label if it might
2508      be used.  */
2509   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2510       && ! dwarf2out_do_frame ())
2511     return;
2512 #else
2513   if (! dwarf2out_do_frame ())
2514     return;
2515 #endif
2516
2517   switch_to_section (function_section (current_function_decl));
2518   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2519                                current_function_funcdef_no);
2520   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2521                           current_function_funcdef_no);
2522   dup_label = xstrdup (label);
2523   current_function_func_begin_label = dup_label;
2524
2525 #ifdef TARGET_UNWIND_INFO
2526   /* We can elide the fde allocation if we're not emitting debug info.  */
2527   if (! dwarf2out_do_frame ())
2528     return;
2529 #endif
2530
2531   /* Expand the fde table if necessary.  */
2532   if (fde_table_in_use == fde_table_allocated)
2533     {
2534       fde_table_allocated += FDE_TABLE_INCREMENT;
2535       fde_table = ggc_realloc (fde_table,
2536                                fde_table_allocated * sizeof (dw_fde_node));
2537       memset (fde_table + fde_table_in_use, 0,
2538               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2539     }
2540
2541   /* Record the FDE associated with this function.  */
2542   current_funcdef_fde = fde_table_in_use;
2543
2544   /* Add the new FDE at the end of the fde_table.  */
2545   fde = &fde_table[fde_table_in_use++];
2546   fde->decl = current_function_decl;
2547   fde->dw_fde_begin = dup_label;
2548   fde->dw_fde_current_label = NULL;
2549   fde->dw_fde_hot_section_label = NULL;
2550   fde->dw_fde_hot_section_end_label = NULL;
2551   fde->dw_fde_unlikely_section_label = NULL;
2552   fde->dw_fde_unlikely_section_end_label = NULL;
2553   fde->dw_fde_switched_sections = false;
2554   fde->dw_fde_end = NULL;
2555   fde->dw_fde_cfi = NULL;
2556   fde->funcdef_number = current_function_funcdef_no;
2557   fde->nothrow = TREE_NOTHROW (current_function_decl);
2558   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2559   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2560
2561   args_size = old_args_size = 0;
2562
2563   /* We only want to output line number information for the genuine dwarf2
2564      prologue case, not the eh frame case.  */
2565 #ifdef DWARF2_DEBUGGING_INFO
2566   if (file)
2567     dwarf2out_source_line (line, file);
2568 #endif
2569 }
2570
2571 /* Output a marker (i.e. a label) for the absolute end of the generated code
2572    for a function definition.  This gets called *after* the epilogue code has
2573    been generated.  */
2574
2575 void
2576 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2577                         const char *file ATTRIBUTE_UNUSED)
2578 {
2579   dw_fde_ref fde;
2580   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2581
2582   /* Output a label to mark the endpoint of the code generated for this
2583      function.  */
2584   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2585                                current_function_funcdef_no);
2586   ASM_OUTPUT_LABEL (asm_out_file, label);
2587   fde = &fde_table[fde_table_in_use - 1];
2588   fde->dw_fde_end = xstrdup (label);
2589 }
2590
2591 void
2592 dwarf2out_frame_init (void)
2593 {
2594   /* Allocate the initial hunk of the fde_table.  */
2595   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2596   fde_table_allocated = FDE_TABLE_INCREMENT;
2597   fde_table_in_use = 0;
2598
2599   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2600      sake of lookup_cfa.  */
2601
2602   /* On entry, the Canonical Frame Address is at SP.  */
2603   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2604
2605 #ifdef DWARF2_UNWIND_INFO
2606   if (DWARF2_UNWIND_INFO)
2607     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2608 #endif
2609 }
2610
2611 void
2612 dwarf2out_frame_finish (void)
2613 {
2614   /* Output call frame information.  */
2615   if (DWARF2_FRAME_INFO)
2616     output_call_frame_info (0);
2617
2618 #ifndef TARGET_UNWIND_INFO
2619   /* Output another copy for the unwinder.  */
2620   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2621     output_call_frame_info (1);
2622 #endif
2623 }
2624 #endif
2625 \f
2626 /* And now, the subset of the debugging information support code necessary
2627    for emitting location expressions.  */
2628
2629 /* We need some way to distinguish DW_OP_addr with a direct symbol
2630    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2631 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2632
2633
2634 typedef struct dw_val_struct *dw_val_ref;
2635 typedef struct die_struct *dw_die_ref;
2636 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2637 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2638
2639 /* Each DIE may have a series of attribute/value pairs.  Values
2640    can take on several forms.  The forms that are used in this
2641    implementation are listed below.  */
2642
2643 enum dw_val_class
2644 {
2645   dw_val_class_addr,
2646   dw_val_class_offset,
2647   dw_val_class_loc,
2648   dw_val_class_loc_list,
2649   dw_val_class_range_list,
2650   dw_val_class_const,
2651   dw_val_class_unsigned_const,
2652   dw_val_class_long_long,
2653   dw_val_class_vec,
2654   dw_val_class_flag,
2655   dw_val_class_die_ref,
2656   dw_val_class_fde_ref,
2657   dw_val_class_lbl_id,
2658   dw_val_class_lbl_offset,
2659   dw_val_class_str
2660 };
2661
2662 /* Describe a double word constant value.  */
2663 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2664
2665 typedef struct dw_long_long_struct GTY(())
2666 {
2667   unsigned long hi;
2668   unsigned long low;
2669 }
2670 dw_long_long_const;
2671
2672 /* Describe a floating point constant value, or a vector constant value.  */
2673
2674 typedef struct dw_vec_struct GTY(())
2675 {
2676   unsigned char * GTY((length ("%h.length"))) array;
2677   unsigned length;
2678   unsigned elt_size;
2679 }
2680 dw_vec_const;
2681
2682 /* The dw_val_node describes an attribute's value, as it is
2683    represented internally.  */
2684
2685 typedef struct dw_val_struct GTY(())
2686 {
2687   enum dw_val_class val_class;
2688   union dw_val_struct_union
2689     {
2690       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2691       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2692       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2693       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2694       HOST_WIDE_INT GTY ((default)) val_int;
2695       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2696       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2697       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2698       struct dw_val_die_union
2699         {
2700           dw_die_ref die;
2701           int external;
2702         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2703       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2704       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2705       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2706       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2707     }
2708   GTY ((desc ("%1.val_class"))) v;
2709 }
2710 dw_val_node;
2711
2712 /* Locations in memory are described using a sequence of stack machine
2713    operations.  */
2714
2715 typedef struct dw_loc_descr_struct GTY(())
2716 {
2717   dw_loc_descr_ref dw_loc_next;
2718   enum dwarf_location_atom dw_loc_opc;
2719   dw_val_node dw_loc_oprnd1;
2720   dw_val_node dw_loc_oprnd2;
2721   int dw_loc_addr;
2722 }
2723 dw_loc_descr_node;
2724
2725 /* Location lists are ranges + location descriptions for that range,
2726    so you can track variables that are in different places over
2727    their entire life.  */
2728 typedef struct dw_loc_list_struct GTY(())
2729 {
2730   dw_loc_list_ref dw_loc_next;
2731   const char *begin; /* Label for begin address of range */
2732   const char *end;  /* Label for end address of range */
2733   char *ll_symbol; /* Label for beginning of location list.
2734                       Only on head of list */
2735   const char *section; /* Section this loclist is relative to */
2736   dw_loc_descr_ref expr;
2737 } dw_loc_list_node;
2738
2739 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2740
2741 static const char *dwarf_stack_op_name (unsigned);
2742 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2743                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2744 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2745 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2746 static unsigned long size_of_locs (dw_loc_descr_ref);
2747 static void output_loc_operands (dw_loc_descr_ref);
2748 static void output_loc_sequence (dw_loc_descr_ref);
2749
2750 /* Convert a DWARF stack opcode into its string name.  */
2751
2752 static const char *
2753 dwarf_stack_op_name (unsigned int op)
2754 {
2755   switch (op)
2756     {
2757     case DW_OP_addr:
2758     case INTERNAL_DW_OP_tls_addr:
2759       return "DW_OP_addr";
2760     case DW_OP_deref:
2761       return "DW_OP_deref";
2762     case DW_OP_const1u:
2763       return "DW_OP_const1u";
2764     case DW_OP_const1s:
2765       return "DW_OP_const1s";
2766     case DW_OP_const2u:
2767       return "DW_OP_const2u";
2768     case DW_OP_const2s:
2769       return "DW_OP_const2s";
2770     case DW_OP_const4u:
2771       return "DW_OP_const4u";
2772     case DW_OP_const4s:
2773       return "DW_OP_const4s";
2774     case DW_OP_const8u:
2775       return "DW_OP_const8u";
2776     case DW_OP_const8s:
2777       return "DW_OP_const8s";
2778     case DW_OP_constu:
2779       return "DW_OP_constu";
2780     case DW_OP_consts:
2781       return "DW_OP_consts";
2782     case DW_OP_dup:
2783       return "DW_OP_dup";
2784     case DW_OP_drop:
2785       return "DW_OP_drop";
2786     case DW_OP_over:
2787       return "DW_OP_over";
2788     case DW_OP_pick:
2789       return "DW_OP_pick";
2790     case DW_OP_swap:
2791       return "DW_OP_swap";
2792     case DW_OP_rot:
2793       return "DW_OP_rot";
2794     case DW_OP_xderef:
2795       return "DW_OP_xderef";
2796     case DW_OP_abs:
2797       return "DW_OP_abs";
2798     case DW_OP_and:
2799       return "DW_OP_and";
2800     case DW_OP_div:
2801       return "DW_OP_div";
2802     case DW_OP_minus:
2803       return "DW_OP_minus";
2804     case DW_OP_mod:
2805       return "DW_OP_mod";
2806     case DW_OP_mul:
2807       return "DW_OP_mul";
2808     case DW_OP_neg:
2809       return "DW_OP_neg";
2810     case DW_OP_not:
2811       return "DW_OP_not";
2812     case DW_OP_or:
2813       return "DW_OP_or";
2814     case DW_OP_plus:
2815       return "DW_OP_plus";
2816     case DW_OP_plus_uconst:
2817       return "DW_OP_plus_uconst";
2818     case DW_OP_shl:
2819       return "DW_OP_shl";
2820     case DW_OP_shr:
2821       return "DW_OP_shr";
2822     case DW_OP_shra:
2823       return "DW_OP_shra";
2824     case DW_OP_xor:
2825       return "DW_OP_xor";
2826     case DW_OP_bra:
2827       return "DW_OP_bra";
2828     case DW_OP_eq:
2829       return "DW_OP_eq";
2830     case DW_OP_ge:
2831       return "DW_OP_ge";
2832     case DW_OP_gt:
2833       return "DW_OP_gt";
2834     case DW_OP_le:
2835       return "DW_OP_le";
2836     case DW_OP_lt:
2837       return "DW_OP_lt";
2838     case DW_OP_ne:
2839       return "DW_OP_ne";
2840     case DW_OP_skip:
2841       return "DW_OP_skip";
2842     case DW_OP_lit0:
2843       return "DW_OP_lit0";
2844     case DW_OP_lit1:
2845       return "DW_OP_lit1";
2846     case DW_OP_lit2:
2847       return "DW_OP_lit2";
2848     case DW_OP_lit3:
2849       return "DW_OP_lit3";
2850     case DW_OP_lit4:
2851       return "DW_OP_lit4";
2852     case DW_OP_lit5:
2853       return "DW_OP_lit5";
2854     case DW_OP_lit6:
2855       return "DW_OP_lit6";
2856     case DW_OP_lit7:
2857       return "DW_OP_lit7";
2858     case DW_OP_lit8:
2859       return "DW_OP_lit8";
2860     case DW_OP_lit9:
2861       return "DW_OP_lit9";
2862     case DW_OP_lit10:
2863       return "DW_OP_lit10";
2864     case DW_OP_lit11:
2865       return "DW_OP_lit11";
2866     case DW_OP_lit12:
2867       return "DW_OP_lit12";
2868     case DW_OP_lit13:
2869       return "DW_OP_lit13";
2870     case DW_OP_lit14:
2871       return "DW_OP_lit14";
2872     case DW_OP_lit15:
2873       return "DW_OP_lit15";
2874     case DW_OP_lit16:
2875       return "DW_OP_lit16";
2876     case DW_OP_lit17:
2877       return "DW_OP_lit17";
2878     case DW_OP_lit18:
2879       return "DW_OP_lit18";
2880     case DW_OP_lit19:
2881       return "DW_OP_lit19";
2882     case DW_OP_lit20:
2883       return "DW_OP_lit20";
2884     case DW_OP_lit21:
2885       return "DW_OP_lit21";
2886     case DW_OP_lit22:
2887       return "DW_OP_lit22";
2888     case DW_OP_lit23:
2889       return "DW_OP_lit23";
2890     case DW_OP_lit24:
2891       return "DW_OP_lit24";
2892     case DW_OP_lit25:
2893       return "DW_OP_lit25";
2894     case DW_OP_lit26:
2895       return "DW_OP_lit26";
2896     case DW_OP_lit27:
2897       return "DW_OP_lit27";
2898     case DW_OP_lit28:
2899       return "DW_OP_lit28";
2900     case DW_OP_lit29:
2901       return "DW_OP_lit29";
2902     case DW_OP_lit30:
2903       return "DW_OP_lit30";
2904     case DW_OP_lit31:
2905       return "DW_OP_lit31";
2906     case DW_OP_reg0:
2907       return "DW_OP_reg0";
2908     case DW_OP_reg1:
2909       return "DW_OP_reg1";
2910     case DW_OP_reg2:
2911       return "DW_OP_reg2";
2912     case DW_OP_reg3:
2913       return "DW_OP_reg3";
2914     case DW_OP_reg4:
2915       return "DW_OP_reg4";
2916     case DW_OP_reg5:
2917       return "DW_OP_reg5";
2918     case DW_OP_reg6:
2919       return "DW_OP_reg6";
2920     case DW_OP_reg7:
2921       return "DW_OP_reg7";
2922     case DW_OP_reg8:
2923       return "DW_OP_reg8";
2924     case DW_OP_reg9:
2925       return "DW_OP_reg9";
2926     case DW_OP_reg10:
2927       return "DW_OP_reg10";
2928     case DW_OP_reg11:
2929       return "DW_OP_reg11";
2930     case DW_OP_reg12:
2931       return "DW_OP_reg12";
2932     case DW_OP_reg13:
2933       return "DW_OP_reg13";
2934     case DW_OP_reg14:
2935       return "DW_OP_reg14";
2936     case DW_OP_reg15:
2937       return "DW_OP_reg15";
2938     case DW_OP_reg16:
2939       return "DW_OP_reg16";
2940     case DW_OP_reg17:
2941       return "DW_OP_reg17";
2942     case DW_OP_reg18:
2943       return "DW_OP_reg18";
2944     case DW_OP_reg19:
2945       return "DW_OP_reg19";
2946     case DW_OP_reg20:
2947       return "DW_OP_reg20";
2948     case DW_OP_reg21:
2949       return "DW_OP_reg21";
2950     case DW_OP_reg22:
2951       return "DW_OP_reg22";
2952     case DW_OP_reg23:
2953       return "DW_OP_reg23";
2954     case DW_OP_reg24:
2955       return "DW_OP_reg24";
2956     case DW_OP_reg25:
2957       return "DW_OP_reg25";
2958     case DW_OP_reg26:
2959       return "DW_OP_reg26";
2960     case DW_OP_reg27:
2961       return "DW_OP_reg27";
2962     case DW_OP_reg28:
2963       return "DW_OP_reg28";
2964     case DW_OP_reg29:
2965       return "DW_OP_reg29";
2966     case DW_OP_reg30:
2967       return "DW_OP_reg30";
2968     case DW_OP_reg31:
2969       return "DW_OP_reg31";
2970     case DW_OP_breg0:
2971       return "DW_OP_breg0";
2972     case DW_OP_breg1:
2973       return "DW_OP_breg1";
2974     case DW_OP_breg2:
2975       return "DW_OP_breg2";
2976     case DW_OP_breg3:
2977       return "DW_OP_breg3";
2978     case DW_OP_breg4:
2979       return "DW_OP_breg4";
2980     case DW_OP_breg5:
2981       return "DW_OP_breg5";
2982     case DW_OP_breg6:
2983       return "DW_OP_breg6";
2984     case DW_OP_breg7:
2985       return "DW_OP_breg7";
2986     case DW_OP_breg8:
2987       return "DW_OP_breg8";
2988     case DW_OP_breg9:
2989       return "DW_OP_breg9";
2990     case DW_OP_breg10:
2991       return "DW_OP_breg10";
2992     case DW_OP_breg11:
2993       return "DW_OP_breg11";
2994     case DW_OP_breg12:
2995       return "DW_OP_breg12";
2996     case DW_OP_breg13:
2997       return "DW_OP_breg13";
2998     case DW_OP_breg14:
2999       return "DW_OP_breg14";
3000     case DW_OP_breg15:
3001       return "DW_OP_breg15";
3002     case DW_OP_breg16:
3003       return "DW_OP_breg16";
3004     case DW_OP_breg17:
3005       return "DW_OP_breg17";
3006     case DW_OP_breg18:
3007       return "DW_OP_breg18";
3008     case DW_OP_breg19:
3009       return "DW_OP_breg19";
3010     case DW_OP_breg20:
3011       return "DW_OP_breg20";
3012     case DW_OP_breg21:
3013       return "DW_OP_breg21";
3014     case DW_OP_breg22:
3015       return "DW_OP_breg22";
3016     case DW_OP_breg23:
3017       return "DW_OP_breg23";
3018     case DW_OP_breg24:
3019       return "DW_OP_breg24";
3020     case DW_OP_breg25:
3021       return "DW_OP_breg25";
3022     case DW_OP_breg26:
3023       return "DW_OP_breg26";
3024     case DW_OP_breg27:
3025       return "DW_OP_breg27";
3026     case DW_OP_breg28:
3027       return "DW_OP_breg28";
3028     case DW_OP_breg29:
3029       return "DW_OP_breg29";
3030     case DW_OP_breg30:
3031       return "DW_OP_breg30";
3032     case DW_OP_breg31:
3033       return "DW_OP_breg31";
3034     case DW_OP_regx:
3035       return "DW_OP_regx";
3036     case DW_OP_fbreg:
3037       return "DW_OP_fbreg";
3038     case DW_OP_bregx:
3039       return "DW_OP_bregx";
3040     case DW_OP_piece:
3041       return "DW_OP_piece";
3042     case DW_OP_deref_size:
3043       return "DW_OP_deref_size";
3044     case DW_OP_xderef_size:
3045       return "DW_OP_xderef_size";
3046     case DW_OP_nop:
3047       return "DW_OP_nop";
3048     case DW_OP_push_object_address:
3049       return "DW_OP_push_object_address";
3050     case DW_OP_call2:
3051       return "DW_OP_call2";
3052     case DW_OP_call4:
3053       return "DW_OP_call4";
3054     case DW_OP_call_ref:
3055       return "DW_OP_call_ref";
3056     case DW_OP_GNU_push_tls_address:
3057       return "DW_OP_GNU_push_tls_address";
3058     default:
3059       return "OP_<unknown>";
3060     }
3061 }
3062
3063 /* Return a pointer to a newly allocated location description.  Location
3064    descriptions are simple expression terms that can be strung
3065    together to form more complicated location (address) descriptions.  */
3066
3067 static inline dw_loc_descr_ref
3068 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3069                unsigned HOST_WIDE_INT oprnd2)
3070 {
3071   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3072
3073   descr->dw_loc_opc = op;
3074   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3075   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3076   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3077   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3078
3079   return descr;
3080 }
3081
3082 /* Add a location description term to a location description expression.  */
3083
3084 static inline void
3085 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3086 {
3087   dw_loc_descr_ref *d;
3088
3089   /* Find the end of the chain.  */
3090   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3091     ;
3092
3093   *d = descr;
3094 }
3095
3096 /* Return the size of a location descriptor.  */
3097
3098 static unsigned long
3099 size_of_loc_descr (dw_loc_descr_ref loc)
3100 {
3101   unsigned long size = 1;
3102
3103   switch (loc->dw_loc_opc)
3104     {
3105     case DW_OP_addr:
3106     case INTERNAL_DW_OP_tls_addr:
3107       size += DWARF2_ADDR_SIZE;
3108       break;
3109     case DW_OP_const1u:
3110     case DW_OP_const1s:
3111       size += 1;
3112       break;
3113     case DW_OP_const2u:
3114     case DW_OP_const2s:
3115       size += 2;
3116       break;
3117     case DW_OP_const4u:
3118     case DW_OP_const4s:
3119       size += 4;
3120       break;
3121     case DW_OP_const8u:
3122     case DW_OP_const8s:
3123       size += 8;
3124       break;
3125     case DW_OP_constu:
3126       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3127       break;
3128     case DW_OP_consts:
3129       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3130       break;
3131     case DW_OP_pick:
3132       size += 1;
3133       break;
3134     case DW_OP_plus_uconst:
3135       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3136       break;
3137     case DW_OP_skip:
3138     case DW_OP_bra:
3139       size += 2;
3140       break;
3141     case DW_OP_breg0:
3142     case DW_OP_breg1:
3143     case DW_OP_breg2:
3144     case DW_OP_breg3:
3145     case DW_OP_breg4:
3146     case DW_OP_breg5:
3147     case DW_OP_breg6:
3148     case DW_OP_breg7:
3149     case DW_OP_breg8:
3150     case DW_OP_breg9:
3151     case DW_OP_breg10:
3152     case DW_OP_breg11:
3153     case DW_OP_breg12:
3154     case DW_OP_breg13:
3155     case DW_OP_breg14:
3156     case DW_OP_breg15:
3157     case DW_OP_breg16:
3158     case DW_OP_breg17:
3159     case DW_OP_breg18:
3160     case DW_OP_breg19:
3161     case DW_OP_breg20:
3162     case DW_OP_breg21:
3163     case DW_OP_breg22:
3164     case DW_OP_breg23:
3165     case DW_OP_breg24:
3166     case DW_OP_breg25:
3167     case DW_OP_breg26:
3168     case DW_OP_breg27:
3169     case DW_OP_breg28:
3170     case DW_OP_breg29:
3171     case DW_OP_breg30:
3172     case DW_OP_breg31:
3173       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3174       break;
3175     case DW_OP_regx:
3176       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3177       break;
3178     case DW_OP_fbreg:
3179       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3180       break;
3181     case DW_OP_bregx:
3182       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3183       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3184       break;
3185     case DW_OP_piece:
3186       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3187       break;
3188     case DW_OP_deref_size:
3189     case DW_OP_xderef_size:
3190       size += 1;
3191       break;
3192     case DW_OP_call2:
3193       size += 2;
3194       break;
3195     case DW_OP_call4:
3196       size += 4;
3197       break;
3198     case DW_OP_call_ref:
3199       size += DWARF2_ADDR_SIZE;
3200       break;
3201     default:
3202       break;
3203     }
3204
3205   return size;
3206 }
3207
3208 /* Return the size of a series of location descriptors.  */
3209
3210 static unsigned long
3211 size_of_locs (dw_loc_descr_ref loc)
3212 {
3213   unsigned long size;
3214
3215   for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3216     {
3217       loc->dw_loc_addr = size;
3218       size += size_of_loc_descr (loc);
3219     }
3220
3221   return size;
3222 }
3223
3224 /* Output location description stack opcode's operands (if any).  */
3225
3226 static void
3227 output_loc_operands (dw_loc_descr_ref loc)
3228 {
3229   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3230   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3231
3232   switch (loc->dw_loc_opc)
3233     {
3234 #ifdef DWARF2_DEBUGGING_INFO
3235     case DW_OP_addr:
3236       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3237       break;
3238     case DW_OP_const2u:
3239     case DW_OP_const2s:
3240       dw2_asm_output_data (2, val1->v.val_int, NULL);
3241       break;
3242     case DW_OP_const4u:
3243     case DW_OP_const4s:
3244       dw2_asm_output_data (4, val1->v.val_int, NULL);
3245       break;
3246     case DW_OP_const8u:
3247     case DW_OP_const8s:
3248       gcc_assert (HOST_BITS_PER_LONG >= 64);
3249       dw2_asm_output_data (8, val1->v.val_int, NULL);
3250       break;
3251     case DW_OP_skip:
3252     case DW_OP_bra:
3253       {
3254         int offset;
3255
3256         gcc_assert (val1->val_class == dw_val_class_loc);
3257         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3258
3259         dw2_asm_output_data (2, offset, NULL);
3260       }
3261       break;
3262 #else
3263     case DW_OP_addr:
3264     case DW_OP_const2u:
3265     case DW_OP_const2s:
3266     case DW_OP_const4u:
3267     case DW_OP_const4s:
3268     case DW_OP_const8u:
3269     case DW_OP_const8s:
3270     case DW_OP_skip:
3271     case DW_OP_bra:
3272       /* We currently don't make any attempt to make sure these are
3273          aligned properly like we do for the main unwind info, so
3274          don't support emitting things larger than a byte if we're
3275          only doing unwinding.  */
3276       gcc_unreachable ();
3277 #endif
3278     case DW_OP_const1u:
3279     case DW_OP_const1s:
3280       dw2_asm_output_data (1, val1->v.val_int, NULL);
3281       break;
3282     case DW_OP_constu:
3283       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3284       break;
3285     case DW_OP_consts:
3286       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3287       break;
3288     case DW_OP_pick:
3289       dw2_asm_output_data (1, val1->v.val_int, NULL);
3290       break;
3291     case DW_OP_plus_uconst:
3292       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3293       break;
3294     case DW_OP_breg0:
3295     case DW_OP_breg1:
3296     case DW_OP_breg2:
3297     case DW_OP_breg3:
3298     case DW_OP_breg4:
3299     case DW_OP_breg5:
3300     case DW_OP_breg6:
3301     case DW_OP_breg7:
3302     case DW_OP_breg8:
3303     case DW_OP_breg9:
3304     case DW_OP_breg10:
3305     case DW_OP_breg11:
3306     case DW_OP_breg12:
3307     case DW_OP_breg13:
3308     case DW_OP_breg14:
3309     case DW_OP_breg15:
3310     case DW_OP_breg16:
3311     case DW_OP_breg17:
3312     case DW_OP_breg18:
3313     case DW_OP_breg19:
3314     case DW_OP_breg20:
3315     case DW_OP_breg21:
3316     case DW_OP_breg22:
3317     case DW_OP_breg23:
3318     case DW_OP_breg24:
3319     case DW_OP_breg25:
3320     case DW_OP_breg26:
3321     case DW_OP_breg27:
3322     case DW_OP_breg28:
3323     case DW_OP_breg29:
3324     case DW_OP_breg30:
3325     case DW_OP_breg31:
3326       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3327       break;
3328     case DW_OP_regx:
3329       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3330       break;
3331     case DW_OP_fbreg:
3332       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3333       break;
3334     case DW_OP_bregx:
3335       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3336       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3337       break;
3338     case DW_OP_piece:
3339       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3340       break;
3341     case DW_OP_deref_size:
3342     case DW_OP_xderef_size:
3343       dw2_asm_output_data (1, val1->v.val_int, NULL);
3344       break;
3345
3346     case INTERNAL_DW_OP_tls_addr:
3347       if (targetm.asm_out.output_dwarf_dtprel)
3348         {
3349           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3350                                                DWARF2_ADDR_SIZE,
3351                                                val1->v.val_addr);
3352           fputc ('\n', asm_out_file);
3353         }
3354       else
3355         gcc_unreachable ();
3356       break;
3357
3358     default:
3359       /* Other codes have no operands.  */
3360       break;
3361     }
3362 }
3363
3364 /* Output a sequence of location operations.  */
3365
3366 static void
3367 output_loc_sequence (dw_loc_descr_ref loc)
3368 {
3369   for (; loc != NULL; loc = loc->dw_loc_next)
3370     {
3371       /* Output the opcode.  */
3372       dw2_asm_output_data (1, loc->dw_loc_opc,
3373                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3374
3375       /* Output the operand(s) (if any).  */
3376       output_loc_operands (loc);
3377     }
3378 }
3379
3380 /* This routine will generate the correct assembly data for a location
3381    description based on a cfi entry with a complex address.  */
3382
3383 static void
3384 output_cfa_loc (dw_cfi_ref cfi)
3385 {
3386   dw_loc_descr_ref loc;
3387   unsigned long size;
3388
3389   /* Output the size of the block.  */
3390   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3391   size = size_of_locs (loc);
3392   dw2_asm_output_data_uleb128 (size, NULL);
3393
3394   /* Now output the operations themselves.  */
3395   output_loc_sequence (loc);
3396 }
3397
3398 /* This function builds a dwarf location descriptor sequence from a
3399    dw_cfa_location, adding the given OFFSET to the result of the
3400    expression.  */
3401
3402 static struct dw_loc_descr_struct *
3403 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3404 {
3405   struct dw_loc_descr_struct *head, *tmp;
3406
3407   offset += cfa->offset;
3408
3409   if (cfa->indirect)
3410     {
3411       if (cfa->base_offset)
3412         {
3413           if (cfa->reg <= 31)
3414             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3415           else
3416             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3417         }
3418       else if (cfa->reg <= 31)
3419         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3420       else
3421         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3422
3423       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3424       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3425       add_loc_descr (&head, tmp);
3426       if (offset != 0)
3427         {
3428           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3429           add_loc_descr (&head, tmp);
3430         }
3431     }
3432   else
3433     {
3434       if (offset == 0)
3435         if (cfa->reg <= 31)
3436           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3437         else
3438           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3439       else if (cfa->reg <= 31)
3440         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3441       else
3442         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3443     }
3444
3445   return head;
3446 }
3447
3448 /* This function fills in aa dw_cfa_location structure from a dwarf location
3449    descriptor sequence.  */
3450
3451 static void
3452 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3453 {
3454   struct dw_loc_descr_struct *ptr;
3455   cfa->offset = 0;
3456   cfa->base_offset = 0;
3457   cfa->indirect = 0;
3458   cfa->reg = -1;
3459
3460   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3461     {
3462       enum dwarf_location_atom op = ptr->dw_loc_opc;
3463
3464       switch (op)
3465         {
3466         case DW_OP_reg0:
3467         case DW_OP_reg1:
3468         case DW_OP_reg2:
3469         case DW_OP_reg3:
3470         case DW_OP_reg4:
3471         case DW_OP_reg5:
3472         case DW_OP_reg6:
3473         case DW_OP_reg7:
3474         case DW_OP_reg8:
3475         case DW_OP_reg9:
3476         case DW_OP_reg10:
3477         case DW_OP_reg11:
3478         case DW_OP_reg12:
3479         case DW_OP_reg13:
3480         case DW_OP_reg14:
3481         case DW_OP_reg15:
3482         case DW_OP_reg16:
3483         case DW_OP_reg17:
3484         case DW_OP_reg18:
3485         case DW_OP_reg19:
3486         case DW_OP_reg20:
3487         case DW_OP_reg21:
3488         case DW_OP_reg22:
3489         case DW_OP_reg23:
3490         case DW_OP_reg24:
3491         case DW_OP_reg25:
3492         case DW_OP_reg26:
3493         case DW_OP_reg27:
3494         case DW_OP_reg28:
3495         case DW_OP_reg29:
3496         case DW_OP_reg30:
3497         case DW_OP_reg31:
3498           cfa->reg = op - DW_OP_reg0;
3499           break;
3500         case DW_OP_regx:
3501           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3502           break;
3503         case DW_OP_breg0:
3504         case DW_OP_breg1:
3505         case DW_OP_breg2:
3506         case DW_OP_breg3:
3507         case DW_OP_breg4:
3508         case DW_OP_breg5:
3509         case DW_OP_breg6:
3510         case DW_OP_breg7:
3511         case DW_OP_breg8:
3512         case DW_OP_breg9:
3513         case DW_OP_breg10:
3514         case DW_OP_breg11:
3515         case DW_OP_breg12:
3516         case DW_OP_breg13:
3517         case DW_OP_breg14:
3518         case DW_OP_breg15:
3519         case DW_OP_breg16:
3520         case DW_OP_breg17:
3521         case DW_OP_breg18:
3522         case DW_OP_breg19:
3523         case DW_OP_breg20:
3524         case DW_OP_breg21:
3525         case DW_OP_breg22:
3526         case DW_OP_breg23:
3527         case DW_OP_breg24:
3528         case DW_OP_breg25:
3529         case DW_OP_breg26:
3530         case DW_OP_breg27:
3531         case DW_OP_breg28:
3532         case DW_OP_breg29:
3533         case DW_OP_breg30:
3534         case DW_OP_breg31:
3535           cfa->reg = op - DW_OP_breg0;
3536           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3537           break;
3538         case DW_OP_bregx:
3539           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3540           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3541           break;
3542         case DW_OP_deref:
3543           cfa->indirect = 1;
3544           break;
3545         case DW_OP_plus_uconst:
3546           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3547           break;
3548         default:
3549           internal_error ("DW_LOC_OP %s not implemented",
3550                           dwarf_stack_op_name (ptr->dw_loc_opc));
3551         }
3552     }
3553 }
3554 #endif /* .debug_frame support */
3555 \f
3556 /* And now, the support for symbolic debugging information.  */
3557 #ifdef DWARF2_DEBUGGING_INFO
3558
3559 /* .debug_str support.  */
3560 static int output_indirect_string (void **, void *);
3561
3562 static void dwarf2out_init (const char *);
3563 static void dwarf2out_finish (const char *);
3564 static void dwarf2out_define (unsigned int, const char *);
3565 static void dwarf2out_undef (unsigned int, const char *);
3566 static void dwarf2out_start_source_file (unsigned, const char *);
3567 static void dwarf2out_end_source_file (unsigned);
3568 static void dwarf2out_begin_block (unsigned, unsigned);
3569 static void dwarf2out_end_block (unsigned, unsigned);
3570 static bool dwarf2out_ignore_block (tree);
3571 static void dwarf2out_global_decl (tree);
3572 static void dwarf2out_type_decl (tree, int);
3573 static void dwarf2out_imported_module_or_decl (tree, tree);
3574 static void dwarf2out_abstract_function (tree);
3575 static void dwarf2out_var_location (rtx);
3576 static void dwarf2out_begin_function (tree);
3577 static void dwarf2out_switch_text_section (void);
3578
3579 /* The debug hooks structure.  */
3580
3581 const struct gcc_debug_hooks dwarf2_debug_hooks =
3582 {
3583   dwarf2out_init,
3584   dwarf2out_finish,
3585   dwarf2out_define,
3586   dwarf2out_undef,
3587   dwarf2out_start_source_file,
3588   dwarf2out_end_source_file,
3589   dwarf2out_begin_block,
3590   dwarf2out_end_block,
3591   dwarf2out_ignore_block,
3592   dwarf2out_source_line,
3593   dwarf2out_begin_prologue,
3594   debug_nothing_int_charstar,   /* end_prologue */
3595   dwarf2out_end_epilogue,
3596   dwarf2out_begin_function,
3597   debug_nothing_int,            /* end_function */
3598   dwarf2out_decl,               /* function_decl */
3599   dwarf2out_global_decl,
3600   dwarf2out_type_decl,          /* type_decl */
3601   dwarf2out_imported_module_or_decl,
3602   debug_nothing_tree,           /* deferred_inline_function */
3603   /* The DWARF 2 backend tries to reduce debugging bloat by not
3604      emitting the abstract description of inline functions until
3605      something tries to reference them.  */
3606   dwarf2out_abstract_function,  /* outlining_inline_function */
3607   debug_nothing_rtx,            /* label */
3608   debug_nothing_int,            /* handle_pch */
3609   dwarf2out_var_location,
3610   dwarf2out_switch_text_section,
3611   1                             /* start_end_main_source_file */
3612 };
3613 #endif
3614 \f
3615 /* NOTE: In the comments in this file, many references are made to
3616    "Debugging Information Entries".  This term is abbreviated as `DIE'
3617    throughout the remainder of this file.  */
3618
3619 /* An internal representation of the DWARF output is built, and then
3620    walked to generate the DWARF debugging info.  The walk of the internal
3621    representation is done after the entire program has been compiled.
3622    The types below are used to describe the internal representation.  */
3623
3624 /* Various DIE's use offsets relative to the beginning of the
3625    .debug_info section to refer to each other.  */
3626
3627 typedef long int dw_offset;
3628
3629 /* Define typedefs here to avoid circular dependencies.  */
3630
3631 typedef struct dw_attr_struct *dw_attr_ref;
3632 typedef struct dw_line_info_struct *dw_line_info_ref;
3633 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3634 typedef struct pubname_struct *pubname_ref;
3635 typedef struct dw_ranges_struct *dw_ranges_ref;
3636
3637 /* Each entry in the line_info_table maintains the file and
3638    line number associated with the label generated for that
3639    entry.  The label gives the PC value associated with
3640    the line number entry.  */
3641
3642 typedef struct dw_line_info_struct GTY(())
3643 {
3644   unsigned long dw_file_num;
3645   unsigned long dw_line_num;
3646 }
3647 dw_line_info_entry;
3648
3649 /* Line information for functions in separate sections; each one gets its
3650    own sequence.  */
3651 typedef struct dw_separate_line_info_struct GTY(())
3652 {
3653   unsigned long dw_file_num;
3654   unsigned long dw_line_num;
3655   unsigned long function;
3656 }
3657 dw_separate_line_info_entry;
3658
3659 /* Each DIE attribute has a field specifying the attribute kind,
3660    a link to the next attribute in the chain, and an attribute value.
3661    Attributes are typically linked below the DIE they modify.  */
3662
3663 typedef struct dw_attr_struct GTY(())
3664 {
3665   enum dwarf_attribute dw_attr;
3666   dw_attr_ref dw_attr_next;
3667   dw_val_node dw_attr_val;
3668 }
3669 dw_attr_node;
3670
3671 /* The Debugging Information Entry (DIE) structure */
3672
3673 typedef struct die_struct GTY(())
3674 {
3675   enum dwarf_tag die_tag;
3676   char *die_symbol;
3677   dw_attr_ref die_attr;
3678   dw_die_ref die_parent;
3679   dw_die_ref die_child;
3680   dw_die_ref die_sib;
3681   dw_die_ref die_definition; /* ref from a specification to its definition */
3682   dw_offset die_offset;
3683   unsigned long die_abbrev;
3684   int die_mark;
3685   unsigned int decl_id;
3686 }
3687 die_node;
3688
3689 /* The pubname structure */
3690
3691 typedef struct pubname_struct GTY(())
3692 {
3693   dw_die_ref die;
3694   char *name;
3695 }
3696 pubname_entry;
3697
3698 struct dw_ranges_struct GTY(())
3699 {
3700   int block_num;
3701 };
3702
3703 /* The limbo die list structure.  */
3704 typedef struct limbo_die_struct GTY(())
3705 {
3706   dw_die_ref die;
3707   tree created_for;
3708   struct limbo_die_struct *next;
3709 }
3710 limbo_die_node;
3711
3712 /* How to start an assembler comment.  */
3713 #ifndef ASM_COMMENT_START
3714 #define ASM_COMMENT_START ";#"
3715 #endif
3716
3717 /* Define a macro which returns nonzero for a TYPE_DECL which was
3718    implicitly generated for a tagged type.
3719
3720    Note that unlike the gcc front end (which generates a NULL named
3721    TYPE_DECL node for each complete tagged type, each array type, and
3722    each function type node created) the g++ front end generates a
3723    _named_ TYPE_DECL node for each tagged type node created.
3724    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3725    generate a DW_TAG_typedef DIE for them.  */
3726
3727 #define TYPE_DECL_IS_STUB(decl)                         \
3728   (DECL_NAME (decl) == NULL_TREE                        \
3729    || (DECL_ARTIFICIAL (decl)                           \
3730        && is_tagged_type (TREE_TYPE (decl))             \
3731        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3732            /* This is necessary for stub decls that     \
3733               appear in nested inline functions.  */    \
3734            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3735                && (decl_ultimate_origin (decl)          \
3736                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3737
3738 /* Information concerning the compilation unit's programming
3739    language, and compiler version.  */
3740
3741 /* Fixed size portion of the DWARF compilation unit header.  */
3742 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3743   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3744
3745 /* Fixed size portion of public names info.  */
3746 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3747
3748 /* Fixed size portion of the address range info.  */
3749 #define DWARF_ARANGES_HEADER_SIZE                                       \
3750   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3751                 DWARF2_ADDR_SIZE * 2)                                   \
3752    - DWARF_INITIAL_LENGTH_SIZE)
3753
3754 /* Size of padding portion in the address range info.  It must be
3755    aligned to twice the pointer size.  */
3756 #define DWARF_ARANGES_PAD_SIZE \
3757   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3758                 DWARF2_ADDR_SIZE * 2) \
3759    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3760
3761 /* Use assembler line directives if available.  */
3762 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3763 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3764 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3765 #else
3766 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3767 #endif
3768 #endif
3769
3770 /* Minimum line offset in a special line info. opcode.
3771    This value was chosen to give a reasonable range of values.  */
3772 #define DWARF_LINE_BASE  -10
3773
3774 /* First special line opcode - leave room for the standard opcodes.  */
3775 #define DWARF_LINE_OPCODE_BASE  10
3776
3777 /* Range of line offsets in a special line info. opcode.  */
3778 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3779
3780 /* Flag that indicates the initial value of the is_stmt_start flag.
3781    In the present implementation, we do not mark any lines as
3782    the beginning of a source statement, because that information
3783    is not made available by the GCC front-end.  */
3784 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3785
3786 #ifdef DWARF2_DEBUGGING_INFO
3787 /* This location is used by calc_die_sizes() to keep track
3788    the offset of each DIE within the .debug_info section.  */
3789 static unsigned long next_die_offset;
3790 #endif
3791
3792 /* Record the root of the DIE's built for the current compilation unit.  */
3793 static GTY(()) dw_die_ref comp_unit_die;
3794
3795 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3796 static GTY(()) limbo_die_node *limbo_die_list;
3797
3798 /* Filenames referenced by this compilation unit.  */
3799 static GTY(()) varray_type file_table;
3800 static GTY(()) varray_type file_table_emitted;
3801 static GTY(()) size_t file_table_last_lookup_index;
3802
3803 /* A hash table of references to DIE's that describe declarations.
3804    The key is a DECL_UID() which is a unique number identifying each decl.  */
3805 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3806
3807 /* Node of the variable location list.  */
3808 struct var_loc_node GTY ((chain_next ("%h.next")))
3809 {
3810   rtx GTY (()) var_loc_note;
3811   const char * GTY (()) label;
3812   const char * GTY (()) section_label;
3813   struct var_loc_node * GTY (()) next;
3814 };
3815
3816 /* Variable location list.  */
3817 struct var_loc_list_def GTY (())
3818 {
3819   struct var_loc_node * GTY (()) first;
3820
3821   /* Do not mark the last element of the chained list because
3822      it is marked through the chain.  */
3823   struct var_loc_node * GTY ((skip ("%h"))) last;
3824
3825   /* DECL_UID of the variable decl.  */
3826   unsigned int decl_id;
3827 };
3828 typedef struct var_loc_list_def var_loc_list;
3829
3830
3831 /* Table of decl location linked lists.  */
3832 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3833
3834 /* A pointer to the base of a list of references to DIE's that
3835    are uniquely identified by their tag, presence/absence of
3836    children DIE's, and list of attribute/value pairs.  */
3837 static GTY((length ("abbrev_die_table_allocated")))
3838   dw_die_ref *abbrev_die_table;
3839
3840 /* Number of elements currently allocated for abbrev_die_table.  */
3841 static GTY(()) unsigned abbrev_die_table_allocated;
3842
3843 /* Number of elements in type_die_table currently in use.  */
3844 static GTY(()) unsigned abbrev_die_table_in_use;
3845
3846 /* Size (in elements) of increments by which we may expand the
3847    abbrev_die_table.  */
3848 #define ABBREV_DIE_TABLE_INCREMENT 256
3849
3850 /* A pointer to the base of a table that contains line information
3851    for each source code line in .text in the compilation unit.  */
3852 static GTY((length ("line_info_table_allocated")))
3853      dw_line_info_ref line_info_table;
3854
3855 /* Number of elements currently allocated for line_info_table.  */
3856 static GTY(()) unsigned line_info_table_allocated;
3857
3858 /* Number of elements in line_info_table currently in use.  */
3859 static GTY(()) unsigned line_info_table_in_use;
3860
3861 /* True if the compilation unit places functions in more than one section.  */
3862 static GTY(()) bool have_multiple_function_sections = false;
3863
3864 /* A pointer to the base of a table that contains line information
3865    for each source code line outside of .text in the compilation unit.  */
3866 static GTY ((length ("separate_line_info_table_allocated")))
3867      dw_separate_line_info_ref separate_line_info_table;
3868
3869 /* Number of elements currently allocated for separate_line_info_table.  */
3870 static GTY(()) unsigned separate_line_info_table_allocated;
3871
3872 /* Number of elements in separate_line_info_table currently in use.  */
3873 static GTY(()) unsigned separate_line_info_table_in_use;
3874
3875 /* Size (in elements) of increments by which we may expand the
3876    line_info_table.  */
3877 #define LINE_INFO_TABLE_INCREMENT 1024
3878
3879 /* A pointer to the base of a table that contains a list of publicly
3880    accessible names.  */
3881 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3882
3883 /* Number of elements currently allocated for pubname_table.  */
3884 static GTY(()) unsigned pubname_table_allocated;
3885
3886 /* Number of elements in pubname_table currently in use.  */
3887 static GTY(()) unsigned pubname_table_in_use;
3888
3889 /* Size (in elements) of increments by which we may expand the
3890    pubname_table.  */
3891 #define PUBNAME_TABLE_INCREMENT 64
3892
3893 /* Array of dies for which we should generate .debug_arange info.  */
3894 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3895
3896 /* Number of elements currently allocated for arange_table.  */
3897 static GTY(()) unsigned arange_table_allocated;
3898
3899 /* Number of elements in arange_table currently in use.  */
3900 static GTY(()) unsigned arange_table_in_use;
3901
3902 /* Size (in elements) of increments by which we may expand the
3903    arange_table.  */
3904 #define ARANGE_TABLE_INCREMENT 64
3905
3906 /* Array of dies for which we should generate .debug_ranges info.  */
3907 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3908
3909 /* Number of elements currently allocated for ranges_table.  */
3910 static GTY(()) unsigned ranges_table_allocated;
3911
3912 /* Number of elements in ranges_table currently in use.  */
3913 static GTY(()) unsigned ranges_table_in_use;
3914
3915 /* Size (in elements) of increments by which we may expand the
3916    ranges_table.  */
3917 #define RANGES_TABLE_INCREMENT 64
3918
3919 /* Whether we have location lists that need outputting */
3920 static GTY(()) bool have_location_lists;
3921
3922 /* Unique label counter.  */
3923 static GTY(()) unsigned int loclabel_num;
3924
3925 #ifdef DWARF2_DEBUGGING_INFO
3926 /* Record whether the function being analyzed contains inlined functions.  */
3927 static int current_function_has_inlines;
3928 #endif
3929 #if 0 && defined (MIPS_DEBUGGING_INFO)
3930 static int comp_unit_has_inlines;
3931 #endif
3932
3933 /* Number of file tables emitted in maybe_emit_file().  */
3934 static GTY(()) int emitcount = 0;
3935
3936 /* Number of internal labels generated by gen_internal_sym().  */
3937 static GTY(()) int label_num;
3938
3939 #ifdef DWARF2_DEBUGGING_INFO
3940
3941 /* Offset from the "steady-state frame pointer" to the frame base,
3942    within the current function.  */
3943 static HOST_WIDE_INT frame_pointer_fb_offset;
3944
3945 /* Forward declarations for functions defined in this file.  */
3946
3947 static int is_pseudo_reg (rtx);
3948 static tree type_main_variant (tree);
3949 static int is_tagged_type (tree);
3950 static const char *dwarf_tag_name (unsigned);
3951 static const char *dwarf_attr_name (unsigned);
3952 static const char *dwarf_form_name (unsigned);
3953 static tree decl_ultimate_origin (tree);
3954 static tree block_ultimate_origin (tree);
3955 static tree decl_class_context (tree);
3956 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3957 static inline enum dw_val_class AT_class (dw_attr_ref);
3958 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3959 static inline unsigned AT_flag (dw_attr_ref);
3960 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3961 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3962 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3963 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3964 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3965                               unsigned long);
3966 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3967                                unsigned int, unsigned char *);
3968 static hashval_t debug_str_do_hash (const void *);
3969 static int debug_str_eq (const void *, const void *);
3970 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3971 static inline const char *AT_string (dw_attr_ref);
3972 static int AT_string_form (dw_attr_ref);
3973 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3974 static void add_AT_specification (dw_die_ref, dw_die_ref);
3975 static inline dw_die_ref AT_ref (dw_attr_ref);
3976 static inline int AT_ref_external (dw_attr_ref);
3977 static inline void set_AT_ref_external (dw_attr_ref, int);
3978 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3979 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3980 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3981 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3982                              dw_loc_list_ref);
3983 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3984 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3985 static inline rtx AT_addr (dw_attr_ref);
3986 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3987 static void add_AT_lbl_offset (dw_die_ref, enum dwarf_attribute, const char *);
3988 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3989                            unsigned HOST_WIDE_INT);
3990 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3991                                unsigned long);
3992 static inline const char *AT_lbl (dw_attr_ref);
3993 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3994 static const char *get_AT_low_pc (dw_die_ref);
3995 static const char *get_AT_hi_pc (dw_die_ref);
3996 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3997 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3998 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3999 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4000 static bool is_c_family (void);
4001 static bool is_cxx (void);
4002 static bool is_java (void);
4003 static bool is_fortran (void);
4004 static bool is_ada (void);
4005 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4006 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4007 static inline void free_die (dw_die_ref);
4008 static void remove_children (dw_die_ref);
4009 static void add_child_die (dw_die_ref, dw_die_ref);
4010 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4011 static dw_die_ref lookup_type_die (tree);
4012 static void equate_type_number_to_die (tree, dw_die_ref);
4013 static hashval_t decl_die_table_hash (const void *);
4014 static int decl_die_table_eq (const void *, const void *);
4015 static dw_die_ref lookup_decl_die (tree);
4016 static hashval_t decl_loc_table_hash (const void *);
4017 static int decl_loc_table_eq (const void *, const void *);
4018 static var_loc_list *lookup_decl_loc (tree);
4019 static void equate_decl_number_to_die (tree, dw_die_ref);
4020 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4021 static void print_spaces (FILE *);
4022 static void print_die (dw_die_ref, FILE *);
4023 static void print_dwarf_line_table (FILE *);
4024 static void reverse_die_lists (dw_die_ref);
4025 static void reverse_all_dies (dw_die_ref);
4026 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4027 static dw_die_ref pop_compile_unit (dw_die_ref);
4028 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4029 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4030 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4031 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4032 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4033 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4034 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4035 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4036 static void compute_section_prefix (dw_die_ref);
4037 static int is_type_die (dw_die_ref);
4038 static int is_comdat_die (dw_die_ref);
4039 static int is_symbol_die (dw_die_ref);
4040 static void assign_symbol_names (dw_die_ref);
4041 static void break_out_includes (dw_die_ref);
4042 static hashval_t htab_cu_hash (const void *);
4043 static int htab_cu_eq (const void *, const void *);
4044 static void htab_cu_del (void *);
4045 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4046 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4047 static void add_sibling_attributes (dw_die_ref);
4048 static void build_abbrev_table (dw_die_ref);
4049 static void output_location_lists (dw_die_ref);
4050 static int constant_size (long unsigned);
4051 static unsigned long size_of_die (dw_die_ref);
4052 static void calc_die_sizes (dw_die_ref);
4053 static void mark_dies (dw_die_ref);
4054 static void unmark_dies (dw_die_ref);
4055 static void unmark_all_dies (dw_die_ref);
4056 static unsigned long size_of_pubnames (void);
4057 static unsigned long size_of_aranges (void);
4058 static enum dwarf_form value_format (dw_attr_ref);
4059 static void output_value_format (dw_attr_ref);
4060 static void output_abbrev_section (void);
4061 static void output_die_symbol (dw_die_ref);
4062 static void output_die (dw_die_ref);
4063 static void output_compilation_unit_header (void);
4064 static void output_comp_unit (dw_die_ref, int);
4065 static const char *dwarf2_name (tree, int);
4066 static void add_pubname (tree, dw_die_ref);
4067 static void output_pubnames (void);
4068 static void add_arange (tree, dw_die_ref);
4069 static void output_aranges (void);
4070 static unsigned int add_ranges (tree);
4071 static void output_ranges (void);
4072 static void output_line_info (void);
4073 static void output_file_names (void);
4074 static dw_die_ref base_type_die (tree);
4075 static tree root_type (tree);
4076 static int is_base_type (tree);
4077 static bool is_subrange_type (tree);
4078 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4079 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4080 static int type_is_enum (tree);
4081 static unsigned int dbx_reg_number (rtx);
4082 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4083 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4084 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4085 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4086 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4087 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4088 static int is_based_loc (rtx);
4089 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4090 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4091 static dw_loc_descr_ref loc_descriptor (rtx);
4092 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4093 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4094 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4095 static tree field_type (tree);
4096 static unsigned int simple_type_align_in_bits (tree);
4097 static unsigned int simple_decl_align_in_bits (tree);
4098 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4099 static HOST_WIDE_INT field_byte_offset (tree);
4100 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4101                                          dw_loc_descr_ref);
4102 static void add_data_member_location_attribute (dw_die_ref, tree);
4103 static void add_const_value_attribute (dw_die_ref, rtx);
4104 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4105 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4106 static void insert_float (rtx, unsigned char *);
4107 static rtx rtl_for_decl_location (tree);
4108 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4109                                                    enum dwarf_attribute);
4110 static void tree_add_const_value_attribute (dw_die_ref, tree);
4111 static void add_name_attribute (dw_die_ref, const char *);
4112 static void add_comp_dir_attribute (dw_die_ref);
4113 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4114 static void add_subscript_info (dw_die_ref, tree);
4115 static void add_byte_size_attribute (dw_die_ref, tree);
4116 static void add_bit_offset_attribute (dw_die_ref, tree);
4117 static void add_bit_size_attribute (dw_die_ref, tree);
4118 static void add_prototyped_attribute (dw_die_ref, tree);
4119 static void add_abstract_origin_attribute (dw_die_ref, tree);
4120 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4121 static void add_src_coords_attributes (dw_die_ref, tree);
4122 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4123 static void push_decl_scope (tree);
4124 static void pop_decl_scope (void);
4125 static dw_die_ref scope_die_for (tree, dw_die_ref);
4126 static inline int local_scope_p (dw_die_ref);
4127 static inline int class_or_namespace_scope_p (dw_die_ref);
4128 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4129 static void add_calling_convention_attribute (dw_die_ref, tree);
4130 static const char *type_tag (tree);
4131 static tree member_declared_type (tree);
4132 #if 0
4133 static const char *decl_start_label (tree);
4134 #endif
4135 static void gen_array_type_die (tree, dw_die_ref);
4136 #if 0
4137 static void gen_entry_point_die (tree, dw_die_ref);
4138 #endif
4139 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4140 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4141 static void gen_inlined_union_type_die (tree, dw_die_ref);
4142 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4143 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4144 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4145 static void gen_formal_types_die (tree, dw_die_ref);
4146 static void gen_subprogram_die (tree, dw_die_ref);
4147 static void gen_variable_die (tree, dw_die_ref);
4148 static void gen_label_die (tree, dw_die_ref);
4149 static void gen_lexical_block_die (tree, dw_die_ref, int);
4150 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4151 static void gen_field_die (tree, dw_die_ref);
4152 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4153 static dw_die_ref gen_compile_unit_die (const char *);
4154 static void gen_inheritance_die (tree, tree, dw_die_ref);
4155 static void gen_member_die (tree, dw_die_ref);
4156 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4157 static void gen_subroutine_type_die (tree, dw_die_ref);
4158 static void gen_typedef_die (tree, dw_die_ref);
4159 static void gen_type_die (tree, dw_die_ref);
4160 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4161 static void gen_block_die (tree, dw_die_ref, int);
4162 static void decls_for_scope (tree, dw_die_ref, int);
4163 static int is_redundant_typedef (tree);
4164 static void gen_namespace_die (tree);
4165 static void gen_decl_die (tree, dw_die_ref);
4166 static dw_die_ref force_decl_die (tree);
4167 static dw_die_ref force_type_die (tree);
4168 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4169 static void declare_in_namespace (tree, dw_die_ref);
4170 static unsigned lookup_filename (const char *);
4171 static void init_file_table (void);
4172 static void retry_incomplete_types (void);
4173 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4174 static void splice_child_die (dw_die_ref, dw_die_ref);
4175 static int file_info_cmp (const void *, const void *);
4176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4177                                      const char *, const char *, unsigned);
4178 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4179                                        const char *, const char *,
4180                                        const char *);
4181 static void output_loc_list (dw_loc_list_ref);
4182 static char *gen_internal_sym (const char *);
4183
4184 static void prune_unmark_dies (dw_die_ref);
4185 static void prune_unused_types_mark (dw_die_ref, int);
4186 static void prune_unused_types_walk (dw_die_ref);
4187 static void prune_unused_types_walk_attribs (dw_die_ref);
4188 static void prune_unused_types_prune (dw_die_ref);
4189 static void prune_unused_types (void);
4190 static int maybe_emit_file (int);
4191
4192 /* Section names used to hold DWARF debugging information.  */
4193 #ifndef DEBUG_INFO_SECTION
4194 #define DEBUG_INFO_SECTION      ".debug_info"
4195 #endif
4196 #ifndef DEBUG_ABBREV_SECTION
4197 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4198 #endif
4199 #ifndef DEBUG_ARANGES_SECTION
4200 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4201 #endif
4202 #ifndef DEBUG_MACINFO_SECTION
4203 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4204 #endif
4205 #ifndef DEBUG_LINE_SECTION
4206 #define DEBUG_LINE_SECTION      ".debug_line"
4207 #endif
4208 #ifndef DEBUG_LOC_SECTION
4209 #define DEBUG_LOC_SECTION       ".debug_loc"
4210 #endif
4211 #ifndef DEBUG_PUBNAMES_SECTION
4212 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4213 #endif
4214 #ifndef DEBUG_STR_SECTION
4215 #define DEBUG_STR_SECTION       ".debug_str"
4216 #endif
4217 #ifndef DEBUG_RANGES_SECTION
4218 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4219 #endif
4220
4221 /* Standard ELF section names for compiled code and data.  */
4222 #ifndef TEXT_SECTION_NAME
4223 #define TEXT_SECTION_NAME       ".text"
4224 #endif
4225
4226 /* Section flags for .debug_str section.  */
4227 #define DEBUG_STR_SECTION_FLAGS \
4228   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4229    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4230    : SECTION_DEBUG)
4231
4232 /* Labels we insert at beginning sections we can reference instead of
4233    the section names themselves.  */
4234
4235 #ifndef TEXT_SECTION_LABEL
4236 #define TEXT_SECTION_LABEL              "Ltext"
4237 #endif
4238 #ifndef COLD_TEXT_SECTION_LABEL
4239 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4240 #endif
4241 #ifndef DEBUG_LINE_SECTION_LABEL
4242 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4243 #endif
4244 #ifndef DEBUG_INFO_SECTION_LABEL
4245 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4246 #endif
4247 #ifndef DEBUG_ABBREV_SECTION_LABEL
4248 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4249 #endif
4250 #ifndef DEBUG_LOC_SECTION_LABEL
4251 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4252 #endif
4253 #ifndef DEBUG_RANGES_SECTION_LABEL
4254 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4255 #endif
4256 #ifndef DEBUG_MACINFO_SECTION_LABEL
4257 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4258 #endif
4259
4260 /* Definitions of defaults for formats and names of various special
4261    (artificial) labels which may be generated within this file (when the -g
4262    options is used and DWARF2_DEBUGGING_INFO is in effect.
4263    If necessary, these may be overridden from within the tm.h file, but
4264    typically, overriding these defaults is unnecessary.  */
4265
4266 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4268 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4269 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES]; 
4270 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4271 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4272 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4273 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4274 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4275 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4276
4277 #ifndef TEXT_END_LABEL
4278 #define TEXT_END_LABEL          "Letext"
4279 #endif
4280 #ifndef COLD_END_LABEL
4281 #define COLD_END_LABEL          "Letext_cold"
4282 #endif
4283 #ifndef BLOCK_BEGIN_LABEL
4284 #define BLOCK_BEGIN_LABEL       "LBB"
4285 #endif
4286 #ifndef BLOCK_END_LABEL
4287 #define BLOCK_END_LABEL         "LBE"
4288 #endif
4289 #ifndef LINE_CODE_LABEL
4290 #define LINE_CODE_LABEL         "LM"
4291 #endif
4292 #ifndef SEPARATE_LINE_CODE_LABEL
4293 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4294 #endif
4295 \f
4296 /* We allow a language front-end to designate a function that is to be
4297    called to "demangle" any name before it is put into a DIE.  */
4298
4299 static const char *(*demangle_name_func) (const char *);
4300
4301 void
4302 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4303 {
4304   demangle_name_func = func;
4305 }
4306
4307 /* Test if rtl node points to a pseudo register.  */
4308
4309 static inline int
4310 is_pseudo_reg (rtx rtl)
4311 {
4312   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4313           || (GET_CODE (rtl) == SUBREG
4314               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4315 }
4316
4317 /* Return a reference to a type, with its const and volatile qualifiers
4318    removed.  */
4319
4320 static inline tree
4321 type_main_variant (tree type)
4322 {
4323   type = TYPE_MAIN_VARIANT (type);
4324
4325   /* ??? There really should be only one main variant among any group of
4326      variants of a given type (and all of the MAIN_VARIANT values for all
4327      members of the group should point to that one type) but sometimes the C
4328      front-end messes this up for array types, so we work around that bug
4329      here.  */
4330   if (TREE_CODE (type) == ARRAY_TYPE)
4331     while (type != TYPE_MAIN_VARIANT (type))
4332       type = TYPE_MAIN_VARIANT (type);
4333
4334   return type;
4335 }
4336
4337 /* Return nonzero if the given type node represents a tagged type.  */
4338
4339 static inline int
4340 is_tagged_type (tree type)
4341 {
4342   enum tree_code code = TREE_CODE (type);
4343
4344   return (code == RECORD_TYPE || code == UNION_TYPE
4345           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4346 }
4347
4348 /* Convert a DIE tag into its string name.  */
4349
4350 static const char *
4351 dwarf_tag_name (unsigned int tag)
4352 {
4353   switch (tag)
4354     {
4355     case DW_TAG_padding:
4356       return "DW_TAG_padding";
4357     case DW_TAG_array_type:
4358       return "DW_TAG_array_type";
4359     case DW_TAG_class_type:
4360       return "DW_TAG_class_type";
4361     case DW_TAG_entry_point:
4362       return "DW_TAG_entry_point";
4363     case DW_TAG_enumeration_type:
4364       return "DW_TAG_enumeration_type";
4365     case DW_TAG_formal_parameter:
4366       return "DW_TAG_formal_parameter";
4367     case DW_TAG_imported_declaration:
4368       return "DW_TAG_imported_declaration";
4369     case DW_TAG_label:
4370       return "DW_TAG_label";
4371     case DW_TAG_lexical_block:
4372       return "DW_TAG_lexical_block";
4373     case DW_TAG_member:
4374       return "DW_TAG_member";
4375     case DW_TAG_pointer_type:
4376       return "DW_TAG_pointer_type";
4377     case DW_TAG_reference_type:
4378       return "DW_TAG_reference_type";
4379     case DW_TAG_compile_unit:
4380       return "DW_TAG_compile_unit";
4381     case DW_TAG_string_type:
4382       return "DW_TAG_string_type";
4383     case DW_TAG_structure_type:
4384       return "DW_TAG_structure_type";
4385     case DW_TAG_subroutine_type:
4386       return "DW_TAG_subroutine_type";
4387     case DW_TAG_typedef:
4388       return "DW_TAG_typedef";
4389     case DW_TAG_union_type:
4390       return "DW_TAG_union_type";
4391     case DW_TAG_unspecified_parameters:
4392       return "DW_TAG_unspecified_parameters";
4393     case DW_TAG_variant:
4394       return "DW_TAG_variant";
4395     case DW_TAG_common_block:
4396       return "DW_TAG_common_block";
4397     case DW_TAG_common_inclusion:
4398       return "DW_TAG_common_inclusion";
4399     case DW_TAG_inheritance:
4400       return "DW_TAG_inheritance";
4401     case DW_TAG_inlined_subroutine:
4402       return "DW_TAG_inlined_subroutine";
4403     case DW_TAG_module:
4404       return "DW_TAG_module";
4405     case DW_TAG_ptr_to_member_type:
4406       return "DW_TAG_ptr_to_member_type";
4407     case DW_TAG_set_type:
4408       return "DW_TAG_set_type";
4409     case DW_TAG_subrange_type:
4410       return "DW_TAG_subrange_type";
4411     case DW_TAG_with_stmt:
4412       return "DW_TAG_with_stmt";
4413     case DW_TAG_access_declaration:
4414       return "DW_TAG_access_declaration";
4415     case DW_TAG_base_type:
4416       return "DW_TAG_base_type";
4417     case DW_TAG_catch_block:
4418       return "DW_TAG_catch_block";
4419     case DW_TAG_const_type:
4420       return "DW_TAG_const_type";
4421     case DW_TAG_constant:
4422       return "DW_TAG_constant";
4423     case DW_TAG_enumerator:
4424       return "DW_TAG_enumerator";
4425     case DW_TAG_file_type:
4426       return "DW_TAG_file_type";
4427     case DW_TAG_friend:
4428       return "DW_TAG_friend";
4429     case DW_TAG_namelist:
4430       return "DW_TAG_namelist";
4431     case DW_TAG_namelist_item:
4432       return "DW_TAG_namelist_item";
4433     case DW_TAG_namespace:
4434       return "DW_TAG_namespace";
4435     case DW_TAG_packed_type:
4436       return "DW_TAG_packed_type";
4437     case DW_TAG_subprogram:
4438       return "DW_TAG_subprogram";
4439     case DW_TAG_template_type_param:
4440       return "DW_TAG_template_type_param";
4441     case DW_TAG_template_value_param:
4442       return "DW_TAG_template_value_param";
4443     case DW_TAG_thrown_type:
4444       return "DW_TAG_thrown_type";
4445     case DW_TAG_try_block:
4446       return "DW_TAG_try_block";
4447     case DW_TAG_variant_part:
4448       return "DW_TAG_variant_part";
4449     case DW_TAG_variable:
4450       return "DW_TAG_variable";
4451     case DW_TAG_volatile_type:
4452       return "DW_TAG_volatile_type";
4453     case DW_TAG_imported_module:
4454       return "DW_TAG_imported_module";
4455     case DW_TAG_MIPS_loop:
4456       return "DW_TAG_MIPS_loop";
4457     case DW_TAG_format_label:
4458       return "DW_TAG_format_label";
4459     case DW_TAG_function_template:
4460       return "DW_TAG_function_template";
4461     case DW_TAG_class_template:
4462       return "DW_TAG_class_template";
4463     case DW_TAG_GNU_BINCL:
4464       return "DW_TAG_GNU_BINCL";
4465     case DW_TAG_GNU_EINCL:
4466       return "DW_TAG_GNU_EINCL";
4467     default:
4468       return "DW_TAG_<unknown>";
4469     }
4470 }
4471
4472 /* Convert a DWARF attribute code into its string name.  */
4473
4474 static const char *
4475 dwarf_attr_name (unsigned int attr)
4476 {
4477   switch (attr)
4478     {
4479     case DW_AT_sibling:
4480       return "DW_AT_sibling";
4481     case DW_AT_location:
4482       return "DW_AT_location";
4483     case DW_AT_name:
4484       return "DW_AT_name";
4485     case DW_AT_ordering:
4486       return "DW_AT_ordering";
4487     case DW_AT_subscr_data:
4488       return "DW_AT_subscr_data";
4489     case DW_AT_byte_size:
4490       return "DW_AT_byte_size";
4491     case DW_AT_bit_offset:
4492       return "DW_AT_bit_offset";
4493     case DW_AT_bit_size:
4494       return "DW_AT_bit_size";
4495     case DW_AT_element_list:
4496       return "DW_AT_element_list";
4497     case DW_AT_stmt_list:
4498       return "DW_AT_stmt_list";
4499     case DW_AT_low_pc:
4500       return "DW_AT_low_pc";
4501     case DW_AT_high_pc:
4502       return "DW_AT_high_pc";
4503     case DW_AT_language:
4504       return "DW_AT_language";
4505     case DW_AT_member:
4506       return "DW_AT_member";
4507     case DW_AT_discr:
4508       return "DW_AT_discr";
4509     case DW_AT_discr_value:
4510       return "DW_AT_discr_value";
4511     case DW_AT_visibility:
4512       return "DW_AT_visibility";
4513     case DW_AT_import:
4514       return "DW_AT_import";
4515     case DW_AT_string_length:
4516       return "DW_AT_string_length";
4517     case DW_AT_common_reference:
4518       return "DW_AT_common_reference";
4519     case DW_AT_comp_dir:
4520       return "DW_AT_comp_dir";
4521     case DW_AT_const_value:
4522       return "DW_AT_const_value";
4523     case DW_AT_containing_type:
4524       return "DW_AT_containing_type";
4525     case DW_AT_default_value:
4526       return "DW_AT_default_value";
4527     case DW_AT_inline:
4528       return "DW_AT_inline";
4529     case DW_AT_is_optional:
4530       return "DW_AT_is_optional";
4531     case DW_AT_lower_bound:
4532       return "DW_AT_lower_bound";
4533     case DW_AT_producer:
4534       return "DW_AT_producer";
4535     case DW_AT_prototyped:
4536       return "DW_AT_prototyped";
4537     case DW_AT_return_addr:
4538       return "DW_AT_return_addr";
4539     case DW_AT_start_scope:
4540       return "DW_AT_start_scope";
4541     case DW_AT_stride_size:
4542       return "DW_AT_stride_size";
4543     case DW_AT_upper_bound:
4544       return "DW_AT_upper_bound";
4545     case DW_AT_abstract_origin:
4546       return "DW_AT_abstract_origin";
4547     case DW_AT_accessibility:
4548       return "DW_AT_accessibility";
4549     case DW_AT_address_class:
4550       return "DW_AT_address_class";
4551     case DW_AT_artificial:
4552       return "DW_AT_artificial";
4553     case DW_AT_base_types:
4554       return "DW_AT_base_types";
4555     case DW_AT_calling_convention:
4556       return "DW_AT_calling_convention";
4557     case DW_AT_count:
4558       return "DW_AT_count";
4559     case DW_AT_data_member_location:
4560       return "DW_AT_data_member_location";
4561     case DW_AT_decl_column:
4562       return "DW_AT_decl_column";
4563     case DW_AT_decl_file:
4564       return "DW_AT_decl_file";
4565     case DW_AT_decl_line:
4566       return "DW_AT_decl_line";
4567     case DW_AT_declaration:
4568       return "DW_AT_declaration";
4569     case DW_AT_discr_list:
4570       return "DW_AT_discr_list";
4571     case DW_AT_encoding:
4572       return "DW_AT_encoding";
4573     case DW_AT_external:
4574       return "DW_AT_external";
4575     case DW_AT_frame_base:
4576       return "DW_AT_frame_base";
4577     case DW_AT_friend:
4578       return "DW_AT_friend";
4579     case DW_AT_identifier_case:
4580       return "DW_AT_identifier_case";
4581     case DW_AT_macro_info:
4582       return "DW_AT_macro_info";
4583     case DW_AT_namelist_items:
4584       return "DW_AT_namelist_items";
4585     case DW_AT_priority:
4586       return "DW_AT_priority";
4587     case DW_AT_segment:
4588       return "DW_AT_segment";
4589     case DW_AT_specification:
4590       return "DW_AT_specification";
4591     case DW_AT_static_link:
4592       return "DW_AT_static_link";
4593     case DW_AT_type:
4594       return "DW_AT_type";
4595     case DW_AT_use_location:
4596       return "DW_AT_use_location";
4597     case DW_AT_variable_parameter:
4598       return "DW_AT_variable_parameter";
4599     case DW_AT_virtuality:
4600       return "DW_AT_virtuality";
4601     case DW_AT_vtable_elem_location:
4602       return "DW_AT_vtable_elem_location";
4603
4604     case DW_AT_allocated:
4605       return "DW_AT_allocated";
4606     case DW_AT_associated:
4607       return "DW_AT_associated";
4608     case DW_AT_data_location:
4609       return "DW_AT_data_location";
4610     case DW_AT_stride:
4611       return "DW_AT_stride";
4612     case DW_AT_entry_pc:
4613       return "DW_AT_entry_pc";
4614     case DW_AT_use_UTF8:
4615       return "DW_AT_use_UTF8";
4616     case DW_AT_extension:
4617       return "DW_AT_extension";
4618     case DW_AT_ranges:
4619       return "DW_AT_ranges";
4620     case DW_AT_trampoline:
4621       return "DW_AT_trampoline";
4622     case DW_AT_call_column:
4623       return "DW_AT_call_column";
4624     case DW_AT_call_file:
4625       return "DW_AT_call_file";
4626     case DW_AT_call_line:
4627       return "DW_AT_call_line";
4628
4629     case DW_AT_MIPS_fde:
4630       return "DW_AT_MIPS_fde";
4631     case DW_AT_MIPS_loop_begin:
4632       return "DW_AT_MIPS_loop_begin";
4633     case DW_AT_MIPS_tail_loop_begin:
4634       return "DW_AT_MIPS_tail_loop_begin";
4635     case DW_AT_MIPS_epilog_begin:
4636       return "DW_AT_MIPS_epilog_begin";
4637     case DW_AT_MIPS_loop_unroll_factor:
4638       return "DW_AT_MIPS_loop_unroll_factor";
4639     case DW_AT_MIPS_software_pipeline_depth:
4640       return "DW_AT_MIPS_software_pipeline_depth";
4641     case DW_AT_MIPS_linkage_name:
4642       return "DW_AT_MIPS_linkage_name";
4643     case DW_AT_MIPS_stride:
4644       return "DW_AT_MIPS_stride";
4645     case DW_AT_MIPS_abstract_name:
4646       return "DW_AT_MIPS_abstract_name";
4647     case DW_AT_MIPS_clone_origin:
4648       return "DW_AT_MIPS_clone_origin";
4649     case DW_AT_MIPS_has_inlines:
4650       return "DW_AT_MIPS_has_inlines";
4651
4652     case DW_AT_sf_names:
4653       return "DW_AT_sf_names";
4654     case DW_AT_src_info:
4655       return "DW_AT_src_info";
4656     case DW_AT_mac_info:
4657       return "DW_AT_mac_info";
4658     case DW_AT_src_coords:
4659       return "DW_AT_src_coords";
4660     case DW_AT_body_begin:
4661       return "DW_AT_body_begin";
4662     case DW_AT_body_end:
4663       return "DW_AT_body_end";
4664     case DW_AT_GNU_vector:
4665       return "DW_AT_GNU_vector";
4666
4667     case DW_AT_VMS_rtnbeg_pd_address:
4668       return "DW_AT_VMS_rtnbeg_pd_address";
4669
4670     default:
4671       return "DW_AT_<unknown>";
4672     }
4673 }
4674
4675 /* Convert a DWARF value form code into its string name.  */
4676
4677 static const char *
4678 dwarf_form_name (unsigned int form)
4679 {
4680   switch (form)
4681     {
4682     case DW_FORM_addr:
4683       return "DW_FORM_addr";
4684     case DW_FORM_block2:
4685       return "DW_FORM_block2";
4686     case DW_FORM_block4:
4687       return "DW_FORM_block4";
4688     case DW_FORM_data2:
4689       return "DW_FORM_data2";
4690     case DW_FORM_data4:
4691       return "DW_FORM_data4";
4692     case DW_FORM_data8:
4693       return "DW_FORM_data8";
4694     case DW_FORM_string:
4695       return "DW_FORM_string";
4696     case DW_FORM_block:
4697       return "DW_FORM_block";
4698     case DW_FORM_block1:
4699       return "DW_FORM_block1";
4700     case DW_FORM_data1:
4701       return "DW_FORM_data1";
4702     case DW_FORM_flag:
4703       return "DW_FORM_flag";
4704     case DW_FORM_sdata:
4705       return "DW_FORM_sdata";
4706     case DW_FORM_strp:
4707       return "DW_FORM_strp";
4708     case DW_FORM_udata:
4709       return "DW_FORM_udata";
4710     case DW_FORM_ref_addr:
4711       return "DW_FORM_ref_addr";
4712     case DW_FORM_ref1:
4713       return "DW_FORM_ref1";
4714     case DW_FORM_ref2:
4715       return "DW_FORM_ref2";
4716     case DW_FORM_ref4:
4717       return "DW_FORM_ref4";
4718     case DW_FORM_ref8:
4719       return "DW_FORM_ref8";
4720     case DW_FORM_ref_udata:
4721       return "DW_FORM_ref_udata";
4722     case DW_FORM_indirect:
4723       return "DW_FORM_indirect";
4724     default:
4725       return "DW_FORM_<unknown>";
4726     }
4727 }
4728 \f
4729 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4730    instance of an inlined instance of a decl which is local to an inline
4731    function, so we have to trace all of the way back through the origin chain
4732    to find out what sort of node actually served as the original seed for the
4733    given block.  */
4734
4735 static tree
4736 decl_ultimate_origin (tree decl)
4737 {
4738   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4739     return NULL_TREE;
4740
4741   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4742      nodes in the function to point to themselves; ignore that if
4743      we're trying to output the abstract instance of this function.  */
4744   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4745     return NULL_TREE;
4746
4747   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4748      most distant ancestor, this should never happen.  */
4749   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4750
4751   return DECL_ABSTRACT_ORIGIN (decl);
4752 }
4753
4754 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4755    instance of an inlined instance of a block which is local to an inline
4756    function, so we have to trace all of the way back through the origin chain
4757    to find out what sort of node actually served as the original seed for the
4758    given block.  */
4759
4760 static tree
4761 block_ultimate_origin (tree block)
4762 {
4763   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4764
4765   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4766      nodes in the function to point to themselves; ignore that if
4767      we're trying to output the abstract instance of this function.  */
4768   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4769     return NULL_TREE;
4770
4771   if (immediate_origin == NULL_TREE)
4772     return NULL_TREE;
4773   else
4774     {
4775       tree ret_val;
4776       tree lookahead = immediate_origin;
4777
4778       do
4779         {
4780           ret_val = lookahead;
4781           lookahead = (TREE_CODE (ret_val) == BLOCK
4782                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4783         }
4784       while (lookahead != NULL && lookahead != ret_val);
4785       
4786       /* The block's abstract origin chain may not be the *ultimate* origin of
4787          the block. It could lead to a DECL that has an abstract origin set.
4788          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4789          will give us if it has one).  Note that DECL's abstract origins are
4790          supposed to be the most distant ancestor (or so decl_ultimate_origin
4791          claims), so we don't need to loop following the DECL origins.  */
4792       if (DECL_P (ret_val))
4793         return DECL_ORIGIN (ret_val);
4794
4795       return ret_val;
4796     }
4797 }
4798
4799 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4800    of a virtual function may refer to a base class, so we check the 'this'
4801    parameter.  */
4802
4803 static tree
4804 decl_class_context (tree decl)
4805 {
4806   tree context = NULL_TREE;
4807
4808   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4809     context = DECL_CONTEXT (decl);
4810   else
4811     context = TYPE_MAIN_VARIANT
4812       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4813
4814   if (context && !TYPE_P (context))
4815     context = NULL_TREE;
4816
4817   return context;
4818 }
4819 \f
4820 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4821    addition order, and correct that in reverse_all_dies.  */
4822
4823 static inline void
4824 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4825 {
4826   if (die != NULL && attr != NULL)
4827     {
4828       attr->dw_attr_next = die->die_attr;
4829       die->die_attr = attr;
4830     }
4831 }
4832
4833 static inline enum dw_val_class
4834 AT_class (dw_attr_ref a)
4835 {
4836   return a->dw_attr_val.val_class;
4837 }
4838
4839 /* Add a flag value attribute to a DIE.  */
4840
4841 static inline void
4842 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4843 {
4844   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4845
4846   attr->dw_attr_next = NULL;
4847   attr->dw_attr = attr_kind;
4848   attr->dw_attr_val.val_class = dw_val_class_flag;
4849   attr->dw_attr_val.v.val_flag = flag;
4850   add_dwarf_attr (die, attr);
4851 }
4852
4853 static inline unsigned
4854 AT_flag (dw_attr_ref a)
4855 {
4856   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4857   return a->dw_attr_val.v.val_flag;
4858 }
4859
4860 /* Add a signed integer attribute value to a DIE.  */
4861
4862 static inline void
4863 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4864 {
4865   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4866
4867   attr->dw_attr_next = NULL;
4868   attr->dw_attr = attr_kind;
4869   attr->dw_attr_val.val_class = dw_val_class_const;
4870   attr->dw_attr_val.v.val_int = int_val;
4871   add_dwarf_attr (die, attr);
4872 }
4873
4874 static inline HOST_WIDE_INT
4875 AT_int (dw_attr_ref a)
4876 {
4877   gcc_assert (a && AT_class (a) == dw_val_class_const);
4878   return a->dw_attr_val.v.val_int;
4879 }
4880
4881 /* Add an unsigned integer attribute value to a DIE.  */
4882
4883 static inline void
4884 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4885                  unsigned HOST_WIDE_INT unsigned_val)
4886 {
4887   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4888
4889   attr->dw_attr_next = NULL;
4890   attr->dw_attr = attr_kind;
4891   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4892   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4893   add_dwarf_attr (die, attr);
4894 }
4895
4896 static inline unsigned HOST_WIDE_INT
4897 AT_unsigned (dw_attr_ref a)
4898 {
4899   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4900   return a->dw_attr_val.v.val_unsigned;
4901 }
4902
4903 /* Add an unsigned double integer attribute value to a DIE.  */
4904
4905 static inline void
4906 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4907                   long unsigned int val_hi, long unsigned int val_low)
4908 {
4909   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4910
4911   attr->dw_attr_next = NULL;
4912   attr->dw_attr = attr_kind;
4913   attr->dw_attr_val.val_class = dw_val_class_long_long;
4914   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4915   attr->dw_attr_val.v.val_long_long.low = val_low;
4916   add_dwarf_attr (die, attr);
4917 }
4918
4919 /* Add a floating point attribute value to a DIE and return it.  */
4920
4921 static inline void
4922 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4923             unsigned int length, unsigned int elt_size, unsigned char *array)
4924 {
4925   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4926
4927   attr->dw_attr_next = NULL;
4928   attr->dw_attr = attr_kind;
4929   attr->dw_attr_val.val_class = dw_val_class_vec;
4930   attr->dw_attr_val.v.val_vec.length = length;
4931   attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4932   attr->dw_attr_val.v.val_vec.array = array;
4933   add_dwarf_attr (die, attr);
4934 }
4935
4936 /* Hash and equality functions for debug_str_hash.  */
4937
4938 static hashval_t
4939 debug_str_do_hash (const void *x)
4940 {
4941   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4942 }
4943
4944 static int
4945 debug_str_eq (const void *x1, const void *x2)
4946 {
4947   return strcmp ((((const struct indirect_string_node *)x1)->str),
4948                  (const char *)x2) == 0;
4949 }
4950
4951 /* Add a string attribute value to a DIE.  */
4952
4953 static inline void
4954 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4955 {
4956   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4957   struct indirect_string_node *node;
4958   void **slot;
4959
4960   if (! debug_str_hash)
4961     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4962                                       debug_str_eq, NULL);
4963
4964   slot = htab_find_slot_with_hash (debug_str_hash, str,
4965                                    htab_hash_string (str), INSERT);
4966   if (*slot == NULL)
4967     *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4968   node = (struct indirect_string_node *) *slot;
4969   node->str = ggc_strdup (str);
4970   node->refcount++;
4971
4972   attr->dw_attr_next = NULL;
4973   attr->dw_attr = attr_kind;
4974   attr->dw_attr_val.val_class = dw_val_class_str;
4975   attr->dw_attr_val.v.val_str = node;
4976   add_dwarf_attr (die, attr);
4977 }
4978
4979 static inline const char *
4980 AT_string (dw_attr_ref a)
4981 {
4982   gcc_assert (a && AT_class (a) == dw_val_class_str);
4983   return a->dw_attr_val.v.val_str->str;
4984 }
4985
4986 /* Find out whether a string should be output inline in DIE
4987    or out-of-line in .debug_str section.  */
4988
4989 static int
4990 AT_string_form (dw_attr_ref a)
4991 {
4992   struct indirect_string_node *node;
4993   unsigned int len;
4994   char label[32];
4995
4996   gcc_assert (a && AT_class (a) == dw_val_class_str);
4997
4998   node = a->dw_attr_val.v.val_str;
4999   if (node->form)
5000     return node->form;
5001
5002   len = strlen (node->str) + 1;
5003
5004   /* If the string is shorter or equal to the size of the reference, it is
5005      always better to put it inline.  */
5006   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5007     return node->form = DW_FORM_string;
5008
5009   /* If we cannot expect the linker to merge strings in .debug_str
5010      section, only put it into .debug_str if it is worth even in this
5011      single module.  */
5012   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5013       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5014     return node->form = DW_FORM_string;
5015
5016   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5017   ++dw2_string_counter;
5018   node->label = xstrdup (label);
5019
5020   return node->form = DW_FORM_strp;
5021 }
5022
5023 /* Add a DIE reference attribute value to a DIE.  */
5024
5025 static inline void
5026 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5027 {
5028   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5029
5030   attr->dw_attr_next = NULL;
5031   attr->dw_attr = attr_kind;
5032   attr->dw_attr_val.val_class = dw_val_class_die_ref;
5033   attr->dw_attr_val.v.val_die_ref.die = targ_die;
5034   attr->dw_attr_val.v.val_die_ref.external = 0;
5035   add_dwarf_attr (die, attr);
5036 }
5037
5038 /* Add an AT_specification attribute to a DIE, and also make the back
5039    pointer from the specification to the definition.  */
5040
5041 static inline void
5042 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5043 {
5044   add_AT_die_ref (die, DW_AT_specification, targ_die);
5045   gcc_assert (!targ_die->die_definition);
5046   targ_die->die_definition = die;
5047 }
5048
5049 static inline dw_die_ref
5050 AT_ref (dw_attr_ref a)
5051 {
5052   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5053   return a->dw_attr_val.v.val_die_ref.die;
5054 }
5055
5056 static inline int
5057 AT_ref_external (dw_attr_ref a)
5058 {
5059   if (a && AT_class (a) == dw_val_class_die_ref)
5060     return a->dw_attr_val.v.val_die_ref.external;
5061
5062   return 0;
5063 }
5064
5065 static inline void
5066 set_AT_ref_external (dw_attr_ref a, int i)
5067 {
5068   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5069   a->dw_attr_val.v.val_die_ref.external = i;
5070 }
5071
5072 /* Add an FDE reference attribute value to a DIE.  */
5073
5074 static inline void
5075 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5076 {
5077   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5078
5079   attr->dw_attr_next = NULL;
5080   attr->dw_attr = attr_kind;
5081   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5082   attr->dw_attr_val.v.val_fde_index = targ_fde;
5083   add_dwarf_attr (die, attr);
5084 }
5085
5086 /* Add a location description attribute value to a DIE.  */
5087
5088 static inline void
5089 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5090 {
5091   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5092
5093   attr->dw_attr_next = NULL;
5094   attr->dw_attr = attr_kind;
5095   attr->dw_attr_val.val_class = dw_val_class_loc;
5096   attr->dw_attr_val.v.val_loc = loc;
5097   add_dwarf_attr (die, attr);
5098 }
5099
5100 static inline dw_loc_descr_ref
5101 AT_loc (dw_attr_ref a)
5102 {
5103   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5104   return a->dw_attr_val.v.val_loc;
5105 }
5106
5107 static inline void
5108 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5109 {
5110   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5111
5112   attr->dw_attr_next = NULL;
5113   attr->dw_attr = attr_kind;
5114   attr->dw_attr_val.val_class = dw_val_class_loc_list;
5115   attr->dw_attr_val.v.val_loc_list = loc_list;
5116   add_dwarf_attr (die, attr);
5117   have_location_lists = true;
5118 }
5119
5120 static inline dw_loc_list_ref
5121 AT_loc_list (dw_attr_ref a)
5122 {
5123   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5124   return a->dw_attr_val.v.val_loc_list;
5125 }
5126
5127 /* Add an address constant attribute value to a DIE.  */
5128
5129 static inline void
5130 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5131 {
5132   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5133
5134   attr->dw_attr_next = NULL;
5135   attr->dw_attr = attr_kind;
5136   attr->dw_attr_val.val_class = dw_val_class_addr;
5137   attr->dw_attr_val.v.val_addr = addr;
5138   add_dwarf_attr (die, attr);
5139 }
5140
5141 static inline rtx
5142 AT_addr (dw_attr_ref a)
5143 {
5144   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5145   return a->dw_attr_val.v.val_addr;
5146 }
5147
5148 /* Add a label identifier attribute value to a DIE.  */
5149
5150 static inline void
5151 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5152 {
5153   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5154
5155   attr->dw_attr_next = NULL;
5156   attr->dw_attr = attr_kind;
5157   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5158   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5159   add_dwarf_attr (die, attr);
5160 }
5161
5162 /* Add a section offset attribute value to a DIE.  */
5163
5164 static inline void
5165 add_AT_lbl_offset (dw_die_ref die, enum dwarf_attribute attr_kind, const char *label)
5166 {
5167   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5168
5169   attr->dw_attr_next = NULL;
5170   attr->dw_attr = attr_kind;
5171   attr->dw_attr_val.val_class = dw_val_class_lbl_offset;
5172   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5173   add_dwarf_attr (die, attr);
5174 }
5175
5176 /* Add an offset attribute value to a DIE.  */
5177
5178 static inline void
5179 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5180                unsigned HOST_WIDE_INT offset)
5181 {
5182   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5183
5184   attr->dw_attr_next = NULL;
5185   attr->dw_attr = attr_kind;
5186   attr->dw_attr_val.val_class = dw_val_class_offset;
5187   attr->dw_attr_val.v.val_offset = offset;
5188   add_dwarf_attr (die, attr);
5189 }
5190
5191 /* Add an range_list attribute value to a DIE.  */
5192
5193 static void
5194 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5195                    long unsigned int offset)
5196 {
5197   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5198
5199   attr->dw_attr_next = NULL;
5200   attr->dw_attr = attr_kind;
5201   attr->dw_attr_val.val_class = dw_val_class_range_list;
5202   attr->dw_attr_val.v.val_offset = offset;
5203   add_dwarf_attr (die, attr);
5204 }
5205
5206 static inline const char *
5207 AT_lbl (dw_attr_ref a)
5208 {
5209   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5210                     || AT_class (a) == dw_val_class_lbl_offset));
5211   return a->dw_attr_val.v.val_lbl_id;
5212 }
5213
5214 /* Get the attribute of type attr_kind.  */
5215
5216 static dw_attr_ref
5217 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5218 {
5219   dw_attr_ref a;
5220   dw_die_ref spec = NULL;
5221
5222   if (die != NULL)
5223     {
5224       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5225         if (a->dw_attr == attr_kind)
5226           return a;
5227         else if (a->dw_attr == DW_AT_specification
5228                  || a->dw_attr == DW_AT_abstract_origin)
5229           spec = AT_ref (a);
5230
5231       if (spec)
5232         return get_AT (spec, attr_kind);
5233     }
5234
5235   return NULL;
5236 }
5237
5238 /* Return the "low pc" attribute value, typically associated with a subprogram
5239    DIE.  Return null if the "low pc" attribute is either not present, or if it
5240    cannot be represented as an assembler label identifier.  */
5241
5242 static inline const char *
5243 get_AT_low_pc (dw_die_ref die)
5244 {
5245   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5246
5247   return a ? AT_lbl (a) : NULL;
5248 }
5249
5250 /* Return the "high pc" attribute value, typically associated with a subprogram
5251    DIE.  Return null if the "high pc" attribute is either not present, or if it
5252    cannot be represented as an assembler label identifier.  */
5253
5254 static inline const char *
5255 get_AT_hi_pc (dw_die_ref die)
5256 {
5257   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5258
5259   return a ? AT_lbl (a) : NULL;
5260 }
5261
5262 /* Return the value of the string attribute designated by ATTR_KIND, or
5263    NULL if it is not present.  */
5264
5265 static inline const char *
5266 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5267 {
5268   dw_attr_ref a = get_AT (die, attr_kind);
5269
5270   return a ? AT_string (a) : NULL;
5271 }
5272
5273 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5274    if it is not present.  */
5275
5276 static inline int
5277 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5278 {
5279   dw_attr_ref a = get_AT (die, attr_kind);
5280
5281   return a ? AT_flag (a) : 0;
5282 }
5283
5284 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5285    if it is not present.  */
5286
5287 static inline unsigned
5288 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5289 {
5290   dw_attr_ref a = get_AT (die, attr_kind);
5291
5292   return a ? AT_unsigned (a) : 0;
5293 }
5294
5295 static inline dw_die_ref
5296 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5297 {
5298   dw_attr_ref a = get_AT (die, attr_kind);
5299
5300   return a ? AT_ref (a) : NULL;
5301 }
5302
5303 /* Return TRUE if the language is C or C++.  */
5304
5305 static inline bool
5306 is_c_family (void)
5307 {
5308   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5309
5310   return (lang == DW_LANG_C || lang == DW_LANG_C89
5311           || lang == DW_LANG_C_plus_plus);
5312 }
5313
5314 /* Return TRUE if the language is C++.  */
5315
5316 static inline bool
5317 is_cxx (void)
5318 {
5319   return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5320           == DW_LANG_C_plus_plus);
5321 }
5322
5323 /* Return TRUE if the language is Fortran.  */
5324
5325 static inline bool
5326 is_fortran (void)
5327 {
5328   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5329
5330   return (lang == DW_LANG_Fortran77
5331           || lang == DW_LANG_Fortran90
5332           || lang == DW_LANG_Fortran95);
5333 }
5334
5335 /* Return TRUE if the language is Java.  */
5336
5337 static inline bool
5338 is_java (void)
5339 {
5340   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5341
5342   return lang == DW_LANG_Java;
5343 }
5344
5345 /* Return TRUE if the language is Ada.  */
5346
5347 static inline bool
5348 is_ada (void)
5349 {
5350   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5351
5352   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5353 }
5354
5355 /* Free up the memory used by A.  */
5356
5357 static inline void free_AT (dw_attr_ref);
5358 static inline void
5359 free_AT (dw_attr_ref a)
5360 {
5361   if (AT_class (a) == dw_val_class_str)
5362     if (a->dw_attr_val.v.val_str->refcount)
5363       a->dw_attr_val.v.val_str->refcount--;
5364 }
5365
5366 /* Remove the specified attribute if present.  */
5367
5368 static void
5369 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5370 {
5371   dw_attr_ref *p;
5372   dw_attr_ref removed = NULL;
5373
5374   if (die != NULL)
5375     {
5376       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5377         if ((*p)->dw_attr == attr_kind)
5378           {
5379             removed = *p;
5380             *p = (*p)->dw_attr_next;
5381             break;
5382           }
5383
5384       if (removed != 0)
5385         free_AT (removed);
5386     }
5387 }
5388
5389 /* Remove child die whose die_tag is specified tag.  */
5390
5391 static void
5392 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5393 {
5394   dw_die_ref current, prev, next;
5395   current = die->die_child;
5396   prev = NULL;
5397   while (current != NULL)
5398     {
5399       if (current->die_tag == tag)
5400         {
5401           next = current->die_sib;
5402           if (prev == NULL)
5403             die->die_child = next;
5404           else
5405             prev->die_sib = next;
5406           free_die (current);
5407           current = next;
5408         }
5409       else
5410         {
5411           prev = current;
5412           current = current->die_sib;
5413         }
5414     }
5415 }
5416
5417 /* Free up the memory used by DIE.  */
5418
5419 static inline void
5420 free_die (dw_die_ref die)
5421 {
5422   remove_children (die);
5423 }
5424
5425 /* Discard the children of this DIE.  */
5426
5427 static void
5428 remove_children (dw_die_ref die)
5429 {
5430   dw_die_ref child_die = die->die_child;
5431
5432   die->die_child = NULL;
5433
5434   while (child_die != NULL)
5435     {
5436       dw_die_ref tmp_die = child_die;
5437       dw_attr_ref a;
5438
5439       child_die = child_die->die_sib;
5440
5441       for (a = tmp_die->die_attr; a != NULL;)
5442         {
5443           dw_attr_ref tmp_a = a;
5444
5445           a = a->dw_attr_next;
5446           free_AT (tmp_a);
5447         }
5448
5449       free_die (tmp_die);
5450     }
5451 }
5452
5453 /* Add a child DIE below its parent.  We build the lists up in reverse
5454    addition order, and correct that in reverse_all_dies.  */
5455
5456 static inline void
5457 add_child_die (dw_die_ref die, dw_die_ref child_die)
5458 {
5459   if (die != NULL && child_die != NULL)
5460     {
5461       gcc_assert (die != child_die);
5462
5463       child_die->die_parent = die;
5464       child_die->die_sib = die->die_child;
5465       die->die_child = child_die;
5466     }
5467 }
5468
5469 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5470    is the specification, to the front of PARENT's list of children.  */
5471
5472 static void
5473 splice_child_die (dw_die_ref parent, dw_die_ref child)
5474 {
5475   dw_die_ref *p;
5476
5477   /* We want the declaration DIE from inside the class, not the
5478      specification DIE at toplevel.  */
5479   if (child->die_parent != parent)
5480     {
5481       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5482
5483       if (tmp)
5484         child = tmp;
5485     }
5486
5487   gcc_assert (child->die_parent == parent
5488               || (child->die_parent
5489                   == get_AT_ref (parent, DW_AT_specification)));
5490
5491   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5492     if (*p == child)
5493       {
5494         *p = child->die_sib;
5495         break;
5496       }
5497
5498   child->die_parent = parent;
5499   child->die_sib = parent->die_child;
5500   parent->die_child = child;
5501 }
5502
5503 /* Return a pointer to a newly created DIE node.  */
5504
5505 static inline dw_die_ref
5506 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5507 {
5508   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5509
5510   die->die_tag = tag_value;
5511
5512   if (parent_die != NULL)
5513     add_child_die (parent_die, die);
5514   else
5515     {
5516       limbo_die_node *limbo_node;
5517
5518       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5519       limbo_node->die = die;
5520       limbo_node->created_for = t;
5521       limbo_node->next = limbo_die_list;
5522       limbo_die_list = limbo_node;
5523     }
5524
5525   return die;
5526 }
5527
5528 /* Return the DIE associated with the given type specifier.  */
5529
5530 static inline dw_die_ref
5531 lookup_type_die (tree type)
5532 {
5533   return TYPE_SYMTAB_DIE (type);
5534 }
5535
5536 /* Equate a DIE to a given type specifier.  */
5537
5538 static inline void
5539 equate_type_number_to_die (tree type, dw_die_ref type_die)
5540 {
5541   TYPE_SYMTAB_DIE (type) = type_die;
5542 }
5543
5544 /* Returns a hash value for X (which really is a die_struct).  */
5545
5546 static hashval_t
5547 decl_die_table_hash (const void *x)
5548 {
5549   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5550 }
5551
5552 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5553
5554 static int
5555 decl_die_table_eq (const void *x, const void *y)
5556 {
5557   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5558 }
5559
5560 /* Return the DIE associated with a given declaration.  */
5561
5562 static inline dw_die_ref
5563 lookup_decl_die (tree decl)
5564 {
5565   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5566 }
5567
5568 /* Returns a hash value for X (which really is a var_loc_list).  */
5569
5570 static hashval_t
5571 decl_loc_table_hash (const void *x)
5572 {
5573   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5574 }
5575
5576 /* Return nonzero if decl_id of var_loc_list X is the same as
5577    UID of decl *Y.  */
5578
5579 static int
5580 decl_loc_table_eq (const void *x, const void *y)
5581 {
5582   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5583 }
5584
5585 /* Return the var_loc list associated with a given declaration.  */
5586
5587 static inline var_loc_list *
5588 lookup_decl_loc (tree decl)
5589 {
5590   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5591 }
5592
5593 /* Equate a DIE to a particular declaration.  */
5594
5595 static void
5596 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5597 {
5598   unsigned int decl_id = DECL_UID (decl);
5599   void **slot;
5600
5601   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5602   *slot = decl_die;
5603   decl_die->decl_id = decl_id;
5604 }
5605
5606 /* Add a variable location node to the linked list for DECL.  */
5607
5608 static void
5609 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5610 {
5611   unsigned int decl_id = DECL_UID (decl);
5612   var_loc_list *temp;
5613   void **slot;
5614
5615   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5616   if (*slot == NULL)
5617     {
5618       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5619       temp->decl_id = decl_id;
5620       *slot = temp;
5621     }
5622   else
5623     temp = *slot;
5624
5625   if (temp->last)
5626     {
5627       /* If the current location is the same as the end of the list,
5628          we have nothing to do.  */
5629       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5630                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5631         {
5632           /* Add LOC to the end of list and update LAST.  */
5633           temp->last->next = loc;
5634           temp->last = loc;
5635         }
5636     }
5637   /* Do not add empty location to the beginning of the list.  */
5638   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5639     {
5640       temp->first = loc;
5641       temp->last = loc;
5642     }
5643 }
5644 \f
5645 /* Keep track of the number of spaces used to indent the
5646    output of the debugging routines that print the structure of
5647    the DIE internal representation.  */
5648 static int print_indent;
5649
5650 /* Indent the line the number of spaces given by print_indent.  */
5651
5652 static inline void
5653 print_spaces (FILE *outfile)
5654 {
5655   fprintf (outfile, "%*s", print_indent, "");
5656 }
5657
5658 /* Print the information associated with a given DIE, and its children.
5659    This routine is a debugging aid only.  */
5660
5661 static void
5662 print_die (dw_die_ref die, FILE *outfile)
5663 {
5664   dw_attr_ref a;
5665   dw_die_ref c;
5666
5667   print_spaces (outfile);
5668   fprintf (outfile, "DIE %4lu: %s\n",
5669            die->die_offset, dwarf_tag_name (die->die_tag));
5670   print_spaces (outfile);
5671   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5672   fprintf (outfile, " offset: %lu\n", die->die_offset);
5673
5674   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5675     {
5676       print_spaces (outfile);
5677       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5678
5679       switch (AT_class (a))
5680         {
5681         case dw_val_class_addr:
5682           fprintf (outfile, "address");
5683           break;
5684         case dw_val_class_offset:
5685           fprintf (outfile, "offset");
5686           break;
5687         case dw_val_class_loc:
5688           fprintf (outfile, "location descriptor");
5689           break;
5690         case dw_val_class_loc_list:
5691           fprintf (outfile, "location list -> label:%s",
5692                    AT_loc_list (a)->ll_symbol);
5693           break;
5694         case dw_val_class_range_list:
5695           fprintf (outfile, "range list");
5696           break;
5697         case dw_val_class_const:
5698           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5699           break;
5700         case dw_val_class_unsigned_const:
5701           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5702           break;
5703         case dw_val_class_long_long:
5704           fprintf (outfile, "constant (%lu,%lu)",
5705                    a->dw_attr_val.v.val_long_long.hi,
5706                    a->dw_attr_val.v.val_long_long.low);
5707           break;
5708         case dw_val_class_vec:
5709           fprintf (outfile, "floating-point or vector constant");
5710           break;
5711         case dw_val_class_flag:
5712           fprintf (outfile, "%u", AT_flag (a));
5713           break;
5714         case dw_val_class_die_ref:
5715           if (AT_ref (a) != NULL)
5716             {
5717               if (AT_ref (a)->die_symbol)
5718                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5719               else
5720                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5721             }
5722           else
5723             fprintf (outfile, "die -> <null>");
5724           break;
5725         case dw_val_class_lbl_id:
5726         case dw_val_class_lbl_offset:
5727           fprintf (outfile, "label: %s", AT_lbl (a));
5728           break;
5729         case dw_val_class_str:
5730           if (AT_string (a) != NULL)
5731             fprintf (outfile, "\"%s\"", AT_string (a));
5732           else
5733             fprintf (outfile, "<null>");
5734           break;
5735         default:
5736           break;
5737         }
5738
5739       fprintf (outfile, "\n");
5740     }
5741
5742   if (die->die_child != NULL)
5743     {
5744       print_indent += 4;
5745       for (c = die->die_child; c != NULL; c = c->die_sib)
5746         print_die (c, outfile);
5747
5748       print_indent -= 4;
5749     }
5750   if (print_indent == 0)
5751     fprintf (outfile, "\n");
5752 }
5753
5754 /* Print the contents of the source code line number correspondence table.
5755    This routine is a debugging aid only.  */
5756
5757 static void
5758 print_dwarf_line_table (FILE *outfile)
5759 {
5760   unsigned i;
5761   dw_line_info_ref line_info;
5762
5763   fprintf (outfile, "\n\nDWARF source line information\n");
5764   for (i = 1; i < line_info_table_in_use; i++)
5765     {
5766       line_info = &line_info_table[i];
5767       fprintf (outfile, "%5d: ", i);
5768       fprintf (outfile, "%-20s",
5769                VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5770       fprintf (outfile, "%6ld", line_info->dw_line_num);
5771       fprintf (outfile, "\n");
5772     }
5773
5774   fprintf (outfile, "\n\n");
5775 }
5776
5777 /* Print the information collected for a given DIE.  */
5778
5779 void
5780 debug_dwarf_die (dw_die_ref die)
5781 {
5782   print_die (die, stderr);
5783 }
5784
5785 /* Print all DWARF information collected for the compilation unit.
5786    This routine is a debugging aid only.  */
5787
5788 void
5789 debug_dwarf (void)
5790 {
5791   print_indent = 0;
5792   print_die (comp_unit_die, stderr);
5793   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5794     print_dwarf_line_table (stderr);
5795 }
5796 \f
5797 /* We build up the lists of children and attributes by pushing new ones
5798    onto the beginning of the list.  Reverse the lists for DIE so that
5799    they are in order of addition.  */
5800
5801 static void
5802 reverse_die_lists (dw_die_ref die)
5803 {
5804   dw_die_ref c, cp, cn;
5805   dw_attr_ref a, ap, an;
5806
5807   for (a = die->die_attr, ap = 0; a; a = an)
5808     {
5809       an = a->dw_attr_next;
5810       a->dw_attr_next = ap;
5811       ap = a;
5812     }
5813
5814   die->die_attr = ap;
5815
5816   for (c = die->die_child, cp = 0; c; c = cn)
5817     {
5818       cn = c->die_sib;
5819       c->die_sib = cp;
5820       cp = c;
5821     }
5822
5823   die->die_child = cp;
5824 }
5825
5826 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5827    reverse all dies in add_sibling_attributes, which runs through all the dies,
5828    it would reverse all the dies.  Now, however, since we don't call
5829    reverse_die_lists in add_sibling_attributes, we need a routine to
5830    recursively reverse all the dies. This is that routine.  */
5831
5832 static void
5833 reverse_all_dies (dw_die_ref die)
5834 {
5835   dw_die_ref c;
5836
5837   reverse_die_lists (die);
5838
5839   for (c = die->die_child; c; c = c->die_sib)
5840     reverse_all_dies (c);
5841 }
5842
5843 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5844    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5845    DIE that marks the start of the DIEs for this include file.  */
5846
5847 static dw_die_ref
5848 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5849 {
5850   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5851   dw_die_ref new_unit = gen_compile_unit_die (filename);
5852
5853   new_unit->die_sib = old_unit;
5854   return new_unit;
5855 }
5856
5857 /* Close an include-file CU and reopen the enclosing one.  */
5858
5859 static dw_die_ref
5860 pop_compile_unit (dw_die_ref old_unit)
5861 {
5862   dw_die_ref new_unit = old_unit->die_sib;
5863
5864   old_unit->die_sib = NULL;
5865   return new_unit;
5866 }
5867
5868 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5869 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5870
5871 /* Calculate the checksum of a location expression.  */
5872
5873 static inline void
5874 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5875 {
5876   CHECKSUM (loc->dw_loc_opc);
5877   CHECKSUM (loc->dw_loc_oprnd1);
5878   CHECKSUM (loc->dw_loc_oprnd2);
5879 }
5880
5881 /* Calculate the checksum of an attribute.  */
5882
5883 static void
5884 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5885 {
5886   dw_loc_descr_ref loc;
5887   rtx r;
5888
5889   CHECKSUM (at->dw_attr);
5890
5891   /* We don't care about differences in file numbering.  */
5892   if (at->dw_attr == DW_AT_decl_file
5893       /* Or that this was compiled with a different compiler snapshot; if
5894          the output is the same, that's what matters.  */
5895       || at->dw_attr == DW_AT_producer)
5896     return;
5897
5898   switch (AT_class (at))
5899     {
5900     case dw_val_class_const:
5901       CHECKSUM (at->dw_attr_val.v.val_int);
5902       break;
5903     case dw_val_class_unsigned_const:
5904       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5905       break;
5906     case dw_val_class_long_long:
5907       CHECKSUM (at->dw_attr_val.v.val_long_long);
5908       break;
5909     case dw_val_class_vec:
5910       CHECKSUM (at->dw_attr_val.v.val_vec);
5911       break;
5912     case dw_val_class_flag:
5913       CHECKSUM (at->dw_attr_val.v.val_flag);
5914       break;
5915     case dw_val_class_str:
5916       CHECKSUM_STRING (AT_string (at));
5917       break;
5918
5919     case dw_val_class_addr:
5920       r = AT_addr (at);
5921       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5922       CHECKSUM_STRING (XSTR (r, 0));
5923       break;
5924
5925     case dw_val_class_offset:
5926       CHECKSUM (at->dw_attr_val.v.val_offset);
5927       break;
5928
5929     case dw_val_class_loc:
5930       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5931         loc_checksum (loc, ctx);
5932       break;
5933
5934     case dw_val_class_die_ref:
5935       die_checksum (AT_ref (at), ctx, mark);
5936       break;
5937
5938     case dw_val_class_fde_ref:
5939     case dw_val_class_lbl_id:
5940     case dw_val_class_lbl_offset:
5941       break;
5942
5943     default:
5944       break;
5945     }
5946 }
5947
5948 /* Calculate the checksum of a DIE.  */
5949
5950 static void
5951 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5952 {
5953   dw_die_ref c;
5954   dw_attr_ref a;
5955
5956   /* To avoid infinite recursion.  */
5957   if (die->die_mark)
5958     {
5959       CHECKSUM (die->die_mark);
5960       return;
5961     }
5962   die->die_mark = ++(*mark);
5963
5964   CHECKSUM (die->die_tag);
5965
5966   for (a = die->die_attr; a; a = a->dw_attr_next)
5967     attr_checksum (a, ctx, mark);
5968
5969   for (c = die->die_child; c; c = c->die_sib)
5970     die_checksum (c, ctx, mark);
5971 }
5972
5973 #undef CHECKSUM
5974 #undef CHECKSUM_STRING
5975
5976 /* Do the location expressions look same?  */
5977 static inline int
5978 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5979 {
5980   return loc1->dw_loc_opc == loc2->dw_loc_opc
5981          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5982          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5983 }
5984
5985 /* Do the values look the same?  */
5986 static int
5987 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
5988 {
5989   dw_loc_descr_ref loc1, loc2;
5990   rtx r1, r2;
5991
5992   if (v1->val_class != v2->val_class)
5993     return 0;
5994
5995   switch (v1->val_class)
5996     {
5997     case dw_val_class_const:
5998       return v1->v.val_int == v2->v.val_int;
5999     case dw_val_class_unsigned_const:
6000       return v1->v.val_unsigned == v2->v.val_unsigned;
6001     case dw_val_class_long_long:
6002       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6003              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6004     case dw_val_class_vec:
6005       if (v1->v.val_vec.length != v2->v.val_vec.length
6006           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6007         return 0;
6008       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6009                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6010         return 0;
6011       return 1;
6012     case dw_val_class_flag:
6013       return v1->v.val_flag == v2->v.val_flag;
6014     case dw_val_class_str:
6015       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6016
6017     case dw_val_class_addr:
6018       r1 = v1->v.val_addr;
6019       r2 = v2->v.val_addr;
6020       if (GET_CODE (r1) != GET_CODE (r2))
6021         return 0;
6022       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6023       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6024
6025     case dw_val_class_offset:
6026       return v1->v.val_offset == v2->v.val_offset;
6027
6028     case dw_val_class_loc:
6029       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6030            loc1 && loc2;
6031            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6032         if (!same_loc_p (loc1, loc2, mark))
6033           return 0;
6034       return !loc1 && !loc2;
6035
6036     case dw_val_class_die_ref:
6037       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6038
6039     case dw_val_class_fde_ref:
6040     case dw_val_class_lbl_id:
6041     case dw_val_class_lbl_offset:
6042       return 1;
6043
6044     default:
6045       return 1;
6046     }
6047 }
6048
6049 /* Do the attributes look the same?  */
6050
6051 static int
6052 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6053 {
6054   if (at1->dw_attr != at2->dw_attr)
6055     return 0;
6056
6057   /* We don't care about differences in file numbering.  */
6058   if (at1->dw_attr == DW_AT_decl_file
6059       /* Or that this was compiled with a different compiler snapshot; if
6060          the output is the same, that's what matters.  */
6061       || at1->dw_attr == DW_AT_producer)
6062     return 1;
6063
6064   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6065 }
6066
6067 /* Do the dies look the same?  */
6068
6069 static int
6070 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6071 {
6072   dw_die_ref c1, c2;
6073   dw_attr_ref a1, a2;
6074
6075   /* To avoid infinite recursion.  */
6076   if (die1->die_mark)
6077     return die1->die_mark == die2->die_mark;
6078   die1->die_mark = die2->die_mark = ++(*mark);
6079
6080   if (die1->die_tag != die2->die_tag)
6081     return 0;
6082
6083   for (a1 = die1->die_attr, a2 = die2->die_attr;
6084        a1 && a2;
6085        a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6086     if (!same_attr_p (a1, a2, mark))
6087       return 0;
6088   if (a1 || a2)
6089     return 0;
6090
6091   for (c1 = die1->die_child, c2 = die2->die_child;
6092        c1 && c2;
6093        c1 = c1->die_sib, c2 = c2->die_sib)
6094     if (!same_die_p (c1, c2, mark))
6095       return 0;
6096   if (c1 || c2)
6097     return 0;
6098
6099   return 1;
6100 }
6101
6102 /* Do the dies look the same?  Wrapper around same_die_p.  */
6103
6104 static int
6105 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6106 {
6107   int mark = 0;
6108   int ret = same_die_p (die1, die2, &mark);
6109
6110   unmark_all_dies (die1);
6111   unmark_all_dies (die2);
6112
6113   return ret;
6114 }
6115
6116 /* The prefix to attach to symbols on DIEs in the current comdat debug
6117    info section.  */
6118 static char *comdat_symbol_id;
6119
6120 /* The index of the current symbol within the current comdat CU.  */
6121 static unsigned int comdat_symbol_number;
6122
6123 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6124    children, and set comdat_symbol_id accordingly.  */
6125
6126 static void
6127 compute_section_prefix (dw_die_ref unit_die)
6128 {
6129   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6130   const char *base = die_name ? lbasename (die_name) : "anonymous";
6131   char *name = alloca (strlen (base) + 64);
6132   char *p;
6133   int i, mark;
6134   unsigned char checksum[16];
6135   struct md5_ctx ctx;
6136
6137   /* Compute the checksum of the DIE, then append part of it as hex digits to
6138      the name filename of the unit.  */
6139
6140   md5_init_ctx (&ctx);
6141   mark = 0;
6142   die_checksum (unit_die, &ctx, &mark);
6143   unmark_all_dies (unit_die);
6144   md5_finish_ctx (&ctx, checksum);
6145
6146   sprintf (name, "%s.", base);
6147   clean_symbol_name (name);
6148
6149   p = name + strlen (name);
6150   for (i = 0; i < 4; i++)
6151     {
6152       sprintf (p, "%.2x", checksum[i]);
6153       p += 2;
6154     }
6155
6156   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6157   comdat_symbol_number = 0;
6158 }
6159
6160 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6161
6162 static int
6163 is_type_die (dw_die_ref die)
6164 {
6165   switch (die->die_tag)
6166     {
6167     case DW_TAG_array_type:
6168     case DW_TAG_class_type:
6169     case DW_TAG_enumeration_type:
6170     case DW_TAG_pointer_type:
6171     case DW_TAG_reference_type:
6172     case DW_TAG_string_type:
6173     case DW_TAG_structure_type:
6174     case DW_TAG_subroutine_type:
6175     case DW_TAG_union_type:
6176     case DW_TAG_ptr_to_member_type:
6177     case DW_TAG_set_type:
6178     case DW_TAG_subrange_type:
6179     case DW_TAG_base_type:
6180     case DW_TAG_const_type:
6181     case DW_TAG_file_type:
6182     case DW_TAG_packed_type:
6183     case DW_TAG_volatile_type:
6184     case DW_TAG_typedef:
6185       return 1;
6186     default:
6187       return 0;
6188     }
6189 }
6190
6191 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6192    Basically, we want to choose the bits that are likely to be shared between
6193    compilations (types) and leave out the bits that are specific to individual
6194    compilations (functions).  */
6195
6196 static int
6197 is_comdat_die (dw_die_ref c)
6198 {
6199   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6200      we do for stabs.  The advantage is a greater likelihood of sharing between
6201      objects that don't include headers in the same order (and therefore would
6202      put the base types in a different comdat).  jason 8/28/00 */
6203
6204   if (c->die_tag == DW_TAG_base_type)
6205     return 0;
6206
6207   if (c->die_tag == DW_TAG_pointer_type
6208       || c->die_tag == DW_TAG_reference_type
6209       || c->die_tag == DW_TAG_const_type
6210       || c->die_tag == DW_TAG_volatile_type)
6211     {
6212       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6213
6214       return t ? is_comdat_die (t) : 0;
6215     }
6216
6217   return is_type_die (c);
6218 }
6219
6220 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6221    compilation unit.  */
6222
6223 static int
6224 is_symbol_die (dw_die_ref c)
6225 {
6226   return (is_type_die (c)
6227           || (get_AT (c, DW_AT_declaration)
6228               && !get_AT (c, DW_AT_specification)));
6229 }
6230
6231 static char *
6232 gen_internal_sym (const char *prefix)
6233 {
6234   char buf[256];
6235
6236   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6237   return xstrdup (buf);
6238 }
6239
6240 /* Assign symbols to all worthy DIEs under DIE.  */
6241
6242 static void
6243 assign_symbol_names (dw_die_ref die)
6244 {
6245   dw_die_ref c;
6246
6247   if (is_symbol_die (die))
6248     {
6249       if (comdat_symbol_id)
6250         {
6251           char *p = alloca (strlen (comdat_symbol_id) + 64);
6252
6253           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6254                    comdat_symbol_id, comdat_symbol_number++);
6255           die->die_symbol = xstrdup (p);
6256         }
6257       else
6258         die->die_symbol = gen_internal_sym ("LDIE");
6259     }
6260
6261   for (c = die->die_child; c != NULL; c = c->die_sib)
6262     assign_symbol_names (c);
6263 }
6264
6265 struct cu_hash_table_entry
6266 {
6267   dw_die_ref cu;
6268   unsigned min_comdat_num, max_comdat_num;
6269   struct cu_hash_table_entry *next;
6270 };
6271
6272 /* Routines to manipulate hash table of CUs.  */
6273 static hashval_t
6274 htab_cu_hash (const void *of)
6275 {
6276   const struct cu_hash_table_entry *entry = of;
6277
6278   return htab_hash_string (entry->cu->die_symbol);
6279 }
6280
6281 static int
6282 htab_cu_eq (const void *of1, const void *of2)
6283 {
6284   const struct cu_hash_table_entry *entry1 = of1;
6285   const struct die_struct *entry2 = of2;
6286
6287   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6288 }
6289
6290 static void
6291 htab_cu_del (void *what)
6292 {
6293   struct cu_hash_table_entry *next, *entry = what;
6294
6295   while (entry)
6296     {
6297       next = entry->next;
6298       free (entry);
6299       entry = next;
6300     }
6301 }
6302
6303 /* Check whether we have already seen this CU and set up SYM_NUM
6304    accordingly.  */
6305 static int
6306 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6307 {
6308   struct cu_hash_table_entry dummy;
6309   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6310
6311   dummy.max_comdat_num = 0;
6312
6313   slot = (struct cu_hash_table_entry **)
6314     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6315         INSERT);
6316   entry = *slot;
6317
6318   for (; entry; last = entry, entry = entry->next)
6319     {
6320       if (same_die_p_wrap (cu, entry->cu))
6321         break;
6322     }
6323
6324   if (entry)
6325     {
6326       *sym_num = entry->min_comdat_num;
6327       return 1;
6328     }
6329
6330   entry = XCNEW (struct cu_hash_table_entry);
6331   entry->cu = cu;
6332   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6333   entry->next = *slot;
6334   *slot = entry;
6335
6336   return 0;
6337 }
6338
6339 /* Record SYM_NUM to record of CU in HTABLE.  */
6340 static void
6341 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6342 {
6343   struct cu_hash_table_entry **slot, *entry;
6344
6345   slot = (struct cu_hash_table_entry **)
6346     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6347         NO_INSERT);
6348   entry = *slot;
6349
6350   entry->max_comdat_num = sym_num;
6351 }
6352
6353 /* Traverse the DIE (which is always comp_unit_die), and set up
6354    additional compilation units for each of the include files we see
6355    bracketed by BINCL/EINCL.  */
6356
6357 static void
6358 break_out_includes (dw_die_ref die)
6359 {
6360   dw_die_ref *ptr;
6361   dw_die_ref unit = NULL;
6362   limbo_die_node *node, **pnode;
6363   htab_t cu_hash_table;
6364
6365   for (ptr = &(die->die_child); *ptr;)
6366     {
6367       dw_die_ref c = *ptr;
6368
6369       if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6370           || (unit && is_comdat_die (c)))
6371         {
6372           /* This DIE is for a secondary CU; remove it from the main one.  */
6373           *ptr = c->die_sib;
6374
6375           if (c->die_tag == DW_TAG_GNU_BINCL)
6376             {
6377               unit = push_new_compile_unit (unit, c);
6378               free_die (c);
6379             }
6380           else if (c->die_tag == DW_TAG_GNU_EINCL)
6381             {
6382               unit = pop_compile_unit (unit);
6383               free_die (c);
6384             }
6385           else
6386             add_child_die (unit, c);
6387         }
6388       else
6389         {
6390           /* Leave this DIE in the main CU.  */
6391           ptr = &(c->die_sib);
6392           continue;
6393         }
6394     }
6395
6396 #if 0
6397   /* We can only use this in debugging, since the frontend doesn't check
6398      to make sure that we leave every include file we enter.  */
6399   gcc_assert (!unit);
6400 #endif
6401
6402   assign_symbol_names (die);
6403   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6404   for (node = limbo_die_list, pnode = &limbo_die_list;
6405        node;
6406        node = node->next)
6407     {
6408       int is_dupl;
6409
6410       compute_section_prefix (node->die);
6411       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6412                         &comdat_symbol_number);
6413       assign_symbol_names (node->die);
6414       if (is_dupl)
6415         *pnode = node->next;
6416       else
6417         {
6418           pnode = &node->next;
6419           record_comdat_symbol_number (node->die, cu_hash_table,
6420                 comdat_symbol_number);
6421         }
6422     }
6423   htab_delete (cu_hash_table);
6424 }
6425
6426 /* Traverse the DIE and add a sibling attribute if it may have the
6427    effect of speeding up access to siblings.  To save some space,
6428    avoid generating sibling attributes for DIE's without children.  */
6429
6430 static void
6431 add_sibling_attributes (dw_die_ref die)
6432 {
6433   dw_die_ref c;
6434
6435   if (die->die_tag != DW_TAG_compile_unit
6436       && die->die_sib && die->die_child != NULL)
6437     /* Add the sibling link to the front of the attribute list.  */
6438     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6439
6440   for (c = die->die_child; c != NULL; c = c->die_sib)
6441     add_sibling_attributes (c);
6442 }
6443
6444 /* Output all location lists for the DIE and its children.  */
6445
6446 static void
6447 output_location_lists (dw_die_ref die)
6448 {
6449   dw_die_ref c;
6450   dw_attr_ref d_attr;
6451
6452   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6453     if (AT_class (d_attr) == dw_val_class_loc_list)
6454       output_loc_list (AT_loc_list (d_attr));
6455
6456   for (c = die->die_child; c != NULL; c = c->die_sib)
6457     output_location_lists (c);
6458
6459 }
6460
6461 /* The format of each DIE (and its attribute value pairs) is encoded in an
6462    abbreviation table.  This routine builds the abbreviation table and assigns
6463    a unique abbreviation id for each abbreviation entry.  The children of each
6464    die are visited recursively.  */
6465
6466 static void
6467 build_abbrev_table (dw_die_ref die)
6468 {
6469   unsigned long abbrev_id;
6470   unsigned int n_alloc;
6471   dw_die_ref c;
6472   dw_attr_ref d_attr, a_attr;
6473
6474   /* Scan the DIE references, and mark as external any that refer to
6475      DIEs from other CUs (i.e. those which are not marked).  */
6476   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6477     if (AT_class (d_attr) == dw_val_class_die_ref
6478         && AT_ref (d_attr)->die_mark == 0)
6479       {
6480         gcc_assert (AT_ref (d_attr)->die_symbol);
6481
6482         set_AT_ref_external (d_attr, 1);
6483       }
6484
6485   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6486     {
6487       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6488
6489       if (abbrev->die_tag == die->die_tag)
6490         {
6491           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6492             {
6493               a_attr = abbrev->die_attr;
6494               d_attr = die->die_attr;
6495
6496               while (a_attr != NULL && d_attr != NULL)
6497                 {
6498                   if ((a_attr->dw_attr != d_attr->dw_attr)
6499                       || (value_format (a_attr) != value_format (d_attr)))
6500                     break;
6501
6502                   a_attr = a_attr->dw_attr_next;
6503                   d_attr = d_attr->dw_attr_next;
6504                 }
6505
6506               if (a_attr == NULL && d_attr == NULL)
6507                 break;
6508             }
6509         }
6510     }
6511
6512   if (abbrev_id >= abbrev_die_table_in_use)
6513     {
6514       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6515         {
6516           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6517           abbrev_die_table = ggc_realloc (abbrev_die_table,
6518                                           sizeof (dw_die_ref) * n_alloc);
6519
6520           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6521                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6522           abbrev_die_table_allocated = n_alloc;
6523         }
6524
6525       ++abbrev_die_table_in_use;
6526       abbrev_die_table[abbrev_id] = die;
6527     }
6528
6529   die->die_abbrev = abbrev_id;
6530   for (c = die->die_child; c != NULL; c = c->die_sib)
6531     build_abbrev_table (c);
6532 }
6533 \f
6534 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6535
6536 static int
6537 constant_size (long unsigned int value)
6538 {
6539   int log;
6540
6541   if (value == 0)
6542     log = 0;
6543   else
6544     log = floor_log2 (value);
6545
6546   log = log / 8;
6547   log = 1 << (floor_log2 (log) + 1);
6548
6549   return log;
6550 }
6551
6552 /* Return the size of a DIE as it is represented in the
6553    .debug_info section.  */
6554
6555 static unsigned long
6556 size_of_die (dw_die_ref die)
6557 {
6558   unsigned long size = 0;
6559   dw_attr_ref a;
6560
6561   size += size_of_uleb128 (die->die_abbrev);
6562   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6563     {
6564       switch (AT_class (a))
6565         {
6566         case dw_val_class_addr:
6567           size += DWARF2_ADDR_SIZE;
6568           break;
6569         case dw_val_class_offset:
6570           size += DWARF_OFFSET_SIZE;
6571           break;
6572         case dw_val_class_loc:
6573           {
6574             unsigned long lsize = size_of_locs (AT_loc (a));
6575
6576             /* Block length.  */
6577             size += constant_size (lsize);
6578             size += lsize;
6579           }
6580           break;
6581         case dw_val_class_loc_list:
6582           size += DWARF_OFFSET_SIZE;
6583           break;
6584         case dw_val_class_range_list:
6585           size += DWARF_OFFSET_SIZE;
6586           break;
6587         case dw_val_class_const:
6588           size += size_of_sleb128 (AT_int (a));
6589           break;
6590         case dw_val_class_unsigned_const:
6591           size += constant_size (AT_unsigned (a));
6592           break;
6593         case dw_val_class_long_long:
6594           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6595           break;
6596         case dw_val_class_vec:
6597           size += 1 + (a->dw_attr_val.v.val_vec.length
6598                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6599           break;
6600         case dw_val_class_flag:
6601           size += 1;
6602           break;
6603         case dw_val_class_die_ref:
6604           if (AT_ref_external (a))
6605             size += DWARF2_ADDR_SIZE;
6606           else
6607             size += DWARF_OFFSET_SIZE;
6608           break;
6609         case dw_val_class_fde_ref:
6610           size += DWARF_OFFSET_SIZE;
6611           break;
6612         case dw_val_class_lbl_id:
6613           size += DWARF2_ADDR_SIZE;
6614           break;
6615         case dw_val_class_lbl_offset:
6616           size += DWARF_OFFSET_SIZE;
6617           break;
6618         case dw_val_class_str:
6619           if (AT_string_form (a) == DW_FORM_strp)
6620             size += DWARF_OFFSET_SIZE;
6621           else
6622             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6623           break;
6624         default:
6625           gcc_unreachable ();
6626         }
6627     }
6628
6629   return size;
6630 }
6631
6632 /* Size the debugging information associated with a given DIE.  Visits the
6633    DIE's children recursively.  Updates the global variable next_die_offset, on
6634    each time through.  Uses the current value of next_die_offset to update the
6635    die_offset field in each DIE.  */
6636
6637 static void
6638 calc_die_sizes (dw_die_ref die)
6639 {
6640   dw_die_ref c;
6641
6642   die->die_offset = next_die_offset;
6643   next_die_offset += size_of_die (die);
6644
6645   for (c = die->die_child; c != NULL; c = c->die_sib)
6646     calc_die_sizes (c);
6647
6648   if (die->die_child != NULL)
6649     /* Count the null byte used to terminate sibling lists.  */
6650     next_die_offset += 1;
6651 }
6652
6653 /* Set the marks for a die and its children.  We do this so
6654    that we know whether or not a reference needs to use FORM_ref_addr; only
6655    DIEs in the same CU will be marked.  We used to clear out the offset
6656    and use that as the flag, but ran into ordering problems.  */
6657
6658 static void
6659 mark_dies (dw_die_ref die)
6660 {
6661   dw_die_ref c;
6662
6663   gcc_assert (!die->die_mark);
6664
6665   die->die_mark = 1;
6666   for (c = die->die_child; c; c = c->die_sib)
6667     mark_dies (c);
6668 }
6669
6670 /* Clear the marks for a die and its children.  */
6671
6672 static void
6673 unmark_dies (dw_die_ref die)
6674 {
6675   dw_die_ref c;
6676
6677   gcc_assert (die->die_mark);
6678
6679   die->die_mark = 0;
6680   for (c = die->die_child; c; c = c->die_sib)
6681     unmark_dies (c);
6682 }
6683
6684 /* Clear the marks for a die, its children and referred dies.  */
6685
6686 static void
6687 unmark_all_dies (dw_die_ref die)
6688 {
6689   dw_die_ref c;
6690   dw_attr_ref a;
6691
6692   if (!die->die_mark)
6693     return;
6694   die->die_mark = 0;
6695
6696   for (c = die->die_child; c; c = c->die_sib)
6697     unmark_all_dies (c);
6698
6699   for (a = die->die_attr; a; a = a->dw_attr_next)
6700     if (AT_class (a) == dw_val_class_die_ref)
6701       unmark_all_dies (AT_ref (a));
6702 }
6703
6704 /* Return the size of the .debug_pubnames table  generated for the
6705    compilation unit.  */
6706
6707 static unsigned long
6708 size_of_pubnames (void)
6709 {
6710   unsigned long size;
6711   unsigned i;
6712
6713   size = DWARF_PUBNAMES_HEADER_SIZE;
6714   for (i = 0; i < pubname_table_in_use; i++)
6715     {
6716       pubname_ref p = &pubname_table[i];
6717       size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6718     }
6719
6720   size += DWARF_OFFSET_SIZE;
6721   return size;
6722 }
6723
6724 /* Return the size of the information in the .debug_aranges section.  */
6725
6726 static unsigned long
6727 size_of_aranges (void)
6728 {
6729   unsigned long size;
6730
6731   size = DWARF_ARANGES_HEADER_SIZE;
6732
6733   /* Count the address/length pair for this compilation unit.  */
6734   size += 2 * DWARF2_ADDR_SIZE;
6735   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6736
6737   /* Count the two zero words used to terminated the address range table.  */
6738   size += 2 * DWARF2_ADDR_SIZE;
6739   return size;
6740 }
6741 \f
6742 /* Select the encoding of an attribute value.  */
6743
6744 static enum dwarf_form
6745 value_format (dw_attr_ref a)
6746 {
6747   switch (a->dw_attr_val.val_class)
6748     {
6749     case dw_val_class_addr:
6750       return DW_FORM_addr;
6751     case dw_val_class_range_list:
6752     case dw_val_class_offset:
6753       switch (DWARF_OFFSET_SIZE)
6754         {
6755         case 4:
6756           return DW_FORM_data4;
6757         case 8:
6758           return DW_FORM_data8;
6759         default:
6760           gcc_unreachable ();
6761         }
6762     case dw_val_class_loc_list:
6763       /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6764          .debug_loc section */
6765       return DW_FORM_data4;
6766     case dw_val_class_loc:
6767       switch (constant_size (size_of_locs (AT_loc (a))))
6768         {
6769         case 1:
6770           return DW_FORM_block1;
6771         case 2:
6772           return DW_FORM_block2;
6773         default:
6774           gcc_unreachable ();
6775         }
6776     case dw_val_class_const:
6777       return DW_FORM_sdata;
6778     case dw_val_class_unsigned_const:
6779       switch (constant_size (AT_unsigned (a)))
6780         {
6781         case 1:
6782           return DW_FORM_data1;
6783         case 2:
6784           return DW_FORM_data2;
6785         case 4:
6786           return DW_FORM_data4;
6787         case 8:
6788           return DW_FORM_data8;
6789         default:
6790           gcc_unreachable ();
6791         }
6792     case dw_val_class_long_long:
6793       return DW_FORM_block1;
6794     case dw_val_class_vec:
6795       return DW_FORM_block1;
6796     case dw_val_class_flag:
6797       return DW_FORM_flag;
6798     case dw_val_class_die_ref:
6799       if (AT_ref_external (a))
6800         return DW_FORM_ref_addr;
6801       else
6802         return DW_FORM_ref;
6803     case dw_val_class_fde_ref:
6804       return DW_FORM_data;
6805     case dw_val_class_lbl_id:
6806       return DW_FORM_addr;
6807     case dw_val_class_lbl_offset:
6808       return DW_FORM_data;
6809     case dw_val_class_str:
6810       return AT_string_form (a);
6811
6812     default:
6813       gcc_unreachable ();
6814     }
6815 }
6816
6817 /* Output the encoding of an attribute value.  */
6818
6819 static void
6820 output_value_format (dw_attr_ref a)
6821 {
6822   enum dwarf_form form = value_format (a);
6823
6824   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6825 }
6826
6827 /* Output the .debug_abbrev section which defines the DIE abbreviation
6828    table.  */
6829
6830 static void
6831 output_abbrev_section (void)
6832 {
6833   unsigned long abbrev_id;
6834
6835   dw_attr_ref a_attr;
6836
6837   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6838     {
6839       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6840
6841       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6842       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6843                                    dwarf_tag_name (abbrev->die_tag));
6844
6845       if (abbrev->die_child != NULL)
6846         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6847       else
6848         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6849
6850       for (a_attr = abbrev->die_attr; a_attr != NULL;
6851            a_attr = a_attr->dw_attr_next)
6852         {
6853           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6854                                        dwarf_attr_name (a_attr->dw_attr));
6855           output_value_format (a_attr);
6856         }
6857
6858       dw2_asm_output_data (1, 0, NULL);
6859       dw2_asm_output_data (1, 0, NULL);
6860     }
6861
6862   /* Terminate the table.  */
6863   dw2_asm_output_data (1, 0, NULL);
6864 }
6865
6866 /* Output a symbol we can use to refer to this DIE from another CU.  */
6867
6868 static inline void
6869 output_die_symbol (dw_die_ref die)
6870 {
6871   char *sym = die->die_symbol;
6872
6873   if (sym == 0)
6874     return;
6875
6876   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6877     /* We make these global, not weak; if the target doesn't support
6878        .linkonce, it doesn't support combining the sections, so debugging
6879        will break.  */
6880     targetm.asm_out.globalize_label (asm_out_file, sym);
6881
6882   ASM_OUTPUT_LABEL (asm_out_file, sym);
6883 }
6884
6885 /* Return a new location list, given the begin and end range, and the
6886    expression. gensym tells us whether to generate a new internal symbol for
6887    this location list node, which is done for the head of the list only.  */
6888
6889 static inline dw_loc_list_ref
6890 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6891               const char *section, unsigned int gensym)
6892 {
6893   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6894
6895   retlist->begin = begin;
6896   retlist->end = end;
6897   retlist->expr = expr;
6898   retlist->section = section;
6899   if (gensym)
6900     retlist->ll_symbol = gen_internal_sym ("LLST");
6901
6902   return retlist;
6903 }
6904
6905 /* Add a location description expression to a location list.  */
6906
6907 static inline void
6908 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6909                            const char *begin, const char *end,
6910                            const char *section)
6911 {
6912   dw_loc_list_ref *d;
6913
6914   /* Find the end of the chain.  */
6915   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6916     ;
6917
6918   /* Add a new location list node to the list.  */
6919   *d = new_loc_list (descr, begin, end, section, 0);
6920 }
6921
6922 static void
6923 dwarf2out_switch_text_section (void)
6924 {
6925   dw_fde_ref fde;
6926
6927   gcc_assert (cfun);
6928
6929   fde = &fde_table[fde_table_in_use - 1];
6930   fde->dw_fde_switched_sections = true;
6931   fde->dw_fde_hot_section_label = cfun->hot_section_label;
6932   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6933   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6934   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6935   have_multiple_function_sections = true;
6936 }
6937
6938 /* Output the location list given to us.  */
6939
6940 static void
6941 output_loc_list (dw_loc_list_ref list_head)
6942 {
6943   dw_loc_list_ref curr = list_head;
6944
6945   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6946
6947   /* Walk the location list, and output each range + expression.  */
6948   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6949     {
6950       unsigned long size;
6951       if (!have_multiple_function_sections)
6952         {
6953           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6954                                 "Location list begin address (%s)",
6955                                 list_head->ll_symbol);
6956           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6957                                 "Location list end address (%s)",
6958                                 list_head->ll_symbol);
6959         }
6960       else
6961         {
6962           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6963                                "Location list begin address (%s)",
6964                                list_head->ll_symbol);
6965           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6966                                "Location list end address (%s)",
6967                                list_head->ll_symbol);
6968         }
6969       size = size_of_locs (curr->expr);
6970
6971       /* Output the block length for this list of location operations.  */
6972       gcc_assert (size <= 0xffff);
6973       dw2_asm_output_data (2, size, "%s", "Location expression size");
6974
6975       output_loc_sequence (curr->expr);
6976     }
6977
6978   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6979                        "Location list terminator begin (%s)",
6980                        list_head->ll_symbol);
6981   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
6982                        "Location list terminator end (%s)",
6983                        list_head->ll_symbol);
6984 }
6985
6986 /* Output the DIE and its attributes.  Called recursively to generate
6987    the definitions of each child DIE.  */
6988
6989 static void
6990 output_die (dw_die_ref die)
6991 {
6992   dw_attr_ref a;
6993   dw_die_ref c;
6994   unsigned long size;
6995
6996   /* If someone in another CU might refer to us, set up a symbol for
6997      them to point to.  */
6998   if (die->die_symbol)
6999     output_die_symbol (die);
7000
7001   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7002                                die->die_offset, dwarf_tag_name (die->die_tag));
7003
7004   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7005     {
7006       const char *name = dwarf_attr_name (a->dw_attr);
7007
7008       switch (AT_class (a))
7009         {
7010         case dw_val_class_addr:
7011           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7012           break;
7013
7014         case dw_val_class_offset:
7015           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7016                                "%s", name);
7017           break;
7018
7019         case dw_val_class_range_list:
7020           {
7021             char *p = strchr (ranges_section_label, '\0');
7022
7023             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7024                      a->dw_attr_val.v.val_offset);
7025             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7026                                    "%s", name);
7027             *p = '\0';
7028           }
7029           break;
7030
7031         case dw_val_class_loc:
7032           size = size_of_locs (AT_loc (a));
7033
7034           /* Output the block length for this list of location operations.  */
7035           dw2_asm_output_data (constant_size (size), size, "%s", name);
7036
7037           output_loc_sequence (AT_loc (a));
7038           break;
7039
7040         case dw_val_class_const:
7041           /* ??? It would be slightly more efficient to use a scheme like is
7042              used for unsigned constants below, but gdb 4.x does not sign
7043              extend.  Gdb 5.x does sign extend.  */
7044           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7045           break;
7046
7047         case dw_val_class_unsigned_const:
7048           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7049                                AT_unsigned (a), "%s", name);
7050           break;
7051
7052         case dw_val_class_long_long:
7053           {
7054             unsigned HOST_WIDE_INT first, second;
7055
7056             dw2_asm_output_data (1,
7057                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7058                                  "%s", name);
7059
7060             if (WORDS_BIG_ENDIAN)
7061               {
7062                 first = a->dw_attr_val.v.val_long_long.hi;
7063                 second = a->dw_attr_val.v.val_long_long.low;
7064               }
7065             else
7066               {
7067                 first = a->dw_attr_val.v.val_long_long.low;
7068                 second = a->dw_attr_val.v.val_long_long.hi;
7069               }
7070
7071             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7072                                  first, "long long constant");
7073             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7074                                  second, NULL);
7075           }
7076           break;
7077
7078         case dw_val_class_vec:
7079           {
7080             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7081             unsigned int len = a->dw_attr_val.v.val_vec.length;
7082             unsigned int i;
7083             unsigned char *p;
7084
7085             dw2_asm_output_data (1, len * elt_size, "%s", name);
7086             if (elt_size > sizeof (HOST_WIDE_INT))
7087               {
7088                 elt_size /= 2;
7089                 len *= 2;
7090               }
7091             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7092                  i < len;
7093                  i++, p += elt_size)
7094               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7095                                    "fp or vector constant word %u", i);
7096             break;
7097           }
7098
7099         case dw_val_class_flag:
7100           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7101           break;
7102
7103         case dw_val_class_loc_list:
7104           {
7105             char *sym = AT_loc_list (a)->ll_symbol;
7106
7107             gcc_assert (sym);
7108             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, "%s", name);
7109           }
7110           break;
7111
7112         case dw_val_class_die_ref:
7113           if (AT_ref_external (a))
7114             {
7115               char *sym = AT_ref (a)->die_symbol;
7116
7117               gcc_assert (sym);
7118               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, "%s", name);
7119             }
7120           else
7121             {
7122               gcc_assert (AT_ref (a)->die_offset);
7123               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7124                                    "%s", name);
7125             }
7126           break;
7127
7128         case dw_val_class_fde_ref:
7129           {
7130             char l1[20];
7131
7132             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7133                                          a->dw_attr_val.v.val_fde_index * 2);
7134             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, "%s", name);
7135           }
7136           break;
7137
7138         case dw_val_class_lbl_id:
7139           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7140           break;
7141
7142         case dw_val_class_lbl_offset:
7143           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a), "%s", name);
7144           break;
7145
7146         case dw_val_class_str:
7147           if (AT_string_form (a) == DW_FORM_strp)
7148             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7149                                    a->dw_attr_val.v.val_str->label,
7150                                    "%s: \"%s\"", name, AT_string (a));
7151           else
7152             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7153           break;
7154
7155         default:
7156           gcc_unreachable ();
7157         }
7158     }
7159
7160   for (c = die->die_child; c != NULL; c = c->die_sib)
7161     output_die (c);
7162
7163   /* Add null byte to terminate sibling list.  */
7164   if (die->die_child != NULL)
7165     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7166                          die->die_offset);
7167 }
7168
7169 /* Output the compilation unit that appears at the beginning of the
7170    .debug_info section, and precedes the DIE descriptions.  */
7171
7172 static void
7173 output_compilation_unit_header (void)
7174 {
7175   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7176     dw2_asm_output_data (4, 0xffffffff,
7177       "Initial length escape value indicating 64-bit DWARF extension");
7178   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7179                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7180                        "Length of Compilation Unit Info");
7181   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7182   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7183                          "Offset Into Abbrev. Section");
7184   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7185 }
7186
7187 /* Output the compilation unit DIE and its children.  */
7188
7189 static void
7190 output_comp_unit (dw_die_ref die, int output_if_empty)
7191 {
7192   const char *secname;
7193   char *oldsym, *tmp;
7194
7195   /* Unless we are outputting main CU, we may throw away empty ones.  */
7196   if (!output_if_empty && die->die_child == NULL)
7197     return;
7198
7199   /* Even if there are no children of this DIE, we must output the information
7200      about the compilation unit.  Otherwise, on an empty translation unit, we
7201      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7202      will then complain when examining the file.  First mark all the DIEs in
7203      this CU so we know which get local refs.  */
7204   mark_dies (die);
7205
7206   build_abbrev_table (die);
7207
7208   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7209   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7210   calc_die_sizes (die);
7211
7212   oldsym = die->die_symbol;
7213   if (oldsym)
7214     {
7215       tmp = alloca (strlen (oldsym) + 24);
7216
7217       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7218       secname = tmp;
7219       die->die_symbol = NULL;
7220       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7221     }
7222   else
7223     switch_to_section (debug_info_section);
7224
7225   /* Output debugging information.  */
7226   output_compilation_unit_header ();
7227   output_die (die);
7228
7229   /* Leave the marks on the main CU, so we can check them in
7230      output_pubnames.  */
7231   if (oldsym)
7232     {
7233       unmark_dies (die);
7234       die->die_symbol = oldsym;
7235     }
7236 }
7237
7238 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The
7239    output of lang_hooks.decl_printable_name for C++ looks like
7240    "A::f(int)".  Let's drop the argument list, and maybe the scope.  */
7241
7242 static const char *
7243 dwarf2_name (tree decl, int scope)
7244 {
7245   return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7246 }
7247
7248 /* Add a new entry to .debug_pubnames if appropriate.  */
7249
7250 static void
7251 add_pubname (tree decl, dw_die_ref die)
7252 {
7253   pubname_ref p;
7254
7255   if (! TREE_PUBLIC (decl))
7256     return;
7257
7258   if (pubname_table_in_use == pubname_table_allocated)
7259     {
7260       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7261       pubname_table
7262         = ggc_realloc (pubname_table,
7263                        (pubname_table_allocated * sizeof (pubname_entry)));
7264       memset (pubname_table + pubname_table_in_use, 0,
7265               PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7266     }
7267
7268   p = &pubname_table[pubname_table_in_use++];
7269   p->die = die;
7270   p->name = xstrdup (dwarf2_name (decl, 1));
7271 }
7272
7273 /* Output the public names table used to speed up access to externally
7274    visible names.  For now, only generate entries for externally
7275    visible procedures.  */
7276
7277 static void
7278 output_pubnames (void)
7279 {
7280   unsigned i;
7281   unsigned long pubnames_length = size_of_pubnames ();
7282
7283   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7284     dw2_asm_output_data (4, 0xffffffff,
7285       "Initial length escape value indicating 64-bit DWARF extension");
7286   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7287                        "Length of Public Names Info");
7288   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7289   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7290                          "Offset of Compilation Unit Info");
7291   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7292                        "Compilation Unit Length");
7293
7294   for (i = 0; i < pubname_table_in_use; i++)
7295     {
7296       pubname_ref pub = &pubname_table[i];
7297
7298       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7299       gcc_assert (pub->die->die_mark);
7300
7301       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7302                            "DIE offset");
7303
7304       dw2_asm_output_nstring (pub->name, -1, "external name");
7305     }
7306
7307   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7308 }
7309
7310 /* Add a new entry to .debug_aranges if appropriate.  */
7311
7312 static void
7313 add_arange (tree decl, dw_die_ref die)
7314 {
7315   if (! DECL_SECTION_NAME (decl))
7316     return;
7317
7318   if (arange_table_in_use == arange_table_allocated)
7319     {
7320       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7321       arange_table = ggc_realloc (arange_table,
7322                                   (arange_table_allocated
7323                                    * sizeof (dw_die_ref)));
7324       memset (arange_table + arange_table_in_use, 0,
7325               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7326     }
7327
7328   arange_table[arange_table_in_use++] = die;
7329 }
7330
7331 /* Output the information that goes into the .debug_aranges table.
7332    Namely, define the beginning and ending address range of the
7333    text section generated for this compilation unit.  */
7334
7335 static void
7336 output_aranges (void)
7337 {
7338   unsigned i;
7339   unsigned long aranges_length = size_of_aranges ();
7340
7341   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7342     dw2_asm_output_data (4, 0xffffffff,
7343       "Initial length escape value indicating 64-bit DWARF extension");
7344   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7345                        "Length of Address Ranges Info");
7346   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7347   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7348                          "Offset of Compilation Unit Info");
7349   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7350   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7351
7352   /* We need to align to twice the pointer size here.  */
7353   if (DWARF_ARANGES_PAD_SIZE)
7354     {
7355       /* Pad using a 2 byte words so that padding is correct for any
7356          pointer size.  */
7357       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7358                            2 * DWARF2_ADDR_SIZE);
7359       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7360         dw2_asm_output_data (2, 0, NULL);
7361     }
7362
7363   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7364   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7365                         text_section_label, "Length");
7366   if (flag_reorder_blocks_and_partition)
7367     {
7368       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label, 
7369                            "Address");
7370       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7371                             cold_text_section_label, "Length");
7372     }
7373
7374   for (i = 0; i < arange_table_in_use; i++)
7375     {
7376       dw_die_ref die = arange_table[i];
7377
7378       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7379       gcc_assert (die->die_mark);
7380
7381       if (die->die_tag == DW_TAG_subprogram)
7382         {
7383           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7384                                "Address");
7385           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7386                                 get_AT_low_pc (die), "Length");
7387         }
7388       else
7389         {
7390           /* A static variable; extract the symbol from DW_AT_location.
7391              Note that this code isn't currently hit, as we only emit
7392              aranges for functions (jason 9/23/99).  */
7393           dw_attr_ref a = get_AT (die, DW_AT_location);
7394           dw_loc_descr_ref loc;
7395
7396           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7397
7398           loc = AT_loc (a);
7399           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7400
7401           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7402                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7403           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7404                                get_AT_unsigned (die, DW_AT_byte_size),
7405                                "Length");
7406         }
7407     }
7408
7409   /* Output the terminator words.  */
7410   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7411   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7412 }
7413
7414 /* Add a new entry to .debug_ranges.  Return the offset at which it
7415    was placed.  */
7416
7417 static unsigned int
7418 add_ranges (tree block)
7419 {
7420   unsigned int in_use = ranges_table_in_use;
7421
7422   if (in_use == ranges_table_allocated)
7423     {
7424       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7425       ranges_table
7426         = ggc_realloc (ranges_table, (ranges_table_allocated
7427                                       * sizeof (struct dw_ranges_struct)));
7428       memset (ranges_table + ranges_table_in_use, 0,
7429               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7430     }
7431
7432   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7433   ranges_table_in_use = in_use + 1;
7434
7435   return in_use * 2 * DWARF2_ADDR_SIZE;
7436 }
7437
7438 static void
7439 output_ranges (void)
7440 {
7441   unsigned i;
7442   static const char *const start_fmt = "Offset 0x%x";
7443   const char *fmt = start_fmt;
7444
7445   for (i = 0; i < ranges_table_in_use; i++)
7446     {
7447       int block_num = ranges_table[i].block_num;
7448
7449       if (block_num)
7450         {
7451           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7452           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7453
7454           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7455           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7456
7457           /* If all code is in the text section, then the compilation
7458              unit base address defaults to DW_AT_low_pc, which is the
7459              base of the text section.  */
7460           if (!have_multiple_function_sections)
7461             {
7462               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7463                                     text_section_label,
7464                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7465               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7466                                     text_section_label, NULL);
7467             }
7468
7469           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7470              compilation unit base address to zero, which allows us to
7471              use absolute addresses, and not worry about whether the
7472              target supports cross-section arithmetic.  */
7473           else
7474             {
7475               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7476                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7477               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7478             }
7479
7480           fmt = NULL;
7481         }
7482       else
7483         {
7484           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7485           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7486           fmt = start_fmt;
7487         }
7488     }
7489 }
7490
7491 /* Data structure containing information about input files.  */
7492 struct file_info
7493 {
7494   char *path;           /* Complete file name.  */
7495   char *fname;          /* File name part.  */
7496   int length;           /* Length of entire string.  */
7497   int file_idx;         /* Index in input file table.  */
7498   int dir_idx;          /* Index in directory table.  */
7499 };
7500
7501 /* Data structure containing information about directories with source
7502    files.  */
7503 struct dir_info
7504 {
7505   char *path;           /* Path including directory name.  */
7506   int length;           /* Path length.  */
7507   int prefix;           /* Index of directory entry which is a prefix.  */
7508   int count;            /* Number of files in this directory.  */
7509   int dir_idx;          /* Index of directory used as base.  */
7510   int used;             /* Used in the end?  */
7511 };
7512
7513 /* Callback function for file_info comparison.  We sort by looking at
7514    the directories in the path.  */
7515
7516 static int
7517 file_info_cmp (const void *p1, const void *p2)
7518 {
7519   const struct file_info *s1 = p1;
7520   const struct file_info *s2 = p2;
7521   unsigned char *cp1;
7522   unsigned char *cp2;
7523
7524   /* Take care of file names without directories.  We need to make sure that
7525      we return consistent values to qsort since some will get confused if
7526      we return the same value when identical operands are passed in opposite
7527      orders.  So if neither has a directory, return 0 and otherwise return
7528      1 or -1 depending on which one has the directory.  */
7529   if ((s1->path == s1->fname || s2->path == s2->fname))
7530     return (s2->path == s2->fname) - (s1->path == s1->fname);
7531
7532   cp1 = (unsigned char *) s1->path;
7533   cp2 = (unsigned char *) s2->path;
7534
7535   while (1)
7536     {
7537       ++cp1;
7538       ++cp2;
7539       /* Reached the end of the first path?  If so, handle like above.  */
7540       if ((cp1 == (unsigned char *) s1->fname)
7541           || (cp2 == (unsigned char *) s2->fname))
7542         return ((cp2 == (unsigned char *) s2->fname)
7543                 - (cp1 == (unsigned char *) s1->fname));
7544
7545       /* Character of current path component the same?  */
7546       else if (*cp1 != *cp2)
7547         return *cp1 - *cp2;
7548     }
7549 }
7550
7551 /* Output the directory table and the file name table.  We try to minimize
7552    the total amount of memory needed.  A heuristic is used to avoid large
7553    slowdowns with many input files.  */
7554
7555 static void
7556 output_file_names (void)
7557 {
7558   struct file_info *files;
7559   struct dir_info *dirs;
7560   int *saved;
7561   int *savehere;
7562   int *backmap;
7563   size_t ndirs;
7564   int idx_offset;
7565   size_t i;
7566   int idx;
7567
7568   /* Handle the case where file_table is empty.  */
7569   if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7570     {
7571       dw2_asm_output_data (1, 0, "End directory table");
7572       dw2_asm_output_data (1, 0, "End file name table");
7573       return;
7574     }
7575
7576   /* Allocate the various arrays we need.  */
7577   files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7578   dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7579
7580   /* Sort the file names.  */
7581   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7582     {
7583       char *f;
7584
7585       /* Skip all leading "./".  */
7586       f = VARRAY_CHAR_PTR (file_table, i);
7587       while (f[0] == '.' && f[1] == '/')
7588         f += 2;
7589
7590       /* Create a new array entry.  */
7591       files[i].path = f;
7592       files[i].length = strlen (f);
7593       files[i].file_idx = i;
7594
7595       /* Search for the file name part.  */
7596       f = strrchr (f, '/');
7597       files[i].fname = f == NULL ? files[i].path : f + 1;
7598     }
7599
7600   qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7601          sizeof (files[0]), file_info_cmp);
7602
7603   /* Find all the different directories used.  */
7604   dirs[0].path = files[1].path;
7605   dirs[0].length = files[1].fname - files[1].path;
7606   dirs[0].prefix = -1;
7607   dirs[0].count = 1;
7608   dirs[0].dir_idx = 0;
7609   dirs[0].used = 0;
7610   files[1].dir_idx = 0;
7611   ndirs = 1;
7612
7613   for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7614     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7615         && memcmp (dirs[ndirs - 1].path, files[i].path,
7616                    dirs[ndirs - 1].length) == 0)
7617       {
7618         /* Same directory as last entry.  */
7619         files[i].dir_idx = ndirs - 1;
7620         ++dirs[ndirs - 1].count;
7621       }
7622     else
7623       {
7624         size_t j;
7625
7626         /* This is a new directory.  */
7627         dirs[ndirs].path = files[i].path;
7628         dirs[ndirs].length = files[i].fname - files[i].path;
7629         dirs[ndirs].count = 1;
7630         dirs[ndirs].dir_idx = ndirs;
7631         dirs[ndirs].used = 0;
7632         files[i].dir_idx = ndirs;
7633
7634         /* Search for a prefix.  */
7635         dirs[ndirs].prefix = -1;
7636         for (j = 0; j < ndirs; j++)
7637           if (dirs[j].length < dirs[ndirs].length
7638               && dirs[j].length > 1
7639               && (dirs[ndirs].prefix == -1
7640                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7641               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7642             dirs[ndirs].prefix = j;
7643
7644         ++ndirs;
7645       }
7646
7647   /* Now to the actual work.  We have to find a subset of the directories which
7648      allow expressing the file name using references to the directory table
7649      with the least amount of characters.  We do not do an exhaustive search
7650      where we would have to check out every combination of every single
7651      possible prefix.  Instead we use a heuristic which provides nearly optimal
7652      results in most cases and never is much off.  */
7653   saved = alloca (ndirs * sizeof (int));
7654   savehere = alloca (ndirs * sizeof (int));
7655
7656   memset (saved, '\0', ndirs * sizeof (saved[0]));
7657   for (i = 0; i < ndirs; i++)
7658     {
7659       size_t j;
7660       int total;
7661
7662       /* We can always save some space for the current directory.  But this
7663          does not mean it will be enough to justify adding the directory.  */
7664       savehere[i] = dirs[i].length;
7665       total = (savehere[i] - saved[i]) * dirs[i].count;
7666
7667       for (j = i + 1; j < ndirs; j++)
7668         {
7669           savehere[j] = 0;
7670           if (saved[j] < dirs[i].length)
7671             {
7672               /* Determine whether the dirs[i] path is a prefix of the
7673                  dirs[j] path.  */
7674               int k;
7675
7676               k = dirs[j].prefix;
7677               while (k != -1 && k != (int) i)
7678                 k = dirs[k].prefix;
7679
7680               if (k == (int) i)
7681                 {
7682                   /* Yes it is.  We can possibly safe some memory but
7683                      writing the filenames in dirs[j] relative to
7684                      dirs[i].  */
7685                   savehere[j] = dirs[i].length;
7686                   total += (savehere[j] - saved[j]) * dirs[j].count;
7687                 }
7688             }
7689         }
7690
7691       /* Check whether we can safe enough to justify adding the dirs[i]
7692          directory.  */
7693       if (total > dirs[i].length + 1)
7694         {
7695           /* It's worthwhile adding.  */
7696           for (j = i; j < ndirs; j++)
7697             if (savehere[j] > 0)
7698               {
7699                 /* Remember how much we saved for this directory so far.  */
7700                 saved[j] = savehere[j];
7701
7702                 /* Remember the prefix directory.  */
7703                 dirs[j].dir_idx = i;
7704               }
7705         }
7706     }
7707
7708   /* We have to emit them in the order they appear in the file_table array
7709      since the index is used in the debug info generation.  To do this
7710      efficiently we generate a back-mapping of the indices first.  */
7711   backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7712   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7713     {
7714       backmap[files[i].file_idx] = i;
7715
7716       /* Mark this directory as used.  */
7717       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7718     }
7719
7720   /* That was it.  We are ready to emit the information.  First emit the
7721      directory name table.  We have to make sure the first actually emitted
7722      directory name has index one; zero is reserved for the current working
7723      directory.  Make sure we do not confuse these indices with the one for the
7724      constructed table (even though most of the time they are identical).  */
7725   idx = 1;
7726   idx_offset = dirs[0].length > 0 ? 1 : 0;
7727   for (i = 1 - idx_offset; i < ndirs; i++)
7728     if (dirs[i].used != 0)
7729       {
7730         dirs[i].used = idx++;
7731         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7732                                 "Directory Entry: 0x%x", dirs[i].used);
7733       }
7734
7735   dw2_asm_output_data (1, 0, "End directory table");
7736
7737   /* Correct the index for the current working directory entry if it
7738      exists.  */
7739   if (idx_offset == 0)
7740     dirs[0].used = 0;
7741
7742   /* Now write all the file names.  */
7743   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7744     {
7745       int file_idx = backmap[i];
7746       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7747
7748       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7749                               "File Entry: 0x%lx", (unsigned long) i);
7750
7751       /* Include directory index.  */
7752       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7753
7754       /* Modification time.  */
7755       dw2_asm_output_data_uleb128 (0, NULL);
7756
7757       /* File length in bytes.  */
7758       dw2_asm_output_data_uleb128 (0, NULL);
7759     }
7760
7761   dw2_asm_output_data (1, 0, "End file name table");
7762 }
7763
7764
7765 /* Output the source line number correspondence information.  This
7766    information goes into the .debug_line section.  */
7767
7768 static void
7769 output_line_info (void)
7770 {
7771   char l1[20], l2[20], p1[20], p2[20];
7772   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7773   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7774   unsigned opc;
7775   unsigned n_op_args;
7776   unsigned long lt_index;
7777   unsigned long current_line;
7778   long line_offset;
7779   long line_delta;
7780   unsigned long current_file;
7781   unsigned long function;
7782
7783   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7784   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7785   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7786   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7787
7788   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7789     dw2_asm_output_data (4, 0xffffffff,
7790       "Initial length escape value indicating 64-bit DWARF extension");
7791   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7792                         "Length of Source Line Info");
7793   ASM_OUTPUT_LABEL (asm_out_file, l1);
7794
7795   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7796   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7797   ASM_OUTPUT_LABEL (asm_out_file, p1);
7798
7799   /* Define the architecture-dependent minimum instruction length (in
7800    bytes).  In this implementation of DWARF, this field is used for
7801    information purposes only.  Since GCC generates assembly language,
7802    we have no a priori knowledge of how many instruction bytes are
7803    generated for each source line, and therefore can use only the
7804    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7805    commands.  Accordingly, we fix this as `1', which is "correct
7806    enough" for all architectures, and don't let the target override.  */
7807   dw2_asm_output_data (1, 1,
7808                        "Minimum Instruction Length");
7809
7810   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7811                        "Default is_stmt_start flag");
7812   dw2_asm_output_data (1, DWARF_LINE_BASE,
7813                        "Line Base Value (Special Opcodes)");
7814   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7815                        "Line Range Value (Special Opcodes)");
7816   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7817                        "Special Opcode Base");
7818
7819   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7820     {
7821       switch (opc)
7822         {
7823         case DW_LNS_advance_pc:
7824         case DW_LNS_advance_line:
7825         case DW_LNS_set_file:
7826         case DW_LNS_set_column:
7827         case DW_LNS_fixed_advance_pc:
7828           n_op_args = 1;
7829           break;
7830         default:
7831           n_op_args = 0;
7832           break;
7833         }
7834
7835       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7836                            opc, n_op_args);
7837     }
7838
7839   /* Write out the information about the files we use.  */
7840   output_file_names ();
7841   ASM_OUTPUT_LABEL (asm_out_file, p2);
7842
7843   /* We used to set the address register to the first location in the text
7844      section here, but that didn't accomplish anything since we already
7845      have a line note for the opening brace of the first function.  */
7846
7847   /* Generate the line number to PC correspondence table, encoded as
7848      a series of state machine operations.  */
7849   current_file = 1;
7850   current_line = 1;
7851
7852   if (cfun && in_cold_section_p)
7853     strcpy (prev_line_label, cfun->cold_section_label);
7854   else
7855     strcpy (prev_line_label, text_section_label);
7856   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7857     {
7858       dw_line_info_ref line_info = &line_info_table[lt_index];
7859
7860 #if 0
7861       /* Disable this optimization for now; GDB wants to see two line notes
7862          at the beginning of a function so it can find the end of the
7863          prologue.  */
7864
7865       /* Don't emit anything for redundant notes.  Just updating the
7866          address doesn't accomplish anything, because we already assume
7867          that anything after the last address is this line.  */
7868       if (line_info->dw_line_num == current_line
7869           && line_info->dw_file_num == current_file)
7870         continue;
7871 #endif
7872
7873       /* Emit debug info for the address of the current line.
7874
7875          Unfortunately, we have little choice here currently, and must always
7876          use the most general form.  GCC does not know the address delta
7877          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7878          attributes which will give an upper bound on the address range.  We
7879          could perhaps use length attributes to determine when it is safe to
7880          use DW_LNS_fixed_advance_pc.  */
7881
7882       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7883       if (0)
7884         {
7885           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7886           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7887                                "DW_LNS_fixed_advance_pc");
7888           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7889         }
7890       else
7891         {
7892           /* This can handle any delta.  This takes
7893              4+DWARF2_ADDR_SIZE bytes.  */
7894           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7895           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7896           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7897           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7898         }
7899
7900       strcpy (prev_line_label, line_label);
7901
7902       /* Emit debug info for the source file of the current line, if
7903          different from the previous line.  */
7904       if (line_info->dw_file_num != current_file)
7905         {
7906           current_file = line_info->dw_file_num;
7907           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7908           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7909                                        VARRAY_CHAR_PTR (file_table,
7910                                                         current_file));
7911         }
7912
7913       /* Emit debug info for the current line number, choosing the encoding
7914          that uses the least amount of space.  */
7915       if (line_info->dw_line_num != current_line)
7916         {
7917           line_offset = line_info->dw_line_num - current_line;
7918           line_delta = line_offset - DWARF_LINE_BASE;
7919           current_line = line_info->dw_line_num;
7920           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7921             /* This can handle deltas from -10 to 234, using the current
7922                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7923                takes 1 byte.  */
7924             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7925                                  "line %lu", current_line);
7926           else
7927             {
7928               /* This can handle any delta.  This takes at least 4 bytes,
7929                  depending on the value being encoded.  */
7930               dw2_asm_output_data (1, DW_LNS_advance_line,
7931                                    "advance to line %lu", current_line);
7932               dw2_asm_output_data_sleb128 (line_offset, NULL);
7933               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7934             }
7935         }
7936       else
7937         /* We still need to start a new row, so output a copy insn.  */
7938         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7939     }
7940
7941   /* Emit debug info for the address of the end of the function.  */
7942   if (0)
7943     {
7944       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7945                            "DW_LNS_fixed_advance_pc");
7946       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7947     }
7948   else
7949     {
7950       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7951       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7952       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7953       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7954     }
7955
7956   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7957   dw2_asm_output_data_uleb128 (1, NULL);
7958   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7959
7960   function = 0;
7961   current_file = 1;
7962   current_line = 1;
7963   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
7964     {
7965       dw_separate_line_info_ref line_info
7966         = &separate_line_info_table[lt_index];
7967
7968 #if 0
7969       /* Don't emit anything for redundant notes.  */
7970       if (line_info->dw_line_num == current_line
7971           && line_info->dw_file_num == current_file
7972           && line_info->function == function)
7973         goto cont;
7974 #endif
7975
7976       /* Emit debug info for the address of the current line.  If this is
7977          a new function, or the first line of a function, then we need
7978          to handle it differently.  */
7979       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
7980                                    lt_index);
7981       if (function != line_info->function)
7982         {
7983           function = line_info->function;
7984
7985           /* Set the address register to the first line in the function.  */
7986           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7987           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7988           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7989           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7990         }
7991       else
7992         {
7993           /* ??? See the DW_LNS_advance_pc comment above.  */
7994           if (0)
7995             {
7996               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7997                                    "DW_LNS_fixed_advance_pc");
7998               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7999             }
8000           else
8001             {
8002               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8003               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8004               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8005               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8006             }
8007         }
8008
8009       strcpy (prev_line_label, line_label);
8010
8011       /* Emit debug info for the source file of the current line, if
8012          different from the previous line.  */
8013       if (line_info->dw_file_num != current_file)
8014         {
8015           current_file = line_info->dw_file_num;
8016           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8017           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8018                                        VARRAY_CHAR_PTR (file_table,
8019                                                         current_file));
8020         }
8021
8022       /* Emit debug info for the current line number, choosing the encoding
8023          that uses the least amount of space.  */
8024       if (line_info->dw_line_num != current_line)
8025         {
8026           line_offset = line_info->dw_line_num - current_line;
8027           line_delta = line_offset - DWARF_LINE_BASE;
8028           current_line = line_info->dw_line_num;
8029           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8030             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8031                                  "line %lu", current_line);
8032           else
8033             {
8034               dw2_asm_output_data (1, DW_LNS_advance_line,
8035                                    "advance to line %lu", current_line);
8036               dw2_asm_output_data_sleb128 (line_offset, NULL);
8037               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8038             }
8039         }
8040       else
8041         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8042
8043 #if 0
8044     cont:
8045 #endif
8046
8047       lt_index++;
8048
8049       /* If we're done with a function, end its sequence.  */
8050       if (lt_index == separate_line_info_table_in_use
8051           || separate_line_info_table[lt_index].function != function)
8052         {
8053           current_file = 1;
8054           current_line = 1;
8055
8056           /* Emit debug info for the address of the end of the function.  */
8057           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8058           if (0)
8059             {
8060               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8061                                    "DW_LNS_fixed_advance_pc");
8062               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8063             }
8064           else
8065             {
8066               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8067               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8068               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8069               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8070             }
8071
8072           /* Output the marker for the end of this sequence.  */
8073           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8074           dw2_asm_output_data_uleb128 (1, NULL);
8075           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8076         }
8077     }
8078
8079   /* Output the marker for the end of the line number info.  */
8080   ASM_OUTPUT_LABEL (asm_out_file, l2);
8081 }
8082 \f
8083 /* Given a pointer to a tree node for some base type, return a pointer to
8084    a DIE that describes the given type.
8085
8086    This routine must only be called for GCC type nodes that correspond to
8087    Dwarf base (fundamental) types.  */
8088
8089 static dw_die_ref
8090 base_type_die (tree type)
8091 {
8092   dw_die_ref base_type_result;
8093   enum dwarf_type encoding;
8094
8095   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8096     return 0;
8097
8098   switch (TREE_CODE (type))
8099     {
8100     case INTEGER_TYPE:
8101       if (TYPE_STRING_FLAG (type))
8102         {
8103           if (TYPE_UNSIGNED (type))
8104             encoding = DW_ATE_unsigned_char;
8105           else
8106             encoding = DW_ATE_signed_char;
8107         }
8108       else if (TYPE_UNSIGNED (type))
8109         encoding = DW_ATE_unsigned;
8110       else
8111         encoding = DW_ATE_signed;
8112       break;
8113
8114     case REAL_TYPE:
8115       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8116         encoding = DW_ATE_decimal_float;
8117       else
8118         encoding = DW_ATE_float;
8119       break;
8120
8121       /* Dwarf2 doesn't know anything about complex ints, so use
8122          a user defined type for it.  */
8123     case COMPLEX_TYPE:
8124       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8125         encoding = DW_ATE_complex_float;
8126       else
8127         encoding = DW_ATE_lo_user;
8128       break;
8129
8130     case BOOLEAN_TYPE:
8131       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8132       encoding = DW_ATE_boolean;
8133       break;
8134
8135     default:
8136       /* No other TREE_CODEs are Dwarf fundamental types.  */
8137       gcc_unreachable ();
8138     }
8139
8140   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8141
8142   /* This probably indicates a bug.  */
8143   if (! TYPE_NAME (type))
8144     add_name_attribute (base_type_result, "__unknown__");
8145
8146   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8147                    int_size_in_bytes (type));
8148   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8149
8150   return base_type_result;
8151 }
8152
8153 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8154    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
8155    a given type is generally the same as the given type, except that if the
8156    given type is a pointer or reference type, then the root type of the given
8157    type is the root type of the "basis" type for the pointer or reference
8158    type.  (This definition of the "root" type is recursive.) Also, the root
8159    type of a `const' qualified type or a `volatile' qualified type is the
8160    root type of the given type without the qualifiers.  */
8161
8162 static tree
8163 root_type (tree type)
8164 {
8165   if (TREE_CODE (type) == ERROR_MARK)
8166     return error_mark_node;
8167
8168   switch (TREE_CODE (type))
8169     {
8170     case ERROR_MARK:
8171       return error_mark_node;
8172
8173     case POINTER_TYPE:
8174     case REFERENCE_TYPE:
8175       return type_main_variant (root_type (TREE_TYPE (type)));
8176
8177     default:
8178       return type_main_variant (type);
8179     }
8180 }
8181
8182 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8183    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8184
8185 static inline int
8186 is_base_type (tree type)
8187 {
8188   switch (TREE_CODE (type))
8189     {
8190     case ERROR_MARK:
8191     case VOID_TYPE:
8192     case INTEGER_TYPE:
8193     case REAL_TYPE:
8194     case COMPLEX_TYPE:
8195     case BOOLEAN_TYPE:
8196       return 1;
8197
8198     case ARRAY_TYPE:
8199     case RECORD_TYPE:
8200     case UNION_TYPE:
8201     case QUAL_UNION_TYPE:
8202     case ENUMERAL_TYPE:
8203     case FUNCTION_TYPE:
8204     case METHOD_TYPE:
8205     case POINTER_TYPE:
8206     case REFERENCE_TYPE:
8207     case OFFSET_TYPE:
8208     case LANG_TYPE:
8209     case VECTOR_TYPE:
8210       return 0;
8211
8212     default:
8213       gcc_unreachable ();
8214     }
8215
8216   return 0;
8217 }
8218
8219 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8220    node, return the size in bits for the type if it is a constant, or else
8221    return the alignment for the type if the type's size is not constant, or
8222    else return BITS_PER_WORD if the type actually turns out to be an
8223    ERROR_MARK node.  */
8224
8225 static inline unsigned HOST_WIDE_INT
8226 simple_type_size_in_bits (tree type)
8227 {
8228   if (TREE_CODE (type) == ERROR_MARK)
8229     return BITS_PER_WORD;
8230   else if (TYPE_SIZE (type) == NULL_TREE)
8231     return 0;
8232   else if (host_integerp (TYPE_SIZE (type), 1))
8233     return tree_low_cst (TYPE_SIZE (type), 1);
8234   else
8235     return TYPE_ALIGN (type);
8236 }
8237
8238 /* Return true if the debug information for the given type should be
8239    emitted as a subrange type.  */
8240
8241 static inline bool
8242 is_subrange_type (tree type)
8243 {
8244   tree subtype = TREE_TYPE (type);
8245
8246   /* Subrange types are identified by the fact that they are integer
8247      types, and that they have a subtype which is either an integer type
8248      or an enumeral type.  */
8249
8250   if (TREE_CODE (type) != INTEGER_TYPE
8251       || subtype == NULL_TREE)
8252     return false;
8253
8254   if (TREE_CODE (subtype) != INTEGER_TYPE
8255       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8256     return false;
8257
8258   if (TREE_CODE (type) == TREE_CODE (subtype)
8259       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8260       && TYPE_MIN_VALUE (type) != NULL
8261       && TYPE_MIN_VALUE (subtype) != NULL
8262       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8263       && TYPE_MAX_VALUE (type) != NULL
8264       && TYPE_MAX_VALUE (subtype) != NULL
8265       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8266     {
8267       /* The type and its subtype have the same representation.  If in
8268          addition the two types also have the same name, then the given
8269          type is not a subrange type, but rather a plain base type.  */
8270       /* FIXME: brobecker/2004-03-22:
8271          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8272          therefore be sufficient to check the TYPE_SIZE node pointers
8273          rather than checking the actual size.  Unfortunately, we have
8274          found some cases, such as in the Ada "integer" type, where
8275          this is not the case.  Until this problem is solved, we need to
8276          keep checking the actual size.  */
8277       tree type_name = TYPE_NAME (type);
8278       tree subtype_name = TYPE_NAME (subtype);
8279
8280       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8281         type_name = DECL_NAME (type_name);
8282
8283       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8284         subtype_name = DECL_NAME (subtype_name);
8285
8286       if (type_name == subtype_name)
8287         return false;
8288     }
8289
8290   return true;
8291 }
8292
8293 /*  Given a pointer to a tree node for a subrange type, return a pointer
8294     to a DIE that describes the given type.  */
8295
8296 static dw_die_ref
8297 subrange_type_die (tree type, dw_die_ref context_die)
8298 {
8299   dw_die_ref subrange_die;
8300   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8301
8302   if (context_die == NULL)
8303     context_die = comp_unit_die;
8304
8305   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8306
8307   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8308     {
8309       /* The size of the subrange type and its base type do not match,
8310          so we need to generate a size attribute for the subrange type.  */
8311       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8312     }
8313
8314   if (TYPE_MIN_VALUE (type) != NULL)
8315     add_bound_info (subrange_die, DW_AT_lower_bound,
8316                     TYPE_MIN_VALUE (type));
8317   if (TYPE_MAX_VALUE (type) != NULL)
8318     add_bound_info (subrange_die, DW_AT_upper_bound,
8319                     TYPE_MAX_VALUE (type));
8320
8321   return subrange_die;
8322 }
8323
8324 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8325    entry that chains various modifiers in front of the given type.  */
8326
8327 static dw_die_ref
8328 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8329                    dw_die_ref context_die)
8330 {
8331   enum tree_code code = TREE_CODE (type);
8332   dw_die_ref mod_type_die;
8333   dw_die_ref sub_die = NULL;
8334   tree item_type = NULL;
8335   tree qualified_type;
8336   tree name;
8337
8338   if (code == ERROR_MARK)
8339     return NULL;
8340
8341   /* See if we already have the appropriately qualified variant of
8342      this type.  */
8343   qualified_type
8344     = get_qualified_type (type,
8345                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8346                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8347   
8348   /* If we do, then we can just use its DIE, if it exists.  */
8349   if (qualified_type)
8350     {
8351       mod_type_die = lookup_type_die (qualified_type);
8352       if (mod_type_die)
8353         return mod_type_die;
8354     }
8355   
8356   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8357   
8358   /* Handle C typedef types.  */
8359   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8360     {
8361       tree dtype = TREE_TYPE (name);
8362       
8363       if (qualified_type == dtype)
8364         {
8365           /* For a named type, use the typedef.  */
8366           gen_type_die (qualified_type, context_die);
8367           return lookup_type_die (qualified_type);
8368         }
8369       else if (DECL_ORIGINAL_TYPE (name)
8370                && (is_const_type < TYPE_READONLY (dtype)
8371                    || is_volatile_type < TYPE_VOLATILE (dtype)))
8372         /* cv-unqualified version of named type.  Just use the unnamed
8373            type to which it refers.  */
8374         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8375                                   is_const_type, is_volatile_type,
8376                                   context_die);
8377       /* Else cv-qualified version of named type; fall through.  */
8378     }
8379   
8380   if (is_const_type)
8381     {
8382       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8383       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8384     }
8385   else if (is_volatile_type)
8386     {
8387       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8388       sub_die = modified_type_die (type, 0, 0, context_die);
8389     }
8390   else if (code == POINTER_TYPE)
8391     {
8392       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8393       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8394                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8395       item_type = TREE_TYPE (type);
8396     }
8397   else if (code == REFERENCE_TYPE)
8398     {
8399       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8400       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8401                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8402       item_type = TREE_TYPE (type);
8403     }
8404   else if (is_subrange_type (type))
8405     {
8406       mod_type_die = subrange_type_die (type, context_die);
8407       item_type = TREE_TYPE (type);
8408     }
8409   else if (is_base_type (type))
8410     mod_type_die = base_type_die (type);
8411   else
8412     {
8413       gen_type_die (type, context_die);
8414       
8415       /* We have to get the type_main_variant here (and pass that to the
8416          `lookup_type_die' routine) because the ..._TYPE node we have
8417          might simply be a *copy* of some original type node (where the
8418          copy was created to help us keep track of typedef names) and
8419          that copy might have a different TYPE_UID from the original
8420          ..._TYPE node.  */
8421       if (TREE_CODE (type) != VECTOR_TYPE)
8422         return lookup_type_die (type_main_variant (type));
8423       else
8424         /* Vectors have the debugging information in the type,
8425            not the main variant.  */
8426         return lookup_type_die (type);
8427     }
8428   
8429   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8430      don't output a DW_TAG_typedef, since there isn't one in the
8431      user's program; just attach a DW_AT_name to the type.  */
8432   if (name
8433       && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8434     {
8435       if (TREE_CODE (name) == TYPE_DECL)
8436         /* Could just call add_name_and_src_coords_attributes here,
8437            but since this is a builtin type it doesn't have any
8438            useful source coordinates anyway.  */
8439         name = DECL_NAME (name);
8440       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8441     }
8442   
8443   if (qualified_type)
8444     equate_type_number_to_die (qualified_type, mod_type_die);
8445
8446   if (item_type)
8447     /* We must do this after the equate_type_number_to_die call, in case
8448        this is a recursive type.  This ensures that the modified_type_die
8449        recursion will terminate even if the type is recursive.  Recursive
8450        types are possible in Ada.  */
8451     sub_die = modified_type_die (item_type,
8452                                  TYPE_READONLY (item_type),
8453                                  TYPE_VOLATILE (item_type),
8454                                  context_die);
8455
8456   if (sub_die != NULL)
8457     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8458
8459   return mod_type_die;
8460 }
8461
8462 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8463    an enumerated type.  */
8464
8465 static inline int
8466 type_is_enum (tree type)
8467 {
8468   return TREE_CODE (type) == ENUMERAL_TYPE;
8469 }
8470
8471 /* Return the DBX register number described by a given RTL node.  */
8472
8473 static unsigned int
8474 dbx_reg_number (rtx rtl)
8475 {
8476   unsigned regno = REGNO (rtl);
8477
8478   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8479
8480 #ifdef LEAF_REG_REMAP
8481   regno = LEAF_REG_REMAP (regno);
8482 #endif
8483
8484   return DBX_REGISTER_NUMBER (regno);
8485 }
8486
8487 /* Optionally add a DW_OP_piece term to a location description expression.
8488    DW_OP_piece is only added if the location description expression already
8489    doesn't end with DW_OP_piece.  */
8490
8491 static void
8492 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8493 {
8494   dw_loc_descr_ref loc;
8495
8496   if (*list_head != NULL)
8497     {
8498       /* Find the end of the chain.  */
8499       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8500         ;
8501
8502       if (loc->dw_loc_opc != DW_OP_piece)
8503         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8504     }
8505 }
8506
8507 /* Return a location descriptor that designates a machine register or
8508    zero if there is none.  */
8509
8510 static dw_loc_descr_ref
8511 reg_loc_descriptor (rtx rtl)
8512 {
8513   rtx regs;
8514
8515   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8516     return 0;
8517
8518   regs = targetm.dwarf_register_span (rtl);
8519
8520   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8521     return multiple_reg_loc_descriptor (rtl, regs);
8522   else
8523     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8524 }
8525
8526 /* Return a location descriptor that designates a machine register for
8527    a given hard register number.  */
8528
8529 static dw_loc_descr_ref
8530 one_reg_loc_descriptor (unsigned int regno)
8531 {
8532   if (regno <= 31)
8533     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8534   else
8535     return new_loc_descr (DW_OP_regx, regno, 0);
8536 }
8537
8538 /* Given an RTL of a register, return a location descriptor that
8539    designates a value that spans more than one register.  */
8540
8541 static dw_loc_descr_ref
8542 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8543 {
8544   int nregs, size, i;
8545   unsigned reg;
8546   dw_loc_descr_ref loc_result = NULL;
8547
8548   reg = REGNO (rtl);
8549 #ifdef LEAF_REG_REMAP
8550   reg = LEAF_REG_REMAP (reg);
8551 #endif
8552   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8553   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8554
8555   /* Simple, contiguous registers.  */
8556   if (regs == NULL_RTX)
8557     {
8558       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8559
8560       loc_result = NULL;
8561       while (nregs--)
8562         {
8563           dw_loc_descr_ref t;
8564
8565           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8566           add_loc_descr (&loc_result, t);
8567           add_loc_descr_op_piece (&loc_result, size);
8568           ++reg;
8569         }
8570       return loc_result;
8571     }
8572
8573   /* Now onto stupid register sets in non contiguous locations.  */
8574
8575   gcc_assert (GET_CODE (regs) == PARALLEL);
8576
8577   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8578   loc_result = NULL;
8579
8580   for (i = 0; i < XVECLEN (regs, 0); ++i)
8581     {
8582       dw_loc_descr_ref t;
8583
8584       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8585       add_loc_descr (&loc_result, t);
8586       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8587       add_loc_descr_op_piece (&loc_result, size);
8588     }
8589   return loc_result;
8590 }
8591
8592 /* Return a location descriptor that designates a constant.  */
8593
8594 static dw_loc_descr_ref
8595 int_loc_descriptor (HOST_WIDE_INT i)
8596 {
8597   enum dwarf_location_atom op;
8598
8599   /* Pick the smallest representation of a constant, rather than just
8600      defaulting to the LEB encoding.  */
8601   if (i >= 0)
8602     {
8603       if (i <= 31)
8604         op = DW_OP_lit0 + i;
8605       else if (i <= 0xff)
8606         op = DW_OP_const1u;
8607       else if (i <= 0xffff)
8608         op = DW_OP_const2u;
8609       else if (HOST_BITS_PER_WIDE_INT == 32
8610                || i <= 0xffffffff)
8611         op = DW_OP_const4u;
8612       else
8613         op = DW_OP_constu;
8614     }
8615   else
8616     {
8617       if (i >= -0x80)
8618         op = DW_OP_const1s;
8619       else if (i >= -0x8000)
8620         op = DW_OP_const2s;
8621       else if (HOST_BITS_PER_WIDE_INT == 32
8622                || i >= -0x80000000)
8623         op = DW_OP_const4s;
8624       else
8625         op = DW_OP_consts;
8626     }
8627
8628   return new_loc_descr (op, i, 0);
8629 }
8630
8631 /* Return a location descriptor that designates a base+offset location.  */
8632
8633 static dw_loc_descr_ref
8634 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8635 {
8636   unsigned int regno;
8637
8638   /* We only use "frame base" when we're sure we're talking about the
8639      post-prologue local stack frame.  We do this by *not* running
8640      register elimination until this point, and recognizing the special
8641      argument pointer and soft frame pointer rtx's.  */
8642   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8643     {
8644       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8645
8646       if (elim != reg)
8647         {
8648           if (GET_CODE (elim) == PLUS)
8649             {
8650               offset += INTVAL (XEXP (elim, 1));
8651               elim = XEXP (elim, 0);
8652             }
8653           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8654                       : stack_pointer_rtx));
8655           offset += frame_pointer_fb_offset;
8656
8657           return new_loc_descr (DW_OP_fbreg, offset, 0);
8658         }
8659     }
8660
8661   regno = dbx_reg_number (reg);
8662   if (regno <= 31)
8663     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8664   else
8665     return new_loc_descr (DW_OP_bregx, regno, offset);
8666 }
8667
8668 /* Return true if this RTL expression describes a base+offset calculation.  */
8669
8670 static inline int
8671 is_based_loc (rtx rtl)
8672 {
8673   return (GET_CODE (rtl) == PLUS
8674           && ((REG_P (XEXP (rtl, 0))
8675                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8676                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8677 }
8678
8679 /* The following routine converts the RTL for a variable or parameter
8680    (resident in memory) into an equivalent Dwarf representation of a
8681    mechanism for getting the address of that same variable onto the top of a
8682    hypothetical "address evaluation" stack.
8683
8684    When creating memory location descriptors, we are effectively transforming
8685    the RTL for a memory-resident object into its Dwarf postfix expression
8686    equivalent.  This routine recursively descends an RTL tree, turning
8687    it into Dwarf postfix code as it goes.
8688
8689    MODE is the mode of the memory reference, needed to handle some
8690    autoincrement addressing modes.
8691
8692    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8693    location list for RTL.
8694
8695    Return 0 if we can't represent the location.  */
8696
8697 static dw_loc_descr_ref
8698 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8699 {
8700   dw_loc_descr_ref mem_loc_result = NULL;
8701   enum dwarf_location_atom op;
8702
8703   /* Note that for a dynamically sized array, the location we will generate a
8704      description of here will be the lowest numbered location which is
8705      actually within the array.  That's *not* necessarily the same as the
8706      zeroth element of the array.  */
8707
8708   rtl = targetm.delegitimize_address (rtl);
8709
8710   switch (GET_CODE (rtl))
8711     {
8712     case POST_INC:
8713     case POST_DEC:
8714     case POST_MODIFY:
8715       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8716          just fall into the SUBREG code.  */
8717
8718       /* ... fall through ...  */
8719
8720     case SUBREG:
8721       /* The case of a subreg may arise when we have a local (register)
8722          variable or a formal (register) parameter which doesn't quite fill
8723          up an entire register.  For now, just assume that it is
8724          legitimate to make the Dwarf info refer to the whole register which
8725          contains the given subreg.  */
8726       rtl = XEXP (rtl, 0);
8727
8728       /* ... fall through ...  */
8729
8730     case REG:
8731       /* Whenever a register number forms a part of the description of the
8732          method for calculating the (dynamic) address of a memory resident
8733          object, DWARF rules require the register number be referred to as
8734          a "base register".  This distinction is not based in any way upon
8735          what category of register the hardware believes the given register
8736          belongs to.  This is strictly DWARF terminology we're dealing with
8737          here. Note that in cases where the location of a memory-resident
8738          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8739          OP_CONST (0)) the actual DWARF location descriptor that we generate
8740          may just be OP_BASEREG (basereg).  This may look deceptively like
8741          the object in question was allocated to a register (rather than in
8742          memory) so DWARF consumers need to be aware of the subtle
8743          distinction between OP_REG and OP_BASEREG.  */
8744       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8745         mem_loc_result = based_loc_descr (rtl, 0);
8746       break;
8747
8748     case MEM:
8749       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8750       if (mem_loc_result != 0)
8751         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8752       break;
8753
8754     case LO_SUM:
8755          rtl = XEXP (rtl, 1);
8756
8757       /* ... fall through ...  */
8758
8759     case LABEL_REF:
8760       /* Some ports can transform a symbol ref into a label ref, because
8761          the symbol ref is too far away and has to be dumped into a constant
8762          pool.  */
8763     case CONST:
8764     case SYMBOL_REF:
8765       /* Alternatively, the symbol in the constant pool might be referenced
8766          by a different symbol.  */
8767       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8768         {
8769           bool marked;
8770           rtx tmp = get_pool_constant_mark (rtl, &marked);
8771
8772           if (GET_CODE (tmp) == SYMBOL_REF)
8773             {
8774               rtl = tmp;
8775               if (CONSTANT_POOL_ADDRESS_P (tmp))
8776                 get_pool_constant_mark (tmp, &marked);
8777               else
8778                 marked = true;
8779             }
8780
8781           /* If all references to this pool constant were optimized away,
8782              it was not output and thus we can't represent it.
8783              FIXME: might try to use DW_OP_const_value here, though
8784              DW_OP_piece complicates it.  */
8785           if (!marked)
8786             return 0;
8787         }
8788
8789       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8790       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8791       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8792       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8793       break;
8794
8795     case PRE_MODIFY:
8796       /* Extract the PLUS expression nested inside and fall into
8797          PLUS code below.  */
8798       rtl = XEXP (rtl, 1);
8799       goto plus;
8800
8801     case PRE_INC:
8802     case PRE_DEC:
8803       /* Turn these into a PLUS expression and fall into the PLUS code
8804          below.  */
8805       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8806                           GEN_INT (GET_CODE (rtl) == PRE_INC
8807                                    ? GET_MODE_UNIT_SIZE (mode)
8808                                    : -GET_MODE_UNIT_SIZE (mode)));
8809
8810       /* ... fall through ...  */
8811
8812     case PLUS:
8813     plus:
8814       if (is_based_loc (rtl))
8815         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8816                                           INTVAL (XEXP (rtl, 1)));
8817       else
8818         {
8819           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8820           if (mem_loc_result == 0)
8821             break;
8822
8823           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8824               && INTVAL (XEXP (rtl, 1)) >= 0)
8825             add_loc_descr (&mem_loc_result,
8826                            new_loc_descr (DW_OP_plus_uconst,
8827                                           INTVAL (XEXP (rtl, 1)), 0));
8828           else
8829             {
8830               add_loc_descr (&mem_loc_result,
8831                              mem_loc_descriptor (XEXP (rtl, 1), mode));
8832               add_loc_descr (&mem_loc_result,
8833                              new_loc_descr (DW_OP_plus, 0, 0));
8834             }
8835         }
8836       break;
8837
8838     /* If a pseudo-reg is optimized away, it is possible for it to
8839        be replaced with a MEM containing a multiply or shift.  */
8840     case MULT:
8841       op = DW_OP_mul;
8842       goto do_binop;
8843
8844     case ASHIFT:
8845       op = DW_OP_shl;
8846       goto do_binop;
8847
8848     case ASHIFTRT:
8849       op = DW_OP_shra;
8850       goto do_binop;
8851
8852     case LSHIFTRT:
8853       op = DW_OP_shr;
8854       goto do_binop;
8855
8856     do_binop:
8857       {
8858         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8859         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8860
8861         if (op0 == 0 || op1 == 0)
8862           break;
8863
8864         mem_loc_result = op0;
8865         add_loc_descr (&mem_loc_result, op1);
8866         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8867         break;
8868       }
8869
8870     case CONST_INT:
8871       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8872       break;
8873
8874     default:
8875       gcc_unreachable ();
8876     }
8877
8878   return mem_loc_result;
8879 }
8880
8881 /* Return a descriptor that describes the concatenation of two locations.
8882    This is typically a complex variable.  */
8883
8884 static dw_loc_descr_ref
8885 concat_loc_descriptor (rtx x0, rtx x1)
8886 {
8887   dw_loc_descr_ref cc_loc_result = NULL;
8888   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8889   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8890
8891   if (x0_ref == 0 || x1_ref == 0)
8892     return 0;
8893
8894   cc_loc_result = x0_ref;
8895   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8896
8897   add_loc_descr (&cc_loc_result, x1_ref);
8898   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8899
8900   return cc_loc_result;
8901 }
8902
8903 /* Output a proper Dwarf location descriptor for a variable or parameter
8904    which is either allocated in a register or in a memory location.  For a
8905    register, we just generate an OP_REG and the register number.  For a
8906    memory location we provide a Dwarf postfix expression describing how to
8907    generate the (dynamic) address of the object onto the address stack.
8908
8909    If we don't know how to describe it, return 0.  */
8910
8911 static dw_loc_descr_ref
8912 loc_descriptor (rtx rtl)
8913 {
8914   dw_loc_descr_ref loc_result = NULL;
8915
8916   switch (GET_CODE (rtl))
8917     {
8918     case SUBREG:
8919       /* The case of a subreg may arise when we have a local (register)
8920          variable or a formal (register) parameter which doesn't quite fill
8921          up an entire register.  For now, just assume that it is
8922          legitimate to make the Dwarf info refer to the whole register which
8923          contains the given subreg.  */
8924       rtl = SUBREG_REG (rtl);
8925
8926       /* ... fall through ...  */
8927
8928     case REG:
8929       loc_result = reg_loc_descriptor (rtl);
8930       break;
8931
8932     case MEM:
8933       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8934       break;
8935
8936     case CONCAT:
8937       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8938       break;
8939
8940     case VAR_LOCATION:
8941       /* Single part.  */
8942       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8943         {
8944           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8945           break;
8946         }
8947
8948       rtl = XEXP (rtl, 1);
8949       /* FALLTHRU */
8950
8951     case PARALLEL:
8952       {
8953         rtvec par_elems = XVEC (rtl, 0);
8954         int num_elem = GET_NUM_ELEM (par_elems);
8955         enum machine_mode mode;
8956         int i;
8957
8958         /* Create the first one, so we have something to add to.  */
8959         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
8960         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
8961         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8962         for (i = 1; i < num_elem; i++)
8963           {
8964             dw_loc_descr_ref temp;
8965
8966             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
8967             add_loc_descr (&loc_result, temp);
8968             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
8969             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
8970           }
8971       }
8972       break;
8973
8974     default:
8975       gcc_unreachable ();
8976     }
8977
8978   return loc_result;
8979 }
8980
8981 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
8982    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
8983    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
8984    top-level invocation, and we require the address of LOC; is 0 if we require
8985    the value of LOC.  */
8986
8987 static dw_loc_descr_ref
8988 loc_descriptor_from_tree_1 (tree loc, int want_address)
8989 {
8990   dw_loc_descr_ref ret, ret1;
8991   int have_address = 0;
8992   enum dwarf_location_atom op;
8993
8994   /* ??? Most of the time we do not take proper care for sign/zero
8995      extending the values properly.  Hopefully this won't be a real
8996      problem...  */
8997
8998   switch (TREE_CODE (loc))
8999     {
9000     case ERROR_MARK:
9001       return 0;
9002
9003     case PLACEHOLDER_EXPR:
9004       /* This case involves extracting fields from an object to determine the
9005          position of other fields.  We don't try to encode this here.  The
9006          only user of this is Ada, which encodes the needed information using
9007          the names of types.  */
9008       return 0;
9009
9010     case CALL_EXPR:
9011       return 0;
9012
9013     case PREINCREMENT_EXPR:
9014     case PREDECREMENT_EXPR:
9015     case POSTINCREMENT_EXPR:
9016     case POSTDECREMENT_EXPR:
9017       /* There are no opcodes for these operations.  */
9018       return 0;
9019
9020     case ADDR_EXPR:
9021       /* If we already want an address, there's nothing we can do.  */
9022       if (want_address)
9023         return 0;
9024
9025       /* Otherwise, process the argument and look for the address.  */
9026       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9027
9028     case VAR_DECL:
9029       if (DECL_THREAD_LOCAL_P (loc))
9030         {
9031           rtx rtl;
9032
9033           /* If this is not defined, we have no way to emit the data.  */
9034           if (!targetm.asm_out.output_dwarf_dtprel)
9035             return 0;
9036
9037           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9038              look up addresses of objects in the current module.  */
9039           if (DECL_EXTERNAL (loc))
9040             return 0;
9041
9042           rtl = rtl_for_decl_location (loc);
9043           if (rtl == NULL_RTX)
9044             return 0;
9045
9046           if (!MEM_P (rtl))
9047             return 0;
9048           rtl = XEXP (rtl, 0);
9049           if (! CONSTANT_P (rtl))
9050             return 0;
9051
9052           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9053           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9054           ret->dw_loc_oprnd1.v.val_addr = rtl;
9055
9056           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9057           add_loc_descr (&ret, ret1);
9058
9059           have_address = 1;
9060           break;
9061         }
9062       /* FALLTHRU */
9063
9064     case PARM_DECL:
9065       if (DECL_HAS_VALUE_EXPR_P (loc))
9066         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9067                                            want_address);
9068       /* FALLTHRU */
9069
9070     case RESULT_DECL:
9071       {
9072         rtx rtl = rtl_for_decl_location (loc);
9073
9074         if (rtl == NULL_RTX)
9075           return 0;
9076         else if (GET_CODE (rtl) == CONST_INT)
9077           {
9078             HOST_WIDE_INT val = INTVAL (rtl);
9079             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9080               val &= GET_MODE_MASK (DECL_MODE (loc));
9081             ret = int_loc_descriptor (val);
9082           }
9083         else if (GET_CODE (rtl) == CONST_STRING)
9084           return 0;
9085         else if (CONSTANT_P (rtl))
9086           {
9087             ret = new_loc_descr (DW_OP_addr, 0, 0);
9088             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9089             ret->dw_loc_oprnd1.v.val_addr = rtl;
9090           }
9091         else
9092           {
9093             enum machine_mode mode;
9094
9095             /* Certain constructs can only be represented at top-level.  */
9096             if (want_address == 2)
9097               return loc_descriptor (rtl);
9098
9099             mode = GET_MODE (rtl);
9100             if (MEM_P (rtl))
9101               {
9102                 rtl = XEXP (rtl, 0);
9103                 have_address = 1;
9104               }
9105             ret = mem_loc_descriptor (rtl, mode);
9106           }
9107       }
9108       break;
9109
9110     case INDIRECT_REF:
9111       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9112       have_address = 1;
9113       break;
9114
9115     case COMPOUND_EXPR:
9116       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9117
9118     case NOP_EXPR:
9119     case CONVERT_EXPR:
9120     case NON_LVALUE_EXPR:
9121     case VIEW_CONVERT_EXPR:
9122     case SAVE_EXPR:
9123     case MODIFY_EXPR:
9124       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9125
9126     case COMPONENT_REF:
9127     case BIT_FIELD_REF:
9128     case ARRAY_REF:
9129     case ARRAY_RANGE_REF:
9130       {
9131         tree obj, offset;
9132         HOST_WIDE_INT bitsize, bitpos, bytepos;
9133         enum machine_mode mode;
9134         int volatilep;
9135         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9136
9137         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9138                                    &unsignedp, &volatilep, false);
9139
9140         if (obj == loc)
9141           return 0;
9142
9143         ret = loc_descriptor_from_tree_1 (obj, 1);
9144         if (ret == 0
9145             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9146           return 0;
9147
9148         if (offset != NULL_TREE)
9149           {
9150             /* Variable offset.  */
9151             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9152             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9153           }
9154
9155         bytepos = bitpos / BITS_PER_UNIT;
9156         if (bytepos > 0)
9157           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9158         else if (bytepos < 0)
9159           {
9160             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9161             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9162           }
9163
9164         have_address = 1;
9165         break;
9166       }
9167
9168     case INTEGER_CST:
9169       if (host_integerp (loc, 0))
9170         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9171       else
9172         return 0;
9173       break;
9174
9175     case CONSTRUCTOR:
9176       {
9177         /* Get an RTL for this, if something has been emitted.  */
9178         rtx rtl = lookup_constant_def (loc);
9179         enum machine_mode mode;
9180
9181         if (!rtl || !MEM_P (rtl))
9182           return 0;
9183         mode = GET_MODE (rtl);
9184         rtl = XEXP (rtl, 0);
9185         ret = mem_loc_descriptor (rtl, mode);
9186         have_address = 1;
9187         break;
9188       }
9189
9190     case TRUTH_AND_EXPR:
9191     case TRUTH_ANDIF_EXPR:
9192     case BIT_AND_EXPR:
9193       op = DW_OP_and;
9194       goto do_binop;
9195
9196     case TRUTH_XOR_EXPR:
9197     case BIT_XOR_EXPR:
9198       op = DW_OP_xor;
9199       goto do_binop;
9200
9201     case TRUTH_OR_EXPR:
9202     case TRUTH_ORIF_EXPR:
9203     case BIT_IOR_EXPR:
9204       op = DW_OP_or;
9205       goto do_binop;
9206
9207     case FLOOR_DIV_EXPR:
9208     case CEIL_DIV_EXPR:
9209     case ROUND_DIV_EXPR:
9210     case TRUNC_DIV_EXPR:
9211       op = DW_OP_div;
9212       goto do_binop;
9213
9214     case MINUS_EXPR:
9215       op = DW_OP_minus;
9216       goto do_binop;
9217
9218     case FLOOR_MOD_EXPR:
9219     case CEIL_MOD_EXPR:
9220     case ROUND_MOD_EXPR:
9221     case TRUNC_MOD_EXPR:
9222       op = DW_OP_mod;
9223       goto do_binop;
9224
9225     case MULT_EXPR:
9226       op = DW_OP_mul;
9227       goto do_binop;
9228
9229     case LSHIFT_EXPR:
9230       op = DW_OP_shl;
9231       goto do_binop;
9232
9233     case RSHIFT_EXPR:
9234       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9235       goto do_binop;
9236
9237     case PLUS_EXPR:
9238       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9239           && host_integerp (TREE_OPERAND (loc, 1), 0))
9240         {
9241           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9242           if (ret == 0)
9243             return 0;
9244
9245           add_loc_descr (&ret,
9246                          new_loc_descr (DW_OP_plus_uconst,
9247                                         tree_low_cst (TREE_OPERAND (loc, 1),
9248                                                       0),
9249                                         0));
9250           break;
9251         }
9252
9253       op = DW_OP_plus;
9254       goto do_binop;
9255
9256     case LE_EXPR:
9257       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9258         return 0;
9259
9260       op = DW_OP_le;
9261       goto do_binop;
9262
9263     case GE_EXPR:
9264       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9265         return 0;
9266
9267       op = DW_OP_ge;
9268       goto do_binop;
9269
9270     case LT_EXPR:
9271       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9272         return 0;
9273
9274       op = DW_OP_lt;
9275       goto do_binop;
9276
9277     case GT_EXPR:
9278       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9279         return 0;
9280
9281       op = DW_OP_gt;
9282       goto do_binop;
9283
9284     case EQ_EXPR:
9285       op = DW_OP_eq;
9286       goto do_binop;
9287
9288     case NE_EXPR:
9289       op = DW_OP_ne;
9290       goto do_binop;
9291
9292     do_binop:
9293       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9294       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9295       if (ret == 0 || ret1 == 0)
9296         return 0;
9297
9298       add_loc_descr (&ret, ret1);
9299       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9300       break;
9301
9302     case TRUTH_NOT_EXPR:
9303     case BIT_NOT_EXPR:
9304       op = DW_OP_not;
9305       goto do_unop;
9306
9307     case ABS_EXPR:
9308       op = DW_OP_abs;
9309       goto do_unop;
9310
9311     case NEGATE_EXPR:
9312       op = DW_OP_neg;
9313       goto do_unop;
9314
9315     do_unop:
9316       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9317       if (ret == 0)
9318         return 0;
9319
9320       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9321       break;
9322
9323     case MIN_EXPR:
9324     case MAX_EXPR:
9325       {
9326         const enum tree_code code =
9327           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9328
9329         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9330                       build2 (code, integer_type_node,
9331                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9332                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9333       }
9334
9335       /* ... fall through ...  */
9336
9337     case COND_EXPR:
9338       {
9339         dw_loc_descr_ref lhs
9340           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9341         dw_loc_descr_ref rhs
9342           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9343         dw_loc_descr_ref bra_node, jump_node, tmp;
9344
9345         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9346         if (ret == 0 || lhs == 0 || rhs == 0)
9347           return 0;
9348
9349         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9350         add_loc_descr (&ret, bra_node);
9351
9352         add_loc_descr (&ret, rhs);
9353         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9354         add_loc_descr (&ret, jump_node);
9355
9356         add_loc_descr (&ret, lhs);
9357         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9358         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9359
9360         /* ??? Need a node to point the skip at.  Use a nop.  */
9361         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9362         add_loc_descr (&ret, tmp);
9363         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9364         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9365       }
9366       break;
9367
9368     case FIX_TRUNC_EXPR:
9369     case FIX_CEIL_EXPR:
9370     case FIX_FLOOR_EXPR:
9371     case FIX_ROUND_EXPR:
9372       return 0;
9373
9374     default:
9375       /* Leave front-end specific codes as simply unknown.  This comes
9376          up, for instance, with the C STMT_EXPR.  */
9377       if ((unsigned int) TREE_CODE (loc)
9378           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9379         return 0;
9380
9381 #ifdef ENABLE_CHECKING
9382       /* Otherwise this is a generic code; we should just lists all of
9383          these explicitly.  We forgot one.  */
9384       gcc_unreachable ();
9385 #else
9386       /* In a release build, we want to degrade gracefully: better to
9387          generate incomplete debugging information than to crash.  */
9388       return NULL;
9389 #endif
9390     }
9391
9392   /* Show if we can't fill the request for an address.  */
9393   if (want_address && !have_address)
9394     return 0;
9395
9396   /* If we've got an address and don't want one, dereference.  */
9397   if (!want_address && have_address && ret)
9398     {
9399       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9400
9401       if (size > DWARF2_ADDR_SIZE || size == -1)
9402         return 0;
9403       else if (size == DWARF2_ADDR_SIZE)
9404         op = DW_OP_deref;
9405       else
9406         op = DW_OP_deref_size;
9407
9408       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9409     }
9410
9411   return ret;
9412 }
9413
9414 static inline dw_loc_descr_ref
9415 loc_descriptor_from_tree (tree loc)
9416 {
9417   return loc_descriptor_from_tree_1 (loc, 2);
9418 }
9419
9420 /* Given a value, round it up to the lowest multiple of `boundary'
9421    which is not less than the value itself.  */
9422
9423 static inline HOST_WIDE_INT
9424 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9425 {
9426   return (((value + boundary - 1) / boundary) * boundary);
9427 }
9428
9429 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9430    pointer to the declared type for the relevant field variable, or return
9431    `integer_type_node' if the given node turns out to be an
9432    ERROR_MARK node.  */
9433
9434 static inline tree
9435 field_type (tree decl)
9436 {
9437   tree type;
9438
9439   if (TREE_CODE (decl) == ERROR_MARK)
9440     return integer_type_node;
9441
9442   type = DECL_BIT_FIELD_TYPE (decl);
9443   if (type == NULL_TREE)
9444     type = TREE_TYPE (decl);
9445
9446   return type;
9447 }
9448
9449 /* Given a pointer to a tree node, return the alignment in bits for
9450    it, or else return BITS_PER_WORD if the node actually turns out to
9451    be an ERROR_MARK node.  */
9452
9453 static inline unsigned
9454 simple_type_align_in_bits (tree type)
9455 {
9456   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9457 }
9458
9459 static inline unsigned
9460 simple_decl_align_in_bits (tree decl)
9461 {
9462   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9463 }
9464
9465 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9466    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9467    or return 0 if we are unable to determine what that offset is, either
9468    because the argument turns out to be a pointer to an ERROR_MARK node, or
9469    because the offset is actually variable.  (We can't handle the latter case
9470    just yet).  */
9471
9472 static HOST_WIDE_INT
9473 field_byte_offset (tree decl)
9474 {
9475   unsigned int type_align_in_bits;
9476   unsigned int decl_align_in_bits;
9477   unsigned HOST_WIDE_INT type_size_in_bits;
9478   HOST_WIDE_INT object_offset_in_bits;
9479   tree type;
9480   tree field_size_tree;
9481   HOST_WIDE_INT bitpos_int;
9482   HOST_WIDE_INT deepest_bitpos;
9483   unsigned HOST_WIDE_INT field_size_in_bits;
9484
9485   if (TREE_CODE (decl) == ERROR_MARK)
9486     return 0;
9487
9488   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9489
9490   type = field_type (decl);
9491   field_size_tree = DECL_SIZE (decl);
9492
9493   /* The size could be unspecified if there was an error, or for
9494      a flexible array member.  */
9495   if (! field_size_tree)
9496     field_size_tree = bitsize_zero_node;
9497
9498   /* We cannot yet cope with fields whose positions are variable, so
9499      for now, when we see such things, we simply return 0.  Someday, we may
9500      be able to handle such cases, but it will be damn difficult.  */
9501   if (! host_integerp (bit_position (decl), 0))
9502     return 0;
9503
9504   bitpos_int = int_bit_position (decl);
9505
9506   /* If we don't know the size of the field, pretend it's a full word.  */
9507   if (host_integerp (field_size_tree, 1))
9508     field_size_in_bits = tree_low_cst (field_size_tree, 1);
9509   else
9510     field_size_in_bits = BITS_PER_WORD;
9511
9512   type_size_in_bits = simple_type_size_in_bits (type);
9513   type_align_in_bits = simple_type_align_in_bits (type);
9514   decl_align_in_bits = simple_decl_align_in_bits (decl);
9515
9516   /* The GCC front-end doesn't make any attempt to keep track of the starting
9517      bit offset (relative to the start of the containing structure type) of the
9518      hypothetical "containing object" for a bit-field.  Thus, when computing
9519      the byte offset value for the start of the "containing object" of a
9520      bit-field, we must deduce this information on our own. This can be rather
9521      tricky to do in some cases.  For example, handling the following structure
9522      type definition when compiling for an i386/i486 target (which only aligns
9523      long long's to 32-bit boundaries) can be very tricky:
9524
9525          struct S { int field1; long long field2:31; };
9526
9527      Fortunately, there is a simple rule-of-thumb which can be used in such
9528      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9529      structure shown above.  It decides to do this based upon one simple rule
9530      for bit-field allocation.  GCC allocates each "containing object" for each
9531      bit-field at the first (i.e. lowest addressed) legitimate alignment
9532      boundary (based upon the required minimum alignment for the declared type
9533      of the field) which it can possibly use, subject to the condition that
9534      there is still enough available space remaining in the containing object
9535      (when allocated at the selected point) to fully accommodate all of the
9536      bits of the bit-field itself.
9537
9538      This simple rule makes it obvious why GCC allocates 8 bytes for each
9539      object of the structure type shown above.  When looking for a place to
9540      allocate the "containing object" for `field2', the compiler simply tries
9541      to allocate a 64-bit "containing object" at each successive 32-bit
9542      boundary (starting at zero) until it finds a place to allocate that 64-
9543      bit field such that at least 31 contiguous (and previously unallocated)
9544      bits remain within that selected 64 bit field.  (As it turns out, for the
9545      example above, the compiler finds it is OK to allocate the "containing
9546      object" 64-bit field at bit-offset zero within the structure type.)
9547
9548      Here we attempt to work backwards from the limited set of facts we're
9549      given, and we try to deduce from those facts, where GCC must have believed
9550      that the containing object started (within the structure type). The value
9551      we deduce is then used (by the callers of this routine) to generate
9552      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9553      and, in the case of DW_AT_location, regular fields as well).  */
9554
9555   /* Figure out the bit-distance from the start of the structure to the
9556      "deepest" bit of the bit-field.  */
9557   deepest_bitpos = bitpos_int + field_size_in_bits;
9558
9559   /* This is the tricky part.  Use some fancy footwork to deduce where the
9560      lowest addressed bit of the containing object must be.  */
9561   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9562
9563   /* Round up to type_align by default.  This works best for bitfields.  */
9564   object_offset_in_bits += type_align_in_bits - 1;
9565   object_offset_in_bits /= type_align_in_bits;
9566   object_offset_in_bits *= type_align_in_bits;
9567
9568   if (object_offset_in_bits > bitpos_int)
9569     {
9570       /* Sigh, the decl must be packed.  */
9571       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9572
9573       /* Round up to decl_align instead.  */
9574       object_offset_in_bits += decl_align_in_bits - 1;
9575       object_offset_in_bits /= decl_align_in_bits;
9576       object_offset_in_bits *= decl_align_in_bits;
9577     }
9578
9579   return object_offset_in_bits / BITS_PER_UNIT;
9580 }
9581 \f
9582 /* The following routines define various Dwarf attributes and any data
9583    associated with them.  */
9584
9585 /* Add a location description attribute value to a DIE.
9586
9587    This emits location attributes suitable for whole variables and
9588    whole parameters.  Note that the location attributes for struct fields are
9589    generated by the routine `data_member_location_attribute' below.  */
9590
9591 static inline void
9592 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9593                              dw_loc_descr_ref descr)
9594 {
9595   if (descr != 0)
9596     add_AT_loc (die, attr_kind, descr);
9597 }
9598
9599 /* Attach the specialized form of location attribute used for data members of
9600    struct and union types.  In the special case of a FIELD_DECL node which
9601    represents a bit-field, the "offset" part of this special location
9602    descriptor must indicate the distance in bytes from the lowest-addressed
9603    byte of the containing struct or union type to the lowest-addressed byte of
9604    the "containing object" for the bit-field.  (See the `field_byte_offset'
9605    function above).
9606
9607    For any given bit-field, the "containing object" is a hypothetical object
9608    (of some integral or enum type) within which the given bit-field lives.  The
9609    type of this hypothetical "containing object" is always the same as the
9610    declared type of the individual bit-field itself (for GCC anyway... the
9611    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9612    bytes) of the hypothetical "containing object" which will be given in the
9613    DW_AT_byte_size attribute for this bit-field.  (See the
9614    `byte_size_attribute' function below.)  It is also used when calculating the
9615    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9616    function below.)  */
9617
9618 static void
9619 add_data_member_location_attribute (dw_die_ref die, tree decl)
9620 {
9621   HOST_WIDE_INT offset;
9622   dw_loc_descr_ref loc_descr = 0;
9623
9624   if (TREE_CODE (decl) == TREE_BINFO)
9625     {
9626       /* We're working on the TAG_inheritance for a base class.  */
9627       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9628         {
9629           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9630              aren't at a fixed offset from all (sub)objects of the same
9631              type.  We need to extract the appropriate offset from our
9632              vtable.  The following dwarf expression means
9633
9634                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9635
9636              This is specific to the V3 ABI, of course.  */
9637
9638           dw_loc_descr_ref tmp;
9639
9640           /* Make a copy of the object address.  */
9641           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9642           add_loc_descr (&loc_descr, tmp);
9643
9644           /* Extract the vtable address.  */
9645           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9646           add_loc_descr (&loc_descr, tmp);
9647
9648           /* Calculate the address of the offset.  */
9649           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9650           gcc_assert (offset < 0);
9651
9652           tmp = int_loc_descriptor (-offset);
9653           add_loc_descr (&loc_descr, tmp);
9654           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9655           add_loc_descr (&loc_descr, tmp);
9656
9657           /* Extract the offset.  */
9658           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9659           add_loc_descr (&loc_descr, tmp);
9660
9661           /* Add it to the object address.  */
9662           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9663           add_loc_descr (&loc_descr, tmp);
9664         }
9665       else
9666         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9667     }
9668   else
9669     offset = field_byte_offset (decl);
9670
9671   if (! loc_descr)
9672     {
9673       enum dwarf_location_atom op;
9674
9675       /* The DWARF2 standard says that we should assume that the structure
9676          address is already on the stack, so we can specify a structure field
9677          address by using DW_OP_plus_uconst.  */
9678
9679 #ifdef MIPS_DEBUGGING_INFO
9680       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9681          operator correctly.  It works only if we leave the offset on the
9682          stack.  */
9683       op = DW_OP_constu;
9684 #else
9685       op = DW_OP_plus_uconst;
9686 #endif
9687
9688       loc_descr = new_loc_descr (op, offset, 0);
9689     }
9690
9691   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9692 }
9693
9694 /* Writes integer values to dw_vec_const array.  */
9695
9696 static void
9697 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9698 {
9699   while (size != 0)
9700     {
9701       *dest++ = val & 0xff;
9702       val >>= 8;
9703       --size;
9704     }
9705 }
9706
9707 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9708
9709 static HOST_WIDE_INT
9710 extract_int (const unsigned char *src, unsigned int size)
9711 {
9712   HOST_WIDE_INT val = 0;
9713
9714   src += size;
9715   while (size != 0)
9716     {
9717       val <<= 8;
9718       val |= *--src & 0xff;
9719       --size;
9720     }
9721   return val;
9722 }
9723
9724 /* Writes floating point values to dw_vec_const array.  */
9725
9726 static void
9727 insert_float (rtx rtl, unsigned char *array)
9728 {
9729   REAL_VALUE_TYPE rv;
9730   long val[4];
9731   int i;
9732
9733   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9734   real_to_target (val, &rv, GET_MODE (rtl));
9735
9736   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9737   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9738     {
9739       insert_int (val[i], 4, array);
9740       array += 4;
9741     }
9742 }
9743
9744 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9745    does not have a "location" either in memory or in a register.  These
9746    things can arise in GNU C when a constant is passed as an actual parameter
9747    to an inlined function.  They can also arise in C++ where declared
9748    constants do not necessarily get memory "homes".  */
9749
9750 static void
9751 add_const_value_attribute (dw_die_ref die, rtx rtl)
9752 {
9753   switch (GET_CODE (rtl))
9754     {
9755     case CONST_INT:
9756       {
9757         HOST_WIDE_INT val = INTVAL (rtl);
9758
9759         if (val < 0)
9760           add_AT_int (die, DW_AT_const_value, val);
9761         else
9762           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9763       }
9764       break;
9765
9766     case CONST_DOUBLE:
9767       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9768          floating-point constant.  A CONST_DOUBLE is used whenever the
9769          constant requires more than one word in order to be adequately
9770          represented.  We output CONST_DOUBLEs as blocks.  */
9771       {
9772         enum machine_mode mode = GET_MODE (rtl);
9773
9774         if (SCALAR_FLOAT_MODE_P (mode))
9775           {
9776             unsigned int length = GET_MODE_SIZE (mode);
9777             unsigned char *array = ggc_alloc (length);
9778
9779             insert_float (rtl, array);
9780             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9781           }
9782         else
9783           {
9784             /* ??? We really should be using HOST_WIDE_INT throughout.  */
9785             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9786
9787             add_AT_long_long (die, DW_AT_const_value,
9788                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9789           }
9790       }
9791       break;
9792
9793     case CONST_VECTOR:
9794       {
9795         enum machine_mode mode = GET_MODE (rtl);
9796         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9797         unsigned int length = CONST_VECTOR_NUNITS (rtl);
9798         unsigned char *array = ggc_alloc (length * elt_size);
9799         unsigned int i;
9800         unsigned char *p;
9801
9802         switch (GET_MODE_CLASS (mode))
9803           {
9804           case MODE_VECTOR_INT:
9805             for (i = 0, p = array; i < length; i++, p += elt_size)
9806               {
9807                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9808                 HOST_WIDE_INT lo, hi;
9809
9810                 switch (GET_CODE (elt))
9811                   {
9812                   case CONST_INT:
9813                     lo = INTVAL (elt);
9814                     hi = -(lo < 0);
9815                     break;
9816
9817                   case CONST_DOUBLE:
9818                     lo = CONST_DOUBLE_LOW (elt);
9819                     hi = CONST_DOUBLE_HIGH (elt);
9820                     break;
9821
9822                   default:
9823                     gcc_unreachable ();
9824                   }
9825
9826                 if (elt_size <= sizeof (HOST_WIDE_INT))
9827                   insert_int (lo, elt_size, p);
9828                 else
9829                   {
9830                     unsigned char *p0 = p;
9831                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9832
9833                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9834                     if (WORDS_BIG_ENDIAN)
9835                       {
9836                         p0 = p1;
9837                         p1 = p;
9838                       }
9839                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9840                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9841                   }
9842               }
9843             break;
9844
9845           case MODE_VECTOR_FLOAT:
9846             for (i = 0, p = array; i < length; i++, p += elt_size)
9847               {
9848                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9849                 insert_float (elt, p);
9850               }
9851             break;
9852
9853           default:
9854             gcc_unreachable ();
9855           }
9856
9857         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9858       }
9859       break;
9860
9861     case CONST_STRING:
9862       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9863       break;
9864
9865     case SYMBOL_REF:
9866     case LABEL_REF:
9867     case CONST:
9868       add_AT_addr (die, DW_AT_const_value, rtl);
9869       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9870       break;
9871
9872     case PLUS:
9873       /* In cases where an inlined instance of an inline function is passed
9874          the address of an `auto' variable (which is local to the caller) we
9875          can get a situation where the DECL_RTL of the artificial local
9876          variable (for the inlining) which acts as a stand-in for the
9877          corresponding formal parameter (of the inline function) will look
9878          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9879          exactly a compile-time constant expression, but it isn't the address
9880          of the (artificial) local variable either.  Rather, it represents the
9881          *value* which the artificial local variable always has during its
9882          lifetime.  We currently have no way to represent such quasi-constant
9883          values in Dwarf, so for now we just punt and generate nothing.  */
9884       break;
9885
9886     default:
9887       /* No other kinds of rtx should be possible here.  */
9888       gcc_unreachable ();
9889     }
9890
9891 }
9892
9893 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9894    for use in a later add_const_value_attribute call.  */
9895
9896 static rtx
9897 rtl_for_decl_init (tree init, tree type)
9898 {
9899   rtx rtl = NULL_RTX;
9900
9901   /* If a variable is initialized with a string constant without embedded
9902      zeros, build CONST_STRING.  */
9903   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9904     {
9905       tree enttype = TREE_TYPE (type);
9906       tree domain = TYPE_DOMAIN (type);
9907       enum machine_mode mode = TYPE_MODE (enttype);
9908
9909       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9910           && domain
9911           && integer_zerop (TYPE_MIN_VALUE (domain))
9912           && compare_tree_int (TYPE_MAX_VALUE (domain),
9913                                TREE_STRING_LENGTH (init) - 1) == 0
9914           && ((size_t) TREE_STRING_LENGTH (init)
9915               == strlen (TREE_STRING_POINTER (init)) + 1))
9916         rtl = gen_rtx_CONST_STRING (VOIDmode,
9917                                     ggc_strdup (TREE_STRING_POINTER (init)));
9918     }
9919   /* If the initializer is something that we know will expand into an
9920      immediate RTL constant, expand it now.  Expanding anything else
9921      tends to produce unresolved symbols; see debug/5770 and c++/6381.  */
9922   /* Aggregate, vector, and complex types may contain constructors that may
9923      result in code being generated when expand_expr is called, so we can't
9924      handle them here.  Integer and float are useful and safe types to handle
9925      here.  */
9926   else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9927            && initializer_constant_valid_p (init, type) == null_pointer_node)
9928     {
9929       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9930
9931       /* If expand_expr returns a MEM, it wasn't immediate.  */
9932       gcc_assert (!rtl || !MEM_P (rtl));
9933     }
9934
9935   return rtl;
9936 }
9937
9938 /* Generate RTL for the variable DECL to represent its location.  */
9939
9940 static rtx
9941 rtl_for_decl_location (tree decl)
9942 {
9943   rtx rtl;
9944
9945   /* Here we have to decide where we are going to say the parameter "lives"
9946      (as far as the debugger is concerned).  We only have a couple of
9947      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9948
9949      DECL_RTL normally indicates where the parameter lives during most of the
9950      activation of the function.  If optimization is enabled however, this
9951      could be either NULL or else a pseudo-reg.  Both of those cases indicate
9952      that the parameter doesn't really live anywhere (as far as the code
9953      generation parts of GCC are concerned) during most of the function's
9954      activation.  That will happen (for example) if the parameter is never
9955      referenced within the function.
9956
9957      We could just generate a location descriptor here for all non-NULL
9958      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9959      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
9960      where DECL_RTL is NULL or is a pseudo-reg.
9961
9962      Note however that we can only get away with using DECL_INCOMING_RTL as
9963      a backup substitute for DECL_RTL in certain limited cases.  In cases
9964      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
9965      we can be sure that the parameter was passed using the same type as it is
9966      declared to have within the function, and that its DECL_INCOMING_RTL
9967      points us to a place where a value of that type is passed.
9968
9969      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
9970      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
9971      because in these cases DECL_INCOMING_RTL points us to a value of some
9972      type which is *different* from the type of the parameter itself.  Thus,
9973      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
9974      such cases, the debugger would end up (for example) trying to fetch a
9975      `float' from a place which actually contains the first part of a
9976      `double'.  That would lead to really incorrect and confusing
9977      output at debug-time.
9978
9979      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
9980      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
9981      are a couple of exceptions however.  On little-endian machines we can
9982      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
9983      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
9984      an integral type that is smaller than TREE_TYPE (decl). These cases arise
9985      when (on a little-endian machine) a non-prototyped function has a
9986      parameter declared to be of type `short' or `char'.  In such cases,
9987      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
9988      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
9989      passed `int' value.  If the debugger then uses that address to fetch
9990      a `short' or a `char' (on a little-endian machine) the result will be
9991      the correct data, so we allow for such exceptional cases below.
9992
9993      Note that our goal here is to describe the place where the given formal
9994      parameter lives during most of the function's activation (i.e. between the
9995      end of the prologue and the start of the epilogue).  We'll do that as best
9996      as we can. Note however that if the given formal parameter is modified
9997      sometime during the execution of the function, then a stack backtrace (at
9998      debug-time) will show the function as having been called with the *new*
9999      value rather than the value which was originally passed in.  This happens
10000      rarely enough that it is not a major problem, but it *is* a problem, and
10001      I'd like to fix it.
10002
10003      A future version of dwarf2out.c may generate two additional attributes for
10004      any given DW_TAG_formal_parameter DIE which will describe the "passed
10005      type" and the "passed location" for the given formal parameter in addition
10006      to the attributes we now generate to indicate the "declared type" and the
10007      "active location" for each parameter.  This additional set of attributes
10008      could be used by debuggers for stack backtraces. Separately, note that
10009      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10010      This happens (for example) for inlined-instances of inline function formal
10011      parameters which are never referenced.  This really shouldn't be
10012      happening.  All PARM_DECL nodes should get valid non-NULL
10013      DECL_INCOMING_RTL values.  FIXME.  */
10014
10015   /* Use DECL_RTL as the "location" unless we find something better.  */
10016   rtl = DECL_RTL_IF_SET (decl);
10017
10018   /* When generating abstract instances, ignore everything except
10019      constants, symbols living in memory, and symbols living in
10020      fixed registers.  */
10021   if (! reload_completed)
10022     {
10023       if (rtl
10024           && (CONSTANT_P (rtl)
10025               || (MEM_P (rtl)
10026                   && CONSTANT_P (XEXP (rtl, 0)))
10027               || (REG_P (rtl)
10028                   && TREE_CODE (decl) == VAR_DECL
10029                   && TREE_STATIC (decl))))
10030         {
10031           rtl = targetm.delegitimize_address (rtl);
10032           return rtl;
10033         }
10034       rtl = NULL_RTX;
10035     }
10036   else if (TREE_CODE (decl) == PARM_DECL)
10037     {
10038       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10039         {
10040           tree declared_type = TREE_TYPE (decl);
10041           tree passed_type = DECL_ARG_TYPE (decl);
10042           enum machine_mode dmode = TYPE_MODE (declared_type);
10043           enum machine_mode pmode = TYPE_MODE (passed_type);
10044
10045           /* This decl represents a formal parameter which was optimized out.
10046              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10047              all cases where (rtl == NULL_RTX) just below.  */
10048           if (dmode == pmode)
10049             rtl = DECL_INCOMING_RTL (decl);
10050           else if (SCALAR_INT_MODE_P (dmode)
10051                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10052                    && DECL_INCOMING_RTL (decl))
10053             {
10054               rtx inc = DECL_INCOMING_RTL (decl);
10055               if (REG_P (inc))
10056                 rtl = inc;
10057               else if (MEM_P (inc))
10058                 {
10059                   if (BYTES_BIG_ENDIAN)
10060                     rtl = adjust_address_nv (inc, dmode,
10061                                              GET_MODE_SIZE (pmode)
10062                                              - GET_MODE_SIZE (dmode));
10063                   else
10064                     rtl = inc;
10065                 }
10066             }
10067         }
10068
10069       /* If the parm was passed in registers, but lives on the stack, then
10070          make a big endian correction if the mode of the type of the
10071          parameter is not the same as the mode of the rtl.  */
10072       /* ??? This is the same series of checks that are made in dbxout.c before
10073          we reach the big endian correction code there.  It isn't clear if all
10074          of these checks are necessary here, but keeping them all is the safe
10075          thing to do.  */
10076       else if (MEM_P (rtl)
10077                && XEXP (rtl, 0) != const0_rtx
10078                && ! CONSTANT_P (XEXP (rtl, 0))
10079                /* Not passed in memory.  */
10080                && !MEM_P (DECL_INCOMING_RTL (decl))
10081                /* Not passed by invisible reference.  */
10082                && (!REG_P (XEXP (rtl, 0))
10083                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10084                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10085 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10086                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10087 #endif
10088                      )
10089                /* Big endian correction check.  */
10090                && BYTES_BIG_ENDIAN
10091                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10092                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10093                    < UNITS_PER_WORD))
10094         {
10095           int offset = (UNITS_PER_WORD
10096                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10097
10098           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10099                              plus_constant (XEXP (rtl, 0), offset));
10100         }
10101     }
10102   else if (TREE_CODE (decl) == VAR_DECL
10103            && rtl
10104            && MEM_P (rtl)
10105            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10106            && BYTES_BIG_ENDIAN)
10107     {
10108       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10109       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10110
10111       /* If a variable is declared "register" yet is smaller than
10112          a register, then if we store the variable to memory, it
10113          looks like we're storing a register-sized value, when in
10114          fact we are not.  We need to adjust the offset of the
10115          storage location to reflect the actual value's bytes,
10116          else gdb will not be able to display it.  */
10117       if (rsize > dsize)
10118         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10119                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10120     }
10121
10122   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10123      and will have been substituted directly into all expressions that use it.
10124      C does not have such a concept, but C++ and other languages do.  */
10125   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10126     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10127
10128   if (rtl)
10129     rtl = targetm.delegitimize_address (rtl);
10130
10131   /* If we don't look past the constant pool, we risk emitting a
10132      reference to a constant pool entry that isn't referenced from
10133      code, and thus is not emitted.  */
10134   if (rtl)
10135     rtl = avoid_constant_pool_reference (rtl);
10136
10137   return rtl;
10138 }
10139
10140 /* We need to figure out what section we should use as the base for the
10141    address ranges where a given location is valid.
10142    1. If this particular DECL has a section associated with it, use that.
10143    2. If this function has a section associated with it, use that.
10144    3. Otherwise, use the text section.
10145    XXX: If you split a variable across multiple sections, we won't notice.  */
10146
10147 static const char *
10148 secname_for_decl (tree decl)
10149 {
10150   const char *secname;
10151
10152   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10153     {
10154       tree sectree = DECL_SECTION_NAME (decl);
10155       secname = TREE_STRING_POINTER (sectree);
10156     }
10157   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10158     {
10159       tree sectree = DECL_SECTION_NAME (current_function_decl);
10160       secname = TREE_STRING_POINTER (sectree);
10161     }
10162   else if (cfun && in_cold_section_p)
10163     secname = cfun->cold_section_label;
10164   else
10165     secname = text_section_label;
10166
10167   return secname;
10168 }
10169
10170 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10171    data attribute for a variable or a parameter.  We generate the
10172    DW_AT_const_value attribute only in those cases where the given variable
10173    or parameter does not have a true "location" either in memory or in a
10174    register.  This can happen (for example) when a constant is passed as an
10175    actual argument in a call to an inline function.  (It's possible that
10176    these things can crop up in other ways also.)  Note that one type of
10177    constant value which can be passed into an inlined function is a constant
10178    pointer.  This can happen for example if an actual argument in an inlined
10179    function call evaluates to a compile-time constant address.  */
10180
10181 static void
10182 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10183                                        enum dwarf_attribute attr)
10184 {
10185   rtx rtl;
10186   dw_loc_descr_ref descr;
10187   var_loc_list *loc_list;
10188   struct var_loc_node *node;
10189   if (TREE_CODE (decl) == ERROR_MARK)
10190     return;
10191
10192   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10193               || TREE_CODE (decl) == RESULT_DECL);
10194              
10195   /* See if we possibly have multiple locations for this variable.  */
10196   loc_list = lookup_decl_loc (decl);
10197
10198   /* If it truly has multiple locations, the first and last node will
10199      differ.  */
10200   if (loc_list && loc_list->first != loc_list->last)
10201     {
10202       const char *endname, *secname;
10203       dw_loc_list_ref list;
10204       rtx varloc;
10205
10206       /* Now that we know what section we are using for a base,
10207          actually construct the list of locations.
10208          The first location information is what is passed to the
10209          function that creates the location list, and the remaining
10210          locations just get added on to that list.
10211          Note that we only know the start address for a location
10212          (IE location changes), so to build the range, we use
10213          the range [current location start, next location start].
10214          This means we have to special case the last node, and generate
10215          a range of [last location start, end of function label].  */
10216
10217       node = loc_list->first;
10218       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10219       secname = secname_for_decl (decl);
10220
10221       list = new_loc_list (loc_descriptor (varloc),
10222                            node->label, node->next->label, secname, 1);
10223       node = node->next;
10224
10225       for (; node->next; node = node->next)
10226         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10227           {
10228             /* The variable has a location between NODE->LABEL and
10229                NODE->NEXT->LABEL.  */
10230             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10231             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10232                                        node->label, node->next->label, secname);
10233           }
10234
10235       /* If the variable has a location at the last label
10236          it keeps its location until the end of function.  */
10237       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10238         {
10239           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10240
10241           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10242           if (!current_function_decl)
10243             endname = text_end_label;
10244           else
10245             {
10246               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10247                                            current_function_funcdef_no);
10248               endname = ggc_strdup (label_id);
10249             }
10250           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10251                                      node->label, endname, secname);
10252         }
10253
10254       /* Finally, add the location list to the DIE, and we are done.  */
10255       add_AT_loc_list (die, attr, list);
10256       return;
10257     }
10258
10259   /* Try to get some constant RTL for this decl, and use that as the value of
10260      the location.  */
10261   
10262   rtl = rtl_for_decl_location (decl);
10263   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10264     {
10265       add_const_value_attribute (die, rtl);
10266       return;
10267     }
10268   
10269   /* If we have tried to generate the location otherwise, and it
10270      didn't work out (we wouldn't be here if we did), and we have a one entry
10271      location list, try generating a location from that.  */
10272   if (loc_list && loc_list->first)
10273     {
10274       node = loc_list->first;
10275       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10276       if (descr)
10277         {
10278           add_AT_location_description (die, attr, descr);
10279           return;
10280         }
10281     }
10282
10283   /* We couldn't get any rtl, so try directly generating the location
10284      description from the tree.  */
10285   descr = loc_descriptor_from_tree (decl);
10286   if (descr)
10287     {
10288       add_AT_location_description (die, attr, descr);
10289       return;
10290     }
10291 }
10292
10293 /* If we don't have a copy of this variable in memory for some reason (such
10294    as a C++ member constant that doesn't have an out-of-line definition),
10295    we should tell the debugger about the constant value.  */
10296
10297 static void
10298 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10299 {
10300   tree init = DECL_INITIAL (decl);
10301   tree type = TREE_TYPE (decl);
10302   rtx rtl;
10303
10304   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10305     /* OK */;
10306   else
10307     return;
10308
10309   rtl = rtl_for_decl_init (init, type);
10310   if (rtl)
10311     add_const_value_attribute (var_die, rtl);
10312 }
10313
10314 /* Convert the CFI instructions for the current function into a
10315    location list.  This is used for DW_AT_frame_base when we targeting
10316    a dwarf2 consumer that does not support the dwarf3
10317    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10318    expressions.  */
10319
10320 static dw_loc_list_ref
10321 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10322 {
10323   dw_fde_ref fde;
10324   dw_loc_list_ref list, *list_tail;
10325   dw_cfi_ref cfi;
10326   dw_cfa_location last_cfa, next_cfa;
10327   const char *start_label, *last_label, *section;
10328
10329   fde = &fde_table[fde_table_in_use - 1];
10330
10331   section = secname_for_decl (current_function_decl);
10332   list_tail = &list;
10333   list = NULL;
10334
10335   next_cfa.reg = INVALID_REGNUM;
10336   next_cfa.offset = 0;
10337   next_cfa.indirect = 0;
10338   next_cfa.base_offset = 0;
10339
10340   start_label = fde->dw_fde_begin;
10341
10342   /* ??? Bald assumption that the CIE opcode list does not contain
10343      advance opcodes.  */
10344   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10345     lookup_cfa_1 (cfi, &next_cfa);
10346
10347   last_cfa = next_cfa;
10348   last_label = start_label;
10349
10350   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10351     switch (cfi->dw_cfi_opc)
10352       {
10353       case DW_CFA_advance_loc1:
10354       case DW_CFA_advance_loc2:
10355       case DW_CFA_advance_loc4:
10356         if (!cfa_equal_p (&last_cfa, &next_cfa))
10357           {
10358             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10359                                        start_label, last_label, section,
10360                                        list == NULL);
10361
10362             list_tail = &(*list_tail)->dw_loc_next;
10363             last_cfa = next_cfa;
10364             start_label = last_label;
10365           }
10366         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10367         break;
10368
10369       case DW_CFA_advance_loc:
10370         /* The encoding is complex enough that we should never emit this.  */
10371       case DW_CFA_remember_state:
10372       case DW_CFA_restore_state:
10373         /* We don't handle these two in this function.  It would be possible
10374            if it were to be required.  */
10375         gcc_unreachable ();
10376
10377       default:
10378         lookup_cfa_1 (cfi, &next_cfa);
10379         break;
10380       }
10381
10382   if (!cfa_equal_p (&last_cfa, &next_cfa))
10383     {
10384       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10385                                  start_label, last_label, section,
10386                                  list == NULL);
10387       list_tail = &(*list_tail)->dw_loc_next;
10388       start_label = last_label;
10389     }
10390   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10391                              start_label, fde->dw_fde_end, section,
10392                              list == NULL);
10393
10394   return list;
10395 }
10396
10397 /* Compute a displacement from the "steady-state frame pointer" to the
10398    frame base (often the same as the CFA), and store it in
10399    frame_pointer_fb_offset.  OFFSET is added to the displacement
10400    before the latter is negated.  */
10401
10402 static void
10403 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10404 {
10405   rtx reg, elim;
10406
10407 #ifdef FRAME_POINTER_CFA_OFFSET
10408   reg = frame_pointer_rtx;
10409   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10410 #else
10411   reg = arg_pointer_rtx;
10412   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10413 #endif
10414
10415   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10416   if (GET_CODE (elim) == PLUS)
10417     {
10418       offset += INTVAL (XEXP (elim, 1));
10419       elim = XEXP (elim, 0);
10420     }
10421   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10422                        : stack_pointer_rtx));
10423
10424   frame_pointer_fb_offset = -offset;
10425 }
10426
10427 /* Generate a DW_AT_name attribute given some string value to be included as
10428    the value of the attribute.  */
10429
10430 static void
10431 add_name_attribute (dw_die_ref die, const char *name_string)
10432 {
10433   if (name_string != NULL && *name_string != 0)
10434     {
10435       if (demangle_name_func)
10436         name_string = (*demangle_name_func) (name_string);
10437
10438       add_AT_string (die, DW_AT_name, name_string);
10439     }
10440 }
10441
10442 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10443
10444 static void
10445 add_comp_dir_attribute (dw_die_ref die)
10446 {
10447   const char *wd = get_src_pwd ();
10448   if (wd != NULL)
10449     add_AT_string (die, DW_AT_comp_dir, wd);
10450 }
10451
10452 /* Given a tree node describing an array bound (either lower or upper) output
10453    a representation for that bound.  */
10454
10455 static void
10456 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10457 {
10458   switch (TREE_CODE (bound))
10459     {
10460     case ERROR_MARK:
10461       return;
10462
10463     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10464     case INTEGER_CST:
10465       if (! host_integerp (bound, 0)
10466           || (bound_attr == DW_AT_lower_bound
10467               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10468                   || (is_fortran () && integer_onep (bound)))))
10469         /* Use the default.  */
10470         ;
10471       else
10472         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10473       break;
10474
10475     case CONVERT_EXPR:
10476     case NOP_EXPR:
10477     case NON_LVALUE_EXPR:
10478     case VIEW_CONVERT_EXPR:
10479       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10480       break;
10481
10482     case SAVE_EXPR:
10483       break;
10484
10485     case VAR_DECL:
10486     case PARM_DECL:
10487     case RESULT_DECL:
10488       {
10489         dw_die_ref decl_die = lookup_decl_die (bound);
10490
10491         /* ??? Can this happen, or should the variable have been bound
10492            first?  Probably it can, since I imagine that we try to create
10493            the types of parameters in the order in which they exist in
10494            the list, and won't have created a forward reference to a
10495            later parameter.  */
10496         if (decl_die != NULL)
10497           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10498         break;
10499       }
10500
10501     default:
10502       {
10503         /* Otherwise try to create a stack operation procedure to
10504            evaluate the value of the array bound.  */
10505
10506         dw_die_ref ctx, decl_die;
10507         dw_loc_descr_ref loc;
10508
10509         loc = loc_descriptor_from_tree (bound);
10510         if (loc == NULL)
10511           break;
10512
10513         if (current_function_decl == 0)
10514           ctx = comp_unit_die;
10515         else
10516           ctx = lookup_decl_die (current_function_decl);
10517
10518         decl_die = new_die (DW_TAG_variable, ctx, bound);
10519         add_AT_flag (decl_die, DW_AT_artificial, 1);
10520         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10521         add_AT_loc (decl_die, DW_AT_location, loc);
10522
10523         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10524         break;
10525       }
10526     }
10527 }
10528
10529 /* Note that the block of subscript information for an array type also
10530    includes information about the element type of type given array type.  */
10531
10532 static void
10533 add_subscript_info (dw_die_ref type_die, tree type)
10534 {
10535 #ifndef MIPS_DEBUGGING_INFO
10536   unsigned dimension_number;
10537 #endif
10538   tree lower, upper;
10539   dw_die_ref subrange_die;
10540
10541   /* The GNU compilers represent multidimensional array types as sequences of
10542      one dimensional array types whose element types are themselves array
10543      types.  Here we squish that down, so that each multidimensional array
10544      type gets only one array_type DIE in the Dwarf debugging info. The draft
10545      Dwarf specification say that we are allowed to do this kind of
10546      compression in C (because there is no difference between an array or
10547      arrays and a multidimensional array in C) but for other source languages
10548      (e.g. Ada) we probably shouldn't do this.  */
10549
10550   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10551      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10552      We work around this by disabling this feature.  See also
10553      gen_array_type_die.  */
10554 #ifndef MIPS_DEBUGGING_INFO
10555   for (dimension_number = 0;
10556        TREE_CODE (type) == ARRAY_TYPE;
10557        type = TREE_TYPE (type), dimension_number++)
10558 #endif
10559     {
10560       tree domain = TYPE_DOMAIN (type);
10561
10562       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10563          and (in GNU C only) variable bounds.  Handle all three forms
10564          here.  */
10565       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10566       if (domain)
10567         {
10568           /* We have an array type with specified bounds.  */
10569           lower = TYPE_MIN_VALUE (domain);
10570           upper = TYPE_MAX_VALUE (domain);
10571
10572           /* Define the index type.  */
10573           if (TREE_TYPE (domain))
10574             {
10575               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10576                  TREE_TYPE field.  We can't emit debug info for this
10577                  because it is an unnamed integral type.  */
10578               if (TREE_CODE (domain) == INTEGER_TYPE
10579                   && TYPE_NAME (domain) == NULL_TREE
10580                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10581                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10582                 ;
10583               else
10584                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10585                                     type_die);
10586             }
10587
10588           /* ??? If upper is NULL, the array has unspecified length,
10589              but it does have a lower bound.  This happens with Fortran
10590                dimension arr(N:*)
10591              Since the debugger is definitely going to need to know N
10592              to produce useful results, go ahead and output the lower
10593              bound solo, and hope the debugger can cope.  */
10594
10595           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10596           if (upper)
10597             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10598         }
10599
10600       /* Otherwise we have an array type with an unspecified length.  The
10601          DWARF-2 spec does not say how to handle this; let's just leave out the
10602          bounds.  */
10603     }
10604 }
10605
10606 static void
10607 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10608 {
10609   unsigned size;
10610
10611   switch (TREE_CODE (tree_node))
10612     {
10613     case ERROR_MARK:
10614       size = 0;
10615       break;
10616     case ENUMERAL_TYPE:
10617     case RECORD_TYPE:
10618     case UNION_TYPE:
10619     case QUAL_UNION_TYPE:
10620       size = int_size_in_bytes (tree_node);
10621       break;
10622     case FIELD_DECL:
10623       /* For a data member of a struct or union, the DW_AT_byte_size is
10624          generally given as the number of bytes normally allocated for an
10625          object of the *declared* type of the member itself.  This is true
10626          even for bit-fields.  */
10627       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10628       break;
10629     default:
10630       gcc_unreachable ();
10631     }
10632
10633   /* Note that `size' might be -1 when we get to this point.  If it is, that
10634      indicates that the byte size of the entity in question is variable.  We
10635      have no good way of expressing this fact in Dwarf at the present time,
10636      so just let the -1 pass on through.  */
10637   add_AT_unsigned (die, DW_AT_byte_size, size);
10638 }
10639
10640 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10641    which specifies the distance in bits from the highest order bit of the
10642    "containing object" for the bit-field to the highest order bit of the
10643    bit-field itself.
10644
10645    For any given bit-field, the "containing object" is a hypothetical object
10646    (of some integral or enum type) within which the given bit-field lives.  The
10647    type of this hypothetical "containing object" is always the same as the
10648    declared type of the individual bit-field itself.  The determination of the
10649    exact location of the "containing object" for a bit-field is rather
10650    complicated.  It's handled by the `field_byte_offset' function (above).
10651
10652    Note that it is the size (in bytes) of the hypothetical "containing object"
10653    which will be given in the DW_AT_byte_size attribute for this bit-field.
10654    (See `byte_size_attribute' above).  */
10655
10656 static inline void
10657 add_bit_offset_attribute (dw_die_ref die, tree decl)
10658 {
10659   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10660   tree type = DECL_BIT_FIELD_TYPE (decl);
10661   HOST_WIDE_INT bitpos_int;
10662   HOST_WIDE_INT highest_order_object_bit_offset;
10663   HOST_WIDE_INT highest_order_field_bit_offset;
10664   HOST_WIDE_INT unsigned bit_offset;
10665
10666   /* Must be a field and a bit field.  */
10667   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10668
10669   /* We can't yet handle bit-fields whose offsets are variable, so if we
10670      encounter such things, just return without generating any attribute
10671      whatsoever.  Likewise for variable or too large size.  */
10672   if (! host_integerp (bit_position (decl), 0)
10673       || ! host_integerp (DECL_SIZE (decl), 1))
10674     return;
10675
10676   bitpos_int = int_bit_position (decl);
10677
10678   /* Note that the bit offset is always the distance (in bits) from the
10679      highest-order bit of the "containing object" to the highest-order bit of
10680      the bit-field itself.  Since the "high-order end" of any object or field
10681      is different on big-endian and little-endian machines, the computation
10682      below must take account of these differences.  */
10683   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10684   highest_order_field_bit_offset = bitpos_int;
10685
10686   if (! BYTES_BIG_ENDIAN)
10687     {
10688       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10689       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10690     }
10691
10692   bit_offset
10693     = (! BYTES_BIG_ENDIAN
10694        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10695        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10696
10697   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10698 }
10699
10700 /* For a FIELD_DECL node which represents a bit field, output an attribute
10701    which specifies the length in bits of the given field.  */
10702
10703 static inline void
10704 add_bit_size_attribute (dw_die_ref die, tree decl)
10705 {
10706   /* Must be a field and a bit field.  */
10707   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10708               && DECL_BIT_FIELD_TYPE (decl));
10709
10710   if (host_integerp (DECL_SIZE (decl), 1))
10711     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10712 }
10713
10714 /* If the compiled language is ANSI C, then add a 'prototyped'
10715    attribute, if arg types are given for the parameters of a function.  */
10716
10717 static inline void
10718 add_prototyped_attribute (dw_die_ref die, tree func_type)
10719 {
10720   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10721       && TYPE_ARG_TYPES (func_type) != NULL)
10722     add_AT_flag (die, DW_AT_prototyped, 1);
10723 }
10724
10725 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10726    by looking in either the type declaration or object declaration
10727    equate table.  */
10728
10729 static inline void
10730 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10731 {
10732   dw_die_ref origin_die = NULL;
10733
10734   if (TREE_CODE (origin) != FUNCTION_DECL)
10735     {
10736       /* We may have gotten separated from the block for the inlined
10737          function, if we're in an exception handler or some such; make
10738          sure that the abstract function has been written out.
10739
10740          Doing this for nested functions is wrong, however; functions are
10741          distinct units, and our context might not even be inline.  */
10742       tree fn = origin;
10743
10744       if (TYPE_P (fn))
10745         fn = TYPE_STUB_DECL (fn);
10746       
10747       fn = decl_function_context (fn);
10748       if (fn)
10749         dwarf2out_abstract_function (fn);
10750     }
10751
10752   if (DECL_P (origin))
10753     origin_die = lookup_decl_die (origin);
10754   else if (TYPE_P (origin))
10755     origin_die = lookup_type_die (origin);
10756
10757   /* XXX: Functions that are never lowered don't always have correct block
10758      trees (in the case of java, they simply have no block tree, in some other
10759      languages).  For these functions, there is nothing we can really do to
10760      output correct debug info for inlined functions in all cases.  Rather
10761      than die, we'll just produce deficient debug info now, in that we will
10762      have variables without a proper abstract origin.  In the future, when all
10763      functions are lowered, we should re-add a gcc_assert (origin_die)
10764      here.  */
10765
10766   if (origin_die)
10767       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10768 }
10769
10770 /* We do not currently support the pure_virtual attribute.  */
10771
10772 static inline void
10773 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10774 {
10775   if (DECL_VINDEX (func_decl))
10776     {
10777       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10778
10779       if (host_integerp (DECL_VINDEX (func_decl), 0))
10780         add_AT_loc (die, DW_AT_vtable_elem_location,
10781                     new_loc_descr (DW_OP_constu,
10782                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
10783                                    0));
10784
10785       /* GNU extension: Record what type this method came from originally.  */
10786       if (debug_info_level > DINFO_LEVEL_TERSE)
10787         add_AT_die_ref (die, DW_AT_containing_type,
10788                         lookup_type_die (DECL_CONTEXT (func_decl)));
10789     }
10790 }
10791 \f
10792 /* Add source coordinate attributes for the given decl.  */
10793
10794 static void
10795 add_src_coords_attributes (dw_die_ref die, tree decl)
10796 {
10797   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10798   unsigned file_index = lookup_filename (s.file);
10799
10800   add_AT_unsigned (die, DW_AT_decl_file, file_index);
10801   add_AT_unsigned (die, DW_AT_decl_line, s.line);
10802 }
10803
10804 /* Add a DW_AT_name attribute and source coordinate attribute for the
10805    given decl, but only if it actually has a name.  */
10806
10807 static void
10808 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10809 {
10810   tree decl_name;
10811
10812   decl_name = DECL_NAME (decl);
10813   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10814     {
10815       add_name_attribute (die, dwarf2_name (decl, 0));
10816       if (! DECL_ARTIFICIAL (decl))
10817         add_src_coords_attributes (die, decl);
10818
10819       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10820           && TREE_PUBLIC (decl)
10821           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10822           && !DECL_ABSTRACT (decl)
10823           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10824         add_AT_string (die, DW_AT_MIPS_linkage_name,
10825                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10826     }
10827
10828 #ifdef VMS_DEBUGGING_INFO
10829   /* Get the function's name, as described by its RTL.  This may be different
10830      from the DECL_NAME name used in the source file.  */
10831   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10832     {
10833       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10834                    XEXP (DECL_RTL (decl), 0));
10835       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10836     }
10837 #endif
10838 }
10839
10840 /* Push a new declaration scope.  */
10841
10842 static void
10843 push_decl_scope (tree scope)
10844 {
10845   VEC_safe_push (tree, gc, decl_scope_table, scope);
10846 }
10847
10848 /* Pop a declaration scope.  */
10849
10850 static inline void
10851 pop_decl_scope (void)
10852 {
10853   VEC_pop (tree, decl_scope_table);
10854 }
10855
10856 /* Return the DIE for the scope that immediately contains this type.
10857    Non-named types get global scope.  Named types nested in other
10858    types get their containing scope if it's open, or global scope
10859    otherwise.  All other types (i.e. function-local named types) get
10860    the current active scope.  */
10861
10862 static dw_die_ref
10863 scope_die_for (tree t, dw_die_ref context_die)
10864 {
10865   dw_die_ref scope_die = NULL;
10866   tree containing_scope;
10867   int i;
10868
10869   /* Non-types always go in the current scope.  */
10870   gcc_assert (TYPE_P (t));
10871
10872   containing_scope = TYPE_CONTEXT (t);
10873
10874   /* Use the containing namespace if it was passed in (for a declaration).  */
10875   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10876     {
10877       if (context_die == lookup_decl_die (containing_scope))
10878         /* OK */;
10879       else
10880         containing_scope = NULL_TREE;
10881     }
10882
10883   /* Ignore function type "scopes" from the C frontend.  They mean that
10884      a tagged type is local to a parmlist of a function declarator, but
10885      that isn't useful to DWARF.  */
10886   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10887     containing_scope = NULL_TREE;
10888
10889   if (containing_scope == NULL_TREE)
10890     scope_die = comp_unit_die;
10891   else if (TYPE_P (containing_scope))
10892     {
10893       /* For types, we can just look up the appropriate DIE.  But
10894          first we check to see if we're in the middle of emitting it
10895          so we know where the new DIE should go.  */
10896       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10897         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10898           break;
10899
10900       if (i < 0)
10901         {
10902           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10903                       || TREE_ASM_WRITTEN (containing_scope));
10904
10905           /* If none of the current dies are suitable, we get file scope.  */
10906           scope_die = comp_unit_die;
10907         }
10908       else
10909         scope_die = lookup_type_die (containing_scope);
10910     }
10911   else
10912     scope_die = context_die;
10913
10914   return scope_die;
10915 }
10916
10917 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
10918
10919 static inline int
10920 local_scope_p (dw_die_ref context_die)
10921 {
10922   for (; context_die; context_die = context_die->die_parent)
10923     if (context_die->die_tag == DW_TAG_inlined_subroutine
10924         || context_die->die_tag == DW_TAG_subprogram)
10925       return 1;
10926
10927   return 0;
10928 }
10929
10930 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10931    whether or not to treat a DIE in this context as a declaration.  */
10932
10933 static inline int
10934 class_or_namespace_scope_p (dw_die_ref context_die)
10935 {
10936   return (context_die
10937           && (context_die->die_tag == DW_TAG_structure_type
10938               || context_die->die_tag == DW_TAG_union_type
10939               || context_die->die_tag == DW_TAG_namespace));
10940 }
10941
10942 /* Many forms of DIEs require a "type description" attribute.  This
10943    routine locates the proper "type descriptor" die for the type given
10944    by 'type', and adds a DW_AT_type attribute below the given die.  */
10945
10946 static void
10947 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10948                     int decl_volatile, dw_die_ref context_die)
10949 {
10950   enum tree_code code  = TREE_CODE (type);
10951   dw_die_ref type_die  = NULL;
10952
10953   /* ??? If this type is an unnamed subrange type of an integral or
10954      floating-point type, use the inner type.  This is because we have no
10955      support for unnamed types in base_type_die.  This can happen if this is
10956      an Ada subrange type.  Correct solution is emit a subrange type die.  */
10957   if ((code == INTEGER_TYPE || code == REAL_TYPE)
10958       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10959     type = TREE_TYPE (type), code = TREE_CODE (type);
10960
10961   if (code == ERROR_MARK
10962       /* Handle a special case.  For functions whose return type is void, we
10963          generate *no* type attribute.  (Note that no object may have type
10964          `void', so this only applies to function return types).  */
10965       || code == VOID_TYPE)
10966     return;
10967
10968   type_die = modified_type_die (type,
10969                                 decl_const || TYPE_READONLY (type),
10970                                 decl_volatile || TYPE_VOLATILE (type),
10971                                 context_die);
10972
10973   if (type_die != NULL)
10974     add_AT_die_ref (object_die, DW_AT_type, type_die);
10975 }
10976
10977 /* Given an object die, add the calling convention attribute for the
10978    function call type.  */
10979 static void
10980 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
10981 {
10982   enum dwarf_calling_convention value = DW_CC_normal;
10983
10984   value = targetm.dwarf_calling_convention (type);
10985
10986   /* Only add the attribute if the backend requests it, and
10987      is not DW_CC_normal.  */
10988   if (value && (value != DW_CC_normal))
10989     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
10990 }
10991
10992 /* Given a tree pointer to a struct, class, union, or enum type node, return
10993    a pointer to the (string) tag name for the given type, or zero if the type
10994    was declared without a tag.  */
10995
10996 static const char *
10997 type_tag (tree type)
10998 {
10999   const char *name = 0;
11000
11001   if (TYPE_NAME (type) != 0)
11002     {
11003       tree t = 0;
11004
11005       /* Find the IDENTIFIER_NODE for the type name.  */
11006       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11007         t = TYPE_NAME (type);
11008
11009       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11010          a TYPE_DECL node, regardless of whether or not a `typedef' was
11011          involved.  */
11012       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11013                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11014         t = DECL_NAME (TYPE_NAME (type));
11015
11016       /* Now get the name as a string, or invent one.  */
11017       if (t != 0)
11018         name = IDENTIFIER_POINTER (t);
11019     }
11020
11021   return (name == 0 || *name == '\0') ? 0 : name;
11022 }
11023
11024 /* Return the type associated with a data member, make a special check
11025    for bit field types.  */
11026
11027 static inline tree
11028 member_declared_type (tree member)
11029 {
11030   return (DECL_BIT_FIELD_TYPE (member)
11031           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11032 }
11033
11034 /* Get the decl's label, as described by its RTL. This may be different
11035    from the DECL_NAME name used in the source file.  */
11036
11037 #if 0
11038 static const char *
11039 decl_start_label (tree decl)
11040 {
11041   rtx x;
11042   const char *fnname;
11043
11044   x = DECL_RTL (decl);
11045   gcc_assert (MEM_P (x));
11046
11047   x = XEXP (x, 0);
11048   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11049
11050   fnname = XSTR (x, 0);
11051   return fnname;
11052 }
11053 #endif
11054 \f
11055 /* These routines generate the internal representation of the DIE's for
11056    the compilation unit.  Debugging information is collected by walking
11057    the declaration trees passed in from dwarf2out_decl().  */
11058
11059 static void
11060 gen_array_type_die (tree type, dw_die_ref context_die)
11061 {
11062   dw_die_ref scope_die = scope_die_for (type, context_die);
11063   dw_die_ref array_die;
11064   tree element_type;
11065
11066   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11067      the inner array type comes before the outer array type.  Thus we must
11068      call gen_type_die before we call new_die.  See below also.  */
11069 #ifdef MIPS_DEBUGGING_INFO
11070   gen_type_die (TREE_TYPE (type), context_die);
11071 #endif
11072
11073   array_die = new_die (DW_TAG_array_type, scope_die, type);
11074   add_name_attribute (array_die, type_tag (type));
11075   equate_type_number_to_die (type, array_die);
11076
11077   if (TREE_CODE (type) == VECTOR_TYPE)
11078     {
11079       /* The frontend feeds us a representation for the vector as a struct
11080          containing an array.  Pull out the array type.  */
11081       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11082       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11083     }
11084
11085 #if 0
11086   /* We default the array ordering.  SDB will probably do
11087      the right things even if DW_AT_ordering is not present.  It's not even
11088      an issue until we start to get into multidimensional arrays anyway.  If
11089      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11090      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11091      and when we find out that we need to put these in, we will only do so
11092      for multidimensional arrays.  */
11093   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11094 #endif
11095
11096 #ifdef MIPS_DEBUGGING_INFO
11097   /* The SGI compilers handle arrays of unknown bound by setting
11098      AT_declaration and not emitting any subrange DIEs.  */
11099   if (! TYPE_DOMAIN (type))
11100     add_AT_flag (array_die, DW_AT_declaration, 1);
11101   else
11102 #endif
11103     add_subscript_info (array_die, type);
11104
11105   /* Add representation of the type of the elements of this array type.  */
11106   element_type = TREE_TYPE (type);
11107
11108   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11109      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11110      We work around this by disabling this feature.  See also
11111      add_subscript_info.  */
11112 #ifndef MIPS_DEBUGGING_INFO
11113   while (TREE_CODE (element_type) == ARRAY_TYPE)
11114     element_type = TREE_TYPE (element_type);
11115
11116   gen_type_die (element_type, context_die);
11117 #endif
11118
11119   add_type_attribute (array_die, element_type, 0, 0, context_die);
11120 }
11121
11122 #if 0
11123 static void
11124 gen_entry_point_die (tree decl, dw_die_ref context_die)
11125 {
11126   tree origin = decl_ultimate_origin (decl);
11127   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11128
11129   if (origin != NULL)
11130     add_abstract_origin_attribute (decl_die, origin);
11131   else
11132     {
11133       add_name_and_src_coords_attributes (decl_die, decl);
11134       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11135                           0, 0, context_die);
11136     }
11137
11138   if (DECL_ABSTRACT (decl))
11139     equate_decl_number_to_die (decl, decl_die);
11140   else
11141     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11142 }
11143 #endif
11144
11145 /* Walk through the list of incomplete types again, trying once more to
11146    emit full debugging info for them.  */
11147
11148 static void
11149 retry_incomplete_types (void)
11150 {
11151   int i;
11152
11153   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11154     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11155 }
11156
11157 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11158
11159 static void
11160 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11161 {
11162   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11163
11164   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11165      be incomplete and such types are not marked.  */
11166   add_abstract_origin_attribute (type_die, type);
11167 }
11168
11169 /* Generate a DIE to represent an inlined instance of a structure type.  */
11170
11171 static void
11172 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11173 {
11174   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11175
11176   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11177      be incomplete and such types are not marked.  */
11178   add_abstract_origin_attribute (type_die, type);
11179 }
11180
11181 /* Generate a DIE to represent an inlined instance of a union type.  */
11182
11183 static void
11184 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11185 {
11186   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11187
11188   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11189      be incomplete and such types are not marked.  */
11190   add_abstract_origin_attribute (type_die, type);
11191 }
11192
11193 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11194    include all of the information about the enumeration values also. Each
11195    enumerated type name/value is listed as a child of the enumerated type
11196    DIE.  */
11197
11198 static dw_die_ref
11199 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11200 {
11201   dw_die_ref type_die = lookup_type_die (type);
11202
11203   if (type_die == NULL)
11204     {
11205       type_die = new_die (DW_TAG_enumeration_type,
11206                           scope_die_for (type, context_die), type);
11207       equate_type_number_to_die (type, type_die);
11208       add_name_attribute (type_die, type_tag (type));
11209     }
11210   else if (! TYPE_SIZE (type))
11211     return type_die;
11212   else
11213     remove_AT (type_die, DW_AT_declaration);
11214
11215   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11216      given enum type is incomplete, do not generate the DW_AT_byte_size
11217      attribute or the DW_AT_element_list attribute.  */
11218   if (TYPE_SIZE (type))
11219     {
11220       tree link;
11221
11222       TREE_ASM_WRITTEN (type) = 1;
11223       add_byte_size_attribute (type_die, type);
11224       if (TYPE_STUB_DECL (type) != NULL_TREE)
11225         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11226
11227       /* If the first reference to this type was as the return type of an
11228          inline function, then it may not have a parent.  Fix this now.  */
11229       if (type_die->die_parent == NULL)
11230         add_child_die (scope_die_for (type, context_die), type_die);
11231
11232       for (link = TYPE_VALUES (type);
11233            link != NULL; link = TREE_CHAIN (link))
11234         {
11235           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11236           tree value = TREE_VALUE (link);
11237
11238           add_name_attribute (enum_die,
11239                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11240
11241           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11242             /* DWARF2 does not provide a way of indicating whether or
11243                not enumeration constants are signed or unsigned.  GDB
11244                always assumes the values are signed, so we output all
11245                values as if they were signed.  That means that
11246                enumeration constants with very large unsigned values
11247                will appear to have negative values in the debugger.  */
11248             add_AT_int (enum_die, DW_AT_const_value,
11249                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11250         }
11251     }
11252   else
11253     add_AT_flag (type_die, DW_AT_declaration, 1);
11254
11255   return type_die;
11256 }
11257
11258 /* Generate a DIE to represent either a real live formal parameter decl or to
11259    represent just the type of some formal parameter position in some function
11260    type.
11261
11262    Note that this routine is a bit unusual because its argument may be a
11263    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11264    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11265    node.  If it's the former then this function is being called to output a
11266    DIE to represent a formal parameter object (or some inlining thereof).  If
11267    it's the latter, then this function is only being called to output a
11268    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11269    argument type of some subprogram type.  */
11270
11271 static dw_die_ref
11272 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11273 {
11274   dw_die_ref parm_die
11275     = new_die (DW_TAG_formal_parameter, context_die, node);
11276   tree origin;
11277
11278   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11279     {
11280     case tcc_declaration:
11281       origin = decl_ultimate_origin (node);
11282       if (origin != NULL)
11283         add_abstract_origin_attribute (parm_die, origin);
11284       else
11285         {
11286           add_name_and_src_coords_attributes (parm_die, node);
11287           add_type_attribute (parm_die, TREE_TYPE (node),
11288                               TREE_READONLY (node),
11289                               TREE_THIS_VOLATILE (node),
11290                               context_die);
11291           if (DECL_ARTIFICIAL (node))
11292             add_AT_flag (parm_die, DW_AT_artificial, 1);
11293         }
11294
11295       equate_decl_number_to_die (node, parm_die);
11296       if (! DECL_ABSTRACT (node))
11297         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11298
11299       break;
11300
11301     case tcc_type:
11302       /* We were called with some kind of a ..._TYPE node.  */
11303       add_type_attribute (parm_die, node, 0, 0, context_die);
11304       break;
11305
11306     default:
11307       gcc_unreachable ();
11308     }
11309
11310   return parm_die;
11311 }
11312
11313 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11314    at the end of an (ANSI prototyped) formal parameters list.  */
11315
11316 static void
11317 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11318 {
11319   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11320 }
11321
11322 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11323    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11324    parameters as specified in some function type specification (except for
11325    those which appear as part of a function *definition*).  */
11326
11327 static void
11328 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11329 {
11330   tree link;
11331   tree formal_type = NULL;
11332   tree first_parm_type;
11333   tree arg;
11334
11335   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11336     {
11337       arg = DECL_ARGUMENTS (function_or_method_type);
11338       function_or_method_type = TREE_TYPE (function_or_method_type);
11339     }
11340   else
11341     arg = NULL_TREE;
11342
11343   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11344
11345   /* Make our first pass over the list of formal parameter types and output a
11346      DW_TAG_formal_parameter DIE for each one.  */
11347   for (link = first_parm_type; link; )
11348     {
11349       dw_die_ref parm_die;
11350
11351       formal_type = TREE_VALUE (link);
11352       if (formal_type == void_type_node)
11353         break;
11354
11355       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11356       parm_die = gen_formal_parameter_die (formal_type, context_die);
11357       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11358            && link == first_parm_type)
11359           || (arg && DECL_ARTIFICIAL (arg)))
11360         add_AT_flag (parm_die, DW_AT_artificial, 1);
11361
11362       link = TREE_CHAIN (link);
11363       if (arg)
11364         arg = TREE_CHAIN (arg);
11365     }
11366
11367   /* If this function type has an ellipsis, add a
11368      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11369   if (formal_type != void_type_node)
11370     gen_unspecified_parameters_die (function_or_method_type, context_die);
11371
11372   /* Make our second (and final) pass over the list of formal parameter types
11373      and output DIEs to represent those types (as necessary).  */
11374   for (link = TYPE_ARG_TYPES (function_or_method_type);
11375        link && TREE_VALUE (link);
11376        link = TREE_CHAIN (link))
11377     gen_type_die (TREE_VALUE (link), context_die);
11378 }
11379
11380 /* We want to generate the DIE for TYPE so that we can generate the
11381    die for MEMBER, which has been defined; we will need to refer back
11382    to the member declaration nested within TYPE.  If we're trying to
11383    generate minimal debug info for TYPE, processing TYPE won't do the
11384    trick; we need to attach the member declaration by hand.  */
11385
11386 static void
11387 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11388 {
11389   gen_type_die (type, context_die);
11390
11391   /* If we're trying to avoid duplicate debug info, we may not have
11392      emitted the member decl for this function.  Emit it now.  */
11393   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11394       && ! lookup_decl_die (member))
11395     {
11396       dw_die_ref type_die;
11397       gcc_assert (!decl_ultimate_origin (member));
11398
11399       push_decl_scope (type);
11400       type_die = lookup_type_die (type);
11401       if (TREE_CODE (member) == FUNCTION_DECL)
11402         gen_subprogram_die (member, type_die);
11403       else if (TREE_CODE (member) == FIELD_DECL)
11404         {
11405           /* Ignore the nameless fields that are used to skip bits but handle
11406              C++ anonymous unions and structs.  */
11407           if (DECL_NAME (member) != NULL_TREE
11408               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11409               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11410             {
11411               gen_type_die (member_declared_type (member), type_die);
11412               gen_field_die (member, type_die);
11413             }
11414         }
11415       else
11416         gen_variable_die (member, type_die);
11417
11418       pop_decl_scope ();
11419     }
11420 }
11421
11422 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11423    may later generate inlined and/or out-of-line instances of.  */
11424
11425 static void
11426 dwarf2out_abstract_function (tree decl)
11427 {
11428   dw_die_ref old_die;
11429   tree save_fn;
11430   tree context;
11431   int was_abstract = DECL_ABSTRACT (decl);
11432
11433   /* Make sure we have the actual abstract inline, not a clone.  */
11434   decl = DECL_ORIGIN (decl);
11435
11436   old_die = lookup_decl_die (decl);
11437   if (old_die && get_AT (old_die, DW_AT_inline))
11438     /* We've already generated the abstract instance.  */
11439     return;
11440
11441   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11442      we don't get confused by DECL_ABSTRACT.  */
11443   if (debug_info_level > DINFO_LEVEL_TERSE)
11444     {
11445       context = decl_class_context (decl);
11446       if (context)
11447         gen_type_die_for_member
11448           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11449     }
11450
11451   /* Pretend we've just finished compiling this function.  */
11452   save_fn = current_function_decl;
11453   current_function_decl = decl;
11454
11455   set_decl_abstract_flags (decl, 1);
11456   dwarf2out_decl (decl);
11457   if (! was_abstract)
11458     set_decl_abstract_flags (decl, 0);
11459
11460   current_function_decl = save_fn;
11461 }
11462
11463 /* Generate a DIE to represent a declared function (either file-scope or
11464    block-local).  */
11465
11466 static void
11467 gen_subprogram_die (tree decl, dw_die_ref context_die)
11468 {
11469   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11470   tree origin = decl_ultimate_origin (decl);
11471   dw_die_ref subr_die;
11472   tree fn_arg_types;
11473   tree outer_scope;
11474   dw_die_ref old_die = lookup_decl_die (decl);
11475   int declaration = (current_function_decl != decl
11476                      || class_or_namespace_scope_p (context_die));
11477
11478   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11479      started to generate the abstract instance of an inline, decided to output
11480      its containing class, and proceeded to emit the declaration of the inline
11481      from the member list for the class.  If so, DECLARATION takes priority;
11482      we'll get back to the abstract instance when done with the class.  */
11483
11484   /* The class-scope declaration DIE must be the primary DIE.  */
11485   if (origin && declaration && class_or_namespace_scope_p (context_die))
11486     {
11487       origin = NULL;
11488       gcc_assert (!old_die);
11489     }
11490
11491   /* Now that the C++ front end lazily declares artificial member fns, we
11492      might need to retrofit the declaration into its class.  */
11493   if (!declaration && !origin && !old_die
11494       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11495       && !class_or_namespace_scope_p (context_die)
11496       && debug_info_level > DINFO_LEVEL_TERSE)
11497     old_die = force_decl_die (decl);
11498
11499   if (origin != NULL)
11500     {
11501       gcc_assert (!declaration || local_scope_p (context_die));
11502
11503       /* Fixup die_parent for the abstract instance of a nested
11504          inline function.  */
11505       if (old_die && old_die->die_parent == NULL)
11506         add_child_die (context_die, old_die);
11507
11508       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11509       add_abstract_origin_attribute (subr_die, origin);
11510     }
11511   else if (old_die)
11512     {
11513       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11514       unsigned file_index = lookup_filename (s.file);
11515
11516       if (!get_AT_flag (old_die, DW_AT_declaration)
11517           /* We can have a normal definition following an inline one in the
11518              case of redefinition of GNU C extern inlines.
11519              It seems reasonable to use AT_specification in this case.  */
11520           && !get_AT (old_die, DW_AT_inline))
11521         {
11522           /* Detect and ignore this case, where we are trying to output
11523              something we have already output.  */
11524           return;
11525         }
11526
11527       /* If the definition comes from the same place as the declaration,
11528          maybe use the old DIE.  We always want the DIE for this function
11529          that has the *_pc attributes to be under comp_unit_die so the
11530          debugger can find it.  We also need to do this for abstract
11531          instances of inlines, since the spec requires the out-of-line copy
11532          to have the same parent.  For local class methods, this doesn't
11533          apply; we just use the old DIE.  */
11534       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11535           && (DECL_ARTIFICIAL (decl)
11536               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11537                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11538                       == (unsigned) s.line))))
11539         {
11540           subr_die = old_die;
11541
11542           /* Clear out the declaration attribute and the formal parameters.
11543              Do not remove all children, because it is possible that this
11544              declaration die was forced using force_decl_die(). In such
11545              cases die that forced declaration die (e.g. TAG_imported_module)
11546              is one of the children that we do not want to remove.  */
11547           remove_AT (subr_die, DW_AT_declaration);
11548           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11549         }
11550       else
11551         {
11552           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11553           add_AT_specification (subr_die, old_die);
11554           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11555             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11556           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11557               != (unsigned) s.line)
11558             add_AT_unsigned
11559               (subr_die, DW_AT_decl_line, s.line);
11560         }
11561     }
11562   else
11563     {
11564       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11565
11566       if (TREE_PUBLIC (decl))
11567         add_AT_flag (subr_die, DW_AT_external, 1);
11568
11569       add_name_and_src_coords_attributes (subr_die, decl);
11570       if (debug_info_level > DINFO_LEVEL_TERSE)
11571         {
11572           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11573           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11574                               0, 0, context_die);
11575         }
11576
11577       add_pure_or_virtual_attribute (subr_die, decl);
11578       if (DECL_ARTIFICIAL (decl))
11579         add_AT_flag (subr_die, DW_AT_artificial, 1);
11580
11581       if (TREE_PROTECTED (decl))
11582         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11583       else if (TREE_PRIVATE (decl))
11584         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11585     }
11586
11587   if (declaration)
11588     {
11589       if (!old_die || !get_AT (old_die, DW_AT_inline))
11590         {
11591           add_AT_flag (subr_die, DW_AT_declaration, 1);
11592
11593           /* The first time we see a member function, it is in the context of
11594              the class to which it belongs.  We make sure of this by emitting
11595              the class first.  The next time is the definition, which is
11596              handled above.  The two may come from the same source text.
11597
11598              Note that force_decl_die() forces function declaration die. It is
11599              later reused to represent definition.  */
11600           equate_decl_number_to_die (decl, subr_die);
11601         }
11602     }
11603   else if (DECL_ABSTRACT (decl))
11604     {
11605       if (DECL_DECLARED_INLINE_P (decl))
11606         {
11607           if (cgraph_function_possibly_inlined_p (decl))
11608             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11609           else
11610             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11611         }
11612       else
11613         {
11614           if (cgraph_function_possibly_inlined_p (decl))
11615             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11616           else
11617             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11618         }
11619
11620       equate_decl_number_to_die (decl, subr_die);
11621     }
11622   else if (!DECL_EXTERNAL (decl))
11623     {
11624       HOST_WIDE_INT cfa_fb_offset;
11625
11626       if (!old_die || !get_AT (old_die, DW_AT_inline))
11627         equate_decl_number_to_die (decl, subr_die);
11628
11629       if (!flag_reorder_blocks_and_partition)
11630         {
11631           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11632                                        current_function_funcdef_no);
11633           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11634           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11635                                        current_function_funcdef_no);
11636           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11637           
11638           add_pubname (decl, subr_die);
11639           add_arange (decl, subr_die);
11640         }
11641       else
11642         {  /* Do nothing for now; maybe need to duplicate die, one for
11643               hot section and ond for cold section, then use the hot/cold
11644               section begin/end labels to generate the aranges...  */
11645           /*
11646             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11647             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11648             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11649             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11650
11651             add_pubname (decl, subr_die);
11652             add_arange (decl, subr_die);
11653             add_arange (decl, subr_die);
11654            */
11655         }
11656
11657 #ifdef MIPS_DEBUGGING_INFO
11658       /* Add a reference to the FDE for this routine.  */
11659       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11660 #endif
11661
11662       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11663
11664       /* We define the "frame base" as the function's CFA.  This is more
11665          convenient for several reasons: (1) It's stable across the prologue
11666          and epilogue, which makes it better than just a frame pointer,
11667          (2) With dwarf3, there exists a one-byte encoding that allows us
11668          to reference the .debug_frame data by proxy, but failing that,
11669          (3) We can at least reuse the code inspection and interpretation
11670          code that determines the CFA position at various points in the
11671          function.  */
11672       /* ??? Use some command-line or configury switch to enable the use
11673          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11674          consumers that understand it; fall back to "pure" dwarf2 and
11675          convert the CFA data into a location list.  */
11676       {
11677         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11678         if (list->dw_loc_next)
11679           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11680         else
11681           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11682       }
11683
11684       /* Compute a displacement from the "steady-state frame pointer" to
11685          the CFA.  The former is what all stack slots and argument slots
11686          will reference in the rtl; the later is what we've told the 
11687          debugger about.  We'll need to adjust all frame_base references
11688          by this displacement.  */
11689       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11690
11691       if (cfun->static_chain_decl)
11692         add_AT_location_description (subr_die, DW_AT_static_link,
11693                  loc_descriptor_from_tree (cfun->static_chain_decl));
11694     }
11695
11696   /* Now output descriptions of the arguments for this function. This gets
11697      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11698      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11699      `...' at the end of the formal parameter list.  In order to find out if
11700      there was a trailing ellipsis or not, we must instead look at the type
11701      associated with the FUNCTION_DECL.  This will be a node of type
11702      FUNCTION_TYPE. If the chain of type nodes hanging off of this
11703      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11704      an ellipsis at the end.  */
11705
11706   /* In the case where we are describing a mere function declaration, all we
11707      need to do here (and all we *can* do here) is to describe the *types* of
11708      its formal parameters.  */
11709   if (debug_info_level <= DINFO_LEVEL_TERSE)
11710     ;
11711   else if (declaration)
11712     gen_formal_types_die (decl, subr_die);
11713   else
11714     {
11715       /* Generate DIEs to represent all known formal parameters.  */
11716       tree arg_decls = DECL_ARGUMENTS (decl);
11717       tree parm;
11718
11719       /* When generating DIEs, generate the unspecified_parameters DIE
11720          instead if we come across the arg "__builtin_va_alist" */
11721       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11722         if (TREE_CODE (parm) == PARM_DECL)
11723           {
11724             if (DECL_NAME (parm)
11725                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11726                             "__builtin_va_alist"))
11727               gen_unspecified_parameters_die (parm, subr_die);
11728             else
11729               gen_decl_die (parm, subr_die);
11730           }
11731
11732       /* Decide whether we need an unspecified_parameters DIE at the end.
11733          There are 2 more cases to do this for: 1) the ansi ... declaration -
11734          this is detectable when the end of the arg list is not a
11735          void_type_node 2) an unprototyped function declaration (not a
11736          definition).  This just means that we have no info about the
11737          parameters at all.  */
11738       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11739       if (fn_arg_types != NULL)
11740         {
11741           /* This is the prototyped case, check for....  */
11742           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11743             gen_unspecified_parameters_die (decl, subr_die);
11744         }
11745       else if (DECL_INITIAL (decl) == NULL_TREE)
11746         gen_unspecified_parameters_die (decl, subr_die);
11747     }
11748
11749   /* Output Dwarf info for all of the stuff within the body of the function
11750      (if it has one - it may be just a declaration).  */
11751   outer_scope = DECL_INITIAL (decl);
11752
11753   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11754      a function.  This BLOCK actually represents the outermost binding contour
11755      for the function, i.e. the contour in which the function's formal
11756      parameters and labels get declared. Curiously, it appears that the front
11757      end doesn't actually put the PARM_DECL nodes for the current function onto
11758      the BLOCK_VARS list for this outer scope, but are strung off of the
11759      DECL_ARGUMENTS list for the function instead.
11760
11761      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11762      the LABEL_DECL nodes for the function however, and we output DWARF info
11763      for those in decls_for_scope.  Just within the `outer_scope' there will be
11764      a BLOCK node representing the function's outermost pair of curly braces,
11765      and any blocks used for the base and member initializers of a C++
11766      constructor function.  */
11767   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11768     {
11769       /* Emit a DW_TAG_variable DIE for a named return value.  */
11770       if (DECL_NAME (DECL_RESULT (decl)))
11771         gen_decl_die (DECL_RESULT (decl), subr_die);
11772
11773       current_function_has_inlines = 0;
11774       decls_for_scope (outer_scope, subr_die, 0);
11775
11776 #if 0 && defined (MIPS_DEBUGGING_INFO)
11777       if (current_function_has_inlines)
11778         {
11779           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11780           if (! comp_unit_has_inlines)
11781             {
11782               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11783               comp_unit_has_inlines = 1;
11784             }
11785         }
11786 #endif
11787     }
11788   /* Add the calling convention attribute if requested.  */
11789   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11790
11791 }
11792
11793 /* Generate a DIE to represent a declared data object.  */
11794
11795 static void
11796 gen_variable_die (tree decl, dw_die_ref context_die)
11797 {
11798   tree origin = decl_ultimate_origin (decl);
11799   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11800
11801   dw_die_ref old_die = lookup_decl_die (decl);
11802   int declaration = (DECL_EXTERNAL (decl)
11803                      /* If DECL is COMDAT and has not actually been
11804                         emitted, we cannot take its address; there
11805                         might end up being no definition anywhere in
11806                         the program.  For example, consider the C++
11807                         test case:
11808
11809                           template <class T>
11810                           struct S { static const int i = 7; };
11811
11812                           template <class T>
11813                           const int S<T>::i;
11814
11815                           int f() { return S<int>::i; }
11816                           
11817                         Here, S<int>::i is not DECL_EXTERNAL, but no
11818                         definition is required, so the compiler will
11819                         not emit a definition.  */  
11820                      || (TREE_CODE (decl) == VAR_DECL
11821                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11822                      || class_or_namespace_scope_p (context_die));
11823
11824   if (origin != NULL)
11825     add_abstract_origin_attribute (var_die, origin);
11826
11827   /* Loop unrolling can create multiple blocks that refer to the same
11828      static variable, so we must test for the DW_AT_declaration flag.
11829
11830      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11831      copy decls and set the DECL_ABSTRACT flag on them instead of
11832      sharing them.
11833
11834      ??? Duplicated blocks have been rewritten to use .debug_ranges.
11835
11836      ??? The declare_in_namespace support causes us to get two DIEs for one
11837      variable, both of which are declarations.  We want to avoid considering
11838      one to be a specification, so we must test that this DIE is not a
11839      declaration.  */
11840   else if (old_die && TREE_STATIC (decl) && ! declaration
11841            && get_AT_flag (old_die, DW_AT_declaration) == 1)
11842     {
11843       /* This is a definition of a C++ class level static.  */
11844       add_AT_specification (var_die, old_die);
11845       if (DECL_NAME (decl))
11846         {
11847           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11848           unsigned file_index = lookup_filename (s.file);
11849
11850           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11851             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11852
11853           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11854               != (unsigned) s.line)
11855
11856             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11857         }
11858     }
11859   else
11860     {
11861       add_name_and_src_coords_attributes (var_die, decl);
11862       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11863                           TREE_THIS_VOLATILE (decl), context_die);
11864
11865       if (TREE_PUBLIC (decl))
11866         add_AT_flag (var_die, DW_AT_external, 1);
11867
11868       if (DECL_ARTIFICIAL (decl))
11869         add_AT_flag (var_die, DW_AT_artificial, 1);
11870
11871       if (TREE_PROTECTED (decl))
11872         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11873       else if (TREE_PRIVATE (decl))
11874         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11875     }
11876
11877   if (declaration)
11878     add_AT_flag (var_die, DW_AT_declaration, 1);
11879
11880   if (DECL_ABSTRACT (decl) || declaration)
11881     equate_decl_number_to_die (decl, var_die);
11882
11883   if (! declaration && ! DECL_ABSTRACT (decl))
11884     {
11885       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11886       add_pubname (decl, var_die);
11887     }
11888   else
11889     tree_add_const_value_attribute (var_die, decl);
11890 }
11891
11892 /* Generate a DIE to represent a label identifier.  */
11893
11894 static void
11895 gen_label_die (tree decl, dw_die_ref context_die)
11896 {
11897   tree origin = decl_ultimate_origin (decl);
11898   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11899   rtx insn;
11900   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11901
11902   if (origin != NULL)
11903     add_abstract_origin_attribute (lbl_die, origin);
11904   else
11905     add_name_and_src_coords_attributes (lbl_die, decl);
11906
11907   if (DECL_ABSTRACT (decl))
11908     equate_decl_number_to_die (decl, lbl_die);
11909   else
11910     {
11911       insn = DECL_RTL_IF_SET (decl);
11912
11913       /* Deleted labels are programmer specified labels which have been
11914          eliminated because of various optimizations.  We still emit them
11915          here so that it is possible to put breakpoints on them.  */
11916       if (insn
11917           && (LABEL_P (insn)
11918               || ((NOTE_P (insn)
11919                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11920         {
11921           /* When optimization is enabled (via -O) some parts of the compiler
11922              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11923              represent source-level labels which were explicitly declared by
11924              the user.  This really shouldn't be happening though, so catch
11925              it if it ever does happen.  */
11926           gcc_assert (!INSN_DELETED_P (insn));
11927
11928           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11929           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11930         }
11931     }
11932 }
11933
11934 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
11935    attributes to the DIE for a block STMT, to describe where the inlined
11936    function was called from.  This is similar to add_src_coords_attributes.  */
11937
11938 static inline void
11939 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11940 {
11941   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11942   unsigned file_index = lookup_filename (s.file);
11943
11944   add_AT_unsigned (die, DW_AT_call_file, file_index);
11945   add_AT_unsigned (die, DW_AT_call_line, s.line);
11946 }
11947
11948 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11949    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
11950
11951 static inline void
11952 add_high_low_attributes (tree stmt, dw_die_ref die)
11953 {
11954   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11955
11956   if (BLOCK_FRAGMENT_CHAIN (stmt))
11957     {
11958       tree chain;
11959
11960       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
11961
11962       chain = BLOCK_FRAGMENT_CHAIN (stmt);
11963       do
11964         {
11965           add_ranges (chain);
11966           chain = BLOCK_FRAGMENT_CHAIN (chain);
11967         }
11968       while (chain);
11969       add_ranges (NULL);
11970     }
11971   else
11972     {
11973       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
11974                                    BLOCK_NUMBER (stmt));
11975       add_AT_lbl_id (die, DW_AT_low_pc, label);
11976       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
11977                                    BLOCK_NUMBER (stmt));
11978       add_AT_lbl_id (die, DW_AT_high_pc, label);
11979     }
11980 }
11981
11982 /* Generate a DIE for a lexical block.  */
11983
11984 static void
11985 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
11986 {
11987   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
11988
11989   if (! BLOCK_ABSTRACT (stmt))
11990     add_high_low_attributes (stmt, stmt_die);
11991
11992   decls_for_scope (stmt, stmt_die, depth);
11993 }
11994
11995 /* Generate a DIE for an inlined subprogram.  */
11996
11997 static void
11998 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
11999 {
12000   tree decl = block_ultimate_origin (stmt);
12001
12002   /* Emit info for the abstract instance first, if we haven't yet.  We
12003      must emit this even if the block is abstract, otherwise when we
12004      emit the block below (or elsewhere), we may end up trying to emit
12005      a die whose origin die hasn't been emitted, and crashing.  */
12006   dwarf2out_abstract_function (decl);
12007
12008   if (! BLOCK_ABSTRACT (stmt))
12009     {
12010       dw_die_ref subr_die
12011         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12012
12013       add_abstract_origin_attribute (subr_die, decl);
12014       add_high_low_attributes (stmt, subr_die);
12015       add_call_src_coords_attributes (stmt, subr_die);
12016
12017       decls_for_scope (stmt, subr_die, depth);
12018       current_function_has_inlines = 1;
12019     }
12020   else
12021     /* We may get here if we're the outer block of function A that was
12022        inlined into function B that was inlined into function C.  When
12023        generating debugging info for C, dwarf2out_abstract_function(B)
12024        would mark all inlined blocks as abstract, including this one.
12025        So, we wouldn't (and shouldn't) expect labels to be generated
12026        for this one.  Instead, just emit debugging info for
12027        declarations within the block.  This is particularly important
12028        in the case of initializers of arguments passed from B to us:
12029        if they're statement expressions containing declarations, we
12030        wouldn't generate dies for their abstract variables, and then,
12031        when generating dies for the real variables, we'd die (pun
12032        intended :-)  */
12033     gen_lexical_block_die (stmt, context_die, depth);
12034 }
12035
12036 /* Generate a DIE for a field in a record, or structure.  */
12037
12038 static void
12039 gen_field_die (tree decl, dw_die_ref context_die)
12040 {
12041   dw_die_ref decl_die;
12042
12043   if (TREE_TYPE (decl) == error_mark_node)
12044     return;
12045
12046   decl_die = new_die (DW_TAG_member, context_die, decl);
12047   add_name_and_src_coords_attributes (decl_die, decl);
12048   add_type_attribute (decl_die, member_declared_type (decl),
12049                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12050                       context_die);
12051
12052   if (DECL_BIT_FIELD_TYPE (decl))
12053     {
12054       add_byte_size_attribute (decl_die, decl);
12055       add_bit_size_attribute (decl_die, decl);
12056       add_bit_offset_attribute (decl_die, decl);
12057     }
12058
12059   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12060     add_data_member_location_attribute (decl_die, decl);
12061
12062   if (DECL_ARTIFICIAL (decl))
12063     add_AT_flag (decl_die, DW_AT_artificial, 1);
12064
12065   if (TREE_PROTECTED (decl))
12066     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12067   else if (TREE_PRIVATE (decl))
12068     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12069
12070   /* Equate decl number to die, so that we can look up this decl later on.  */
12071   equate_decl_number_to_die (decl, decl_die);
12072 }
12073
12074 #if 0
12075 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12076    Use modified_type_die instead.
12077    We keep this code here just in case these types of DIEs may be needed to
12078    represent certain things in other languages (e.g. Pascal) someday.  */
12079
12080 static void
12081 gen_pointer_type_die (tree type, dw_die_ref context_die)
12082 {
12083   dw_die_ref ptr_die
12084     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12085
12086   equate_type_number_to_die (type, ptr_die);
12087   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12088   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12089 }
12090
12091 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12092    Use modified_type_die instead.
12093    We keep this code here just in case these types of DIEs may be needed to
12094    represent certain things in other languages (e.g. Pascal) someday.  */
12095
12096 static void
12097 gen_reference_type_die (tree type, dw_die_ref context_die)
12098 {
12099   dw_die_ref ref_die
12100     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12101
12102   equate_type_number_to_die (type, ref_die);
12103   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12104   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12105 }
12106 #endif
12107
12108 /* Generate a DIE for a pointer to a member type.  */
12109
12110 static void
12111 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12112 {
12113   dw_die_ref ptr_die
12114     = new_die (DW_TAG_ptr_to_member_type,
12115                scope_die_for (type, context_die), type);
12116
12117   equate_type_number_to_die (type, ptr_die);
12118   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12119                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12120   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12121 }
12122
12123 /* Generate the DIE for the compilation unit.  */
12124
12125 static dw_die_ref
12126 gen_compile_unit_die (const char *filename)
12127 {
12128   dw_die_ref die;
12129   char producer[250];
12130   const char *language_string = lang_hooks.name;
12131   int language;
12132
12133   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12134
12135   if (filename)
12136     {
12137       add_name_attribute (die, filename);
12138       /* Don't add cwd for <built-in>.  */
12139       if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12140         add_comp_dir_attribute (die);
12141     }
12142
12143   sprintf (producer, "%s %s", language_string, version_string);
12144
12145 #ifdef MIPS_DEBUGGING_INFO
12146   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12147      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12148      not appear in the producer string, the debugger reaches the conclusion
12149      that the object file is stripped and has no debugging information.
12150      To get the MIPS/SGI debugger to believe that there is debugging
12151      information in the object file, we add a -g to the producer string.  */
12152   if (debug_info_level > DINFO_LEVEL_TERSE)
12153     strcat (producer, " -g");
12154 #endif
12155
12156   add_AT_string (die, DW_AT_producer, producer);
12157
12158   if (strcmp (language_string, "GNU C++") == 0)
12159     language = DW_LANG_C_plus_plus;
12160   else if (strcmp (language_string, "GNU Ada") == 0)
12161     language = DW_LANG_Ada95;
12162   else if (strcmp (language_string, "GNU F77") == 0)
12163     language = DW_LANG_Fortran77;
12164   else if (strcmp (language_string, "GNU F95") == 0)
12165     language = DW_LANG_Fortran95;
12166   else if (strcmp (language_string, "GNU Pascal") == 0)
12167     language = DW_LANG_Pascal83;
12168   else if (strcmp (language_string, "GNU Java") == 0)
12169     language = DW_LANG_Java;
12170   else
12171     language = DW_LANG_C89;
12172
12173   add_AT_unsigned (die, DW_AT_language, language);
12174   return die;
12175 }
12176
12177 /* Generate the DIE for a base class.  */
12178
12179 static void
12180 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12181 {
12182   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12183
12184   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12185   add_data_member_location_attribute (die, binfo);
12186
12187   if (BINFO_VIRTUAL_P (binfo))
12188     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12189
12190   if (access == access_public_node)
12191     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12192   else if (access == access_protected_node)
12193     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12194 }
12195
12196 /* Generate a DIE for a class member.  */
12197
12198 static void
12199 gen_member_die (tree type, dw_die_ref context_die)
12200 {
12201   tree member;
12202   tree binfo = TYPE_BINFO (type);
12203   dw_die_ref child;
12204
12205   /* If this is not an incomplete type, output descriptions of each of its
12206      members. Note that as we output the DIEs necessary to represent the
12207      members of this record or union type, we will also be trying to output
12208      DIEs to represent the *types* of those members. However the `type'
12209      function (above) will specifically avoid generating type DIEs for member
12210      types *within* the list of member DIEs for this (containing) type except
12211      for those types (of members) which are explicitly marked as also being
12212      members of this (containing) type themselves.  The g++ front- end can
12213      force any given type to be treated as a member of some other (containing)
12214      type by setting the TYPE_CONTEXT of the given (member) type to point to
12215      the TREE node representing the appropriate (containing) type.  */
12216
12217   /* First output info about the base classes.  */
12218   if (binfo)
12219     {
12220       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12221       int i;
12222       tree base;
12223
12224       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12225         gen_inheritance_die (base,
12226                              (accesses ? VEC_index (tree, accesses, i)
12227                               : access_public_node), context_die);
12228     }
12229
12230   /* Now output info about the data members and type members.  */
12231   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12232     {
12233       /* If we thought we were generating minimal debug info for TYPE
12234          and then changed our minds, some of the member declarations
12235          may have already been defined.  Don't define them again, but
12236          do put them in the right order.  */
12237
12238       child = lookup_decl_die (member);
12239       if (child)
12240         splice_child_die (context_die, child);
12241       else
12242         gen_decl_die (member, context_die);
12243     }
12244
12245   /* Now output info about the function members (if any).  */
12246   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12247     {
12248       /* Don't include clones in the member list.  */
12249       if (DECL_ABSTRACT_ORIGIN (member))
12250         continue;
12251
12252       child = lookup_decl_die (member);
12253       if (child)
12254         splice_child_die (context_die, child);
12255       else
12256         gen_decl_die (member, context_die);
12257     }
12258 }
12259
12260 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12261    is set, we pretend that the type was never defined, so we only get the
12262    member DIEs needed by later specification DIEs.  */
12263
12264 static void
12265 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12266 {
12267   dw_die_ref type_die = lookup_type_die (type);
12268   dw_die_ref scope_die = 0;
12269   int nested = 0;
12270   int complete = (TYPE_SIZE (type)
12271                   && (! TYPE_STUB_DECL (type)
12272                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12273   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12274
12275   if (type_die && ! complete)
12276     return;
12277
12278   if (TYPE_CONTEXT (type) != NULL_TREE
12279       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12280           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12281     nested = 1;
12282
12283   scope_die = scope_die_for (type, context_die);
12284
12285   if (! type_die || (nested && scope_die == comp_unit_die))
12286     /* First occurrence of type or toplevel definition of nested class.  */
12287     {
12288       dw_die_ref old_die = type_die;
12289
12290       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12291                           ? DW_TAG_structure_type : DW_TAG_union_type,
12292                           scope_die, type);
12293       equate_type_number_to_die (type, type_die);
12294       if (old_die)
12295         add_AT_specification (type_die, old_die);
12296       else
12297         add_name_attribute (type_die, type_tag (type));
12298     }
12299   else
12300     remove_AT (type_die, DW_AT_declaration);
12301
12302   /* If this type has been completed, then give it a byte_size attribute and
12303      then give a list of members.  */
12304   if (complete && !ns_decl)
12305     {
12306       /* Prevent infinite recursion in cases where the type of some member of
12307          this type is expressed in terms of this type itself.  */
12308       TREE_ASM_WRITTEN (type) = 1;
12309       add_byte_size_attribute (type_die, type);
12310       if (TYPE_STUB_DECL (type) != NULL_TREE)
12311         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12312
12313       /* If the first reference to this type was as the return type of an
12314          inline function, then it may not have a parent.  Fix this now.  */
12315       if (type_die->die_parent == NULL)
12316         add_child_die (scope_die, type_die);
12317
12318       push_decl_scope (type);
12319       gen_member_die (type, type_die);
12320       pop_decl_scope ();
12321
12322       /* GNU extension: Record what type our vtable lives in.  */
12323       if (TYPE_VFIELD (type))
12324         {
12325           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12326
12327           gen_type_die (vtype, context_die);
12328           add_AT_die_ref (type_die, DW_AT_containing_type,
12329                           lookup_type_die (vtype));
12330         }
12331     }
12332   else
12333     {
12334       add_AT_flag (type_die, DW_AT_declaration, 1);
12335
12336       /* We don't need to do this for function-local types.  */
12337       if (TYPE_STUB_DECL (type)
12338           && ! decl_function_context (TYPE_STUB_DECL (type)))
12339         VEC_safe_push (tree, gc, incomplete_types, type);
12340     }
12341 }
12342
12343 /* Generate a DIE for a subroutine _type_.  */
12344
12345 static void
12346 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12347 {
12348   tree return_type = TREE_TYPE (type);
12349   dw_die_ref subr_die
12350     = new_die (DW_TAG_subroutine_type,
12351                scope_die_for (type, context_die), type);
12352
12353   equate_type_number_to_die (type, subr_die);
12354   add_prototyped_attribute (subr_die, type);
12355   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12356   gen_formal_types_die (type, subr_die);
12357 }
12358
12359 /* Generate a DIE for a type definition.  */
12360
12361 static void
12362 gen_typedef_die (tree decl, dw_die_ref context_die)
12363 {
12364   dw_die_ref type_die;
12365   tree origin;
12366
12367   if (TREE_ASM_WRITTEN (decl))
12368     return;
12369
12370   TREE_ASM_WRITTEN (decl) = 1;
12371   type_die = new_die (DW_TAG_typedef, context_die, decl);
12372   origin = decl_ultimate_origin (decl);
12373   if (origin != NULL)
12374     add_abstract_origin_attribute (type_die, origin);
12375   else
12376     {
12377       tree type;
12378
12379       add_name_and_src_coords_attributes (type_die, decl);
12380       if (DECL_ORIGINAL_TYPE (decl))
12381         {
12382           type = DECL_ORIGINAL_TYPE (decl);
12383
12384           gcc_assert (type != TREE_TYPE (decl));
12385           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12386         }
12387       else
12388         type = TREE_TYPE (decl);
12389
12390       add_type_attribute (type_die, type, TREE_READONLY (decl),
12391                           TREE_THIS_VOLATILE (decl), context_die);
12392     }
12393
12394   if (DECL_ABSTRACT (decl))
12395     equate_decl_number_to_die (decl, type_die);
12396 }
12397
12398 /* Generate a type description DIE.  */
12399
12400 static void
12401 gen_type_die (tree type, dw_die_ref context_die)
12402 {
12403   int need_pop;
12404
12405   if (type == NULL_TREE || type == error_mark_node)
12406     return;
12407
12408   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12409       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12410     {
12411       if (TREE_ASM_WRITTEN (type))
12412         return;
12413
12414       /* Prevent broken recursion; we can't hand off to the same type.  */
12415       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12416
12417       TREE_ASM_WRITTEN (type) = 1;
12418       gen_decl_die (TYPE_NAME (type), context_die);
12419       return;
12420     }
12421
12422   /* We are going to output a DIE to represent the unqualified version
12423      of this type (i.e. without any const or volatile qualifiers) so
12424      get the main variant (i.e. the unqualified version) of this type
12425      now.  (Vectors are special because the debugging info is in the
12426      cloned type itself).  */
12427   if (TREE_CODE (type) != VECTOR_TYPE)
12428     type = type_main_variant (type);
12429
12430   if (TREE_ASM_WRITTEN (type))
12431     return;
12432
12433   switch (TREE_CODE (type))
12434     {
12435     case ERROR_MARK:
12436       break;
12437
12438     case POINTER_TYPE:
12439     case REFERENCE_TYPE:
12440       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12441          ensures that the gen_type_die recursion will terminate even if the
12442          type is recursive.  Recursive types are possible in Ada.  */
12443       /* ??? We could perhaps do this for all types before the switch
12444          statement.  */
12445       TREE_ASM_WRITTEN (type) = 1;
12446
12447       /* For these types, all that is required is that we output a DIE (or a
12448          set of DIEs) to represent the "basis" type.  */
12449       gen_type_die (TREE_TYPE (type), context_die);
12450       break;
12451
12452     case OFFSET_TYPE:
12453       /* This code is used for C++ pointer-to-data-member types.
12454          Output a description of the relevant class type.  */
12455       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12456
12457       /* Output a description of the type of the object pointed to.  */
12458       gen_type_die (TREE_TYPE (type), context_die);
12459
12460       /* Now output a DIE to represent this pointer-to-data-member type
12461          itself.  */
12462       gen_ptr_to_mbr_type_die (type, context_die);
12463       break;
12464
12465     case FUNCTION_TYPE:
12466       /* Force out return type (in case it wasn't forced out already).  */
12467       gen_type_die (TREE_TYPE (type), context_die);
12468       gen_subroutine_type_die (type, context_die);
12469       break;
12470
12471     case METHOD_TYPE:
12472       /* Force out return type (in case it wasn't forced out already).  */
12473       gen_type_die (TREE_TYPE (type), context_die);
12474       gen_subroutine_type_die (type, context_die);
12475       break;
12476
12477     case ARRAY_TYPE:
12478       gen_array_type_die (type, context_die);
12479       break;
12480
12481     case VECTOR_TYPE:
12482       gen_array_type_die (type, context_die);
12483       break;
12484
12485     case ENUMERAL_TYPE:
12486     case RECORD_TYPE:
12487     case UNION_TYPE:
12488     case QUAL_UNION_TYPE:
12489       /* If this is a nested type whose containing class hasn't been written
12490          out yet, writing it out will cover this one, too.  This does not apply
12491          to instantiations of member class templates; they need to be added to
12492          the containing class as they are generated.  FIXME: This hurts the
12493          idea of combining type decls from multiple TUs, since we can't predict
12494          what set of template instantiations we'll get.  */
12495       if (TYPE_CONTEXT (type)
12496           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12497           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12498         {
12499           gen_type_die (TYPE_CONTEXT (type), context_die);
12500
12501           if (TREE_ASM_WRITTEN (type))
12502             return;
12503
12504           /* If that failed, attach ourselves to the stub.  */
12505           push_decl_scope (TYPE_CONTEXT (type));
12506           context_die = lookup_type_die (TYPE_CONTEXT (type));
12507           need_pop = 1;
12508         }
12509       else
12510         {
12511           declare_in_namespace (type, context_die);
12512           need_pop = 0;
12513         }
12514
12515       if (TREE_CODE (type) == ENUMERAL_TYPE)
12516         gen_enumeration_type_die (type, context_die);
12517       else
12518         gen_struct_or_union_type_die (type, context_die);
12519
12520       if (need_pop)
12521         pop_decl_scope ();
12522
12523       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12524          it up if it is ever completed.  gen_*_type_die will set it for us
12525          when appropriate.  */
12526       return;
12527
12528     case VOID_TYPE:
12529     case INTEGER_TYPE:
12530     case REAL_TYPE:
12531     case COMPLEX_TYPE:
12532     case BOOLEAN_TYPE:
12533       /* No DIEs needed for fundamental types.  */
12534       break;
12535
12536     case LANG_TYPE:
12537       /* No Dwarf representation currently defined.  */
12538       break;
12539
12540     default:
12541       gcc_unreachable ();
12542     }
12543
12544   TREE_ASM_WRITTEN (type) = 1;
12545 }
12546
12547 /* Generate a DIE for a tagged type instantiation.  */
12548
12549 static void
12550 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12551 {
12552   if (type == NULL_TREE || type == error_mark_node)
12553     return;
12554
12555   /* We are going to output a DIE to represent the unqualified version of
12556      this type (i.e. without any const or volatile qualifiers) so make sure
12557      that we have the main variant (i.e. the unqualified version) of this
12558      type now.  */
12559   gcc_assert (type == type_main_variant (type));
12560
12561   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12562      an instance of an unresolved type.  */
12563
12564   switch (TREE_CODE (type))
12565     {
12566     case ERROR_MARK:
12567       break;
12568
12569     case ENUMERAL_TYPE:
12570       gen_inlined_enumeration_type_die (type, context_die);
12571       break;
12572
12573     case RECORD_TYPE:
12574       gen_inlined_structure_type_die (type, context_die);
12575       break;
12576
12577     case UNION_TYPE:
12578     case QUAL_UNION_TYPE:
12579       gen_inlined_union_type_die (type, context_die);
12580       break;
12581
12582     default:
12583       gcc_unreachable ();
12584     }
12585 }
12586
12587 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12588    things which are local to the given block.  */
12589
12590 static void
12591 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12592 {
12593   int must_output_die = 0;
12594   tree origin;
12595   tree decl;
12596   enum tree_code origin_code;
12597
12598   /* Ignore blocks that are NULL.  */
12599   if (stmt == NULL_TREE)
12600     return;
12601
12602   /* If the block is one fragment of a non-contiguous block, do not
12603      process the variables, since they will have been done by the
12604      origin block.  Do process subblocks.  */
12605   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12606     {
12607       tree sub;
12608
12609       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12610         gen_block_die (sub, context_die, depth + 1);
12611
12612       return;
12613     }
12614
12615   /* Determine the "ultimate origin" of this block.  This block may be an
12616      inlined instance of an inlined instance of inline function, so we have
12617      to trace all of the way back through the origin chain to find out what
12618      sort of node actually served as the original seed for the creation of
12619      the current block.  */
12620   origin = block_ultimate_origin (stmt);
12621   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12622
12623   /* Determine if we need to output any Dwarf DIEs at all to represent this
12624      block.  */
12625   if (origin_code == FUNCTION_DECL)
12626     /* The outer scopes for inlinings *must* always be represented.  We
12627        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12628     must_output_die = 1;
12629   else
12630     {
12631       /* In the case where the current block represents an inlining of the
12632          "body block" of an inline function, we must *NOT* output any DIE for
12633          this block because we have already output a DIE to represent the whole
12634          inlined function scope and the "body block" of any function doesn't
12635          really represent a different scope according to ANSI C rules.  So we
12636          check here to make sure that this block does not represent a "body
12637          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12638       if (! is_body_block (origin ? origin : stmt))
12639         {
12640           /* Determine if this block directly contains any "significant"
12641              local declarations which we will need to output DIEs for.  */
12642           if (debug_info_level > DINFO_LEVEL_TERSE)
12643             /* We are not in terse mode so *any* local declaration counts
12644                as being a "significant" one.  */
12645             must_output_die = (BLOCK_VARS (stmt) != NULL 
12646                                && (TREE_USED (stmt) 
12647                                    || TREE_ASM_WRITTEN (stmt)
12648                                    || BLOCK_ABSTRACT (stmt)));
12649           else
12650             /* We are in terse mode, so only local (nested) function
12651                definitions count as "significant" local declarations.  */
12652             for (decl = BLOCK_VARS (stmt);
12653                  decl != NULL; decl = TREE_CHAIN (decl))
12654               if (TREE_CODE (decl) == FUNCTION_DECL
12655                   && DECL_INITIAL (decl))
12656                 {
12657                   must_output_die = 1;
12658                   break;
12659                 }
12660         }
12661     }
12662
12663   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12664      DIE for any block which contains no significant local declarations at
12665      all.  Rather, in such cases we just call `decls_for_scope' so that any
12666      needed Dwarf info for any sub-blocks will get properly generated. Note
12667      that in terse mode, our definition of what constitutes a "significant"
12668      local declaration gets restricted to include only inlined function
12669      instances and local (nested) function definitions.  */
12670   if (must_output_die)
12671     {
12672       if (origin_code == FUNCTION_DECL)
12673         gen_inlined_subroutine_die (stmt, context_die, depth);
12674       else
12675         gen_lexical_block_die (stmt, context_die, depth);
12676     }
12677   else
12678     decls_for_scope (stmt, context_die, depth);
12679 }
12680
12681 /* Generate all of the decls declared within a given scope and (recursively)
12682    all of its sub-blocks.  */
12683
12684 static void
12685 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12686 {
12687   tree decl;
12688   tree subblocks;
12689
12690   /* Ignore NULL blocks.  */
12691   if (stmt == NULL_TREE)
12692     return;
12693
12694   if (TREE_USED (stmt))
12695     {
12696       /* Output the DIEs to represent all of the data objects and typedefs
12697          declared directly within this block but not within any nested
12698          sub-blocks.  Also, nested function and tag DIEs have been
12699          generated with a parent of NULL; fix that up now.  */
12700       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12701         {
12702           dw_die_ref die;
12703           
12704           if (TREE_CODE (decl) == FUNCTION_DECL)
12705             die = lookup_decl_die (decl);
12706           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12707             die = lookup_type_die (TREE_TYPE (decl));
12708           else
12709             die = NULL;
12710           
12711           if (die != NULL && die->die_parent == NULL)
12712             add_child_die (context_die, die);
12713           /* Do not produce debug information for static variables since
12714              these might be optimized out.  We are called for these later
12715              in cgraph_varpool_analyze_pending_decls. */
12716           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12717             ;
12718           else
12719             gen_decl_die (decl, context_die);
12720         }
12721     }
12722
12723   /* If we're at -g1, we're not interested in subblocks.  */
12724   if (debug_info_level <= DINFO_LEVEL_TERSE)
12725     return;
12726
12727   /* Output the DIEs to represent all sub-blocks (and the items declared
12728      therein) of this block.  */
12729   for (subblocks = BLOCK_SUBBLOCKS (stmt);
12730        subblocks != NULL;
12731        subblocks = BLOCK_CHAIN (subblocks))
12732     gen_block_die (subblocks, context_die, depth + 1);
12733 }
12734
12735 /* Is this a typedef we can avoid emitting?  */
12736
12737 static inline int
12738 is_redundant_typedef (tree decl)
12739 {
12740   if (TYPE_DECL_IS_STUB (decl))
12741     return 1;
12742
12743   if (DECL_ARTIFICIAL (decl)
12744       && DECL_CONTEXT (decl)
12745       && is_tagged_type (DECL_CONTEXT (decl))
12746       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12747       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12748     /* Also ignore the artificial member typedef for the class name.  */
12749     return 1;
12750
12751   return 0;
12752 }
12753
12754 /* Returns the DIE for decl.  A DIE will always be returned.  */
12755
12756 static dw_die_ref
12757 force_decl_die (tree decl)
12758 {
12759   dw_die_ref decl_die;
12760   unsigned saved_external_flag;
12761   tree save_fn = NULL_TREE;
12762   decl_die = lookup_decl_die (decl);
12763   if (!decl_die)
12764     {
12765       dw_die_ref context_die;
12766       tree decl_context = DECL_CONTEXT (decl);
12767       if (decl_context)
12768         {
12769           /* Find die that represents this context.  */
12770           if (TYPE_P (decl_context))
12771             context_die = force_type_die (decl_context);
12772           else
12773             context_die = force_decl_die (decl_context);
12774         }
12775       else
12776         context_die = comp_unit_die;
12777
12778       decl_die = lookup_decl_die (decl);
12779       if (decl_die)
12780         return decl_die;
12781
12782       switch (TREE_CODE (decl))
12783         {
12784         case FUNCTION_DECL:
12785           /* Clear current_function_decl, so that gen_subprogram_die thinks
12786              that this is a declaration. At this point, we just want to force
12787              declaration die.  */
12788           save_fn = current_function_decl;
12789           current_function_decl = NULL_TREE;
12790           gen_subprogram_die (decl, context_die);
12791           current_function_decl = save_fn;
12792           break;
12793
12794         case VAR_DECL:
12795           /* Set external flag to force declaration die. Restore it after
12796            gen_decl_die() call.  */
12797           saved_external_flag = DECL_EXTERNAL (decl);
12798           DECL_EXTERNAL (decl) = 1;
12799           gen_decl_die (decl, context_die);
12800           DECL_EXTERNAL (decl) = saved_external_flag;
12801           break;
12802
12803         case NAMESPACE_DECL:
12804           dwarf2out_decl (decl);
12805           break;
12806
12807         default:
12808           gcc_unreachable ();
12809         }
12810
12811       /* We should be able to find the DIE now.  */
12812       if (!decl_die)
12813         decl_die = lookup_decl_die (decl);
12814       gcc_assert (decl_die);
12815     }
12816
12817   return decl_die;
12818 }
12819
12820 /* Returns the DIE for TYPE.  A DIE is always returned.  */
12821
12822 static dw_die_ref
12823 force_type_die (tree type)
12824 {
12825   dw_die_ref type_die;
12826
12827   type_die = lookup_type_die (type);
12828   if (!type_die)
12829     {
12830       dw_die_ref context_die;
12831       if (TYPE_CONTEXT (type))
12832         {
12833           if (TYPE_P (TYPE_CONTEXT (type)))
12834             context_die = force_type_die (TYPE_CONTEXT (type));
12835           else
12836             context_die = force_decl_die (TYPE_CONTEXT (type));
12837         }
12838       else
12839         context_die = comp_unit_die;
12840
12841       type_die = lookup_type_die (type);
12842       if (type_die)
12843         return type_die;
12844       gen_type_die (type, context_die);
12845       type_die = lookup_type_die (type);
12846       gcc_assert (type_die);
12847     }
12848   return type_die;
12849 }
12850
12851 /* Force out any required namespaces to be able to output DECL,
12852    and return the new context_die for it, if it's changed.  */
12853
12854 static dw_die_ref
12855 setup_namespace_context (tree thing, dw_die_ref context_die)
12856 {
12857   tree context = (DECL_P (thing)
12858                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12859   if (context && TREE_CODE (context) == NAMESPACE_DECL)
12860     /* Force out the namespace.  */
12861     context_die = force_decl_die (context);
12862
12863   return context_die;
12864 }
12865
12866 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12867    type) within its namespace, if appropriate.
12868
12869    For compatibility with older debuggers, namespace DIEs only contain
12870    declarations; all definitions are emitted at CU scope.  */
12871
12872 static void
12873 declare_in_namespace (tree thing, dw_die_ref context_die)
12874 {
12875   dw_die_ref ns_context;
12876
12877   if (debug_info_level <= DINFO_LEVEL_TERSE)
12878     return;
12879
12880   /* If this decl is from an inlined function, then don't try to emit it in its
12881      namespace, as we will get confused.  It would have already been emitted
12882      when the abstract instance of the inline function was emitted anyways.  */
12883   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12884     return;
12885
12886   ns_context = setup_namespace_context (thing, context_die);
12887
12888   if (ns_context != context_die)
12889     {
12890       if (DECL_P (thing))
12891         gen_decl_die (thing, ns_context);
12892       else
12893         gen_type_die (thing, ns_context);
12894     }
12895 }
12896
12897 /* Generate a DIE for a namespace or namespace alias.  */
12898
12899 static void
12900 gen_namespace_die (tree decl)
12901 {
12902   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12903
12904   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12905      they are an alias of.  */
12906   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12907     {
12908       /* Output a real namespace.  */
12909       dw_die_ref namespace_die
12910         = new_die (DW_TAG_namespace, context_die, decl);
12911       add_name_and_src_coords_attributes (namespace_die, decl);
12912       equate_decl_number_to_die (decl, namespace_die);
12913     }
12914   else
12915     {
12916       /* Output a namespace alias.  */
12917
12918       /* Force out the namespace we are an alias of, if necessary.  */
12919       dw_die_ref origin_die
12920         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12921
12922       /* Now create the namespace alias DIE.  */
12923       dw_die_ref namespace_die
12924         = new_die (DW_TAG_imported_declaration, context_die, decl);
12925       add_name_and_src_coords_attributes (namespace_die, decl);
12926       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12927       equate_decl_number_to_die (decl, namespace_die);
12928     }
12929 }
12930
12931 /* Generate Dwarf debug information for a decl described by DECL.  */
12932
12933 static void
12934 gen_decl_die (tree decl, dw_die_ref context_die)
12935 {
12936   tree origin;
12937
12938   if (DECL_P (decl) && DECL_IGNORED_P (decl))
12939     return;
12940
12941   switch (TREE_CODE (decl))
12942     {
12943     case ERROR_MARK:
12944       break;
12945
12946     case CONST_DECL:
12947       /* The individual enumerators of an enum type get output when we output
12948          the Dwarf representation of the relevant enum type itself.  */
12949       break;
12950
12951     case FUNCTION_DECL:
12952       /* Don't output any DIEs to represent mere function declarations,
12953          unless they are class members or explicit block externs.  */
12954       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12955           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12956         break;
12957
12958 #if 0
12959       /* FIXME */
12960       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
12961          on local redeclarations of global functions.  That seems broken.  */
12962       if (current_function_decl != decl)
12963         /* This is only a declaration.  */;
12964 #endif
12965
12966       /* If we're emitting a clone, emit info for the abstract instance.  */
12967       if (DECL_ORIGIN (decl) != decl)
12968         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
12969
12970       /* If we're emitting an out-of-line copy of an inline function,
12971          emit info for the abstract instance and set up to refer to it.  */
12972       else if (cgraph_function_possibly_inlined_p (decl)
12973                && ! DECL_ABSTRACT (decl)
12974                && ! class_or_namespace_scope_p (context_die)
12975                /* dwarf2out_abstract_function won't emit a die if this is just
12976                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
12977                   that case, because that works only if we have a die.  */
12978                && DECL_INITIAL (decl) != NULL_TREE)
12979         {
12980           dwarf2out_abstract_function (decl);
12981           set_decl_origin_self (decl);
12982         }
12983
12984       /* Otherwise we're emitting the primary DIE for this decl.  */
12985       else if (debug_info_level > DINFO_LEVEL_TERSE)
12986         {
12987           /* Before we describe the FUNCTION_DECL itself, make sure that we
12988              have described its return type.  */
12989           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
12990
12991           /* And its virtual context.  */
12992           if (DECL_VINDEX (decl) != NULL_TREE)
12993             gen_type_die (DECL_CONTEXT (decl), context_die);
12994
12995           /* And its containing type.  */
12996           origin = decl_class_context (decl);
12997           if (origin != NULL_TREE)
12998             gen_type_die_for_member (origin, decl, context_die);
12999
13000           /* And its containing namespace.  */
13001           declare_in_namespace (decl, context_die);
13002         }
13003
13004       /* Now output a DIE to represent the function itself.  */
13005       gen_subprogram_die (decl, context_die);
13006       break;
13007
13008     case TYPE_DECL:
13009       /* If we are in terse mode, don't generate any DIEs to represent any
13010          actual typedefs.  */
13011       if (debug_info_level <= DINFO_LEVEL_TERSE)
13012         break;
13013
13014       /* In the special case of a TYPE_DECL node representing the declaration
13015          of some type tag, if the given TYPE_DECL is marked as having been
13016          instantiated from some other (original) TYPE_DECL node (e.g. one which
13017          was generated within the original definition of an inline function) we
13018          have to generate a special (abbreviated) DW_TAG_structure_type,
13019          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13020       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13021         {
13022           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13023           break;
13024         }
13025
13026       if (is_redundant_typedef (decl))
13027         gen_type_die (TREE_TYPE (decl), context_die);
13028       else
13029         /* Output a DIE to represent the typedef itself.  */
13030         gen_typedef_die (decl, context_die);
13031       break;
13032
13033     case LABEL_DECL:
13034       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13035         gen_label_die (decl, context_die);
13036       break;
13037
13038     case VAR_DECL:
13039     case RESULT_DECL:
13040       /* If we are in terse mode, don't generate any DIEs to represent any
13041          variable declarations or definitions.  */
13042       if (debug_info_level <= DINFO_LEVEL_TERSE)
13043         break;
13044
13045       /* Output any DIEs that are needed to specify the type of this data
13046          object.  */
13047       gen_type_die (TREE_TYPE (decl), context_die);
13048
13049       /* And its containing type.  */
13050       origin = decl_class_context (decl);
13051       if (origin != NULL_TREE)
13052         gen_type_die_for_member (origin, decl, context_die);
13053
13054       /* And its containing namespace.  */
13055       declare_in_namespace (decl, context_die);
13056
13057       /* Now output the DIE to represent the data object itself.  This gets
13058          complicated because of the possibility that the VAR_DECL really
13059          represents an inlined instance of a formal parameter for an inline
13060          function.  */
13061       origin = decl_ultimate_origin (decl);
13062       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13063         gen_formal_parameter_die (decl, context_die);
13064       else
13065         gen_variable_die (decl, context_die);
13066       break;
13067
13068     case FIELD_DECL:
13069       /* Ignore the nameless fields that are used to skip bits but handle C++
13070          anonymous unions and structs.  */
13071       if (DECL_NAME (decl) != NULL_TREE
13072           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13073           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13074         {
13075           gen_type_die (member_declared_type (decl), context_die);
13076           gen_field_die (decl, context_die);
13077         }
13078       break;
13079
13080     case PARM_DECL:
13081       gen_type_die (TREE_TYPE (decl), context_die);
13082       gen_formal_parameter_die (decl, context_die);
13083       break;
13084
13085     case NAMESPACE_DECL:
13086       gen_namespace_die (decl);
13087       break;
13088
13089     default:
13090       /* Probably some frontend-internal decl.  Assume we don't care.  */
13091       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13092       break;
13093     }
13094 }
13095 \f
13096 /* Output debug information for global decl DECL.  Called from toplev.c after
13097    compilation proper has finished.  */
13098
13099 static void
13100 dwarf2out_global_decl (tree decl)
13101 {
13102   /* Output DWARF2 information for file-scope tentative data object
13103      declarations, file-scope (extern) function declarations (which had no
13104      corresponding body) and file-scope tagged type declarations and
13105      definitions which have not yet been forced out.  */
13106   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13107     dwarf2out_decl (decl);
13108 }
13109
13110 /* Output debug information for type decl DECL.  Called from toplev.c
13111    and from language front ends (to record built-in types).  */
13112 static void
13113 dwarf2out_type_decl (tree decl, int local)
13114 {
13115   if (!local)
13116     dwarf2out_decl (decl);
13117 }
13118
13119 /* Output debug information for imported module or decl.  */
13120
13121 static void
13122 dwarf2out_imported_module_or_decl (tree decl, tree context)
13123 {
13124   dw_die_ref imported_die, at_import_die;
13125   dw_die_ref scope_die;
13126   unsigned file_index;
13127   expanded_location xloc;
13128
13129   if (debug_info_level <= DINFO_LEVEL_TERSE)
13130     return;
13131
13132   gcc_assert (decl);
13133
13134   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13135      We need decl DIE for reference and scope die. First, get DIE for the decl
13136      itself.  */
13137
13138   /* Get the scope die for decl context. Use comp_unit_die for global module
13139      or decl. If die is not found for non globals, force new die.  */
13140   if (!context)
13141     scope_die = comp_unit_die;
13142   else if (TYPE_P (context))
13143     scope_die = force_type_die (context);
13144   else
13145     scope_die = force_decl_die (context);
13146
13147   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13148   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13149     at_import_die = force_type_die (TREE_TYPE (decl));
13150   else
13151     {
13152       at_import_die = lookup_decl_die (decl);
13153       if (!at_import_die)
13154         {
13155           /* If we're trying to avoid duplicate debug info, we may not have
13156              emitted the member decl for this field.  Emit it now.  */
13157           if (TREE_CODE (decl) == FIELD_DECL)
13158             {
13159               tree type = DECL_CONTEXT (decl);
13160               dw_die_ref type_context_die;
13161
13162               if (TYPE_CONTEXT (type))
13163                 if (TYPE_P (TYPE_CONTEXT (type)))
13164                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13165               else
13166                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13167               else
13168                 type_context_die = comp_unit_die;
13169               gen_type_die_for_member (type, decl, type_context_die);
13170             }
13171           at_import_die = force_decl_die (decl);
13172         }
13173     }
13174
13175   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13176   if (TREE_CODE (decl) == NAMESPACE_DECL)
13177     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13178   else
13179     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13180
13181   xloc = expand_location (input_location);
13182   file_index = lookup_filename (xloc.file);
13183   add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13184   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13185   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13186 }
13187
13188 /* Write the debugging output for DECL.  */
13189
13190 void
13191 dwarf2out_decl (tree decl)
13192 {
13193   dw_die_ref context_die = comp_unit_die;
13194
13195   switch (TREE_CODE (decl))
13196     {
13197     case ERROR_MARK:
13198       return;
13199
13200     case FUNCTION_DECL:
13201       /* What we would really like to do here is to filter out all mere
13202          file-scope declarations of file-scope functions which are never
13203          referenced later within this translation unit (and keep all of ones
13204          that *are* referenced later on) but we aren't clairvoyant, so we have
13205          no idea which functions will be referenced in the future (i.e. later
13206          on within the current translation unit). So here we just ignore all
13207          file-scope function declarations which are not also definitions.  If
13208          and when the debugger needs to know something about these functions,
13209          it will have to hunt around and find the DWARF information associated
13210          with the definition of the function.
13211
13212          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13213          nodes represent definitions and which ones represent mere
13214          declarations.  We have to check DECL_INITIAL instead. That's because
13215          the C front-end supports some weird semantics for "extern inline"
13216          function definitions.  These can get inlined within the current
13217          translation unit (and thus, we need to generate Dwarf info for their
13218          abstract instances so that the Dwarf info for the concrete inlined
13219          instances can have something to refer to) but the compiler never
13220          generates any out-of-lines instances of such things (despite the fact
13221          that they *are* definitions).
13222
13223          The important point is that the C front-end marks these "extern
13224          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13225          them anyway. Note that the C++ front-end also plays some similar games
13226          for inline function definitions appearing within include files which
13227          also contain `#pragma interface' pragmas.  */
13228       if (DECL_INITIAL (decl) == NULL_TREE)
13229         return;
13230
13231       /* If we're a nested function, initially use a parent of NULL; if we're
13232          a plain function, this will be fixed up in decls_for_scope.  If
13233          we're a method, it will be ignored, since we already have a DIE.  */
13234       if (decl_function_context (decl)
13235           /* But if we're in terse mode, we don't care about scope.  */
13236           && debug_info_level > DINFO_LEVEL_TERSE)
13237         context_die = NULL;
13238       break;
13239
13240     case VAR_DECL:
13241       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13242          declaration and if the declaration was never even referenced from
13243          within this entire compilation unit.  We suppress these DIEs in
13244          order to save space in the .debug section (by eliminating entries
13245          which are probably useless).  Note that we must not suppress
13246          block-local extern declarations (whether used or not) because that
13247          would screw-up the debugger's name lookup mechanism and cause it to
13248          miss things which really ought to be in scope at a given point.  */
13249       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13250         return;
13251
13252       /* For local statics lookup proper context die.  */
13253       if (TREE_STATIC (decl) && decl_function_context (decl))
13254         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13255
13256       /* If we are in terse mode, don't generate any DIEs to represent any
13257          variable declarations or definitions.  */
13258       if (debug_info_level <= DINFO_LEVEL_TERSE)
13259         return;
13260       break;
13261
13262     case NAMESPACE_DECL:
13263       if (debug_info_level <= DINFO_LEVEL_TERSE)
13264         return;
13265       if (lookup_decl_die (decl) != NULL)
13266         return;
13267       break;
13268
13269     case TYPE_DECL:
13270       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13271       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13272         return;
13273
13274       /* Don't bother trying to generate any DIEs to represent any of the
13275          normal built-in types for the language we are compiling.  */
13276       if (DECL_IS_BUILTIN (decl))
13277         {
13278           /* OK, we need to generate one for `bool' so GDB knows what type
13279              comparisons have.  */
13280           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13281                == DW_LANG_C_plus_plus)
13282               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13283               && ! DECL_IGNORED_P (decl))
13284             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13285
13286           return;
13287         }
13288
13289       /* If we are in terse mode, don't generate any DIEs for types.  */
13290       if (debug_info_level <= DINFO_LEVEL_TERSE)
13291         return;
13292
13293       /* If we're a function-scope tag, initially use a parent of NULL;
13294          this will be fixed up in decls_for_scope.  */
13295       if (decl_function_context (decl))
13296         context_die = NULL;
13297
13298       break;
13299
13300     default:
13301       return;
13302     }
13303
13304   gen_decl_die (decl, context_die);
13305 }
13306
13307 /* Output a marker (i.e. a label) for the beginning of the generated code for
13308    a lexical block.  */
13309
13310 static void
13311 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13312                        unsigned int blocknum)
13313 {
13314   switch_to_section (current_function_section ());
13315   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13316 }
13317
13318 /* Output a marker (i.e. a label) for the end of the generated code for a
13319    lexical block.  */
13320
13321 static void
13322 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13323 {
13324   switch_to_section (current_function_section ());
13325   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13326 }
13327
13328 /* Returns nonzero if it is appropriate not to emit any debugging
13329    information for BLOCK, because it doesn't contain any instructions.
13330
13331    Don't allow this for blocks with nested functions or local classes
13332    as we would end up with orphans, and in the presence of scheduling
13333    we may end up calling them anyway.  */
13334
13335 static bool
13336 dwarf2out_ignore_block (tree block)
13337 {
13338   tree decl;
13339
13340   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13341     if (TREE_CODE (decl) == FUNCTION_DECL
13342         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13343       return 0;
13344
13345   return 1;
13346 }
13347
13348 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13349    dwarf2out.c) and return its "index".  The index of each (known) filename is
13350    just a unique number which is associated with only that one filename.  We
13351    need such numbers for the sake of generating labels (in the .debug_sfnames
13352    section) and references to those files numbers (in the .debug_srcinfo
13353    and.debug_macinfo sections).  If the filename given as an argument is not
13354    found in our current list, add it to the list and assign it the next
13355    available unique index number.  In order to speed up searches, we remember
13356    the index of the filename was looked up last.  This handles the majority of
13357    all searches.  */
13358
13359 static unsigned
13360 lookup_filename (const char *file_name)
13361 {
13362   size_t i, n;
13363   char *save_file_name;
13364
13365   /* Check to see if the file name that was searched on the previous
13366      call matches this file name.  If so, return the index.  */
13367   if (file_table_last_lookup_index != 0)
13368     {
13369       const char *last
13370         = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13371       if (strcmp (file_name, last) == 0)
13372         return file_table_last_lookup_index;
13373     }
13374
13375   /* Didn't match the previous lookup, search the table.  */
13376   n = VARRAY_ACTIVE_SIZE (file_table);
13377   for (i = 1; i < n; i++)
13378     if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13379       {
13380         file_table_last_lookup_index = i;
13381         return i;
13382       }
13383
13384   /* Add the new entry to the end of the filename table.  */
13385   file_table_last_lookup_index = n;
13386   save_file_name = (char *) ggc_strdup (file_name);
13387   VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13388   VARRAY_PUSH_UINT (file_table_emitted, 0);
13389
13390   /* If the assembler is emitting the file table, and we aren't eliminating
13391      unused debug types, then we must emit .file here.  If we are eliminating
13392      unused debug types, then this will be done by the maybe_emit_file call in
13393      prune_unused_types_walk_attribs.  */
13394
13395   if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13396     return maybe_emit_file (i);
13397
13398   return i;
13399 }
13400
13401 /* If the assembler will construct the file table, then translate the compiler
13402    internal file table number into the assembler file table number, and emit
13403    a .file directive if we haven't already emitted one yet.  The file table
13404    numbers are different because we prune debug info for unused variables and
13405    types, which may include filenames.  */
13406
13407 static int
13408 maybe_emit_file (int fileno)
13409 {
13410   if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13411     {
13412       if (!VARRAY_UINT (file_table_emitted, fileno))
13413         {
13414           VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13415           fprintf (asm_out_file, "\t.file %u ",
13416                    VARRAY_UINT (file_table_emitted, fileno));
13417           output_quoted_string (asm_out_file,
13418                                 VARRAY_CHAR_PTR (file_table, fileno));
13419           fputc ('\n', asm_out_file);
13420         }
13421       return VARRAY_UINT (file_table_emitted, fileno);
13422     }
13423   else
13424     return fileno;
13425 }
13426
13427 /* Initialize the compiler internal file table.  */
13428
13429 static void
13430 init_file_table (void)
13431 {
13432   /* Allocate the initial hunk of the file_table.  */
13433   VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13434   VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13435
13436   /* Skip the first entry - file numbers begin at 1.  */
13437   VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13438   VARRAY_PUSH_UINT (file_table_emitted, 0);
13439   file_table_last_lookup_index = 0;
13440 }
13441
13442 /* Called by the final INSN scan whenever we see a var location.  We
13443    use it to drop labels in the right places, and throw the location in
13444    our lookup table.  */
13445
13446 static void
13447 dwarf2out_var_location (rtx loc_note)
13448 {
13449   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13450   struct var_loc_node *newloc;
13451   rtx prev_insn;
13452   static rtx last_insn;
13453   static const char *last_label;
13454   tree decl;
13455
13456   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13457     return;
13458   prev_insn = PREV_INSN (loc_note);
13459
13460   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13461   /* If the insn we processed last time is the previous insn
13462      and it is also a var location note, use the label we emitted
13463      last time.  */
13464   if (last_insn != NULL_RTX
13465       && last_insn == prev_insn
13466       && NOTE_P (prev_insn)
13467       && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13468     {
13469       newloc->label = last_label;
13470     }
13471   else
13472     {
13473       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13474       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13475       loclabel_num++;
13476       newloc->label = ggc_strdup (loclabel);
13477     }
13478   newloc->var_loc_note = loc_note;
13479   newloc->next = NULL;
13480
13481   if (cfun && in_cold_section_p)
13482     newloc->section_label = cfun->cold_section_label;
13483   else
13484     newloc->section_label = text_section_label;
13485
13486   last_insn = loc_note;
13487   last_label = newloc->label;
13488   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13489   if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl) 
13490       && DECL_P (DECL_DEBUG_EXPR (decl)))
13491     decl = DECL_DEBUG_EXPR (decl); 
13492   add_var_loc_to_decl (decl, newloc);
13493 }
13494
13495 /* We need to reset the locations at the beginning of each
13496    function. We can't do this in the end_function hook, because the
13497    declarations that use the locations won't have been output when
13498    that hook is called.  Also compute have_multiple_function_sections here.  */
13499
13500 static void
13501 dwarf2out_begin_function (tree fun)
13502 {
13503   htab_empty (decl_loc_table);
13504   
13505   if (function_section (fun) != text_section)
13506     have_multiple_function_sections = true;
13507 }
13508
13509 /* Output a label to mark the beginning of a source code line entry
13510    and record information relating to this source line, in
13511    'line_info_table' for later output of the .debug_line section.  */
13512
13513 static void
13514 dwarf2out_source_line (unsigned int line, const char *filename)
13515 {
13516   if (debug_info_level >= DINFO_LEVEL_NORMAL
13517       && line != 0)
13518     {
13519       switch_to_section (current_function_section ());
13520
13521       /* If requested, emit something human-readable.  */
13522       if (flag_debug_asm)
13523         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13524                  filename, line);
13525
13526       if (DWARF2_ASM_LINE_DEBUG_INFO)
13527         {
13528           unsigned file_num = lookup_filename (filename);
13529
13530           file_num = maybe_emit_file (file_num);
13531
13532           /* Emit the .loc directive understood by GNU as.  */
13533           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13534
13535           /* Indicate that line number info exists.  */
13536           line_info_table_in_use++;
13537         }
13538       else if (function_section (current_function_decl) != text_section)
13539         {
13540           dw_separate_line_info_ref line_info;
13541           targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13542                                      separate_line_info_table_in_use);
13543
13544           /* Expand the line info table if necessary.  */
13545           if (separate_line_info_table_in_use
13546               == separate_line_info_table_allocated)
13547             {
13548               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13549               separate_line_info_table
13550                 = ggc_realloc (separate_line_info_table,
13551                                separate_line_info_table_allocated
13552                                * sizeof (dw_separate_line_info_entry));
13553               memset (separate_line_info_table
13554                        + separate_line_info_table_in_use,
13555                       0,
13556                       (LINE_INFO_TABLE_INCREMENT
13557                        * sizeof (dw_separate_line_info_entry)));
13558             }
13559
13560           /* Add the new entry at the end of the line_info_table.  */
13561           line_info
13562             = &separate_line_info_table[separate_line_info_table_in_use++];
13563           line_info->dw_file_num = lookup_filename (filename);
13564           line_info->dw_line_num = line;
13565           line_info->function = current_function_funcdef_no;
13566         }
13567       else
13568         {
13569           dw_line_info_ref line_info;
13570
13571           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13572                                      line_info_table_in_use);
13573
13574           /* Expand the line info table if necessary.  */
13575           if (line_info_table_in_use == line_info_table_allocated)
13576             {
13577               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13578               line_info_table
13579                 = ggc_realloc (line_info_table,
13580                                (line_info_table_allocated
13581                                 * sizeof (dw_line_info_entry)));
13582               memset (line_info_table + line_info_table_in_use, 0,
13583                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13584             }
13585
13586           /* Add the new entry at the end of the line_info_table.  */
13587           line_info = &line_info_table[line_info_table_in_use++];
13588           line_info->dw_file_num = lookup_filename (filename);
13589           line_info->dw_line_num = line;
13590         }
13591     }
13592 }
13593
13594 /* Record the beginning of a new source file.  */
13595
13596 static void
13597 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13598 {
13599   if (flag_eliminate_dwarf2_dups)
13600     {
13601       /* Record the beginning of the file for break_out_includes.  */
13602       dw_die_ref bincl_die;
13603
13604       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13605       add_AT_string (bincl_die, DW_AT_name, filename);
13606     }
13607
13608   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13609     {
13610       int fileno;
13611
13612       switch_to_section (debug_macinfo_section);
13613       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13614       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13615                                    lineno);
13616
13617       fileno = maybe_emit_file (lookup_filename (filename));
13618       dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13619     }
13620 }
13621
13622 /* Record the end of a source file.  */
13623
13624 static void
13625 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13626 {
13627   if (flag_eliminate_dwarf2_dups)
13628     /* Record the end of the file for break_out_includes.  */
13629     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13630
13631   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13632     {
13633       switch_to_section (debug_macinfo_section);
13634       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13635     }
13636 }
13637
13638 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
13639    the tail part of the directive line, i.e. the part which is past the
13640    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13641
13642 static void
13643 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13644                   const char *buffer ATTRIBUTE_UNUSED)
13645 {
13646   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13647     {
13648       switch_to_section (debug_macinfo_section);
13649       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13650       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13651       dw2_asm_output_nstring (buffer, -1, "The macro");
13652     }
13653 }
13654
13655 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13656    the tail part of the directive line, i.e. the part which is past the
13657    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13658
13659 static void
13660 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13661                  const char *buffer ATTRIBUTE_UNUSED)
13662 {
13663   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13664     {
13665       switch_to_section (debug_macinfo_section);
13666       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13667       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13668       dw2_asm_output_nstring (buffer, -1, "The macro");
13669     }
13670 }
13671
13672 /* Set up for Dwarf output at the start of compilation.  */
13673
13674 static void
13675 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13676 {
13677   init_file_table ();
13678
13679   /* Allocate the decl_die_table.  */
13680   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13681                                     decl_die_table_eq, NULL);
13682
13683   /* Allocate the decl_loc_table.  */
13684   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13685                                     decl_loc_table_eq, NULL);
13686
13687   /* Allocate the initial hunk of the decl_scope_table.  */
13688   decl_scope_table = VEC_alloc (tree, gc, 256);
13689
13690   /* Allocate the initial hunk of the abbrev_die_table.  */
13691   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13692                                         * sizeof (dw_die_ref));
13693   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13694   /* Zero-th entry is allocated, but unused.  */
13695   abbrev_die_table_in_use = 1;
13696
13697   /* Allocate the initial hunk of the line_info_table.  */
13698   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13699                                        * sizeof (dw_line_info_entry));
13700   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13701
13702   /* Zero-th entry is allocated, but unused.  */
13703   line_info_table_in_use = 1;
13704
13705   /* Generate the initial DIE for the .debug section.  Note that the (string)
13706      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13707      will (typically) be a relative pathname and that this pathname should be
13708      taken as being relative to the directory from which the compiler was
13709      invoked when the given (base) source file was compiled.  We will fill
13710      in this value in dwarf2out_finish.  */
13711   comp_unit_die = gen_compile_unit_die (NULL);
13712
13713   incomplete_types = VEC_alloc (tree, gc, 64);
13714
13715   used_rtx_array = VEC_alloc (rtx, gc, 32);
13716
13717   debug_info_section = get_section (DEBUG_INFO_SECTION,
13718                                     SECTION_DEBUG, NULL);
13719   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
13720                                       SECTION_DEBUG, NULL);
13721   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
13722                                        SECTION_DEBUG, NULL);
13723   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
13724                                        SECTION_DEBUG, NULL);
13725   debug_line_section = get_section (DEBUG_LINE_SECTION,
13726                                     SECTION_DEBUG, NULL);
13727   debug_loc_section = get_section (DEBUG_LOC_SECTION,
13728                                    SECTION_DEBUG, NULL);
13729   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
13730                                         SECTION_DEBUG, NULL);
13731   debug_str_section = get_section (DEBUG_STR_SECTION,
13732                                    DEBUG_STR_SECTION_FLAGS, NULL);
13733   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
13734                                       SECTION_DEBUG, NULL);
13735
13736   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13737   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13738                                DEBUG_ABBREV_SECTION_LABEL, 0);
13739   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13740   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label, 
13741                                COLD_TEXT_SECTION_LABEL, 0);
13742   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13743
13744   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13745                                DEBUG_INFO_SECTION_LABEL, 0);
13746   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13747                                DEBUG_LINE_SECTION_LABEL, 0);
13748   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13749                                DEBUG_RANGES_SECTION_LABEL, 0);
13750   switch_to_section (debug_abbrev_section);
13751   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13752   switch_to_section (debug_info_section);
13753   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13754   switch_to_section (debug_line_section);
13755   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13756
13757   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13758     {
13759       switch_to_section (debug_macinfo_section);
13760       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13761                                    DEBUG_MACINFO_SECTION_LABEL, 0);
13762       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13763     }
13764
13765   switch_to_section (text_section);
13766   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13767   if (flag_reorder_blocks_and_partition)
13768     {
13769       switch_to_section (unlikely_text_section ());
13770       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13771     }
13772 }
13773
13774 /* A helper function for dwarf2out_finish called through
13775    ht_forall.  Emit one queued .debug_str string.  */
13776
13777 static int
13778 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13779 {
13780   struct indirect_string_node *node = (struct indirect_string_node *) *h;
13781
13782   if (node->form == DW_FORM_strp)
13783     {
13784       switch_to_section (debug_str_section);
13785       ASM_OUTPUT_LABEL (asm_out_file, node->label);
13786       assemble_string (node->str, strlen (node->str) + 1);
13787     }
13788
13789   return 1;
13790 }
13791
13792
13793
13794 /* Clear the marks for a die and its children.
13795    Be cool if the mark isn't set.  */
13796
13797 static void
13798 prune_unmark_dies (dw_die_ref die)
13799 {
13800   dw_die_ref c;
13801   die->die_mark = 0;
13802   for (c = die->die_child; c; c = c->die_sib)
13803     prune_unmark_dies (c);
13804 }
13805
13806
13807 /* Given DIE that we're marking as used, find any other dies
13808    it references as attributes and mark them as used.  */
13809
13810 static void
13811 prune_unused_types_walk_attribs (dw_die_ref die)
13812 {
13813   dw_attr_ref a;
13814
13815   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13816     {
13817       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13818         {
13819           /* A reference to another DIE.
13820              Make sure that it will get emitted.  */
13821           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13822         }
13823       else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13824         {
13825           /* A reference to a file.  Make sure the file name is emitted.  */
13826           a->dw_attr_val.v.val_unsigned =
13827             maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13828         }
13829     }
13830 }
13831
13832
13833 /* Mark DIE as being used.  If DOKIDS is true, then walk down
13834    to DIE's children.  */
13835
13836 static void
13837 prune_unused_types_mark (dw_die_ref die, int dokids)
13838 {
13839   dw_die_ref c;
13840
13841   if (die->die_mark == 0)
13842     {
13843       /* We haven't done this node yet.  Mark it as used.  */
13844       die->die_mark = 1;
13845
13846       /* We also have to mark its parents as used.
13847          (But we don't want to mark our parents' kids due to this.)  */
13848       if (die->die_parent)
13849         prune_unused_types_mark (die->die_parent, 0);
13850
13851       /* Mark any referenced nodes.  */
13852       prune_unused_types_walk_attribs (die);
13853
13854       /* If this node is a specification,
13855          also mark the definition, if it exists.  */
13856       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13857         prune_unused_types_mark (die->die_definition, 1);
13858     }
13859
13860   if (dokids && die->die_mark != 2)
13861     {
13862       /* We need to walk the children, but haven't done so yet.
13863          Remember that we've walked the kids.  */
13864       die->die_mark = 2;
13865
13866       /* Walk them.  */
13867       for (c = die->die_child; c; c = c->die_sib)
13868         {
13869           /* If this is an array type, we need to make sure our
13870              kids get marked, even if they're types.  */
13871           if (die->die_tag == DW_TAG_array_type)
13872             prune_unused_types_mark (c, 1);
13873           else
13874             prune_unused_types_walk (c);
13875         }
13876     }
13877 }
13878
13879
13880 /* Walk the tree DIE and mark types that we actually use.  */
13881
13882 static void
13883 prune_unused_types_walk (dw_die_ref die)
13884 {
13885   dw_die_ref c;
13886
13887   /* Don't do anything if this node is already marked.  */
13888   if (die->die_mark)
13889     return;
13890
13891   switch (die->die_tag) {
13892   case DW_TAG_const_type:
13893   case DW_TAG_packed_type:
13894   case DW_TAG_pointer_type:
13895   case DW_TAG_reference_type:
13896   case DW_TAG_volatile_type:
13897   case DW_TAG_typedef:
13898   case DW_TAG_array_type:
13899   case DW_TAG_structure_type:
13900   case DW_TAG_union_type:
13901   case DW_TAG_class_type:
13902   case DW_TAG_friend:
13903   case DW_TAG_variant_part:
13904   case DW_TAG_enumeration_type:
13905   case DW_TAG_subroutine_type:
13906   case DW_TAG_string_type:
13907   case DW_TAG_set_type:
13908   case DW_TAG_subrange_type:
13909   case DW_TAG_ptr_to_member_type:
13910   case DW_TAG_file_type:
13911     /* It's a type node --- don't mark it.  */
13912     return;
13913
13914   default:
13915     /* Mark everything else.  */
13916     break;
13917   }
13918
13919   die->die_mark = 1;
13920
13921   /* Now, mark any dies referenced from here.  */
13922   prune_unused_types_walk_attribs (die);
13923
13924   /* Mark children.  */
13925   for (c = die->die_child; c; c = c->die_sib)
13926     prune_unused_types_walk (c);
13927 }
13928
13929
13930 /* Remove from the tree DIE any dies that aren't marked.  */
13931
13932 static void
13933 prune_unused_types_prune (dw_die_ref die)
13934 {
13935   dw_die_ref c, p, n;
13936
13937   gcc_assert (die->die_mark);
13938
13939   p = NULL;
13940   for (c = die->die_child; c; c = n)
13941     {
13942       n = c->die_sib;
13943       if (c->die_mark)
13944         {
13945           prune_unused_types_prune (c);
13946           p = c;
13947         }
13948       else
13949         {
13950           if (p)
13951             p->die_sib = n;
13952           else
13953             die->die_child = n;
13954           free_die (c);
13955         }
13956     }
13957 }
13958
13959
13960 /* Remove dies representing declarations that we never use.  */
13961
13962 static void
13963 prune_unused_types (void)
13964 {
13965   unsigned int i;
13966   limbo_die_node *node;
13967
13968   /* Clear all the marks.  */
13969   prune_unmark_dies (comp_unit_die);
13970   for (node = limbo_die_list; node; node = node->next)
13971     prune_unmark_dies (node->die);
13972
13973   /* Set the mark on nodes that are actually used.  */
13974   prune_unused_types_walk (comp_unit_die);
13975   for (node = limbo_die_list; node; node = node->next)
13976     prune_unused_types_walk (node->die);
13977
13978   /* Also set the mark on nodes referenced from the
13979      pubname_table or arange_table.  */
13980   for (i = 0; i < pubname_table_in_use; i++)
13981     prune_unused_types_mark (pubname_table[i].die, 1);
13982   for (i = 0; i < arange_table_in_use; i++)
13983     prune_unused_types_mark (arange_table[i], 1);
13984
13985   /* Get rid of nodes that aren't marked.  */
13986   prune_unused_types_prune (comp_unit_die);
13987   for (node = limbo_die_list; node; node = node->next)
13988     prune_unused_types_prune (node->die);
13989
13990   /* Leave the marks clear.  */
13991   prune_unmark_dies (comp_unit_die);
13992   for (node = limbo_die_list; node; node = node->next)
13993     prune_unmark_dies (node->die);
13994 }
13995
13996 /* Output stuff that dwarf requires at the end of every file,
13997    and generate the DWARF-2 debugging info.  */
13998
13999 static void
14000 dwarf2out_finish (const char *filename)
14001 {
14002   limbo_die_node *node, *next_node;
14003   dw_die_ref die = 0;
14004
14005   /* Add the name for the main input file now.  We delayed this from
14006      dwarf2out_init to avoid complications with PCH.  */
14007   add_name_attribute (comp_unit_die, filename);
14008   if (filename[0] != DIR_SEPARATOR)
14009     add_comp_dir_attribute (comp_unit_die);
14010   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14011     {
14012       size_t i;
14013       for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14014         if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14015             /* Don't add cwd for <built-in>.  */
14016             && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14017           {
14018             add_comp_dir_attribute (comp_unit_die);
14019             break;
14020           }
14021     }
14022
14023   /* Traverse the limbo die list, and add parent/child links.  The only
14024      dies without parents that should be here are concrete instances of
14025      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14026      For concrete instances, we can get the parent die from the abstract
14027      instance.  */
14028   for (node = limbo_die_list; node; node = next_node)
14029     {
14030       next_node = node->next;
14031       die = node->die;
14032
14033       if (die->die_parent == NULL)
14034         {
14035           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14036
14037           if (origin)
14038             add_child_die (origin->die_parent, die);
14039           else if (die == comp_unit_die)
14040             ;
14041           else if (errorcount > 0 || sorrycount > 0)
14042             /* It's OK to be confused by errors in the input.  */
14043             add_child_die (comp_unit_die, die);
14044           else
14045             {
14046               /* In certain situations, the lexical block containing a
14047                  nested function can be optimized away, which results
14048                  in the nested function die being orphaned.  Likewise
14049                  with the return type of that nested function.  Force
14050                  this to be a child of the containing function.
14051
14052                  It may happen that even the containing function got fully
14053                  inlined and optimized out.  In that case we are lost and
14054                  assign the empty child.  This should not be big issue as
14055                  the function is likely unreachable too.  */
14056               tree context = NULL_TREE;
14057
14058               gcc_assert (node->created_for);
14059
14060               if (DECL_P (node->created_for))
14061                 context = DECL_CONTEXT (node->created_for);
14062               else if (TYPE_P (node->created_for))
14063                 context = TYPE_CONTEXT (node->created_for);
14064
14065               gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14066
14067               origin = lookup_decl_die (context);
14068               if (origin)
14069                 add_child_die (origin, die);
14070               else
14071                 add_child_die (comp_unit_die, die);
14072             }
14073         }
14074     }
14075
14076   limbo_die_list = NULL;
14077
14078   /* Walk through the list of incomplete types again, trying once more to
14079      emit full debugging info for them.  */
14080   retry_incomplete_types ();
14081
14082   /* We need to reverse all the dies before break_out_includes, or
14083      we'll see the end of an include file before the beginning.  */
14084   reverse_all_dies (comp_unit_die);
14085
14086   if (flag_eliminate_unused_debug_types)
14087     prune_unused_types ();
14088
14089   /* Generate separate CUs for each of the include files we've seen.
14090      They will go into limbo_die_list.  */
14091   if (flag_eliminate_dwarf2_dups)
14092     break_out_includes (comp_unit_die);
14093
14094   /* Traverse the DIE's and add add sibling attributes to those DIE's
14095      that have children.  */
14096   add_sibling_attributes (comp_unit_die);
14097   for (node = limbo_die_list; node; node = node->next)
14098     add_sibling_attributes (node->die);
14099
14100   /* Output a terminator label for the .text section.  */
14101   switch_to_section (text_section);
14102   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14103   if (flag_reorder_blocks_and_partition)
14104     {
14105       switch_to_section (unlikely_text_section ());
14106       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14107     }
14108
14109   /* Output the source line correspondence table.  We must do this
14110      even if there is no line information.  Otherwise, on an empty
14111      translation unit, we will generate a present, but empty,
14112      .debug_info section.  IRIX 6.5 `nm' will then complain when
14113      examining the file.  */
14114   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14115     {
14116       switch_to_section (debug_line_section);
14117       output_line_info ();
14118     }
14119
14120   /* We can only use the low/high_pc attributes if all of the code was
14121      in .text.  */
14122   if (!have_multiple_function_sections)
14123     {
14124       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14125       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14126     }
14127
14128   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14129      "base address".  Use zero so that these addresses become absolute.  */
14130   else if (have_location_lists || ranges_table_in_use)
14131     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14132
14133   /* Output location list section if necessary.  */
14134   if (have_location_lists)
14135     {
14136       /* Output the location lists info.  */
14137       switch_to_section (debug_loc_section);
14138       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14139                                    DEBUG_LOC_SECTION_LABEL, 0);
14140       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14141       output_location_lists (die);
14142     }
14143
14144   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14145     add_AT_lbl_offset (comp_unit_die, DW_AT_stmt_list,
14146                        debug_line_section_label);
14147
14148   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14149     add_AT_lbl_offset (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14150
14151   /* Output all of the compilation units.  We put the main one last so that
14152      the offsets are available to output_pubnames.  */
14153   for (node = limbo_die_list; node; node = node->next)
14154     output_comp_unit (node->die, 0);
14155
14156   output_comp_unit (comp_unit_die, 0);
14157
14158   /* Output the abbreviation table.  */
14159   switch_to_section (debug_abbrev_section);
14160   output_abbrev_section ();
14161
14162   /* Output public names table if necessary.  */
14163   if (pubname_table_in_use)
14164     {
14165       switch_to_section (debug_pubnames_section);
14166       output_pubnames ();
14167     }
14168
14169   /* Output the address range information.  We only put functions in the arange
14170      table, so don't write it out if we don't have any.  */
14171   if (fde_table_in_use)
14172     {
14173       switch_to_section (debug_aranges_section);
14174       output_aranges ();
14175     }
14176
14177   /* Output ranges section if necessary.  */
14178   if (ranges_table_in_use)
14179     {
14180       switch_to_section (debug_ranges_section);
14181       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14182       output_ranges ();
14183     }
14184
14185   /* Have to end the macro section.  */
14186   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14187     {
14188       switch_to_section (debug_macinfo_section);
14189       dw2_asm_output_data (1, 0, "End compilation unit");
14190     }
14191
14192   /* If we emitted any DW_FORM_strp form attribute, output the string
14193      table too.  */
14194   if (debug_str_hash)
14195     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14196 }
14197 #else
14198
14199 /* This should never be used, but its address is needed for comparisons.  */
14200 const struct gcc_debug_hooks dwarf2_debug_hooks;
14201
14202 #endif /* DWARF2_DEBUGGING_INFO */
14203
14204 #include "gt-dwarf2out.h"