OSDN Git Service

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