OSDN Git Service

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