OSDN Git Service

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