OSDN Git Service

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