OSDN Git Service

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