OSDN Git Service

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