OSDN Git Service

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