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