OSDN Git Service

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