OSDN Git Service

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