OSDN Git Service

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