OSDN Git Service

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