OSDN Git Service

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