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