OSDN Git Service

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