OSDN Git Service

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