OSDN Git Service

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